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