1c398230bSWarner Losh /*- 242a58907SGleb Smirnoff * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org> 3f889d2efSBrooks Davis * Copyright (c) 1980, 1986, 1993 4f889d2efSBrooks Davis * The Regents of the University of California. All rights reserved. 5f889d2efSBrooks Davis * 6f889d2efSBrooks Davis * Redistribution and use in source and binary forms, with or without 7f889d2efSBrooks Davis * modification, are permitted provided that the following conditions 8f889d2efSBrooks Davis * are met: 9f889d2efSBrooks Davis * 1. Redistributions of source code must retain the above copyright 10f889d2efSBrooks Davis * notice, this list of conditions and the following disclaimer. 11f889d2efSBrooks Davis * 2. Redistributions in binary form must reproduce the above copyright 12f889d2efSBrooks Davis * notice, this list of conditions and the following disclaimer in the 13f889d2efSBrooks Davis * documentation and/or other materials provided with the distribution. 14f889d2efSBrooks Davis * 4. Neither the name of the University nor the names of its contributors 15f889d2efSBrooks Davis * may be used to endorse or promote products derived from this software 16f889d2efSBrooks Davis * without specific prior written permission. 17f889d2efSBrooks Davis * 18f889d2efSBrooks Davis * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19f889d2efSBrooks Davis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20f889d2efSBrooks Davis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21f889d2efSBrooks Davis * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22f889d2efSBrooks Davis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23f889d2efSBrooks Davis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24f889d2efSBrooks Davis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25f889d2efSBrooks Davis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26f889d2efSBrooks Davis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27f889d2efSBrooks Davis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28f889d2efSBrooks Davis * SUCH DAMAGE. 29f889d2efSBrooks Davis * 30f889d2efSBrooks Davis * @(#)if.c 8.5 (Berkeley) 1/9/95 31f889d2efSBrooks Davis * $FreeBSD$ 32f889d2efSBrooks Davis */ 33f889d2efSBrooks Davis 34f889d2efSBrooks Davis #include <sys/param.h> 35f889d2efSBrooks Davis #include <sys/malloc.h> 36434dbbb3SRuslan Ermilov #include <sys/limits.h> 37f889d2efSBrooks Davis #include <sys/lock.h> 38f889d2efSBrooks Davis #include <sys/mutex.h> 39f889d2efSBrooks Davis #include <sys/kernel.h> 40f889d2efSBrooks Davis #include <sys/systm.h> 41f889d2efSBrooks Davis #include <sys/types.h> 42f889d2efSBrooks Davis #include <sys/socket.h> 43f889d2efSBrooks Davis 44f889d2efSBrooks Davis #include <net/if.h> 45f889d2efSBrooks Davis #include <net/if_var.h> 4676039bc8SGleb Smirnoff #include <net/if_clone.h> 47f889d2efSBrooks Davis #include <net/radix.h> 48f889d2efSBrooks Davis #include <net/route.h> 4921ca7b57SMarko Zec #include <net/vnet.h> 50f889d2efSBrooks Davis 5142a58907SGleb Smirnoff /* Current IF_MAXUNIT expands maximum to 5 characters. */ 5242a58907SGleb Smirnoff #define IFCLOSIZ (IFNAMSIZ - 5) 5342a58907SGleb Smirnoff 5442a58907SGleb Smirnoff /* 5542a58907SGleb Smirnoff * Structure describing a `cloning' interface. 5642a58907SGleb Smirnoff * 5742a58907SGleb Smirnoff * List of locks 5842a58907SGleb Smirnoff * (c) const until freeing 5942a58907SGleb Smirnoff * (d) driver specific data, may need external protection. 6042a58907SGleb Smirnoff * (e) locked by if_cloners_mtx 6142a58907SGleb Smirnoff * (i) locked by ifc_mtx mtx 6242a58907SGleb Smirnoff */ 6342a58907SGleb Smirnoff struct if_clone { 6442a58907SGleb Smirnoff char ifc_name[IFCLOSIZ]; /* (c) Name of device, e.g. `gif' */ 6542a58907SGleb Smirnoff struct unrhdr *ifc_unrhdr; /* (c) alloc_unr(9) header */ 6642a58907SGleb Smirnoff int ifc_maxunit; /* (c) maximum unit number */ 6742a58907SGleb Smirnoff long ifc_refcnt; /* (i) Reference count. */ 6842a58907SGleb Smirnoff LIST_HEAD(, ifnet) ifc_iflist; /* (i) List of cloned interfaces */ 6942a58907SGleb Smirnoff struct mtx ifc_mtx; /* Mutex to protect members. */ 7042a58907SGleb Smirnoff 7142a58907SGleb Smirnoff enum { SIMPLE, ADVANCED } ifc_type; /* (c) */ 7242a58907SGleb Smirnoff 7342a58907SGleb Smirnoff /* (c) Driver specific cloning functions. Called with no locks held. */ 7442a58907SGleb Smirnoff union { 7542a58907SGleb Smirnoff struct { /* advanced cloner */ 7642a58907SGleb Smirnoff ifc_match_t *_ifc_match; 7742a58907SGleb Smirnoff ifc_create_t *_ifc_create; 7842a58907SGleb Smirnoff ifc_destroy_t *_ifc_destroy; 7942a58907SGleb Smirnoff } A; 8042a58907SGleb Smirnoff struct { /* simple cloner */ 8142a58907SGleb Smirnoff ifcs_create_t *_ifcs_create; 8242a58907SGleb Smirnoff ifcs_destroy_t *_ifcs_destroy; 8342a58907SGleb Smirnoff int _ifcs_minifs; /* minimum ifs */ 8442a58907SGleb Smirnoff 8542a58907SGleb Smirnoff } S; 8642a58907SGleb Smirnoff } U; 8742a58907SGleb Smirnoff #define ifc_match U.A._ifc_match 8842a58907SGleb Smirnoff #define ifc_create U.A._ifc_create 8942a58907SGleb Smirnoff #define ifc_destroy U.A._ifc_destroy 9042a58907SGleb Smirnoff #define ifcs_create U.S._ifcs_create 9142a58907SGleb Smirnoff #define ifcs_destroy U.S._ifcs_destroy 9242a58907SGleb Smirnoff #define ifcs_minifs U.S._ifcs_minifs 9342a58907SGleb Smirnoff 9442a58907SGleb Smirnoff LIST_ENTRY(if_clone) ifc_list; /* (e) On list of cloners */ 9542a58907SGleb Smirnoff }; 9642a58907SGleb Smirnoff 97f889d2efSBrooks Davis static void if_clone_free(struct if_clone *ifc); 986b7330e2SSam Leffler static int if_clone_createif(struct if_clone *ifc, char *name, size_t len, 996b7330e2SSam Leffler caddr_t params); 100f889d2efSBrooks Davis 10142a58907SGleb Smirnoff static int ifc_simple_match(struct if_clone *, const char *); 10242a58907SGleb Smirnoff static int ifc_simple_create(struct if_clone *, char *, size_t, caddr_t); 10342a58907SGleb Smirnoff static int ifc_simple_destroy(struct if_clone *, struct ifnet *); 10442a58907SGleb Smirnoff 105f889d2efSBrooks Davis static struct mtx if_cloners_mtx; 1063e288e62SDimitry Andric static VNET_DEFINE(int, if_cloners_count); 107eddfbb76SRobert Watson VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners); 108eddfbb76SRobert Watson 1091e77c105SRobert Watson #define V_if_cloners_count VNET(if_cloners_count) 1101e77c105SRobert Watson #define V_if_cloners VNET(if_cloners) 111f889d2efSBrooks Davis 112f889d2efSBrooks Davis #define IF_CLONERS_LOCK_INIT() \ 113f889d2efSBrooks Davis mtx_init(&if_cloners_mtx, "if_cloners lock", NULL, MTX_DEF) 114f889d2efSBrooks Davis #define IF_CLONERS_LOCK_ASSERT() mtx_assert(&if_cloners_mtx, MA_OWNED) 115f889d2efSBrooks Davis #define IF_CLONERS_LOCK() mtx_lock(&if_cloners_mtx) 116f889d2efSBrooks Davis #define IF_CLONERS_UNLOCK() mtx_unlock(&if_cloners_mtx) 117f889d2efSBrooks Davis 118f889d2efSBrooks Davis #define IF_CLONE_LOCK_INIT(ifc) \ 119f889d2efSBrooks Davis mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF) 120f889d2efSBrooks Davis #define IF_CLONE_LOCK_DESTROY(ifc) mtx_destroy(&(ifc)->ifc_mtx) 121f889d2efSBrooks Davis #define IF_CLONE_LOCK_ASSERT(ifc) mtx_assert(&(ifc)->ifc_mtx, MA_OWNED) 122f889d2efSBrooks Davis #define IF_CLONE_LOCK(ifc) mtx_lock(&(ifc)->ifc_mtx) 123f889d2efSBrooks Davis #define IF_CLONE_UNLOCK(ifc) mtx_unlock(&(ifc)->ifc_mtx) 124f889d2efSBrooks Davis 125f889d2efSBrooks Davis #define IF_CLONE_ADDREF(ifc) \ 126f889d2efSBrooks Davis do { \ 127f889d2efSBrooks Davis IF_CLONE_LOCK(ifc); \ 128f889d2efSBrooks Davis IF_CLONE_ADDREF_LOCKED(ifc); \ 129f889d2efSBrooks Davis IF_CLONE_UNLOCK(ifc); \ 130f889d2efSBrooks Davis } while (0) 131f889d2efSBrooks Davis #define IF_CLONE_ADDREF_LOCKED(ifc) \ 132f889d2efSBrooks Davis do { \ 133f889d2efSBrooks Davis IF_CLONE_LOCK_ASSERT(ifc); \ 134f889d2efSBrooks Davis KASSERT((ifc)->ifc_refcnt >= 0, \ 135f889d2efSBrooks Davis ("negative refcnt %ld", (ifc)->ifc_refcnt)); \ 136f889d2efSBrooks Davis (ifc)->ifc_refcnt++; \ 137f889d2efSBrooks Davis } while (0) 138f889d2efSBrooks Davis #define IF_CLONE_REMREF(ifc) \ 139f889d2efSBrooks Davis do { \ 140f889d2efSBrooks Davis IF_CLONE_LOCK(ifc); \ 141f889d2efSBrooks Davis IF_CLONE_REMREF_LOCKED(ifc); \ 142f889d2efSBrooks Davis } while (0) 143f889d2efSBrooks Davis #define IF_CLONE_REMREF_LOCKED(ifc) \ 144f889d2efSBrooks Davis do { \ 145f889d2efSBrooks Davis IF_CLONE_LOCK_ASSERT(ifc); \ 146f889d2efSBrooks Davis KASSERT((ifc)->ifc_refcnt > 0, \ 147f889d2efSBrooks Davis ("bogus refcnt %ld", (ifc)->ifc_refcnt)); \ 148f889d2efSBrooks Davis if (--(ifc)->ifc_refcnt == 0) { \ 149f889d2efSBrooks Davis IF_CLONE_UNLOCK(ifc); \ 150f889d2efSBrooks Davis if_clone_free(ifc); \ 151ca64c799SMax Laier } else { \ 152f889d2efSBrooks Davis /* silently free the lock */ \ 153f889d2efSBrooks Davis IF_CLONE_UNLOCK(ifc); \ 154ca64c799SMax Laier } \ 155f889d2efSBrooks Davis } while (0) 156f889d2efSBrooks Davis 1574e7e0183SAndrew Thompson #define IFC_IFLIST_INSERT(_ifc, _ifp) \ 1584e7e0183SAndrew Thompson LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones) 1594e7e0183SAndrew Thompson #define IFC_IFLIST_REMOVE(_ifc, _ifp) \ 1604e7e0183SAndrew Thompson LIST_REMOVE(_ifp, if_clones) 1614e7e0183SAndrew Thompson 162c711aea6SPoul-Henning Kamp static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework"); 163f889d2efSBrooks Davis 164d0728d71SRobert Watson void 165d0728d71SRobert Watson vnet_if_clone_init(void) 16637f17770SMarko Zec { 16737f17770SMarko Zec 16837f17770SMarko Zec LIST_INIT(&V_if_cloners); 16937f17770SMarko Zec } 17037f17770SMarko Zec 171f889d2efSBrooks Davis void 172f889d2efSBrooks Davis if_clone_init(void) 173f889d2efSBrooks Davis { 17437f17770SMarko Zec 175f889d2efSBrooks Davis IF_CLONERS_LOCK_INIT(); 176f889d2efSBrooks Davis } 177f889d2efSBrooks Davis 178f889d2efSBrooks Davis /* 1794e7e0183SAndrew Thompson * Lookup and create a clone network interface. 180f889d2efSBrooks Davis */ 181f889d2efSBrooks Davis int 1826b7330e2SSam Leffler if_clone_create(char *name, size_t len, caddr_t params) 183f889d2efSBrooks Davis { 184f889d2efSBrooks Davis struct if_clone *ifc; 185f889d2efSBrooks Davis 186f889d2efSBrooks Davis /* Try to find an applicable cloner for this request */ 187f889d2efSBrooks Davis IF_CLONERS_LOCK(); 18842a58907SGleb Smirnoff LIST_FOREACH(ifc, &V_if_cloners, ifc_list) 18942a58907SGleb Smirnoff if (ifc->ifc_type == SIMPLE) { 19042a58907SGleb Smirnoff if (ifc_simple_match(ifc, name)) 191f889d2efSBrooks Davis break; 19242a58907SGleb Smirnoff } else { 19342a58907SGleb Smirnoff if (ifc->ifc_match(ifc, name)) 19442a58907SGleb Smirnoff break; 195f889d2efSBrooks Davis } 19637f17770SMarko Zec #ifdef VIMAGE 19737f17770SMarko Zec if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) { 19837f17770SMarko Zec CURVNET_SET_QUIET(vnet0); 19942a58907SGleb Smirnoff LIST_FOREACH(ifc, &V_if_cloners, ifc_list) 20042a58907SGleb Smirnoff if (ifc->ifc_type == SIMPLE) { 20142a58907SGleb Smirnoff if (ifc_simple_match(ifc, name)) 20242a58907SGleb Smirnoff break; 20342a58907SGleb Smirnoff } else { 20437f17770SMarko Zec if (ifc->ifc_match(ifc, name)) 20537f17770SMarko Zec break; 20637f17770SMarko Zec } 20737f17770SMarko Zec CURVNET_RESTORE(); 20837f17770SMarko Zec } 20937f17770SMarko Zec #endif 210f889d2efSBrooks Davis IF_CLONERS_UNLOCK(); 211f889d2efSBrooks Davis 212f889d2efSBrooks Davis if (ifc == NULL) 213f889d2efSBrooks Davis return (EINVAL); 214f889d2efSBrooks Davis 2156b7330e2SSam Leffler return (if_clone_createif(ifc, name, len, params)); 2164e7e0183SAndrew Thompson } 2174e7e0183SAndrew Thompson 2184e7e0183SAndrew Thompson /* 2194e7e0183SAndrew Thompson * Create a clone network interface. 2204e7e0183SAndrew Thompson */ 2214e7e0183SAndrew Thompson static int 2226b7330e2SSam Leffler if_clone_createif(struct if_clone *ifc, char *name, size_t len, caddr_t params) 2234e7e0183SAndrew Thompson { 2244e7e0183SAndrew Thompson int err; 2254e7e0183SAndrew Thompson struct ifnet *ifp; 2264e7e0183SAndrew Thompson 2274e7e0183SAndrew Thompson if (ifunit(name) != NULL) 2284e7e0183SAndrew Thompson return (EEXIST); 2294e7e0183SAndrew Thompson 23042a58907SGleb Smirnoff if (ifc->ifc_type == SIMPLE) 23142a58907SGleb Smirnoff err = ifc_simple_create(ifc, name, len, params); 23242a58907SGleb Smirnoff else 2336b7330e2SSam Leffler err = (*ifc->ifc_create)(ifc, name, len, params); 2344e7e0183SAndrew Thompson 2354e7e0183SAndrew Thompson if (!err) { 2364e7e0183SAndrew Thompson ifp = ifunit(name); 2374e7e0183SAndrew Thompson if (ifp == NULL) 2384e7e0183SAndrew Thompson panic("%s: lookup failed for %s", __func__, name); 2394e7e0183SAndrew Thompson 2400dad3f0eSMax Laier if_addgroup(ifp, ifc->ifc_name); 2410dad3f0eSMax Laier 2424e7e0183SAndrew Thompson IF_CLONE_LOCK(ifc); 2434e7e0183SAndrew Thompson IFC_IFLIST_INSERT(ifc, ifp); 2444e7e0183SAndrew Thompson IF_CLONE_UNLOCK(ifc); 2454e7e0183SAndrew Thompson } 2464e7e0183SAndrew Thompson 247f889d2efSBrooks Davis return (err); 248f889d2efSBrooks Davis } 249f889d2efSBrooks Davis 250f889d2efSBrooks Davis /* 2514e7e0183SAndrew Thompson * Lookup and destroy a clone network interface. 252f889d2efSBrooks Davis */ 253f889d2efSBrooks Davis int 254f889d2efSBrooks Davis if_clone_destroy(const char *name) 255f889d2efSBrooks Davis { 256d0088cdeSBjoern A. Zeeb int err; 257f889d2efSBrooks Davis struct if_clone *ifc; 258f889d2efSBrooks Davis struct ifnet *ifp; 259f889d2efSBrooks Davis 260d0088cdeSBjoern A. Zeeb ifp = ifunit_ref(name); 261f889d2efSBrooks Davis if (ifp == NULL) 262f889d2efSBrooks Davis return (ENXIO); 263f889d2efSBrooks Davis 264f889d2efSBrooks Davis /* Find the cloner for this interface */ 265f889d2efSBrooks Davis IF_CLONERS_LOCK(); 26637f17770SMarko Zec LIST_FOREACH(ifc, &V_if_cloners, ifc_list) { 267f889d2efSBrooks Davis if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) { 268f889d2efSBrooks Davis break; 269f889d2efSBrooks Davis } 270f889d2efSBrooks Davis } 27137f17770SMarko Zec #ifdef VIMAGE 27237f17770SMarko Zec if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) { 27337f17770SMarko Zec CURVNET_SET_QUIET(vnet0); 27442a58907SGleb Smirnoff LIST_FOREACH(ifc, &V_if_cloners, ifc_list) 2753aaf0159SGleb Smirnoff if (ifc->ifc_type == SIMPLE) { 27642a58907SGleb Smirnoff if (ifc_simple_match(ifc, name)) 27742a58907SGleb Smirnoff break; 27842a58907SGleb Smirnoff } else { 27937f17770SMarko Zec if (ifc->ifc_match(ifc, name)) 28037f17770SMarko Zec break; 28137f17770SMarko Zec } 28237f17770SMarko Zec CURVNET_RESTORE(); 28337f17770SMarko Zec } 28437f17770SMarko Zec #endif 285f889d2efSBrooks Davis IF_CLONERS_UNLOCK(); 286d0088cdeSBjoern A. Zeeb if (ifc == NULL) { 287d0088cdeSBjoern A. Zeeb if_rele(ifp); 288f889d2efSBrooks Davis return (EINVAL); 289d0088cdeSBjoern A. Zeeb } 290f889d2efSBrooks Davis 291d0088cdeSBjoern A. Zeeb err = if_clone_destroyif(ifc, ifp); 292d0088cdeSBjoern A. Zeeb if_rele(ifp); 293d0088cdeSBjoern A. Zeeb return err; 2944e7e0183SAndrew Thompson } 2954e7e0183SAndrew Thompson 2964e7e0183SAndrew Thompson /* 2974e7e0183SAndrew Thompson * Destroy a clone network interface. 2984e7e0183SAndrew Thompson */ 299cb959fa2SAndrew Thompson int 3004e7e0183SAndrew Thompson if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp) 3014e7e0183SAndrew Thompson { 3024e7e0183SAndrew Thompson int err; 303c769e1beSBjoern A. Zeeb struct ifnet *ifcifp; 3044e7e0183SAndrew Thompson 30542a58907SGleb Smirnoff if (ifc->ifc_type == ADVANCED && ifc->ifc_destroy == NULL) 30621ca7b57SMarko Zec return(EOPNOTSUPP); 307f889d2efSBrooks Davis 30837f17770SMarko Zec /* 30937f17770SMarko Zec * Given that the cloned ifnet might be attached to a different 31037f17770SMarko Zec * vnet from where its cloner was registered, we have to 31137f17770SMarko Zec * switch to the vnet context of the target vnet. 31237f17770SMarko Zec */ 31337f17770SMarko Zec CURVNET_SET_QUIET(ifp->if_vnet); 31437f17770SMarko Zec 315434dbbb3SRuslan Ermilov IF_CLONE_LOCK(ifc); 316c769e1beSBjoern A. Zeeb LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) { 317c769e1beSBjoern A. Zeeb if (ifcifp == ifp) { 318434dbbb3SRuslan Ermilov IFC_IFLIST_REMOVE(ifc, ifp); 319c769e1beSBjoern A. Zeeb break; 320c769e1beSBjoern A. Zeeb } 321c769e1beSBjoern A. Zeeb } 322434dbbb3SRuslan Ermilov IF_CLONE_UNLOCK(ifc); 323c769e1beSBjoern A. Zeeb if (ifcifp == NULL) { 324c769e1beSBjoern A. Zeeb CURVNET_RESTORE(); 325c769e1beSBjoern A. Zeeb return (ENXIO); /* ifp is not on the list. */ 326c769e1beSBjoern A. Zeeb } 327434dbbb3SRuslan Ermilov 3280dad3f0eSMax Laier if_delgroup(ifp, ifc->ifc_name); 3290dad3f0eSMax Laier 33042a58907SGleb Smirnoff if (ifc->ifc_type == SIMPLE) 33142a58907SGleb Smirnoff err = ifc_simple_destroy(ifc, ifp); 33242a58907SGleb Smirnoff else 333f889d2efSBrooks Davis err = (*ifc->ifc_destroy)(ifc, ifp); 334f889d2efSBrooks Davis 335434dbbb3SRuslan Ermilov if (err != 0) { 3360dad3f0eSMax Laier if_addgroup(ifp, ifc->ifc_name); 3370dad3f0eSMax Laier 338434dbbb3SRuslan Ermilov IF_CLONE_LOCK(ifc); 339434dbbb3SRuslan Ermilov IFC_IFLIST_INSERT(ifc, ifp); 340434dbbb3SRuslan Ermilov IF_CLONE_UNLOCK(ifc); 341434dbbb3SRuslan Ermilov } 34221ca7b57SMarko Zec CURVNET_RESTORE(); 343f889d2efSBrooks Davis return (err); 344f889d2efSBrooks Davis } 345f889d2efSBrooks Davis 34642a58907SGleb Smirnoff static struct if_clone * 34742a58907SGleb Smirnoff if_clone_alloc(const char *name, int maxunit) 34842a58907SGleb Smirnoff { 34942a58907SGleb Smirnoff struct if_clone *ifc; 35042a58907SGleb Smirnoff 35142a58907SGleb Smirnoff KASSERT(name != NULL, ("%s: no name\n", __func__)); 35242a58907SGleb Smirnoff 35342a58907SGleb Smirnoff ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO); 35442a58907SGleb Smirnoff strncpy(ifc->ifc_name, name, IFCLOSIZ-1); 35542a58907SGleb Smirnoff IF_CLONE_LOCK_INIT(ifc); 35642a58907SGleb Smirnoff IF_CLONE_ADDREF(ifc); 35742a58907SGleb Smirnoff ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT; 35842a58907SGleb Smirnoff ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx); 35942a58907SGleb Smirnoff LIST_INIT(&ifc->ifc_iflist); 36042a58907SGleb Smirnoff 36142a58907SGleb Smirnoff return (ifc); 36242a58907SGleb Smirnoff } 36342a58907SGleb Smirnoff 36442a58907SGleb Smirnoff static int 365f889d2efSBrooks Davis if_clone_attach(struct if_clone *ifc) 366f889d2efSBrooks Davis { 3672e9fff5bSGleb Smirnoff struct if_clone *ifc1; 368f889d2efSBrooks Davis 369f889d2efSBrooks Davis IF_CLONERS_LOCK(); 3702e9fff5bSGleb Smirnoff LIST_FOREACH(ifc1, &V_if_cloners, ifc_list) 3712e9fff5bSGleb Smirnoff if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) { 3722e9fff5bSGleb Smirnoff IF_CLONERS_UNLOCK(); 3732e9fff5bSGleb Smirnoff IF_CLONE_REMREF(ifc); 3742e9fff5bSGleb Smirnoff return (EEXIST); 3752e9fff5bSGleb Smirnoff } 37637f17770SMarko Zec LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list); 37737f17770SMarko Zec V_if_cloners_count++; 378f889d2efSBrooks Davis IF_CLONERS_UNLOCK(); 379f889d2efSBrooks Davis 38042a58907SGleb Smirnoff return (0); 38142a58907SGleb Smirnoff } 38242a58907SGleb Smirnoff 38342a58907SGleb Smirnoff struct if_clone * 38442a58907SGleb Smirnoff if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match, 38542a58907SGleb Smirnoff ifc_create_t create, ifc_destroy_t destroy) 38642a58907SGleb Smirnoff { 38742a58907SGleb Smirnoff struct if_clone *ifc; 38842a58907SGleb Smirnoff 38942a58907SGleb Smirnoff ifc = if_clone_alloc(name, maxunit); 39042a58907SGleb Smirnoff ifc->ifc_type = ADVANCED; 39142a58907SGleb Smirnoff ifc->ifc_match = match; 39242a58907SGleb Smirnoff ifc->ifc_create = create; 39342a58907SGleb Smirnoff ifc->ifc_destroy = destroy; 39442a58907SGleb Smirnoff 39542a58907SGleb Smirnoff if (if_clone_attach(ifc) != 0) { 39642a58907SGleb Smirnoff if_clone_free(ifc); 39742a58907SGleb Smirnoff return (NULL); 39842a58907SGleb Smirnoff } 39942a58907SGleb Smirnoff 400f889d2efSBrooks Davis EVENTHANDLER_INVOKE(if_clone_event, ifc); 4012e9fff5bSGleb Smirnoff 40242a58907SGleb Smirnoff return (ifc); 40342a58907SGleb Smirnoff } 40442a58907SGleb Smirnoff 40542a58907SGleb Smirnoff struct if_clone * 40642a58907SGleb Smirnoff if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy, 40742a58907SGleb Smirnoff u_int minifs) 40842a58907SGleb Smirnoff { 40942a58907SGleb Smirnoff struct if_clone *ifc; 41042a58907SGleb Smirnoff u_int unit; 41142a58907SGleb Smirnoff 41242a58907SGleb Smirnoff ifc = if_clone_alloc(name, 0); 41342a58907SGleb Smirnoff ifc->ifc_type = SIMPLE; 41442a58907SGleb Smirnoff ifc->ifcs_create = create; 41542a58907SGleb Smirnoff ifc->ifcs_destroy = destroy; 41642a58907SGleb Smirnoff ifc->ifcs_minifs = minifs; 41742a58907SGleb Smirnoff 41842a58907SGleb Smirnoff if (if_clone_attach(ifc) != 0) { 41942a58907SGleb Smirnoff if_clone_free(ifc); 42042a58907SGleb Smirnoff return (NULL); 42142a58907SGleb Smirnoff } 42242a58907SGleb Smirnoff 42342a58907SGleb Smirnoff for (unit = 0; unit < minifs; unit++) { 42442a58907SGleb Smirnoff char name[IFNAMSIZ]; 42542a58907SGleb Smirnoff int error; 42642a58907SGleb Smirnoff 42742a58907SGleb Smirnoff snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit); 42842a58907SGleb Smirnoff error = if_clone_createif(ifc, name, IFNAMSIZ, NULL); 42942a58907SGleb Smirnoff KASSERT(error == 0, 43042a58907SGleb Smirnoff ("%s: failed to create required interface %s", 43142a58907SGleb Smirnoff __func__, name)); 43242a58907SGleb Smirnoff } 43342a58907SGleb Smirnoff 43442a58907SGleb Smirnoff EVENTHANDLER_INVOKE(if_clone_event, ifc); 43542a58907SGleb Smirnoff 43642a58907SGleb Smirnoff return (ifc); 437f889d2efSBrooks Davis } 438f889d2efSBrooks Davis 439f889d2efSBrooks Davis /* 440f889d2efSBrooks Davis * Unregister a network interface cloner. 441f889d2efSBrooks Davis */ 442f889d2efSBrooks Davis void 443f889d2efSBrooks Davis if_clone_detach(struct if_clone *ifc) 444f889d2efSBrooks Davis { 445f889d2efSBrooks Davis 446f889d2efSBrooks Davis IF_CLONERS_LOCK(); 447f889d2efSBrooks Davis LIST_REMOVE(ifc, ifc_list); 44837f17770SMarko Zec V_if_cloners_count--; 449f889d2efSBrooks Davis IF_CLONERS_UNLOCK(); 450f889d2efSBrooks Davis 451febd0759SAndrew Thompson /* Allow all simples to be destroyed */ 45242a58907SGleb Smirnoff if (ifc->ifc_type == SIMPLE) 45342a58907SGleb Smirnoff ifc->ifcs_minifs = 0; 454febd0759SAndrew Thompson 4554e7e0183SAndrew Thompson /* destroy all interfaces for this cloner */ 4564e7e0183SAndrew Thompson while (!LIST_EMPTY(&ifc->ifc_iflist)) 4574e7e0183SAndrew Thompson if_clone_destroyif(ifc, LIST_FIRST(&ifc->ifc_iflist)); 4584e7e0183SAndrew Thompson 459f889d2efSBrooks Davis IF_CLONE_REMREF(ifc); 460f889d2efSBrooks Davis } 461f889d2efSBrooks Davis 462f889d2efSBrooks Davis static void 463f889d2efSBrooks Davis if_clone_free(struct if_clone *ifc) 464f889d2efSBrooks Davis { 465f889d2efSBrooks Davis 4664e7e0183SAndrew Thompson KASSERT(LIST_EMPTY(&ifc->ifc_iflist), 4674e7e0183SAndrew Thompson ("%s: ifc_iflist not empty", __func__)); 4684e7e0183SAndrew Thompson 469f889d2efSBrooks Davis IF_CLONE_LOCK_DESTROY(ifc); 4702e9fff5bSGleb Smirnoff delete_unrhdr(ifc->ifc_unrhdr); 47142a58907SGleb Smirnoff free(ifc, M_CLONE); 472f889d2efSBrooks Davis } 473f889d2efSBrooks Davis 474f889d2efSBrooks Davis /* 475f889d2efSBrooks Davis * Provide list of interface cloners to userspace. 476f889d2efSBrooks Davis */ 477f889d2efSBrooks Davis int 478f889d2efSBrooks Davis if_clone_list(struct if_clonereq *ifcr) 479f889d2efSBrooks Davis { 480c859ef97SBrooks Davis char *buf, *dst, *outbuf = NULL; 481f889d2efSBrooks Davis struct if_clone *ifc; 482c859ef97SBrooks Davis int buf_count, count, err = 0; 483c859ef97SBrooks Davis 484a6d00835SMaxim Konovalov if (ifcr->ifcr_count < 0) 485a6d00835SMaxim Konovalov return (EINVAL); 486a6d00835SMaxim Konovalov 487c859ef97SBrooks Davis IF_CLONERS_LOCK(); 488c859ef97SBrooks Davis /* 489c859ef97SBrooks Davis * Set our internal output buffer size. We could end up not 490c859ef97SBrooks Davis * reporting a cloner that is added between the unlock and lock 491c859ef97SBrooks Davis * below, but that's not a major problem. Not caping our 492c859ef97SBrooks Davis * allocation to the number of cloners actually in the system 493c859ef97SBrooks Davis * could be because that would let arbitrary users cause us to 494c859ef97SBrooks Davis * allocate abritrary amounts of kernel memory. 495c859ef97SBrooks Davis */ 49637f17770SMarko Zec buf_count = (V_if_cloners_count < ifcr->ifcr_count) ? 49737f17770SMarko Zec V_if_cloners_count : ifcr->ifcr_count; 498c859ef97SBrooks Davis IF_CLONERS_UNLOCK(); 499c859ef97SBrooks Davis 500c859ef97SBrooks Davis outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO); 501f889d2efSBrooks Davis 502f889d2efSBrooks Davis IF_CLONERS_LOCK(); 503f889d2efSBrooks Davis 50437f17770SMarko Zec ifcr->ifcr_total = V_if_cloners_count; 505f889d2efSBrooks Davis if ((dst = ifcr->ifcr_buffer) == NULL) { 506f889d2efSBrooks Davis /* Just asking how many there are. */ 507f889d2efSBrooks Davis goto done; 508f889d2efSBrooks Davis } 50937f17770SMarko Zec count = (V_if_cloners_count < buf_count) ? 51037f17770SMarko Zec V_if_cloners_count : buf_count; 511f889d2efSBrooks Davis 51237f17770SMarko Zec for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf; 513c859ef97SBrooks Davis ifc != NULL && count != 0; 514c859ef97SBrooks Davis ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) { 515c859ef97SBrooks Davis strlcpy(buf, ifc->ifc_name, IFNAMSIZ); 516f889d2efSBrooks Davis } 517f889d2efSBrooks Davis 518f889d2efSBrooks Davis done: 519f889d2efSBrooks Davis IF_CLONERS_UNLOCK(); 520c859ef97SBrooks Davis if (err == 0) 521c859ef97SBrooks Davis err = copyout(outbuf, dst, buf_count*IFNAMSIZ); 522c859ef97SBrooks Davis if (outbuf != NULL) 523c859ef97SBrooks Davis free(outbuf, M_CLONE); 524f889d2efSBrooks Davis return (err); 525f889d2efSBrooks Davis } 526f889d2efSBrooks Davis 527f889d2efSBrooks Davis /* 528f889d2efSBrooks Davis * A utility function to extract unit numbers from interface names of 529f889d2efSBrooks Davis * the form name###. 530f889d2efSBrooks Davis * 531f889d2efSBrooks Davis * Returns 0 on success and an error on failure. 532f889d2efSBrooks Davis */ 533f889d2efSBrooks Davis int 534f889d2efSBrooks Davis ifc_name2unit(const char *name, int *unit) 535f889d2efSBrooks Davis { 536f889d2efSBrooks Davis const char *cp; 537434dbbb3SRuslan Ermilov int cutoff = INT_MAX / 10; 538434dbbb3SRuslan Ermilov int cutlim = INT_MAX % 10; 539f889d2efSBrooks Davis 540f889d2efSBrooks Davis for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++); 541f889d2efSBrooks Davis if (*cp == '\0') { 542f889d2efSBrooks Davis *unit = -1; 543434dbbb3SRuslan Ermilov } else if (cp[0] == '0' && cp[1] != '\0') { 544434dbbb3SRuslan Ermilov /* Disallow leading zeroes. */ 545434dbbb3SRuslan Ermilov return (EINVAL); 546f889d2efSBrooks Davis } else { 547f889d2efSBrooks Davis for (*unit = 0; *cp != '\0'; cp++) { 548f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') { 549f889d2efSBrooks Davis /* Bogus unit number. */ 550f889d2efSBrooks Davis return (EINVAL); 551f889d2efSBrooks Davis } 552434dbbb3SRuslan Ermilov if (*unit > cutoff || 553434dbbb3SRuslan Ermilov (*unit == cutoff && *cp - '0' > cutlim)) 554434dbbb3SRuslan Ermilov return (EINVAL); 555f889d2efSBrooks Davis *unit = (*unit * 10) + (*cp - '0'); 556f889d2efSBrooks Davis } 557f889d2efSBrooks Davis } 558f889d2efSBrooks Davis 559f889d2efSBrooks Davis return (0); 560f889d2efSBrooks Davis } 561f889d2efSBrooks Davis 562f889d2efSBrooks Davis int 563f889d2efSBrooks Davis ifc_alloc_unit(struct if_clone *ifc, int *unit) 564f889d2efSBrooks Davis { 5652e9fff5bSGleb Smirnoff char name[IFNAMSIZ]; 5662e9fff5bSGleb Smirnoff int wildcard; 567f889d2efSBrooks Davis 568f889d2efSBrooks Davis wildcard = (*unit < 0); 5692e9fff5bSGleb Smirnoff retry: 5703932d760SGleb Smirnoff if (*unit > ifc->ifc_maxunit) 5713932d760SGleb Smirnoff return (ENOSPC); 5723932d760SGleb Smirnoff if (*unit < 0) { 5732e9fff5bSGleb Smirnoff *unit = alloc_unr(ifc->ifc_unrhdr); 5742e9fff5bSGleb Smirnoff if (*unit == -1) 5752e9fff5bSGleb Smirnoff return (ENOSPC); 5762e9fff5bSGleb Smirnoff } else { 5772e9fff5bSGleb Smirnoff *unit = alloc_unr_specific(ifc->ifc_unrhdr, *unit); 5783932d760SGleb Smirnoff if (*unit == -1) { 5793932d760SGleb Smirnoff if (wildcard) { 5803932d760SGleb Smirnoff (*unit)++; 5813932d760SGleb Smirnoff goto retry; 5823932d760SGleb Smirnoff } else 5832e9fff5bSGleb Smirnoff return (EEXIST); 584f889d2efSBrooks Davis } 5853932d760SGleb Smirnoff } 586f889d2efSBrooks Davis 5872e9fff5bSGleb Smirnoff snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit); 5882e9fff5bSGleb Smirnoff if (ifunit(name) != NULL) { 5893932d760SGleb Smirnoff free_unr(ifc->ifc_unrhdr, *unit); 5903932d760SGleb Smirnoff if (wildcard) { 5913932d760SGleb Smirnoff (*unit)++; 5923932d760SGleb Smirnoff goto retry; 5933932d760SGleb Smirnoff } else 5942e9fff5bSGleb Smirnoff return (EEXIST); 595f889d2efSBrooks Davis } 596f889d2efSBrooks Davis 5972e9fff5bSGleb Smirnoff IF_CLONE_ADDREF(ifc); 598f889d2efSBrooks Davis 5992e9fff5bSGleb Smirnoff return (0); 600f889d2efSBrooks Davis } 601f889d2efSBrooks Davis 602f889d2efSBrooks Davis void 603f889d2efSBrooks Davis ifc_free_unit(struct if_clone *ifc, int unit) 604f889d2efSBrooks Davis { 605f889d2efSBrooks Davis 6062e9fff5bSGleb Smirnoff free_unr(ifc->ifc_unrhdr, unit); 6072e9fff5bSGleb Smirnoff IF_CLONE_REMREF(ifc); 608f889d2efSBrooks Davis } 609f889d2efSBrooks Davis 61042a58907SGleb Smirnoff static int 611f889d2efSBrooks Davis ifc_simple_match(struct if_clone *ifc, const char *name) 612f889d2efSBrooks Davis { 613f889d2efSBrooks Davis const char *cp; 614f889d2efSBrooks Davis int i; 615f889d2efSBrooks Davis 616f889d2efSBrooks Davis /* Match the name */ 617f889d2efSBrooks Davis for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) { 618f889d2efSBrooks Davis if (ifc->ifc_name[i] != *cp) 619f889d2efSBrooks Davis return (0); 620f889d2efSBrooks Davis } 621f889d2efSBrooks Davis 622f889d2efSBrooks Davis /* Make sure there's a unit number or nothing after the name */ 623f889d2efSBrooks Davis for (; *cp != '\0'; cp++) { 624f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 625f889d2efSBrooks Davis return (0); 626f889d2efSBrooks Davis } 627f889d2efSBrooks Davis 628f889d2efSBrooks Davis return (1); 629f889d2efSBrooks Davis } 630f889d2efSBrooks Davis 63142a58907SGleb Smirnoff static int 6326b7330e2SSam Leffler ifc_simple_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 633f889d2efSBrooks Davis { 634f889d2efSBrooks Davis char *dp; 635f889d2efSBrooks Davis int wildcard; 636f889d2efSBrooks Davis int unit; 637f889d2efSBrooks Davis int err; 638f889d2efSBrooks Davis 639f889d2efSBrooks Davis err = ifc_name2unit(name, &unit); 640f889d2efSBrooks Davis if (err != 0) 641f889d2efSBrooks Davis return (err); 642f889d2efSBrooks Davis 643f889d2efSBrooks Davis wildcard = (unit < 0); 644f889d2efSBrooks Davis 645f889d2efSBrooks Davis err = ifc_alloc_unit(ifc, &unit); 646f889d2efSBrooks Davis if (err != 0) 647f889d2efSBrooks Davis return (err); 648f889d2efSBrooks Davis 64942a58907SGleb Smirnoff err = ifc->ifcs_create(ifc, unit, params); 650f889d2efSBrooks Davis if (err != 0) { 651f889d2efSBrooks Davis ifc_free_unit(ifc, unit); 652f889d2efSBrooks Davis return (err); 653f889d2efSBrooks Davis } 654f889d2efSBrooks Davis 655f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 656f889d2efSBrooks Davis if (wildcard) { 657f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 658f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 659f889d2efSBrooks Davis len - (dp-name) - 1) { 660f889d2efSBrooks Davis /* 661f889d2efSBrooks Davis * This can only be a programmer error and 662f889d2efSBrooks Davis * there's no straightforward way to recover if 663f889d2efSBrooks Davis * it happens. 664f889d2efSBrooks Davis */ 665f889d2efSBrooks Davis panic("if_clone_create(): interface name too long"); 666f889d2efSBrooks Davis } 667f889d2efSBrooks Davis 668f889d2efSBrooks Davis } 669f889d2efSBrooks Davis 670f889d2efSBrooks Davis return (0); 671f889d2efSBrooks Davis } 672f889d2efSBrooks Davis 67342a58907SGleb Smirnoff static int 674f889d2efSBrooks Davis ifc_simple_destroy(struct if_clone *ifc, struct ifnet *ifp) 675f889d2efSBrooks Davis { 676f889d2efSBrooks Davis int unit; 677f889d2efSBrooks Davis 678f889d2efSBrooks Davis unit = ifp->if_dunit; 679f889d2efSBrooks Davis 68042a58907SGleb Smirnoff if (unit < ifc->ifcs_minifs) 681f889d2efSBrooks Davis return (EINVAL); 682f889d2efSBrooks Davis 68342a58907SGleb Smirnoff ifc->ifcs_destroy(ifp); 684f889d2efSBrooks Davis 685f889d2efSBrooks Davis ifc_free_unit(ifc, unit); 686f889d2efSBrooks Davis 687f889d2efSBrooks Davis return (0); 688f889d2efSBrooks Davis } 689