xref: /freebsd/sys/net/if_clone.c (revision 51369649b03ece2aed3eb61b0c8214b9aa5b2fa2)
1c398230bSWarner Losh /*-
2*51369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*51369649SPedro F. Giffuni  *
442a58907SGleb Smirnoff  * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
5f889d2efSBrooks Davis  * Copyright (c) 1980, 1986, 1993
6f889d2efSBrooks Davis  *	The Regents of the University of California.  All rights reserved.
7f889d2efSBrooks Davis  *
8f889d2efSBrooks Davis  * Redistribution and use in source and binary forms, with or without
9f889d2efSBrooks Davis  * modification, are permitted provided that the following conditions
10f889d2efSBrooks Davis  * are met:
11f889d2efSBrooks Davis  * 1. Redistributions of source code must retain the above copyright
12f889d2efSBrooks Davis  *    notice, this list of conditions and the following disclaimer.
13f889d2efSBrooks Davis  * 2. Redistributions in binary form must reproduce the above copyright
14f889d2efSBrooks Davis  *    notice, this list of conditions and the following disclaimer in the
15f889d2efSBrooks Davis  *    documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
17f889d2efSBrooks Davis  *    may be used to endorse or promote products derived from this software
18f889d2efSBrooks Davis  *    without specific prior written permission.
19f889d2efSBrooks Davis  *
20f889d2efSBrooks Davis  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21f889d2efSBrooks Davis  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22f889d2efSBrooks Davis  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23f889d2efSBrooks Davis  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24f889d2efSBrooks Davis  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25f889d2efSBrooks Davis  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26f889d2efSBrooks Davis  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27f889d2efSBrooks Davis  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28f889d2efSBrooks Davis  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29f889d2efSBrooks Davis  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30f889d2efSBrooks Davis  * SUCH DAMAGE.
31f889d2efSBrooks Davis  *
32f889d2efSBrooks Davis  *	@(#)if.c	8.5 (Berkeley) 1/9/95
33f889d2efSBrooks Davis  * $FreeBSD$
34f889d2efSBrooks Davis  */
35f889d2efSBrooks Davis 
36f889d2efSBrooks Davis #include <sys/param.h>
37c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
38f889d2efSBrooks Davis #include <sys/malloc.h>
39434dbbb3SRuslan Ermilov #include <sys/limits.h>
40f889d2efSBrooks Davis #include <sys/lock.h>
41f889d2efSBrooks Davis #include <sys/mutex.h>
42f889d2efSBrooks Davis #include <sys/kernel.h>
43f889d2efSBrooks Davis #include <sys/systm.h>
44f889d2efSBrooks Davis #include <sys/types.h>
45f889d2efSBrooks Davis #include <sys/socket.h>
46f889d2efSBrooks Davis 
47f889d2efSBrooks Davis #include <net/if.h>
48f889d2efSBrooks Davis #include <net/if_var.h>
4976039bc8SGleb Smirnoff #include <net/if_clone.h>
50f889d2efSBrooks Davis #include <net/radix.h>
51f889d2efSBrooks Davis #include <net/route.h>
5221ca7b57SMarko Zec #include <net/vnet.h>
53f889d2efSBrooks Davis 
5442a58907SGleb Smirnoff /* Current IF_MAXUNIT expands maximum to 5 characters. */
5542a58907SGleb Smirnoff #define	IFCLOSIZ	(IFNAMSIZ - 5)
5642a58907SGleb Smirnoff 
5742a58907SGleb Smirnoff /*
5842a58907SGleb Smirnoff  * Structure describing a `cloning' interface.
5942a58907SGleb Smirnoff  *
6042a58907SGleb Smirnoff  * List of locks
6142a58907SGleb Smirnoff  * (c)		const until freeing
6242a58907SGleb Smirnoff  * (d)		driver specific data, may need external protection.
6342a58907SGleb Smirnoff  * (e)		locked by if_cloners_mtx
6442a58907SGleb Smirnoff  * (i)		locked by ifc_mtx mtx
6542a58907SGleb Smirnoff  */
6642a58907SGleb Smirnoff struct if_clone {
6742a58907SGleb Smirnoff 	char ifc_name[IFCLOSIZ];	/* (c) Name of device, e.g. `gif' */
6842a58907SGleb Smirnoff 	struct unrhdr *ifc_unrhdr;	/* (c) alloc_unr(9) header */
6942a58907SGleb Smirnoff 	int ifc_maxunit;		/* (c) maximum unit number */
7042a58907SGleb Smirnoff 	long ifc_refcnt;		/* (i) Reference count. */
7142a58907SGleb Smirnoff 	LIST_HEAD(, ifnet) ifc_iflist;	/* (i) List of cloned interfaces */
7242a58907SGleb Smirnoff 	struct mtx ifc_mtx;		/* Mutex to protect members. */
7342a58907SGleb Smirnoff 
7442a58907SGleb Smirnoff 	enum { SIMPLE, ADVANCED } ifc_type; /* (c) */
7542a58907SGleb Smirnoff 
7642a58907SGleb Smirnoff 	/* (c) Driver specific cloning functions.  Called with no locks held. */
7742a58907SGleb Smirnoff 	union {
7842a58907SGleb Smirnoff 		struct {	/* advanced cloner */
7942a58907SGleb Smirnoff 			ifc_match_t	*_ifc_match;
8042a58907SGleb Smirnoff 			ifc_create_t	*_ifc_create;
8142a58907SGleb Smirnoff 			ifc_destroy_t	*_ifc_destroy;
8242a58907SGleb Smirnoff 		} A;
8342a58907SGleb Smirnoff 		struct {	/* simple cloner */
8442a58907SGleb Smirnoff 			ifcs_create_t	*_ifcs_create;
8542a58907SGleb Smirnoff 			ifcs_destroy_t	*_ifcs_destroy;
8642a58907SGleb Smirnoff 			int		_ifcs_minifs;	/* minimum ifs */
8742a58907SGleb Smirnoff 
8842a58907SGleb Smirnoff 		} S;
8942a58907SGleb Smirnoff 	} U;
9042a58907SGleb Smirnoff #define	ifc_match	U.A._ifc_match
9142a58907SGleb Smirnoff #define	ifc_create	U.A._ifc_create
9242a58907SGleb Smirnoff #define	ifc_destroy	U.A._ifc_destroy
9342a58907SGleb Smirnoff #define	ifcs_create	U.S._ifcs_create
9442a58907SGleb Smirnoff #define	ifcs_destroy	U.S._ifcs_destroy
9542a58907SGleb Smirnoff #define	ifcs_minifs	U.S._ifcs_minifs
9642a58907SGleb Smirnoff 
9742a58907SGleb Smirnoff 	LIST_ENTRY(if_clone) ifc_list;	/* (e) On list of cloners */
9842a58907SGleb Smirnoff };
9942a58907SGleb Smirnoff 
100f889d2efSBrooks Davis static void	if_clone_free(struct if_clone *ifc);
1016b7330e2SSam Leffler static int	if_clone_createif(struct if_clone *ifc, char *name, size_t len,
1026b7330e2SSam Leffler 		    caddr_t params);
103f889d2efSBrooks Davis 
10442a58907SGleb Smirnoff static int     ifc_simple_match(struct if_clone *, const char *);
10542a58907SGleb Smirnoff static int     ifc_simple_create(struct if_clone *, char *, size_t, caddr_t);
10642a58907SGleb Smirnoff static int     ifc_simple_destroy(struct if_clone *, struct ifnet *);
10742a58907SGleb Smirnoff 
108f889d2efSBrooks Davis static struct mtx if_cloners_mtx;
1094ea05db8SGleb Smirnoff MTX_SYSINIT(if_cloners_lock, &if_cloners_mtx, "if_cloners lock", MTX_DEF);
1103e288e62SDimitry Andric static VNET_DEFINE(int, if_cloners_count);
111eddfbb76SRobert Watson VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners);
112eddfbb76SRobert Watson 
1131e77c105SRobert Watson #define	V_if_cloners_count	VNET(if_cloners_count)
1141e77c105SRobert Watson #define	V_if_cloners		VNET(if_cloners)
115f889d2efSBrooks Davis 
116f889d2efSBrooks Davis #define IF_CLONERS_LOCK_ASSERT()	mtx_assert(&if_cloners_mtx, MA_OWNED)
117f889d2efSBrooks Davis #define IF_CLONERS_LOCK()		mtx_lock(&if_cloners_mtx)
118f889d2efSBrooks Davis #define IF_CLONERS_UNLOCK()		mtx_unlock(&if_cloners_mtx)
119f889d2efSBrooks Davis 
120f889d2efSBrooks Davis #define IF_CLONE_LOCK_INIT(ifc)		\
121f889d2efSBrooks Davis     mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
122f889d2efSBrooks Davis #define IF_CLONE_LOCK_DESTROY(ifc)	mtx_destroy(&(ifc)->ifc_mtx)
123f889d2efSBrooks Davis #define IF_CLONE_LOCK_ASSERT(ifc)	mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
124f889d2efSBrooks Davis #define IF_CLONE_LOCK(ifc)		mtx_lock(&(ifc)->ifc_mtx)
125f889d2efSBrooks Davis #define IF_CLONE_UNLOCK(ifc)		mtx_unlock(&(ifc)->ifc_mtx)
126f889d2efSBrooks Davis 
127f889d2efSBrooks Davis #define IF_CLONE_ADDREF(ifc)						\
128f889d2efSBrooks Davis 	do {								\
129f889d2efSBrooks Davis 		IF_CLONE_LOCK(ifc);					\
130f889d2efSBrooks Davis 		IF_CLONE_ADDREF_LOCKED(ifc);				\
131f889d2efSBrooks Davis 		IF_CLONE_UNLOCK(ifc);					\
132f889d2efSBrooks Davis 	} while (0)
133f889d2efSBrooks Davis #define IF_CLONE_ADDREF_LOCKED(ifc)					\
134f889d2efSBrooks Davis 	do {								\
135f889d2efSBrooks Davis 		IF_CLONE_LOCK_ASSERT(ifc);				\
136f889d2efSBrooks Davis 		KASSERT((ifc)->ifc_refcnt >= 0,				\
137f889d2efSBrooks Davis 		    ("negative refcnt %ld", (ifc)->ifc_refcnt));	\
138f889d2efSBrooks Davis 		(ifc)->ifc_refcnt++;					\
139f889d2efSBrooks Davis 	} while (0)
140f889d2efSBrooks Davis #define IF_CLONE_REMREF(ifc)						\
141f889d2efSBrooks Davis 	do {								\
142f889d2efSBrooks Davis 		IF_CLONE_LOCK(ifc);					\
143f889d2efSBrooks Davis 		IF_CLONE_REMREF_LOCKED(ifc);				\
144f889d2efSBrooks Davis 	} while (0)
145f889d2efSBrooks Davis #define IF_CLONE_REMREF_LOCKED(ifc)					\
146f889d2efSBrooks Davis 	do {								\
147f889d2efSBrooks Davis 		IF_CLONE_LOCK_ASSERT(ifc);				\
148f889d2efSBrooks Davis 		KASSERT((ifc)->ifc_refcnt > 0,				\
149f889d2efSBrooks Davis 		    ("bogus refcnt %ld", (ifc)->ifc_refcnt));		\
150f889d2efSBrooks Davis 		if (--(ifc)->ifc_refcnt == 0) {				\
151f889d2efSBrooks Davis 			IF_CLONE_UNLOCK(ifc);				\
152f889d2efSBrooks Davis 			if_clone_free(ifc);				\
153ca64c799SMax Laier 		} else {						\
154f889d2efSBrooks Davis 			/* silently free the lock */			\
155f889d2efSBrooks Davis 			IF_CLONE_UNLOCK(ifc);				\
156ca64c799SMax Laier 		}							\
157f889d2efSBrooks Davis 	} while (0)
158f889d2efSBrooks Davis 
1594e7e0183SAndrew Thompson #define IFC_IFLIST_INSERT(_ifc, _ifp)					\
1604e7e0183SAndrew Thompson 	LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones)
1614e7e0183SAndrew Thompson #define IFC_IFLIST_REMOVE(_ifc, _ifp)					\
1624e7e0183SAndrew Thompson 	LIST_REMOVE(_ifp, if_clones)
1634e7e0183SAndrew Thompson 
164c711aea6SPoul-Henning Kamp static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
165f889d2efSBrooks Davis 
166d0728d71SRobert Watson void
167d0728d71SRobert Watson vnet_if_clone_init(void)
16837f17770SMarko Zec {
16937f17770SMarko Zec 
17037f17770SMarko Zec 	LIST_INIT(&V_if_cloners);
17137f17770SMarko Zec }
17237f17770SMarko Zec 
173f889d2efSBrooks Davis /*
1744e7e0183SAndrew Thompson  * Lookup and create a clone network interface.
175f889d2efSBrooks Davis  */
176f889d2efSBrooks Davis int
1776b7330e2SSam Leffler if_clone_create(char *name, size_t len, caddr_t params)
178f889d2efSBrooks Davis {
179f889d2efSBrooks Davis 	struct if_clone *ifc;
180f889d2efSBrooks Davis 
181f889d2efSBrooks Davis 	/* Try to find an applicable cloner for this request */
182f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
18342a58907SGleb Smirnoff 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
18442a58907SGleb Smirnoff 		if (ifc->ifc_type == SIMPLE) {
18542a58907SGleb Smirnoff 			if (ifc_simple_match(ifc, name))
186f889d2efSBrooks Davis 				break;
18742a58907SGleb Smirnoff 		} else {
18842a58907SGleb Smirnoff 			if (ifc->ifc_match(ifc, name))
18942a58907SGleb Smirnoff 				break;
190f889d2efSBrooks Davis 		}
19137f17770SMarko Zec #ifdef VIMAGE
19237f17770SMarko Zec 	if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
19337f17770SMarko Zec 		CURVNET_SET_QUIET(vnet0);
19442a58907SGleb Smirnoff 		LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
19542a58907SGleb Smirnoff 			if (ifc->ifc_type == SIMPLE) {
19642a58907SGleb Smirnoff 				if (ifc_simple_match(ifc, name))
19742a58907SGleb Smirnoff 					break;
19842a58907SGleb Smirnoff 			} else {
19937f17770SMarko Zec 				if (ifc->ifc_match(ifc, name))
20037f17770SMarko Zec 					break;
20137f17770SMarko Zec 			}
20237f17770SMarko Zec 		CURVNET_RESTORE();
20337f17770SMarko Zec 	}
20437f17770SMarko Zec #endif
205f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
206f889d2efSBrooks Davis 
207f889d2efSBrooks Davis 	if (ifc == NULL)
208f889d2efSBrooks Davis 		return (EINVAL);
209f889d2efSBrooks Davis 
2106b7330e2SSam Leffler 	return (if_clone_createif(ifc, name, len, params));
2114e7e0183SAndrew Thompson }
2124e7e0183SAndrew Thompson 
2134e7e0183SAndrew Thompson /*
2144e7e0183SAndrew Thompson  * Create a clone network interface.
2154e7e0183SAndrew Thompson  */
2164e7e0183SAndrew Thompson static int
2176b7330e2SSam Leffler if_clone_createif(struct if_clone *ifc, char *name, size_t len, caddr_t params)
2184e7e0183SAndrew Thompson {
2194e7e0183SAndrew Thompson 	int err;
2204e7e0183SAndrew Thompson 	struct ifnet *ifp;
2214e7e0183SAndrew Thompson 
2224e7e0183SAndrew Thompson 	if (ifunit(name) != NULL)
2234e7e0183SAndrew Thompson 		return (EEXIST);
2244e7e0183SAndrew Thompson 
22542a58907SGleb Smirnoff 	if (ifc->ifc_type == SIMPLE)
22642a58907SGleb Smirnoff 		err = ifc_simple_create(ifc, name, len, params);
22742a58907SGleb Smirnoff 	else
2286b7330e2SSam Leffler 		err = (*ifc->ifc_create)(ifc, name, len, params);
2294e7e0183SAndrew Thompson 
2304e7e0183SAndrew Thompson 	if (!err) {
2314e7e0183SAndrew Thompson 		ifp = ifunit(name);
2324e7e0183SAndrew Thompson 		if (ifp == NULL)
2334e7e0183SAndrew Thompson 			panic("%s: lookup failed for %s", __func__, name);
2344e7e0183SAndrew Thompson 
2350dad3f0eSMax Laier 		if_addgroup(ifp, ifc->ifc_name);
2360dad3f0eSMax Laier 
2374e7e0183SAndrew Thompson 		IF_CLONE_LOCK(ifc);
2384e7e0183SAndrew Thompson 		IFC_IFLIST_INSERT(ifc, ifp);
2394e7e0183SAndrew Thompson 		IF_CLONE_UNLOCK(ifc);
2404e7e0183SAndrew Thompson 	}
2414e7e0183SAndrew Thompson 
242f889d2efSBrooks Davis 	return (err);
243f889d2efSBrooks Davis }
244f889d2efSBrooks Davis 
245f889d2efSBrooks Davis /*
2464e7e0183SAndrew Thompson  * Lookup and destroy a clone network interface.
247f889d2efSBrooks Davis  */
248f889d2efSBrooks Davis int
249f889d2efSBrooks Davis if_clone_destroy(const char *name)
250f889d2efSBrooks Davis {
251d0088cdeSBjoern A. Zeeb 	int err;
252f889d2efSBrooks Davis 	struct if_clone *ifc;
253f889d2efSBrooks Davis 	struct ifnet *ifp;
254f889d2efSBrooks Davis 
255d0088cdeSBjoern A. Zeeb 	ifp = ifunit_ref(name);
256f889d2efSBrooks Davis 	if (ifp == NULL)
257f889d2efSBrooks Davis 		return (ENXIO);
258f889d2efSBrooks Davis 
259f889d2efSBrooks Davis 	/* Find the cloner for this interface */
260f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
26137f17770SMarko Zec 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
262f889d2efSBrooks Davis 		if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) {
263f889d2efSBrooks Davis 			break;
264f889d2efSBrooks Davis 		}
265f889d2efSBrooks Davis 	}
26637f17770SMarko Zec #ifdef VIMAGE
26737f17770SMarko Zec 	if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
26837f17770SMarko Zec 		CURVNET_SET_QUIET(vnet0);
26942a58907SGleb Smirnoff 		LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
2703aaf0159SGleb Smirnoff 			if (ifc->ifc_type == SIMPLE) {
27142a58907SGleb Smirnoff 				if (ifc_simple_match(ifc, name))
27242a58907SGleb Smirnoff 					break;
27342a58907SGleb Smirnoff 			} else {
27437f17770SMarko Zec 				if (ifc->ifc_match(ifc, name))
27537f17770SMarko Zec 					break;
27637f17770SMarko Zec 			}
27737f17770SMarko Zec 		CURVNET_RESTORE();
27837f17770SMarko Zec 	}
27937f17770SMarko Zec #endif
280f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
281d0088cdeSBjoern A. Zeeb 	if (ifc == NULL) {
282d0088cdeSBjoern A. Zeeb 		if_rele(ifp);
283f889d2efSBrooks Davis 		return (EINVAL);
284d0088cdeSBjoern A. Zeeb 	}
285f889d2efSBrooks Davis 
286d0088cdeSBjoern A. Zeeb 	err = if_clone_destroyif(ifc, ifp);
287d0088cdeSBjoern A. Zeeb 	if_rele(ifp);
288d0088cdeSBjoern A. Zeeb 	return err;
2894e7e0183SAndrew Thompson }
2904e7e0183SAndrew Thompson 
2914e7e0183SAndrew Thompson /*
2924e7e0183SAndrew Thompson  * Destroy a clone network interface.
2934e7e0183SAndrew Thompson  */
294cb959fa2SAndrew Thompson int
2954e7e0183SAndrew Thompson if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
2964e7e0183SAndrew Thompson {
2974e7e0183SAndrew Thompson 	int err;
298c769e1beSBjoern A. Zeeb 	struct ifnet *ifcifp;
2994e7e0183SAndrew Thompson 
30042a58907SGleb Smirnoff 	if (ifc->ifc_type == ADVANCED && ifc->ifc_destroy == NULL)
30121ca7b57SMarko Zec 		return(EOPNOTSUPP);
302f889d2efSBrooks Davis 
30337f17770SMarko Zec 	/*
30437f17770SMarko Zec 	 * Given that the cloned ifnet might be attached to a different
30537f17770SMarko Zec 	 * vnet from where its cloner was registered, we have to
30637f17770SMarko Zec 	 * switch to the vnet context of the target vnet.
30737f17770SMarko Zec 	 */
30837f17770SMarko Zec 	CURVNET_SET_QUIET(ifp->if_vnet);
30937f17770SMarko Zec 
310434dbbb3SRuslan Ermilov 	IF_CLONE_LOCK(ifc);
311c769e1beSBjoern A. Zeeb 	LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
312c769e1beSBjoern A. Zeeb 		if (ifcifp == ifp) {
313434dbbb3SRuslan Ermilov 			IFC_IFLIST_REMOVE(ifc, ifp);
314c769e1beSBjoern A. Zeeb 			break;
315c769e1beSBjoern A. Zeeb 		}
316c769e1beSBjoern A. Zeeb 	}
317434dbbb3SRuslan Ermilov 	IF_CLONE_UNLOCK(ifc);
318c769e1beSBjoern A. Zeeb 	if (ifcifp == NULL) {
319c769e1beSBjoern A. Zeeb 		CURVNET_RESTORE();
320c769e1beSBjoern A. Zeeb 		return (ENXIO);		/* ifp is not on the list. */
321c769e1beSBjoern A. Zeeb 	}
322434dbbb3SRuslan Ermilov 
3230dad3f0eSMax Laier 	if_delgroup(ifp, ifc->ifc_name);
3240dad3f0eSMax Laier 
32542a58907SGleb Smirnoff 	if (ifc->ifc_type == SIMPLE)
32642a58907SGleb Smirnoff 		err = ifc_simple_destroy(ifc, ifp);
32742a58907SGleb Smirnoff 	else
328f889d2efSBrooks Davis 		err = (*ifc->ifc_destroy)(ifc, ifp);
329f889d2efSBrooks Davis 
330434dbbb3SRuslan Ermilov 	if (err != 0) {
3310dad3f0eSMax Laier 		if_addgroup(ifp, ifc->ifc_name);
3320dad3f0eSMax Laier 
333434dbbb3SRuslan Ermilov 		IF_CLONE_LOCK(ifc);
334434dbbb3SRuslan Ermilov 		IFC_IFLIST_INSERT(ifc, ifp);
335434dbbb3SRuslan Ermilov 		IF_CLONE_UNLOCK(ifc);
336434dbbb3SRuslan Ermilov 	}
33721ca7b57SMarko Zec 	CURVNET_RESTORE();
338f889d2efSBrooks Davis 	return (err);
339f889d2efSBrooks Davis }
340f889d2efSBrooks Davis 
34142a58907SGleb Smirnoff static struct if_clone *
34242a58907SGleb Smirnoff if_clone_alloc(const char *name, int maxunit)
34342a58907SGleb Smirnoff {
34442a58907SGleb Smirnoff 	struct if_clone *ifc;
34542a58907SGleb Smirnoff 
34642a58907SGleb Smirnoff 	KASSERT(name != NULL, ("%s: no name\n", __func__));
34742a58907SGleb Smirnoff 
34842a58907SGleb Smirnoff 	ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO);
34942a58907SGleb Smirnoff 	strncpy(ifc->ifc_name, name, IFCLOSIZ-1);
35042a58907SGleb Smirnoff 	IF_CLONE_LOCK_INIT(ifc);
35142a58907SGleb Smirnoff 	IF_CLONE_ADDREF(ifc);
35242a58907SGleb Smirnoff 	ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT;
35342a58907SGleb Smirnoff 	ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx);
35442a58907SGleb Smirnoff 	LIST_INIT(&ifc->ifc_iflist);
35542a58907SGleb Smirnoff 
35642a58907SGleb Smirnoff 	return (ifc);
35742a58907SGleb Smirnoff }
35842a58907SGleb Smirnoff 
35942a58907SGleb Smirnoff static int
360f889d2efSBrooks Davis if_clone_attach(struct if_clone *ifc)
361f889d2efSBrooks Davis {
3622e9fff5bSGleb Smirnoff 	struct if_clone *ifc1;
363f889d2efSBrooks Davis 
364f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
3652e9fff5bSGleb Smirnoff 	LIST_FOREACH(ifc1, &V_if_cloners, ifc_list)
3662e9fff5bSGleb Smirnoff 		if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) {
3672e9fff5bSGleb Smirnoff 			IF_CLONERS_UNLOCK();
3682e9fff5bSGleb Smirnoff 			IF_CLONE_REMREF(ifc);
3692e9fff5bSGleb Smirnoff 			return (EEXIST);
3702e9fff5bSGleb Smirnoff 		}
37137f17770SMarko Zec 	LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
37237f17770SMarko Zec 	V_if_cloners_count++;
373f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
374f889d2efSBrooks Davis 
37542a58907SGleb Smirnoff 	return (0);
37642a58907SGleb Smirnoff }
37742a58907SGleb Smirnoff 
37842a58907SGleb Smirnoff struct if_clone *
37942a58907SGleb Smirnoff if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
38042a58907SGleb Smirnoff 	ifc_create_t create, ifc_destroy_t destroy)
38142a58907SGleb Smirnoff {
38242a58907SGleb Smirnoff 	struct if_clone *ifc;
38342a58907SGleb Smirnoff 
38442a58907SGleb Smirnoff 	ifc = if_clone_alloc(name, maxunit);
38542a58907SGleb Smirnoff 	ifc->ifc_type = ADVANCED;
38642a58907SGleb Smirnoff 	ifc->ifc_match = match;
38742a58907SGleb Smirnoff 	ifc->ifc_create = create;
38842a58907SGleb Smirnoff 	ifc->ifc_destroy = destroy;
38942a58907SGleb Smirnoff 
39042a58907SGleb Smirnoff 	if (if_clone_attach(ifc) != 0) {
39142a58907SGleb Smirnoff 		if_clone_free(ifc);
39242a58907SGleb Smirnoff 		return (NULL);
39342a58907SGleb Smirnoff 	}
39442a58907SGleb Smirnoff 
395f889d2efSBrooks Davis 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
3962e9fff5bSGleb Smirnoff 
39742a58907SGleb Smirnoff 	return (ifc);
39842a58907SGleb Smirnoff }
39942a58907SGleb Smirnoff 
40042a58907SGleb Smirnoff struct if_clone *
40142a58907SGleb Smirnoff if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
40242a58907SGleb Smirnoff 	u_int minifs)
40342a58907SGleb Smirnoff {
40442a58907SGleb Smirnoff 	struct if_clone *ifc;
40542a58907SGleb Smirnoff 	u_int unit;
40642a58907SGleb Smirnoff 
40742a58907SGleb Smirnoff 	ifc = if_clone_alloc(name, 0);
40842a58907SGleb Smirnoff 	ifc->ifc_type = SIMPLE;
40942a58907SGleb Smirnoff 	ifc->ifcs_create = create;
41042a58907SGleb Smirnoff 	ifc->ifcs_destroy = destroy;
41142a58907SGleb Smirnoff 	ifc->ifcs_minifs = minifs;
41242a58907SGleb Smirnoff 
41342a58907SGleb Smirnoff 	if (if_clone_attach(ifc) != 0) {
41442a58907SGleb Smirnoff 		if_clone_free(ifc);
41542a58907SGleb Smirnoff 		return (NULL);
41642a58907SGleb Smirnoff 	}
41742a58907SGleb Smirnoff 
41842a58907SGleb Smirnoff 	for (unit = 0; unit < minifs; unit++) {
41942a58907SGleb Smirnoff 		char name[IFNAMSIZ];
42042a58907SGleb Smirnoff 		int error;
42142a58907SGleb Smirnoff 
42242a58907SGleb Smirnoff 		snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
42342a58907SGleb Smirnoff 		error = if_clone_createif(ifc, name, IFNAMSIZ, NULL);
42442a58907SGleb Smirnoff 		KASSERT(error == 0,
42542a58907SGleb Smirnoff 		    ("%s: failed to create required interface %s",
42642a58907SGleb Smirnoff 		    __func__, name));
42742a58907SGleb Smirnoff 	}
42842a58907SGleb Smirnoff 
42942a58907SGleb Smirnoff 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
43042a58907SGleb Smirnoff 
43142a58907SGleb Smirnoff 	return (ifc);
432f889d2efSBrooks Davis }
433f889d2efSBrooks Davis 
434f889d2efSBrooks Davis /*
435f889d2efSBrooks Davis  * Unregister a network interface cloner.
436f889d2efSBrooks Davis  */
437f889d2efSBrooks Davis void
438f889d2efSBrooks Davis if_clone_detach(struct if_clone *ifc)
439f889d2efSBrooks Davis {
440f889d2efSBrooks Davis 
441f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
442f889d2efSBrooks Davis 	LIST_REMOVE(ifc, ifc_list);
44337f17770SMarko Zec 	V_if_cloners_count--;
444f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
445f889d2efSBrooks Davis 
446febd0759SAndrew Thompson 	/* Allow all simples to be destroyed */
44742a58907SGleb Smirnoff 	if (ifc->ifc_type == SIMPLE)
44842a58907SGleb Smirnoff 		ifc->ifcs_minifs = 0;
449febd0759SAndrew Thompson 
4504e7e0183SAndrew Thompson 	/* destroy all interfaces for this cloner */
4514e7e0183SAndrew Thompson 	while (!LIST_EMPTY(&ifc->ifc_iflist))
4524e7e0183SAndrew Thompson 		if_clone_destroyif(ifc, LIST_FIRST(&ifc->ifc_iflist));
4534e7e0183SAndrew Thompson 
454f889d2efSBrooks Davis 	IF_CLONE_REMREF(ifc);
455f889d2efSBrooks Davis }
456f889d2efSBrooks Davis 
457f889d2efSBrooks Davis static void
458f889d2efSBrooks Davis if_clone_free(struct if_clone *ifc)
459f889d2efSBrooks Davis {
460f889d2efSBrooks Davis 
4614e7e0183SAndrew Thompson 	KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
4624e7e0183SAndrew Thompson 	    ("%s: ifc_iflist not empty", __func__));
4634e7e0183SAndrew Thompson 
464f889d2efSBrooks Davis 	IF_CLONE_LOCK_DESTROY(ifc);
4652e9fff5bSGleb Smirnoff 	delete_unrhdr(ifc->ifc_unrhdr);
46642a58907SGleb Smirnoff 	free(ifc, M_CLONE);
467f889d2efSBrooks Davis }
468f889d2efSBrooks Davis 
469f889d2efSBrooks Davis /*
470f889d2efSBrooks Davis  * Provide list of interface cloners to userspace.
471f889d2efSBrooks Davis  */
472f889d2efSBrooks Davis int
473f889d2efSBrooks Davis if_clone_list(struct if_clonereq *ifcr)
474f889d2efSBrooks Davis {
475c859ef97SBrooks Davis 	char *buf, *dst, *outbuf = NULL;
476f889d2efSBrooks Davis 	struct if_clone *ifc;
477c859ef97SBrooks Davis 	int buf_count, count, err = 0;
478c859ef97SBrooks Davis 
479a6d00835SMaxim Konovalov 	if (ifcr->ifcr_count < 0)
480a6d00835SMaxim Konovalov 		return (EINVAL);
481a6d00835SMaxim Konovalov 
482c859ef97SBrooks Davis 	IF_CLONERS_LOCK();
483c859ef97SBrooks Davis 	/*
484c859ef97SBrooks Davis 	 * Set our internal output buffer size.  We could end up not
485c859ef97SBrooks Davis 	 * reporting a cloner that is added between the unlock and lock
486c859ef97SBrooks Davis 	 * below, but that's not a major problem.  Not caping our
487c859ef97SBrooks Davis 	 * allocation to the number of cloners actually in the system
488c859ef97SBrooks Davis 	 * could be because that would let arbitrary users cause us to
489a4641f4eSPedro F. Giffuni 	 * allocate arbitrary amounts of kernel memory.
490c859ef97SBrooks Davis 	 */
49137f17770SMarko Zec 	buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
49237f17770SMarko Zec 	    V_if_cloners_count : ifcr->ifcr_count;
493c859ef97SBrooks Davis 	IF_CLONERS_UNLOCK();
494c859ef97SBrooks Davis 
495c859ef97SBrooks Davis 	outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
496f889d2efSBrooks Davis 
497f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
498f889d2efSBrooks Davis 
49937f17770SMarko Zec 	ifcr->ifcr_total = V_if_cloners_count;
500f889d2efSBrooks Davis 	if ((dst = ifcr->ifcr_buffer) == NULL) {
501f889d2efSBrooks Davis 		/* Just asking how many there are. */
502f889d2efSBrooks Davis 		goto done;
503f889d2efSBrooks Davis 	}
50437f17770SMarko Zec 	count = (V_if_cloners_count < buf_count) ?
50537f17770SMarko Zec 	    V_if_cloners_count : buf_count;
506f889d2efSBrooks Davis 
50737f17770SMarko Zec 	for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
508c859ef97SBrooks Davis 	    ifc != NULL && count != 0;
509c859ef97SBrooks Davis 	    ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
510c859ef97SBrooks Davis 		strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
511f889d2efSBrooks Davis 	}
512f889d2efSBrooks Davis 
513f889d2efSBrooks Davis done:
514f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
515c859ef97SBrooks Davis 	if (err == 0)
516c859ef97SBrooks Davis 		err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
517c859ef97SBrooks Davis 	if (outbuf != NULL)
518c859ef97SBrooks Davis 		free(outbuf, M_CLONE);
519f889d2efSBrooks Davis 	return (err);
520f889d2efSBrooks Davis }
521f889d2efSBrooks Davis 
522f889d2efSBrooks Davis /*
523c92a456bSHiroki Sato  * if_clone_findifc() looks up ifnet from the current
524c92a456bSHiroki Sato  * cloner list, and returns ifc if found.  Note that ifc_refcnt
525c92a456bSHiroki Sato  * is incremented.
526c92a456bSHiroki Sato  */
527c92a456bSHiroki Sato struct if_clone *
528c92a456bSHiroki Sato if_clone_findifc(struct ifnet *ifp)
529c92a456bSHiroki Sato {
530c92a456bSHiroki Sato 	struct if_clone *ifc, *ifc0;
531c92a456bSHiroki Sato 	struct ifnet *ifcifp;
532c92a456bSHiroki Sato 
533c92a456bSHiroki Sato 	ifc0 = NULL;
534c92a456bSHiroki Sato 	IF_CLONERS_LOCK();
535c92a456bSHiroki Sato 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
536c92a456bSHiroki Sato 		IF_CLONE_LOCK(ifc);
537c92a456bSHiroki Sato 		LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
538c92a456bSHiroki Sato 			if (ifp == ifcifp) {
539c92a456bSHiroki Sato 				ifc0 = ifc;
540c92a456bSHiroki Sato 				IF_CLONE_ADDREF_LOCKED(ifc);
541c92a456bSHiroki Sato 				break;
542c92a456bSHiroki Sato 			}
543c92a456bSHiroki Sato 		}
544c92a456bSHiroki Sato 		IF_CLONE_UNLOCK(ifc);
545c92a456bSHiroki Sato 		if (ifc0 != NULL)
546c92a456bSHiroki Sato 			break;
547c92a456bSHiroki Sato 	}
548c92a456bSHiroki Sato 	IF_CLONERS_UNLOCK();
549c92a456bSHiroki Sato 
550c92a456bSHiroki Sato 	return (ifc0);
551c92a456bSHiroki Sato }
552c92a456bSHiroki Sato 
553c92a456bSHiroki Sato /*
554c92a456bSHiroki Sato  * if_clone_addgroup() decrements ifc_refcnt because it is called after
555c92a456bSHiroki Sato  * if_clone_findifc().
556c92a456bSHiroki Sato  */
557c92a456bSHiroki Sato void
558c92a456bSHiroki Sato if_clone_addgroup(struct ifnet *ifp, struct if_clone *ifc)
559c92a456bSHiroki Sato {
560c92a456bSHiroki Sato 
561c92a456bSHiroki Sato 	if_addgroup(ifp, ifc->ifc_name);
562c92a456bSHiroki Sato 	IF_CLONE_REMREF(ifc);
563c92a456bSHiroki Sato }
564c92a456bSHiroki Sato 
565c92a456bSHiroki Sato /*
566f889d2efSBrooks Davis  * A utility function to extract unit numbers from interface names of
567f889d2efSBrooks Davis  * the form name###.
568f889d2efSBrooks Davis  *
569f889d2efSBrooks Davis  * Returns 0 on success and an error on failure.
570f889d2efSBrooks Davis  */
571f889d2efSBrooks Davis int
572f889d2efSBrooks Davis ifc_name2unit(const char *name, int *unit)
573f889d2efSBrooks Davis {
574f889d2efSBrooks Davis 	const char	*cp;
575434dbbb3SRuslan Ermilov 	int		cutoff = INT_MAX / 10;
576434dbbb3SRuslan Ermilov 	int		cutlim = INT_MAX % 10;
577f889d2efSBrooks Davis 
578f889d2efSBrooks Davis 	for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++);
579f889d2efSBrooks Davis 	if (*cp == '\0') {
580f889d2efSBrooks Davis 		*unit = -1;
581434dbbb3SRuslan Ermilov 	} else if (cp[0] == '0' && cp[1] != '\0') {
582434dbbb3SRuslan Ermilov 		/* Disallow leading zeroes. */
583434dbbb3SRuslan Ermilov 		return (EINVAL);
584f889d2efSBrooks Davis 	} else {
585f889d2efSBrooks Davis 		for (*unit = 0; *cp != '\0'; cp++) {
586f889d2efSBrooks Davis 			if (*cp < '0' || *cp > '9') {
587f889d2efSBrooks Davis 				/* Bogus unit number. */
588f889d2efSBrooks Davis 				return (EINVAL);
589f889d2efSBrooks Davis 			}
590434dbbb3SRuslan Ermilov 			if (*unit > cutoff ||
591434dbbb3SRuslan Ermilov 			    (*unit == cutoff && *cp - '0' > cutlim))
592434dbbb3SRuslan Ermilov 				return (EINVAL);
593f889d2efSBrooks Davis 			*unit = (*unit * 10) + (*cp - '0');
594f889d2efSBrooks Davis 		}
595f889d2efSBrooks Davis 	}
596f889d2efSBrooks Davis 
597f889d2efSBrooks Davis 	return (0);
598f889d2efSBrooks Davis }
599f889d2efSBrooks Davis 
600c64c1f95SAndriy Voskoboinyk static int
601c64c1f95SAndriy Voskoboinyk ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
602f889d2efSBrooks Davis {
6032e9fff5bSGleb Smirnoff 	char name[IFNAMSIZ];
604f889d2efSBrooks Davis 
6053932d760SGleb Smirnoff 	if (*unit > ifc->ifc_maxunit)
6063932d760SGleb Smirnoff 		return (ENOSPC);
607c64c1f95SAndriy Voskoboinyk 
608c64c1f95SAndriy Voskoboinyk 	if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
6092e9fff5bSGleb Smirnoff 		return (EEXIST);
610f889d2efSBrooks Davis 
6112e9fff5bSGleb Smirnoff 	snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
6122e9fff5bSGleb Smirnoff 	if (ifunit(name) != NULL) {
6133932d760SGleb Smirnoff 		free_unr(ifc->ifc_unrhdr, *unit);
6142e9fff5bSGleb Smirnoff 		return (EEXIST);
615f889d2efSBrooks Davis 	}
616f889d2efSBrooks Davis 
6172e9fff5bSGleb Smirnoff 	IF_CLONE_ADDREF(ifc);
618f889d2efSBrooks Davis 
6192e9fff5bSGleb Smirnoff 	return (0);
620f889d2efSBrooks Davis }
621f889d2efSBrooks Davis 
622c64c1f95SAndriy Voskoboinyk static int
623c64c1f95SAndriy Voskoboinyk ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
624c64c1f95SAndriy Voskoboinyk {
625c64c1f95SAndriy Voskoboinyk 	int error;
626c64c1f95SAndriy Voskoboinyk 
627c64c1f95SAndriy Voskoboinyk 	*unit = alloc_unr(ifc->ifc_unrhdr);
628c64c1f95SAndriy Voskoboinyk 	if (*unit == -1)
629c64c1f95SAndriy Voskoboinyk 		return (ENOSPC);
630c64c1f95SAndriy Voskoboinyk 
631c64c1f95SAndriy Voskoboinyk 	free_unr(ifc->ifc_unrhdr, *unit);
632c64c1f95SAndriy Voskoboinyk 	for (;;) {
633c64c1f95SAndriy Voskoboinyk 		error = ifc_alloc_unit_specific(ifc, unit);
634c64c1f95SAndriy Voskoboinyk 		if (error != EEXIST)
635c64c1f95SAndriy Voskoboinyk 			break;
636c64c1f95SAndriy Voskoboinyk 
637c64c1f95SAndriy Voskoboinyk 		(*unit)++;
638c64c1f95SAndriy Voskoboinyk 	}
639c64c1f95SAndriy Voskoboinyk 
640c64c1f95SAndriy Voskoboinyk 	return (error);
641c64c1f95SAndriy Voskoboinyk }
642c64c1f95SAndriy Voskoboinyk 
643c64c1f95SAndriy Voskoboinyk int
644c64c1f95SAndriy Voskoboinyk ifc_alloc_unit(struct if_clone *ifc, int *unit)
645c64c1f95SAndriy Voskoboinyk {
646c64c1f95SAndriy Voskoboinyk 	if (*unit < 0)
647c64c1f95SAndriy Voskoboinyk 		return (ifc_alloc_unit_next(ifc, unit));
648c64c1f95SAndriy Voskoboinyk 	else
649c64c1f95SAndriy Voskoboinyk 		return (ifc_alloc_unit_specific(ifc, unit));
650c64c1f95SAndriy Voskoboinyk }
651c64c1f95SAndriy Voskoboinyk 
652f889d2efSBrooks Davis void
653f889d2efSBrooks Davis ifc_free_unit(struct if_clone *ifc, int unit)
654f889d2efSBrooks Davis {
655f889d2efSBrooks Davis 
6562e9fff5bSGleb Smirnoff 	free_unr(ifc->ifc_unrhdr, unit);
6572e9fff5bSGleb Smirnoff 	IF_CLONE_REMREF(ifc);
658f889d2efSBrooks Davis }
659f889d2efSBrooks Davis 
66042a58907SGleb Smirnoff static int
661f889d2efSBrooks Davis ifc_simple_match(struct if_clone *ifc, const char *name)
662f889d2efSBrooks Davis {
663f889d2efSBrooks Davis 	const char *cp;
664f889d2efSBrooks Davis 	int i;
665f889d2efSBrooks Davis 
666f889d2efSBrooks Davis 	/* Match the name */
667f889d2efSBrooks Davis 	for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
668f889d2efSBrooks Davis 		if (ifc->ifc_name[i] != *cp)
669f889d2efSBrooks Davis 			return (0);
670f889d2efSBrooks Davis 	}
671f889d2efSBrooks Davis 
672f889d2efSBrooks Davis 	/* Make sure there's a unit number or nothing after the name */
673f889d2efSBrooks Davis 	for (; *cp != '\0'; cp++) {
674f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
675f889d2efSBrooks Davis 			return (0);
676f889d2efSBrooks Davis 	}
677f889d2efSBrooks Davis 
678f889d2efSBrooks Davis 	return (1);
679f889d2efSBrooks Davis }
680f889d2efSBrooks Davis 
68142a58907SGleb Smirnoff static int
6826b7330e2SSam Leffler ifc_simple_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
683f889d2efSBrooks Davis {
684f889d2efSBrooks Davis 	char *dp;
685f889d2efSBrooks Davis 	int wildcard;
686f889d2efSBrooks Davis 	int unit;
687f889d2efSBrooks Davis 	int err;
688f889d2efSBrooks Davis 
689f889d2efSBrooks Davis 	err = ifc_name2unit(name, &unit);
690f889d2efSBrooks Davis 	if (err != 0)
691f889d2efSBrooks Davis 		return (err);
692f889d2efSBrooks Davis 
693f889d2efSBrooks Davis 	wildcard = (unit < 0);
694f889d2efSBrooks Davis 
695f889d2efSBrooks Davis 	err = ifc_alloc_unit(ifc, &unit);
696f889d2efSBrooks Davis 	if (err != 0)
697f889d2efSBrooks Davis 		return (err);
698f889d2efSBrooks Davis 
69942a58907SGleb Smirnoff 	err = ifc->ifcs_create(ifc, unit, params);
700f889d2efSBrooks Davis 	if (err != 0) {
701f889d2efSBrooks Davis 		ifc_free_unit(ifc, unit);
702f889d2efSBrooks Davis 		return (err);
703f889d2efSBrooks Davis 	}
704f889d2efSBrooks Davis 
705f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
706f889d2efSBrooks Davis 	if (wildcard) {
707f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
708f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
709f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
710f889d2efSBrooks Davis 			/*
711f889d2efSBrooks Davis 			 * This can only be a programmer error and
712f889d2efSBrooks Davis 			 * there's no straightforward way to recover if
713f889d2efSBrooks Davis 			 * it happens.
714f889d2efSBrooks Davis 			 */
715f889d2efSBrooks Davis 			panic("if_clone_create(): interface name too long");
716f889d2efSBrooks Davis 		}
717f889d2efSBrooks Davis 
718f889d2efSBrooks Davis 	}
719f889d2efSBrooks Davis 
720f889d2efSBrooks Davis 	return (0);
721f889d2efSBrooks Davis }
722f889d2efSBrooks Davis 
72342a58907SGleb Smirnoff static int
724f889d2efSBrooks Davis ifc_simple_destroy(struct if_clone *ifc, struct ifnet *ifp)
725f889d2efSBrooks Davis {
726f889d2efSBrooks Davis 	int unit;
727f889d2efSBrooks Davis 
728f889d2efSBrooks Davis 	unit = ifp->if_dunit;
729f889d2efSBrooks Davis 
73042a58907SGleb Smirnoff 	if (unit < ifc->ifcs_minifs)
731f889d2efSBrooks Davis 		return (EINVAL);
732f889d2efSBrooks Davis 
73342a58907SGleb Smirnoff 	ifc->ifcs_destroy(ifp);
734f889d2efSBrooks Davis 
735f889d2efSBrooks Davis 	ifc_free_unit(ifc, unit);
736f889d2efSBrooks Davis 
737f889d2efSBrooks Davis 	return (0);
738f889d2efSBrooks Davis }
739