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