1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org> 5 * Copyright (c) 1980, 1986, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/eventhandler.h> 35 #include <sys/malloc.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/kernel.h> 40 #include <sys/systm.h> 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 44 #include <net/if.h> 45 #include <net/if_var.h> 46 #include <net/if_private.h> 47 #include <net/if_clone.h> 48 #include <net/radix.h> 49 #include <net/route.h> 50 #include <net/vnet.h> 51 52 #include <netlink/netlink.h> 53 #include <netlink/netlink_ctl.h> 54 #include <netlink/netlink_route.h> 55 #include <netlink/route/route_var.h> 56 57 /* Current IF_MAXUNIT expands maximum to 5 characters. */ 58 #define IFCLOSIZ (IFNAMSIZ - 5) 59 60 /* 61 * Structure describing a `cloning' interface. 62 * 63 * List of locks 64 * (c) const until freeing 65 * (d) driver specific data, may need external protection. 66 * (e) locked by if_cloners_mtx 67 * (i) locked by ifc_mtx mtx 68 */ 69 struct if_clone { 70 char ifc_name[IFCLOSIZ]; /* (c) Name of device, e.g. `gif' */ 71 struct unrhdr *ifc_unrhdr; /* (c) alloc_unr(9) header */ 72 int ifc_maxunit; /* (c) maximum unit number */ 73 int ifc_flags; 74 long ifc_refcnt; /* (i) Reference count. */ 75 LIST_HEAD(, ifnet) ifc_iflist; /* (i) List of cloned interfaces */ 76 struct mtx ifc_mtx; /* Mutex to protect members. */ 77 78 ifc_match_f *ifc_match; /* (c) Matcher function */ 79 ifc_create_f *ifc_create; /* (c) Creates new interface */ 80 ifc_destroy_f *ifc_destroy; /* (c) Destroys cloned interface */ 81 82 ifc_create_nl_f *create_nl; /* (c) Netlink creation handler */ 83 ifc_modify_nl_f *modify_nl; /* (c) Netlink modification handler */ 84 ifc_dump_nl_f *dump_nl; /* (c) Netlink dump handler */ 85 86 #ifdef CLONE_COMPAT_13 87 /* (c) Driver specific cloning functions. Called with no locks held. */ 88 union { 89 struct { /* advanced cloner */ 90 ifc_create_t *_ifc_create; 91 ifc_destroy_t *_ifc_destroy; 92 } A; 93 struct { /* simple cloner */ 94 ifcs_create_t *_ifcs_create; 95 ifcs_destroy_t *_ifcs_destroy; 96 int _ifcs_minifs; /* minimum ifs */ 97 98 } S; 99 } U; 100 #define ifca_create U.A._ifc_create 101 #define ifca_destroy U.A._ifc_destroy 102 #define ifcs_create U.S._ifcs_create 103 #define ifcs_destroy U.S._ifcs_destroy 104 #define ifcs_minifs U.S._ifcs_minifs 105 #endif 106 107 LIST_ENTRY(if_clone) ifc_list; /* (e) On list of cloners */ 108 }; 109 110 111 112 static void if_clone_free(struct if_clone *ifc); 113 static int if_clone_createif_nl(struct if_clone *ifc, const char *name, 114 struct ifc_data_nl *ifd); 115 116 static int ifc_simple_match(struct if_clone *ifc, const char *name); 117 static int ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit); 118 static struct if_clone *ifc_find_cloner(const char *name); 119 static struct if_clone *ifc_find_cloner_match(const char *name); 120 121 #ifdef CLONE_COMPAT_13 122 static int ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen, 123 struct ifc_data *ifc_data, struct ifnet **ifpp); 124 static int ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen, 125 struct ifc_data *ifc_data, struct ifnet **ifpp); 126 #endif 127 128 static struct mtx if_cloners_mtx; 129 MTX_SYSINIT(if_cloners_lock, &if_cloners_mtx, "if_cloners lock", MTX_DEF); 130 VNET_DEFINE_STATIC(int, if_cloners_count); 131 VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners); 132 133 #define V_if_cloners_count VNET(if_cloners_count) 134 #define V_if_cloners VNET(if_cloners) 135 136 #define IF_CLONERS_LOCK_ASSERT() mtx_assert(&if_cloners_mtx, MA_OWNED) 137 #define IF_CLONERS_LOCK() mtx_lock(&if_cloners_mtx) 138 #define IF_CLONERS_UNLOCK() mtx_unlock(&if_cloners_mtx) 139 140 #define IF_CLONE_LOCK_INIT(ifc) \ 141 mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF) 142 #define IF_CLONE_LOCK_DESTROY(ifc) mtx_destroy(&(ifc)->ifc_mtx) 143 #define IF_CLONE_LOCK_ASSERT(ifc) mtx_assert(&(ifc)->ifc_mtx, MA_OWNED) 144 #define IF_CLONE_LOCK(ifc) mtx_lock(&(ifc)->ifc_mtx) 145 #define IF_CLONE_UNLOCK(ifc) mtx_unlock(&(ifc)->ifc_mtx) 146 147 #define IF_CLONE_ADDREF(ifc) \ 148 do { \ 149 IF_CLONE_LOCK(ifc); \ 150 IF_CLONE_ADDREF_LOCKED(ifc); \ 151 IF_CLONE_UNLOCK(ifc); \ 152 } while (0) 153 #define IF_CLONE_ADDREF_LOCKED(ifc) \ 154 do { \ 155 IF_CLONE_LOCK_ASSERT(ifc); \ 156 KASSERT((ifc)->ifc_refcnt >= 0, \ 157 ("negative refcnt %ld", (ifc)->ifc_refcnt)); \ 158 (ifc)->ifc_refcnt++; \ 159 } while (0) 160 #define IF_CLONE_REMREF(ifc) \ 161 do { \ 162 IF_CLONE_LOCK(ifc); \ 163 IF_CLONE_REMREF_LOCKED(ifc); \ 164 } while (0) 165 #define IF_CLONE_REMREF_LOCKED(ifc) \ 166 do { \ 167 IF_CLONE_LOCK_ASSERT(ifc); \ 168 KASSERT((ifc)->ifc_refcnt > 0, \ 169 ("bogus refcnt %ld", (ifc)->ifc_refcnt)); \ 170 if (--(ifc)->ifc_refcnt == 0) { \ 171 IF_CLONE_UNLOCK(ifc); \ 172 if_clone_free(ifc); \ 173 } else { \ 174 /* silently free the lock */ \ 175 IF_CLONE_UNLOCK(ifc); \ 176 } \ 177 } while (0) 178 179 #define IFC_IFLIST_INSERT(_ifc, _ifp) \ 180 LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones) 181 #define IFC_IFLIST_REMOVE(_ifc, _ifp) \ 182 LIST_REMOVE(_ifp, if_clones) 183 184 static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework"); 185 186 void 187 vnet_if_clone_init(void) 188 { 189 190 LIST_INIT(&V_if_cloners); 191 } 192 193 /* 194 * Lookup and create a clone network interface. 195 */ 196 int 197 ifc_create_ifp(const char *name, struct ifc_data *ifd, struct ifnet **ifpp) 198 { 199 struct if_clone *ifc = ifc_find_cloner_match(name); 200 201 if (ifc == NULL) 202 return (EINVAL); 203 204 struct ifc_data_nl ifd_new = { 205 .flags = ifd->flags, 206 .unit = ifd->unit, 207 .params = ifd->params, 208 }; 209 210 int error = if_clone_createif_nl(ifc, name, &ifd_new); 211 212 if (ifpp != NULL) 213 *ifpp = ifd_new.ifp; 214 215 return (error); 216 } 217 218 bool 219 ifc_create_ifp_nl(const char *name, struct ifc_data_nl *ifd) 220 { 221 struct if_clone *ifc = ifc_find_cloner_match(name); 222 if (ifc == NULL) { 223 ifd->error = EINVAL; 224 return (false); 225 } 226 227 ifd->error = if_clone_createif_nl(ifc, name, ifd); 228 229 return (true); 230 } 231 232 int 233 if_clone_create(char *name, size_t len, caddr_t params) 234 { 235 struct ifc_data ifd = { .params = params }; 236 struct ifnet *ifp; 237 238 int error = ifc_create_ifp(name, &ifd, &ifp); 239 240 if (error == 0) 241 strlcpy(name, if_name(ifp), len); 242 243 return (error); 244 } 245 246 bool 247 ifc_modify_ifp_nl(struct ifnet *ifp, struct ifc_data_nl *ifd) 248 { 249 struct if_clone *ifc = ifc_find_cloner(ifp->if_dname); 250 if (ifc == NULL) { 251 ifd->error = EINVAL; 252 return (false); 253 } 254 255 ifd->error = (*ifc->modify_nl)(ifp, ifd); 256 return (true); 257 } 258 259 bool 260 ifc_dump_ifp_nl(struct ifnet *ifp, struct nl_writer *nw) 261 { 262 struct if_clone *ifc = ifc_find_cloner(ifp->if_dname); 263 if (ifc == NULL) 264 return (false); 265 266 (*ifc->dump_nl)(ifp, nw); 267 return (true); 268 } 269 270 static int 271 ifc_create_ifp_nl_default(struct if_clone *ifc, char *name, size_t len, 272 struct ifc_data_nl *ifd) 273 { 274 struct ifc_data ifd_new = { 275 .flags = ifd->flags, 276 .unit = ifd->unit, 277 .params = ifd->params, 278 }; 279 280 return ((*ifc->ifc_create)(ifc, name, len, &ifd_new, &ifd->ifp)); 281 } 282 283 static int 284 ifc_modify_ifp_nl_default(struct ifnet *ifp, struct ifc_data_nl *ifd) 285 { 286 if (ifd->lattrs != NULL) 287 return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt)); 288 return (0); 289 } 290 291 static void 292 ifc_dump_ifp_nl_default(struct ifnet *ifp, struct nl_writer *nw) 293 { 294 int off = nlattr_add_nested(nw, IFLA_LINKINFO); 295 296 if (off != 0) { 297 nlattr_add_string(nw, IFLA_INFO_KIND, ifp->if_dname); 298 nlattr_set_len(nw, off); 299 } 300 } 301 302 void 303 ifc_link_ifp(struct if_clone *ifc, struct ifnet *ifp) 304 { 305 306 if_addgroup(ifp, ifc->ifc_name); 307 308 IF_CLONE_LOCK(ifc); 309 IFC_IFLIST_INSERT(ifc, ifp); 310 IF_CLONE_UNLOCK(ifc); 311 } 312 313 void 314 if_clone_addif(struct if_clone *ifc, struct ifnet *ifp) 315 { 316 ifc_link_ifp(ifc, ifp); 317 } 318 319 bool 320 ifc_unlink_ifp(struct if_clone *ifc, struct ifnet *ifp) 321 { 322 struct ifnet *ifcifp; 323 324 IF_CLONE_LOCK(ifc); 325 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) { 326 if (ifcifp == ifp) { 327 IFC_IFLIST_REMOVE(ifc, ifp); 328 break; 329 } 330 } 331 IF_CLONE_UNLOCK(ifc); 332 333 if (ifcifp != NULL) 334 if_delgroup(ifp, ifc->ifc_name); 335 336 return (ifcifp != NULL); 337 } 338 339 static struct if_clone * 340 ifc_find_cloner_match(const char *name) 341 { 342 struct if_clone *ifc; 343 344 IF_CLONERS_LOCK(); 345 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) { 346 if (ifc->ifc_match(ifc, name)) 347 break; 348 } 349 IF_CLONERS_UNLOCK(); 350 351 return (ifc); 352 } 353 354 static struct if_clone * 355 ifc_find_cloner(const char *name) 356 { 357 struct if_clone *ifc; 358 359 IF_CLONERS_LOCK(); 360 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) { 361 if (strcmp(ifc->ifc_name, name) == 0) { 362 break; 363 } 364 } 365 IF_CLONERS_UNLOCK(); 366 367 return (ifc); 368 } 369 370 static struct if_clone * 371 ifc_find_cloner_in_vnet(const char *name, struct vnet *vnet) 372 { 373 CURVNET_SET_QUIET(vnet); 374 struct if_clone *ifc = ifc_find_cloner(name); 375 CURVNET_RESTORE(); 376 377 return (ifc); 378 } 379 380 /* 381 * Create a clone network interface. 382 */ 383 static int 384 if_clone_createif_nl(struct if_clone *ifc, const char *ifname, struct ifc_data_nl *ifd) 385 { 386 char name[IFNAMSIZ]; 387 int error; 388 389 strlcpy(name, ifname, sizeof(name)); 390 391 if (ifunit(name) != NULL) 392 return (EEXIST); 393 394 if (ifc->ifc_flags & IFC_F_AUTOUNIT) { 395 if ((error = ifc_handle_unit(ifc, name, sizeof(name), &ifd->unit)) != 0) 396 return (error); 397 } 398 399 if (ifd->lattrs != NULL) 400 error = (*ifc->create_nl)(ifc, name, sizeof(name), ifd); 401 else 402 error = ifc_create_ifp_nl_default(ifc, name, sizeof(name), ifd); 403 if (error != 0) { 404 if (ifc->ifc_flags & IFC_F_AUTOUNIT) 405 ifc_free_unit(ifc, ifd->unit); 406 return (error); 407 } 408 409 MPASS(ifd->ifp != NULL); 410 if_clone_addif(ifc, ifd->ifp); 411 412 if (ifd->lattrs != NULL) 413 error = (*ifc->modify_nl)(ifd->ifp, ifd); 414 415 return (error); 416 } 417 418 /* 419 * Lookup and destroy a clone network interface. 420 */ 421 int 422 if_clone_destroy(const char *name) 423 { 424 int err; 425 struct if_clone *ifc; 426 struct ifnet *ifp; 427 428 ifp = ifunit_ref(name); 429 if (ifp == NULL) 430 return (ENXIO); 431 432 ifc = ifc_find_cloner_in_vnet(ifp->if_dname, ifp->if_home_vnet); 433 if (ifc == NULL) { 434 if_rele(ifp); 435 return (EINVAL); 436 } 437 438 err = if_clone_destroyif(ifc, ifp); 439 if_rele(ifp); 440 return err; 441 } 442 443 /* 444 * Destroy a clone network interface. 445 */ 446 static int 447 if_clone_destroyif_flags(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 448 { 449 int err; 450 451 /* 452 * Given that the cloned ifnet might be attached to a different 453 * vnet from where its cloner was registered, we have to 454 * switch to the vnet context of the target vnet. 455 */ 456 CURVNET_SET_QUIET(ifp->if_vnet); 457 458 if (!ifc_unlink_ifp(ifc, ifp)) { 459 CURVNET_RESTORE(); 460 return (ENXIO); /* ifp is not on the list. */ 461 } 462 463 int unit = ifp->if_dunit; 464 err = (*ifc->ifc_destroy)(ifc, ifp, flags); 465 466 if (err != 0) 467 ifc_link_ifp(ifc, ifp); 468 else if (ifc->ifc_flags & IFC_F_AUTOUNIT) 469 ifc_free_unit(ifc, unit); 470 CURVNET_RESTORE(); 471 return (err); 472 } 473 474 int 475 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp) 476 { 477 return (if_clone_destroyif_flags(ifc, ifp, 0)); 478 } 479 480 static struct if_clone * 481 if_clone_alloc(const char *name, int maxunit) 482 { 483 struct if_clone *ifc; 484 485 KASSERT(name != NULL, ("%s: no name\n", __func__)); 486 487 ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO); 488 strncpy(ifc->ifc_name, name, IFCLOSIZ-1); 489 IF_CLONE_LOCK_INIT(ifc); 490 IF_CLONE_ADDREF(ifc); 491 ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT; 492 ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx); 493 LIST_INIT(&ifc->ifc_iflist); 494 495 ifc->create_nl = ifc_create_ifp_nl_default; 496 ifc->modify_nl = ifc_modify_ifp_nl_default; 497 ifc->dump_nl = ifc_dump_ifp_nl_default; 498 499 return (ifc); 500 } 501 502 static int 503 if_clone_attach(struct if_clone *ifc) 504 { 505 struct if_clone *ifc1; 506 507 IF_CLONERS_LOCK(); 508 LIST_FOREACH(ifc1, &V_if_cloners, ifc_list) 509 if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) { 510 IF_CLONERS_UNLOCK(); 511 IF_CLONE_REMREF(ifc); 512 return (EEXIST); 513 } 514 LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list); 515 V_if_cloners_count++; 516 IF_CLONERS_UNLOCK(); 517 518 return (0); 519 } 520 521 struct if_clone * 522 ifc_attach_cloner(const char *name, struct if_clone_addreq *req) 523 { 524 if (req->create_f == NULL || req->destroy_f == NULL) 525 return (NULL); 526 if (strnlen(name, IFCLOSIZ) >= (IFCLOSIZ - 1)) 527 return (NULL); 528 529 struct if_clone *ifc = if_clone_alloc(name, req->maxunit); 530 ifc->ifc_match = req->match_f != NULL ? req->match_f : ifc_simple_match; 531 ifc->ifc_create = req->create_f; 532 ifc->ifc_destroy = req->destroy_f; 533 ifc->ifc_flags = (req->flags & IFC_F_AUTOUNIT); 534 535 if (req->version == 2) { 536 struct if_clone_addreq_v2 *req2 = (struct if_clone_addreq_v2 *)req; 537 538 ifc->create_nl = req2->create_nl_f; 539 ifc->modify_nl = req2->modify_nl_f; 540 ifc->dump_nl = req2->dump_nl_f; 541 } 542 543 ifc->dump_nl = ifc_dump_ifp_nl_default; 544 545 if (if_clone_attach(ifc) != 0) 546 return (NULL); 547 548 EVENTHANDLER_INVOKE(if_clone_event, ifc); 549 550 return (ifc); 551 } 552 553 void 554 ifc_detach_cloner(struct if_clone *ifc) 555 { 556 if_clone_detach(ifc); 557 } 558 559 560 #ifdef CLONE_COMPAT_13 561 562 static int 563 ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen, 564 struct ifc_data *ifc_data, struct ifnet **ifpp) 565 { 566 int error = ifc->ifca_create(ifc, name, maxlen, ifc_data->params); 567 568 if (error == 0) 569 *ifpp = ifunit(name); 570 return (error); 571 } 572 573 static int 574 ifc_advanced_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 575 { 576 if (ifc->ifca_destroy == NULL) 577 return (ENOTSUP); 578 return (ifc->ifca_destroy(ifc, ifp)); 579 } 580 581 struct if_clone * 582 if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match, 583 ifc_create_t create, ifc_destroy_t destroy) 584 { 585 struct if_clone *ifc; 586 587 ifc = if_clone_alloc(name, maxunit); 588 ifc->ifc_match = match; 589 ifc->ifc_create = ifc_advanced_create_wrapper; 590 ifc->ifc_destroy = ifc_advanced_destroy_wrapper; 591 ifc->ifca_destroy = destroy; 592 ifc->ifca_create = create; 593 594 if (if_clone_attach(ifc) != 0) 595 return (NULL); 596 597 EVENTHANDLER_INVOKE(if_clone_event, ifc); 598 599 return (ifc); 600 } 601 602 static int 603 ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen, 604 struct ifc_data *ifc_data, struct ifnet **ifpp) 605 { 606 int unit = 0; 607 608 ifc_name2unit(name, &unit); 609 int error = ifc->ifcs_create(ifc, unit, ifc_data->params); 610 if (error == 0) 611 *ifpp = ifunit(name); 612 return (error); 613 } 614 615 static int 616 ifc_simple_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags) 617 { 618 if (ifp->if_dunit < ifc->ifcs_minifs && (flags & IFC_F_FORCE) == 0) 619 return (EINVAL); 620 621 ifc->ifcs_destroy(ifp); 622 return (0); 623 } 624 625 struct if_clone * 626 if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy, 627 u_int minifs) 628 { 629 struct if_clone *ifc; 630 u_int unit; 631 632 ifc = if_clone_alloc(name, 0); 633 ifc->ifc_match = ifc_simple_match; 634 ifc->ifc_create = ifc_simple_create_wrapper; 635 ifc->ifc_destroy = ifc_simple_destroy_wrapper; 636 ifc->ifcs_create = create; 637 ifc->ifcs_destroy = destroy; 638 ifc->ifcs_minifs = minifs; 639 ifc->ifc_flags = IFC_F_AUTOUNIT; 640 641 if (if_clone_attach(ifc) != 0) 642 return (NULL); 643 644 for (unit = 0; unit < minifs; unit++) { 645 char name[IFNAMSIZ]; 646 int error __unused; 647 struct ifc_data_nl ifd = {}; 648 649 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit); 650 error = if_clone_createif_nl(ifc, name, &ifd); 651 KASSERT(error == 0, 652 ("%s: failed to create required interface %s", 653 __func__, name)); 654 } 655 656 EVENTHANDLER_INVOKE(if_clone_event, ifc); 657 658 return (ifc); 659 } 660 #endif 661 662 /* 663 * Unregister a network interface cloner. 664 */ 665 void 666 if_clone_detach(struct if_clone *ifc) 667 { 668 669 IF_CLONERS_LOCK(); 670 LIST_REMOVE(ifc, ifc_list); 671 V_if_cloners_count--; 672 IF_CLONERS_UNLOCK(); 673 674 /* destroy all interfaces for this cloner */ 675 while (!LIST_EMPTY(&ifc->ifc_iflist)) 676 if_clone_destroyif_flags(ifc, LIST_FIRST(&ifc->ifc_iflist), IFC_F_FORCE); 677 678 IF_CLONE_REMREF(ifc); 679 } 680 681 static void 682 if_clone_free(struct if_clone *ifc) 683 { 684 685 KASSERT(LIST_EMPTY(&ifc->ifc_iflist), 686 ("%s: ifc_iflist not empty", __func__)); 687 688 IF_CLONE_LOCK_DESTROY(ifc); 689 delete_unrhdr(ifc->ifc_unrhdr); 690 free(ifc, M_CLONE); 691 } 692 693 /* 694 * Provide list of interface cloners to userspace. 695 */ 696 int 697 if_clone_list(struct if_clonereq *ifcr) 698 { 699 char *buf, *dst, *outbuf = NULL; 700 struct if_clone *ifc; 701 int buf_count, count, err = 0; 702 703 if (ifcr->ifcr_count < 0) 704 return (EINVAL); 705 706 IF_CLONERS_LOCK(); 707 /* 708 * Set our internal output buffer size. We could end up not 709 * reporting a cloner that is added between the unlock and lock 710 * below, but that's not a major problem. Not caping our 711 * allocation to the number of cloners actually in the system 712 * could be because that would let arbitrary users cause us to 713 * allocate arbitrary amounts of kernel memory. 714 */ 715 buf_count = (V_if_cloners_count < ifcr->ifcr_count) ? 716 V_if_cloners_count : ifcr->ifcr_count; 717 IF_CLONERS_UNLOCK(); 718 719 outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO); 720 721 IF_CLONERS_LOCK(); 722 723 ifcr->ifcr_total = V_if_cloners_count; 724 if ((dst = ifcr->ifcr_buffer) == NULL) { 725 /* Just asking how many there are. */ 726 goto done; 727 } 728 count = (V_if_cloners_count < buf_count) ? 729 V_if_cloners_count : buf_count; 730 731 for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf; 732 ifc != NULL && count != 0; 733 ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) { 734 strlcpy(buf, ifc->ifc_name, IFNAMSIZ); 735 } 736 737 done: 738 IF_CLONERS_UNLOCK(); 739 if (err == 0 && dst != NULL) 740 err = copyout(outbuf, dst, buf_count*IFNAMSIZ); 741 if (outbuf != NULL) 742 free(outbuf, M_CLONE); 743 return (err); 744 } 745 746 #ifdef VIMAGE 747 /* 748 * if_clone_restoregroup() is used in context of if_vmove(). 749 * 750 * Since if_detach_internal() has removed the interface from ALL groups, we 751 * need to "restore" interface membership in the cloner's group. Note that 752 * interface belongs to cloner in its home vnet, so we first find the original 753 * cloner, and then we confirm that cloner with the same name exists in the 754 * current vnet. 755 */ 756 void 757 if_clone_restoregroup(struct ifnet *ifp) 758 { 759 struct if_clone *ifc; 760 struct ifnet *ifcifp; 761 char ifc_name[IFCLOSIZ] = { [0] = '\0' }; 762 763 CURVNET_SET_QUIET(ifp->if_home_vnet); 764 IF_CLONERS_LOCK(); 765 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) { 766 IF_CLONE_LOCK(ifc); 767 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) { 768 if (ifp == ifcifp) { 769 strncpy(ifc_name, ifc->ifc_name, IFCLOSIZ-1); 770 break; 771 } 772 } 773 IF_CLONE_UNLOCK(ifc); 774 if (ifc_name[0] != '\0') 775 break; 776 } 777 CURVNET_RESTORE(); 778 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) 779 if (strcmp(ifc->ifc_name, ifc_name) == 0) 780 break; 781 IF_CLONERS_UNLOCK(); 782 783 if (ifc != NULL) 784 if_addgroup(ifp, ifc_name); 785 } 786 #endif 787 788 /* 789 * A utility function to extract unit numbers from interface names of 790 * the form name###. 791 * 792 * Returns 0 on success and an error on failure. 793 */ 794 int 795 ifc_name2unit(const char *name, int *unit) 796 { 797 const char *cp; 798 int cutoff = INT_MAX / 10; 799 int cutlim = INT_MAX % 10; 800 801 for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++) 802 ; 803 if (*cp == '\0') { 804 *unit = -1; 805 } else if (cp[0] == '0' && cp[1] != '\0') { 806 /* Disallow leading zeroes. */ 807 return (EINVAL); 808 } else { 809 for (*unit = 0; *cp != '\0'; cp++) { 810 if (*cp < '0' || *cp > '9') { 811 /* Bogus unit number. */ 812 return (EINVAL); 813 } 814 if (*unit > cutoff || 815 (*unit == cutoff && *cp - '0' > cutlim)) 816 return (EINVAL); 817 *unit = (*unit * 10) + (*cp - '0'); 818 } 819 } 820 821 return (0); 822 } 823 824 static int 825 ifc_alloc_unit_specific(struct if_clone *ifc, int *unit) 826 { 827 char name[IFNAMSIZ]; 828 829 if (*unit > ifc->ifc_maxunit) 830 return (ENOSPC); 831 832 if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1) 833 return (EEXIST); 834 835 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit); 836 if (ifunit(name) != NULL) { 837 free_unr(ifc->ifc_unrhdr, *unit); 838 return (EEXIST); 839 } 840 841 IF_CLONE_ADDREF(ifc); 842 843 return (0); 844 } 845 846 static int 847 ifc_alloc_unit_next(struct if_clone *ifc, int *unit) 848 { 849 int error; 850 851 *unit = alloc_unr(ifc->ifc_unrhdr); 852 if (*unit == -1) 853 return (ENOSPC); 854 855 free_unr(ifc->ifc_unrhdr, *unit); 856 for (;;) { 857 error = ifc_alloc_unit_specific(ifc, unit); 858 if (error != EEXIST) 859 break; 860 861 (*unit)++; 862 } 863 864 return (error); 865 } 866 867 int 868 ifc_alloc_unit(struct if_clone *ifc, int *unit) 869 { 870 if (*unit < 0) 871 return (ifc_alloc_unit_next(ifc, unit)); 872 else 873 return (ifc_alloc_unit_specific(ifc, unit)); 874 } 875 876 void 877 ifc_free_unit(struct if_clone *ifc, int unit) 878 { 879 880 free_unr(ifc->ifc_unrhdr, unit); 881 IF_CLONE_REMREF(ifc); 882 } 883 884 static int 885 ifc_simple_match(struct if_clone *ifc, const char *name) 886 { 887 const char *cp; 888 int i; 889 890 /* Match the name */ 891 for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) { 892 if (ifc->ifc_name[i] != *cp) 893 return (0); 894 } 895 896 /* Make sure there's a unit number or nothing after the name */ 897 for (; *cp != '\0'; cp++) { 898 if (*cp < '0' || *cp > '9') 899 return (0); 900 } 901 902 return (1); 903 } 904 905 static int 906 ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit) 907 { 908 char *dp; 909 int wildcard; 910 int unit; 911 int err; 912 913 err = ifc_name2unit(name, &unit); 914 if (err != 0) 915 return (err); 916 917 wildcard = (unit < 0); 918 919 err = ifc_alloc_unit(ifc, &unit); 920 if (err != 0) 921 return (err); 922 923 /* In the wildcard case, we need to update the name. */ 924 if (wildcard) { 925 for (dp = name; *dp != '\0'; dp++); 926 if (snprintf(dp, len - (dp-name), "%d", unit) > 927 len - (dp-name) - 1) { 928 /* 929 * This can only be a programmer error and 930 * there's no straightforward way to recover if 931 * it happens. 932 */ 933 panic("if_clone_create(): interface name too long"); 934 } 935 } 936 *punit = unit; 937 938 return (0); 939 } 940 941 int 942 ifc_copyin(const struct ifc_data *ifd, void *target, size_t len) 943 { 944 if (ifd->params == NULL) 945 return (EINVAL); 946 947 if (ifd->flags & IFC_F_SYSSPACE) { 948 memcpy(target, ifd->params, len); 949 return (0); 950 } else 951 return (copyin(ifd->params, target, len)); 952 } 953