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