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