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