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