xref: /openbmc/linux/net/netlink/genetlink.c (revision e657c18a)
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(&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 int genl_lock_start(struct netlink_callback *cb)
462 {
463 	/* our ops are always const - netlink API doesn't propagate that */
464 	const struct genl_ops *ops = cb->data;
465 	int rc = 0;
466 
467 	if (ops->start) {
468 		genl_lock();
469 		rc = ops->start(cb);
470 		genl_unlock();
471 	}
472 	return rc;
473 }
474 
475 static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
476 {
477 	/* our ops are always const - netlink API doesn't propagate that */
478 	const struct genl_ops *ops = cb->data;
479 	int rc;
480 
481 	genl_lock();
482 	rc = ops->dumpit(skb, cb);
483 	genl_unlock();
484 	return rc;
485 }
486 
487 static int genl_lock_done(struct netlink_callback *cb)
488 {
489 	/* our ops are always const - netlink API doesn't propagate that */
490 	const struct genl_ops *ops = cb->data;
491 	int rc = 0;
492 
493 	if (ops->done) {
494 		genl_lock();
495 		rc = ops->done(cb);
496 		genl_unlock();
497 	}
498 	return rc;
499 }
500 
501 static int genl_family_rcv_msg(const struct genl_family *family,
502 			       struct sk_buff *skb,
503 			       struct nlmsghdr *nlh,
504 			       struct netlink_ext_ack *extack)
505 {
506 	const struct genl_ops *ops;
507 	struct net *net = sock_net(skb->sk);
508 	struct genl_info info;
509 	struct genlmsghdr *hdr = nlmsg_data(nlh);
510 	struct nlattr **attrbuf;
511 	int hdrlen, err;
512 
513 	/* this family doesn't exist in this netns */
514 	if (!family->netnsok && !net_eq(net, &init_net))
515 		return -ENOENT;
516 
517 	hdrlen = GENL_HDRLEN + family->hdrsize;
518 	if (nlh->nlmsg_len < nlmsg_msg_size(hdrlen))
519 		return -EINVAL;
520 
521 	ops = genl_get_cmd(hdr->cmd, family);
522 	if (ops == NULL)
523 		return -EOPNOTSUPP;
524 
525 	if ((ops->flags & GENL_ADMIN_PERM) &&
526 	    !netlink_capable(skb, CAP_NET_ADMIN))
527 		return -EPERM;
528 
529 	if ((ops->flags & GENL_UNS_ADMIN_PERM) &&
530 	    !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN))
531 		return -EPERM;
532 
533 	if ((nlh->nlmsg_flags & NLM_F_DUMP) == NLM_F_DUMP) {
534 		int rc;
535 
536 		if (ops->dumpit == NULL)
537 			return -EOPNOTSUPP;
538 
539 		if (!family->parallel_ops) {
540 			struct netlink_dump_control c = {
541 				.module = family->module,
542 				/* we have const, but the netlink API doesn't */
543 				.data = (void *)ops,
544 				.start = genl_lock_start,
545 				.dump = genl_lock_dumpit,
546 				.done = genl_lock_done,
547 			};
548 
549 			genl_unlock();
550 			rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
551 			genl_lock();
552 
553 		} else {
554 			struct netlink_dump_control c = {
555 				.module = family->module,
556 				.start = ops->start,
557 				.dump = ops->dumpit,
558 				.done = ops->done,
559 			};
560 
561 			rc = __netlink_dump_start(net->genl_sock, skb, nlh, &c);
562 		}
563 
564 		return rc;
565 	}
566 
567 	if (ops->doit == NULL)
568 		return -EOPNOTSUPP;
569 
570 	if (family->maxattr && family->parallel_ops) {
571 		attrbuf = kmalloc_array(family->maxattr + 1,
572 					sizeof(struct nlattr *),
573 					GFP_KERNEL);
574 		if (attrbuf == NULL)
575 			return -ENOMEM;
576 	} else
577 		attrbuf = family->attrbuf;
578 
579 	if (attrbuf) {
580 		err = nlmsg_parse(nlh, hdrlen, attrbuf, family->maxattr,
581 				  ops->policy, extack);
582 		if (err < 0)
583 			goto out;
584 	}
585 
586 	info.snd_seq = nlh->nlmsg_seq;
587 	info.snd_portid = NETLINK_CB(skb).portid;
588 	info.nlhdr = nlh;
589 	info.genlhdr = nlmsg_data(nlh);
590 	info.userhdr = nlmsg_data(nlh) + GENL_HDRLEN;
591 	info.attrs = attrbuf;
592 	info.extack = extack;
593 	genl_info_net_set(&info, net);
594 	memset(&info.user_ptr, 0, sizeof(info.user_ptr));
595 
596 	if (family->pre_doit) {
597 		err = family->pre_doit(ops, skb, &info);
598 		if (err)
599 			goto out;
600 	}
601 
602 	err = ops->doit(skb, &info);
603 
604 	if (family->post_doit)
605 		family->post_doit(ops, skb, &info);
606 
607 out:
608 	if (family->parallel_ops)
609 		kfree(attrbuf);
610 
611 	return err;
612 }
613 
614 static int genl_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
615 			struct netlink_ext_ack *extack)
616 {
617 	const struct genl_family *family;
618 	int err;
619 
620 	family = genl_family_find_byid(nlh->nlmsg_type);
621 	if (family == NULL)
622 		return -ENOENT;
623 
624 	if (!family->parallel_ops)
625 		genl_lock();
626 
627 	err = genl_family_rcv_msg(family, skb, nlh, extack);
628 
629 	if (!family->parallel_ops)
630 		genl_unlock();
631 
632 	return err;
633 }
634 
635 static void genl_rcv(struct sk_buff *skb)
636 {
637 	down_read(&cb_lock);
638 	netlink_rcv_skb(skb, &genl_rcv_msg);
639 	up_read(&cb_lock);
640 }
641 
642 /**************************************************************************
643  * Controller
644  **************************************************************************/
645 
646 static struct genl_family genl_ctrl;
647 
648 static int ctrl_fill_info(const struct genl_family *family, u32 portid, u32 seq,
649 			  u32 flags, struct sk_buff *skb, u8 cmd)
650 {
651 	void *hdr;
652 
653 	hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
654 	if (hdr == NULL)
655 		return -1;
656 
657 	if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
658 	    nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id) ||
659 	    nla_put_u32(skb, CTRL_ATTR_VERSION, family->version) ||
660 	    nla_put_u32(skb, CTRL_ATTR_HDRSIZE, family->hdrsize) ||
661 	    nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
662 		goto nla_put_failure;
663 
664 	if (family->n_ops) {
665 		struct nlattr *nla_ops;
666 		int i;
667 
668 		nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
669 		if (nla_ops == NULL)
670 			goto nla_put_failure;
671 
672 		for (i = 0; i < family->n_ops; i++) {
673 			struct nlattr *nest;
674 			const struct genl_ops *ops = &family->ops[i];
675 			u32 op_flags = ops->flags;
676 
677 			if (ops->dumpit)
678 				op_flags |= GENL_CMD_CAP_DUMP;
679 			if (ops->doit)
680 				op_flags |= GENL_CMD_CAP_DO;
681 			if (ops->policy)
682 				op_flags |= GENL_CMD_CAP_HASPOL;
683 
684 			nest = nla_nest_start(skb, i + 1);
685 			if (nest == NULL)
686 				goto nla_put_failure;
687 
688 			if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
689 			    nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
690 				goto nla_put_failure;
691 
692 			nla_nest_end(skb, nest);
693 		}
694 
695 		nla_nest_end(skb, nla_ops);
696 	}
697 
698 	if (family->n_mcgrps) {
699 		struct nlattr *nla_grps;
700 		int i;
701 
702 		nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
703 		if (nla_grps == NULL)
704 			goto nla_put_failure;
705 
706 		for (i = 0; i < family->n_mcgrps; i++) {
707 			struct nlattr *nest;
708 			const struct genl_multicast_group *grp;
709 
710 			grp = &family->mcgrps[i];
711 
712 			nest = nla_nest_start(skb, i + 1);
713 			if (nest == NULL)
714 				goto nla_put_failure;
715 
716 			if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
717 					family->mcgrp_offset + i) ||
718 			    nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
719 					   grp->name))
720 				goto nla_put_failure;
721 
722 			nla_nest_end(skb, nest);
723 		}
724 		nla_nest_end(skb, nla_grps);
725 	}
726 
727 	genlmsg_end(skb, hdr);
728 	return 0;
729 
730 nla_put_failure:
731 	genlmsg_cancel(skb, hdr);
732 	return -EMSGSIZE;
733 }
734 
735 static int ctrl_fill_mcgrp_info(const struct genl_family *family,
736 				const struct genl_multicast_group *grp,
737 				int grp_id, u32 portid, u32 seq, u32 flags,
738 				struct sk_buff *skb, u8 cmd)
739 {
740 	void *hdr;
741 	struct nlattr *nla_grps;
742 	struct nlattr *nest;
743 
744 	hdr = genlmsg_put(skb, portid, seq, &genl_ctrl, flags, cmd);
745 	if (hdr == NULL)
746 		return -1;
747 
748 	if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
749 	    nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
750 		goto nla_put_failure;
751 
752 	nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
753 	if (nla_grps == NULL)
754 		goto nla_put_failure;
755 
756 	nest = nla_nest_start(skb, 1);
757 	if (nest == NULL)
758 		goto nla_put_failure;
759 
760 	if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
761 	    nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
762 			   grp->name))
763 		goto nla_put_failure;
764 
765 	nla_nest_end(skb, nest);
766 	nla_nest_end(skb, nla_grps);
767 
768 	genlmsg_end(skb, hdr);
769 	return 0;
770 
771 nla_put_failure:
772 	genlmsg_cancel(skb, hdr);
773 	return -EMSGSIZE;
774 }
775 
776 static int ctrl_dumpfamily(struct sk_buff *skb, struct netlink_callback *cb)
777 {
778 	int n = 0;
779 	struct genl_family *rt;
780 	struct net *net = sock_net(skb->sk);
781 	int fams_to_skip = cb->args[0];
782 	unsigned int id;
783 
784 	idr_for_each_entry(&genl_fam_idr, rt, id) {
785 		if (!rt->netnsok && !net_eq(net, &init_net))
786 			continue;
787 
788 		if (n++ < fams_to_skip)
789 			continue;
790 
791 		if (ctrl_fill_info(rt, NETLINK_CB(cb->skb).portid,
792 				   cb->nlh->nlmsg_seq, NLM_F_MULTI,
793 				   skb, CTRL_CMD_NEWFAMILY) < 0) {
794 			n--;
795 			break;
796 		}
797 	}
798 
799 	cb->args[0] = n;
800 	return skb->len;
801 }
802 
803 static struct sk_buff *ctrl_build_family_msg(const struct genl_family *family,
804 					     u32 portid, int seq, u8 cmd)
805 {
806 	struct sk_buff *skb;
807 	int err;
808 
809 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
810 	if (skb == NULL)
811 		return ERR_PTR(-ENOBUFS);
812 
813 	err = ctrl_fill_info(family, portid, seq, 0, skb, cmd);
814 	if (err < 0) {
815 		nlmsg_free(skb);
816 		return ERR_PTR(err);
817 	}
818 
819 	return skb;
820 }
821 
822 static struct sk_buff *
823 ctrl_build_mcgrp_msg(const struct genl_family *family,
824 		     const struct genl_multicast_group *grp,
825 		     int grp_id, u32 portid, int seq, u8 cmd)
826 {
827 	struct sk_buff *skb;
828 	int err;
829 
830 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
831 	if (skb == NULL)
832 		return ERR_PTR(-ENOBUFS);
833 
834 	err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
835 				   seq, 0, skb, cmd);
836 	if (err < 0) {
837 		nlmsg_free(skb);
838 		return ERR_PTR(err);
839 	}
840 
841 	return skb;
842 }
843 
844 static const struct nla_policy ctrl_policy[CTRL_ATTR_MAX+1] = {
845 	[CTRL_ATTR_FAMILY_ID]	= { .type = NLA_U16 },
846 	[CTRL_ATTR_FAMILY_NAME]	= { .type = NLA_NUL_STRING,
847 				    .len = GENL_NAMSIZ - 1 },
848 };
849 
850 static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
851 {
852 	struct sk_buff *msg;
853 	const struct genl_family *res = NULL;
854 	int err = -EINVAL;
855 
856 	if (info->attrs[CTRL_ATTR_FAMILY_ID]) {
857 		u16 id = nla_get_u16(info->attrs[CTRL_ATTR_FAMILY_ID]);
858 		res = genl_family_find_byid(id);
859 		err = -ENOENT;
860 	}
861 
862 	if (info->attrs[CTRL_ATTR_FAMILY_NAME]) {
863 		char *name;
864 
865 		name = nla_data(info->attrs[CTRL_ATTR_FAMILY_NAME]);
866 		res = genl_family_find_byname(name);
867 #ifdef CONFIG_MODULES
868 		if (res == NULL) {
869 			genl_unlock();
870 			up_read(&cb_lock);
871 			request_module("net-pf-%d-proto-%d-family-%s",
872 				       PF_NETLINK, NETLINK_GENERIC, name);
873 			down_read(&cb_lock);
874 			genl_lock();
875 			res = genl_family_find_byname(name);
876 		}
877 #endif
878 		err = -ENOENT;
879 	}
880 
881 	if (res == NULL)
882 		return err;
883 
884 	if (!res->netnsok && !net_eq(genl_info_net(info), &init_net)) {
885 		/* family doesn't exist here */
886 		return -ENOENT;
887 	}
888 
889 	msg = ctrl_build_family_msg(res, info->snd_portid, info->snd_seq,
890 				    CTRL_CMD_NEWFAMILY);
891 	if (IS_ERR(msg))
892 		return PTR_ERR(msg);
893 
894 	return genlmsg_reply(msg, info);
895 }
896 
897 static int genl_ctrl_event(int event, const struct genl_family *family,
898 			   const struct genl_multicast_group *grp,
899 			   int grp_id)
900 {
901 	struct sk_buff *msg;
902 
903 	/* genl is still initialising */
904 	if (!init_net.genl_sock)
905 		return 0;
906 
907 	switch (event) {
908 	case CTRL_CMD_NEWFAMILY:
909 	case CTRL_CMD_DELFAMILY:
910 		WARN_ON(grp);
911 		msg = ctrl_build_family_msg(family, 0, 0, event);
912 		break;
913 	case CTRL_CMD_NEWMCAST_GRP:
914 	case CTRL_CMD_DELMCAST_GRP:
915 		BUG_ON(!grp);
916 		msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
917 		break;
918 	default:
919 		return -EINVAL;
920 	}
921 
922 	if (IS_ERR(msg))
923 		return PTR_ERR(msg);
924 
925 	if (!family->netnsok) {
926 		genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
927 					0, GFP_KERNEL);
928 	} else {
929 		rcu_read_lock();
930 		genlmsg_multicast_allns(&genl_ctrl, msg, 0,
931 					0, GFP_ATOMIC);
932 		rcu_read_unlock();
933 	}
934 
935 	return 0;
936 }
937 
938 static const struct genl_ops genl_ctrl_ops[] = {
939 	{
940 		.cmd		= CTRL_CMD_GETFAMILY,
941 		.doit		= ctrl_getfamily,
942 		.dumpit		= ctrl_dumpfamily,
943 		.policy		= ctrl_policy,
944 	},
945 };
946 
947 static const struct genl_multicast_group genl_ctrl_groups[] = {
948 	{ .name = "notify", },
949 };
950 
951 static struct genl_family genl_ctrl __ro_after_init = {
952 	.module = THIS_MODULE,
953 	.ops = genl_ctrl_ops,
954 	.n_ops = ARRAY_SIZE(genl_ctrl_ops),
955 	.mcgrps = genl_ctrl_groups,
956 	.n_mcgrps = ARRAY_SIZE(genl_ctrl_groups),
957 	.id = GENL_ID_CTRL,
958 	.name = "nlctrl",
959 	.version = 0x2,
960 	.maxattr = CTRL_ATTR_MAX,
961 	.netnsok = true,
962 };
963 
964 static int genl_bind(struct net *net, int group)
965 {
966 	struct genl_family *f;
967 	int err = -ENOENT;
968 	unsigned int id;
969 
970 	down_read(&cb_lock);
971 
972 	idr_for_each_entry(&genl_fam_idr, f, id) {
973 		if (group >= f->mcgrp_offset &&
974 		    group < f->mcgrp_offset + f->n_mcgrps) {
975 			int fam_grp = group - f->mcgrp_offset;
976 
977 			if (!f->netnsok && net != &init_net)
978 				err = -ENOENT;
979 			else if (f->mcast_bind)
980 				err = f->mcast_bind(net, fam_grp);
981 			else
982 				err = 0;
983 			break;
984 		}
985 	}
986 	up_read(&cb_lock);
987 
988 	return err;
989 }
990 
991 static void genl_unbind(struct net *net, int group)
992 {
993 	struct genl_family *f;
994 	unsigned int id;
995 
996 	down_read(&cb_lock);
997 
998 	idr_for_each_entry(&genl_fam_idr, f, id) {
999 		if (group >= f->mcgrp_offset &&
1000 		    group < f->mcgrp_offset + f->n_mcgrps) {
1001 			int fam_grp = group - f->mcgrp_offset;
1002 
1003 			if (f->mcast_unbind)
1004 				f->mcast_unbind(net, fam_grp);
1005 			break;
1006 		}
1007 	}
1008 	up_read(&cb_lock);
1009 }
1010 
1011 static int __net_init genl_pernet_init(struct net *net)
1012 {
1013 	struct netlink_kernel_cfg cfg = {
1014 		.input		= genl_rcv,
1015 		.flags		= NL_CFG_F_NONROOT_RECV,
1016 		.bind		= genl_bind,
1017 		.unbind		= genl_unbind,
1018 	};
1019 
1020 	/* we'll bump the group number right afterwards */
1021 	net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, &cfg);
1022 
1023 	if (!net->genl_sock && net_eq(net, &init_net))
1024 		panic("GENL: Cannot initialize generic netlink\n");
1025 
1026 	if (!net->genl_sock)
1027 		return -ENOMEM;
1028 
1029 	return 0;
1030 }
1031 
1032 static void __net_exit genl_pernet_exit(struct net *net)
1033 {
1034 	netlink_kernel_release(net->genl_sock);
1035 	net->genl_sock = NULL;
1036 }
1037 
1038 static struct pernet_operations genl_pernet_ops = {
1039 	.init = genl_pernet_init,
1040 	.exit = genl_pernet_exit,
1041 };
1042 
1043 static int __init genl_init(void)
1044 {
1045 	int err;
1046 
1047 	err = genl_register_family(&genl_ctrl);
1048 	if (err < 0)
1049 		goto problem;
1050 
1051 	err = register_pernet_subsys(&genl_pernet_ops);
1052 	if (err)
1053 		goto problem;
1054 
1055 	return 0;
1056 
1057 problem:
1058 	panic("GENL: Cannot register controller: %d\n", err);
1059 }
1060 
1061 subsys_initcall(genl_init);
1062 
1063 /**
1064  * genl_family_attrbuf - return family's attrbuf
1065  * @family: the family
1066  *
1067  * Return the family's attrbuf, while validating that it's
1068  * actually valid to access it.
1069  *
1070  * You cannot use this function with a family that has parallel_ops
1071  * and you can only use it within (pre/post) doit/dumpit callbacks.
1072  */
1073 struct nlattr **genl_family_attrbuf(const struct genl_family *family)
1074 {
1075 	if (!WARN_ON(family->parallel_ops))
1076 		lockdep_assert_held(&genl_mutex);
1077 
1078 	return family->attrbuf;
1079 }
1080 EXPORT_SYMBOL(genl_family_attrbuf);
1081 
1082 static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
1083 			 gfp_t flags)
1084 {
1085 	struct sk_buff *tmp;
1086 	struct net *net, *prev = NULL;
1087 	bool delivered = false;
1088 	int err;
1089 
1090 	for_each_net_rcu(net) {
1091 		if (prev) {
1092 			tmp = skb_clone(skb, flags);
1093 			if (!tmp) {
1094 				err = -ENOMEM;
1095 				goto error;
1096 			}
1097 			err = nlmsg_multicast(prev->genl_sock, tmp,
1098 					      portid, group, flags);
1099 			if (!err)
1100 				delivered = true;
1101 			else if (err != -ESRCH)
1102 				goto error;
1103 		}
1104 
1105 		prev = net;
1106 	}
1107 
1108 	err = nlmsg_multicast(prev->genl_sock, skb, portid, group, flags);
1109 	if (!err)
1110 		delivered = true;
1111 	else if (err != -ESRCH)
1112 		return err;
1113 	return delivered ? 0 : -ESRCH;
1114  error:
1115 	kfree_skb(skb);
1116 	return err;
1117 }
1118 
1119 int genlmsg_multicast_allns(const struct genl_family *family,
1120 			    struct sk_buff *skb, u32 portid,
1121 			    unsigned int group, gfp_t flags)
1122 {
1123 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
1124 		return -EINVAL;
1125 	group = family->mcgrp_offset + group;
1126 	return genlmsg_mcast(skb, portid, group, flags);
1127 }
1128 EXPORT_SYMBOL(genlmsg_multicast_allns);
1129 
1130 void genl_notify(const struct genl_family *family, struct sk_buff *skb,
1131 		 struct genl_info *info, u32 group, gfp_t flags)
1132 {
1133 	struct net *net = genl_info_net(info);
1134 	struct sock *sk = net->genl_sock;
1135 	int report = 0;
1136 
1137 	if (info->nlhdr)
1138 		report = nlmsg_report(info->nlhdr);
1139 
1140 	if (WARN_ON_ONCE(group >= family->n_mcgrps))
1141 		return;
1142 	group = family->mcgrp_offset + group;
1143 	nlmsg_notify(sk, skb, info->snd_portid, group, report, flags);
1144 }
1145 EXPORT_SYMBOL(genl_notify);
1146