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