xref: /freebsd/sys/netlink/netlink_generic.c (revision 544f1492c026f358507d812b63943eea08d3155d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/ck.h>
32 #include <sys/epoch.h>
33 #include <sys/kernel.h>
34 #include <sys/jail.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/priv.h>
38 #include <sys/socket.h>
39 #include <sys/sx.h>
40 
41 #include <netlink/netlink.h>
42 #include <netlink/netlink_ctl.h>
43 #include <netlink/netlink_generic.h>
44 
45 #define	DEBUG_MOD_NAME	nl_generic
46 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
47 #include <netlink/netlink_debug.h>
48 _DECLARE_DEBUG(LOG_DEBUG);
49 
50 #define	CTRL_FAMILY_NAME	"nlctrl"
51 
52 #define	MAX_FAMILIES	20
53 #define	MAX_GROUPS	64
54 
55 #define	MIN_GROUP_NUM	48
56 
57 static struct sx sx_lock;
58 
59 #define	GENL_LOCK_INIT()	sx_init(&sx_lock, "genetlink lock")
60 #define	GENL_LOCK_DESTROY()	sx_destroy(&sx_lock)
61 #define	GENL_LOCK()		sx_xlock(&sx_lock)
62 #define	GENL_UNLOCK()		sx_xunlock(&sx_lock)
63 
64 struct genl_family {
65 	const char	*family_name;
66 	uint16_t	family_hdrsize;
67 	uint16_t	family_id;
68 	uint16_t	family_version;
69 	uint16_t	family_attr_max;
70 	uint16_t	family_cmd_size;
71 	uint16_t	family_num_groups;
72 	struct genl_cmd	*family_cmds;
73 };
74 
75 static struct genl_family	families[MAX_FAMILIES];
76 
77 
78 struct genl_group {
79 	struct genl_family	*group_family;
80 	const char		*group_name;
81 };
82 static struct genl_group	groups[MAX_GROUPS];
83 
84 
85 static int dump_family(struct nlmsghdr *hdr, struct genlmsghdr *ghdr,
86     const struct genl_family *gf, struct nl_writer *nw);
87 static void nlctrl_notify(const struct genl_family *gf, int action);
88 
89 static struct genl_family *
90 find_family(const char *family_name)
91 {
92 	for (int i = 0; i < MAX_FAMILIES; i++) {
93 		struct genl_family *gf = &families[i];
94 		if (gf->family_name != NULL && !strcmp(gf->family_name, family_name))
95 			return (gf);
96 	}
97 
98 	return (NULL);
99 }
100 
101 static struct genl_family *
102 find_empty_family_id(const char *family_name)
103 {
104 	struct genl_family *gf = NULL;
105 
106 	if (!strcmp(family_name, CTRL_FAMILY_NAME)) {
107 		gf = &families[0];
108 		gf->family_id = GENL_MIN_ID;
109 	} else {
110 		/* Index 0 is reserved for the control family */
111 		for (int i = 1; i < MAX_FAMILIES; i++) {
112 			struct genl_family *gf = &families[i];
113 			if (gf->family_name == NULL) {
114 				gf->family_id = GENL_MIN_ID + i;
115 				break;
116 			}
117 		}
118 	}
119 
120 	return (gf);
121 }
122 
123 uint32_t
124 genl_register_family(const char *family_name, size_t hdrsize, int family_version,
125     int max_attr_idx)
126 {
127 	uint32_t family_id = 0;
128 
129 	MPASS(family_name != NULL);
130 	if (find_family(family_name) != NULL)
131 		return (0);
132 
133 	GENL_LOCK();
134 
135 	struct genl_family *gf = find_empty_family_id(family_name);
136 	MPASS(gf != NULL);
137 
138 	gf->family_name = family_name;
139 	gf->family_version = family_version;
140 	gf->family_hdrsize = hdrsize;
141 	gf->family_attr_max = max_attr_idx;
142 	NL_LOG(LOG_DEBUG2, "Registered family %s id %d", gf->family_name, gf->family_id);
143 	family_id = gf->family_id;
144 	nlctrl_notify(gf, CTRL_CMD_NEWFAMILY);
145 
146 	GENL_UNLOCK();
147 
148 	return (family_id);
149 }
150 
151 static void
152 free_family(struct genl_family *gf)
153 {
154 	if (gf->family_cmds != NULL)
155 		free(gf->family_cmds, M_NETLINK);
156 }
157 
158 /*
159  * unregister groups of a given family
160  */
161 static void
162 unregister_groups(const struct genl_family *gf)
163 {
164 
165 	for (int i = 0; i < MAX_GROUPS; i++) {
166 		struct genl_group *gg = &groups[i];
167 		if (gg->group_family == gf && gg->group_name != NULL) {
168 			gg->group_family = NULL;
169 			gg->group_name = NULL;
170 		}
171 	}
172 }
173 
174 /*
175  * Can sleep, I guess
176  */
177 bool
178 genl_unregister_family(const char *family_name)
179 {
180 	bool found = false;
181 
182 	GENL_LOCK();
183 	struct genl_family *gf = find_family(family_name);
184 
185 	if (gf != NULL) {
186 		nlctrl_notify(gf, CTRL_CMD_DELFAMILY);
187 		found = true;
188 		unregister_groups(gf);
189 		/* TODO: zero pointer first */
190 		free_family(gf);
191 		bzero(gf, sizeof(*gf));
192 	}
193 	GENL_UNLOCK();
194 
195 	return (found);
196 }
197 
198 bool
199 genl_register_cmds(const char *family_name, const struct genl_cmd *cmds, int count)
200 {
201 	GENL_LOCK();
202 	struct genl_family *gf = find_family(family_name);
203 	if (gf == NULL) {
204 		GENL_UNLOCK();
205 		return (false);
206 	}
207 
208 	int cmd_size = gf->family_cmd_size;
209 
210 	for (int i = 0; i < count; i++) {
211 		MPASS(cmds[i].cmd_cb != NULL);
212 		if (cmds[i].cmd_num >= cmd_size)
213 			cmd_size = cmds[i].cmd_num + 1;
214 	}
215 
216 	if (cmd_size > gf->family_cmd_size) {
217 		/* need to realloc */
218 		size_t sz = cmd_size * sizeof(struct genl_cmd);
219 		void *data = malloc(sz, M_NETLINK, M_WAITOK | M_ZERO);
220 
221 		memcpy(data, gf->family_cmds, gf->family_cmd_size * sizeof(struct genl_cmd));
222 		void *old_data = gf->family_cmds;
223 		gf->family_cmds = data;
224 		gf->family_cmd_size = cmd_size;
225 		free(old_data, M_NETLINK);
226 	}
227 
228 	for (int i = 0; i < count; i++) {
229 		const struct genl_cmd *cmd = &cmds[i];
230 		MPASS(gf->family_cmds[cmd->cmd_num].cmd_cb == NULL);
231 		gf->family_cmds[cmd->cmd_num] = cmds[i];
232 		NL_LOG(LOG_DEBUG2, "Adding cmd %s(%d) to family %s",
233 		    cmd->cmd_name, cmd->cmd_num, gf->family_name);
234 	}
235 	GENL_UNLOCK();
236 	return (true);
237 }
238 
239 static struct genl_group *
240 find_group(const struct genl_family *gf, const char *group_name)
241 {
242 	for (int i = 0; i < MAX_GROUPS; i++) {
243 		struct genl_group *gg = &groups[i];
244 		if (gg->group_family == gf && !strcmp(gg->group_name, group_name))
245 			return (gg);
246 	}
247 	return (NULL);
248 }
249 
250 uint32_t
251 genl_register_group(const char *family_name, const char *group_name)
252 {
253 	uint32_t group_id = 0;
254 
255 	MPASS(family_name != NULL);
256 	MPASS(group_name != NULL);
257 
258 	GENL_LOCK();
259 	struct genl_family *gf = find_family(family_name);
260 
261 	if (gf == NULL || find_group(gf, group_name) != NULL) {
262 		GENL_UNLOCK();
263 		return (0);
264 	}
265 
266 	for (int i = 0; i < MAX_GROUPS; i++) {
267 		struct genl_group *gg = &groups[i];
268 		if (gg->group_family == NULL) {
269 			gf->family_num_groups++;
270 			gg->group_family = gf;
271 			gg->group_name = group_name;
272 			group_id = i + MIN_GROUP_NUM;
273 			break;
274 		}
275 	}
276 	GENL_UNLOCK();
277 
278 	return (group_id);
279 }
280 
281 /*
282  * Handler called by netlink subsystem when matching netlink message is received
283  */
284 static int
285 genl_handle_message(struct nlmsghdr *hdr, struct nl_pstate *npt)
286 {
287 	struct nlpcb *nlp = npt->nlp;
288 	int error = 0;
289 
290 	int family_id = (int)hdr->nlmsg_type - GENL_MIN_ID;
291 
292 	if (__predict_false(family_id < 0 || family_id >= MAX_FAMILIES)) {
293 		NLP_LOG(LOG_DEBUG, nlp, "invalid message type: %d", hdr->nlmsg_type);
294 		return (ENOTSUP);
295 	}
296 
297 	if (__predict_false(hdr->nlmsg_len < sizeof(hdr) + GENL_HDRLEN)) {
298 		NLP_LOG(LOG_DEBUG, nlp, "invalid message size: %d", hdr->nlmsg_len);
299 		return (EINVAL);
300 	}
301 
302 	struct genl_family *gf = &families[family_id];
303 
304 	struct genlmsghdr *ghdr = (struct genlmsghdr *)(hdr + 1);
305 
306 	if (ghdr->cmd >= gf->family_cmd_size || gf->family_cmds[ghdr->cmd].cmd_cb == NULL) {
307 		NLP_LOG(LOG_DEBUG, nlp, "family %s: invalid cmd %d",
308 		    gf->family_name, ghdr->cmd);
309 		return (ENOTSUP);
310 	}
311 
312 	struct genl_cmd *cmd = &gf->family_cmds[ghdr->cmd];
313 
314 	if (cmd->cmd_priv != 0 && !nlp_has_priv(nlp, cmd->cmd_priv)) {
315 		NLP_LOG(LOG_DEBUG, nlp, "family %s: cmd %d priv_check() failed",
316 		    gf->family_name, ghdr->cmd);
317 		return (EPERM);
318 	}
319 
320 	NLP_LOG(LOG_DEBUG2, nlp, "received family %s cmd %s(%d) len %d",
321 	    gf->family_name, cmd->cmd_name, ghdr->cmd, hdr->nlmsg_len);
322 
323 	error = cmd->cmd_cb(hdr, npt);
324 
325 	return (error);
326 }
327 
328 static uint32_t
329 get_cmd_flags(const struct genl_cmd *cmd)
330 {
331 	uint32_t flags = cmd->cmd_flags;
332 	if (cmd->cmd_priv != 0)
333 		flags |= GENL_ADMIN_PERM;
334 	return (flags);
335 }
336 
337 static int
338 dump_family(struct nlmsghdr *hdr, struct genlmsghdr *ghdr,
339     const struct genl_family *gf, struct nl_writer *nw)
340 {
341 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
342 		goto enomem;
343 
344 	struct genlmsghdr *ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
345 	ghdr_new->cmd = ghdr->cmd;
346 	ghdr_new->version = gf->family_version;
347 	ghdr_new->reserved = 0;
348 
349         nlattr_add_string(nw, CTRL_ATTR_FAMILY_NAME, gf->family_name);
350         nlattr_add_u16(nw, CTRL_ATTR_FAMILY_ID, gf->family_id);
351         nlattr_add_u32(nw, CTRL_ATTR_VERSION, gf->family_version);
352         nlattr_add_u32(nw, CTRL_ATTR_HDRSIZE, gf->family_hdrsize);
353         nlattr_add_u32(nw, CTRL_ATTR_MAXATTR, gf->family_attr_max);
354 
355 	if (gf->family_cmd_size > 0) {
356 		int off = nlattr_add_nested(nw, CTRL_ATTR_OPS);
357 		if (off == 0)
358 			goto enomem;
359 		for (int i = 0, cnt=0; i < gf->family_cmd_size; i++) {
360 			struct genl_cmd *cmd = &gf->family_cmds[i];
361 			if (cmd->cmd_cb == NULL)
362 				continue;
363 			int cmd_off = nlattr_add_nested(nw, ++cnt);
364 			if (cmd_off == 0)
365 				goto enomem;
366 
367 			nlattr_add_u32(nw, CTRL_ATTR_OP_ID, cmd->cmd_num);
368 			nlattr_add_u32(nw, CTRL_ATTR_OP_FLAGS, get_cmd_flags(cmd));
369 			nlattr_set_len(nw, cmd_off);
370 		}
371 		nlattr_set_len(nw, off);
372 	}
373 	if (gf->family_num_groups > 0) {
374 		int off = nlattr_add_nested(nw, CTRL_ATTR_MCAST_GROUPS);
375 		if (off == 0)
376 			goto enomem;
377 		for (int i = 0, cnt = 0; i < MAX_GROUPS; i++) {
378 			struct genl_group *gg = &groups[i];
379 			if (gg->group_family != gf)
380 				continue;
381 
382 			int cmd_off = nlattr_add_nested(nw, ++cnt);
383 			if (cmd_off == 0)
384 				goto enomem;
385 			nlattr_add_u32(nw, CTRL_ATTR_MCAST_GRP_ID, i + MIN_GROUP_NUM);
386 			nlattr_add_string(nw, CTRL_ATTR_MCAST_GRP_NAME, gg->group_name);
387 			nlattr_set_len(nw, cmd_off);
388 		}
389 		nlattr_set_len(nw, off);
390 	}
391 	if (nlmsg_end(nw))
392 		return (0);
393 enomem:
394         NL_LOG(LOG_DEBUG, "unable to dump family %s state (ENOMEM)", gf->family_name);
395         nlmsg_abort(nw);
396 	return (ENOMEM);
397 }
398 
399 
400 /* Declare ourself as a user */
401 
402 static uint32_t ctrl_family_id;
403 static uint32_t ctrl_group_id;
404 
405 struct nl_parsed_family {
406 	uint32_t	family_id;
407 	char		*family_name;
408 	uint8_t		version;
409 };
410 
411 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
412 #define	_OUT(_field)	offsetof(struct nl_parsed_family, _field)
413 static const struct nlfield_parser nlf_p_generic[] = {
414 	{ .off_in = _IN(version), .off_out = _OUT(version), .cb = nlf_get_u8 },
415 };
416 
417 static struct nlattr_parser nla_p_generic[] = {
418 	{ .type = CTRL_ATTR_FAMILY_ID , .off = _OUT(family_id), .cb = nlattr_get_uint16 },
419 	{ .type = CTRL_ATTR_FAMILY_NAME , .off = _OUT(family_name), .cb = nlattr_get_string },
420 };
421 #undef _IN
422 #undef _OUT
423 NL_DECLARE_PARSER(genl_parser, struct genlmsghdr, nlf_p_generic, nla_p_generic);
424 
425 static bool
426 match_family(const struct genl_family *gf, const struct nl_parsed_family *attrs)
427 {
428 	if (gf->family_name == NULL)
429 		return (false);
430 	if (attrs->family_id != 0 && attrs->family_id != gf->family_id)
431 		return (false);
432 	if (attrs->family_name != NULL && strcmp(attrs->family_name, gf->family_name))
433 		return (false);
434 	return (true);
435 }
436 
437 static int
438 nlctrl_handle_getfamily(struct nlmsghdr *hdr, struct nl_pstate *npt)
439 {
440 	int error = 0;
441 
442 	struct nl_parsed_family attrs = {};
443 	error = nl_parse_nlmsg(hdr, &genl_parser, npt, &attrs);
444 	if (error != 0)
445 		return (error);
446 
447 	struct genlmsghdr ghdr = {
448 		.cmd = CTRL_CMD_NEWFAMILY,
449 	};
450 
451 	if (attrs.family_id != 0 || attrs.family_name != NULL) {
452 		/* Resolve request */
453 		for (int i = 0; i < MAX_FAMILIES; i++) {
454 			struct genl_family *gf = &families[i];
455 			if (match_family(gf, &attrs)) {
456 				error = dump_family(hdr, &ghdr, gf, npt->nw);
457 				return (error);
458 			}
459 		}
460 		return (ENOENT);
461 	}
462 
463 	hdr->nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI;
464 	for (int i = 0; i < MAX_FAMILIES; i++) {
465 		struct genl_family *gf = &families[i];
466 		if (match_family(gf, &attrs)) {
467 			error = dump_family(hdr, &ghdr, gf, npt->nw);
468 			if (error != 0)
469 				break;
470 		}
471 	}
472 
473 	if (!nlmsg_end_dump(npt->nw, error, hdr)) {
474                 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
475                 return (ENOMEM);
476         }
477 
478 	return (error);
479 }
480 
481 static void
482 nlctrl_notify(const struct genl_family *gf, int cmd)
483 {
484 	struct nlmsghdr hdr = {.nlmsg_type = NETLINK_GENERIC };
485 	struct genlmsghdr ghdr = { .cmd = cmd };
486 	struct nl_writer nw = {};
487 
488 	if (nlmsg_get_group_writer(&nw, NLMSG_SMALL, NETLINK_GENERIC, ctrl_group_id)) {
489 		dump_family(&hdr, &ghdr, gf, &nw);
490 		nlmsg_flush(&nw);
491 		return;
492 	}
493 	NL_LOG(LOG_DEBUG, "error allocating group writer");
494 }
495 
496 static const struct genl_cmd nlctrl_cmds[] = {
497 	{
498 		.cmd_num = CTRL_CMD_GETFAMILY,
499 		.cmd_name = "GETFAMILY",
500 		.cmd_cb = nlctrl_handle_getfamily,
501 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
502 	},
503 };
504 
505 static void
506 genl_nlctrl_init(void)
507 {
508 	ctrl_family_id = genl_register_family(CTRL_FAMILY_NAME, 0, 2, CTRL_ATTR_MAX);
509 	genl_register_cmds(CTRL_FAMILY_NAME, nlctrl_cmds, NL_ARRAY_LEN(nlctrl_cmds));
510 	ctrl_group_id = genl_register_group(CTRL_FAMILY_NAME, "notify");
511 }
512 
513 static void
514 genl_nlctrl_destroy(void)
515 {
516 	genl_unregister_family(CTRL_FAMILY_NAME);
517 }
518 
519 static const struct nlhdr_parser *all_parsers[] = { &genl_parser };
520 
521 static void
522 genl_load(void *u __unused)
523 {
524 	GENL_LOCK_INIT();
525 	NL_VERIFY_PARSERS(all_parsers);
526 	netlink_register_proto(NETLINK_GENERIC, "NETLINK_GENERIC", genl_handle_message);
527 	genl_nlctrl_init();
528 }
529 SYSINIT(genl_load, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, genl_load, NULL);
530 
531 static void
532 genl_unload(void *u __unused)
533 {
534 	genl_nlctrl_destroy();
535 	GENL_LOCK_DESTROY();
536 	NET_EPOCH_WAIT();
537 }
538 SYSUNINIT(genl_unload, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, genl_unload, NULL);
539