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/epoch.h> 33 #include <sys/kernel.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 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 64 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 * unregister groups of a given family 138 */ 139 static void 140 unregister_groups(const struct genl_family *gf) 141 { 142 143 for (int i = 0; i < MAX_GROUPS; i++) { 144 struct genl_group *gg = &groups[i]; 145 if (gg->group_family == gf && gg->group_name != NULL) { 146 gg->group_family = NULL; 147 gg->group_name = NULL; 148 } 149 } 150 } 151 152 /* 153 * Can sleep, I guess 154 */ 155 bool 156 genl_unregister_family(const char *family_name) 157 { 158 bool found = false; 159 160 GENL_LOCK(); 161 struct genl_family *gf = find_family(family_name); 162 163 if (gf != NULL) { 164 nlctrl_notify(gf, CTRL_CMD_DELFAMILY); 165 found = true; 166 unregister_groups(gf); 167 /* TODO: zero pointer first */ 168 free_family(gf); 169 bzero(gf, sizeof(*gf)); 170 } 171 GENL_UNLOCK(); 172 173 return (found); 174 } 175 176 bool 177 genl_register_cmds(const char *family_name, const struct genl_cmd *cmds, int count) 178 { 179 GENL_LOCK(); 180 struct genl_family *gf = find_family(family_name); 181 if (gf == NULL) { 182 GENL_UNLOCK(); 183 return (false); 184 } 185 186 int cmd_size = gf->family_cmd_size; 187 188 for (int i = 0; i < count; i++) { 189 MPASS(cmds[i].cmd_cb != NULL); 190 if (cmds[i].cmd_num >= cmd_size) 191 cmd_size = cmds[i].cmd_num + 1; 192 } 193 194 if (cmd_size > gf->family_cmd_size) { 195 /* need to realloc */ 196 size_t sz = cmd_size * sizeof(struct genl_cmd); 197 void *data = malloc(sz, M_NETLINK, M_WAITOK | M_ZERO); 198 199 memcpy(data, gf->family_cmds, gf->family_cmd_size * sizeof(struct genl_cmd)); 200 void *old_data = gf->family_cmds; 201 gf->family_cmds = data; 202 gf->family_cmd_size = cmd_size; 203 free(old_data, M_NETLINK); 204 } 205 206 for (int i = 0; i < count; i++) { 207 const struct genl_cmd *cmd = &cmds[i]; 208 MPASS(gf->family_cmds[cmd->cmd_num].cmd_cb == NULL); 209 gf->family_cmds[cmd->cmd_num] = cmds[i]; 210 NL_LOG(LOG_DEBUG2, "Adding cmd %s(%d) to family %s", 211 cmd->cmd_name, cmd->cmd_num, gf->family_name); 212 } 213 GENL_UNLOCK(); 214 return (true); 215 } 216 217 static struct genl_group * 218 find_group(const struct genl_family *gf, const char *group_name) 219 { 220 for (int i = 0; i < MAX_GROUPS; i++) { 221 struct genl_group *gg = &groups[i]; 222 if (gg->group_family == gf && !strcmp(gg->group_name, group_name)) 223 return (gg); 224 } 225 return (NULL); 226 } 227 228 uint32_t 229 genl_register_group(const char *family_name, const char *group_name) 230 { 231 uint32_t group_id = 0; 232 233 MPASS(family_name != NULL); 234 MPASS(group_name != NULL); 235 236 GENL_LOCK(); 237 struct genl_family *gf = find_family(family_name); 238 239 if (gf == NULL || find_group(gf, group_name) != NULL) { 240 GENL_UNLOCK(); 241 return (0); 242 } 243 244 for (int i = 0; i < MAX_GROUPS; i++) { 245 struct genl_group *gg = &groups[i]; 246 if (gg->group_family == NULL) { 247 gf->family_num_groups++; 248 gg->group_family = gf; 249 gg->group_name = group_name; 250 group_id = i + MIN_GROUP_NUM; 251 break; 252 } 253 } 254 GENL_UNLOCK(); 255 256 return (group_id); 257 } 258 259 /* 260 * Handler called by netlink subsystem when matching netlink message is received 261 */ 262 static int 263 genl_handle_message(struct nlmsghdr *hdr, struct nl_pstate *npt) 264 { 265 struct nlpcb *nlp = npt->nlp; 266 int error = 0; 267 268 int family_id = (int)hdr->nlmsg_type - GENL_MIN_ID; 269 270 if (__predict_false(family_id < 0 || family_id >= MAX_FAMILIES)) { 271 NLP_LOG(LOG_DEBUG, nlp, "invalid message type: %d", hdr->nlmsg_type); 272 return (ENOTSUP); 273 } 274 275 if (__predict_false(hdr->nlmsg_len < sizeof(hdr) + GENL_HDRLEN)) { 276 NLP_LOG(LOG_DEBUG, nlp, "invalid message size: %d", hdr->nlmsg_len); 277 return (EINVAL); 278 } 279 280 struct genl_family *gf = &families[family_id]; 281 282 struct genlmsghdr *ghdr = (struct genlmsghdr *)(hdr + 1); 283 284 if (ghdr->cmd >= gf->family_cmd_size || gf->family_cmds[ghdr->cmd].cmd_cb == NULL) { 285 NLP_LOG(LOG_DEBUG, nlp, "family %s: invalid cmd %d", 286 gf->family_name, ghdr->cmd); 287 return (ENOTSUP); 288 } 289 290 struct genl_cmd *cmd = &gf->family_cmds[ghdr->cmd]; 291 292 if (cmd->cmd_priv != 0 && !nlp_has_priv(nlp, cmd->cmd_priv)) { 293 NLP_LOG(LOG_DEBUG, nlp, "family %s: cmd %d priv_check() failed", 294 gf->family_name, ghdr->cmd); 295 return (EPERM); 296 } 297 298 NLP_LOG(LOG_DEBUG2, nlp, "received family %s cmd %s(%d) len %d", 299 gf->family_name, cmd->cmd_name, ghdr->cmd, hdr->nlmsg_len); 300 301 error = cmd->cmd_cb(hdr, npt); 302 303 return (error); 304 } 305 306 static uint32_t 307 get_cmd_flags(const struct genl_cmd *cmd) 308 { 309 uint32_t flags = cmd->cmd_flags; 310 if (cmd->cmd_priv != 0) 311 flags |= GENL_ADMIN_PERM; 312 return (flags); 313 } 314 315 static int 316 dump_family(struct nlmsghdr *hdr, struct genlmsghdr *ghdr, 317 const struct genl_family *gf, struct nl_writer *nw) 318 { 319 if (!nlmsg_reply(nw, hdr, sizeof(struct genlmsghdr))) 320 goto enomem; 321 322 struct genlmsghdr *ghdr_new = nlmsg_reserve_object(nw, struct genlmsghdr); 323 ghdr_new->cmd = ghdr->cmd; 324 ghdr_new->version = gf->family_version; 325 ghdr_new->reserved = 0; 326 327 nlattr_add_string(nw, CTRL_ATTR_FAMILY_NAME, gf->family_name); 328 nlattr_add_u16(nw, CTRL_ATTR_FAMILY_ID, gf->family_id); 329 nlattr_add_u32(nw, CTRL_ATTR_VERSION, gf->family_version); 330 nlattr_add_u32(nw, CTRL_ATTR_HDRSIZE, gf->family_hdrsize); 331 nlattr_add_u32(nw, CTRL_ATTR_MAXATTR, gf->family_attr_max); 332 333 if (gf->family_cmd_size > 0) { 334 int off = nlattr_add_nested(nw, CTRL_ATTR_OPS); 335 if (off == 0) 336 goto enomem; 337 for (int i = 0, cnt=0; i < gf->family_cmd_size; i++) { 338 struct genl_cmd *cmd = &gf->family_cmds[i]; 339 if (cmd->cmd_cb == NULL) 340 continue; 341 int cmd_off = nlattr_add_nested(nw, ++cnt); 342 if (cmd_off == 0) 343 goto enomem; 344 345 nlattr_add_u32(nw, CTRL_ATTR_OP_ID, cmd->cmd_num); 346 nlattr_add_u32(nw, CTRL_ATTR_OP_FLAGS, get_cmd_flags(cmd)); 347 nlattr_set_len(nw, cmd_off); 348 } 349 nlattr_set_len(nw, off); 350 } 351 if (gf->family_num_groups > 0) { 352 int off = nlattr_add_nested(nw, CTRL_ATTR_MCAST_GROUPS); 353 if (off == 0) 354 goto enomem; 355 for (int i = 0, cnt = 0; i < MAX_GROUPS; i++) { 356 struct genl_group *gg = &groups[i]; 357 if (gg->group_family != gf) 358 continue; 359 360 int cmd_off = nlattr_add_nested(nw, ++cnt); 361 if (cmd_off == 0) 362 goto enomem; 363 nlattr_add_u32(nw, CTRL_ATTR_MCAST_GRP_ID, i + MIN_GROUP_NUM); 364 nlattr_add_string(nw, CTRL_ATTR_MCAST_GRP_NAME, gg->group_name); 365 nlattr_set_len(nw, cmd_off); 366 } 367 nlattr_set_len(nw, off); 368 } 369 if (nlmsg_end(nw)) 370 return (0); 371 enomem: 372 NL_LOG(LOG_DEBUG, "unable to dump family %s state (ENOMEM)", gf->family_name); 373 nlmsg_abort(nw); 374 return (ENOMEM); 375 } 376 377 378 /* Declare ourself as a user */ 379 #define CTRL_FAMILY_NAME "nlctrl" 380 381 static uint32_t ctrl_family_id; 382 static uint32_t ctrl_group_id; 383 384 struct nl_parsed_family { 385 uint32_t family_id; 386 char *family_name; 387 uint8_t version; 388 }; 389 390 #define _IN(_field) offsetof(struct genlmsghdr, _field) 391 #define _OUT(_field) offsetof(struct nl_parsed_family, _field) 392 static const struct nlfield_parser nlf_p_generic[] = { 393 { .off_in = _IN(version), .off_out = _OUT(version), .cb = nlf_get_u8 }, 394 }; 395 396 static struct nlattr_parser nla_p_generic[] = { 397 { .type = CTRL_ATTR_FAMILY_ID , .off = _OUT(family_id), .cb = nlattr_get_uint16 }, 398 { .type = CTRL_ATTR_FAMILY_NAME , .off = _OUT(family_name), .cb = nlattr_get_string }, 399 }; 400 #undef _IN 401 #undef _OUT 402 NL_DECLARE_PARSER(genl_parser, struct genlmsghdr, nlf_p_generic, nla_p_generic); 403 404 static bool 405 match_family(const struct genl_family *gf, const struct nl_parsed_family *attrs) 406 { 407 if (gf->family_name == NULL) 408 return (false); 409 if (attrs->family_id != 0 && attrs->family_id != gf->family_id) 410 return (false); 411 if (attrs->family_name != NULL && strcmp(attrs->family_name, gf->family_name)) 412 return (false); 413 return (true); 414 } 415 416 static int 417 nlctrl_handle_getfamily(struct nlmsghdr *hdr, struct nl_pstate *npt) 418 { 419 int error = 0; 420 421 struct nl_parsed_family attrs = {}; 422 error = nl_parse_nlmsg(hdr, &genl_parser, npt, &attrs); 423 if (error != 0) 424 return (error); 425 426 struct genlmsghdr ghdr = { 427 .cmd = CTRL_CMD_NEWFAMILY, 428 }; 429 430 if (attrs.family_id != 0 || attrs.family_name != NULL) { 431 /* Resolve request */ 432 for (int i = 0; i < MAX_FAMILIES; i++) { 433 struct genl_family *gf = &families[i]; 434 if (match_family(gf, &attrs)) { 435 error = dump_family(hdr, &ghdr, gf, npt->nw); 436 return (error); 437 } 438 } 439 return (ENOENT); 440 } 441 442 hdr->nlmsg_flags = hdr->nlmsg_flags | NLM_F_MULTI; 443 for (int i = 0; i < MAX_FAMILIES; i++) { 444 struct genl_family *gf = &families[i]; 445 if (match_family(gf, &attrs)) { 446 error = dump_family(hdr, &ghdr, gf, npt->nw); 447 if (error != 0) 448 break; 449 } 450 } 451 452 if (!nlmsg_end_dump(npt->nw, error, hdr)) { 453 NL_LOG(LOG_DEBUG, "Unable to finalize the dump"); 454 return (ENOMEM); 455 } 456 457 return (error); 458 } 459 460 static void 461 nlctrl_notify(const struct genl_family *gf, int cmd) 462 { 463 struct nlmsghdr hdr = {.nlmsg_type = NETLINK_GENERIC }; 464 struct genlmsghdr ghdr = { .cmd = cmd }; 465 struct nl_writer nw = {}; 466 467 if (nlmsg_get_group_writer(&nw, NLMSG_SMALL, NETLINK_GENERIC, ctrl_group_id)) { 468 dump_family(&hdr, &ghdr, gf, &nw); 469 nlmsg_flush(&nw); 470 return; 471 } 472 NL_LOG(LOG_DEBUG, "error allocating group writer"); 473 } 474 475 static const struct genl_cmd nlctrl_cmds[] = { 476 { 477 .cmd_num = CTRL_CMD_GETFAMILY, 478 .cmd_name = "GETFAMILY", 479 .cmd_cb = nlctrl_handle_getfamily, 480 .cmd_flags = GENL_CMD_CAP_DO | GENL_CMD_CAP_DUMP | GENL_CMD_CAP_HASPOL, 481 }, 482 }; 483 484 static void 485 genl_nlctrl_init(void) 486 { 487 ctrl_family_id = genl_register_family(CTRL_FAMILY_NAME, 0, 2, CTRL_ATTR_MAX); 488 genl_register_cmds(CTRL_FAMILY_NAME, nlctrl_cmds, NL_ARRAY_LEN(nlctrl_cmds)); 489 ctrl_group_id = genl_register_group(CTRL_FAMILY_NAME, "notify"); 490 } 491 492 static void 493 genl_nlctrl_destroy(void) 494 { 495 genl_unregister_family(CTRL_FAMILY_NAME); 496 } 497 498 static const struct nlhdr_parser *all_parsers[] = { &genl_parser }; 499 500 static void 501 genl_load(void *u __unused) 502 { 503 GENL_LOCK_INIT(); 504 NL_VERIFY_PARSERS(all_parsers); 505 netlink_register_proto(NETLINK_GENERIC, "NETLINK_GENERIC", genl_handle_message); 506 genl_nlctrl_init(); 507 } 508 SYSINIT(genl_load, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, genl_load, NULL); 509 510 static void 511 genl_unload(void *u __unused) 512 { 513 genl_nlctrl_destroy(); 514 GENL_LOCK_DESTROY(); 515 NET_EPOCH_WAIT(); 516 } 517 SYSUNINIT(genl_unload, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, genl_unload, NULL); 518