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 * @(#)if.c 8.5 (Berkeley) 1/9/95 33 * $FreeBSD$ 34 */ 35 36 #include <sys/param.h> 37 #include <sys/eventhandler.h> 38 #include <sys/malloc.h> 39 #include <sys/limits.h> 40 #include <sys/lock.h> 41 #include <sys/mutex.h> 42 #include <sys/kernel.h> 43 #include <sys/systm.h> 44 #include <sys/types.h> 45 #include <sys/socket.h> 46 47 #include <net/if.h> 48 #include <net/if_var.h> 49 #include <net/if_clone.h> 50 #include <net/radix.h> 51 #include <net/route.h> 52 #include <net/vnet.h> 53 54 /* Current IF_MAXUNIT expands maximum to 5 characters. */ 55 #define IFCLOSIZ (IFNAMSIZ - 5) 56 57 /* 58 * Structure describing a `cloning' interface. 59 * 60 * List of locks 61 * (c) const until freeing 62 * (d) driver specific data, may need external protection. 63 * (e) locked by if_cloners_mtx 64 * (i) locked by ifc_mtx mtx 65 */ 66 struct if_clone { 67 char ifc_name[IFCLOSIZ]; /* (c) Name of device, e.g. `gif' */ 68 struct unrhdr *ifc_unrhdr; /* (c) alloc_unr(9) header */ 69 int ifc_maxunit; /* (c) maximum unit number */ 70 int ifc_flags; 71 long ifc_refcnt; /* (i) Reference count. */ 72 LIST_HEAD(, ifnet) ifc_iflist; /* (i) List of cloned interfaces */ 73 struct mtx ifc_mtx; /* Mutex to protect members. */ 74 75 enum { SIMPLE, ADVANCED } ifc_type; /* (c) */ 76 77 /* (c) Driver specific cloning functions. Called with no locks held. */ 78 union { 79 struct { /* advanced cloner */ 80 ifc_match_t *_ifc_match; 81 ifc_create_t *_ifc_create; 82 ifc_destroy_t *_ifc_destroy; 83 } A; 84 struct { /* simple cloner */ 85 ifcs_create_t *_ifcs_create; 86 ifcs_destroy_t *_ifcs_destroy; 87 int _ifcs_minifs; /* minimum ifs */ 88 89 } S; 90 } U; 91 #define ifc_match U.A._ifc_match 92 #define ifc_create U.A._ifc_create 93 #define ifc_destroy U.A._ifc_destroy 94 #define ifcs_create U.S._ifcs_create 95 #define ifcs_destroy U.S._ifcs_destroy 96 #define ifcs_minifs U.S._ifcs_minifs 97 98 LIST_ENTRY(if_clone) ifc_list; /* (e) On list of cloners */ 99 }; 100 101 static void if_clone_free(struct if_clone *ifc); 102 static int if_clone_createif(struct if_clone *ifc, char *name, size_t len, 103 caddr_t params); 104 105 static int ifc_simple_match(struct if_clone *, const char *); 106 static int ifc_simple_create(struct if_clone *, char *, size_t, caddr_t); 107 static int ifc_simple_destroy(struct if_clone *, struct ifnet *); 108 109 static struct mtx if_cloners_mtx; 110 MTX_SYSINIT(if_cloners_lock, &if_cloners_mtx, "if_cloners lock", MTX_DEF); 111 VNET_DEFINE_STATIC(int, if_cloners_count); 112 VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners); 113 114 #define V_if_cloners_count VNET(if_cloners_count) 115 #define V_if_cloners VNET(if_cloners) 116 117 #define IF_CLONERS_LOCK_ASSERT() mtx_assert(&if_cloners_mtx, MA_OWNED) 118 #define IF_CLONERS_LOCK() mtx_lock(&if_cloners_mtx) 119 #define IF_CLONERS_UNLOCK() mtx_unlock(&if_cloners_mtx) 120 121 #define IF_CLONE_LOCK_INIT(ifc) \ 122 mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF) 123 #define IF_CLONE_LOCK_DESTROY(ifc) mtx_destroy(&(ifc)->ifc_mtx) 124 #define IF_CLONE_LOCK_ASSERT(ifc) mtx_assert(&(ifc)->ifc_mtx, MA_OWNED) 125 #define IF_CLONE_LOCK(ifc) mtx_lock(&(ifc)->ifc_mtx) 126 #define IF_CLONE_UNLOCK(ifc) mtx_unlock(&(ifc)->ifc_mtx) 127 128 #define IF_CLONE_ADDREF(ifc) \ 129 do { \ 130 IF_CLONE_LOCK(ifc); \ 131 IF_CLONE_ADDREF_LOCKED(ifc); \ 132 IF_CLONE_UNLOCK(ifc); \ 133 } while (0) 134 #define IF_CLONE_ADDREF_LOCKED(ifc) \ 135 do { \ 136 IF_CLONE_LOCK_ASSERT(ifc); \ 137 KASSERT((ifc)->ifc_refcnt >= 0, \ 138 ("negative refcnt %ld", (ifc)->ifc_refcnt)); \ 139 (ifc)->ifc_refcnt++; \ 140 } while (0) 141 #define IF_CLONE_REMREF(ifc) \ 142 do { \ 143 IF_CLONE_LOCK(ifc); \ 144 IF_CLONE_REMREF_LOCKED(ifc); \ 145 } while (0) 146 #define IF_CLONE_REMREF_LOCKED(ifc) \ 147 do { \ 148 IF_CLONE_LOCK_ASSERT(ifc); \ 149 KASSERT((ifc)->ifc_refcnt > 0, \ 150 ("bogus refcnt %ld", (ifc)->ifc_refcnt)); \ 151 if (--(ifc)->ifc_refcnt == 0) { \ 152 IF_CLONE_UNLOCK(ifc); \ 153 if_clone_free(ifc); \ 154 } else { \ 155 /* silently free the lock */ \ 156 IF_CLONE_UNLOCK(ifc); \ 157 } \ 158 } while (0) 159 160 #define IFC_IFLIST_INSERT(_ifc, _ifp) \ 161 LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones) 162 #define IFC_IFLIST_REMOVE(_ifc, _ifp) \ 163 LIST_REMOVE(_ifp, if_clones) 164 165 static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework"); 166 167 void 168 vnet_if_clone_init(void) 169 { 170 171 LIST_INIT(&V_if_cloners); 172 } 173 174 /* 175 * Lookup and create a clone network interface. 176 */ 177 int 178 if_clone_create(char *name, size_t len, caddr_t params) 179 { 180 struct if_clone *ifc; 181 182 /* Try to find an applicable cloner for this request */ 183 IF_CLONERS_LOCK(); 184 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) 185 if (ifc->ifc_type == SIMPLE) { 186 if (ifc_simple_match(ifc, name)) 187 break; 188 } else { 189 if (ifc->ifc_match(ifc, name)) 190 break; 191 } 192 #ifdef VIMAGE 193 if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) { 194 CURVNET_SET_QUIET(vnet0); 195 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) 196 if (ifc->ifc_type == SIMPLE) { 197 if (ifc_simple_match(ifc, name)) 198 break; 199 } else { 200 if (ifc->ifc_match(ifc, name)) 201 break; 202 } 203 CURVNET_RESTORE(); 204 } 205 #endif 206 IF_CLONERS_UNLOCK(); 207 208 if (ifc == NULL) 209 return (EINVAL); 210 211 return (if_clone_createif(ifc, name, len, params)); 212 } 213 214 /* 215 * Create a clone network interface. 216 */ 217 static int 218 if_clone_createif(struct if_clone *ifc, char *name, size_t len, caddr_t params) 219 { 220 int err; 221 struct ifnet *ifp; 222 223 if (ifunit(name) != NULL) 224 return (EEXIST); 225 226 if (ifc->ifc_type == SIMPLE) 227 err = ifc_simple_create(ifc, name, len, params); 228 else 229 err = (*ifc->ifc_create)(ifc, name, len, params); 230 231 if (!err) { 232 ifp = ifunit(name); 233 if (ifp == NULL) 234 panic("%s: lookup failed for %s", __func__, name); 235 236 if ((ifc->ifc_flags & IFC_NOGROUP) == 0) 237 if_addgroup(ifp, ifc->ifc_name); 238 239 IF_CLONE_LOCK(ifc); 240 IFC_IFLIST_INSERT(ifc, ifp); 241 IF_CLONE_UNLOCK(ifc); 242 } 243 244 return (err); 245 } 246 247 /* 248 * Lookup and destroy a clone network interface. 249 */ 250 int 251 if_clone_destroy(const char *name) 252 { 253 int err; 254 struct if_clone *ifc; 255 struct ifnet *ifp; 256 257 ifp = ifunit_ref(name); 258 if (ifp == NULL) 259 return (ENXIO); 260 261 /* Find the cloner for this interface */ 262 IF_CLONERS_LOCK(); 263 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) { 264 if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) { 265 break; 266 } 267 } 268 #ifdef VIMAGE 269 if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) { 270 CURVNET_SET_QUIET(vnet0); 271 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) 272 if (ifc->ifc_type == SIMPLE) { 273 if (ifc_simple_match(ifc, name)) 274 break; 275 } else { 276 if (ifc->ifc_match(ifc, name)) 277 break; 278 } 279 CURVNET_RESTORE(); 280 } 281 #endif 282 IF_CLONERS_UNLOCK(); 283 if (ifc == NULL) { 284 if_rele(ifp); 285 return (EINVAL); 286 } 287 288 err = if_clone_destroyif(ifc, ifp); 289 if_rele(ifp); 290 return err; 291 } 292 293 /* 294 * Destroy a clone network interface. 295 */ 296 int 297 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp) 298 { 299 int err; 300 struct ifnet *ifcifp; 301 302 if (ifc->ifc_type == ADVANCED && ifc->ifc_destroy == NULL) 303 return(EOPNOTSUPP); 304 305 /* 306 * Given that the cloned ifnet might be attached to a different 307 * vnet from where its cloner was registered, we have to 308 * switch to the vnet context of the target vnet. 309 */ 310 CURVNET_SET_QUIET(ifp->if_vnet); 311 312 IF_CLONE_LOCK(ifc); 313 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) { 314 if (ifcifp == ifp) { 315 IFC_IFLIST_REMOVE(ifc, ifp); 316 break; 317 } 318 } 319 IF_CLONE_UNLOCK(ifc); 320 if (ifcifp == NULL) { 321 CURVNET_RESTORE(); 322 return (ENXIO); /* ifp is not on the list. */ 323 } 324 if ((ifc->ifc_flags & IFC_NOGROUP) == 0) 325 if_delgroup(ifp, ifc->ifc_name); 326 327 if (ifc->ifc_type == SIMPLE) 328 err = ifc_simple_destroy(ifc, ifp); 329 else 330 err = (*ifc->ifc_destroy)(ifc, ifp); 331 332 if (err != 0) { 333 if ((ifc->ifc_flags & IFC_NOGROUP) == 0) 334 if_addgroup(ifp, ifc->ifc_name); 335 336 IF_CLONE_LOCK(ifc); 337 IFC_IFLIST_INSERT(ifc, ifp); 338 IF_CLONE_UNLOCK(ifc); 339 } 340 CURVNET_RESTORE(); 341 return (err); 342 } 343 344 static struct if_clone * 345 if_clone_alloc(const char *name, int maxunit) 346 { 347 struct if_clone *ifc; 348 349 KASSERT(name != NULL, ("%s: no name\n", __func__)); 350 351 ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO); 352 strncpy(ifc->ifc_name, name, IFCLOSIZ-1); 353 IF_CLONE_LOCK_INIT(ifc); 354 IF_CLONE_ADDREF(ifc); 355 ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT; 356 ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx); 357 LIST_INIT(&ifc->ifc_iflist); 358 359 return (ifc); 360 } 361 362 static int 363 if_clone_attach(struct if_clone *ifc) 364 { 365 struct if_clone *ifc1; 366 367 IF_CLONERS_LOCK(); 368 LIST_FOREACH(ifc1, &V_if_cloners, ifc_list) 369 if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) { 370 IF_CLONERS_UNLOCK(); 371 IF_CLONE_REMREF(ifc); 372 return (EEXIST); 373 } 374 LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list); 375 V_if_cloners_count++; 376 IF_CLONERS_UNLOCK(); 377 378 return (0); 379 } 380 381 struct if_clone * 382 if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match, 383 ifc_create_t create, ifc_destroy_t destroy) 384 { 385 struct if_clone *ifc; 386 387 ifc = if_clone_alloc(name, maxunit); 388 ifc->ifc_type = ADVANCED; 389 ifc->ifc_match = match; 390 ifc->ifc_create = create; 391 ifc->ifc_destroy = destroy; 392 393 if (if_clone_attach(ifc) != 0) 394 return (NULL); 395 396 EVENTHANDLER_INVOKE(if_clone_event, ifc); 397 398 return (ifc); 399 } 400 401 struct if_clone * 402 if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy, 403 u_int minifs) 404 { 405 struct if_clone *ifc; 406 u_int unit; 407 408 ifc = if_clone_alloc(name, 0); 409 ifc->ifc_type = SIMPLE; 410 ifc->ifcs_create = create; 411 ifc->ifcs_destroy = destroy; 412 ifc->ifcs_minifs = minifs; 413 414 if (if_clone_attach(ifc) != 0) 415 return (NULL); 416 417 for (unit = 0; unit < minifs; unit++) { 418 char name[IFNAMSIZ]; 419 int error __unused; 420 421 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit); 422 error = if_clone_createif(ifc, name, IFNAMSIZ, NULL); 423 KASSERT(error == 0, 424 ("%s: failed to create required interface %s", 425 __func__, name)); 426 } 427 428 EVENTHANDLER_INVOKE(if_clone_event, ifc); 429 430 return (ifc); 431 } 432 433 /* 434 * Unregister a network interface cloner. 435 */ 436 void 437 if_clone_detach(struct if_clone *ifc) 438 { 439 440 IF_CLONERS_LOCK(); 441 LIST_REMOVE(ifc, ifc_list); 442 V_if_cloners_count--; 443 IF_CLONERS_UNLOCK(); 444 445 /* Allow all simples to be destroyed */ 446 if (ifc->ifc_type == SIMPLE) 447 ifc->ifcs_minifs = 0; 448 449 /* destroy all interfaces for this cloner */ 450 while (!LIST_EMPTY(&ifc->ifc_iflist)) 451 if_clone_destroyif(ifc, LIST_FIRST(&ifc->ifc_iflist)); 452 453 IF_CLONE_REMREF(ifc); 454 } 455 456 static void 457 if_clone_free(struct if_clone *ifc) 458 { 459 460 KASSERT(LIST_EMPTY(&ifc->ifc_iflist), 461 ("%s: ifc_iflist not empty", __func__)); 462 463 IF_CLONE_LOCK_DESTROY(ifc); 464 delete_unrhdr(ifc->ifc_unrhdr); 465 free(ifc, M_CLONE); 466 } 467 468 /* 469 * Provide list of interface cloners to userspace. 470 */ 471 int 472 if_clone_list(struct if_clonereq *ifcr) 473 { 474 char *buf, *dst, *outbuf = NULL; 475 struct if_clone *ifc; 476 int buf_count, count, err = 0; 477 478 if (ifcr->ifcr_count < 0) 479 return (EINVAL); 480 481 IF_CLONERS_LOCK(); 482 /* 483 * Set our internal output buffer size. We could end up not 484 * reporting a cloner that is added between the unlock and lock 485 * below, but that's not a major problem. Not caping our 486 * allocation to the number of cloners actually in the system 487 * could be because that would let arbitrary users cause us to 488 * allocate arbitrary amounts of kernel memory. 489 */ 490 buf_count = (V_if_cloners_count < ifcr->ifcr_count) ? 491 V_if_cloners_count : ifcr->ifcr_count; 492 IF_CLONERS_UNLOCK(); 493 494 outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO); 495 496 IF_CLONERS_LOCK(); 497 498 ifcr->ifcr_total = V_if_cloners_count; 499 if ((dst = ifcr->ifcr_buffer) == NULL) { 500 /* Just asking how many there are. */ 501 goto done; 502 } 503 count = (V_if_cloners_count < buf_count) ? 504 V_if_cloners_count : buf_count; 505 506 for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf; 507 ifc != NULL && count != 0; 508 ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) { 509 strlcpy(buf, ifc->ifc_name, IFNAMSIZ); 510 } 511 512 done: 513 IF_CLONERS_UNLOCK(); 514 if (err == 0 && dst != NULL) 515 err = copyout(outbuf, dst, buf_count*IFNAMSIZ); 516 if (outbuf != NULL) 517 free(outbuf, M_CLONE); 518 return (err); 519 } 520 521 /* 522 * if_clone_findifc() looks up ifnet from the current 523 * cloner list, and returns ifc if found. Note that ifc_refcnt 524 * is incremented. 525 */ 526 struct if_clone * 527 if_clone_findifc(struct ifnet *ifp) 528 { 529 struct if_clone *ifc, *ifc0; 530 struct ifnet *ifcifp; 531 532 ifc0 = NULL; 533 IF_CLONERS_LOCK(); 534 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) { 535 IF_CLONE_LOCK(ifc); 536 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) { 537 if (ifp == ifcifp) { 538 ifc0 = ifc; 539 IF_CLONE_ADDREF_LOCKED(ifc); 540 break; 541 } 542 } 543 IF_CLONE_UNLOCK(ifc); 544 if (ifc0 != NULL) 545 break; 546 } 547 IF_CLONERS_UNLOCK(); 548 549 return (ifc0); 550 } 551 552 /* 553 * if_clone_addgroup() decrements ifc_refcnt because it is called after 554 * if_clone_findifc(). 555 */ 556 void 557 if_clone_addgroup(struct ifnet *ifp, struct if_clone *ifc) 558 { 559 if ((ifc->ifc_flags & IFC_NOGROUP) == 0) { 560 if_addgroup(ifp, ifc->ifc_name); 561 IF_CLONE_REMREF(ifc); 562 } 563 } 564 565 /* 566 * A utility function to extract unit numbers from interface names of 567 * the form name###. 568 * 569 * Returns 0 on success and an error on failure. 570 */ 571 int 572 ifc_name2unit(const char *name, int *unit) 573 { 574 const char *cp; 575 int cutoff = INT_MAX / 10; 576 int cutlim = INT_MAX % 10; 577 578 for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++); 579 if (*cp == '\0') { 580 *unit = -1; 581 } else if (cp[0] == '0' && cp[1] != '\0') { 582 /* Disallow leading zeroes. */ 583 return (EINVAL); 584 } else { 585 for (*unit = 0; *cp != '\0'; cp++) { 586 if (*cp < '0' || *cp > '9') { 587 /* Bogus unit number. */ 588 return (EINVAL); 589 } 590 if (*unit > cutoff || 591 (*unit == cutoff && *cp - '0' > cutlim)) 592 return (EINVAL); 593 *unit = (*unit * 10) + (*cp - '0'); 594 } 595 } 596 597 return (0); 598 } 599 600 static int 601 ifc_alloc_unit_specific(struct if_clone *ifc, int *unit) 602 { 603 char name[IFNAMSIZ]; 604 605 if (*unit > ifc->ifc_maxunit) 606 return (ENOSPC); 607 608 if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1) 609 return (EEXIST); 610 611 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit); 612 if (ifunit(name) != NULL) { 613 free_unr(ifc->ifc_unrhdr, *unit); 614 return (EEXIST); 615 } 616 617 IF_CLONE_ADDREF(ifc); 618 619 return (0); 620 } 621 622 static int 623 ifc_alloc_unit_next(struct if_clone *ifc, int *unit) 624 { 625 int error; 626 627 *unit = alloc_unr(ifc->ifc_unrhdr); 628 if (*unit == -1) 629 return (ENOSPC); 630 631 free_unr(ifc->ifc_unrhdr, *unit); 632 for (;;) { 633 error = ifc_alloc_unit_specific(ifc, unit); 634 if (error != EEXIST) 635 break; 636 637 (*unit)++; 638 } 639 640 return (error); 641 } 642 643 int 644 ifc_alloc_unit(struct if_clone *ifc, int *unit) 645 { 646 if (*unit < 0) 647 return (ifc_alloc_unit_next(ifc, unit)); 648 else 649 return (ifc_alloc_unit_specific(ifc, unit)); 650 } 651 652 void 653 ifc_free_unit(struct if_clone *ifc, int unit) 654 { 655 656 free_unr(ifc->ifc_unrhdr, unit); 657 IF_CLONE_REMREF(ifc); 658 } 659 660 static int 661 ifc_simple_match(struct if_clone *ifc, const char *name) 662 { 663 const char *cp; 664 int i; 665 666 /* Match the name */ 667 for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) { 668 if (ifc->ifc_name[i] != *cp) 669 return (0); 670 } 671 672 /* Make sure there's a unit number or nothing after the name */ 673 for (; *cp != '\0'; cp++) { 674 if (*cp < '0' || *cp > '9') 675 return (0); 676 } 677 678 return (1); 679 } 680 681 static int 682 ifc_simple_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 683 { 684 char *dp; 685 int wildcard; 686 int unit; 687 int err; 688 689 err = ifc_name2unit(name, &unit); 690 if (err != 0) 691 return (err); 692 693 wildcard = (unit < 0); 694 695 err = ifc_alloc_unit(ifc, &unit); 696 if (err != 0) 697 return (err); 698 699 err = ifc->ifcs_create(ifc, unit, params); 700 if (err != 0) { 701 ifc_free_unit(ifc, unit); 702 return (err); 703 } 704 705 /* In the wildcard case, we need to update the name. */ 706 if (wildcard) { 707 for (dp = name; *dp != '\0'; dp++); 708 if (snprintf(dp, len - (dp-name), "%d", unit) > 709 len - (dp-name) - 1) { 710 /* 711 * This can only be a programmer error and 712 * there's no straightforward way to recover if 713 * it happens. 714 */ 715 panic("if_clone_create(): interface name too long"); 716 } 717 718 } 719 720 return (0); 721 } 722 723 static int 724 ifc_simple_destroy(struct if_clone *ifc, struct ifnet *ifp) 725 { 726 int unit; 727 728 unit = ifp->if_dunit; 729 730 if (unit < ifc->ifcs_minifs) 731 return (EINVAL); 732 733 ifc->ifcs_destroy(ifp); 734 735 ifc_free_unit(ifc, unit); 736 737 return (0); 738 } 739 740 const char * 741 ifc_name(struct if_clone *ifc) 742 { 743 return (ifc->ifc_name); 744 } 745 746 void 747 ifc_flags_set(struct if_clone *ifc, int flags) 748 { 749 ifc->ifc_flags = flags; 750 } 751 752 int 753 ifc_flags_get(struct if_clone *ifc) 754 { 755 return (ifc->ifc_flags); 756 } 757