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