2 * SPDX-License-Identifier: BSD-3-Clause
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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * @(#)if.c 8.5 (Berkeley) 1/9/95
36 #include <sys/param.h>
37 #include <sys/eventhandler.h>
38 #include <sys/malloc.h>
39 #include <sys/limits.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>
48 #include <net/if_var.h>
49 #include <net/if_clone.h>
50 #include <net/radix.h>
51 #include <net/route.h>
54 /* Current IF_MAXUNIT expands maximum to 5 characters. */
55 #define IFCLOSIZ (IFNAMSIZ - 5)
58 * Structure describing a `cloning' interface.
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
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 */
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. */
75 ifc_match_f *ifc_match; /* (c) Matcher function */
76 ifc_create_f *ifc_create; /* (c) Creates new interface */
77 ifc_destroy_f *ifc_destroy; /* (c) Destroys cloned interface */
79 #ifdef CLONE_COMPAT_13
80 /* (c) Driver specific cloning functions. Called with no locks held. */
82 struct { /* advanced cloner */
83 ifc_create_t *_ifc_create;
84 ifc_destroy_t *_ifc_destroy;
86 struct { /* simple cloner */
87 ifcs_create_t *_ifcs_create;
88 ifcs_destroy_t *_ifcs_destroy;
89 int _ifcs_minifs; /* minimum ifs */
93 #define ifca_create U.A._ifc_create
94 #define ifca_destroy U.A._ifc_destroy
95 #define ifcs_create U.S._ifcs_create
96 #define ifcs_destroy U.S._ifcs_destroy
97 #define ifcs_minifs U.S._ifcs_minifs
100 LIST_ENTRY(if_clone) ifc_list; /* (e) On list of cloners */
105 static void if_clone_free(struct if_clone *ifc);
106 static int if_clone_createif(struct if_clone *ifc, char *name, size_t len,
107 struct ifc_data *ifd, struct ifnet **ifpp);
109 static int ifc_simple_match(struct if_clone *ifc, const char *name);
110 static int ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit);
112 #ifdef CLONE_COMPAT_13
113 static int ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
114 struct ifc_data *ifc_data, struct ifnet **ifpp);
115 static int ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
116 struct ifc_data *ifc_data, struct ifnet **ifpp);
119 static struct mtx if_cloners_mtx;
120 MTX_SYSINIT(if_cloners_lock, &if_cloners_mtx, "if_cloners lock", MTX_DEF);
121 VNET_DEFINE_STATIC(int, if_cloners_count);
122 VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners);
124 #define V_if_cloners_count VNET(if_cloners_count)
125 #define V_if_cloners VNET(if_cloners)
127 #define IF_CLONERS_LOCK_ASSERT() mtx_assert(&if_cloners_mtx, MA_OWNED)
128 #define IF_CLONERS_LOCK() mtx_lock(&if_cloners_mtx)
129 #define IF_CLONERS_UNLOCK() mtx_unlock(&if_cloners_mtx)
131 #define IF_CLONE_LOCK_INIT(ifc) \
132 mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
133 #define IF_CLONE_LOCK_DESTROY(ifc) mtx_destroy(&(ifc)->ifc_mtx)
134 #define IF_CLONE_LOCK_ASSERT(ifc) mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
135 #define IF_CLONE_LOCK(ifc) mtx_lock(&(ifc)->ifc_mtx)
136 #define IF_CLONE_UNLOCK(ifc) mtx_unlock(&(ifc)->ifc_mtx)
138 #define IF_CLONE_ADDREF(ifc) \
140 IF_CLONE_LOCK(ifc); \
141 IF_CLONE_ADDREF_LOCKED(ifc); \
142 IF_CLONE_UNLOCK(ifc); \
144 #define IF_CLONE_ADDREF_LOCKED(ifc) \
146 IF_CLONE_LOCK_ASSERT(ifc); \
147 KASSERT((ifc)->ifc_refcnt >= 0, \
148 ("negative refcnt %ld", (ifc)->ifc_refcnt)); \
149 (ifc)->ifc_refcnt++; \
151 #define IF_CLONE_REMREF(ifc) \
153 IF_CLONE_LOCK(ifc); \
154 IF_CLONE_REMREF_LOCKED(ifc); \
156 #define IF_CLONE_REMREF_LOCKED(ifc) \
158 IF_CLONE_LOCK_ASSERT(ifc); \
159 KASSERT((ifc)->ifc_refcnt > 0, \
160 ("bogus refcnt %ld", (ifc)->ifc_refcnt)); \
161 if (--(ifc)->ifc_refcnt == 0) { \
162 IF_CLONE_UNLOCK(ifc); \
163 if_clone_free(ifc); \
165 /* silently free the lock */ \
166 IF_CLONE_UNLOCK(ifc); \
170 #define IFC_IFLIST_INSERT(_ifc, _ifp) \
171 LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones)
172 #define IFC_IFLIST_REMOVE(_ifc, _ifp) \
173 LIST_REMOVE(_ifp, if_clones)
175 static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
178 vnet_if_clone_init(void)
181 LIST_INIT(&V_if_cloners);
185 * Lookup and create a clone network interface.
188 ifc_create_ifp(const char *name, struct ifc_data *ifd,
191 struct if_clone *ifc;
192 char ifname[IFNAMSIZ];
193 struct ifnet *ifp = NULL;
196 /* Try to find an applicable cloner for this request */
198 LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
199 if (ifc->ifc_match(ifc, name))
202 if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
203 CURVNET_SET_QUIET(vnet0);
204 LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
205 if (ifc->ifc_match(ifc, name))
215 strlcpy(ifname, name, IFNAMSIZ);
216 error = if_clone_createif(ifc, ifname, IFNAMSIZ, ifd, &ifp);
224 if_clone_create(char *name, size_t len, caddr_t params)
226 struct ifc_data ifd = { .params = params };
229 int error = ifc_create_ifp(name, &ifd, &ifp);
232 strlcpy(name, if_name(ifp), len);
238 if_clone_addif(struct if_clone *ifc, struct ifnet *ifp)
241 if ((ifc->ifc_flags & IFC_NOGROUP) == 0)
242 if_addgroup(ifp, ifc->ifc_name);
245 IFC_IFLIST_INSERT(ifc, ifp);
246 IF_CLONE_UNLOCK(ifc);
250 * Create a clone network interface.
253 if_clone_createif(struct if_clone *ifc, char *name, size_t len,
254 struct ifc_data *ifd, struct ifnet **ifpp)
258 if (ifunit(name) != NULL)
261 if (ifc->ifc_flags & IFC_F_AUTOUNIT) {
262 if ((err = ifc_handle_unit(ifc, name, len, &unit)) != 0)
267 err = (*ifc->ifc_create)(ifc, name, len, ifd, ifpp);
270 MPASS(*ifpp != NULL);
271 if_clone_addif(ifc, *ifpp);
272 } else if (ifc->ifc_flags & IFC_F_AUTOUNIT)
273 ifc_free_unit(ifc, unit);
279 * Lookup and destroy a clone network interface.
282 if_clone_destroy(const char *name)
285 struct if_clone *ifc;
288 ifp = ifunit_ref(name);
292 /* Find the cloner for this interface */
294 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
295 if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) {
300 if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
301 CURVNET_SET_QUIET(vnet0);
302 LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
303 if (ifc->ifc_match(ifc, name))
314 err = if_clone_destroyif(ifc, ifp);
320 * Destroy a clone network interface.
323 if_clone_destroyif_flags(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
326 struct ifnet *ifcifp;
329 * Given that the cloned ifnet might be attached to a different
330 * vnet from where its cloner was registered, we have to
331 * switch to the vnet context of the target vnet.
333 CURVNET_SET_QUIET(ifp->if_vnet);
336 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
338 IFC_IFLIST_REMOVE(ifc, ifp);
342 IF_CLONE_UNLOCK(ifc);
343 if (ifcifp == NULL) {
345 return (ENXIO); /* ifp is not on the list. */
347 if ((ifc->ifc_flags & IFC_F_NOGROUP) == 0)
348 if_delgroup(ifp, ifc->ifc_name);
350 int unit = ifp->if_dunit;
351 err = (*ifc->ifc_destroy)(ifc, ifp, flags);
354 if ((ifc->ifc_flags & IFC_F_NOGROUP) == 0)
355 if_addgroup(ifp, ifc->ifc_name);
358 IFC_IFLIST_INSERT(ifc, ifp);
359 IF_CLONE_UNLOCK(ifc);
360 } else if (ifc->ifc_flags & IFC_F_AUTOUNIT)
361 ifc_free_unit(ifc, unit);
367 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
369 return (if_clone_destroyif_flags(ifc, ifp, 0));
372 static struct if_clone *
373 if_clone_alloc(const char *name, int maxunit)
375 struct if_clone *ifc;
377 KASSERT(name != NULL, ("%s: no name\n", __func__));
379 ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO);
380 strncpy(ifc->ifc_name, name, IFCLOSIZ-1);
381 IF_CLONE_LOCK_INIT(ifc);
382 IF_CLONE_ADDREF(ifc);
383 ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT;
384 ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx);
385 LIST_INIT(&ifc->ifc_iflist);
391 if_clone_attach(struct if_clone *ifc)
393 struct if_clone *ifc1;
396 LIST_FOREACH(ifc1, &V_if_cloners, ifc_list)
397 if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) {
399 IF_CLONE_REMREF(ifc);
402 LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
403 V_if_cloners_count++;
410 ifc_attach_cloner(const char *name, struct if_clone_addreq *req)
412 if (req->create_f == NULL || req->destroy_f == NULL)
414 if (strnlen(name, IFCLOSIZ) >= (IFCLOSIZ - 1))
417 struct if_clone *ifc = if_clone_alloc(name, req->maxunit);
418 ifc->ifc_match = req->match_f != NULL ? req->match_f : ifc_simple_match;
419 ifc->ifc_create = req->create_f;
420 ifc->ifc_destroy = req->destroy_f;
421 ifc->ifc_flags = (req->flags & (IFC_F_AUTOUNIT | IFC_F_NOGROUP));
423 if (if_clone_attach(ifc) != 0)
426 EVENTHANDLER_INVOKE(if_clone_event, ifc);
432 ifc_detach_cloner(struct if_clone *ifc)
434 if_clone_detach(ifc);
438 #ifdef CLONE_COMPAT_13
441 ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
442 struct ifc_data *ifc_data, struct ifnet **ifpp)
444 int error = ifc->ifca_create(ifc, name, maxlen, ifc_data->params);
447 *ifpp = ifunit(name);
452 ifc_advanced_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
454 if (ifc->ifca_destroy == NULL)
456 return (ifc->ifca_destroy(ifc, ifp));
460 if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
461 ifc_create_t create, ifc_destroy_t destroy)
463 struct if_clone *ifc;
465 ifc = if_clone_alloc(name, maxunit);
466 ifc->ifc_match = match;
467 ifc->ifc_create = ifc_advanced_create_wrapper;
468 ifc->ifc_destroy = ifc_advanced_destroy_wrapper;
469 ifc->ifca_destroy = destroy;
470 ifc->ifca_create = create;
472 if (if_clone_attach(ifc) != 0)
475 EVENTHANDLER_INVOKE(if_clone_event, ifc);
481 ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
482 struct ifc_data *ifc_data, struct ifnet **ifpp)
486 ifc_name2unit(name, &unit);
487 int error = ifc->ifcs_create(ifc, unit, ifc_data->params);
489 *ifpp = ifunit(name);
494 ifc_simple_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
496 if (ifp->if_dunit < ifc->ifcs_minifs && (flags & IFC_F_FORCE) == 0)
499 ifc->ifcs_destroy(ifp);
504 if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
507 struct if_clone *ifc;
510 ifc = if_clone_alloc(name, 0);
511 ifc->ifc_match = ifc_simple_match;
512 ifc->ifc_create = ifc_simple_create_wrapper;
513 ifc->ifc_destroy = ifc_simple_destroy_wrapper;
514 ifc->ifcs_create = create;
515 ifc->ifcs_destroy = destroy;
516 ifc->ifcs_minifs = minifs;
517 ifc->ifc_flags = IFC_F_AUTOUNIT;
519 if (if_clone_attach(ifc) != 0)
522 for (unit = 0; unit < minifs; unit++) {
525 struct ifc_data ifd = {};
528 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
529 error = if_clone_createif(ifc, name, IFNAMSIZ, &ifd, &ifp);
531 ("%s: failed to create required interface %s",
535 EVENTHANDLER_INVOKE(if_clone_event, ifc);
542 * Unregister a network interface cloner.
545 if_clone_detach(struct if_clone *ifc)
549 LIST_REMOVE(ifc, ifc_list);
550 V_if_cloners_count--;
553 /* destroy all interfaces for this cloner */
554 while (!LIST_EMPTY(&ifc->ifc_iflist))
555 if_clone_destroyif_flags(ifc, LIST_FIRST(&ifc->ifc_iflist), IFC_F_FORCE);
557 IF_CLONE_REMREF(ifc);
561 if_clone_free(struct if_clone *ifc)
564 KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
565 ("%s: ifc_iflist not empty", __func__));
567 IF_CLONE_LOCK_DESTROY(ifc);
568 delete_unrhdr(ifc->ifc_unrhdr);
573 * Provide list of interface cloners to userspace.
576 if_clone_list(struct if_clonereq *ifcr)
578 char *buf, *dst, *outbuf = NULL;
579 struct if_clone *ifc;
580 int buf_count, count, err = 0;
582 if (ifcr->ifcr_count < 0)
587 * Set our internal output buffer size. We could end up not
588 * reporting a cloner that is added between the unlock and lock
589 * below, but that's not a major problem. Not caping our
590 * allocation to the number of cloners actually in the system
591 * could be because that would let arbitrary users cause us to
592 * allocate arbitrary amounts of kernel memory.
594 buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
595 V_if_cloners_count : ifcr->ifcr_count;
598 outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
602 ifcr->ifcr_total = V_if_cloners_count;
603 if ((dst = ifcr->ifcr_buffer) == NULL) {
604 /* Just asking how many there are. */
607 count = (V_if_cloners_count < buf_count) ?
608 V_if_cloners_count : buf_count;
610 for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
611 ifc != NULL && count != 0;
612 ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
613 strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
618 if (err == 0 && dst != NULL)
619 err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
621 free(outbuf, M_CLONE);
626 * if_clone_findifc() looks up ifnet from the current
627 * cloner list, and returns ifc if found. Note that ifc_refcnt
631 if_clone_findifc(struct ifnet *ifp)
633 struct if_clone *ifc, *ifc0;
634 struct ifnet *ifcifp;
638 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
640 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
643 IF_CLONE_ADDREF_LOCKED(ifc);
647 IF_CLONE_UNLOCK(ifc);
657 * if_clone_addgroup() decrements ifc_refcnt because it is called after
658 * if_clone_findifc().
661 if_clone_addgroup(struct ifnet *ifp, struct if_clone *ifc)
663 if ((ifc->ifc_flags & IFC_NOGROUP) == 0) {
664 if_addgroup(ifp, ifc->ifc_name);
665 IF_CLONE_REMREF(ifc);
670 * A utility function to extract unit numbers from interface names of
673 * Returns 0 on success and an error on failure.
676 ifc_name2unit(const char *name, int *unit)
679 int cutoff = INT_MAX / 10;
680 int cutlim = INT_MAX % 10;
682 for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++)
686 } else if (cp[0] == '0' && cp[1] != '\0') {
687 /* Disallow leading zeroes. */
690 for (*unit = 0; *cp != '\0'; cp++) {
691 if (*cp < '0' || *cp > '9') {
692 /* Bogus unit number. */
695 if (*unit > cutoff ||
696 (*unit == cutoff && *cp - '0' > cutlim))
698 *unit = (*unit * 10) + (*cp - '0');
706 ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
710 if (*unit > ifc->ifc_maxunit)
713 if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
716 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
717 if (ifunit(name) != NULL) {
718 free_unr(ifc->ifc_unrhdr, *unit);
722 IF_CLONE_ADDREF(ifc);
728 ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
732 *unit = alloc_unr(ifc->ifc_unrhdr);
736 free_unr(ifc->ifc_unrhdr, *unit);
738 error = ifc_alloc_unit_specific(ifc, unit);
749 ifc_alloc_unit(struct if_clone *ifc, int *unit)
752 return (ifc_alloc_unit_next(ifc, unit));
754 return (ifc_alloc_unit_specific(ifc, unit));
758 ifc_free_unit(struct if_clone *ifc, int unit)
761 free_unr(ifc->ifc_unrhdr, unit);
762 IF_CLONE_REMREF(ifc);
766 ifc_simple_match(struct if_clone *ifc, const char *name)
772 for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
773 if (ifc->ifc_name[i] != *cp)
777 /* Make sure there's a unit number or nothing after the name */
778 for (; *cp != '\0'; cp++) {
779 if (*cp < '0' || *cp > '9')
787 ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
794 err = ifc_name2unit(name, &unit);
798 wildcard = (unit < 0);
800 err = ifc_alloc_unit(ifc, &unit);
804 /* In the wildcard case, we need to update the name. */
806 for (dp = name; *dp != '\0'; dp++);
807 if (snprintf(dp, len - (dp-name), "%d", unit) >
808 len - (dp-name) - 1) {
810 * This can only be a programmer error and
811 * there's no straightforward way to recover if
814 panic("if_clone_create(): interface name too long");
823 ifc_copyin(const struct ifc_data *ifd, void *target, size_t len)
825 if (ifd->params == NULL)
828 if (ifd->flags & IFC_F_SYSSPACE) {
829 memcpy(target, ifd->params, len);
832 return (copyin(ifd->params, target, len));
836 ifc_name(struct if_clone *ifc)
838 return (ifc->ifc_name);
842 ifc_flags_set(struct if_clone *ifc, int flags)
844 ifc->ifc_flags = flags;
848 ifc_flags_get(struct if_clone *ifc)
850 return (ifc->ifc_flags);