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