1c398230bSWarner Losh /*- 2f889d2efSBrooks Davis * Copyright (c) 1980, 1986, 1993 3f889d2efSBrooks Davis * The Regents of the University of California. All rights reserved. 4f889d2efSBrooks Davis * 5f889d2efSBrooks Davis * Redistribution and use in source and binary forms, with or without 6f889d2efSBrooks Davis * modification, are permitted provided that the following conditions 7f889d2efSBrooks Davis * are met: 8f889d2efSBrooks Davis * 1. Redistributions of source code must retain the above copyright 9f889d2efSBrooks Davis * notice, this list of conditions and the following disclaimer. 10f889d2efSBrooks Davis * 2. Redistributions in binary form must reproduce the above copyright 11f889d2efSBrooks Davis * notice, this list of conditions and the following disclaimer in the 12f889d2efSBrooks Davis * documentation and/or other materials provided with the distribution. 13f889d2efSBrooks Davis * 4. Neither the name of the University nor the names of its contributors 14f889d2efSBrooks Davis * may be used to endorse or promote products derived from this software 15f889d2efSBrooks Davis * without specific prior written permission. 16f889d2efSBrooks Davis * 17f889d2efSBrooks Davis * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18f889d2efSBrooks Davis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19f889d2efSBrooks Davis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20f889d2efSBrooks Davis * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21f889d2efSBrooks Davis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22f889d2efSBrooks Davis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23f889d2efSBrooks Davis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24f889d2efSBrooks Davis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25f889d2efSBrooks Davis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26f889d2efSBrooks Davis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27f889d2efSBrooks Davis * SUCH DAMAGE. 28f889d2efSBrooks Davis * 29f889d2efSBrooks Davis * @(#)if.c 8.5 (Berkeley) 1/9/95 30f889d2efSBrooks Davis * $FreeBSD$ 31f889d2efSBrooks Davis */ 32f889d2efSBrooks Davis 33f889d2efSBrooks Davis #include <sys/param.h> 34f889d2efSBrooks Davis #include <sys/malloc.h> 35434dbbb3SRuslan Ermilov #include <sys/limits.h> 36f889d2efSBrooks Davis #include <sys/lock.h> 37f889d2efSBrooks Davis #include <sys/mutex.h> 38f889d2efSBrooks Davis #include <sys/kernel.h> 39f889d2efSBrooks Davis #include <sys/systm.h> 40f889d2efSBrooks Davis #include <sys/types.h> 41f889d2efSBrooks Davis #include <sys/socket.h> 42f889d2efSBrooks Davis 43f889d2efSBrooks Davis #include <net/if.h> 44f889d2efSBrooks Davis #include <net/if_clone.h> 45f889d2efSBrooks Davis #if 0 46f889d2efSBrooks Davis #include <net/if_dl.h> 47f889d2efSBrooks Davis #endif 48f889d2efSBrooks Davis #include <net/if_types.h> 49f889d2efSBrooks Davis #include <net/if_var.h> 50f889d2efSBrooks Davis #include <net/radix.h> 51f889d2efSBrooks Davis #include <net/route.h> 5221ca7b57SMarko Zec #include <net/vnet.h> 53f889d2efSBrooks Davis 54f889d2efSBrooks Davis static void if_clone_free(struct if_clone *ifc); 556b7330e2SSam Leffler static int if_clone_createif(struct if_clone *ifc, char *name, size_t len, 566b7330e2SSam Leffler caddr_t params); 57f889d2efSBrooks Davis 58f889d2efSBrooks Davis static struct mtx if_cloners_mtx; 59eddfbb76SRobert Watson static VNET_DEFINE(int, if_cloners_count); 60eddfbb76SRobert Watson VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners); 61eddfbb76SRobert Watson 621e77c105SRobert Watson #define V_if_cloners_count VNET(if_cloners_count) 631e77c105SRobert Watson #define V_if_cloners VNET(if_cloners) 64f889d2efSBrooks Davis 65f889d2efSBrooks Davis #define IF_CLONERS_LOCK_INIT() \ 66f889d2efSBrooks Davis mtx_init(&if_cloners_mtx, "if_cloners lock", NULL, MTX_DEF) 67f889d2efSBrooks Davis #define IF_CLONERS_LOCK_ASSERT() mtx_assert(&if_cloners_mtx, MA_OWNED) 68f889d2efSBrooks Davis #define IF_CLONERS_LOCK() mtx_lock(&if_cloners_mtx) 69f889d2efSBrooks Davis #define IF_CLONERS_UNLOCK() mtx_unlock(&if_cloners_mtx) 70f889d2efSBrooks Davis 71f889d2efSBrooks Davis #define IF_CLONE_LOCK_INIT(ifc) \ 72f889d2efSBrooks Davis mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF) 73f889d2efSBrooks Davis #define IF_CLONE_LOCK_DESTROY(ifc) mtx_destroy(&(ifc)->ifc_mtx) 74f889d2efSBrooks Davis #define IF_CLONE_LOCK_ASSERT(ifc) mtx_assert(&(ifc)->ifc_mtx, MA_OWNED) 75f889d2efSBrooks Davis #define IF_CLONE_LOCK(ifc) mtx_lock(&(ifc)->ifc_mtx) 76f889d2efSBrooks Davis #define IF_CLONE_UNLOCK(ifc) mtx_unlock(&(ifc)->ifc_mtx) 77f889d2efSBrooks Davis 78f889d2efSBrooks Davis #define IF_CLONE_ADDREF(ifc) \ 79f889d2efSBrooks Davis do { \ 80f889d2efSBrooks Davis IF_CLONE_LOCK(ifc); \ 81f889d2efSBrooks Davis IF_CLONE_ADDREF_LOCKED(ifc); \ 82f889d2efSBrooks Davis IF_CLONE_UNLOCK(ifc); \ 83f889d2efSBrooks Davis } while (0) 84f889d2efSBrooks Davis #define IF_CLONE_ADDREF_LOCKED(ifc) \ 85f889d2efSBrooks Davis do { \ 86f889d2efSBrooks Davis IF_CLONE_LOCK_ASSERT(ifc); \ 87f889d2efSBrooks Davis KASSERT((ifc)->ifc_refcnt >= 0, \ 88f889d2efSBrooks Davis ("negative refcnt %ld", (ifc)->ifc_refcnt)); \ 89f889d2efSBrooks Davis (ifc)->ifc_refcnt++; \ 90f889d2efSBrooks Davis } while (0) 91f889d2efSBrooks Davis #define IF_CLONE_REMREF(ifc) \ 92f889d2efSBrooks Davis do { \ 93f889d2efSBrooks Davis IF_CLONE_LOCK(ifc); \ 94f889d2efSBrooks Davis IF_CLONE_REMREF_LOCKED(ifc); \ 95f889d2efSBrooks Davis } while (0) 96f889d2efSBrooks Davis #define IF_CLONE_REMREF_LOCKED(ifc) \ 97f889d2efSBrooks Davis do { \ 98f889d2efSBrooks Davis IF_CLONE_LOCK_ASSERT(ifc); \ 99f889d2efSBrooks Davis KASSERT((ifc)->ifc_refcnt > 0, \ 100f889d2efSBrooks Davis ("bogus refcnt %ld", (ifc)->ifc_refcnt)); \ 101f889d2efSBrooks Davis if (--(ifc)->ifc_refcnt == 0) { \ 102f889d2efSBrooks Davis IF_CLONE_UNLOCK(ifc); \ 103f889d2efSBrooks Davis if_clone_free(ifc); \ 104ca64c799SMax Laier } else { \ 105f889d2efSBrooks Davis /* silently free the lock */ \ 106f889d2efSBrooks Davis IF_CLONE_UNLOCK(ifc); \ 107ca64c799SMax Laier } \ 108f889d2efSBrooks Davis } while (0) 109f889d2efSBrooks Davis 1104e7e0183SAndrew Thompson #define IFC_IFLIST_INSERT(_ifc, _ifp) \ 1114e7e0183SAndrew Thompson LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones) 1124e7e0183SAndrew Thompson #define IFC_IFLIST_REMOVE(_ifc, _ifp) \ 1134e7e0183SAndrew Thompson LIST_REMOVE(_ifp, if_clones) 1144e7e0183SAndrew Thompson 115c711aea6SPoul-Henning Kamp static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework"); 116f889d2efSBrooks Davis 117d0728d71SRobert Watson void 118d0728d71SRobert Watson vnet_if_clone_init(void) 11937f17770SMarko Zec { 12037f17770SMarko Zec 12137f17770SMarko Zec LIST_INIT(&V_if_cloners); 12237f17770SMarko Zec } 12337f17770SMarko Zec 124f889d2efSBrooks Davis void 125f889d2efSBrooks Davis if_clone_init(void) 126f889d2efSBrooks Davis { 12737f17770SMarko Zec 128f889d2efSBrooks Davis IF_CLONERS_LOCK_INIT(); 129f889d2efSBrooks Davis } 130f889d2efSBrooks Davis 131f889d2efSBrooks Davis /* 1324e7e0183SAndrew Thompson * Lookup and create a clone network interface. 133f889d2efSBrooks Davis */ 134f889d2efSBrooks Davis int 1356b7330e2SSam Leffler if_clone_create(char *name, size_t len, caddr_t params) 136f889d2efSBrooks Davis { 137f889d2efSBrooks Davis struct if_clone *ifc; 138f889d2efSBrooks Davis 139f889d2efSBrooks Davis /* Try to find an applicable cloner for this request */ 140f889d2efSBrooks Davis IF_CLONERS_LOCK(); 14137f17770SMarko Zec LIST_FOREACH(ifc, &V_if_cloners, ifc_list) { 142f889d2efSBrooks Davis if (ifc->ifc_match(ifc, name)) { 143f889d2efSBrooks Davis break; 144f889d2efSBrooks Davis } 145f889d2efSBrooks Davis } 14637f17770SMarko Zec #ifdef VIMAGE 14737f17770SMarko Zec if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) { 14837f17770SMarko Zec CURVNET_SET_QUIET(vnet0); 14937f17770SMarko Zec LIST_FOREACH(ifc, &V_if_cloners, ifc_list) { 15037f17770SMarko Zec if (ifc->ifc_match(ifc, name)) 15137f17770SMarko Zec break; 15237f17770SMarko Zec } 15337f17770SMarko Zec CURVNET_RESTORE(); 15437f17770SMarko Zec } 15537f17770SMarko Zec #endif 156f889d2efSBrooks Davis IF_CLONERS_UNLOCK(); 157f889d2efSBrooks Davis 158f889d2efSBrooks Davis if (ifc == NULL) 159f889d2efSBrooks Davis return (EINVAL); 160f889d2efSBrooks Davis 1616b7330e2SSam Leffler return (if_clone_createif(ifc, name, len, params)); 1624e7e0183SAndrew Thompson } 1634e7e0183SAndrew Thompson 1644e7e0183SAndrew Thompson /* 1654e7e0183SAndrew Thompson * Create a clone network interface. 1664e7e0183SAndrew Thompson */ 1674e7e0183SAndrew Thompson static int 1686b7330e2SSam Leffler if_clone_createif(struct if_clone *ifc, char *name, size_t len, caddr_t params) 1694e7e0183SAndrew Thompson { 1704e7e0183SAndrew Thompson int err; 1714e7e0183SAndrew Thompson struct ifnet *ifp; 1724e7e0183SAndrew Thompson 1734e7e0183SAndrew Thompson if (ifunit(name) != NULL) 1744e7e0183SAndrew Thompson return (EEXIST); 1754e7e0183SAndrew Thompson 1766b7330e2SSam Leffler err = (*ifc->ifc_create)(ifc, name, len, params); 1774e7e0183SAndrew Thompson 1784e7e0183SAndrew Thompson if (!err) { 1794e7e0183SAndrew Thompson ifp = ifunit(name); 1804e7e0183SAndrew Thompson if (ifp == NULL) 1814e7e0183SAndrew Thompson panic("%s: lookup failed for %s", __func__, name); 1824e7e0183SAndrew Thompson 1830dad3f0eSMax Laier if_addgroup(ifp, ifc->ifc_name); 1840dad3f0eSMax Laier 1854e7e0183SAndrew Thompson IF_CLONE_LOCK(ifc); 1864e7e0183SAndrew Thompson IFC_IFLIST_INSERT(ifc, ifp); 1874e7e0183SAndrew Thompson IF_CLONE_UNLOCK(ifc); 1884e7e0183SAndrew Thompson } 1894e7e0183SAndrew Thompson 190f889d2efSBrooks Davis return (err); 191f889d2efSBrooks Davis } 192f889d2efSBrooks Davis 193f889d2efSBrooks Davis /* 1944e7e0183SAndrew Thompson * Lookup and destroy a clone network interface. 195f889d2efSBrooks Davis */ 196f889d2efSBrooks Davis int 197f889d2efSBrooks Davis if_clone_destroy(const char *name) 198f889d2efSBrooks Davis { 199f889d2efSBrooks Davis struct if_clone *ifc; 200f889d2efSBrooks Davis struct ifnet *ifp; 201f889d2efSBrooks Davis 202f889d2efSBrooks Davis ifp = ifunit(name); 203f889d2efSBrooks Davis if (ifp == NULL) 204f889d2efSBrooks Davis return (ENXIO); 205f889d2efSBrooks Davis 206f889d2efSBrooks Davis /* Find the cloner for this interface */ 207f889d2efSBrooks Davis IF_CLONERS_LOCK(); 20837f17770SMarko Zec LIST_FOREACH(ifc, &V_if_cloners, ifc_list) { 209f889d2efSBrooks Davis if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) { 210f889d2efSBrooks Davis break; 211f889d2efSBrooks Davis } 212f889d2efSBrooks Davis } 21337f17770SMarko Zec #ifdef VIMAGE 21437f17770SMarko Zec if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) { 21537f17770SMarko Zec CURVNET_SET_QUIET(vnet0); 21637f17770SMarko Zec LIST_FOREACH(ifc, &V_if_cloners, ifc_list) { 21737f17770SMarko Zec if (ifc->ifc_match(ifc, name)) 21837f17770SMarko Zec break; 21937f17770SMarko Zec } 22037f17770SMarko Zec CURVNET_RESTORE(); 22137f17770SMarko Zec } 22237f17770SMarko Zec #endif 223f889d2efSBrooks Davis IF_CLONERS_UNLOCK(); 224f889d2efSBrooks Davis if (ifc == NULL) 225f889d2efSBrooks Davis return (EINVAL); 226f889d2efSBrooks Davis 2274e7e0183SAndrew Thompson return (if_clone_destroyif(ifc, ifp)); 2284e7e0183SAndrew Thompson } 2294e7e0183SAndrew Thompson 2304e7e0183SAndrew Thompson /* 2314e7e0183SAndrew Thompson * Destroy a clone network interface. 2324e7e0183SAndrew Thompson */ 233cb959fa2SAndrew Thompson int 2344e7e0183SAndrew Thompson if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp) 2354e7e0183SAndrew Thompson { 2364e7e0183SAndrew Thompson int err; 237c769e1beSBjoern A. Zeeb struct ifnet *ifcifp; 2384e7e0183SAndrew Thompson 23921ca7b57SMarko Zec if (ifc->ifc_destroy == NULL) 24021ca7b57SMarko Zec return(EOPNOTSUPP); 241f889d2efSBrooks Davis 24237f17770SMarko Zec /* 24337f17770SMarko Zec * Given that the cloned ifnet might be attached to a different 24437f17770SMarko Zec * vnet from where its cloner was registered, we have to 24537f17770SMarko Zec * switch to the vnet context of the target vnet. 24637f17770SMarko Zec */ 24737f17770SMarko Zec CURVNET_SET_QUIET(ifp->if_vnet); 24837f17770SMarko Zec 249434dbbb3SRuslan Ermilov IF_CLONE_LOCK(ifc); 250c769e1beSBjoern A. Zeeb LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) { 251c769e1beSBjoern A. Zeeb if (ifcifp == ifp) { 252434dbbb3SRuslan Ermilov IFC_IFLIST_REMOVE(ifc, ifp); 253c769e1beSBjoern A. Zeeb break; 254c769e1beSBjoern A. Zeeb } 255c769e1beSBjoern A. Zeeb } 256434dbbb3SRuslan Ermilov IF_CLONE_UNLOCK(ifc); 257c769e1beSBjoern A. Zeeb if (ifcifp == NULL) { 258c769e1beSBjoern A. Zeeb CURVNET_RESTORE(); 259c769e1beSBjoern A. Zeeb return (ENXIO); /* ifp is not on the list. */ 260c769e1beSBjoern A. Zeeb } 261434dbbb3SRuslan Ermilov 2620dad3f0eSMax Laier if_delgroup(ifp, ifc->ifc_name); 2630dad3f0eSMax Laier 264f889d2efSBrooks Davis err = (*ifc->ifc_destroy)(ifc, ifp); 265f889d2efSBrooks Davis 266434dbbb3SRuslan Ermilov if (err != 0) { 2670dad3f0eSMax Laier if_addgroup(ifp, ifc->ifc_name); 2680dad3f0eSMax Laier 269434dbbb3SRuslan Ermilov IF_CLONE_LOCK(ifc); 270434dbbb3SRuslan Ermilov IFC_IFLIST_INSERT(ifc, ifp); 271434dbbb3SRuslan Ermilov IF_CLONE_UNLOCK(ifc); 272434dbbb3SRuslan Ermilov } 27321ca7b57SMarko Zec CURVNET_RESTORE(); 274f889d2efSBrooks Davis return (err); 275f889d2efSBrooks Davis } 276f889d2efSBrooks Davis 277f889d2efSBrooks Davis /* 278f889d2efSBrooks Davis * Register a network interface cloner. 279f889d2efSBrooks Davis */ 280f889d2efSBrooks Davis void 281f889d2efSBrooks Davis if_clone_attach(struct if_clone *ifc) 282f889d2efSBrooks Davis { 283f889d2efSBrooks Davis int len, maxclone; 284f889d2efSBrooks Davis 285f889d2efSBrooks Davis /* 286f889d2efSBrooks Davis * Compute bitmap size and allocate it. 287f889d2efSBrooks Davis */ 288f889d2efSBrooks Davis maxclone = ifc->ifc_maxunit + 1; 289f889d2efSBrooks Davis len = maxclone >> 3; 290f889d2efSBrooks Davis if ((len << 3) < maxclone) 291f889d2efSBrooks Davis len++; 292f889d2efSBrooks Davis ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO); 293f889d2efSBrooks Davis ifc->ifc_bmlen = len; 294f889d2efSBrooks Davis IF_CLONE_LOCK_INIT(ifc); 295f889d2efSBrooks Davis IF_CLONE_ADDREF(ifc); 296f889d2efSBrooks Davis 297f889d2efSBrooks Davis IF_CLONERS_LOCK(); 29837f17770SMarko Zec LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list); 29937f17770SMarko Zec V_if_cloners_count++; 300f889d2efSBrooks Davis IF_CLONERS_UNLOCK(); 301f889d2efSBrooks Davis 3024e7e0183SAndrew Thompson LIST_INIT(&ifc->ifc_iflist); 3034e7e0183SAndrew Thompson 304f889d2efSBrooks Davis if (ifc->ifc_attach != NULL) 305f889d2efSBrooks Davis (*ifc->ifc_attach)(ifc); 306f889d2efSBrooks Davis EVENTHANDLER_INVOKE(if_clone_event, ifc); 307f889d2efSBrooks Davis } 308f889d2efSBrooks Davis 309f889d2efSBrooks Davis /* 310f889d2efSBrooks Davis * Unregister a network interface cloner. 311f889d2efSBrooks Davis */ 312f889d2efSBrooks Davis void 313f889d2efSBrooks Davis if_clone_detach(struct if_clone *ifc) 314f889d2efSBrooks Davis { 315febd0759SAndrew Thompson struct ifc_simple_data *ifcs = ifc->ifc_data; 316f889d2efSBrooks Davis 317f889d2efSBrooks Davis IF_CLONERS_LOCK(); 318f889d2efSBrooks Davis LIST_REMOVE(ifc, ifc_list); 31937f17770SMarko Zec V_if_cloners_count--; 320f889d2efSBrooks Davis IF_CLONERS_UNLOCK(); 321f889d2efSBrooks Davis 322febd0759SAndrew Thompson /* Allow all simples to be destroyed */ 323febd0759SAndrew Thompson if (ifc->ifc_attach == ifc_simple_attach) 324febd0759SAndrew Thompson ifcs->ifcs_minifs = 0; 325febd0759SAndrew Thompson 3264e7e0183SAndrew Thompson /* destroy all interfaces for this cloner */ 3274e7e0183SAndrew Thompson while (!LIST_EMPTY(&ifc->ifc_iflist)) 3284e7e0183SAndrew Thompson if_clone_destroyif(ifc, LIST_FIRST(&ifc->ifc_iflist)); 3294e7e0183SAndrew Thompson 330f889d2efSBrooks Davis IF_CLONE_REMREF(ifc); 331f889d2efSBrooks Davis } 332f889d2efSBrooks Davis 333f889d2efSBrooks Davis static void 334f889d2efSBrooks Davis if_clone_free(struct if_clone *ifc) 335f889d2efSBrooks Davis { 336febd0759SAndrew Thompson for (int bytoff = 0; bytoff < ifc->ifc_bmlen; bytoff++) { 337febd0759SAndrew Thompson KASSERT(ifc->ifc_units[bytoff] == 0x00, 338febd0759SAndrew Thompson ("ifc_units[%d] is not empty", bytoff)); 339febd0759SAndrew Thompson } 340f889d2efSBrooks Davis 3414e7e0183SAndrew Thompson KASSERT(LIST_EMPTY(&ifc->ifc_iflist), 3424e7e0183SAndrew Thompson ("%s: ifc_iflist not empty", __func__)); 3434e7e0183SAndrew Thompson 344f889d2efSBrooks Davis IF_CLONE_LOCK_DESTROY(ifc); 345f889d2efSBrooks Davis free(ifc->ifc_units, M_CLONE); 346f889d2efSBrooks Davis } 347f889d2efSBrooks Davis 348f889d2efSBrooks Davis /* 349f889d2efSBrooks Davis * Provide list of interface cloners to userspace. 350f889d2efSBrooks Davis */ 351f889d2efSBrooks Davis int 352f889d2efSBrooks Davis if_clone_list(struct if_clonereq *ifcr) 353f889d2efSBrooks Davis { 354c859ef97SBrooks Davis char *buf, *dst, *outbuf = NULL; 355f889d2efSBrooks Davis struct if_clone *ifc; 356c859ef97SBrooks Davis int buf_count, count, err = 0; 357c859ef97SBrooks Davis 358a6d00835SMaxim Konovalov if (ifcr->ifcr_count < 0) 359a6d00835SMaxim Konovalov return (EINVAL); 360a6d00835SMaxim Konovalov 361c859ef97SBrooks Davis IF_CLONERS_LOCK(); 362c859ef97SBrooks Davis /* 363c859ef97SBrooks Davis * Set our internal output buffer size. We could end up not 364c859ef97SBrooks Davis * reporting a cloner that is added between the unlock and lock 365c859ef97SBrooks Davis * below, but that's not a major problem. Not caping our 366c859ef97SBrooks Davis * allocation to the number of cloners actually in the system 367c859ef97SBrooks Davis * could be because that would let arbitrary users cause us to 368c859ef97SBrooks Davis * allocate abritrary amounts of kernel memory. 369c859ef97SBrooks Davis */ 37037f17770SMarko Zec buf_count = (V_if_cloners_count < ifcr->ifcr_count) ? 37137f17770SMarko Zec V_if_cloners_count : ifcr->ifcr_count; 372c859ef97SBrooks Davis IF_CLONERS_UNLOCK(); 373c859ef97SBrooks Davis 374c859ef97SBrooks Davis outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO); 375f889d2efSBrooks Davis 376f889d2efSBrooks Davis IF_CLONERS_LOCK(); 377f889d2efSBrooks Davis 37837f17770SMarko Zec ifcr->ifcr_total = V_if_cloners_count; 379f889d2efSBrooks Davis if ((dst = ifcr->ifcr_buffer) == NULL) { 380f889d2efSBrooks Davis /* Just asking how many there are. */ 381f889d2efSBrooks Davis goto done; 382f889d2efSBrooks Davis } 38337f17770SMarko Zec count = (V_if_cloners_count < buf_count) ? 38437f17770SMarko Zec V_if_cloners_count : buf_count; 385f889d2efSBrooks Davis 38637f17770SMarko Zec for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf; 387c859ef97SBrooks Davis ifc != NULL && count != 0; 388c859ef97SBrooks Davis ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) { 389c859ef97SBrooks Davis strlcpy(buf, ifc->ifc_name, IFNAMSIZ); 390f889d2efSBrooks Davis } 391f889d2efSBrooks Davis 392f889d2efSBrooks Davis done: 393f889d2efSBrooks Davis IF_CLONERS_UNLOCK(); 394c859ef97SBrooks Davis if (err == 0) 395c859ef97SBrooks Davis err = copyout(outbuf, dst, buf_count*IFNAMSIZ); 396c859ef97SBrooks Davis if (outbuf != NULL) 397c859ef97SBrooks Davis free(outbuf, M_CLONE); 398f889d2efSBrooks Davis return (err); 399f889d2efSBrooks Davis } 400f889d2efSBrooks Davis 401f889d2efSBrooks Davis /* 402f889d2efSBrooks Davis * A utility function to extract unit numbers from interface names of 403f889d2efSBrooks Davis * the form name###. 404f889d2efSBrooks Davis * 405f889d2efSBrooks Davis * Returns 0 on success and an error on failure. 406f889d2efSBrooks Davis */ 407f889d2efSBrooks Davis int 408f889d2efSBrooks Davis ifc_name2unit(const char *name, int *unit) 409f889d2efSBrooks Davis { 410f889d2efSBrooks Davis const char *cp; 411434dbbb3SRuslan Ermilov int cutoff = INT_MAX / 10; 412434dbbb3SRuslan Ermilov int cutlim = INT_MAX % 10; 413f889d2efSBrooks Davis 414f889d2efSBrooks Davis for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++); 415f889d2efSBrooks Davis if (*cp == '\0') { 416f889d2efSBrooks Davis *unit = -1; 417434dbbb3SRuslan Ermilov } else if (cp[0] == '0' && cp[1] != '\0') { 418434dbbb3SRuslan Ermilov /* Disallow leading zeroes. */ 419434dbbb3SRuslan Ermilov return (EINVAL); 420f889d2efSBrooks Davis } else { 421f889d2efSBrooks Davis for (*unit = 0; *cp != '\0'; cp++) { 422f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') { 423f889d2efSBrooks Davis /* Bogus unit number. */ 424f889d2efSBrooks Davis return (EINVAL); 425f889d2efSBrooks Davis } 426434dbbb3SRuslan Ermilov if (*unit > cutoff || 427434dbbb3SRuslan Ermilov (*unit == cutoff && *cp - '0' > cutlim)) 428434dbbb3SRuslan Ermilov return (EINVAL); 429f889d2efSBrooks Davis *unit = (*unit * 10) + (*cp - '0'); 430f889d2efSBrooks Davis } 431f889d2efSBrooks Davis } 432f889d2efSBrooks Davis 433f889d2efSBrooks Davis return (0); 434f889d2efSBrooks Davis } 435f889d2efSBrooks Davis 436f889d2efSBrooks Davis int 437f889d2efSBrooks Davis ifc_alloc_unit(struct if_clone *ifc, int *unit) 438f889d2efSBrooks Davis { 439f889d2efSBrooks Davis int wildcard, bytoff, bitoff; 440f889d2efSBrooks Davis int err = 0; 441f889d2efSBrooks Davis 442f889d2efSBrooks Davis IF_CLONE_LOCK(ifc); 443f889d2efSBrooks Davis 444f889d2efSBrooks Davis bytoff = bitoff = 0; 445f889d2efSBrooks Davis wildcard = (*unit < 0); 446f889d2efSBrooks Davis /* 447f889d2efSBrooks Davis * Find a free unit if none was given. 448f889d2efSBrooks Davis */ 449f889d2efSBrooks Davis if (wildcard) { 450f889d2efSBrooks Davis while ((bytoff < ifc->ifc_bmlen) 451f889d2efSBrooks Davis && (ifc->ifc_units[bytoff] == 0xff)) 452f889d2efSBrooks Davis bytoff++; 453f889d2efSBrooks Davis if (bytoff >= ifc->ifc_bmlen) { 454f889d2efSBrooks Davis err = ENOSPC; 455f889d2efSBrooks Davis goto done; 456f889d2efSBrooks Davis } 457f889d2efSBrooks Davis while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) 458f889d2efSBrooks Davis bitoff++; 459f889d2efSBrooks Davis *unit = (bytoff << 3) + bitoff; 460f889d2efSBrooks Davis } 461f889d2efSBrooks Davis 462f889d2efSBrooks Davis if (*unit > ifc->ifc_maxunit) { 463f889d2efSBrooks Davis err = ENOSPC; 464f889d2efSBrooks Davis goto done; 465f889d2efSBrooks Davis } 466f889d2efSBrooks Davis 467f889d2efSBrooks Davis if (!wildcard) { 468f889d2efSBrooks Davis bytoff = *unit >> 3; 469f889d2efSBrooks Davis bitoff = *unit - (bytoff << 3); 470f889d2efSBrooks Davis } 471f889d2efSBrooks Davis 472f889d2efSBrooks Davis if((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) { 473f889d2efSBrooks Davis err = EEXIST; 474f889d2efSBrooks Davis goto done; 475f889d2efSBrooks Davis } 476f889d2efSBrooks Davis /* 477f889d2efSBrooks Davis * Allocate the unit in the bitmap. 478f889d2efSBrooks Davis */ 479febd0759SAndrew Thompson KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0, 480febd0759SAndrew Thompson ("%s: bit is already set", __func__)); 481f889d2efSBrooks Davis ifc->ifc_units[bytoff] |= (1 << bitoff); 482febd0759SAndrew Thompson IF_CLONE_ADDREF_LOCKED(ifc); 483f889d2efSBrooks Davis 484f889d2efSBrooks Davis done: 485f889d2efSBrooks Davis IF_CLONE_UNLOCK(ifc); 486f889d2efSBrooks Davis return (err); 487f889d2efSBrooks Davis } 488f889d2efSBrooks Davis 489f889d2efSBrooks Davis void 490f889d2efSBrooks Davis ifc_free_unit(struct if_clone *ifc, int unit) 491f889d2efSBrooks Davis { 492f889d2efSBrooks Davis int bytoff, bitoff; 493f889d2efSBrooks Davis 494f889d2efSBrooks Davis 495f889d2efSBrooks Davis /* 496f889d2efSBrooks Davis * Compute offset in the bitmap and deallocate the unit. 497f889d2efSBrooks Davis */ 498f889d2efSBrooks Davis bytoff = unit >> 3; 499f889d2efSBrooks Davis bitoff = unit - (bytoff << 3); 500f889d2efSBrooks Davis 501f889d2efSBrooks Davis IF_CLONE_LOCK(ifc); 502f889d2efSBrooks Davis KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0, 503f889d2efSBrooks Davis ("%s: bit is already cleared", __func__)); 504f889d2efSBrooks Davis ifc->ifc_units[bytoff] &= ~(1 << bitoff); 505febd0759SAndrew Thompson IF_CLONE_REMREF_LOCKED(ifc); /* releases lock */ 506f889d2efSBrooks Davis } 507f889d2efSBrooks Davis 508f889d2efSBrooks Davis void 509f889d2efSBrooks Davis ifc_simple_attach(struct if_clone *ifc) 510f889d2efSBrooks Davis { 511f889d2efSBrooks Davis int err; 512f889d2efSBrooks Davis int unit; 513f889d2efSBrooks Davis char name[IFNAMSIZ]; 514f889d2efSBrooks Davis struct ifc_simple_data *ifcs = ifc->ifc_data; 515f889d2efSBrooks Davis 516f889d2efSBrooks Davis KASSERT(ifcs->ifcs_minifs - 1 <= ifc->ifc_maxunit, 517434dbbb3SRuslan Ermilov ("%s: %s requested more units than allowed (%d > %d)", 518f889d2efSBrooks Davis __func__, ifc->ifc_name, ifcs->ifcs_minifs, 519f889d2efSBrooks Davis ifc->ifc_maxunit + 1)); 520f889d2efSBrooks Davis 521f889d2efSBrooks Davis for (unit = 0; unit < ifcs->ifcs_minifs; unit++) { 522f889d2efSBrooks Davis snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit); 5236b7330e2SSam Leffler err = if_clone_createif(ifc, name, IFNAMSIZ, NULL); 524f889d2efSBrooks Davis KASSERT(err == 0, 525f889d2efSBrooks Davis ("%s: failed to create required interface %s", 526f889d2efSBrooks Davis __func__, name)); 527f889d2efSBrooks Davis } 528f889d2efSBrooks Davis } 529f889d2efSBrooks Davis 530f889d2efSBrooks Davis int 531f889d2efSBrooks Davis ifc_simple_match(struct if_clone *ifc, const char *name) 532f889d2efSBrooks Davis { 533f889d2efSBrooks Davis const char *cp; 534f889d2efSBrooks Davis int i; 535f889d2efSBrooks Davis 536f889d2efSBrooks Davis /* Match the name */ 537f889d2efSBrooks Davis for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) { 538f889d2efSBrooks Davis if (ifc->ifc_name[i] != *cp) 539f889d2efSBrooks Davis return (0); 540f889d2efSBrooks Davis } 541f889d2efSBrooks Davis 542f889d2efSBrooks Davis /* Make sure there's a unit number or nothing after the name */ 543f889d2efSBrooks Davis for (; *cp != '\0'; cp++) { 544f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') 545f889d2efSBrooks Davis return (0); 546f889d2efSBrooks Davis } 547f889d2efSBrooks Davis 548f889d2efSBrooks Davis return (1); 549f889d2efSBrooks Davis } 550f889d2efSBrooks Davis 551f889d2efSBrooks Davis int 5526b7330e2SSam Leffler ifc_simple_create(struct if_clone *ifc, char *name, size_t len, caddr_t params) 553f889d2efSBrooks Davis { 554f889d2efSBrooks Davis char *dp; 555f889d2efSBrooks Davis int wildcard; 556f889d2efSBrooks Davis int unit; 557f889d2efSBrooks Davis int err; 558f889d2efSBrooks Davis struct ifc_simple_data *ifcs = ifc->ifc_data; 559f889d2efSBrooks Davis 560f889d2efSBrooks Davis err = ifc_name2unit(name, &unit); 561f889d2efSBrooks Davis if (err != 0) 562f889d2efSBrooks Davis return (err); 563f889d2efSBrooks Davis 564f889d2efSBrooks Davis wildcard = (unit < 0); 565f889d2efSBrooks Davis 566f889d2efSBrooks Davis err = ifc_alloc_unit(ifc, &unit); 567f889d2efSBrooks Davis if (err != 0) 568f889d2efSBrooks Davis return (err); 569f889d2efSBrooks Davis 5706b7330e2SSam Leffler err = ifcs->ifcs_create(ifc, unit, params); 571f889d2efSBrooks Davis if (err != 0) { 572f889d2efSBrooks Davis ifc_free_unit(ifc, unit); 573f889d2efSBrooks Davis return (err); 574f889d2efSBrooks Davis } 575f889d2efSBrooks Davis 576f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */ 577f889d2efSBrooks Davis if (wildcard) { 578f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++); 579f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) > 580f889d2efSBrooks Davis len - (dp-name) - 1) { 581f889d2efSBrooks Davis /* 582f889d2efSBrooks Davis * This can only be a programmer error and 583f889d2efSBrooks Davis * there's no straightforward way to recover if 584f889d2efSBrooks Davis * it happens. 585f889d2efSBrooks Davis */ 586f889d2efSBrooks Davis panic("if_clone_create(): interface name too long"); 587f889d2efSBrooks Davis } 588f889d2efSBrooks Davis 589f889d2efSBrooks Davis } 590f889d2efSBrooks Davis 591f889d2efSBrooks Davis return (0); 592f889d2efSBrooks Davis } 593f889d2efSBrooks Davis 594f889d2efSBrooks Davis int 595f889d2efSBrooks Davis ifc_simple_destroy(struct if_clone *ifc, struct ifnet *ifp) 596f889d2efSBrooks Davis { 597f889d2efSBrooks Davis int unit; 598f889d2efSBrooks Davis struct ifc_simple_data *ifcs = ifc->ifc_data; 599f889d2efSBrooks Davis 600f889d2efSBrooks Davis unit = ifp->if_dunit; 601f889d2efSBrooks Davis 602f889d2efSBrooks Davis if (unit < ifcs->ifcs_minifs) 603f889d2efSBrooks Davis return (EINVAL); 604f889d2efSBrooks Davis 605f889d2efSBrooks Davis ifcs->ifcs_destroy(ifp); 606f889d2efSBrooks Davis 607f889d2efSBrooks Davis ifc_free_unit(ifc, unit); 608f889d2efSBrooks Davis 609f889d2efSBrooks Davis return (0); 610f889d2efSBrooks Davis } 611