2 * Copyright (c) 1980, 1986, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)if.c 8.5 (Berkeley) 1/9/95
33 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/limits.h>
37 #include <sys/mutex.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
44 #include <net/if_clone.h>
46 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/if_var.h>
50 #include <net/radix.h>
51 #include <net/route.h>
54 static void if_clone_free(struct if_clone *ifc);
55 static int if_clone_createif(struct if_clone *ifc, char *name, size_t len,
58 static struct mtx if_cloners_mtx;
59 static VNET_DEFINE(int, if_cloners_count);
60 VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners);
62 #define V_if_cloners_count VNET(if_cloners_count)
63 #define V_if_cloners VNET(if_cloners)
65 #define IF_CLONERS_LOCK_INIT() \
66 mtx_init(&if_cloners_mtx, "if_cloners lock", NULL, MTX_DEF)
67 #define IF_CLONERS_LOCK_ASSERT() mtx_assert(&if_cloners_mtx, MA_OWNED)
68 #define IF_CLONERS_LOCK() mtx_lock(&if_cloners_mtx)
69 #define IF_CLONERS_UNLOCK() mtx_unlock(&if_cloners_mtx)
71 #define IF_CLONE_LOCK_INIT(ifc) \
72 mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
73 #define IF_CLONE_LOCK_DESTROY(ifc) mtx_destroy(&(ifc)->ifc_mtx)
74 #define IF_CLONE_LOCK_ASSERT(ifc) mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
75 #define IF_CLONE_LOCK(ifc) mtx_lock(&(ifc)->ifc_mtx)
76 #define IF_CLONE_UNLOCK(ifc) mtx_unlock(&(ifc)->ifc_mtx)
78 #define IF_CLONE_ADDREF(ifc) \
81 IF_CLONE_ADDREF_LOCKED(ifc); \
82 IF_CLONE_UNLOCK(ifc); \
84 #define IF_CLONE_ADDREF_LOCKED(ifc) \
86 IF_CLONE_LOCK_ASSERT(ifc); \
87 KASSERT((ifc)->ifc_refcnt >= 0, \
88 ("negative refcnt %ld", (ifc)->ifc_refcnt)); \
89 (ifc)->ifc_refcnt++; \
91 #define IF_CLONE_REMREF(ifc) \
94 IF_CLONE_REMREF_LOCKED(ifc); \
96 #define IF_CLONE_REMREF_LOCKED(ifc) \
98 IF_CLONE_LOCK_ASSERT(ifc); \
99 KASSERT((ifc)->ifc_refcnt > 0, \
100 ("bogus refcnt %ld", (ifc)->ifc_refcnt)); \
101 if (--(ifc)->ifc_refcnt == 0) { \
102 IF_CLONE_UNLOCK(ifc); \
103 if_clone_free(ifc); \
105 /* silently free the lock */ \
106 IF_CLONE_UNLOCK(ifc); \
110 #define IFC_IFLIST_INSERT(_ifc, _ifp) \
111 LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones)
112 #define IFC_IFLIST_REMOVE(_ifc, _ifp) \
113 LIST_REMOVE(_ifp, if_clones)
115 static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
118 vnet_if_clone_init(void)
121 LIST_INIT(&V_if_cloners);
128 IF_CLONERS_LOCK_INIT();
132 * Lookup and create a clone network interface.
135 if_clone_create(char *name, size_t len, caddr_t params)
137 struct if_clone *ifc;
139 /* Try to find an applicable cloner for this request */
141 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
142 if (ifc->ifc_match(ifc, name)) {
147 if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
148 CURVNET_SET_QUIET(vnet0);
149 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
150 if (ifc->ifc_match(ifc, name))
161 return (if_clone_createif(ifc, name, len, params));
165 * Create a clone network interface.
168 if_clone_createif(struct if_clone *ifc, char *name, size_t len, caddr_t params)
173 if (ifunit(name) != NULL)
176 err = (*ifc->ifc_create)(ifc, name, len, params);
181 panic("%s: lookup failed for %s", __func__, name);
183 if_addgroup(ifp, ifc->ifc_name);
186 IFC_IFLIST_INSERT(ifc, ifp);
187 IF_CLONE_UNLOCK(ifc);
194 * Lookup and destroy a clone network interface.
197 if_clone_destroy(const char *name)
200 struct if_clone *ifc;
203 ifp = ifunit_ref(name);
207 /* Find the cloner for this interface */
209 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
210 if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) {
215 if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
216 CURVNET_SET_QUIET(vnet0);
217 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
218 if (ifc->ifc_match(ifc, name))
230 err = if_clone_destroyif(ifc, ifp);
236 * Destroy a clone network interface.
239 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
242 struct ifnet *ifcifp;
244 if (ifc->ifc_destroy == NULL)
248 * Given that the cloned ifnet might be attached to a different
249 * vnet from where its cloner was registered, we have to
250 * switch to the vnet context of the target vnet.
252 CURVNET_SET_QUIET(ifp->if_vnet);
255 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
257 IFC_IFLIST_REMOVE(ifc, ifp);
261 IF_CLONE_UNLOCK(ifc);
262 if (ifcifp == NULL) {
264 return (ENXIO); /* ifp is not on the list. */
267 if_delgroup(ifp, ifc->ifc_name);
269 err = (*ifc->ifc_destroy)(ifc, ifp);
272 if_addgroup(ifp, ifc->ifc_name);
275 IFC_IFLIST_INSERT(ifc, ifp);
276 IF_CLONE_UNLOCK(ifc);
283 * Register a network interface cloner.
286 if_clone_attach(struct if_clone *ifc)
291 * Compute bitmap size and allocate it.
293 maxclone = ifc->ifc_maxunit + 1;
295 if ((len << 3) < maxclone)
297 ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
298 ifc->ifc_bmlen = len;
299 IF_CLONE_LOCK_INIT(ifc);
300 IF_CLONE_ADDREF(ifc);
303 LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
304 V_if_cloners_count++;
307 LIST_INIT(&ifc->ifc_iflist);
309 if (ifc->ifc_attach != NULL)
310 (*ifc->ifc_attach)(ifc);
311 EVENTHANDLER_INVOKE(if_clone_event, ifc);
315 * Unregister a network interface cloner.
318 if_clone_detach(struct if_clone *ifc)
320 struct ifc_simple_data *ifcs = ifc->ifc_data;
323 LIST_REMOVE(ifc, ifc_list);
324 V_if_cloners_count--;
327 /* Allow all simples to be destroyed */
328 if (ifc->ifc_attach == ifc_simple_attach)
329 ifcs->ifcs_minifs = 0;
331 /* destroy all interfaces for this cloner */
332 while (!LIST_EMPTY(&ifc->ifc_iflist))
333 if_clone_destroyif(ifc, LIST_FIRST(&ifc->ifc_iflist));
335 IF_CLONE_REMREF(ifc);
339 if_clone_free(struct if_clone *ifc)
341 for (int bytoff = 0; bytoff < ifc->ifc_bmlen; bytoff++) {
342 KASSERT(ifc->ifc_units[bytoff] == 0x00,
343 ("ifc_units[%d] is not empty", bytoff));
346 KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
347 ("%s: ifc_iflist not empty", __func__));
349 IF_CLONE_LOCK_DESTROY(ifc);
350 free(ifc->ifc_units, M_CLONE);
354 * Provide list of interface cloners to userspace.
357 if_clone_list(struct if_clonereq *ifcr)
359 char *buf, *dst, *outbuf = NULL;
360 struct if_clone *ifc;
361 int buf_count, count, err = 0;
363 if (ifcr->ifcr_count < 0)
368 * Set our internal output buffer size. We could end up not
369 * reporting a cloner that is added between the unlock and lock
370 * below, but that's not a major problem. Not caping our
371 * allocation to the number of cloners actually in the system
372 * could be because that would let arbitrary users cause us to
373 * allocate abritrary amounts of kernel memory.
375 buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
376 V_if_cloners_count : ifcr->ifcr_count;
379 outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
383 ifcr->ifcr_total = V_if_cloners_count;
384 if ((dst = ifcr->ifcr_buffer) == NULL) {
385 /* Just asking how many there are. */
388 count = (V_if_cloners_count < buf_count) ?
389 V_if_cloners_count : buf_count;
391 for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
392 ifc != NULL && count != 0;
393 ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
394 strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
400 err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
402 free(outbuf, M_CLONE);
407 * A utility function to extract unit numbers from interface names of
410 * Returns 0 on success and an error on failure.
413 ifc_name2unit(const char *name, int *unit)
416 int cutoff = INT_MAX / 10;
417 int cutlim = INT_MAX % 10;
419 for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++);
422 } else if (cp[0] == '0' && cp[1] != '\0') {
423 /* Disallow leading zeroes. */
426 for (*unit = 0; *cp != '\0'; cp++) {
427 if (*cp < '0' || *cp > '9') {
428 /* Bogus unit number. */
431 if (*unit > cutoff ||
432 (*unit == cutoff && *cp - '0' > cutlim))
434 *unit = (*unit * 10) + (*cp - '0');
442 ifc_alloc_unit(struct if_clone *ifc, int *unit)
444 int wildcard, bytoff, bitoff;
450 wildcard = (*unit < 0);
452 * Find a free unit if none was given.
455 while ((bytoff < ifc->ifc_bmlen)
456 && (ifc->ifc_units[bytoff] == 0xff))
458 if (bytoff >= ifc->ifc_bmlen) {
462 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
464 *unit = (bytoff << 3) + bitoff;
467 if (*unit > ifc->ifc_maxunit) {
474 bitoff = *unit - (bytoff << 3);
477 if((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) {
482 * Allocate the unit in the bitmap.
484 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
485 ("%s: bit is already set", __func__));
486 ifc->ifc_units[bytoff] |= (1 << bitoff);
487 IF_CLONE_ADDREF_LOCKED(ifc);
490 IF_CLONE_UNLOCK(ifc);
495 ifc_free_unit(struct if_clone *ifc, int unit)
501 * Compute offset in the bitmap and deallocate the unit.
504 bitoff = unit - (bytoff << 3);
507 KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
508 ("%s: bit is already cleared", __func__));
509 ifc->ifc_units[bytoff] &= ~(1 << bitoff);
510 IF_CLONE_REMREF_LOCKED(ifc); /* releases lock */
514 ifc_simple_attach(struct if_clone *ifc)
519 struct ifc_simple_data *ifcs = ifc->ifc_data;
521 KASSERT(ifcs->ifcs_minifs - 1 <= ifc->ifc_maxunit,
522 ("%s: %s requested more units than allowed (%d > %d)",
523 __func__, ifc->ifc_name, ifcs->ifcs_minifs,
524 ifc->ifc_maxunit + 1));
526 for (unit = 0; unit < ifcs->ifcs_minifs; unit++) {
527 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
528 err = if_clone_createif(ifc, name, IFNAMSIZ, NULL);
530 ("%s: failed to create required interface %s",
536 ifc_simple_match(struct if_clone *ifc, const char *name)
542 for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
543 if (ifc->ifc_name[i] != *cp)
547 /* Make sure there's a unit number or nothing after the name */
548 for (; *cp != '\0'; cp++) {
549 if (*cp < '0' || *cp > '9')
557 ifc_simple_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
563 struct ifc_simple_data *ifcs = ifc->ifc_data;
565 err = ifc_name2unit(name, &unit);
569 wildcard = (unit < 0);
571 err = ifc_alloc_unit(ifc, &unit);
575 err = ifcs->ifcs_create(ifc, unit, params);
577 ifc_free_unit(ifc, unit);
581 /* In the wildcard case, we need to update the name. */
583 for (dp = name; *dp != '\0'; dp++);
584 if (snprintf(dp, len - (dp-name), "%d", unit) >
585 len - (dp-name) - 1) {
587 * This can only be a programmer error and
588 * there's no straightforward way to recover if
591 panic("if_clone_create(): interface name too long");
600 ifc_simple_destroy(struct if_clone *ifc, struct ifnet *ifp)
603 struct ifc_simple_data *ifcs = ifc->ifc_data;
605 unit = ifp->if_dunit;
607 if (unit < ifcs->ifcs_minifs)
610 ifcs->ifcs_destroy(ifp);
612 ifc_free_unit(ifc, unit);