xref: /freebsd/sys/netlink/netlink_generic.c (revision b17b639832e707aab0e9514cf94727498e2d67bd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 "opt_netlink.h"
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/ck.h>
34 #include <sys/epoch.h>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/jail.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/priv.h>
41 #include <sys/socket.h>
42 #include <sys/sx.h>
43 
44 #include <netlink/netlink.h>
45 #include <netlink/netlink_ctl.h>
46 #include <netlink/netlink_generic.h>
47 #include <netlink/netlink_var.h>
48 
49 #define	DEBUG_MOD_NAME	nl_generic
50 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
51 #include <netlink/netlink_debug.h>
52 _DECLARE_DEBUG(LOG_INFO);
53 
54 static int dump_family(struct nlmsghdr *hdr, struct genlmsghdr *ghdr,
55     const struct genl_family *gf, struct nl_writer *nw);
56 
57 /*
58  * Handler called by netlink subsystem when matching netlink message is received
59  */
60 static int
61 genl_handle_message(struct nlmsghdr *hdr, struct nl_pstate *npt)
62 {
63 	struct nlpcb *nlp = npt->nlp;
64 	struct genl_family *gf = NULL;
65 	int error = 0;
66 
67 	int family_id = (int)hdr->nlmsg_type - GENL_MIN_ID;
68 
69 	if (__predict_false(family_id < 0 || (gf = genl_get_family(family_id)) == NULL)) {
70 		NLP_LOG(LOG_DEBUG, nlp, "invalid message type: %d", hdr->nlmsg_type);
71 		return (ENOTSUP);
72 	}
73 
74 	if (__predict_false(hdr->nlmsg_len < sizeof(hdr) + GENL_HDRLEN)) {
75 		NLP_LOG(LOG_DEBUG, nlp, "invalid message size: %d", hdr->nlmsg_len);
76 		return (EINVAL);
77 	}
78 
79 	struct genlmsghdr *ghdr = (struct genlmsghdr *)(hdr + 1);
80 
81 	if (ghdr->cmd >= gf->family_cmd_size || gf->family_cmds[ghdr->cmd].cmd_cb == NULL) {
82 		NLP_LOG(LOG_DEBUG, nlp, "family %s: invalid cmd %d",
83 		    gf->family_name, ghdr->cmd);
84 		return (ENOTSUP);
85 	}
86 
87 	struct genl_cmd *cmd = &gf->family_cmds[ghdr->cmd];
88 
89 	if (cmd->cmd_priv != 0 && !nlp_has_priv(nlp, cmd->cmd_priv)) {
90 		NLP_LOG(LOG_DEBUG, nlp, "family %s: cmd %d priv_check() failed",
91 		    gf->family_name, ghdr->cmd);
92 		return (EPERM);
93 	}
94 
95 	NLP_LOG(LOG_DEBUG2, nlp, "received family %s cmd %s(%d) len %d",
96 	    gf->family_name, cmd->cmd_name, ghdr->cmd, hdr->nlmsg_len);
97 
98 	error = cmd->cmd_cb(hdr, npt);
99 
100 	return (error);
101 }
102 
103 static uint32_t
104 get_cmd_flags(const struct genl_cmd *cmd)
105 {
106 	uint32_t flags = cmd->cmd_flags;
107 	if (cmd->cmd_priv != 0)
108 		flags |= GENL_ADMIN_PERM;
109 	return (flags);
110 }
111 
112 static int
113 dump_family(struct nlmsghdr *hdr, struct genlmsghdr *ghdr,
114     const struct genl_family *gf, struct nl_writer *nw)
115 {
116 	if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr)))
117 		goto enomem;
118 
119 	struct genlmsghdr *ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr);
120 	ghdr_new->cmd = ghdr->cmd;
121 	ghdr_new->version = gf->family_version;
122 	ghdr_new->reserved = 0;
123 
124         nlattr_add_string(nw, CTRL_ATTR_FAMILY_NAME, gf->family_name);
125         nlattr_add_u16(nw, CTRL_ATTR_FAMILY_ID, gf->family_id);
126         nlattr_add_u32(nw, CTRL_ATTR_VERSION, gf->family_version);
127         nlattr_add_u32(nw, CTRL_ATTR_HDRSIZE, gf->family_hdrsize);
128         nlattr_add_u32(nw, CTRL_ATTR_MAXATTR, gf->family_attr_max);
129 
130 	if (gf->family_cmd_size > 0) {
131 		int off = nlattr_add_nested(nw, CTRL_ATTR_OPS);
132 		if (off == 0)
133 			goto enomem;
134 		for (int i = 0, cnt=0; i < gf->family_cmd_size; i++) {
135 			struct genl_cmd *cmd = &gf->family_cmds[i];
136 			if (cmd->cmd_cb == NULL)
137 				continue;
138 			int cmd_off = nlattr_add_nested(nw, ++cnt);
139 			if (cmd_off == 0)
140 				goto enomem;
141 
142 			nlattr_add_u32(nw, CTRL_ATTR_OP_ID, cmd->cmd_num);
143 			nlattr_add_u32(nw, CTRL_ATTR_OP_FLAGS, get_cmd_flags(cmd));
144 			nlattr_set_len(nw, cmd_off);
145 		}
146 		nlattr_set_len(nw, off);
147 	}
148 	if (gf->family_num_groups > 0) {
149 		int off = nlattr_add_nested(nw, CTRL_ATTR_MCAST_GROUPS);
150 		if (off == 0)
151 			goto enomem;
152 		for (int i = 0, cnt = 0; i < MAX_GROUPS; i++) {
153 			struct genl_group *gg = genl_get_group(i);
154 			if (gg == NULL || gg->group_family != gf)
155 				continue;
156 
157 			int cmd_off = nlattr_add_nested(nw, ++cnt);
158 			if (cmd_off == 0)
159 				goto enomem;
160 			nlattr_add_u32(nw, CTRL_ATTR_MCAST_GRP_ID, i + MIN_GROUP_NUM);
161 			nlattr_add_string(nw, CTRL_ATTR_MCAST_GRP_NAME, gg->group_name);
162 			nlattr_set_len(nw, cmd_off);
163 		}
164 		nlattr_set_len(nw, off);
165 	}
166 	if (nlmsg_end(nw))
167 		return (0);
168 enomem:
169         NL_LOG(LOG_DEBUG, "unable to dump family %s state (ENOMEM)", gf->family_name);
170         nlmsg_abort(nw);
171 	return (ENOMEM);
172 }
173 
174 
175 /* Declare ourself as a user */
176 static void nlctrl_notify(void *arg, const struct genl_family *gf, int action);
177 static eventhandler_tag family_event_tag;
178 
179 static uint32_t ctrl_family_id;
180 static uint32_t ctrl_group_id;
181 
182 struct nl_parsed_family {
183 	uint32_t	family_id;
184 	char		*family_name;
185 	uint8_t		version;
186 };
187 
188 #define	_IN(_field)	offsetof(struct genlmsghdr, _field)
189 #define	_OUT(_field)	offsetof(struct nl_parsed_family, _field)
190 static const struct nlfield_parser nlf_p_generic[] = {
191 	{ .off_in = _IN(version), .off_out = _OUT(version), .cb = nlf_get_u8 },
192 };
193 
194 static struct nlattr_parser nla_p_generic[] = {
195 	{ .type = CTRL_ATTR_FAMILY_ID , .off = _OUT(family_id), .cb = nlattr_get_uint16 },
196 	{ .type = CTRL_ATTR_FAMILY_NAME , .off = _OUT(family_name), .cb = nlattr_get_string },
197 };
198 #undef _IN
199 #undef _OUT
200 NL_DECLARE_PARSER(genl_parser, struct genlmsghdr, nlf_p_generic, nla_p_generic);
201 
202 static bool
203 match_family(const struct genl_family *gf, const struct nl_parsed_family *attrs)
204 {
205 	if (gf->family_name == NULL)
206 		return (false);
207 	if (attrs->family_id != 0 && attrs->family_id != gf->family_id)
208 		return (false);
209 	if (attrs->family_name != NULL && strcmp(attrs->family_name, gf->family_name))
210 		return (false);
211 	return (true);
212 }
213 
214 static int
215 nlctrl_handle_getfamily(struct nlmsghdr *hdr, struct nl_pstate *npt)
216 {
217 	int error = 0;
218 
219 	struct nl_parsed_family attrs = {};
220 	error = nl_parse_nlmsg(hdr, &genl_parser, npt, &attrs);
221 	if (error != 0)
222 		return (error);
223 
224 	struct genlmsghdr ghdr = {
225 		.cmd = CTRL_CMD_NEWFAMILY,
226 	};
227 
228 	if (attrs.family_id != 0 || attrs.family_name != NULL) {
229 		/* Resolve request */
230 		for (int i = 0; i < MAX_FAMILIES; i++) {
231 			struct genl_family *gf = genl_get_family(i);
232 			if (gf != NULL && match_family(gf, &attrs)) {
233 				error = dump_family(hdr, &ghdr, gf, npt->nw);
234 				return (error);
235 			}
236 		}
237 		return (ENOENT);
238 	}
239 
240 	hdr->nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI;
241 	for (int i = 0; i < MAX_FAMILIES; i++) {
242 		struct genl_family *gf = genl_get_family(i);
243 		if (gf != NULL && match_family(gf, &attrs)) {
244 			error = dump_family(hdr, &ghdr, gf, npt->nw);
245 			if (error != 0)
246 				break;
247 		}
248 	}
249 
250 	if (!nlmsg_end_dump(npt->nw, error, hdr)) {
251                 NL_LOG(LOG_DEBUG, "Unable to finalize the dump");
252                 return (ENOMEM);
253         }
254 
255 	return (error);
256 }
257 
258 static void
259 nlctrl_notify(void *arg __unused, const struct genl_family *gf, int cmd)
260 {
261 	struct nlmsghdr hdr = {.nlmsg_type = NETLINK_GENERIC };
262 	struct genlmsghdr ghdr = { .cmd = cmd };
263 	struct nl_writer nw = {};
264 
265 	if (nlmsg_get_group_writer(&nw, NLMSG_SMALL, NETLINK_GENERIC, ctrl_group_id)) {
266 		dump_family(&hdr, &ghdr, gf, &nw);
267 		nlmsg_flush(&nw);
268 		return;
269 	}
270 	NL_LOG(LOG_DEBUG, "error allocating group writer");
271 }
272 
273 static const struct genl_cmd nlctrl_cmds[] = {
274 	{
275 		.cmd_num = CTRL_CMD_GETFAMILY,
276 		.cmd_name = "GETFAMILY",
277 		.cmd_cb = nlctrl_handle_getfamily,
278 		.cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL,
279 	},
280 };
281 
282 static const struct nlhdr_parser *all_parsers[] = { &genl_parser };
283 
284 static void
285 genl_load_all(void *u __unused)
286 {
287 	NL_VERIFY_PARSERS(all_parsers);
288 	ctrl_family_id = genl_register_family(CTRL_FAMILY_NAME, 0, 2, CTRL_ATTR_MAX);
289 	genl_register_cmds(CTRL_FAMILY_NAME, nlctrl_cmds, NL_ARRAY_LEN(nlctrl_cmds));
290 	ctrl_group_id = genl_register_group(CTRL_FAMILY_NAME, "notify");
291 	family_event_tag = EVENTHANDLER_REGISTER(genl_family_event, nlctrl_notify, NULL,
292 	    EVENTHANDLER_PRI_ANY);
293 	netlink_register_proto(NETLINK_GENERIC, "NETLINK_GENERIC", genl_handle_message);
294 }
295 SYSINIT(genl_load_all, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, genl_load_all, NULL);
296 
297 static void
298 genl_unload(void *u __unused)
299 {
300 	EVENTHANDLER_DEREGISTER(genl_family_event, family_event_tag);
301 	genl_unregister_family(CTRL_FAMILY_NAME);
302 	NET_EPOCH_WAIT();
303 }
304 SYSUNINIT(genl_unload, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, genl_unload, NULL);
305