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