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
35 #include <sys/param.h>
36 #include <sys/eventhandler.h>
37 #include <sys/malloc.h>
38 #include <sys/limits.h>
40 #include <sys/mutex.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
47 #include <net/if_var.h>
48 #include <net/if_clone.h>
49 #include <net/radix.h>
50 #include <net/route.h>
53 /* Current IF_MAXUNIT expands maximum to 5 characters. */
54 #define IFCLOSIZ (IFNAMSIZ - 5)
57 * Structure describing a `cloning' interface.
60 * (c) const until freeing
61 * (d) driver specific data, may need external protection.
62 * (e) locked by if_cloners_mtx
63 * (i) locked by ifc_mtx mtx
66 char ifc_name[IFCLOSIZ]; /* (c) Name of device, e.g. `gif' */
67 struct unrhdr *ifc_unrhdr; /* (c) alloc_unr(9) header */
68 int ifc_maxunit; /* (c) maximum unit number */
70 long ifc_refcnt; /* (i) Reference count. */
71 LIST_HEAD(, ifnet) ifc_iflist; /* (i) List of cloned interfaces */
72 struct mtx ifc_mtx; /* Mutex to protect members. */
74 ifc_match_f *ifc_match; /* (c) Matcher function */
75 ifc_create_f *ifc_create; /* (c) Creates new interface */
76 ifc_destroy_f *ifc_destroy; /* (c) Destroys cloned interface */
78 #ifdef CLONE_COMPAT_13
79 /* (c) Driver specific cloning functions. Called with no locks held. */
81 struct { /* advanced cloner */
82 ifc_create_t *_ifc_create;
83 ifc_destroy_t *_ifc_destroy;
85 struct { /* simple cloner */
86 ifcs_create_t *_ifcs_create;
87 ifcs_destroy_t *_ifcs_destroy;
88 int _ifcs_minifs; /* minimum ifs */
92 #define ifca_create U.A._ifc_create
93 #define ifca_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
99 LIST_ENTRY(if_clone) ifc_list; /* (e) On list of cloners */
104 static void if_clone_free(struct if_clone *ifc);
105 static int if_clone_createif(struct if_clone *ifc, char *name, size_t len,
106 struct ifc_data *ifd, struct ifnet **ifpp);
108 static int ifc_simple_match(struct if_clone *ifc, const char *name);
109 static int ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit);
111 #ifdef CLONE_COMPAT_13
112 static int ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
113 struct ifc_data *ifc_data, struct ifnet **ifpp);
114 static int ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
115 struct ifc_data *ifc_data, struct ifnet **ifpp);
118 static struct mtx if_cloners_mtx;
119 MTX_SYSINIT(if_cloners_lock, &if_cloners_mtx, "if_cloners lock", MTX_DEF);
120 VNET_DEFINE_STATIC(int, if_cloners_count);
121 VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners);
123 #define V_if_cloners_count VNET(if_cloners_count)
124 #define V_if_cloners VNET(if_cloners)
126 #define IF_CLONERS_LOCK_ASSERT() mtx_assert(&if_cloners_mtx, MA_OWNED)
127 #define IF_CLONERS_LOCK() mtx_lock(&if_cloners_mtx)
128 #define IF_CLONERS_UNLOCK() mtx_unlock(&if_cloners_mtx)
130 #define IF_CLONE_LOCK_INIT(ifc) \
131 mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
132 #define IF_CLONE_LOCK_DESTROY(ifc) mtx_destroy(&(ifc)->ifc_mtx)
133 #define IF_CLONE_LOCK_ASSERT(ifc) mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
134 #define IF_CLONE_LOCK(ifc) mtx_lock(&(ifc)->ifc_mtx)
135 #define IF_CLONE_UNLOCK(ifc) mtx_unlock(&(ifc)->ifc_mtx)
137 #define IF_CLONE_ADDREF(ifc) \
139 IF_CLONE_LOCK(ifc); \
140 IF_CLONE_ADDREF_LOCKED(ifc); \
141 IF_CLONE_UNLOCK(ifc); \
143 #define IF_CLONE_ADDREF_LOCKED(ifc) \
145 IF_CLONE_LOCK_ASSERT(ifc); \
146 KASSERT((ifc)->ifc_refcnt >= 0, \
147 ("negative refcnt %ld", (ifc)->ifc_refcnt)); \
148 (ifc)->ifc_refcnt++; \
150 #define IF_CLONE_REMREF(ifc) \
152 IF_CLONE_LOCK(ifc); \
153 IF_CLONE_REMREF_LOCKED(ifc); \
155 #define IF_CLONE_REMREF_LOCKED(ifc) \
157 IF_CLONE_LOCK_ASSERT(ifc); \
158 KASSERT((ifc)->ifc_refcnt > 0, \
159 ("bogus refcnt %ld", (ifc)->ifc_refcnt)); \
160 if (--(ifc)->ifc_refcnt == 0) { \
161 IF_CLONE_UNLOCK(ifc); \
162 if_clone_free(ifc); \
164 /* silently free the lock */ \
165 IF_CLONE_UNLOCK(ifc); \
169 #define IFC_IFLIST_INSERT(_ifc, _ifp) \
170 LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones)
171 #define IFC_IFLIST_REMOVE(_ifc, _ifp) \
172 LIST_REMOVE(_ifp, if_clones)
174 static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
177 vnet_if_clone_init(void)
180 LIST_INIT(&V_if_cloners);
184 * Lookup and create a clone network interface.
187 ifc_create_ifp(const char *name, struct ifc_data *ifd,
190 struct if_clone *ifc;
191 char ifname[IFNAMSIZ];
192 struct ifnet *ifp = NULL;
195 /* Try to find an applicable cloner for this request */
197 LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
198 if (ifc->ifc_match(ifc, name))
201 if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
202 CURVNET_SET_QUIET(vnet0);
203 LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
204 if (ifc->ifc_match(ifc, name))
214 strlcpy(ifname, name, IFNAMSIZ);
215 error = if_clone_createif(ifc, ifname, IFNAMSIZ, ifd, &ifp);
223 if_clone_create(char *name, size_t len, caddr_t params)
225 struct ifc_data ifd = { .params = params };
228 int error = ifc_create_ifp(name, &ifd, &ifp);
231 strlcpy(name, if_name(ifp), len);
237 if_clone_addif(struct if_clone *ifc, struct ifnet *ifp)
240 if ((ifc->ifc_flags & IFC_NOGROUP) == 0)
241 if_addgroup(ifp, ifc->ifc_name);
244 IFC_IFLIST_INSERT(ifc, ifp);
245 IF_CLONE_UNLOCK(ifc);
249 * Create a clone network interface.
252 if_clone_createif(struct if_clone *ifc, char *name, size_t len,
253 struct ifc_data *ifd, struct ifnet **ifpp)
257 if (ifunit(name) != NULL)
260 if (ifc->ifc_flags & IFC_F_AUTOUNIT) {
261 if ((err = ifc_handle_unit(ifc, name, len, &unit)) != 0)
266 err = (*ifc->ifc_create)(ifc, name, len, ifd, ifpp);
269 MPASS(*ifpp != NULL);
270 if_clone_addif(ifc, *ifpp);
271 } else if (ifc->ifc_flags & IFC_F_AUTOUNIT)
272 ifc_free_unit(ifc, unit);
278 * Lookup and destroy a clone network interface.
281 if_clone_destroy(const char *name)
284 struct if_clone *ifc;
287 ifp = ifunit_ref(name);
291 /* Find the cloner for this interface */
293 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
294 if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) {
299 if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
300 CURVNET_SET_QUIET(vnet0);
301 LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
302 if (ifc->ifc_match(ifc, name))
313 err = if_clone_destroyif(ifc, ifp);
319 * Destroy a clone network interface.
322 if_clone_destroyif_flags(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
325 struct ifnet *ifcifp;
328 * Given that the cloned ifnet might be attached to a different
329 * vnet from where its cloner was registered, we have to
330 * switch to the vnet context of the target vnet.
332 CURVNET_SET_QUIET(ifp->if_vnet);
335 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
337 IFC_IFLIST_REMOVE(ifc, ifp);
341 IF_CLONE_UNLOCK(ifc);
342 if (ifcifp == NULL) {
344 return (ENXIO); /* ifp is not on the list. */
346 if ((ifc->ifc_flags & IFC_F_NOGROUP) == 0)
347 if_delgroup(ifp, ifc->ifc_name);
349 int unit = ifp->if_dunit;
350 err = (*ifc->ifc_destroy)(ifc, ifp, flags);
353 if ((ifc->ifc_flags & IFC_F_NOGROUP) == 0)
354 if_addgroup(ifp, ifc->ifc_name);
357 IFC_IFLIST_INSERT(ifc, ifp);
358 IF_CLONE_UNLOCK(ifc);
359 } else if (ifc->ifc_flags & IFC_F_AUTOUNIT)
360 ifc_free_unit(ifc, unit);
366 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
368 return (if_clone_destroyif_flags(ifc, ifp, 0));
371 static struct if_clone *
372 if_clone_alloc(const char *name, int maxunit)
374 struct if_clone *ifc;
376 KASSERT(name != NULL, ("%s: no name\n", __func__));
378 ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO);
379 strncpy(ifc->ifc_name, name, IFCLOSIZ-1);
380 IF_CLONE_LOCK_INIT(ifc);
381 IF_CLONE_ADDREF(ifc);
382 ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT;
383 ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx);
384 LIST_INIT(&ifc->ifc_iflist);
390 if_clone_attach(struct if_clone *ifc)
392 struct if_clone *ifc1;
395 LIST_FOREACH(ifc1, &V_if_cloners, ifc_list)
396 if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) {
398 IF_CLONE_REMREF(ifc);
401 LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
402 V_if_cloners_count++;
409 ifc_attach_cloner(const char *name, struct if_clone_addreq *req)
411 if (req->create_f == NULL || req->destroy_f == NULL)
413 if (strnlen(name, IFCLOSIZ) >= (IFCLOSIZ - 1))
416 struct if_clone *ifc = if_clone_alloc(name, req->maxunit);
417 ifc->ifc_match = req->match_f != NULL ? req->match_f : ifc_simple_match;
418 ifc->ifc_create = req->create_f;
419 ifc->ifc_destroy = req->destroy_f;
420 ifc->ifc_flags = (req->flags & (IFC_F_AUTOUNIT | IFC_F_NOGROUP));
422 if (if_clone_attach(ifc) != 0)
425 EVENTHANDLER_INVOKE(if_clone_event, ifc);
431 ifc_detach_cloner(struct if_clone *ifc)
433 if_clone_detach(ifc);
437 #ifdef CLONE_COMPAT_13
440 ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
441 struct ifc_data *ifc_data, struct ifnet **ifpp)
443 int error = ifc->ifca_create(ifc, name, maxlen, ifc_data->params);
446 *ifpp = ifunit(name);
451 ifc_advanced_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
453 if (ifc->ifca_destroy == NULL)
455 return (ifc->ifca_destroy(ifc, ifp));
459 if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
460 ifc_create_t create, ifc_destroy_t destroy)
462 struct if_clone *ifc;
464 ifc = if_clone_alloc(name, maxunit);
465 ifc->ifc_match = match;
466 ifc->ifc_create = ifc_advanced_create_wrapper;
467 ifc->ifc_destroy = ifc_advanced_destroy_wrapper;
468 ifc->ifca_destroy = destroy;
469 ifc->ifca_create = create;
471 if (if_clone_attach(ifc) != 0)
474 EVENTHANDLER_INVOKE(if_clone_event, ifc);
480 ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
481 struct ifc_data *ifc_data, struct ifnet **ifpp)
485 ifc_name2unit(name, &unit);
486 int error = ifc->ifcs_create(ifc, unit, ifc_data->params);
488 *ifpp = ifunit(name);
493 ifc_simple_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
495 if (ifp->if_dunit < ifc->ifcs_minifs && (flags & IFC_F_FORCE) == 0)
498 ifc->ifcs_destroy(ifp);
503 if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
506 struct if_clone *ifc;
509 ifc = if_clone_alloc(name, 0);
510 ifc->ifc_match = ifc_simple_match;
511 ifc->ifc_create = ifc_simple_create_wrapper;
512 ifc->ifc_destroy = ifc_simple_destroy_wrapper;
513 ifc->ifcs_create = create;
514 ifc->ifcs_destroy = destroy;
515 ifc->ifcs_minifs = minifs;
516 ifc->ifc_flags = IFC_F_AUTOUNIT;
518 if (if_clone_attach(ifc) != 0)
521 for (unit = 0; unit < minifs; unit++) {
524 struct ifc_data ifd = {};
527 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
528 error = if_clone_createif(ifc, name, IFNAMSIZ, &ifd, &ifp);
530 ("%s: failed to create required interface %s",
534 EVENTHANDLER_INVOKE(if_clone_event, ifc);
541 * Unregister a network interface cloner.
544 if_clone_detach(struct if_clone *ifc)
548 LIST_REMOVE(ifc, ifc_list);
549 V_if_cloners_count--;
552 /* destroy all interfaces for this cloner */
553 while (!LIST_EMPTY(&ifc->ifc_iflist))
554 if_clone_destroyif_flags(ifc, LIST_FIRST(&ifc->ifc_iflist), IFC_F_FORCE);
556 IF_CLONE_REMREF(ifc);
560 if_clone_free(struct if_clone *ifc)
563 KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
564 ("%s: ifc_iflist not empty", __func__));
566 IF_CLONE_LOCK_DESTROY(ifc);
567 delete_unrhdr(ifc->ifc_unrhdr);
572 * Provide list of interface cloners to userspace.
575 if_clone_list(struct if_clonereq *ifcr)
577 char *buf, *dst, *outbuf = NULL;
578 struct if_clone *ifc;
579 int buf_count, count, err = 0;
581 if (ifcr->ifcr_count < 0)
586 * Set our internal output buffer size. We could end up not
587 * reporting a cloner that is added between the unlock and lock
588 * below, but that's not a major problem. Not caping our
589 * allocation to the number of cloners actually in the system
590 * could be because that would let arbitrary users cause us to
591 * allocate arbitrary amounts of kernel memory.
593 buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
594 V_if_cloners_count : ifcr->ifcr_count;
597 outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
601 ifcr->ifcr_total = V_if_cloners_count;
602 if ((dst = ifcr->ifcr_buffer) == NULL) {
603 /* Just asking how many there are. */
606 count = (V_if_cloners_count < buf_count) ?
607 V_if_cloners_count : buf_count;
609 for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
610 ifc != NULL && count != 0;
611 ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
612 strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
617 if (err == 0 && dst != NULL)
618 err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
620 free(outbuf, M_CLONE);
625 * if_clone_findifc() looks up ifnet from the current
626 * cloner list, and returns ifc if found. Note that ifc_refcnt
630 if_clone_findifc(struct ifnet *ifp)
632 struct if_clone *ifc, *ifc0;
633 struct ifnet *ifcifp;
637 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
639 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
642 IF_CLONE_ADDREF_LOCKED(ifc);
646 IF_CLONE_UNLOCK(ifc);
656 * if_clone_addgroup() decrements ifc_refcnt because it is called after
657 * if_clone_findifc().
660 if_clone_addgroup(struct ifnet *ifp, struct if_clone *ifc)
662 if ((ifc->ifc_flags & IFC_NOGROUP) == 0) {
663 if_addgroup(ifp, ifc->ifc_name);
664 IF_CLONE_REMREF(ifc);
669 * A utility function to extract unit numbers from interface names of
672 * Returns 0 on success and an error on failure.
675 ifc_name2unit(const char *name, int *unit)
678 int cutoff = INT_MAX / 10;
679 int cutlim = INT_MAX % 10;
681 for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++)
685 } else if (cp[0] == '0' && cp[1] != '\0') {
686 /* Disallow leading zeroes. */
689 for (*unit = 0; *cp != '\0'; cp++) {
690 if (*cp < '0' || *cp > '9') {
691 /* Bogus unit number. */
694 if (*unit > cutoff ||
695 (*unit == cutoff && *cp - '0' > cutlim))
697 *unit = (*unit * 10) + (*cp - '0');
705 ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
709 if (*unit > ifc->ifc_maxunit)
712 if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
715 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
716 if (ifunit(name) != NULL) {
717 free_unr(ifc->ifc_unrhdr, *unit);
721 IF_CLONE_ADDREF(ifc);
727 ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
731 *unit = alloc_unr(ifc->ifc_unrhdr);
735 free_unr(ifc->ifc_unrhdr, *unit);
737 error = ifc_alloc_unit_specific(ifc, unit);
748 ifc_alloc_unit(struct if_clone *ifc, int *unit)
751 return (ifc_alloc_unit_next(ifc, unit));
753 return (ifc_alloc_unit_specific(ifc, unit));
757 ifc_free_unit(struct if_clone *ifc, int unit)
760 free_unr(ifc->ifc_unrhdr, unit);
761 IF_CLONE_REMREF(ifc);
765 ifc_simple_match(struct if_clone *ifc, const char *name)
771 for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
772 if (ifc->ifc_name[i] != *cp)
776 /* Make sure there's a unit number or nothing after the name */
777 for (; *cp != '\0'; cp++) {
778 if (*cp < '0' || *cp > '9')
786 ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
793 err = ifc_name2unit(name, &unit);
797 wildcard = (unit < 0);
799 err = ifc_alloc_unit(ifc, &unit);
803 /* In the wildcard case, we need to update the name. */
805 for (dp = name; *dp != '\0'; dp++);
806 if (snprintf(dp, len - (dp-name), "%d", unit) >
807 len - (dp-name) - 1) {
809 * This can only be a programmer error and
810 * there's no straightforward way to recover if
813 panic("if_clone_create(): interface name too long");
822 ifc_copyin(const struct ifc_data *ifd, void *target, size_t len)
824 if (ifd->params == NULL)
827 if (ifd->flags & IFC_F_SYSSPACE) {
828 memcpy(target, ifd->params, len);
831 return (copyin(ifd->params, target, len));
835 ifc_name(struct if_clone *ifc)
837 return (ifc->ifc_name);
841 ifc_flags_set(struct if_clone *ifc, int flags)
843 ifc->ifc_flags = flags;
847 ifc_flags_get(struct if_clone *ifc)
849 return (ifc->ifc_flags);