xref: /linux/net/netlink/genetlink.c (revision e1c4c5436b4ad579762fbe78bfabc8aef59bd5b1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NETLINK      Generic Netlink Family
4  *
5  * 		Authors:	Jamal Hadi Salim
6  * 				Thomas Graf <tgraf@suug.ch>
7  *				Johannes Berg <johannes@sipsolutions.net>
8  */
9 
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/socket.h>
16 #include <linux/string.h>
17 #include <linux/skbuff.h>
18 #include <linux/mutex.h>
19 #include <linux/bitmap.h>
20 #include <linux/rwsem.h>
21 #include <linux/idr.h>
22 #include <net/sock.h>
23 #include <net/genetlink.h>
24 
25 static DEFINE_MUTEX(genl_mutex); /* serialization of message processing */
26 static DECLARE_RWSEM(cb_lock);
27 
28 atomic_t genl_sk_destructing_cnt = ATOMIC_INIT(0);
29 DECLARE_WAIT_QUEUE_HEAD(genl_sk_destructing_waitq);
30 
31 void genl_lock(void)
32 {
33 	mutex_lock(&genl_mutex);
34 }
35 EXPORT_SYMBOL(genl_lock);
36 
37 void genl_unlock(void)
38 {
39 	mutex_unlock(&genl_mutex);
40 }
41 EXPORT_SYMBOL(genl_unlock);
42 
43 static void genl_lock_all(void)
44 {
45 	down_write(&cb_lock);
46 	genl_lock();
47 }
48 
49 static void genl_unlock_all(void)
50 {
51 	genl_unlock();
52 	up_write(&cb_lock);
53 }
54 
55 static DEFINE_IDR(genl_fam_idr);
56 
57 /*
58  * Bitmap of multicast groups that are currently in use.
59  *
60  * To avoid an allocation at boot of just one unsigned long,
61  * declare it global instead.
62  * Bit 0 is marked as already used since group 0 is invalid.
63  * Bit 1 is marked as already used since the drop-monitor code
64  * abuses the API and thinks it can statically use group 1.
65  * That group will typically conflict with other groups that
66  * any proper users use.
67  * Bit 16 is marked as used since it's used for generic netlink
68  * and the code no longer marks pre-reserved IDs as used.
69  * Bit 17 is marked as already used since the VFS quota code
70  * also abused this API and relied on family == group ID, we
71  * cater to that by giving it a static family and group ID.
72  * Bit 18 is marked as already used since the PMCRAID driver
73  * did the same thing as the VFS quota code (maybe copied?)
74  */
75 static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) |
76 				      BIT(GENL_ID_VFS_DQUOT) |
77 				      BIT(GENL_ID_PMCRAID);
78 static unsigned long *mc_groups = &mc_group_start;
79 static unsigned long mc_groups_longs = 1;
80 
81 static int genl_ctrl_event(int event, const struct genl_family *family,
82 			   const struct genl_multicast_group *grp,
83 			   int grp_id);
84 
85 static const struct genl_family *genl_family_find_byid(unsigned int id)
86 {
87 	return idr_find(&genl_fam_idr, id);
88 }
89 
90 static const struct genl_family *genl_family_find_byname(char *name)
91 {
92 	const struct genl_family *family;
93 	unsigned int id;
94 
95 	idr_for_each_entry(&genl_fam_idr, family, id)
96 		if (strcmp(family->name, name) == 0)
97 			return family;
98 
99 	return NULL;
100 }
101 
102 static int genl_get_cmd_cnt(const struct genl_family *family)
103 {
104 	return family->n_ops + family->n_small_ops;
105 }
106 
107 static void genl_op_from_full(const struct genl_family *family,
108 			      unsigned int i, struct genl_ops *op)
109 {
110 	*op = family->ops[i];
111 
112 	if (!op->maxattr)
113 		op->maxattr = family->maxattr;
114 	if (!op->policy)
115 		op->policy = family->policy;
116 }
117 
118 static int genl_get_cmd_full(u32 cmd, const struct genl_family *family,
119 			     struct genl_ops *op)
120 {
121 	int i;
122 
123 	for (i = 0; i < family->n_ops; i++)
124 		if (family->ops[i].cmd == cmd) {
125 			genl_op_from_full(family, i, op);
126 			return 0;
127 		}
128 
129 	return -ENOENT;
130 }
131 
132 static void genl_op_from_small(const struct genl_family *family,
133 			       unsigned int i, struct genl_ops *op)
134 {
135 	memset(op, 0, sizeof(*op));
136 	op->doit	= family->small_ops[i].doit;
137 	op->dumpit	= family->small_ops[i].dumpit;
138 	op->cmd		= family->small_ops[i].cmd;
139 	op->internal_flags = family->small_ops[i].internal_flags;
140 	op->flags	= family->small_ops[i].flags;
141 	op->validate	= family->small_ops[i].validate;
142 
143 	op->maxattr = family->maxattr;
144 	op->policy = family->policy;
145 }
146 
147 static int genl_get_cmd_small(u32 cmd, const struct genl_family *family,
148 			      struct genl_ops *op)
149 {
150 	int i;
151 
152 	for (i = 0; i < family->n_small_ops; i++)
153 		if (family->small_ops[i].cmd == cmd) {
154 			genl_op_from_small(family, i, op);
155 			return 0;
156 		}
157 
158 	return -ENOENT;
159 }
160 
161 static int genl_get_cmd(u32 cmd, const struct genl_family *family,
162 			struct genl_ops *op)
163 {
164 	if (!genl_get_cmd_full(cmd, family, op))
165 		return 0;
166 	return genl_get_cmd_small(cmd, family, op);
167 }
168 
169 static void genl_get_cmd_by_index(unsigned int i,
170 				  const struct genl_family *family,
171 				  struct genl_ops *op)
172 {
173 	if (i < family->n_ops)
174 		genl_op_from_full(family, i, op);
175 	else if (i < family->n_ops + family->n_small_ops)
176 		genl_op_from_small(family, i - family->n_ops, op);
177 	else
178 		WARN_ON_ONCE(1);
179 }
180 
181 static int genl_allocate_reserve_groups(int n_groups, int *first_id)
182 {
183 	unsigned long *new_groups;
184 	int start = 0;
185 	int i;
186 	int id;
187 	bool fits;
188 
189 	do {
190 		if (start == 0)
191 			id = find_first_zero_bit(mc_groups,
192 						 mc_groups_longs *
193 						 BITS_PER_LONG);
194 		else
195 			id = find_next_zero_bit(mc_groups,
196 						mc_groups_longs * BITS_PER_LONG,
197 						start);
198 
199 		fits = true;
200 		for (i = id;
201 		     i < min_t(int, id + n_groups,
202 			       mc_groups_longs * BITS_PER_LONG);
203 		     i++) {
204 			if (test_bit(i, mc_groups)) {
205 				start = i;
206 				fits = false;
207 				break;
208 			}
209 		}
210 
211 		if (id + n_groups > mc_groups_longs * BITS_PER_LONG) {
212 			unsigned long new_longs = mc_groups_longs +
213 						  BITS_TO_LONGS(n_groups);
214 			size_t nlen = new_longs * sizeof(unsigned long);
215 
216 			if (mc_groups == &mc_group_start) {
217 				new_groups = kzalloc(nlen, GFP_KERNEL);
218 				if (!new_groups)
219 					return -ENOMEM;
220 				mc_groups = new_groups;
221 				*mc_groups = mc_group_start;
222 			} else {
223 				new_groups = krealloc(mc_groups, nlen,
224 						      GFP_KERNEL);
225 				if (!new_groups)
226 					return -ENOMEM;
227 				mc_groups = new_groups;
228 				for (i = 0; i < BITS_TO_LONGS(n_groups); i++)
229 					mc_groups[mc_groups_longs + i] = 0;
230 			}
231 			mc_groups_longs = new_longs;
232 		}
233 	} while (!fits);
234 
235 	for (i = id; i < id + n_groups; i++)
236 		set_bit(i, mc_groups);
237 	*first_id = id;
238 	return 0;
239 }
240 
241 static struct genl_family genl_ctrl;
242 
243 static int genl_validate_assign_mc_groups(struct genl_family *family)
244 {
245 	int first_id;
246 	int n_groups = family->n_mcgrps;
247 	int err = 0, i;
248 	bool groups_allocated = false;
249 
250 	if (!n_groups)
251 		return 0;
252 
253 	for (i = 0; i < n_groups; i++) {
254 		const struct genl_multicast_group *grp = &family->mcgrps[i];
255 
256 		if (WARN_ON(grp->name[0] == '\0'))
257 			return -EINVAL;
258 		if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL))
259 			return -EINVAL;
260 	}
261 
262 	/* special-case our own group and hacks */
263 	if (family == &genl_ctrl) {
264 		first_id = GENL_ID_CTRL;
265 		BUG_ON(n_groups != 1);
266 	} else if (strcmp(family->name, "NET_DM") == 0) {
267 		first_id = 1;
268 		BUG_ON(n_groups != 1);
269 	} else if (family->id == GENL_ID_VFS_DQUOT) {
270 		first_id = GENL_ID_VFS_DQUOT;
271 		BUG_ON(n_groups != 1);
272 	} else if (family->id == GENL_ID_PMCRAID) {
273 		first_id = GENL_ID_PMCRAID;
274 		BUG_ON(n_groups != 1);
275 	} else {
276 		groups_allocated = true;
277 		err = genl_allocate_reserve_groups(n_groups, &first_id);
278 		if (err)
279 			return err;
280 	}
281 
282 	family->mcgrp_offset = first_id;
283 
284 	/* if still initializing, can't and don't need to realloc bitmaps */
285 	if (!init_net.genl_sock)
286 		return 0;
287 
288 	if (family->netnsok) {
289 		struct net *net;
290 
291 		netlink_table_grab();
292 		rcu_read_lock();
293 		for_each_net_rcu(net) {
294 			err = __netlink_change_ngroups(net->genl_sock,
295 					mc_groups_longs * BITS_PER_LONG);
296 			if (err) {
297 				/*
298 				 * No need to roll back, can only fail if
299 				 * memory allocation fails and then the
300 				 * number of _possible_ groups has been
301 				 * increased on some sockets which is ok.
302 				 */
303 				break;
304 			}
305 		}
306 		rcu_read_unlock();
307 		netlink_table_ungrab();
308 	} else {
309 		err = netlink_change_ngroups(init_net.genl_sock,
310 					     mc_groups_longs * BITS_PER_LONG);
311 	}
312 
313 	if (groups_allocated && err) {
314 		for (i = 0; i < family->n_mcgrps; i++)
315 			clear_bit(family->mcgrp_offset + i, mc_groups);
316 	}
317 
318 	return err;
319 }
320 
321 static void genl_unregister_mc_groups(const struct genl_family *family)
322 {
323 	struct net *net;
324 	int i;
325 
326 	netlink_table_grab();
327 	rcu_read_lock();
328 	for_each_net_rcu(net) {
329 		for (i = 0; i < family->n_mcgrps; i++)
330 			__netlink_clear_multicast_users(
331 				net->genl_sock, family->mcgrp_offset + i);
332 	}
333 	rcu_read_unlock();
334 	netlink_table_ungrab();
335 
336 	for (i = 0; i < family->n_mcgrps; i++) {
337 		int grp_id = family->mcgrp_offset + i;
338 
339 		if (grp_id != 1)
340 			clear_bit(grp_id, mc_groups);
341 		genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family,
342 				&family->mcgrps[i], grp_id);
343 	}
344 }
345 
346 static int genl_validate_ops(const struct genl_family *family)
347 {
348 	int i, j;
349 
350 	if (WARN_ON(family->n_ops && !family->ops) ||
351 	    WARN_ON(family->n_small_ops && !family->small_ops))
352 		return -EINVAL;
353 
354 	for (i = 0; i < genl_get_cmd_cnt(family); i++) {
355 		struct genl_ops op;
356 
357 		genl_get_cmd_by_index(i, family, &op);
358 		if (op.dumpit == NULL && op.doit == NULL)
359 			return -EINVAL;
360 		for (j = i + 1; j < genl_get_cmd_cnt(family); j++) {
361 			struct genl_ops op2;
362 
363 			genl_get_cmd_by_index(j, family, &op2);
364 			if (op.cmd == op2.cmd)
365 				return -EINVAL;
366 		}
367 	}
368 
369 	return 0;
370 }
371 
372 /**
373  * genl_register_family - register a generic netlink family
374  * @family: generic netlink family
375  *
376  * Registers the specified family after validating it first. Only one
377  * family may be registered with the same family name or identifier.
378  *
379  * The family's ops, multicast groups and module pointer must already
380  * be assigned.
381  *
382  * Return 0 on success or a negative error code.
383  */
384 int genl_register_family(struct genl_family *family)
385 {
386 	int err, i;
387 	int start = GENL_START_ALLOC, end = GENL_MAX_ID;
388 
389 	err = genl_validate_ops(family);
390 	if (err)
391 		return err;
392 
393 	genl_lock_all();
394 
395 	if (genl_family_find_byname(family->name)) {
396 		err = -EEXIST;
397 		goto errout_locked;
398 	}
399 
400 	/*
401 	 * Sadly, a few cases need to be special-cased
402 	 * due to them having previously abused the API
403 	 * and having used their family ID also as their
404 	 * multicast group ID, so we use reserved IDs
405 	 * for both to be sure we can do that mapping.
406 	 */
407 	if (family == &genl_ctrl) {
408 		/* and this needs to be special for initial family lookups */
409 		start = end = GENL_ID_CTRL;
410 	} else if (strcmp(family->name, "pmcraid") == 0) {
411 		start = end = GENL_ID_PMCRAID;
412 	} else if (strcmp(family->name, "VFS_DQUOT") == 0) {
413 		start = end = GENL_ID_VFS_DQUOT;
414 	}
415 
416 	family->id = idr_alloc_cyclic(&genl_fam_idr, family,
417 				      start, end + 1, GFP_KERNEL);
418 	if (family->id < 0) {
419 		err = family->id;
420 		goto errout_locked;
421 	}
422 
423 	err = genl_validate_assign_mc_groups(family);
424 	if (err)
425 		goto errout_remove;
426 
427 	genl_unlock_all();
428 
429 	/* send all events */
430 	genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
431 	for (i = 0; i < family->n_mcgrps; i++)
432 		genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
433 				&family->mcgrps[i], family->mcgrp_offset + i);
434 
435 	return 0;
436 
437 errout_remove:
438 	idr_remove(&genl_fam_idr, family->id);
439 errout_locked:
440 	genl_unlock_all();
441 	return err;
442 }
443 EXPORT_SYMBOL(genl_register_family);
444 
445 /**
446  * genl_unregister_family - unregister generic netlink family
447  * @family: generic netlink family
448  *
449  * Unregisters the specified family.
450  *
451  * Returns 0 on success or a negative error code.
452  */
453 int genl_unregister_family(const struct genl_family *family)
454 {
455 	genl_lock_all();
456 
457 	if (!genl_family_find_byid(family->id)) {
458 		genl_unlock_all();
459 		return -ENOENT;
460 	}
461 
462 	genl_unregister_mc_groups(family);
463 
464 	idr_remove(&genl_fam_idr, family->id);
465 
466 	up_write(&cb_lock);
467 	wait_event(genl_sk_destructing_waitq,
468 		   atomic_read(&genl_sk_destructing_cnt) == 0);
469 	genl_unlock();
470 
471 	genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
472 
473 	return 0;
474 }
475 EXPORT_SYMBOL(genl_unregister_family);
476 
477 /**
478  * genlmsg_put - Add generic netlink header to netlink message
479  * @skb: socket buffer holding the message
480  * @portid: netlink portid the message is addressed to
481  * @seq: sequence number (usually the one of the sender)
482  * @family: generic netlink family
483  * @flags: netlink message flags
484  * @cmd: generic netlink command
485  *
486  * Returns pointer to user specific header
487  */
488 void *genlmsg_put(struct sk_buff *skb, u32 portid, u32 seq,
489 		  const struct genl_family *family, int flags, u8 cmd)
490 {
491 	struct nlmsghdr *nlh;
492 	struct genlmsghdr *hdr;
493 
494 	nlh = nlmsg_put(skb, portid, seq, family->id, GENL_HDRLEN +
495 			family->hdrsize, flags);
496 	if (nlh == NULL)
497 		return NULL;
498 
499 	hdr = nlmsg_data(nlh);
500 	hdr->cmd = cmd;
501 	hdr->version = family->version;
502 	hdr->reserved = 0;
503 
504 	return (char *) hdr + GENL_HDRLEN;
505 }
506 EXPORT_SYMBOL(genlmsg_put);
507 
508 static struct genl_dumpit_info *genl_dumpit_info_alloc(void)
509 {
510 	return kmalloc(sizeof(struct genl_dumpit_info), GFP_KERNEL);
511 }
512 
513 static void genl_dumpit_info_free(const struct genl_dumpit_info *info)
514 {
515 	kfree(info);
516 }
517 
518 static struct nlattr **
519 genl_family_rcv_msg_attrs_parse(const struct genl_family *family,
520 				struct nlmsghdr *nlh,
521 				struct netlink_ext_ack *extack,
522 				const struct genl_ops *ops,
523 				int hdrlen,
524 				enum genl_validate_flags no_strict_flag)
525 {
526 	enum netlink_validation validate = ops->validate & no_strict_flag ?
527 					   NL_VALIDATE_LIBERAL :
528 					   NL_VALIDATE_STRICT;
529 	struct nlattr **attrbuf;
530 	int err;
531 
532 	if (!ops->maxattr)
533 		return NULL;
534 
535 	attrbuf = kmalloc_array(ops->maxattr + 1,
536 				sizeof(struct nlattr *), GFP_KERNEL);
537 	if (!attrbuf)
538 		return ERR_PTR(-ENOMEM);
539 
540 	err = __nlmsg_parse(nlh, hdrlen, attrbuf, ops->maxattr, ops->policy,
541 			    validate, extack);
542 	if (err) {
543 		kfree(attrbuf);
544 		return ERR_PTR(err);
545 	}
546 	return attrbuf;
547 }
548 
549 static void genl_family_rcv_msg_attrs_free(struct nlattr **attrbuf)
550 {
551 	kfree(attrbuf);
552 }
553 
554 struct genl_start_context {
555 	const struct genl_family *family;
556 	struct nlmsghdr *nlh;
557 	struct netlink_ext_ack *extack;
558 	const struct genl_ops *ops;
559 	int hdrlen;
560 };
561 
562 static int genl_start(struct netlink_callback *cb)
563 {
564 	struct genl_start_context *ctx = cb->data;
565 	const struct genl_ops *ops = ctx->ops;
566 	struct genl_dumpit_info *info;
567 	struct nlattr **attrs = NULL;
568 	int rc = 0;
569 
570 	if (ops->validate & GENL_DONT_VALIDATE_DUMP)
571 		goto no_attrs;
572 
573 	if (ctx->nlh->nlmsg_len < nlmsg_msg_size(ctx->hdrlen))
574 		return -EINVAL;
575 
576 	attrs = genl_family_rcv_msg_attrs_parse(ctx->family, ctx->nlh, ctx->extack,
577 						ops, ctx->hdrlen,
578 						GENL_DONT_VALIDATE_DUMP_STRICT);
579 	if (IS_ERR(attrs))
580 		return PTR_ERR(attrs);
581 
582 no_attrs:
583 	info = genl_dumpit_info_alloc();
584 	if (!info) {
585 		genl_family_rcv_msg_attrs_free(attrs);
586 		return -ENOMEM;
587 	}
588 	info->family = ctx->family;
589 	info->op = *ops;
590 	info->attrs = attrs;
591 
592 	cb->data = info;
593 	if (ops->start) {
594 		if (!ctx->family->parallel_ops)
595 			genl_lock();
596 		rc = ops->start(cb);
597 		if (!ctx->family->parallel_ops)
598 			genl_unlock();
599 	}
600 
601 	if (rc) {
602 		genl_family_rcv_msg_attrs_free(info->attrs);
603 		genl_dumpit_info_free(info);
604 		cb->data = NULL;
605 	}
606 	return rc;
607 }
608 
609 static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
610 {
611 	const struct genl_ops *ops = &genl_dumpit_info(cb)->op;
612 	int rc;
613 
614 	genl_lock();
615 	rc = ops->dumpit(skb, cb);
616 	genl_unlock();
617 	return rc;
618 }
619 
620 static int genl_lock_done(struct netlink_callback *cb)
621 {
622 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
623 	const struct genl_ops *ops = &info->op;
624 	int rc = 0;
625 
626 	if (ops->done) {
627 		genl_lock();
628 		rc = ops->done(cb);
629 		genl_unlock();
630 	}
631 	genl_family_rcv_msg_attrs_free(info->attrs);
632 	genl_dumpit_info_free(info);
633 	return rc;
634 }
635 
636 static int genl_parallel_done(struct netlink_callback *cb)
637 {
638 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
639 	const struct genl_ops *ops = &info->op;
640 	int rc = 0;
641 
642 	if (ops->done)
643 		rc = ops->done(cb);
644 	genl_family_rcv_msg_attrs_free(info->attrs);
645 	genl_dumpit_info_free(info);
646 	return rc;
647 }
648 
649 static int genl_family_rcv_msg_dumpit(const struct genl_family *family,
650 				      struct sk_buff *skb,
651 				      struct nlmsghdr *nlh,
652 				      struct netlink_ext_ack *extack,
653 				      const struct genl_ops *ops,
654 				      int hdrlen, struct net *net)
655 {
656 	struct genl_start_context ctx;
657 	int err;
658 
659 	if (!ops->dumpit)
660 		return -EOPNOTSUPP;
661 
662 	ctx.family = family;
663 	ctx.nlh = nlh;
664 	ctx.extack = extack;
665 	ctx.ops = ops;
666 	ctx.hdrlen = hdrlen;
667 
668 	if (!family->parallel_ops) {
669 		struct netlink_dump_control c = {
670 			.module = family->module,
671 			.data = &ctx,
672 			.start = genl_start,
673 			.dump = genl_lock_dumpit,
674 			.done = genl_lock_done,
675 		};
676 
677 		genl_unlock();
678 		err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
679 		genl_lock();
680 	} else {
681 		struct netlink_dump_control c = {
682 			.module = family->module,
683 			.data = &ctx,
684 			.start = genl_start,
685 			.dump = ops->dumpit,
686 			.done = genl_parallel_done,
687 		};
688 
689 		err = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
690 	}
691 
692 	return err;
693 }
694 
695 static int genl_family_rcv_msg_doit(const struct genl_family *family,
696 				    struct sk_buff *skb,
697 				    struct nlmsghdr *nlh,
698 				    struct netlink_ext_ack *extack,
699 				    const struct genl_ops *ops,
700 				    int hdrlen, struct net *net)
701 {
702 	struct nlattr **attrbuf;
703 	struct genl_info info;
704 	int err;
705 
706 	if (!ops->doit)
707 		return -EOPNOTSUPP;
708 
709 	attrbuf = genl_family_rcv_msg_attrs_parse(family, nlh, extack,
710 						  ops, hdrlen,
711 						  GENL_DONT_VALIDATE_STRICT);
712 	if (IS_ERR(attrbuf))
713 		return PTR_ERR(attrbuf);
714 
715 	info.snd_seq = nlh->nlmsg_seq;
716 	info.snd_portid = NETLINK_CB(skb).portid;
717 	info.nlhdr = nlh;
718 	info.genlhdr = nlmsg_data(nlh);
719 	info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
720 	info.attrs = attrbuf;
721 	info.extack = extack;
722 	genl_info_net_set(&info, net);
723 	memset(&info.user_ptr, 0, sizeof(info.user_ptr));
724 
725 	if (family->pre_doit) {
726 		err = family->pre_doit(ops, skb, &info);
727 		if (err)
728 			goto out;
729 	}
730 
731 	err = ops->doit(skb, &info);
732 
733 	if (family->post_doit)
734 		family->post_doit(ops, skb, &info);
735 
736 out:
737 	genl_family_rcv_msg_attrs_free(attrbuf);
738 
739 	return err;
740 }
741 
742 static int genl_header_check(const struct genl_family *family,
743 			     struct nlmsghdr *nlh, struct genlmsghdr *hdr,
744 			     struct netlink_ext_ack *extack)
745 {
746 	u16 flags;
747 
748 	/* Only for commands added after we started validating */
749 	if (hdr->cmd < family->resv_start_op)
750 		return 0;
751 
752 	if (hdr->reserved) {
753 		NL_SET_ERR_MSG(extack, "genlmsghdr.reserved field is not 0");
754 		return -EINVAL;
755 	}
756 
757 	/* Old netlink flags have pretty loose semantics, allow only the flags
758 	 * consumed by the core where we can enforce the meaning.
759 	 */
760 	flags = nlh->nlmsg_flags;
761 	if ((flags & NLM_F_DUMP) == NLM_F_DUMP) /* DUMP is 2 bits */
762 		flags &= ~NLM_F_DUMP;
763 	if (flags & ~(NLM_F_REQUEST | NLM_F_ACK | NLM_F_ECHO)) {
764 		NL_SET_ERR_MSG(extack,
765 			       "ambiguous or reserved bits set in nlmsg_flags");
766 		return -EINVAL;
767 	}
768 
769 	return 0;
770 }
771 
772 static int genl_family_rcv_msg(const struct genl_family *family,
773 			       struct sk_buff *skb,
774 			       struct nlmsghdr *nlh,
775 			       struct netlink_ext_ack *extack)
776 {
777 	struct net *net = sock_net(skb->sk);
778 	struct genlmsghdr *hdr = nlmsg_data(nlh);
779 	struct genl_ops op;
780 	int hdrlen;
781 
782 	/* this family doesn't exist in this netns */
783 	if (!family->netnsok && !net_eq(net, &init_net))
784 		return -ENOENT;
785 
786 	hdrlen = GENL_HDRLEN + family->hdrsize;
787 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
788 		return -EINVAL;
789 
790 	if (genl_header_check(family, nlh, hdr, extack))
791 		return -EINVAL;
792 
793 	if (genl_get_cmd(hdr->cmd, family, &op))
794 		return -EOPNOTSUPP;
795 
796 	if ((op.flags & GENL_ADMIN_PERM) &&
797 	    !netlink_capable(skb, CAP_NET_ADMIN))
798 		return -EPERM;
799 
800 	if ((op.flags & GENL_UNS_ADMIN_PERM) &&
801 	    !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
802 		return -EPERM;
803 
804 	if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP)
805 		return genl_family_rcv_msg_dumpit(family, skb, nlh, extack,
806 						  &op, hdrlen, net);
807 	else
808 		return genl_family_rcv_msg_doit(family, skb, nlh, extack,
809 						&op, hdrlen, net);
810 }
811 
812 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
813 			struct netlink_ext_ack *extack)
814 {
815 	const struct genl_family *family;
816 	int err;
817 
818 	family = genl_family_find_byid(nlh->nlmsg_type);
819 	if (family == NULL)
820 		return -ENOENT;
821 
822 	if (!family->parallel_ops)
823 		genl_lock();
824 
825 	err = genl_family_rcv_msg(family, skb, nlh, extack);
826 
827 	if (!family->parallel_ops)
828 		genl_unlock();
829 
830 	return err;
831 }
832 
833 static void genl_rcv(struct sk_buff *skb)
834 {
835 	down_read(&cb_lock);
836 	netlink_rcv_skb(skb, &genl_rcv_msg);
837 	up_read(&cb_lock);
838 }
839 
840 /**************************************************************************
841  * Controller
842  **************************************************************************/
843 
844 static struct genl_family genl_ctrl;
845 
846 static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
847 			  u32 flags, struct sk_buff *skb, u8 cmd)
848 {
849 	void *hdr;
850 
851 	hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
852 	if (hdr == NULL)
853 		return -1;
854 
855 	if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
856 	    nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
857 	    nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
858 	    nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
859 	    nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
860 		goto nla_put_failure;
861 
862 	if (genl_get_cmd_cnt(family)) {
863 		struct nlattr *nla_ops;
864 		int i;
865 
866 		nla_ops = nla_nest_start_noflag(skb, CTRL_ATTR_OPS);
867 		if (nla_ops == NULL)
868 			goto nla_put_failure;
869 
870 		for (i = 0; i < genl_get_cmd_cnt(family); i++) {
871 			struct nlattr *nest;
872 			struct genl_ops op;
873 			u32 op_flags;
874 
875 			genl_get_cmd_by_index(i, family, &op);
876 			op_flags = op.flags;
877 			if (op.dumpit)
878 				op_flags |= GENL_CMD_CAP_DUMP;
879 			if (op.doit)
880 				op_flags |= GENL_CMD_CAP_DO;
881 			if (op.policy)
882 				op_flags |= GENL_CMD_CAP_HASPOL;
883 
884 			nest = nla_nest_start_noflag(skb, i + 1);
885 			if (nest == NULL)
886 				goto nla_put_failure;
887 
888 			if (nla_put_u32(skb, CTRL_ATTR_OP_ID, op.cmd) ||
889 			    nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
890 				goto nla_put_failure;
891 
892 			nla_nest_end(skb, nest);
893 		}
894 
895 		nla_nest_end(skb, nla_ops);
896 	}
897 
898 	if (family->n_mcgrps) {
899 		struct nlattr *nla_grps;
900 		int i;
901 
902 		nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
903 		if (nla_grps == NULL)
904 			goto nla_put_failure;
905 
906 		for (i = 0; i < family->n_mcgrps; i++) {
907 			struct nlattr *nest;
908 			const struct genl_multicast_group *grp;
909 
910 			grp = &family->mcgrps[i];
911 
912 			nest = nla_nest_start_noflag(skb, i + 1);
913 			if (nest == NULL)
914 				goto nla_put_failure;
915 
916 			if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
917 					family->mcgrp_offset + i) ||
918 			    nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
919 					   grp->name))
920 				goto nla_put_failure;
921 
922 			nla_nest_end(skb, nest);
923 		}
924 		nla_nest_end(skb, nla_grps);
925 	}
926 
927 	genlmsg_end(skb, hdr);
928 	return 0;
929 
930 nla_put_failure:
931 	genlmsg_cancel(skb, hdr);
932 	return -EMSGSIZE;
933 }
934 
935 static int ctrl_fill_mcgrp_info(const struct genl_family *family,
936 				const struct genl_multicast_group *grp,
937 				int grp_id, u32 portid, u32 seq, u32 flags,
938 				struct sk_buff *skb, u8 cmd)
939 {
940 	void *hdr;
941 	struct nlattr *nla_grps;
942 	struct nlattr *nest;
943 
944 	hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
945 	if (hdr == NULL)
946 		return -1;
947 
948 	if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
949 	    nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
950 		goto nla_put_failure;
951 
952 	nla_grps = nla_nest_start_noflag(skb, CTRL_ATTR_MCAST_GROUPS);
953 	if (nla_grps == NULL)
954 		goto nla_put_failure;
955 
956 	nest = nla_nest_start_noflag(skb, 1);
957 	if (nest == NULL)
958 		goto nla_put_failure;
959 
960 	if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
961 	    nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
962 			   grp->name))
963 		goto nla_put_failure;
964 
965 	nla_nest_end(skb, nest);
966 	nla_nest_end(skb, nla_grps);
967 
968 	genlmsg_end(skb, hdr);
969 	return 0;
970 
971 nla_put_failure:
972 	genlmsg_cancel(skb, hdr);
973 	return -EMSGSIZE;
974 }
975 
976 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
977 {
978 	int n = 0;
979 	struct genl_family *rt;
980 	struct net *net = sock_net(skb->sk);
981 	int fams_to_skip = cb->args[0];
982 	unsigned int id;
983 
984 	idr_for_each_entry(&genl_fam_idr, rt, id) {
985 		if (!rt->netnsok && !net_eq(net, &init_net))
986 			continue;
987 
988 		if (n++ < fams_to_skip)
989 			continue;
990 
991 		if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
992 				   cb->nlh->nlmsg_seq, NLM_F_MULTI,
993 				   skb, CTRL_CMD_NEWFAMILY) < 0) {
994 			n--;
995 			break;
996 		}
997 	}
998 
999 	cb->args[0] = n;
1000 	return skb->len;
1001 }
1002 
1003 static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
1004 					     u32 portid, int seq, u8 cmd)
1005 {
1006 	struct sk_buff *skb;
1007 	int err;
1008 
1009 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1010 	if (skb == NULL)
1011 		return ERR_PTR(-ENOBUFS);
1012 
1013 	err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
1014 	if (err < 0) {
1015 		nlmsg_free(skb);
1016 		return ERR_PTR(err);
1017 	}
1018 
1019 	return skb;
1020 }
1021 
1022 static struct sk_buff *
1023 ctrl_build_mcgrp_msg(const struct genl_family *family,
1024 		     const struct genl_multicast_group *grp,
1025 		     int grp_id, u32 portid, int seq, u8 cmd)
1026 {
1027 	struct sk_buff *skb;
1028 	int err;
1029 
1030 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1031 	if (skb == NULL)
1032 		return ERR_PTR(-ENOBUFS);
1033 
1034 	err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
1035 				   seq, 0, skb, cmd);
1036 	if (err < 0) {
1037 		nlmsg_free(skb);
1038 		return ERR_PTR(err);
1039 	}
1040 
1041 	return skb;
1042 }
1043 
1044 static const struct nla_policy ctrl_policy_family[] = {
1045 	[CTRL_ATTR_FAMILY_ID]	= { .type = NLA_U16 },
1046 	[CTRL_ATTR_FAMILY_NAME]	= { .type = NLA_NUL_STRING,
1047 				    .len = GENL_NAMSIZ - 1 },
1048 };
1049 
1050 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
1051 {
1052 	struct sk_buff *msg;
1053 	const struct genl_family *res = NULL;
1054 	int err = -EINVAL;
1055 
1056 	if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
1057 		u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
1058 		res = genl_family_find_byid(id);
1059 		err = -ENOENT;
1060 	}
1061 
1062 	if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
1063 		char *name;
1064 
1065 		name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
1066 		res = genl_family_find_byname(name);
1067 #ifdef CONFIG_MODULES
1068 		if (res == NULL) {
1069 			genl_unlock();
1070 			up_read(&cb_lock);
1071 			request_module("net-pf-%d-proto-%d-family-%s",
1072 				       PF_NETLINK, NETLINK_GENERIC, name);
1073 			down_read(&cb_lock);
1074 			genl_lock();
1075 			res = genl_family_find_byname(name);
1076 		}
1077 #endif
1078 		err = -ENOENT;
1079 	}
1080 
1081 	if (res == NULL)
1082 		return err;
1083 
1084 	if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
1085 		/* family doesn't exist here */
1086 		return -ENOENT;
1087 	}
1088 
1089 	msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
1090 				    CTRL_CMD_NEWFAMILY);
1091 	if (IS_ERR(msg))
1092 		return PTR_ERR(msg);
1093 
1094 	return genlmsg_reply(msg, info);
1095 }
1096 
1097 static int genl_ctrl_event(int event, const struct genl_family *family,
1098 			   const struct genl_multicast_group *grp,
1099 			   int grp_id)
1100 {
1101 	struct sk_buff *msg;
1102 
1103 	/* genl is still initialising */
1104 	if (!init_net.genl_sock)
1105 		return 0;
1106 
1107 	switch (event) {
1108 	case CTRL_CMD_NEWFAMILY:
1109 	case CTRL_CMD_DELFAMILY:
1110 		WARN_ON(grp);
1111 		msg = ctrl_build_family_msg(family, 0, 0, event);
1112 		break;
1113 	case CTRL_CMD_NEWMCAST_GRP:
1114 	case CTRL_CMD_DELMCAST_GRP:
1115 		BUG_ON(!grp);
1116 		msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
1117 		break;
1118 	default:
1119 		return -EINVAL;
1120 	}
1121 
1122 	if (IS_ERR(msg))
1123 		return PTR_ERR(msg);
1124 
1125 	if (!family->netnsok) {
1126 		genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
1127 					0, GFP_KERNEL);
1128 	} else {
1129 		rcu_read_lock();
1130 		genlmsg_multicast_allns(&genl_ctrl, msg, 0,
1131 					0, GFP_ATOMIC);
1132 		rcu_read_unlock();
1133 	}
1134 
1135 	return 0;
1136 }
1137 
1138 struct ctrl_dump_policy_ctx {
1139 	struct netlink_policy_dump_state *state;
1140 	const struct genl_family *rt;
1141 	unsigned int opidx;
1142 	u32 op;
1143 	u16 fam_id;
1144 	u8 policies:1,
1145 	   single_op:1;
1146 };
1147 
1148 static const struct nla_policy ctrl_policy_policy[] = {
1149 	[CTRL_ATTR_FAMILY_ID]	= { .type = NLA_U16 },
1150 	[CTRL_ATTR_FAMILY_NAME]	= { .type = NLA_NUL_STRING,
1151 				    .len = GENL_NAMSIZ - 1 },
1152 	[CTRL_ATTR_OP]		= { .type = NLA_U32 },
1153 };
1154 
1155 static int ctrl_dumppolicy_start(struct netlink_callback *cb)
1156 {
1157 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
1158 	struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1159 	struct nlattr **tb = info->attrs;
1160 	const struct genl_family *rt;
1161 	struct genl_ops op;
1162 	int err, i;
1163 
1164 	BUILD_BUG_ON(sizeof(*ctx) > sizeof(cb->ctx));
1165 
1166 	if (!tb[CTRL_ATTR_FAMILY_ID] && !tb[CTRL_ATTR_FAMILY_NAME])
1167 		return -EINVAL;
1168 
1169 	if (tb[CTRL_ATTR_FAMILY_ID]) {
1170 		ctx->fam_id = nla_get_u16(tb[CTRL_ATTR_FAMILY_ID]);
1171 	} else {
1172 		rt = genl_family_find_byname(
1173 			nla_data(tb[CTRL_ATTR_FAMILY_NAME]));
1174 		if (!rt)
1175 			return -ENOENT;
1176 		ctx->fam_id = rt->id;
1177 	}
1178 
1179 	rt = genl_family_find_byid(ctx->fam_id);
1180 	if (!rt)
1181 		return -ENOENT;
1182 
1183 	ctx->rt = rt;
1184 
1185 	if (tb[CTRL_ATTR_OP]) {
1186 		ctx->single_op = true;
1187 		ctx->op = nla_get_u32(tb[CTRL_ATTR_OP]);
1188 
1189 		err = genl_get_cmd(ctx->op, rt, &op);
1190 		if (err) {
1191 			NL_SET_BAD_ATTR(cb->extack, tb[CTRL_ATTR_OP]);
1192 			return err;
1193 		}
1194 
1195 		if (!op.policy)
1196 			return -ENODATA;
1197 
1198 		return netlink_policy_dump_add_policy(&ctx->state, op.policy,
1199 						      op.maxattr);
1200 	}
1201 
1202 	for (i = 0; i < genl_get_cmd_cnt(rt); i++) {
1203 		genl_get_cmd_by_index(i, rt, &op);
1204 
1205 		if (op.policy) {
1206 			err = netlink_policy_dump_add_policy(&ctx->state,
1207 							     op.policy,
1208 							     op.maxattr);
1209 			if (err)
1210 				goto err_free_state;
1211 		}
1212 	}
1213 
1214 	if (!ctx->state)
1215 		return -ENODATA;
1216 	return 0;
1217 
1218 err_free_state:
1219 	netlink_policy_dump_free(ctx->state);
1220 	return err;
1221 }
1222 
1223 static void *ctrl_dumppolicy_prep(struct sk_buff *skb,
1224 				  struct netlink_callback *cb)
1225 {
1226 	struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1227 	void *hdr;
1228 
1229 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1230 			  cb->nlh->nlmsg_seq, &genl_ctrl,
1231 			  NLM_F_MULTI, CTRL_CMD_GETPOLICY);
1232 	if (!hdr)
1233 		return NULL;
1234 
1235 	if (nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, ctx->fam_id))
1236 		return NULL;
1237 
1238 	return hdr;
1239 }
1240 
1241 static int ctrl_dumppolicy_put_op(struct sk_buff *skb,
1242 				  struct netlink_callback *cb,
1243 			          struct genl_ops *op)
1244 {
1245 	struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1246 	struct nlattr *nest_pol, *nest_op;
1247 	void *hdr;
1248 	int idx;
1249 
1250 	/* skip if we have nothing to show */
1251 	if (!op->policy)
1252 		return 0;
1253 	if (!op->doit &&
1254 	    (!op->dumpit || op->validate & GENL_DONT_VALIDATE_DUMP))
1255 		return 0;
1256 
1257 	hdr = ctrl_dumppolicy_prep(skb, cb);
1258 	if (!hdr)
1259 		return -ENOBUFS;
1260 
1261 	nest_pol = nla_nest_start(skb, CTRL_ATTR_OP_POLICY);
1262 	if (!nest_pol)
1263 		goto err;
1264 
1265 	nest_op = nla_nest_start(skb, op->cmd);
1266 	if (!nest_op)
1267 		goto err;
1268 
1269 	/* for now both do/dump are always the same */
1270 	idx = netlink_policy_dump_get_policy_idx(ctx->state,
1271 						 op->policy,
1272 						 op->maxattr);
1273 
1274 	if (op->doit && nla_put_u32(skb, CTRL_ATTR_POLICY_DO, idx))
1275 		goto err;
1276 
1277 	if (op->dumpit && !(op->validate & GENL_DONT_VALIDATE_DUMP) &&
1278 	    nla_put_u32(skb, CTRL_ATTR_POLICY_DUMP, idx))
1279 		goto err;
1280 
1281 	nla_nest_end(skb, nest_op);
1282 	nla_nest_end(skb, nest_pol);
1283 	genlmsg_end(skb, hdr);
1284 
1285 	return 0;
1286 err:
1287 	genlmsg_cancel(skb, hdr);
1288 	return -ENOBUFS;
1289 }
1290 
1291 static int ctrl_dumppolicy(struct sk_buff *skb, struct netlink_callback *cb)
1292 {
1293 	struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1294 	void *hdr;
1295 
1296 	if (!ctx->policies) {
1297 		while (ctx->opidx < genl_get_cmd_cnt(ctx->rt)) {
1298 			struct genl_ops op;
1299 
1300 			if (ctx->single_op) {
1301 				int err;
1302 
1303 				err = genl_get_cmd(ctx->op, ctx->rt, &op);
1304 				if (WARN_ON(err))
1305 					return skb->len;
1306 
1307 				/* break out of the loop after this one */
1308 				ctx->opidx = genl_get_cmd_cnt(ctx->rt);
1309 			} else {
1310 				genl_get_cmd_by_index(ctx->opidx, ctx->rt, &op);
1311 			}
1312 
1313 			if (ctrl_dumppolicy_put_op(skb, cb, &op))
1314 				return skb->len;
1315 
1316 			ctx->opidx++;
1317 		}
1318 
1319 		/* completed with the per-op policy index list */
1320 		ctx->policies = true;
1321 	}
1322 
1323 	while (netlink_policy_dump_loop(ctx->state)) {
1324 		struct nlattr *nest;
1325 
1326 		hdr = ctrl_dumppolicy_prep(skb, cb);
1327 		if (!hdr)
1328 			goto nla_put_failure;
1329 
1330 		nest = nla_nest_start(skb, CTRL_ATTR_POLICY);
1331 		if (!nest)
1332 			goto nla_put_failure;
1333 
1334 		if (netlink_policy_dump_write(skb, ctx->state))
1335 			goto nla_put_failure;
1336 
1337 		nla_nest_end(skb, nest);
1338 
1339 		genlmsg_end(skb, hdr);
1340 	}
1341 
1342 	return skb->len;
1343 
1344 nla_put_failure:
1345 	genlmsg_cancel(skb, hdr);
1346 	return skb->len;
1347 }
1348 
1349 static int ctrl_dumppolicy_done(struct netlink_callback *cb)
1350 {
1351 	struct ctrl_dump_policy_ctx *ctx = (void *)cb->ctx;
1352 
1353 	netlink_policy_dump_free(ctx->state);
1354 	return 0;
1355 }
1356 
1357 static const struct genl_ops genl_ctrl_ops[] = {
1358 	{
1359 		.cmd		= CTRL_CMD_GETFAMILY,
1360 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1361 		.policy		= ctrl_policy_family,
1362 		.maxattr	= ARRAY_SIZE(ctrl_policy_family) - 1,
1363 		.doit		= ctrl_getfamily,
1364 		.dumpit		= ctrl_dumpfamily,
1365 	},
1366 	{
1367 		.cmd		= CTRL_CMD_GETPOLICY,
1368 		.policy		= ctrl_policy_policy,
1369 		.maxattr	= ARRAY_SIZE(ctrl_policy_policy) - 1,
1370 		.start		= ctrl_dumppolicy_start,
1371 		.dumpit		= ctrl_dumppolicy,
1372 		.done		= ctrl_dumppolicy_done,
1373 	},
1374 };
1375 
1376 static const struct genl_multicast_group genl_ctrl_groups[] = {
1377 	{ .name = "notify", },
1378 };
1379 
1380 static struct genl_family genl_ctrl __ro_after_init = {
1381 	.module = THIS_MODULE,
1382 	.ops = genl_ctrl_ops,
1383 	.n_ops = ARRAY_SIZE(genl_ctrl_ops),
1384 	.resv_start_op = CTRL_CMD_GETPOLICY + 1,
1385 	.mcgrps = genl_ctrl_groups,
1386 	.n_mcgrps = ARRAY_SIZE(genl_ctrl_groups),
1387 	.id = GENL_ID_CTRL,
1388 	.name = "nlctrl",
1389 	.version = 0x2,
1390 	.netnsok = true,
1391 };
1392 
1393 static int genl_bind(struct net *net, int group)
1394 {
1395 	const struct genl_family *family;
1396 	unsigned int id;
1397 	int ret = 0;
1398 
1399 	down_read(&cb_lock);
1400 
1401 	idr_for_each_entry(&genl_fam_idr, family, id) {
1402 		const struct genl_multicast_group *grp;
1403 		int i;
1404 
1405 		if (family->n_mcgrps == 0)
1406 			continue;
1407 
1408 		i = group - family->mcgrp_offset;
1409 		if (i < 0 || i >= family->n_mcgrps)
1410 			continue;
1411 
1412 		grp = &family->mcgrps[i];
1413 		if ((grp->flags & GENL_UNS_ADMIN_PERM) &&
1414 		    !ns_capable(net->user_ns, CAP_NET_ADMIN))
1415 			ret = -EPERM;
1416 
1417 		break;
1418 	}
1419 
1420 	up_read(&cb_lock);
1421 	return ret;
1422 }
1423 
1424 static int __net_init genl_pernet_init(struct net *net)
1425 {
1426 	struct netlink_kernel_cfg cfg = {
1427 		.input		= genl_rcv,
1428 		.flags		= NL_CFG_F_NONROOT_RECV,
1429 		.bind		= genl_bind,
1430 	};
1431 
1432 	/* we'll bump the group number right afterwards */
1433 	net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
1434 
1435 	if (!net->genl_sock && net_eq(net, &init_net))
1436 		panic("GENL: Cannot initialize generic netlink\n");
1437 
1438 	if (!net->genl_sock)
1439 		return -ENOMEM;
1440 
1441 	return 0;
1442 }
1443 
1444 static void __net_exit genl_pernet_exit(struct net *net)
1445 {
1446 	netlink_kernel_release(net->genl_sock);
1447 	net->genl_sock = NULL;
1448 }
1449 
1450 static struct pernet_operations genl_pernet_ops = {
1451 	.init = genl_pernet_init,
1452 	.exit = genl_pernet_exit,
1453 };
1454 
1455 static int __init genl_init(void)
1456 {
1457 	int err;
1458 
1459 	err = genl_register_family(&genl_ctrl);
1460 	if (err < 0)
1461 		goto problem;
1462 
1463 	err = register_pernet_subsys(&genl_pernet_ops);
1464 	if (err)
1465 		goto problem;
1466 
1467 	return 0;
1468 
1469 problem:
1470 	panic("GENL: Cannot register controller: %d\n", err);
1471 }
1472 
1473 core_initcall(genl_init);
1474 
1475 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
1476 			 gfp_t flags)
1477 {
1478 	struct sk_buff *tmp;
1479 	struct net *net, *prev = NULL;
1480 	bool delivered = false;
1481 	int err;
1482 
1483 	for_each_net_rcu(net) {
1484 		if (prev) {
1485 			tmp = skb_clone(skb, flags);
1486 			if (!tmp) {
1487 				err = -ENOMEM;
1488 				goto error;
1489 			}
1490 			err = nlmsg_multicast(prev->genl_sock, tmp,
1491 					      portid, group, flags);
1492 			if (!err)
1493 				delivered = true;
1494 			else if (err != -ESRCH)
1495 				goto error;
1496 		}
1497 
1498 		prev = net;
1499 	}
1500 
1501 	err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1502 	if (!err)
1503 		delivered = true;
1504 	else if (err != -ESRCH)
1505 		return err;
1506 	return delivered ? 0 : -ESRCH;
1507  error:
1508 	kfree_skb(skb);
1509 	return err;
1510 }
1511 
1512 int genlmsg_multicast_allns(const struct genl_family *family,
1513 			    struct sk_buff *skb, u32 portid,
1514 			    unsigned int group, gfp_t flags)
1515 {
1516 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
1517 		return -EINVAL;
1518 
1519 	group = family->mcgrp_offset + group;
1520 	return genlmsg_mcast(skb, portid, group, flags);
1521 }
1522 EXPORT_SYMBOL(genlmsg_multicast_allns);
1523 
1524 void genl_notify(const struct genl_family *family, struct sk_buff *skb,
1525 		 struct genl_info *info, u32 group, gfp_t flags)
1526 {
1527 	struct net *net = genl_info_net(info);
1528 	struct sock *sk = net->genl_sock;
1529 
1530 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
1531 		return;
1532 
1533 	group = family->mcgrp_offset + group;
1534 	nlmsg_notify(sk, skb, info->snd_portid, group,
1535 		     nlmsg_report(info->nlhdr), flags);
1536 }
1537 EXPORT_SYMBOL(genl_notify);
1538