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/kernel.h> 33 #include <sys/lock.h> 34 #include <sys/malloc.h> 35 #include <sys/priv.h> 36 #include <sys/socket.h> 37 #include <sys/sx.h> 38 39 #include <netlink/netlink.h> 40 #include <netlink/netlink_ctl.h> 41 #include <netlink/netlink_var.h> 42 #include <netlink/netlink_generic.h> 43 44 #define DEBUG_MOD_NAME nl_generic 45 #define DEBUG_MAX_LEVEL LOG_DEBUG3 46 #include <netlink/netlink_debug.h> 47 _DECLARE_DEBUG(LOG_DEBUG3); 48 49 #define MAX_FAMILIES 20 50 #define MAX_GROUPS 20 51 52 #define MIN_GROUP_NUM 48 53 54 static struct sx sx_lock; 55 56 #define GENL_LOCK_INIT() sx_init(&sx_lock, "genetlink lock") 57 #define GENL_LOCK_DESTROY() sx_destroy(&sx_lock) 58 #define GENL_LOCK() sx_xlock(&sx_lock) 59 #define GENL_UNLOCK() sx_xunlock(&sx_lock) 60 61 struct genl_family { 62 const char *family_name; 63 uint16_t family_hdrsize; 64 uint16_t family_id; 65 uint16_t family_version; 66 uint16_t family_attr_max; 67 uint16_t family_cmd_size; 68 uint16_t family_num_groups; 69 struct genl_cmd *family_cmds; 70 }; 71 72 static struct genl_family families[MAX_FAMILIES]; 73 74 75 struct genl_group { 76 struct genl_family *group_family; 77 const char *group_name; 78 }; 79 static struct genl_group groups[MAX_GROUPS]; 80 81 82 static int dump_family(struct nlmsghdr *hdr, struct genlmsghdr *ghdr, 83 const struct genl_family *gf, struct nl_writer *nw); 84 static void nlctrl_notify(const struct genl_family *gf, int action); 85 86 static struct genl_family * 87 find_family(const char *family_name) 88 { 89 for (int i = 0; i < MAX_FAMILIES; i++) { 90 struct genl_family *gf = &families[i]; 91 if (gf->family_name != NULL && !strcmp(gf->family_name, family_name)) 92 return (gf); 93 } 94 95 return (NULL); 96 } 97 98 uint32_t 99 genl_register_family(const char *family_name, size_t hdrsize, int family_version, 100 int max_attr_idx) 101 { 102 uint32_t family_id = 0; 103 104 MPASS(family_name != NULL); 105 if (find_family(family_name) != NULL) 106 return (0); 107 108 GENL_LOCK(); 109 for (int i = 0; i < MAX_FAMILIES; i++) { 110 struct genl_family *gf = &families[i]; 111 if (gf->family_name == NULL) { 112 gf->family_name = family_name; 113 gf->family_version = family_version; 114 gf->family_hdrsize = hdrsize; 115 gf->family_attr_max = max_attr_idx; 116 gf->family_id = i + GENL_MIN_ID; 117 NL_LOG(LOG_DEBUG2, "Registered family %s id %d", 118 gf->family_name, gf->family_id); 119 family_id = gf->family_id; 120 nlctrl_notify(gf, CTRL_CMD_NEWFAMILY); 121 break; 122 } 123 } 124 GENL_UNLOCK(); 125 126 return (family_id); 127 } 128 129 static void 130 free_family(struct genl_family *gf) 131 { 132 if (gf->family_cmds != NULL) 133 free(gf->family_cmds, M_NETLINK); 134 } 135 136 /* 137 * Can sleep, I guess 138 */ 139 bool 140 genl_unregister_family(const char *family_name) 141 { 142 bool found = false; 143 144 GENL_LOCK(); 145 struct genl_family *gf = find_family(family_name); 146 147 nlctrl_notify(gf, CTRL_CMD_DELFAMILY); 148 149 if (gf != NULL) { 150 found = true; 151 /* TODO: zero pointer first */ 152 free_family(gf); 153 bzero(gf, sizeof(*gf)); 154 } 155 GENL_UNLOCK(); 156 157 return (found); 158 } 159 160 bool 161 genl_register_cmds(const char *family_name, const struct genl_cmd *cmds, int count) 162 { 163 GENL_LOCK(); 164 struct genl_family *gf = find_family(family_name); 165 if (gf == NULL) { 166 GENL_UNLOCK(); 167 return (false); 168 } 169 170 int cmd_size = gf->family_cmd_size; 171 172 for (int i = 0; i < count; i++) { 173 MPASS(cmds[i].cmd_cb != NULL); 174 if (cmds[i].cmd_num >= cmd_size) 175 cmd_size = cmds[i].cmd_num + 1; 176 } 177 178 if (cmd_size > gf->family_cmd_size) { 179 /* need to realloc */ 180 size_t sz = cmd_size * sizeof(struct genl_cmd); 181 void *data = malloc(sz, M_NETLINK, M_WAITOK | M_ZERO); 182 183 memcpy(data, gf->family_cmds, gf->family_cmd_size * sizeof(struct genl_cmd)); 184 void *old_data = gf->family_cmds; 185 gf->family_cmds = data; 186 gf->family_cmd_size = cmd_size; 187 free(old_data, M_NETLINK); 188 } 189 190 for (int i = 0; i < count; i++) { 191 const struct genl_cmd *cmd = &cmds[i]; 192 MPASS(gf->family_cmds[cmd->cmd_num].cmd_cb == NULL); 193 gf->family_cmds[cmd->cmd_num] = cmds[i]; 194 NL_LOG(LOG_DEBUG2, "Adding cmd %s(%d) to family %s", 195 cmd->cmd_name, cmd->cmd_num, gf->family_name); 196 } 197 GENL_UNLOCK(); 198 return (true); 199 } 200 201 static struct genl_group * 202 find_group(const struct genl_family *gf, const char *group_name) 203 { 204 for (int i = 0; i < MAX_GROUPS; i++) { 205 struct genl_group *gg = &groups[i]; 206 if (gg->group_family == gf && !strcmp(gg->group_name, group_name)) 207 return (gg); 208 } 209 return (NULL); 210 } 211 212 uint32_t 213 genl_register_group(const char *family_name, const char *group_name) 214 { 215 uint32_t group_id = 0; 216 217 MPASS(family_name != NULL); 218 MPASS(group_name != NULL); 219 220 GENL_LOCK(); 221 struct genl_family *gf = find_family(family_name); 222 223 if (gf == NULL || find_group(gf, group_name) != NULL) { 224 GENL_UNLOCK(); 225 return (0); 226 } 227 228 for (int i = 0; i < MAX_GROUPS; i++) { 229 struct genl_group *gg = &groups[i]; 230 if (gg->group_family == NULL) { 231 gf->family_num_groups++; 232 gg->group_family = gf; 233 gg->group_name = group_name; 234 group_id = i + MIN_GROUP_NUM; 235 break; 236 } 237 } 238 GENL_UNLOCK(); 239 240 return (group_id); 241 } 242 243 /* 244 * Handler called by netlink subsystem when matching netlink message is received 245 */ 246 static int 247 genl_handle_message(struct nlmsghdr *hdr, struct nl_pstate *npt) 248 { 249 struct nlpcb *nlp = npt->nlp; 250 int error = 0; 251 252 int family_id = (int)hdr->nlmsg_type - GENL_MIN_ID; 253 254 if (__predict_false(family_id < 0 || family_id > MAX_FAMILIES)) { 255 NLP_LOG(LOG_DEBUG, nlp, "invalid message type: %d", hdr->nlmsg_type); 256 return (ENOTSUP); 257 } 258 259 if (__predict_false(hdr->nlmsg_len < sizeof(hdr) + GENL_HDRLEN)) { 260 NLP_LOG(LOG_DEBUG, nlp, "invalid message size: %d", hdr->nlmsg_len); 261 return (EINVAL); 262 } 263 264 struct genl_family *gf = &families[family_id]; 265 266 struct genlmsghdr *ghdr = (struct genlmsghdr *)(hdr + 1); 267 268 if (ghdr->cmd >= gf->family_cmd_size || gf->family_cmds[ghdr->cmd].cmd_cb == NULL) { 269 NLP_LOG(LOG_DEBUG, nlp, "family %s: invalid cmd %d", 270 gf->family_name, ghdr->cmd); 271 return (ENOTSUP); 272 } 273 274 struct genl_cmd *cmd = &gf->family_cmds[ghdr->cmd]; 275 276 if (cmd->cmd_priv != 0 && !nlp_has_priv(nlp, cmd->cmd_priv)) { 277 NLP_LOG(LOG_DEBUG, nlp, "family %s: cmd %d priv_check() failed", 278 gf->family_name, ghdr->cmd); 279 return (EPERM); 280 } 281 282 NLP_LOG(LOG_DEBUG2, nlp, "received family %s cmd %s(%d) len %d", 283 gf->family_name, cmd->cmd_name, ghdr->cmd, hdr->nlmsg_len); 284 285 error = cmd->cmd_cb(hdr, npt); 286 287 return (error); 288 } 289 290 static uint32_t 291 get_cmd_flags(const struct genl_cmd *cmd) 292 { 293 uint32_t flags = cmd->cmd_flags; 294 if (cmd->cmd_priv != 0) 295 flags |= GENL_ADMIN_PERM; 296 return (flags); 297 } 298 299 static int 300 dump_family(struct nlmsghdr *hdr, struct genlmsghdr *ghdr, 301 const struct genl_family *gf, struct nl_writer *nw) 302 { 303 if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr))) 304 goto enomem; 305 306 struct genlmsghdr *ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr); 307 ghdr_new->cmd = ghdr->cmd; 308 ghdr_new->version = gf->family_version; 309 ghdr_new->reserved = 0; 310 311 nlattr_add_string(nw, CTRL_ATTR_FAMILY_NAME, gf->family_name); 312 nlattr_add_u16(nw, CTRL_ATTR_FAMILY_ID, gf->family_id); 313 nlattr_add_u32(nw, CTRL_ATTR_VERSION, gf->family_version); 314 nlattr_add_u32(nw, CTRL_ATTR_HDRSIZE, gf->family_hdrsize); 315 nlattr_add_u32(nw, CTRL_ATTR_MAXATTR, gf->family_attr_max); 316 317 if (gf->family_cmd_size > 0) { 318 int off = nlattr_add_nested(nw, CTRL_ATTR_OPS); 319 if (off == 0) 320 goto enomem; 321 for (int i = 0, cnt=0; i < gf->family_cmd_size; i++) { 322 struct genl_cmd *cmd = &gf->family_cmds[i]; 323 if (cmd->cmd_cb == NULL) 324 continue; 325 int cmd_off = nlattr_add_nested(nw, ++cnt); 326 if (cmd_off == 0) 327 goto enomem; 328 329 nlattr_add_u32(nw, CTRL_ATTR_OP_ID, cmd->cmd_num); 330 nlattr_add_u32(nw, CTRL_ATTR_OP_FLAGS, get_cmd_flags(cmd)); 331 nlattr_set_len(nw, cmd_off); 332 } 333 nlattr_set_len(nw, off); 334 } 335 if (gf->family_num_groups > 0) { 336 int off = nlattr_add_nested(nw, CTRL_ATTR_MCAST_GROUPS); 337 if (off == 0) 338 goto enomem; 339 for (int i = 0, cnt = 0; i < MAX_GROUPS; i++) { 340 struct genl_group *gg = &groups[i]; 341 if (gg->group_family != gf) 342 continue; 343 344 int cmd_off = nlattr_add_nested(nw, ++cnt); 345 if (cmd_off == 0) 346 goto enomem; 347 nlattr_add_u32(nw, CTRL_ATTR_MCAST_GRP_ID, i + MIN_GROUP_NUM); 348 nlattr_add_string(nw, CTRL_ATTR_MCAST_GRP_NAME, gg->group_name); 349 nlattr_set_len(nw, cmd_off); 350 } 351 nlattr_set_len(nw, off); 352 } 353 if (nlmsg_end(nw)) 354 return (0); 355 enomem: 356 NL_LOG(LOG_DEBUG, "unable to dump family %s state (ENOMEM)", gf->family_name); 357 nlmsg_abort(nw); 358 return (ENOMEM); 359 } 360 361 362 /* Declare ourself as a user */ 363 #define CTRL_FAMILY_NAME "nlctrl" 364 365 static uint32_t ctrl_family_id; 366 static uint32_t ctrl_group_id; 367 368 struct nl_parsed_family { 369 uint32_t family_id; 370 char *family_name; 371 uint8_t version; 372 }; 373 374 #define _IN(_field) offsetof(struct genlmsghdr, _field) 375 #define _OUT(_field) offsetof(struct nl_parsed_family, _field) 376 static const struct nlfield_parser nlf_p_generic[] = { 377 { .off_in = _IN(version), .off_out = _OUT(version), .cb = nlf_get_u8 }, 378 }; 379 380 static struct nlattr_parser nla_p_generic[] = { 381 { .type = CTRL_ATTR_FAMILY_ID , .off = _OUT(family_id), .cb = nlattr_get_uint32 }, 382 { .type = CTRL_ATTR_FAMILY_NAME , .off = _OUT(family_id), .cb = nlattr_get_string }, 383 }; 384 #undef _IN 385 #undef _OUT 386 NL_DECLARE_PARSER(genl_parser, struct genlmsghdr, nlf_p_generic, nla_p_generic); 387 388 static int 389 nlctrl_handle_getfamily(struct nlmsghdr *hdr, struct nl_pstate *npt) 390 { 391 int error = 0; 392 393 struct nl_parsed_family attrs = {}; 394 error = nl_parse_nlmsg(hdr, &genl_parser, npt, &attrs); 395 if (error != 0) 396 return (error); 397 398 struct genlmsghdr ghdr = { 399 .cmd = CTRL_CMD_NEWFAMILY, 400 }; 401 402 for (int i = 0; i < MAX_FAMILIES; i++) { 403 struct genl_family *gf = &families[i]; 404 if (gf->family_name == NULL) 405 continue; 406 if (attrs.family_id != 0 && attrs.family_id != gf->family_id) 407 continue; 408 if (attrs.family_name != NULL && strcmp(attrs.family_name, gf->family_name)) 409 continue; 410 error = dump_family(hdr, &ghdr, &families[i], npt->nw); 411 if (error != 0) 412 break; 413 } 414 415 return (error); 416 } 417 418 static void 419 nlctrl_notify(const struct genl_family *gf, int cmd) 420 { 421 struct nlmsghdr hdr = {.nlmsg_type = NETLINK_GENERIC }; 422 struct genlmsghdr ghdr = { .cmd = cmd }; 423 struct nl_writer nw = {}; 424 425 if (nlmsg_get_group_writer(&nw, NLMSG_SMALL, NETLINK_GENERIC, ctrl_group_id)) { 426 dump_family(&hdr, &ghdr, gf, &nw); 427 nlmsg_flush(&nw); 428 return; 429 } 430 NL_LOG(LOG_DEBUG, "error allocating group writer"); 431 } 432 433 static const struct genl_cmd nlctrl_cmds[] = { 434 { 435 .cmd_num = CTRL_CMD_GETFAMILY, 436 .cmd_name = "GETFAMILY", 437 .cmd_cb = nlctrl_handle_getfamily, 438 .cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP, GENL_CMD_CAP_HASPOL, 439 }, 440 }; 441 442 static void 443 genl_nlctrl_init() 444 { 445 ctrl_family_id = genl_register_family(CTRL_FAMILY_NAME, 0, 2, CTRL_ATTR_MAX); 446 genl_register_cmds(CTRL_FAMILY_NAME, nlctrl_cmds, NL_ARRAY_LEN(nlctrl_cmds)); 447 ctrl_group_id = genl_register_group(CTRL_FAMILY_NAME, "notify"); 448 } 449 450 static void 451 genl_nlctrl_destroy() 452 { 453 genl_unregister_family(CTRL_FAMILY_NAME); 454 } 455 456 static const struct nlhdr_parser *all_parsers[] = { &genl_parser }; 457 458 static void 459 genl_load(void *u __unused) 460 { 461 GENL_LOCK_INIT(); 462 NL_VERIFY_PARSERS(all_parsers); 463 netlink_register_proto(NETLINK_GENERIC, "NETLINK_GENERIC", genl_handle_message); 464 genl_nlctrl_init(); 465 } 466 SYSINIT(genl_load, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, genl_load, NULL); 467 468 static void 469 genl_unload(void *u __unused) 470 { 471 genl_nlctrl_destroy(); 472 GENL_LOCK_DESTROY(); 473 epoch_wait_preempt(net_epoch_preempt); 474 } 475 SYSUNINIT(genl_unload, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, genl_unload, NULL); 476