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