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