]> Pileus Git - ~andy/linux/blob - net/netlink/genetlink.c
genl: Allow concurrent genl callbacks.
[~andy/linux] / net / netlink / genetlink.c
1 /*
2  * NETLINK      Generic Netlink Family
3  *
4  *              Authors:        Jamal Hadi Salim
5  *                              Thomas Graf <tgraf@suug.ch>
6  *                              Johannes Berg <johannes@sipsolutions.net>
7  */
8
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/errno.h>
13 #include <linux/types.h>
14 #include <linux/socket.h>
15 #include <linux/string.h>
16 #include <linux/skbuff.h>
17 #include <linux/mutex.h>
18 #include <linux/bitmap.h>
19 #include <linux/rwsem.h>
20 #include <net/sock.h>
21 #include <net/genetlink.h>
22
23 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
24 static DECLARE_RWSEM(cb_lock);
25
26 void genl_lock(void)
27 {
28         mutex_lock(&genl_mutex);
29 }
30 EXPORT_SYMBOL(genl_lock);
31
32 void genl_unlock(void)
33 {
34         mutex_unlock(&genl_mutex);
35 }
36 EXPORT_SYMBOL(genl_unlock);
37
38 #ifdef CONFIG_LOCKDEP
39 int lockdep_genl_is_held(void)
40 {
41         return lockdep_is_held(&genl_mutex);
42 }
43 EXPORT_SYMBOL(lockdep_genl_is_held);
44 #endif
45
46 static void genl_lock_all(void)
47 {
48         down_write(&cb_lock);
49         genl_lock();
50 }
51
52 static void genl_unlock_all(void)
53 {
54         genl_unlock();
55         up_write(&cb_lock);
56 }
57
58 #define GENL_FAM_TAB_SIZE       16
59 #define GENL_FAM_TAB_MASK       (GENL_FAM_TAB_SIZE - 1)
60
61 static struct list_head family_ht[GENL_FAM_TAB_SIZE];
62 /*
63  * Bitmap of multicast groups that are currently in use.
64  *
65  * To avoid an allocation at boot of just one unsigned long,
66  * declare it global instead.
67  * Bit 0 is marked as already used since group 0 is invalid.
68  */
69 static unsigned long mc_group_start = 0x1;
70 static unsigned long *mc_groups = &mc_group_start;
71 static unsigned long mc_groups_longs = 1;
72
73 static int genl_ctrl_event(int event, void *data);
74
75 static inline unsigned int genl_family_hash(unsigned int id)
76 {
77         return id & GENL_FAM_TAB_MASK;
78 }
79
80 static inline struct list_head *genl_family_chain(unsigned int id)
81 {
82         return &family_ht[genl_family_hash(id)];
83 }
84
85 static struct genl_family *genl_family_find_byid(unsigned int id)
86 {
87         struct genl_family *f;
88
89         list_for_each_entry(f, genl_family_chain(id), family_list)
90                 if (f->id == id)
91                         return f;
92
93         return NULL;
94 }
95
96 static struct genl_family *genl_family_find_byname(char *name)
97 {
98         struct genl_family *f;
99         int i;
100
101         for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
102                 list_for_each_entry(f, genl_family_chain(i), family_list)
103                         if (strcmp(f->name, name) == 0)
104                                 return f;
105
106         return NULL;
107 }
108
109 static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
110 {
111         struct genl_ops *ops;
112
113         list_for_each_entry(ops, &family->ops_list, ops_list)
114                 if (ops->cmd == cmd)
115                         return ops;
116
117         return NULL;
118 }
119
120 /* Of course we are going to have problems once we hit
121  * 2^16 alive types, but that can only happen by year 2K
122 */
123 static u16 genl_generate_id(void)
124 {
125         static u16 id_gen_idx = GENL_MIN_ID;
126         int i;
127
128         for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
129                 if (!genl_family_find_byid(id_gen_idx))
130                         return id_gen_idx;
131                 if (++id_gen_idx > GENL_MAX_ID)
132                         id_gen_idx = GENL_MIN_ID;
133         }
134
135         return 0;
136 }
137
138 static struct genl_multicast_group notify_grp;
139
140 /**
141  * genl_register_mc_group - register a multicast group
142  *
143  * Registers the specified multicast group and notifies userspace
144  * about the new group.
145  *
146  * Returns 0 on success or a negative error code.
147  *
148  * @family: The generic netlink family the group shall be registered for.
149  * @grp: The group to register, must have a name.
150  */
151 int genl_register_mc_group(struct genl_family *family,
152                            struct genl_multicast_group *grp)
153 {
154         int id;
155         unsigned long *new_groups;
156         int err = 0;
157
158         BUG_ON(grp->name[0] == '\0');
159         BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL);
160
161         genl_lock_all();
162
163         /* special-case our own group */
164         if (grp == &notify_grp)
165                 id = GENL_ID_CTRL;
166         else
167                 id = find_first_zero_bit(mc_groups,
168                                          mc_groups_longs * BITS_PER_LONG);
169
170
171         if (id >= mc_groups_longs * BITS_PER_LONG) {
172                 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long);
173
174                 if (mc_groups == &mc_group_start) {
175                         new_groups = kzalloc(nlen, GFP_KERNEL);
176                         if (!new_groups) {
177                                 err = -ENOMEM;
178                                 goto out;
179                         }
180                         mc_groups = new_groups;
181                         *mc_groups = mc_group_start;
182                 } else {
183                         new_groups = krealloc(mc_groups, nlen, GFP_KERNEL);
184                         if (!new_groups) {
185                                 err = -ENOMEM;
186                                 goto out;
187                         }
188                         mc_groups = new_groups;
189                         mc_groups[mc_groups_longs] = 0;
190                 }
191                 mc_groups_longs++;
192         }
193
194         if (family->netnsok) {
195                 struct net *net;
196
197                 netlink_table_grab();
198                 rcu_read_lock();
199                 for_each_net_rcu(net) {
200                         err = __netlink_change_ngroups(net->genl_sock,
201                                         mc_groups_longs * BITS_PER_LONG);
202                         if (err) {
203                                 /*
204                                  * No need to roll back, can only fail if
205                                  * memory allocation fails and then the
206                                  * number of _possible_ groups has been
207                                  * increased on some sockets which is ok.
208                                  */
209                                 rcu_read_unlock();
210                                 netlink_table_ungrab();
211                                 goto out;
212                         }
213                 }
214                 rcu_read_unlock();
215                 netlink_table_ungrab();
216         } else {
217                 err = netlink_change_ngroups(init_net.genl_sock,
218                                              mc_groups_longs * BITS_PER_LONG);
219                 if (err)
220                         goto out;
221         }
222
223         grp->id = id;
224         set_bit(id, mc_groups);
225         list_add_tail(&grp->list, &family->mcast_groups);
226         grp->family = family;
227
228         genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
229  out:
230         genl_unlock_all();
231         return err;
232 }
233 EXPORT_SYMBOL(genl_register_mc_group);
234
235 static void __genl_unregister_mc_group(struct genl_family *family,
236                                        struct genl_multicast_group *grp)
237 {
238         struct net *net;
239         BUG_ON(grp->family != family);
240
241         netlink_table_grab();
242         rcu_read_lock();
243         for_each_net_rcu(net)
244                 __netlink_clear_multicast_users(net->genl_sock, grp->id);
245         rcu_read_unlock();
246         netlink_table_ungrab();
247
248         clear_bit(grp->id, mc_groups);
249         list_del(&grp->list);
250         genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
251         grp->id = 0;
252         grp->family = NULL;
253 }
254
255 /**
256  * genl_unregister_mc_group - unregister a multicast group
257  *
258  * Unregisters the specified multicast group and notifies userspace
259  * about it. All current listeners on the group are removed.
260  *
261  * Note: It is not necessary to unregister all multicast groups before
262  *       unregistering the family, unregistering the family will cause
263  *       all assigned multicast groups to be unregistered automatically.
264  *
265  * @family: Generic netlink family the group belongs to.
266  * @grp: The group to unregister, must have been registered successfully
267  *       previously.
268  */
269 void genl_unregister_mc_group(struct genl_family *family,
270                               struct genl_multicast_group *grp)
271 {
272         genl_lock_all();
273         __genl_unregister_mc_group(family, grp);
274         genl_unlock_all();
275 }
276 EXPORT_SYMBOL(genl_unregister_mc_group);
277
278 static void genl_unregister_mc_groups(struct genl_family *family)
279 {
280         struct genl_multicast_group *grp, *tmp;
281
282         list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list)
283                 __genl_unregister_mc_group(family, grp);
284 }
285
286 /**
287  * genl_register_ops - register generic netlink operations
288  * @family: generic netlink family
289  * @ops: operations to be registered
290  *
291  * Registers the specified operations and assigns them to the specified
292  * family. Either a doit or dumpit callback must be specified or the
293  * operation will fail. Only one operation structure per command
294  * identifier may be registered.
295  *
296  * See include/net/genetlink.h for more documenation on the operations
297  * structure.
298  *
299  * Returns 0 on success or a negative error code.
300  */
301 int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
302 {
303         int err = -EINVAL;
304
305         if (ops->dumpit == NULL && ops->doit == NULL)
306                 goto errout;
307
308         if (genl_get_cmd(ops->cmd, family)) {
309                 err = -EEXIST;
310                 goto errout;
311         }
312
313         if (ops->dumpit)
314                 ops->flags |= GENL_CMD_CAP_DUMP;
315         if (ops->doit)
316                 ops->flags |= GENL_CMD_CAP_DO;
317         if (ops->policy)
318                 ops->flags |= GENL_CMD_CAP_HASPOL;
319
320         genl_lock_all();
321         list_add_tail(&ops->ops_list, &family->ops_list);
322         genl_unlock_all();
323
324         genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
325         err = 0;
326 errout:
327         return err;
328 }
329 EXPORT_SYMBOL(genl_register_ops);
330
331 /**
332  * genl_unregister_ops - unregister generic netlink operations
333  * @family: generic netlink family
334  * @ops: operations to be unregistered
335  *
336  * Unregisters the specified operations and unassigns them from the
337  * specified family. The operation blocks until the current message
338  * processing has finished and doesn't start again until the
339  * unregister process has finished.
340  *
341  * Note: It is not necessary to unregister all operations before
342  *       unregistering the family, unregistering the family will cause
343  *       all assigned operations to be unregistered automatically.
344  *
345  * Returns 0 on success or a negative error code.
346  */
347 int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
348 {
349         struct genl_ops *rc;
350
351         genl_lock_all();
352         list_for_each_entry(rc, &family->ops_list, ops_list) {
353                 if (rc == ops) {
354                         list_del(&ops->ops_list);
355                         genl_unlock_all();
356                         genl_ctrl_event(CTRL_CMD_DELOPS, ops);
357                         return 0;
358                 }
359         }
360         genl_unlock_all();
361
362         return -ENOENT;
363 }
364 EXPORT_SYMBOL(genl_unregister_ops);
365
366 /**
367  * genl_register_family - register a generic netlink family
368  * @family: generic netlink family
369  *
370  * Registers the specified family after validating it first. Only one
371  * family may be registered with the same family name or identifier.
372  * The family id may equal GENL_ID_GENERATE causing an unique id to
373  * be automatically generated and assigned.
374  *
375  * Return 0 on success or a negative error code.
376  */
377 int genl_register_family(struct genl_family *family)
378 {
379         int err = -EINVAL;
380
381         if (family->id && family->id < GENL_MIN_ID)
382                 goto errout;
383
384         if (family->id > GENL_MAX_ID)
385                 goto errout;
386
387         INIT_LIST_HEAD(&family->ops_list);
388         INIT_LIST_HEAD(&family->mcast_groups);
389
390         genl_lock_all();
391
392         if (genl_family_find_byname(family->name)) {
393                 err = -EEXIST;
394                 goto errout_locked;
395         }
396
397         if (family->id == GENL_ID_GENERATE) {
398                 u16 newid = genl_generate_id();
399
400                 if (!newid) {
401                         err = -ENOMEM;
402                         goto errout_locked;
403                 }
404
405                 family->id = newid;
406         } else if (genl_family_find_byid(family->id)) {
407                 err = -EEXIST;
408                 goto errout_locked;
409         }
410
411         if (family->maxattr && !family->parallel_ops) {
412                 family->attrbuf = kmalloc((family->maxattr+1) *
413                                         sizeof(struct nlattr *), GFP_KERNEL);
414                 if (family->attrbuf == NULL) {
415                         err = -ENOMEM;
416                         goto errout_locked;
417                 }
418         } else
419                 family->attrbuf = NULL;
420
421         list_add_tail(&family->family_list, genl_family_chain(family->id));
422         genl_unlock_all();
423
424         genl_ctrl_event(CTRL_CMD_NEWFAMILY, family);
425
426         return 0;
427
428 errout_locked:
429         genl_unlock_all();
430 errout:
431         return err;
432 }
433 EXPORT_SYMBOL(genl_register_family);
434
435 /**
436  * genl_register_family_with_ops - register a generic netlink family
437  * @family: generic netlink family
438  * @ops: operations to be registered
439  * @n_ops: number of elements to register
440  *
441  * Registers the specified family and operations from the specified table.
442  * Only one family may be registered with the same family name or identifier.
443  *
444  * The family id may equal GENL_ID_GENERATE causing an unique id to
445  * be automatically generated and assigned.
446  *
447  * Either a doit or dumpit callback must be specified for every registered
448  * operation or the function will fail. Only one operation structure per
449  * command identifier may be registered.
450  *
451  * See include/net/genetlink.h for more documenation on the operations
452  * structure.
453  *
454  * This is equivalent to calling genl_register_family() followed by
455  * genl_register_ops() for every operation entry in the table taking
456  * care to unregister the family on error path.
457  *
458  * Return 0 on success or a negative error code.
459  */
460 int genl_register_family_with_ops(struct genl_family *family,
461         struct genl_ops *ops, size_t n_ops)
462 {
463         int err, i;
464
465         err = genl_register_family(family);
466         if (err)
467                 return err;
468
469         for (i = 0; i < n_ops; ++i, ++ops) {
470                 err = genl_register_ops(family, ops);
471                 if (err)
472                         goto err_out;
473         }
474         return 0;
475 err_out:
476         genl_unregister_family(family);
477         return err;
478 }
479 EXPORT_SYMBOL(genl_register_family_with_ops);
480
481 /**
482  * genl_unregister_family - unregister generic netlink family
483  * @family: generic netlink family
484  *
485  * Unregisters the specified family.
486  *
487  * Returns 0 on success or a negative error code.
488  */
489 int genl_unregister_family(struct genl_family *family)
490 {
491         struct genl_family *rc;
492
493         genl_lock_all();
494
495         genl_unregister_mc_groups(family);
496
497         list_for_each_entry(rc, genl_family_chain(family->id), family_list) {
498                 if (family->id != rc->id || strcmp(rc->name, family->name))
499                         continue;
500
501                 list_del(&rc->family_list);
502                 INIT_LIST_HEAD(&family->ops_list);
503                 genl_unlock_all();
504
505                 kfree(family->attrbuf);
506                 genl_ctrl_event(CTRL_CMD_DELFAMILY, family);
507                 return 0;
508         }
509
510         genl_unlock_all();
511
512         return -ENOENT;
513 }
514 EXPORT_SYMBOL(genl_unregister_family);
515
516 /**
517  * genlmsg_put - Add generic netlink header to netlink message
518  * @skb: socket buffer holding the message
519  * @portid: netlink portid the message is addressed to
520  * @seq: sequence number (usually the one of the sender)
521  * @family: generic netlink family
522  * @flags: netlink message flags
523  * @cmd: generic netlink command
524  *
525  * Returns pointer to user specific header
526  */
527 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
528                                 struct genl_family *family, int flags, u8 cmd)
529 {
530         struct nlmsghdr *nlh;
531         struct genlmsghdr *hdr;
532
533         nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
534                         family->hdrsize, flags);
535         if (nlh == NULL)
536                 return NULL;
537
538         hdr = nlmsg_data(nlh);
539         hdr->cmd = cmd;
540         hdr->version = family->version;
541         hdr->reserved = 0;
542
543         return (char *) hdr + GENL_HDRLEN;
544 }
545 EXPORT_SYMBOL(genlmsg_put);
546
547 static int genl_family_rcv_msg(struct genl_family *family,
548                                struct sk_buff *skb,
549                                struct nlmsghdr *nlh)
550 {
551         struct genl_ops *ops;
552         struct net *net = sock_net(skb->sk);
553         struct genl_info info;
554         struct genlmsghdr *hdr = nlmsg_data(nlh);
555         struct nlattr **attrbuf;
556         int hdrlen, err;
557
558         /* this family doesn't exist in this netns */
559         if (!family->netnsok && !net_eq(net, &init_net))
560                 return -ENOENT;
561
562         hdrlen = GENL_HDRLEN + family->hdrsize;
563         if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
564                 return -EINVAL;
565
566         ops = genl_get_cmd(hdr->cmd, family);
567         if (ops == NULL)
568                 return -EOPNOTSUPP;
569
570         if ((ops->flags & GENL_ADMIN_PERM) &&
571             !capable(CAP_NET_ADMIN))
572                 return -EPERM;
573
574         if (nlh->nlmsg_flags & NLM_F_DUMP) {
575                 struct netlink_dump_control c = {
576                         .dump = ops->dumpit,
577                         .done = ops->done,
578                 };
579
580                 if (ops->dumpit == NULL)
581                         return -EOPNOTSUPP;
582
583                 return netlink_dump_start(net->genl_sock, skb, nlh, &c);
584         }
585
586         if (ops->doit == NULL)
587                 return -EOPNOTSUPP;
588
589         if (family->maxattr && family->parallel_ops) {
590                 attrbuf = kmalloc((family->maxattr+1) *
591                                         sizeof(struct nlattr *), GFP_KERNEL);
592                 if (attrbuf == NULL)
593                         return -ENOMEM;
594         } else
595                 attrbuf = family->attrbuf;
596
597         if (attrbuf) {
598                 err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
599                                   ops->policy);
600                 if (err < 0)
601                         return err;
602         }
603
604         info.snd_seq = nlh->nlmsg_seq;
605         info.snd_portid = NETLINK_CB(skb).portid;
606         info.nlhdr = nlh;
607         info.genlhdr = nlmsg_data(nlh);
608         info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
609         info.attrs = attrbuf;
610         genl_info_net_set(&info, net);
611         memset(&info.user_ptr, 0, sizeof(info.user_ptr));
612
613         if (family->pre_doit) {
614                 err = family->pre_doit(ops, skb, &info);
615                 if (err)
616                         return err;
617         }
618
619         err = ops->doit(skb, &info);
620
621         if (family->post_doit)
622                 family->post_doit(ops, skb, &info);
623
624         if (family->parallel_ops)
625                 kfree(attrbuf);
626
627         return err;
628 }
629
630 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
631 {
632         struct genl_family *family;
633         int err;
634
635         family = genl_family_find_byid(nlh->nlmsg_type);
636         if (family == NULL)
637                 return -ENOENT;
638
639         if (!family->parallel_ops)
640                 genl_lock();
641
642         err = genl_family_rcv_msg(family, skb, nlh);
643
644         if (!family->parallel_ops)
645                 genl_unlock();
646
647         return err;
648 }
649
650 static void genl_rcv(struct sk_buff *skb)
651 {
652         down_read(&cb_lock);
653         netlink_rcv_skb(skb, &genl_rcv_msg);
654         up_read(&cb_lock);
655 }
656
657 /**************************************************************************
658  * Controller
659  **************************************************************************/
660
661 static struct genl_family genl_ctrl = {
662         .id = GENL_ID_CTRL,
663         .name = "nlctrl",
664         .version = 0x2,
665         .maxattr = CTRL_ATTR_MAX,
666         .netnsok = true,
667 };
668
669 static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
670                           u32 flags, struct sk_buff *skb, u8 cmd)
671 {
672         void *hdr;
673
674         hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
675         if (hdr == NULL)
676                 return -1;
677
678         if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
679             nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
680             nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
681             nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
682             nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
683                 goto nla_put_failure;
684
685         if (!list_empty(&family->ops_list)) {
686                 struct nlattr *nla_ops;
687                 struct genl_ops *ops;
688                 int idx = 1;
689
690                 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
691                 if (nla_ops == NULL)
692                         goto nla_put_failure;
693
694                 list_for_each_entry(ops, &family->ops_list, ops_list) {
695                         struct nlattr *nest;
696
697                         nest = nla_nest_start(skb, idx++);
698                         if (nest == NULL)
699                                 goto nla_put_failure;
700
701                         if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
702                             nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, ops->flags))
703                                 goto nla_put_failure;
704
705                         nla_nest_end(skb, nest);
706                 }
707
708                 nla_nest_end(skb, nla_ops);
709         }
710
711         if (!list_empty(&family->mcast_groups)) {
712                 struct genl_multicast_group *grp;
713                 struct nlattr *nla_grps;
714                 int idx = 1;
715
716                 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
717                 if (nla_grps == NULL)
718                         goto nla_put_failure;
719
720                 list_for_each_entry(grp, &family->mcast_groups, list) {
721                         struct nlattr *nest;
722
723                         nest = nla_nest_start(skb, idx++);
724                         if (nest == NULL)
725                                 goto nla_put_failure;
726
727                         if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
728                             nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
729                                            grp->name))
730                                 goto nla_put_failure;
731
732                         nla_nest_end(skb, nest);
733                 }
734                 nla_nest_end(skb, nla_grps);
735         }
736
737         return genlmsg_end(skb, hdr);
738
739 nla_put_failure:
740         genlmsg_cancel(skb, hdr);
741         return -EMSGSIZE;
742 }
743
744 static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
745                                 u32 seq, u32 flags, struct sk_buff *skb,
746                                 u8 cmd)
747 {
748         void *hdr;
749         struct nlattr *nla_grps;
750         struct nlattr *nest;
751
752         hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
753         if (hdr == NULL)
754                 return -1;
755
756         if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) ||
757             nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id))
758                 goto nla_put_failure;
759
760         nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
761         if (nla_grps == NULL)
762                 goto nla_put_failure;
763
764         nest = nla_nest_start(skb, 1);
765         if (nest == NULL)
766                 goto nla_put_failure;
767
768         if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) ||
769             nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
770                            grp->name))
771                 goto nla_put_failure;
772
773         nla_nest_end(skb, nest);
774         nla_nest_end(skb, nla_grps);
775
776         return genlmsg_end(skb, hdr);
777
778 nla_put_failure:
779         genlmsg_cancel(skb, hdr);
780         return -EMSGSIZE;
781 }
782
783 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
784 {
785
786         int i, n = 0;
787         struct genl_family *rt;
788         struct net *net = sock_net(skb->sk);
789         int chains_to_skip = cb->args[0];
790         int fams_to_skip = cb->args[1];
791
792         for (i = chains_to_skip; i < GENL_FAM_TAB_SIZE; i++) {
793                 n = 0;
794                 list_for_each_entry(rt, genl_family_chain(i), family_list) {
795                         if (!rt->netnsok && !net_eq(net, &init_net))
796                                 continue;
797                         if (++n < fams_to_skip)
798                                 continue;
799                         if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
800                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
801                                            skb, CTRL_CMD_NEWFAMILY) < 0)
802                                 goto errout;
803                 }
804
805                 fams_to_skip = 0;
806         }
807
808 errout:
809         cb->args[0] = i;
810         cb->args[1] = n;
811
812         return skb->len;
813 }
814
815 static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
816                                              u32 portid, int seq, u8 cmd)
817 {
818         struct sk_buff *skb;
819         int err;
820
821         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
822         if (skb == NULL)
823                 return ERR_PTR(-ENOBUFS);
824
825         err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
826         if (err < 0) {
827                 nlmsg_free(skb);
828                 return ERR_PTR(err);
829         }
830
831         return skb;
832 }
833
834 static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
835                                             u32 portid, int seq, u8 cmd)
836 {
837         struct sk_buff *skb;
838         int err;
839
840         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
841         if (skb == NULL)
842                 return ERR_PTR(-ENOBUFS);
843
844         err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd);
845         if (err < 0) {
846                 nlmsg_free(skb);
847                 return ERR_PTR(err);
848         }
849
850         return skb;
851 }
852
853 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
854         [CTRL_ATTR_FAMILY_ID]   = { .type = NLA_U16 },
855         [CTRL_ATTR_FAMILY_NAME] = { .type = NLA_NUL_STRING,
856                                     .len = GENL_NAMSIZ - 1 },
857 };
858
859 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
860 {
861         struct sk_buff *msg;
862         struct genl_family *res = NULL;
863         int err = -EINVAL;
864
865         if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
866                 u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
867                 res = genl_family_find_byid(id);
868                 err = -ENOENT;
869         }
870
871         if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
872                 char *name;
873
874                 name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
875                 res = genl_family_find_byname(name);
876 #ifdef CONFIG_MODULES
877                 if (res == NULL) {
878                         genl_unlock();
879                         request_module("net-pf-%d-proto-%d-family-%s",
880                                        PF_NETLINK, NETLINK_GENERIC, name);
881                         genl_lock();
882                         res = genl_family_find_byname(name);
883                 }
884 #endif
885                 err = -ENOENT;
886         }
887
888         if (res == NULL)
889                 return err;
890
891         if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
892                 /* family doesn't exist here */
893                 return -ENOENT;
894         }
895
896         msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
897                                     CTRL_CMD_NEWFAMILY);
898         if (IS_ERR(msg))
899                 return PTR_ERR(msg);
900
901         return genlmsg_reply(msg, info);
902 }
903
904 static int genl_ctrl_event(int event, void *data)
905 {
906         struct sk_buff *msg;
907         struct genl_family *family;
908         struct genl_multicast_group *grp;
909
910         /* genl is still initialising */
911         if (!init_net.genl_sock)
912                 return 0;
913
914         switch (event) {
915         case CTRL_CMD_NEWFAMILY:
916         case CTRL_CMD_DELFAMILY:
917                 family = data;
918                 msg = ctrl_build_family_msg(family, 0, 0, event);
919                 break;
920         case CTRL_CMD_NEWMCAST_GRP:
921         case CTRL_CMD_DELMCAST_GRP:
922                 grp = data;
923                 family = grp->family;
924                 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
925                 break;
926         default:
927                 return -EINVAL;
928         }
929
930         if (IS_ERR(msg))
931                 return PTR_ERR(msg);
932
933         if (!family->netnsok) {
934                 genlmsg_multicast_netns(&init_net, msg, 0,
935                                         GENL_ID_CTRL, GFP_KERNEL);
936         } else {
937                 rcu_read_lock();
938                 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC);
939                 rcu_read_unlock();
940         }
941
942         return 0;
943 }
944
945 static struct genl_ops genl_ctrl_ops = {
946         .cmd            = CTRL_CMD_GETFAMILY,
947         .doit           = ctrl_getfamily,
948         .dumpit         = ctrl_dumpfamily,
949         .policy         = ctrl_policy,
950 };
951
952 static struct genl_multicast_group notify_grp = {
953         .name           = "notify",
954 };
955
956 static int __net_init genl_pernet_init(struct net *net)
957 {
958         struct netlink_kernel_cfg cfg = {
959                 .input          = genl_rcv,
960                 .flags          = NL_CFG_F_NONROOT_RECV,
961         };
962
963         /* we'll bump the group number right afterwards */
964         net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
965
966         if (!net->genl_sock && net_eq(net, &init_net))
967                 panic("GENL: Cannot initialize generic netlink\n");
968
969         if (!net->genl_sock)
970                 return -ENOMEM;
971
972         return 0;
973 }
974
975 static void __net_exit genl_pernet_exit(struct net *net)
976 {
977         netlink_kernel_release(net->genl_sock);
978         net->genl_sock = NULL;
979 }
980
981 static struct pernet_operations genl_pernet_ops = {
982         .init = genl_pernet_init,
983         .exit = genl_pernet_exit,
984 };
985
986 static int __init genl_init(void)
987 {
988         int i, err;
989
990         for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
991                 INIT_LIST_HEAD(&family_ht[i]);
992
993         err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1);
994         if (err < 0)
995                 goto problem;
996
997         err = register_pernet_subsys(&genl_pernet_ops);
998         if (err)
999                 goto problem;
1000
1001         err = genl_register_mc_group(&genl_ctrl, &notify_grp);
1002         if (err < 0)
1003                 goto problem;
1004
1005         return 0;
1006
1007 problem:
1008         panic("GENL: Cannot register controller: %d\n", err);
1009 }
1010
1011 subsys_initcall(genl_init);
1012
1013 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
1014                          gfp_t flags)
1015 {
1016         struct sk_buff *tmp;
1017         struct net *net, *prev = NULL;
1018         int err;
1019
1020         for_each_net_rcu(net) {
1021                 if (prev) {
1022                         tmp = skb_clone(skb, flags);
1023                         if (!tmp) {
1024                                 err = -ENOMEM;
1025                                 goto error;
1026                         }
1027                         err = nlmsg_multicast(prev->genl_sock, tmp,
1028                                               portid, group, flags);
1029                         if (err)
1030                                 goto error;
1031                 }
1032
1033                 prev = net;
1034         }
1035
1036         return nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1037  error:
1038         kfree_skb(skb);
1039         return err;
1040 }
1041
1042 int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group,
1043                             gfp_t flags)
1044 {
1045         return genlmsg_mcast(skb, portid, group, flags);
1046 }
1047 EXPORT_SYMBOL(genlmsg_multicast_allns);
1048
1049 void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
1050                  struct nlmsghdr *nlh, gfp_t flags)
1051 {
1052         struct sock *sk = net->genl_sock;
1053         int report = 0;
1054
1055         if (nlh)
1056                 report = nlmsg_report(nlh);
1057
1058         nlmsg_notify(sk, skb, portid, group, report, flags);
1059 }
1060 EXPORT_SYMBOL(genl_notify);