xref: /freebsd/sys/net/if_clone.c (revision d0088cde620a442171ce4b16fd6e161a77ae0feb)
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 {
199d0088cdeSBjoern A. Zeeb 	int err;
200f889d2efSBrooks Davis 	struct if_clone *ifc;
201f889d2efSBrooks Davis 	struct ifnet *ifp;
202f889d2efSBrooks Davis 
203d0088cdeSBjoern A. Zeeb 	ifp = ifunit_ref(name);
204f889d2efSBrooks Davis 	if (ifp == NULL)
205f889d2efSBrooks Davis 		return (ENXIO);
206f889d2efSBrooks Davis 
207f889d2efSBrooks Davis 	/* Find the cloner for this interface */
208f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
20937f17770SMarko Zec 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
210f889d2efSBrooks Davis 		if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) {
211f889d2efSBrooks Davis 			break;
212f889d2efSBrooks Davis 		}
213f889d2efSBrooks Davis 	}
21437f17770SMarko Zec #ifdef VIMAGE
21537f17770SMarko Zec 	if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
21637f17770SMarko Zec 		CURVNET_SET_QUIET(vnet0);
21737f17770SMarko Zec 		LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
21837f17770SMarko Zec 			if (ifc->ifc_match(ifc, name))
21937f17770SMarko Zec 				break;
22037f17770SMarko Zec 		}
22137f17770SMarko Zec 		CURVNET_RESTORE();
22237f17770SMarko Zec 	}
22337f17770SMarko Zec #endif
224f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
225d0088cdeSBjoern A. Zeeb 	if (ifc == NULL) {
226d0088cdeSBjoern A. Zeeb 		if_rele(ifp);
227f889d2efSBrooks Davis 		return (EINVAL);
228d0088cdeSBjoern A. Zeeb 	}
229f889d2efSBrooks Davis 
230d0088cdeSBjoern A. Zeeb 	err = if_clone_destroyif(ifc, ifp);
231d0088cdeSBjoern A. Zeeb 	if_rele(ifp);
232d0088cdeSBjoern A. Zeeb 	return err;
2334e7e0183SAndrew Thompson }
2344e7e0183SAndrew Thompson 
2354e7e0183SAndrew Thompson /*
2364e7e0183SAndrew Thompson  * Destroy a clone network interface.
2374e7e0183SAndrew Thompson  */
238cb959fa2SAndrew Thompson int
2394e7e0183SAndrew Thompson if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
2404e7e0183SAndrew Thompson {
2414e7e0183SAndrew Thompson 	int err;
242c769e1beSBjoern A. Zeeb 	struct ifnet *ifcifp;
2434e7e0183SAndrew Thompson 
24421ca7b57SMarko Zec 	if (ifc->ifc_destroy == NULL)
24521ca7b57SMarko Zec 		return(EOPNOTSUPP);
246f889d2efSBrooks Davis 
24737f17770SMarko Zec 	/*
24837f17770SMarko Zec 	 * Given that the cloned ifnet might be attached to a different
24937f17770SMarko Zec 	 * vnet from where its cloner was registered, we have to
25037f17770SMarko Zec 	 * switch to the vnet context of the target vnet.
25137f17770SMarko Zec 	 */
25237f17770SMarko Zec 	CURVNET_SET_QUIET(ifp->if_vnet);
25337f17770SMarko Zec 
254434dbbb3SRuslan Ermilov 	IF_CLONE_LOCK(ifc);
255c769e1beSBjoern A. Zeeb 	LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
256c769e1beSBjoern A. Zeeb 		if (ifcifp == ifp) {
257434dbbb3SRuslan Ermilov 			IFC_IFLIST_REMOVE(ifc, ifp);
258c769e1beSBjoern A. Zeeb 			break;
259c769e1beSBjoern A. Zeeb 		}
260c769e1beSBjoern A. Zeeb 	}
261434dbbb3SRuslan Ermilov 	IF_CLONE_UNLOCK(ifc);
262c769e1beSBjoern A. Zeeb 	if (ifcifp == NULL) {
263c769e1beSBjoern A. Zeeb 		CURVNET_RESTORE();
264c769e1beSBjoern A. Zeeb 		return (ENXIO);		/* ifp is not on the list. */
265c769e1beSBjoern A. Zeeb 	}
266434dbbb3SRuslan Ermilov 
2670dad3f0eSMax Laier 	if_delgroup(ifp, ifc->ifc_name);
2680dad3f0eSMax Laier 
269f889d2efSBrooks Davis 	err =  (*ifc->ifc_destroy)(ifc, ifp);
270f889d2efSBrooks Davis 
271434dbbb3SRuslan Ermilov 	if (err != 0) {
2720dad3f0eSMax Laier 		if_addgroup(ifp, ifc->ifc_name);
2730dad3f0eSMax Laier 
274434dbbb3SRuslan Ermilov 		IF_CLONE_LOCK(ifc);
275434dbbb3SRuslan Ermilov 		IFC_IFLIST_INSERT(ifc, ifp);
276434dbbb3SRuslan Ermilov 		IF_CLONE_UNLOCK(ifc);
277434dbbb3SRuslan Ermilov 	}
27821ca7b57SMarko Zec 	CURVNET_RESTORE();
279f889d2efSBrooks Davis 	return (err);
280f889d2efSBrooks Davis }
281f889d2efSBrooks Davis 
282f889d2efSBrooks Davis /*
283f889d2efSBrooks Davis  * Register a network interface cloner.
284f889d2efSBrooks Davis  */
285f889d2efSBrooks Davis void
286f889d2efSBrooks Davis if_clone_attach(struct if_clone *ifc)
287f889d2efSBrooks Davis {
288f889d2efSBrooks Davis 	int len, maxclone;
289f889d2efSBrooks Davis 
290f889d2efSBrooks Davis 	/*
291f889d2efSBrooks Davis 	 * Compute bitmap size and allocate it.
292f889d2efSBrooks Davis 	 */
293f889d2efSBrooks Davis 	maxclone = ifc->ifc_maxunit + 1;
294f889d2efSBrooks Davis 	len = maxclone >> 3;
295f889d2efSBrooks Davis 	if ((len << 3) < maxclone)
296f889d2efSBrooks Davis 		len++;
297f889d2efSBrooks Davis 	ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
298f889d2efSBrooks Davis 	ifc->ifc_bmlen = len;
299f889d2efSBrooks Davis 	IF_CLONE_LOCK_INIT(ifc);
300f889d2efSBrooks Davis 	IF_CLONE_ADDREF(ifc);
301f889d2efSBrooks Davis 
302f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
30337f17770SMarko Zec 	LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
30437f17770SMarko Zec 	V_if_cloners_count++;
305f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
306f889d2efSBrooks Davis 
3074e7e0183SAndrew Thompson 	LIST_INIT(&ifc->ifc_iflist);
3084e7e0183SAndrew Thompson 
309f889d2efSBrooks Davis 	if (ifc->ifc_attach != NULL)
310f889d2efSBrooks Davis 		(*ifc->ifc_attach)(ifc);
311f889d2efSBrooks Davis 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
312f889d2efSBrooks Davis }
313f889d2efSBrooks Davis 
314f889d2efSBrooks Davis /*
315f889d2efSBrooks Davis  * Unregister a network interface cloner.
316f889d2efSBrooks Davis  */
317f889d2efSBrooks Davis void
318f889d2efSBrooks Davis if_clone_detach(struct if_clone *ifc)
319f889d2efSBrooks Davis {
320febd0759SAndrew Thompson 	struct ifc_simple_data *ifcs = ifc->ifc_data;
321f889d2efSBrooks Davis 
322f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
323f889d2efSBrooks Davis 	LIST_REMOVE(ifc, ifc_list);
32437f17770SMarko Zec 	V_if_cloners_count--;
325f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
326f889d2efSBrooks Davis 
327febd0759SAndrew Thompson 	/* Allow all simples to be destroyed */
328febd0759SAndrew Thompson 	if (ifc->ifc_attach == ifc_simple_attach)
329febd0759SAndrew Thompson 		ifcs->ifcs_minifs = 0;
330febd0759SAndrew Thompson 
3314e7e0183SAndrew Thompson 	/* destroy all interfaces for this cloner */
3324e7e0183SAndrew Thompson 	while (!LIST_EMPTY(&ifc->ifc_iflist))
3334e7e0183SAndrew Thompson 		if_clone_destroyif(ifc, LIST_FIRST(&ifc->ifc_iflist));
3344e7e0183SAndrew Thompson 
335f889d2efSBrooks Davis 	IF_CLONE_REMREF(ifc);
336f889d2efSBrooks Davis }
337f889d2efSBrooks Davis 
338f889d2efSBrooks Davis static void
339f889d2efSBrooks Davis if_clone_free(struct if_clone *ifc)
340f889d2efSBrooks Davis {
341febd0759SAndrew Thompson 	for (int bytoff = 0; bytoff < ifc->ifc_bmlen; bytoff++) {
342febd0759SAndrew Thompson 		KASSERT(ifc->ifc_units[bytoff] == 0x00,
343febd0759SAndrew Thompson 		    ("ifc_units[%d] is not empty", bytoff));
344febd0759SAndrew Thompson 	}
345f889d2efSBrooks Davis 
3464e7e0183SAndrew Thompson 	KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
3474e7e0183SAndrew Thompson 	    ("%s: ifc_iflist not empty", __func__));
3484e7e0183SAndrew Thompson 
349f889d2efSBrooks Davis 	IF_CLONE_LOCK_DESTROY(ifc);
350f889d2efSBrooks Davis 	free(ifc->ifc_units, M_CLONE);
351f889d2efSBrooks Davis }
352f889d2efSBrooks Davis 
353f889d2efSBrooks Davis /*
354f889d2efSBrooks Davis  * Provide list of interface cloners to userspace.
355f889d2efSBrooks Davis  */
356f889d2efSBrooks Davis int
357f889d2efSBrooks Davis if_clone_list(struct if_clonereq *ifcr)
358f889d2efSBrooks Davis {
359c859ef97SBrooks Davis 	char *buf, *dst, *outbuf = NULL;
360f889d2efSBrooks Davis 	struct if_clone *ifc;
361c859ef97SBrooks Davis 	int buf_count, count, err = 0;
362c859ef97SBrooks Davis 
363a6d00835SMaxim Konovalov 	if (ifcr->ifcr_count < 0)
364a6d00835SMaxim Konovalov 		return (EINVAL);
365a6d00835SMaxim Konovalov 
366c859ef97SBrooks Davis 	IF_CLONERS_LOCK();
367c859ef97SBrooks Davis 	/*
368c859ef97SBrooks Davis 	 * Set our internal output buffer size.  We could end up not
369c859ef97SBrooks Davis 	 * reporting a cloner that is added between the unlock and lock
370c859ef97SBrooks Davis 	 * below, but that's not a major problem.  Not caping our
371c859ef97SBrooks Davis 	 * allocation to the number of cloners actually in the system
372c859ef97SBrooks Davis 	 * could be because that would let arbitrary users cause us to
373c859ef97SBrooks Davis 	 * allocate abritrary amounts of kernel memory.
374c859ef97SBrooks Davis 	 */
37537f17770SMarko Zec 	buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
37637f17770SMarko Zec 	    V_if_cloners_count : ifcr->ifcr_count;
377c859ef97SBrooks Davis 	IF_CLONERS_UNLOCK();
378c859ef97SBrooks Davis 
379c859ef97SBrooks Davis 	outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
380f889d2efSBrooks Davis 
381f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
382f889d2efSBrooks Davis 
38337f17770SMarko Zec 	ifcr->ifcr_total = V_if_cloners_count;
384f889d2efSBrooks Davis 	if ((dst = ifcr->ifcr_buffer) == NULL) {
385f889d2efSBrooks Davis 		/* Just asking how many there are. */
386f889d2efSBrooks Davis 		goto done;
387f889d2efSBrooks Davis 	}
38837f17770SMarko Zec 	count = (V_if_cloners_count < buf_count) ?
38937f17770SMarko Zec 	    V_if_cloners_count : buf_count;
390f889d2efSBrooks Davis 
39137f17770SMarko Zec 	for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
392c859ef97SBrooks Davis 	    ifc != NULL && count != 0;
393c859ef97SBrooks Davis 	    ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
394c859ef97SBrooks Davis 		strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
395f889d2efSBrooks Davis 	}
396f889d2efSBrooks Davis 
397f889d2efSBrooks Davis done:
398f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
399c859ef97SBrooks Davis 	if (err == 0)
400c859ef97SBrooks Davis 		err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
401c859ef97SBrooks Davis 	if (outbuf != NULL)
402c859ef97SBrooks Davis 		free(outbuf, M_CLONE);
403f889d2efSBrooks Davis 	return (err);
404f889d2efSBrooks Davis }
405f889d2efSBrooks Davis 
406f889d2efSBrooks Davis /*
407f889d2efSBrooks Davis  * A utility function to extract unit numbers from interface names of
408f889d2efSBrooks Davis  * the form name###.
409f889d2efSBrooks Davis  *
410f889d2efSBrooks Davis  * Returns 0 on success and an error on failure.
411f889d2efSBrooks Davis  */
412f889d2efSBrooks Davis int
413f889d2efSBrooks Davis ifc_name2unit(const char *name, int *unit)
414f889d2efSBrooks Davis {
415f889d2efSBrooks Davis 	const char	*cp;
416434dbbb3SRuslan Ermilov 	int		cutoff = INT_MAX / 10;
417434dbbb3SRuslan Ermilov 	int		cutlim = INT_MAX % 10;
418f889d2efSBrooks Davis 
419f889d2efSBrooks Davis 	for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++);
420f889d2efSBrooks Davis 	if (*cp == '\0') {
421f889d2efSBrooks Davis 		*unit = -1;
422434dbbb3SRuslan Ermilov 	} else if (cp[0] == '0' && cp[1] != '\0') {
423434dbbb3SRuslan Ermilov 		/* Disallow leading zeroes. */
424434dbbb3SRuslan Ermilov 		return (EINVAL);
425f889d2efSBrooks Davis 	} else {
426f889d2efSBrooks Davis 		for (*unit = 0; *cp != '\0'; cp++) {
427f889d2efSBrooks Davis 			if (*cp < '0' || *cp > '9') {
428f889d2efSBrooks Davis 				/* Bogus unit number. */
429f889d2efSBrooks Davis 				return (EINVAL);
430f889d2efSBrooks Davis 			}
431434dbbb3SRuslan Ermilov 			if (*unit > cutoff ||
432434dbbb3SRuslan Ermilov 			    (*unit == cutoff && *cp - '0' > cutlim))
433434dbbb3SRuslan Ermilov 				return (EINVAL);
434f889d2efSBrooks Davis 			*unit = (*unit * 10) + (*cp - '0');
435f889d2efSBrooks Davis 		}
436f889d2efSBrooks Davis 	}
437f889d2efSBrooks Davis 
438f889d2efSBrooks Davis 	return (0);
439f889d2efSBrooks Davis }
440f889d2efSBrooks Davis 
441f889d2efSBrooks Davis int
442f889d2efSBrooks Davis ifc_alloc_unit(struct if_clone *ifc, int *unit)
443f889d2efSBrooks Davis {
444f889d2efSBrooks Davis 	int wildcard, bytoff, bitoff;
445f889d2efSBrooks Davis 	int err = 0;
446f889d2efSBrooks Davis 
447f889d2efSBrooks Davis 	IF_CLONE_LOCK(ifc);
448f889d2efSBrooks Davis 
449f889d2efSBrooks Davis 	bytoff = bitoff = 0;
450f889d2efSBrooks Davis 	wildcard = (*unit < 0);
451f889d2efSBrooks Davis 	/*
452f889d2efSBrooks Davis 	 * Find a free unit if none was given.
453f889d2efSBrooks Davis 	 */
454f889d2efSBrooks Davis 	if (wildcard) {
455f889d2efSBrooks Davis 		while ((bytoff < ifc->ifc_bmlen)
456f889d2efSBrooks Davis 		    && (ifc->ifc_units[bytoff] == 0xff))
457f889d2efSBrooks Davis 			bytoff++;
458f889d2efSBrooks Davis 		if (bytoff >= ifc->ifc_bmlen) {
459f889d2efSBrooks Davis 			err = ENOSPC;
460f889d2efSBrooks Davis 			goto done;
461f889d2efSBrooks Davis 		}
462f889d2efSBrooks Davis 		while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
463f889d2efSBrooks Davis 			bitoff++;
464f889d2efSBrooks Davis 		*unit = (bytoff << 3) + bitoff;
465f889d2efSBrooks Davis 	}
466f889d2efSBrooks Davis 
467f889d2efSBrooks Davis 	if (*unit > ifc->ifc_maxunit) {
468f889d2efSBrooks Davis 		err = ENOSPC;
469f889d2efSBrooks Davis 		goto done;
470f889d2efSBrooks Davis 	}
471f889d2efSBrooks Davis 
472f889d2efSBrooks Davis 	if (!wildcard) {
473f889d2efSBrooks Davis 		bytoff = *unit >> 3;
474f889d2efSBrooks Davis 		bitoff = *unit - (bytoff << 3);
475f889d2efSBrooks Davis 	}
476f889d2efSBrooks Davis 
477f889d2efSBrooks Davis 	if((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) {
478f889d2efSBrooks Davis 		err = EEXIST;
479f889d2efSBrooks Davis 		goto done;
480f889d2efSBrooks Davis 	}
481f889d2efSBrooks Davis 	/*
482f889d2efSBrooks Davis 	 * Allocate the unit in the bitmap.
483f889d2efSBrooks Davis 	 */
484febd0759SAndrew Thompson 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
485febd0759SAndrew Thompson 	    ("%s: bit is already set", __func__));
486f889d2efSBrooks Davis 	ifc->ifc_units[bytoff] |= (1 << bitoff);
487febd0759SAndrew Thompson 	IF_CLONE_ADDREF_LOCKED(ifc);
488f889d2efSBrooks Davis 
489f889d2efSBrooks Davis done:
490f889d2efSBrooks Davis 	IF_CLONE_UNLOCK(ifc);
491f889d2efSBrooks Davis 	return (err);
492f889d2efSBrooks Davis }
493f889d2efSBrooks Davis 
494f889d2efSBrooks Davis void
495f889d2efSBrooks Davis ifc_free_unit(struct if_clone *ifc, int unit)
496f889d2efSBrooks Davis {
497f889d2efSBrooks Davis 	int bytoff, bitoff;
498f889d2efSBrooks Davis 
499f889d2efSBrooks Davis 
500f889d2efSBrooks Davis 	/*
501f889d2efSBrooks Davis 	 * Compute offset in the bitmap and deallocate the unit.
502f889d2efSBrooks Davis 	 */
503f889d2efSBrooks Davis 	bytoff = unit >> 3;
504f889d2efSBrooks Davis 	bitoff = unit - (bytoff << 3);
505f889d2efSBrooks Davis 
506f889d2efSBrooks Davis 	IF_CLONE_LOCK(ifc);
507f889d2efSBrooks Davis 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
508f889d2efSBrooks Davis 	    ("%s: bit is already cleared", __func__));
509f889d2efSBrooks Davis 	ifc->ifc_units[bytoff] &= ~(1 << bitoff);
510febd0759SAndrew Thompson 	IF_CLONE_REMREF_LOCKED(ifc);	/* releases lock */
511f889d2efSBrooks Davis }
512f889d2efSBrooks Davis 
513f889d2efSBrooks Davis void
514f889d2efSBrooks Davis ifc_simple_attach(struct if_clone *ifc)
515f889d2efSBrooks Davis {
516f889d2efSBrooks Davis 	int err;
517f889d2efSBrooks Davis 	int unit;
518f889d2efSBrooks Davis 	char name[IFNAMSIZ];
519f889d2efSBrooks Davis 	struct ifc_simple_data *ifcs = ifc->ifc_data;
520f889d2efSBrooks Davis 
521f889d2efSBrooks Davis 	KASSERT(ifcs->ifcs_minifs - 1 <= ifc->ifc_maxunit,
522434dbbb3SRuslan Ermilov 	    ("%s: %s requested more units than allowed (%d > %d)",
523f889d2efSBrooks Davis 	    __func__, ifc->ifc_name, ifcs->ifcs_minifs,
524f889d2efSBrooks Davis 	    ifc->ifc_maxunit + 1));
525f889d2efSBrooks Davis 
526f889d2efSBrooks Davis 	for (unit = 0; unit < ifcs->ifcs_minifs; unit++) {
527f889d2efSBrooks Davis 		snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
5286b7330e2SSam Leffler 		err = if_clone_createif(ifc, name, IFNAMSIZ, NULL);
529f889d2efSBrooks Davis 		KASSERT(err == 0,
530f889d2efSBrooks Davis 		    ("%s: failed to create required interface %s",
531f889d2efSBrooks Davis 		    __func__, name));
532f889d2efSBrooks Davis 	}
533f889d2efSBrooks Davis }
534f889d2efSBrooks Davis 
535f889d2efSBrooks Davis int
536f889d2efSBrooks Davis ifc_simple_match(struct if_clone *ifc, const char *name)
537f889d2efSBrooks Davis {
538f889d2efSBrooks Davis 	const char *cp;
539f889d2efSBrooks Davis 	int i;
540f889d2efSBrooks Davis 
541f889d2efSBrooks Davis 	/* Match the name */
542f889d2efSBrooks Davis 	for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
543f889d2efSBrooks Davis 		if (ifc->ifc_name[i] != *cp)
544f889d2efSBrooks Davis 			return (0);
545f889d2efSBrooks Davis 	}
546f889d2efSBrooks Davis 
547f889d2efSBrooks Davis 	/* Make sure there's a unit number or nothing after the name */
548f889d2efSBrooks Davis 	for (; *cp != '\0'; cp++) {
549f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
550f889d2efSBrooks Davis 			return (0);
551f889d2efSBrooks Davis 	}
552f889d2efSBrooks Davis 
553f889d2efSBrooks Davis 	return (1);
554f889d2efSBrooks Davis }
555f889d2efSBrooks Davis 
556f889d2efSBrooks Davis int
5576b7330e2SSam Leffler ifc_simple_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
558f889d2efSBrooks Davis {
559f889d2efSBrooks Davis 	char *dp;
560f889d2efSBrooks Davis 	int wildcard;
561f889d2efSBrooks Davis 	int unit;
562f889d2efSBrooks Davis 	int err;
563f889d2efSBrooks Davis 	struct ifc_simple_data *ifcs = ifc->ifc_data;
564f889d2efSBrooks Davis 
565f889d2efSBrooks Davis 	err = ifc_name2unit(name, &unit);
566f889d2efSBrooks Davis 	if (err != 0)
567f889d2efSBrooks Davis 		return (err);
568f889d2efSBrooks Davis 
569f889d2efSBrooks Davis 	wildcard = (unit < 0);
570f889d2efSBrooks Davis 
571f889d2efSBrooks Davis 	err = ifc_alloc_unit(ifc, &unit);
572f889d2efSBrooks Davis 	if (err != 0)
573f889d2efSBrooks Davis 		return (err);
574f889d2efSBrooks Davis 
5756b7330e2SSam Leffler 	err = ifcs->ifcs_create(ifc, unit, params);
576f889d2efSBrooks Davis 	if (err != 0) {
577f889d2efSBrooks Davis 		ifc_free_unit(ifc, unit);
578f889d2efSBrooks Davis 		return (err);
579f889d2efSBrooks Davis 	}
580f889d2efSBrooks Davis 
581f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
582f889d2efSBrooks Davis 	if (wildcard) {
583f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
584f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
585f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
586f889d2efSBrooks Davis 			/*
587f889d2efSBrooks Davis 			 * This can only be a programmer error and
588f889d2efSBrooks Davis 			 * there's no straightforward way to recover if
589f889d2efSBrooks Davis 			 * it happens.
590f889d2efSBrooks Davis 			 */
591f889d2efSBrooks Davis 			panic("if_clone_create(): interface name too long");
592f889d2efSBrooks Davis 		}
593f889d2efSBrooks Davis 
594f889d2efSBrooks Davis 	}
595f889d2efSBrooks Davis 
596f889d2efSBrooks Davis 	return (0);
597f889d2efSBrooks Davis }
598f889d2efSBrooks Davis 
599f889d2efSBrooks Davis int
600f889d2efSBrooks Davis ifc_simple_destroy(struct if_clone *ifc, struct ifnet *ifp)
601f889d2efSBrooks Davis {
602f889d2efSBrooks Davis 	int unit;
603f889d2efSBrooks Davis 	struct ifc_simple_data *ifcs = ifc->ifc_data;
604f889d2efSBrooks Davis 
605f889d2efSBrooks Davis 	unit = ifp->if_dunit;
606f889d2efSBrooks Davis 
607f889d2efSBrooks Davis 	if (unit < ifcs->ifcs_minifs)
608f889d2efSBrooks Davis 		return (EINVAL);
609f889d2efSBrooks Davis 
610f889d2efSBrooks Davis 	ifcs->ifcs_destroy(ifp);
611f889d2efSBrooks Davis 
612f889d2efSBrooks Davis 	ifc_free_unit(ifc, unit);
613f889d2efSBrooks Davis 
614f889d2efSBrooks Davis 	return (0);
615f889d2efSBrooks Davis }
616