xref: /freebsd/sys/net/if_clone.c (revision 29363fb4)
1c398230bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro 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 
33f889d2efSBrooks Davis #include <sys/param.h>
34c3322cb9SGleb Smirnoff #include <sys/eventhandler.h>
35f889d2efSBrooks Davis #include <sys/malloc.h>
36434dbbb3SRuslan Ermilov #include <sys/limits.h>
37f889d2efSBrooks Davis #include <sys/lock.h>
38f889d2efSBrooks Davis #include <sys/mutex.h>
39f889d2efSBrooks Davis #include <sys/kernel.h>
40f889d2efSBrooks Davis #include <sys/systm.h>
41f889d2efSBrooks Davis #include <sys/types.h>
42f889d2efSBrooks Davis #include <sys/socket.h>
43f889d2efSBrooks Davis 
44f889d2efSBrooks Davis #include <net/if.h>
45f889d2efSBrooks Davis #include <net/if_var.h>
462c2b37adSJustin Hibbits #include <net/if_private.h>
4776039bc8SGleb Smirnoff #include <net/if_clone.h>
48f889d2efSBrooks Davis #include <net/radix.h>
49f889d2efSBrooks Davis #include <net/route.h>
5021ca7b57SMarko Zec #include <net/vnet.h>
51f889d2efSBrooks Davis 
52089104e0SAlexander V. Chernikov #include <netlink/netlink.h>
53089104e0SAlexander V. Chernikov #include <netlink/netlink_ctl.h>
54089104e0SAlexander V. Chernikov #include <netlink/netlink_route.h>
55089104e0SAlexander V. Chernikov #include <netlink/route/route_var.h>
56089104e0SAlexander V. Chernikov 
5742a58907SGleb Smirnoff /* Current IF_MAXUNIT expands maximum to 5 characters. */
5842a58907SGleb Smirnoff #define	IFCLOSIZ	(IFNAMSIZ - 5)
5942a58907SGleb Smirnoff 
6042a58907SGleb Smirnoff /*
6142a58907SGleb Smirnoff  * Structure describing a `cloning' interface.
6242a58907SGleb Smirnoff  *
6342a58907SGleb Smirnoff  * List of locks
6442a58907SGleb Smirnoff  * (c)		const until freeing
6542a58907SGleb Smirnoff  * (d)		driver specific data, may need external protection.
6642a58907SGleb Smirnoff  * (e)		locked by if_cloners_mtx
6742a58907SGleb Smirnoff  * (i)		locked by ifc_mtx mtx
6842a58907SGleb Smirnoff  */
6942a58907SGleb Smirnoff struct if_clone {
7042a58907SGleb Smirnoff 	char ifc_name[IFCLOSIZ];	/* (c) Name of device, e.g. `gif' */
7142a58907SGleb Smirnoff 	struct unrhdr *ifc_unrhdr;	/* (c) alloc_unr(9) header */
7242a58907SGleb Smirnoff 	int ifc_maxunit;		/* (c) maximum unit number */
7309f6ff4fSMatt Macy 	int ifc_flags;
7442a58907SGleb Smirnoff 	long ifc_refcnt;		/* (i) Reference count. */
7542a58907SGleb Smirnoff 	LIST_HEAD(, ifnet) ifc_iflist;	/* (i) List of cloned interfaces */
7642a58907SGleb Smirnoff 	struct mtx ifc_mtx;		/* Mutex to protect members. */
7742a58907SGleb Smirnoff 
7809ee0fc0SAlexander V. Chernikov 	ifc_match_f *ifc_match;		/* (c) Matcher function */
7909ee0fc0SAlexander V. Chernikov 	ifc_create_f *ifc_create;	/* (c) Creates new interface */
8009ee0fc0SAlexander V. Chernikov 	ifc_destroy_f *ifc_destroy;	/* (c) Destroys cloned interface */
8142a58907SGleb Smirnoff 
82089104e0SAlexander V. Chernikov 	ifc_create_nl_f	*create_nl;	/* (c) Netlink creation handler */
83089104e0SAlexander V. Chernikov 	ifc_modify_nl_f	*modify_nl;	/* (c) Netlink modification handler */
84089104e0SAlexander V. Chernikov 	ifc_dump_nl_f	*dump_nl;	/* (c) Netlink dump handler */
85089104e0SAlexander V. Chernikov 
8609ee0fc0SAlexander V. Chernikov #ifdef CLONE_COMPAT_13
8742a58907SGleb Smirnoff 	/* (c) Driver specific cloning functions.  Called with no locks held. */
8842a58907SGleb Smirnoff 	union {
8942a58907SGleb Smirnoff 		struct {	/* advanced cloner */
9042a58907SGleb Smirnoff 			ifc_create_t	*_ifc_create;
9142a58907SGleb Smirnoff 			ifc_destroy_t	*_ifc_destroy;
9242a58907SGleb Smirnoff 		} A;
9342a58907SGleb Smirnoff 		struct {	/* simple cloner */
9442a58907SGleb Smirnoff 			ifcs_create_t	*_ifcs_create;
9542a58907SGleb Smirnoff 			ifcs_destroy_t	*_ifcs_destroy;
9642a58907SGleb Smirnoff 			int		_ifcs_minifs;	/* minimum ifs */
9742a58907SGleb Smirnoff 
9842a58907SGleb Smirnoff 		} S;
9942a58907SGleb Smirnoff 	} U;
10009ee0fc0SAlexander V. Chernikov #define	ifca_create	U.A._ifc_create
10109ee0fc0SAlexander V. Chernikov #define	ifca_destroy	U.A._ifc_destroy
10242a58907SGleb Smirnoff #define	ifcs_create	U.S._ifcs_create
10342a58907SGleb Smirnoff #define	ifcs_destroy	U.S._ifcs_destroy
10442a58907SGleb Smirnoff #define	ifcs_minifs	U.S._ifcs_minifs
10509ee0fc0SAlexander V. Chernikov #endif
10642a58907SGleb Smirnoff 
10742a58907SGleb Smirnoff 	LIST_ENTRY(if_clone) ifc_list;	/* (e) On list of cloners */
10842a58907SGleb Smirnoff };
10942a58907SGleb Smirnoff 
11009ee0fc0SAlexander V. Chernikov 
11109ee0fc0SAlexander V. Chernikov 
112f889d2efSBrooks Davis static void	if_clone_free(struct if_clone *ifc);
113089104e0SAlexander V. Chernikov static int	if_clone_createif_nl(struct if_clone *ifc, const char *name,
114089104e0SAlexander V. Chernikov 		    struct ifc_data_nl *ifd);
115f889d2efSBrooks Davis 
11609ee0fc0SAlexander V. Chernikov static int ifc_simple_match(struct if_clone *ifc, const char *name);
11709ee0fc0SAlexander V. Chernikov static int ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit);
11883b5c80cSAlexander V. Chernikov static struct if_clone *ifc_find_cloner(const char *name);
11983b5c80cSAlexander V. Chernikov static struct if_clone *ifc_find_cloner_match(const char *name);
12009ee0fc0SAlexander V. Chernikov 
12109ee0fc0SAlexander V. Chernikov #ifdef CLONE_COMPAT_13
12209ee0fc0SAlexander V. Chernikov static int ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
12309ee0fc0SAlexander V. Chernikov     struct ifc_data *ifc_data, struct ifnet **ifpp);
12409ee0fc0SAlexander V. Chernikov static int ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
12509ee0fc0SAlexander V. Chernikov     struct ifc_data *ifc_data, struct ifnet **ifpp);
12609ee0fc0SAlexander V. Chernikov #endif
12742a58907SGleb Smirnoff 
128f889d2efSBrooks Davis static struct mtx if_cloners_mtx;
1294ea05db8SGleb Smirnoff MTX_SYSINIT(if_cloners_lock, &if_cloners_mtx, "if_cloners lock", MTX_DEF);
1305f901c92SAndrew Turner VNET_DEFINE_STATIC(int, if_cloners_count);
131eddfbb76SRobert Watson VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners);
132eddfbb76SRobert Watson 
1331e77c105SRobert Watson #define	V_if_cloners_count	VNET(if_cloners_count)
1341e77c105SRobert Watson #define	V_if_cloners		VNET(if_cloners)
135f889d2efSBrooks Davis 
136f889d2efSBrooks Davis #define IF_CLONERS_LOCK_ASSERT()	mtx_assert(&if_cloners_mtx, MA_OWNED)
137f889d2efSBrooks Davis #define IF_CLONERS_LOCK()		mtx_lock(&if_cloners_mtx)
138f889d2efSBrooks Davis #define IF_CLONERS_UNLOCK()		mtx_unlock(&if_cloners_mtx)
139f889d2efSBrooks Davis 
140f889d2efSBrooks Davis #define IF_CLONE_LOCK_INIT(ifc)		\
141f889d2efSBrooks Davis     mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
142f889d2efSBrooks Davis #define IF_CLONE_LOCK_DESTROY(ifc)	mtx_destroy(&(ifc)->ifc_mtx)
143f889d2efSBrooks Davis #define IF_CLONE_LOCK_ASSERT(ifc)	mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
144f889d2efSBrooks Davis #define IF_CLONE_LOCK(ifc)		mtx_lock(&(ifc)->ifc_mtx)
145f889d2efSBrooks Davis #define IF_CLONE_UNLOCK(ifc)		mtx_unlock(&(ifc)->ifc_mtx)
146f889d2efSBrooks Davis 
147f889d2efSBrooks Davis #define IF_CLONE_ADDREF(ifc)						\
148f889d2efSBrooks Davis 	do {								\
149f889d2efSBrooks Davis 		IF_CLONE_LOCK(ifc);					\
150f889d2efSBrooks Davis 		IF_CLONE_ADDREF_LOCKED(ifc);				\
151f889d2efSBrooks Davis 		IF_CLONE_UNLOCK(ifc);					\
152f889d2efSBrooks Davis 	} while (0)
153f889d2efSBrooks Davis #define IF_CLONE_ADDREF_LOCKED(ifc)					\
154f889d2efSBrooks Davis 	do {								\
155f889d2efSBrooks Davis 		IF_CLONE_LOCK_ASSERT(ifc);				\
156f889d2efSBrooks Davis 		KASSERT((ifc)->ifc_refcnt >= 0,				\
157f889d2efSBrooks Davis 		    ("negative refcnt %ld", (ifc)->ifc_refcnt));	\
158f889d2efSBrooks Davis 		(ifc)->ifc_refcnt++;					\
159f889d2efSBrooks Davis 	} while (0)
160f889d2efSBrooks Davis #define IF_CLONE_REMREF(ifc)						\
161f889d2efSBrooks Davis 	do {								\
162f889d2efSBrooks Davis 		IF_CLONE_LOCK(ifc);					\
163f889d2efSBrooks Davis 		IF_CLONE_REMREF_LOCKED(ifc);				\
164f889d2efSBrooks Davis 	} while (0)
165f889d2efSBrooks Davis #define IF_CLONE_REMREF_LOCKED(ifc)					\
166f889d2efSBrooks Davis 	do {								\
167f889d2efSBrooks Davis 		IF_CLONE_LOCK_ASSERT(ifc);				\
168f889d2efSBrooks Davis 		KASSERT((ifc)->ifc_refcnt > 0,				\
169f889d2efSBrooks Davis 		    ("bogus refcnt %ld", (ifc)->ifc_refcnt));		\
170f889d2efSBrooks Davis 		if (--(ifc)->ifc_refcnt == 0) {				\
171f889d2efSBrooks Davis 			IF_CLONE_UNLOCK(ifc);				\
172f889d2efSBrooks Davis 			if_clone_free(ifc);				\
173ca64c799SMax Laier 		} else {						\
174f889d2efSBrooks Davis 			/* silently free the lock */			\
175f889d2efSBrooks Davis 			IF_CLONE_UNLOCK(ifc);				\
176ca64c799SMax Laier 		}							\
177f889d2efSBrooks Davis 	} while (0)
178f889d2efSBrooks Davis 
1794e7e0183SAndrew Thompson #define IFC_IFLIST_INSERT(_ifc, _ifp)					\
1804e7e0183SAndrew Thompson 	LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones)
1814e7e0183SAndrew Thompson #define IFC_IFLIST_REMOVE(_ifc, _ifp)					\
1824e7e0183SAndrew Thompson 	LIST_REMOVE(_ifp, if_clones)
1834e7e0183SAndrew Thompson 
184c711aea6SPoul-Henning Kamp static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
185f889d2efSBrooks Davis 
186d0728d71SRobert Watson void
vnet_if_clone_init(void)187d0728d71SRobert Watson vnet_if_clone_init(void)
18837f17770SMarko Zec {
18937f17770SMarko Zec 
19037f17770SMarko Zec 	LIST_INIT(&V_if_cloners);
19137f17770SMarko Zec }
19237f17770SMarko Zec 
193f889d2efSBrooks Davis /*
1944e7e0183SAndrew Thompson  * Lookup and create a clone network interface.
195f889d2efSBrooks Davis  */
196f889d2efSBrooks Davis int
ifc_create_ifp(const char * name,struct ifc_data * ifd,struct ifnet ** ifpp)197089104e0SAlexander V. Chernikov ifc_create_ifp(const char *name, struct ifc_data *ifd, struct ifnet **ifpp)
198f889d2efSBrooks Davis {
199089104e0SAlexander V. Chernikov 	struct if_clone *ifc = ifc_find_cloner_match(name);
200f889d2efSBrooks Davis 
201f889d2efSBrooks Davis 	if (ifc == NULL)
202f889d2efSBrooks Davis 		return (EINVAL);
203f889d2efSBrooks Davis 
204089104e0SAlexander V. Chernikov 	struct ifc_data_nl ifd_new = {
205089104e0SAlexander V. Chernikov 		.flags = ifd->flags,
206089104e0SAlexander V. Chernikov 		.unit = ifd->unit,
207089104e0SAlexander V. Chernikov 		.params = ifd->params,
208089104e0SAlexander V. Chernikov 	};
209089104e0SAlexander V. Chernikov 
210089104e0SAlexander V. Chernikov 	int error = if_clone_createif_nl(ifc, name, &ifd_new);
211089104e0SAlexander V. Chernikov 
21209ee0fc0SAlexander V. Chernikov 	if (ifpp != NULL)
213089104e0SAlexander V. Chernikov 		*ifpp = ifd_new.ifp;
21409ee0fc0SAlexander V. Chernikov 
21509ee0fc0SAlexander V. Chernikov 	return (error);
21609ee0fc0SAlexander V. Chernikov }
21709ee0fc0SAlexander V. Chernikov 
218089104e0SAlexander V. Chernikov bool
ifc_create_ifp_nl(const char * name,struct ifc_data_nl * ifd)219089104e0SAlexander V. Chernikov ifc_create_ifp_nl(const char *name, struct ifc_data_nl *ifd)
220089104e0SAlexander V. Chernikov {
221089104e0SAlexander V. Chernikov 	struct if_clone *ifc = ifc_find_cloner_match(name);
222089104e0SAlexander V. Chernikov 	if (ifc == NULL) {
223089104e0SAlexander V. Chernikov 		ifd->error = EINVAL;
224089104e0SAlexander V. Chernikov 		return (false);
225089104e0SAlexander V. Chernikov 	}
226089104e0SAlexander V. Chernikov 
227089104e0SAlexander V. Chernikov 	ifd->error = if_clone_createif_nl(ifc, name, ifd);
228089104e0SAlexander V. Chernikov 
229089104e0SAlexander V. Chernikov 	return (true);
230089104e0SAlexander V. Chernikov }
231089104e0SAlexander V. Chernikov 
23209ee0fc0SAlexander V. Chernikov int
if_clone_create(char * name,size_t len,caddr_t params)23309ee0fc0SAlexander V. Chernikov if_clone_create(char *name, size_t len, caddr_t params)
23409ee0fc0SAlexander V. Chernikov {
23509ee0fc0SAlexander V. Chernikov 	struct ifc_data ifd = { .params = params };
23609ee0fc0SAlexander V. Chernikov 	struct ifnet *ifp;
23709ee0fc0SAlexander V. Chernikov 
23809ee0fc0SAlexander V. Chernikov 	int error = ifc_create_ifp(name, &ifd, &ifp);
23909ee0fc0SAlexander V. Chernikov 
24009ee0fc0SAlexander V. Chernikov 	if (error == 0)
24109ee0fc0SAlexander V. Chernikov 		strlcpy(name, if_name(ifp), len);
24209ee0fc0SAlexander V. Chernikov 
24309ee0fc0SAlexander V. Chernikov 	return (error);
2444e7e0183SAndrew Thompson }
2454e7e0183SAndrew Thompson 
246089104e0SAlexander V. Chernikov bool
ifc_modify_ifp_nl(struct ifnet * ifp,struct ifc_data_nl * ifd)247089104e0SAlexander V. Chernikov ifc_modify_ifp_nl(struct ifnet *ifp, struct ifc_data_nl *ifd)
248089104e0SAlexander V. Chernikov {
249089104e0SAlexander V. Chernikov 	struct if_clone *ifc = ifc_find_cloner(ifp->if_dname);
250089104e0SAlexander V. Chernikov 	if (ifc == NULL) {
251089104e0SAlexander V. Chernikov 		ifd->error = EINVAL;
252089104e0SAlexander V. Chernikov 		return (false);
253089104e0SAlexander V. Chernikov 	}
254089104e0SAlexander V. Chernikov 
255089104e0SAlexander V. Chernikov 	ifd->error = (*ifc->modify_nl)(ifp, ifd);
256089104e0SAlexander V. Chernikov 	return (true);
257089104e0SAlexander V. Chernikov }
258089104e0SAlexander V. Chernikov 
259089104e0SAlexander V. Chernikov bool
ifc_dump_ifp_nl(struct ifnet * ifp,struct nl_writer * nw)260089104e0SAlexander V. Chernikov ifc_dump_ifp_nl(struct ifnet *ifp, struct nl_writer *nw)
261089104e0SAlexander V. Chernikov {
262089104e0SAlexander V. Chernikov 	struct if_clone *ifc = ifc_find_cloner(ifp->if_dname);
263089104e0SAlexander V. Chernikov 	if (ifc == NULL)
264089104e0SAlexander V. Chernikov 		return (false);
265089104e0SAlexander V. Chernikov 
266089104e0SAlexander V. Chernikov 	(*ifc->dump_nl)(ifp, nw);
267089104e0SAlexander V. Chernikov 	return (true);
268089104e0SAlexander V. Chernikov }
269089104e0SAlexander V. Chernikov 
270089104e0SAlexander V. Chernikov static int
ifc_create_ifp_nl_default(struct if_clone * ifc,char * name,size_t len,struct ifc_data_nl * ifd)271089104e0SAlexander V. Chernikov ifc_create_ifp_nl_default(struct if_clone *ifc, char *name, size_t len,
272089104e0SAlexander V. Chernikov     struct ifc_data_nl *ifd)
273089104e0SAlexander V. Chernikov {
274089104e0SAlexander V. Chernikov 	struct ifc_data ifd_new = {
275089104e0SAlexander V. Chernikov 		.flags = ifd->flags,
276089104e0SAlexander V. Chernikov 		.unit = ifd->unit,
277089104e0SAlexander V. Chernikov 		.params = ifd->params,
278089104e0SAlexander V. Chernikov 	};
279089104e0SAlexander V. Chernikov 
280089104e0SAlexander V. Chernikov 	return ((*ifc->ifc_create)(ifc, name, len, &ifd_new, &ifd->ifp));
281089104e0SAlexander V. Chernikov }
282089104e0SAlexander V. Chernikov 
283089104e0SAlexander V. Chernikov static int
ifc_modify_ifp_nl_default(struct ifnet * ifp,struct ifc_data_nl * ifd)284089104e0SAlexander V. Chernikov ifc_modify_ifp_nl_default(struct ifnet *ifp, struct ifc_data_nl *ifd)
285089104e0SAlexander V. Chernikov {
286089104e0SAlexander V. Chernikov 	if (ifd->lattrs != NULL)
287089104e0SAlexander V. Chernikov 		return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt));
288089104e0SAlexander V. Chernikov 	return (0);
289089104e0SAlexander V. Chernikov }
290089104e0SAlexander V. Chernikov 
291089104e0SAlexander V. Chernikov static void
ifc_dump_ifp_nl_default(struct ifnet * ifp,struct nl_writer * nw)292089104e0SAlexander V. Chernikov ifc_dump_ifp_nl_default(struct ifnet *ifp, struct nl_writer *nw)
293089104e0SAlexander V. Chernikov {
294089104e0SAlexander V. Chernikov 	int off = nlattr_add_nested(nw, IFLA_LINKINFO);
295089104e0SAlexander V. Chernikov 
296089104e0SAlexander V. Chernikov 	if (off != 0) {
297089104e0SAlexander V. Chernikov 		nlattr_add_string(nw, IFLA_INFO_KIND, ifp->if_dname);
298089104e0SAlexander V. Chernikov 		nlattr_set_len(nw, off);
299089104e0SAlexander V. Chernikov 	}
300089104e0SAlexander V. Chernikov }
301089104e0SAlexander V. Chernikov 
302b02fd8b7SKristof Provost void
ifc_link_ifp(struct if_clone * ifc,struct ifnet * ifp)30326c190d2SAlexander V. Chernikov ifc_link_ifp(struct if_clone *ifc, struct ifnet *ifp)
304b02fd8b7SKristof Provost {
305b02fd8b7SKristof Provost 
306b02fd8b7SKristof Provost 	if_addgroup(ifp, ifc->ifc_name);
307b02fd8b7SKristof Provost 
308b02fd8b7SKristof Provost 	IF_CLONE_LOCK(ifc);
309b02fd8b7SKristof Provost 	IFC_IFLIST_INSERT(ifc, ifp);
310b02fd8b7SKristof Provost 	IF_CLONE_UNLOCK(ifc);
311b02fd8b7SKristof Provost }
312b02fd8b7SKristof Provost 
31326c190d2SAlexander V. Chernikov void
if_clone_addif(struct if_clone * ifc,struct ifnet * ifp)31426c190d2SAlexander V. Chernikov if_clone_addif(struct if_clone *ifc, struct ifnet *ifp)
31526c190d2SAlexander V. Chernikov {
31626c190d2SAlexander V. Chernikov 	ifc_link_ifp(ifc, ifp);
31726c190d2SAlexander V. Chernikov }
31826c190d2SAlexander V. Chernikov 
31926c190d2SAlexander V. Chernikov bool
ifc_unlink_ifp(struct if_clone * ifc,struct ifnet * ifp)32026c190d2SAlexander V. Chernikov ifc_unlink_ifp(struct if_clone *ifc, struct ifnet *ifp)
32126c190d2SAlexander V. Chernikov {
32226c190d2SAlexander V. Chernikov 	struct ifnet *ifcifp;
32326c190d2SAlexander V. Chernikov 
32426c190d2SAlexander V. Chernikov 	IF_CLONE_LOCK(ifc);
32526c190d2SAlexander V. Chernikov 	LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
32626c190d2SAlexander V. Chernikov 		if (ifcifp == ifp) {
32726c190d2SAlexander V. Chernikov 			IFC_IFLIST_REMOVE(ifc, ifp);
32826c190d2SAlexander V. Chernikov 			break;
32926c190d2SAlexander V. Chernikov 		}
33026c190d2SAlexander V. Chernikov 	}
33126c190d2SAlexander V. Chernikov 	IF_CLONE_UNLOCK(ifc);
33226c190d2SAlexander V. Chernikov 
3337ff9ae90SMarius Strobl 	if (ifcifp != NULL)
33426c190d2SAlexander V. Chernikov 		if_delgroup(ifp, ifc->ifc_name);
33526c190d2SAlexander V. Chernikov 
33626c190d2SAlexander V. Chernikov 	return (ifcifp != NULL);
33726c190d2SAlexander V. Chernikov }
33826c190d2SAlexander V. Chernikov 
33926c190d2SAlexander V. Chernikov static struct if_clone *
ifc_find_cloner_match(const char * name)34083b5c80cSAlexander V. Chernikov ifc_find_cloner_match(const char *name)
34126c190d2SAlexander V. Chernikov {
34226c190d2SAlexander V. Chernikov 	struct if_clone *ifc;
34326c190d2SAlexander V. Chernikov 
34483b5c80cSAlexander V. Chernikov 	IF_CLONERS_LOCK();
34583b5c80cSAlexander V. Chernikov 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
34683b5c80cSAlexander V. Chernikov 		if (ifc->ifc_match(ifc, name))
34783b5c80cSAlexander V. Chernikov 			break;
34883b5c80cSAlexander V. Chernikov 	}
34983b5c80cSAlexander V. Chernikov 	IF_CLONERS_UNLOCK();
35083b5c80cSAlexander V. Chernikov 
35183b5c80cSAlexander V. Chernikov 	return (ifc);
35283b5c80cSAlexander V. Chernikov }
35383b5c80cSAlexander V. Chernikov 
35483b5c80cSAlexander V. Chernikov static struct if_clone *
ifc_find_cloner(const char * name)35583b5c80cSAlexander V. Chernikov ifc_find_cloner(const char *name)
35683b5c80cSAlexander V. Chernikov {
35783b5c80cSAlexander V. Chernikov 	struct if_clone *ifc;
35883b5c80cSAlexander V. Chernikov 
35926c190d2SAlexander V. Chernikov 	IF_CLONERS_LOCK();
36026c190d2SAlexander V. Chernikov 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
36126c190d2SAlexander V. Chernikov 		if (strcmp(ifc->ifc_name, name) == 0) {
36226c190d2SAlexander V. Chernikov 			break;
36326c190d2SAlexander V. Chernikov 		}
36426c190d2SAlexander V. Chernikov 	}
36526c190d2SAlexander V. Chernikov 	IF_CLONERS_UNLOCK();
36683b5c80cSAlexander V. Chernikov 
36783b5c80cSAlexander V. Chernikov 	return (ifc);
36883b5c80cSAlexander V. Chernikov }
36983b5c80cSAlexander V. Chernikov 
37083b5c80cSAlexander V. Chernikov static struct if_clone *
ifc_find_cloner_in_vnet(const char * name,struct vnet * vnet)37183b5c80cSAlexander V. Chernikov ifc_find_cloner_in_vnet(const char *name, struct vnet *vnet)
37283b5c80cSAlexander V. Chernikov {
37383b5c80cSAlexander V. Chernikov 	CURVNET_SET_QUIET(vnet);
37483b5c80cSAlexander V. Chernikov 	struct if_clone *ifc = ifc_find_cloner(name);
37526c190d2SAlexander V. Chernikov 	CURVNET_RESTORE();
37626c190d2SAlexander V. Chernikov 
37726c190d2SAlexander V. Chernikov 	return (ifc);
37826c190d2SAlexander V. Chernikov }
37926c190d2SAlexander V. Chernikov 
3804e7e0183SAndrew Thompson /*
3814e7e0183SAndrew Thompson  * Create a clone network interface.
3824e7e0183SAndrew Thompson  */
3834e7e0183SAndrew Thompson static int
if_clone_createif_nl(struct if_clone * ifc,const char * ifname,struct ifc_data_nl * ifd)384089104e0SAlexander V. Chernikov if_clone_createif_nl(struct if_clone *ifc, const char *ifname, struct ifc_data_nl *ifd)
3854e7e0183SAndrew Thompson {
386089104e0SAlexander V. Chernikov 	char name[IFNAMSIZ];
387089104e0SAlexander V. Chernikov 	int error;
388089104e0SAlexander V. Chernikov 
389089104e0SAlexander V. Chernikov 	strlcpy(name, ifname, sizeof(name));
3904e7e0183SAndrew Thompson 
3914e7e0183SAndrew Thompson 	if (ifunit(name) != NULL)
3924e7e0183SAndrew Thompson 		return (EEXIST);
3934e7e0183SAndrew Thompson 
39409ee0fc0SAlexander V. Chernikov 	if (ifc->ifc_flags & IFC_F_AUTOUNIT) {
395089104e0SAlexander V. Chernikov 		if ((error = ifc_handle_unit(ifc, name, sizeof(name), &ifd->unit)) != 0)
396089104e0SAlexander V. Chernikov 			return (error);
3974e7e0183SAndrew Thompson 	}
39809ee0fc0SAlexander V. Chernikov 
399089104e0SAlexander V. Chernikov 	if (ifd->lattrs != NULL)
400089104e0SAlexander V. Chernikov 		error = (*ifc->create_nl)(ifc, name, sizeof(name), ifd);
401089104e0SAlexander V. Chernikov 	else
402089104e0SAlexander V. Chernikov 		error = ifc_create_ifp_nl_default(ifc, name, sizeof(name), ifd);
403089104e0SAlexander V. Chernikov 	if (error != 0) {
404089104e0SAlexander V. Chernikov 		if (ifc->ifc_flags & IFC_F_AUTOUNIT)
405089104e0SAlexander V. Chernikov 			ifc_free_unit(ifc, ifd->unit);
406089104e0SAlexander V. Chernikov 		return (error);
407089104e0SAlexander V. Chernikov 	}
4084e7e0183SAndrew Thompson 
409089104e0SAlexander V. Chernikov 	MPASS(ifd->ifp != NULL);
410089104e0SAlexander V. Chernikov 	if_clone_addif(ifc, ifd->ifp);
411089104e0SAlexander V. Chernikov 
412089104e0SAlexander V. Chernikov 	if (ifd->lattrs != NULL)
413089104e0SAlexander V. Chernikov 		error = (*ifc->modify_nl)(ifd->ifp, ifd);
414089104e0SAlexander V. Chernikov 
415089104e0SAlexander V. Chernikov 	return (error);
416f889d2efSBrooks Davis }
417f889d2efSBrooks Davis 
418f889d2efSBrooks Davis /*
4194e7e0183SAndrew Thompson  * Lookup and destroy a clone network interface.
420f889d2efSBrooks Davis  */
421f889d2efSBrooks Davis int
if_clone_destroy(const char * name)422f889d2efSBrooks Davis if_clone_destroy(const char *name)
423f889d2efSBrooks Davis {
424d0088cdeSBjoern A. Zeeb 	int err;
425f889d2efSBrooks Davis 	struct if_clone *ifc;
426f889d2efSBrooks Davis 	struct ifnet *ifp;
427f889d2efSBrooks Davis 
428d0088cdeSBjoern A. Zeeb 	ifp = ifunit_ref(name);
429f889d2efSBrooks Davis 	if (ifp == NULL)
430f889d2efSBrooks Davis 		return (ENXIO);
431f889d2efSBrooks Davis 
43283b5c80cSAlexander V. Chernikov 	ifc = ifc_find_cloner_in_vnet(ifp->if_dname, ifp->if_home_vnet);
433d0088cdeSBjoern A. Zeeb 	if (ifc == NULL) {
434d0088cdeSBjoern A. Zeeb 		if_rele(ifp);
435f889d2efSBrooks Davis 		return (EINVAL);
436d0088cdeSBjoern A. Zeeb 	}
437f889d2efSBrooks Davis 
438d0088cdeSBjoern A. Zeeb 	err = if_clone_destroyif(ifc, ifp);
439d0088cdeSBjoern A. Zeeb 	if_rele(ifp);
440d0088cdeSBjoern A. Zeeb 	return err;
4414e7e0183SAndrew Thompson }
4424e7e0183SAndrew Thompson 
4434e7e0183SAndrew Thompson /*
4444e7e0183SAndrew Thompson  * Destroy a clone network interface.
4454e7e0183SAndrew Thompson  */
44609ee0fc0SAlexander V. Chernikov static int
if_clone_destroyif_flags(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)44709ee0fc0SAlexander V. Chernikov if_clone_destroyif_flags(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
4484e7e0183SAndrew Thompson {
4494e7e0183SAndrew Thompson 	int err;
4504e7e0183SAndrew Thompson 
45137f17770SMarko Zec 	/*
45237f17770SMarko Zec 	 * Given that the cloned ifnet might be attached to a different
45337f17770SMarko Zec 	 * vnet from where its cloner was registered, we have to
45437f17770SMarko Zec 	 * switch to the vnet context of the target vnet.
45537f17770SMarko Zec 	 */
45637f17770SMarko Zec 	CURVNET_SET_QUIET(ifp->if_vnet);
45737f17770SMarko Zec 
45826c190d2SAlexander V. Chernikov 	if (!ifc_unlink_ifp(ifc, ifp)) {
459c769e1beSBjoern A. Zeeb 		CURVNET_RESTORE();
460c769e1beSBjoern A. Zeeb 		return (ENXIO);		/* ifp is not on the list. */
461c769e1beSBjoern A. Zeeb 	}
4620dad3f0eSMax Laier 
46309ee0fc0SAlexander V. Chernikov 	int unit = ifp->if_dunit;
46409ee0fc0SAlexander V. Chernikov 	err = (*ifc->ifc_destroy)(ifc, ifp, flags);
465f889d2efSBrooks Davis 
46626c190d2SAlexander V. Chernikov 	if (err != 0)
46726c190d2SAlexander V. Chernikov 		ifc_link_ifp(ifc, ifp);
46826c190d2SAlexander V. Chernikov 	else if (ifc->ifc_flags & IFC_F_AUTOUNIT)
46909ee0fc0SAlexander V. Chernikov 		ifc_free_unit(ifc, unit);
47021ca7b57SMarko Zec 	CURVNET_RESTORE();
471f889d2efSBrooks Davis 	return (err);
472f889d2efSBrooks Davis }
473f889d2efSBrooks Davis 
47409ee0fc0SAlexander V. Chernikov int
if_clone_destroyif(struct if_clone * ifc,struct ifnet * ifp)47509ee0fc0SAlexander V. Chernikov if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
47609ee0fc0SAlexander V. Chernikov {
47709ee0fc0SAlexander V. Chernikov 	return (if_clone_destroyif_flags(ifc, ifp, 0));
47809ee0fc0SAlexander V. Chernikov }
47909ee0fc0SAlexander V. Chernikov 
48042a58907SGleb Smirnoff static struct if_clone *
if_clone_alloc(const char * name,int maxunit)48142a58907SGleb Smirnoff if_clone_alloc(const char *name, int maxunit)
48242a58907SGleb Smirnoff {
48342a58907SGleb Smirnoff 	struct if_clone *ifc;
48442a58907SGleb Smirnoff 
48542a58907SGleb Smirnoff 	KASSERT(name != NULL, ("%s: no name\n", __func__));
48642a58907SGleb Smirnoff 
48742a58907SGleb Smirnoff 	ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO);
48842a58907SGleb Smirnoff 	strncpy(ifc->ifc_name, name, IFCLOSIZ-1);
48942a58907SGleb Smirnoff 	IF_CLONE_LOCK_INIT(ifc);
49042a58907SGleb Smirnoff 	IF_CLONE_ADDREF(ifc);
49142a58907SGleb Smirnoff 	ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT;
49242a58907SGleb Smirnoff 	ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx);
49342a58907SGleb Smirnoff 	LIST_INIT(&ifc->ifc_iflist);
49442a58907SGleb Smirnoff 
495089104e0SAlexander V. Chernikov 	ifc->create_nl = ifc_create_ifp_nl_default;
496089104e0SAlexander V. Chernikov 	ifc->modify_nl = ifc_modify_ifp_nl_default;
497089104e0SAlexander V. Chernikov 	ifc->dump_nl = ifc_dump_ifp_nl_default;
498089104e0SAlexander V. Chernikov 
49942a58907SGleb Smirnoff 	return (ifc);
50042a58907SGleb Smirnoff }
50142a58907SGleb Smirnoff 
50242a58907SGleb Smirnoff static int
if_clone_attach(struct if_clone * ifc)503f889d2efSBrooks Davis if_clone_attach(struct if_clone *ifc)
504f889d2efSBrooks Davis {
5052e9fff5bSGleb Smirnoff 	struct if_clone *ifc1;
506f889d2efSBrooks Davis 
507f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
5082e9fff5bSGleb Smirnoff 	LIST_FOREACH(ifc1, &V_if_cloners, ifc_list)
5092e9fff5bSGleb Smirnoff 		if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) {
5102e9fff5bSGleb Smirnoff 			IF_CLONERS_UNLOCK();
5112e9fff5bSGleb Smirnoff 			IF_CLONE_REMREF(ifc);
5122e9fff5bSGleb Smirnoff 			return (EEXIST);
5132e9fff5bSGleb Smirnoff 		}
51437f17770SMarko Zec 	LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
51537f17770SMarko Zec 	V_if_cloners_count++;
516f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
517f889d2efSBrooks Davis 
51842a58907SGleb Smirnoff 	return (0);
51942a58907SGleb Smirnoff }
52042a58907SGleb Smirnoff 
52142a58907SGleb Smirnoff struct if_clone *
ifc_attach_cloner(const char * name,struct if_clone_addreq * req)52209ee0fc0SAlexander V. Chernikov ifc_attach_cloner(const char *name, struct if_clone_addreq *req)
52342a58907SGleb Smirnoff {
52409ee0fc0SAlexander V. Chernikov 	if (req->create_f == NULL || req->destroy_f == NULL)
52509ee0fc0SAlexander V. Chernikov 		return (NULL);
52609ee0fc0SAlexander V. Chernikov 	if (strnlen(name, IFCLOSIZ) >= (IFCLOSIZ - 1))
52709ee0fc0SAlexander V. Chernikov 		return (NULL);
52842a58907SGleb Smirnoff 
52909ee0fc0SAlexander V. Chernikov 	struct if_clone *ifc = if_clone_alloc(name, req->maxunit);
53009ee0fc0SAlexander V. Chernikov 	ifc->ifc_match = req->match_f != NULL ? req->match_f : ifc_simple_match;
53109ee0fc0SAlexander V. Chernikov 	ifc->ifc_create = req->create_f;
53209ee0fc0SAlexander V. Chernikov 	ifc->ifc_destroy = req->destroy_f;
5337ff9ae90SMarius Strobl 	ifc->ifc_flags = (req->flags & IFC_F_AUTOUNIT);
53442a58907SGleb Smirnoff 
535089104e0SAlexander V. Chernikov 	if (req->version == 2) {
536089104e0SAlexander V. Chernikov 		struct if_clone_addreq_v2 *req2 = (struct if_clone_addreq_v2 *)req;
537089104e0SAlexander V. Chernikov 
538089104e0SAlexander V. Chernikov 		ifc->create_nl = req2->create_nl_f;
539089104e0SAlexander V. Chernikov 		ifc->modify_nl = req2->modify_nl_f;
540089104e0SAlexander V. Chernikov 		ifc->dump_nl = req2->dump_nl_f;
541089104e0SAlexander V. Chernikov 	}
542089104e0SAlexander V. Chernikov 
543089104e0SAlexander V. Chernikov 	ifc->dump_nl = ifc_dump_ifp_nl_default;
544089104e0SAlexander V. Chernikov 
5453395dd6eSAlexander Kabaev 	if (if_clone_attach(ifc) != 0)
54642a58907SGleb Smirnoff 		return (NULL);
54742a58907SGleb Smirnoff 
548f889d2efSBrooks Davis 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
5492e9fff5bSGleb Smirnoff 
55042a58907SGleb Smirnoff 	return (ifc);
55142a58907SGleb Smirnoff }
55242a58907SGleb Smirnoff 
55309ee0fc0SAlexander V. Chernikov void
ifc_detach_cloner(struct if_clone * ifc)55409ee0fc0SAlexander V. Chernikov ifc_detach_cloner(struct if_clone *ifc)
55509ee0fc0SAlexander V. Chernikov {
55609ee0fc0SAlexander V. Chernikov 	if_clone_detach(ifc);
55709ee0fc0SAlexander V. Chernikov }
55809ee0fc0SAlexander V. Chernikov 
55909ee0fc0SAlexander V. Chernikov 
56009ee0fc0SAlexander V. Chernikov #ifdef CLONE_COMPAT_13
56109ee0fc0SAlexander V. Chernikov 
56209ee0fc0SAlexander V. Chernikov static int
ifc_advanced_create_wrapper(struct if_clone * ifc,char * name,size_t maxlen,struct ifc_data * ifc_data,struct ifnet ** ifpp)56309ee0fc0SAlexander V. Chernikov ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
56409ee0fc0SAlexander V. Chernikov     struct ifc_data *ifc_data, struct ifnet **ifpp)
56509ee0fc0SAlexander V. Chernikov {
56609ee0fc0SAlexander V. Chernikov 	int error = ifc->ifca_create(ifc, name, maxlen, ifc_data->params);
56709ee0fc0SAlexander V. Chernikov 
56809ee0fc0SAlexander V. Chernikov 	if (error == 0)
56909ee0fc0SAlexander V. Chernikov 		*ifpp = ifunit(name);
57009ee0fc0SAlexander V. Chernikov 	return (error);
57109ee0fc0SAlexander V. Chernikov }
57209ee0fc0SAlexander V. Chernikov 
57309ee0fc0SAlexander V. Chernikov static int
ifc_advanced_destroy_wrapper(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)57409ee0fc0SAlexander V. Chernikov ifc_advanced_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
57509ee0fc0SAlexander V. Chernikov {
57609ee0fc0SAlexander V. Chernikov 	if (ifc->ifca_destroy == NULL)
57709ee0fc0SAlexander V. Chernikov 		return (ENOTSUP);
57809ee0fc0SAlexander V. Chernikov 	return (ifc->ifca_destroy(ifc, ifp));
57909ee0fc0SAlexander V. Chernikov }
58009ee0fc0SAlexander V. Chernikov 
58109ee0fc0SAlexander V. Chernikov struct if_clone *
if_clone_advanced(const char * name,u_int maxunit,ifc_match_t match,ifc_create_t create,ifc_destroy_t destroy)58209ee0fc0SAlexander V. Chernikov if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
58309ee0fc0SAlexander V. Chernikov 	ifc_create_t create, ifc_destroy_t destroy)
58409ee0fc0SAlexander V. Chernikov {
58509ee0fc0SAlexander V. Chernikov 	struct if_clone *ifc;
58609ee0fc0SAlexander V. Chernikov 
58709ee0fc0SAlexander V. Chernikov 	ifc = if_clone_alloc(name, maxunit);
58809ee0fc0SAlexander V. Chernikov 	ifc->ifc_match = match;
58909ee0fc0SAlexander V. Chernikov 	ifc->ifc_create = ifc_advanced_create_wrapper;
59009ee0fc0SAlexander V. Chernikov 	ifc->ifc_destroy = ifc_advanced_destroy_wrapper;
59109ee0fc0SAlexander V. Chernikov 	ifc->ifca_destroy = destroy;
59209ee0fc0SAlexander V. Chernikov 	ifc->ifca_create = create;
59309ee0fc0SAlexander V. Chernikov 
59409ee0fc0SAlexander V. Chernikov 	if (if_clone_attach(ifc) != 0)
59509ee0fc0SAlexander V. Chernikov 		return (NULL);
59609ee0fc0SAlexander V. Chernikov 
59709ee0fc0SAlexander V. Chernikov 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
59809ee0fc0SAlexander V. Chernikov 
59909ee0fc0SAlexander V. Chernikov 	return (ifc);
60009ee0fc0SAlexander V. Chernikov }
60109ee0fc0SAlexander V. Chernikov 
60209ee0fc0SAlexander V. Chernikov static int
ifc_simple_create_wrapper(struct if_clone * ifc,char * name,size_t maxlen,struct ifc_data * ifc_data,struct ifnet ** ifpp)60309ee0fc0SAlexander V. Chernikov ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
60409ee0fc0SAlexander V. Chernikov     struct ifc_data *ifc_data, struct ifnet **ifpp)
60509ee0fc0SAlexander V. Chernikov {
60609ee0fc0SAlexander V. Chernikov 	int unit = 0;
60709ee0fc0SAlexander V. Chernikov 
60809ee0fc0SAlexander V. Chernikov 	ifc_name2unit(name, &unit);
60909ee0fc0SAlexander V. Chernikov 	int error = ifc->ifcs_create(ifc, unit, ifc_data->params);
61009ee0fc0SAlexander V. Chernikov 	if (error == 0)
61109ee0fc0SAlexander V. Chernikov 		*ifpp = ifunit(name);
61209ee0fc0SAlexander V. Chernikov 	return (error);
61309ee0fc0SAlexander V. Chernikov }
61409ee0fc0SAlexander V. Chernikov 
61509ee0fc0SAlexander V. Chernikov static int
ifc_simple_destroy_wrapper(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)61609ee0fc0SAlexander V. Chernikov ifc_simple_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
61709ee0fc0SAlexander V. Chernikov {
61809ee0fc0SAlexander V. Chernikov 	if (ifp->if_dunit < ifc->ifcs_minifs && (flags & IFC_F_FORCE) == 0)
61909ee0fc0SAlexander V. Chernikov 		return (EINVAL);
62009ee0fc0SAlexander V. Chernikov 
62109ee0fc0SAlexander V. Chernikov 	ifc->ifcs_destroy(ifp);
62209ee0fc0SAlexander V. Chernikov 	return (0);
62309ee0fc0SAlexander V. Chernikov }
62409ee0fc0SAlexander V. Chernikov 
62542a58907SGleb Smirnoff struct if_clone *
if_clone_simple(const char * name,ifcs_create_t create,ifcs_destroy_t destroy,u_int minifs)62642a58907SGleb Smirnoff if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
62742a58907SGleb Smirnoff 	u_int minifs)
62842a58907SGleb Smirnoff {
62942a58907SGleb Smirnoff 	struct if_clone *ifc;
63042a58907SGleb Smirnoff 	u_int unit;
63142a58907SGleb Smirnoff 
63242a58907SGleb Smirnoff 	ifc = if_clone_alloc(name, 0);
63309ee0fc0SAlexander V. Chernikov 	ifc->ifc_match = ifc_simple_match;
63409ee0fc0SAlexander V. Chernikov 	ifc->ifc_create = ifc_simple_create_wrapper;
63509ee0fc0SAlexander V. Chernikov 	ifc->ifc_destroy = ifc_simple_destroy_wrapper;
63642a58907SGleb Smirnoff 	ifc->ifcs_create = create;
63742a58907SGleb Smirnoff 	ifc->ifcs_destroy = destroy;
63842a58907SGleb Smirnoff 	ifc->ifcs_minifs = minifs;
63909ee0fc0SAlexander V. Chernikov 	ifc->ifc_flags = IFC_F_AUTOUNIT;
64042a58907SGleb Smirnoff 
6413395dd6eSAlexander Kabaev 	if (if_clone_attach(ifc) != 0)
64242a58907SGleb Smirnoff 		return (NULL);
64342a58907SGleb Smirnoff 
64442a58907SGleb Smirnoff 	for (unit = 0; unit < minifs; unit++) {
64542a58907SGleb Smirnoff 		char name[IFNAMSIZ];
64646d0f824SMatt Macy 		int error __unused;
647089104e0SAlexander V. Chernikov 		struct ifc_data_nl ifd = {};
64842a58907SGleb Smirnoff 
64942a58907SGleb Smirnoff 		snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
650089104e0SAlexander V. Chernikov 		error = if_clone_createif_nl(ifc, name, &ifd);
65142a58907SGleb Smirnoff 		KASSERT(error == 0,
65242a58907SGleb Smirnoff 		    ("%s: failed to create required interface %s",
65342a58907SGleb Smirnoff 		    __func__, name));
65442a58907SGleb Smirnoff 	}
65542a58907SGleb Smirnoff 
65642a58907SGleb Smirnoff 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
65742a58907SGleb Smirnoff 
65842a58907SGleb Smirnoff 	return (ifc);
659f889d2efSBrooks Davis }
66009ee0fc0SAlexander V. Chernikov #endif
661f889d2efSBrooks Davis 
662f889d2efSBrooks Davis /*
663f889d2efSBrooks Davis  * Unregister a network interface cloner.
664f889d2efSBrooks Davis  */
665f889d2efSBrooks Davis void
if_clone_detach(struct if_clone * ifc)666f889d2efSBrooks Davis if_clone_detach(struct if_clone *ifc)
667f889d2efSBrooks Davis {
668f889d2efSBrooks Davis 
669f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
670f889d2efSBrooks Davis 	LIST_REMOVE(ifc, ifc_list);
67137f17770SMarko Zec 	V_if_cloners_count--;
672f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
673f889d2efSBrooks Davis 
6744e7e0183SAndrew Thompson 	/* destroy all interfaces for this cloner */
6754e7e0183SAndrew Thompson 	while (!LIST_EMPTY(&ifc->ifc_iflist))
67609ee0fc0SAlexander V. Chernikov 		if_clone_destroyif_flags(ifc, LIST_FIRST(&ifc->ifc_iflist), IFC_F_FORCE);
6774e7e0183SAndrew Thompson 
678f889d2efSBrooks Davis 	IF_CLONE_REMREF(ifc);
679f889d2efSBrooks Davis }
680f889d2efSBrooks Davis 
681f889d2efSBrooks Davis static void
if_clone_free(struct if_clone * ifc)682f889d2efSBrooks Davis if_clone_free(struct if_clone *ifc)
683f889d2efSBrooks Davis {
684f889d2efSBrooks Davis 
6854e7e0183SAndrew Thompson 	KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
6864e7e0183SAndrew Thompson 	    ("%s: ifc_iflist not empty", __func__));
6874e7e0183SAndrew Thompson 
688f889d2efSBrooks Davis 	IF_CLONE_LOCK_DESTROY(ifc);
6892e9fff5bSGleb Smirnoff 	delete_unrhdr(ifc->ifc_unrhdr);
69042a58907SGleb Smirnoff 	free(ifc, M_CLONE);
691f889d2efSBrooks Davis }
692f889d2efSBrooks Davis 
693f889d2efSBrooks Davis /*
694f889d2efSBrooks Davis  * Provide list of interface cloners to userspace.
695f889d2efSBrooks Davis  */
696f889d2efSBrooks Davis int
if_clone_list(struct if_clonereq * ifcr)697f889d2efSBrooks Davis if_clone_list(struct if_clonereq *ifcr)
698f889d2efSBrooks Davis {
699c859ef97SBrooks Davis 	char *buf, *dst, *outbuf = NULL;
700f889d2efSBrooks Davis 	struct if_clone *ifc;
701c859ef97SBrooks Davis 	int buf_count, count, err = 0;
702c859ef97SBrooks Davis 
703a6d00835SMaxim Konovalov 	if (ifcr->ifcr_count < 0)
704a6d00835SMaxim Konovalov 		return (EINVAL);
705a6d00835SMaxim Konovalov 
706c859ef97SBrooks Davis 	IF_CLONERS_LOCK();
707c859ef97SBrooks Davis 	/*
708c859ef97SBrooks Davis 	 * Set our internal output buffer size.  We could end up not
709c859ef97SBrooks Davis 	 * reporting a cloner that is added between the unlock and lock
710c859ef97SBrooks Davis 	 * below, but that's not a major problem.  Not caping our
711c859ef97SBrooks Davis 	 * allocation to the number of cloners actually in the system
712c859ef97SBrooks Davis 	 * could be because that would let arbitrary users cause us to
713a4641f4eSPedro F. Giffuni 	 * allocate arbitrary amounts of kernel memory.
714c859ef97SBrooks Davis 	 */
71537f17770SMarko Zec 	buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
71637f17770SMarko Zec 	    V_if_cloners_count : ifcr->ifcr_count;
717c859ef97SBrooks Davis 	IF_CLONERS_UNLOCK();
718c859ef97SBrooks Davis 
719c859ef97SBrooks Davis 	outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
720f889d2efSBrooks Davis 
721f889d2efSBrooks Davis 	IF_CLONERS_LOCK();
722f889d2efSBrooks Davis 
72337f17770SMarko Zec 	ifcr->ifcr_total = V_if_cloners_count;
724f889d2efSBrooks Davis 	if ((dst = ifcr->ifcr_buffer) == NULL) {
725f889d2efSBrooks Davis 		/* Just asking how many there are. */
726f889d2efSBrooks Davis 		goto done;
727f889d2efSBrooks Davis 	}
72837f17770SMarko Zec 	count = (V_if_cloners_count < buf_count) ?
72937f17770SMarko Zec 	    V_if_cloners_count : buf_count;
730f889d2efSBrooks Davis 
73137f17770SMarko Zec 	for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
732c859ef97SBrooks Davis 	    ifc != NULL && count != 0;
733c859ef97SBrooks Davis 	    ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
734c859ef97SBrooks Davis 		strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
735f889d2efSBrooks Davis 	}
736f889d2efSBrooks Davis 
737f889d2efSBrooks Davis done:
738f889d2efSBrooks Davis 	IF_CLONERS_UNLOCK();
73992f19df4SAlexander Kabaev 	if (err == 0 && dst != NULL)
740c859ef97SBrooks Davis 		err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
741c859ef97SBrooks Davis 	if (outbuf != NULL)
742c859ef97SBrooks Davis 		free(outbuf, M_CLONE);
743f889d2efSBrooks Davis 	return (err);
744f889d2efSBrooks Davis }
745f889d2efSBrooks Davis 
74654712fc4SGleb Smirnoff #ifdef VIMAGE
747f889d2efSBrooks Davis /*
74854712fc4SGleb Smirnoff  * if_clone_restoregroup() is used in context of if_vmove().
74954712fc4SGleb Smirnoff  *
75054712fc4SGleb Smirnoff  * Since if_detach_internal() has removed the interface from ALL groups, we
75154712fc4SGleb Smirnoff  * need to "restore" interface membership in the cloner's group.  Note that
75254712fc4SGleb Smirnoff  * interface belongs to cloner in its home vnet, so we first find the original
75354712fc4SGleb Smirnoff  * cloner, and then we confirm that cloner with the same name exists in the
75454712fc4SGleb Smirnoff  * current vnet.
755c92a456bSHiroki Sato  */
75654712fc4SGleb Smirnoff void
if_clone_restoregroup(struct ifnet * ifp)75754712fc4SGleb Smirnoff if_clone_restoregroup(struct ifnet *ifp)
758c92a456bSHiroki Sato {
75954712fc4SGleb Smirnoff 	struct if_clone *ifc;
760c92a456bSHiroki Sato 	struct ifnet *ifcifp;
76154712fc4SGleb Smirnoff 	char ifc_name[IFCLOSIZ] = { [0] = '\0' };
762c92a456bSHiroki Sato 
76354712fc4SGleb Smirnoff 	CURVNET_SET_QUIET(ifp->if_home_vnet);
764c92a456bSHiroki Sato 	IF_CLONERS_LOCK();
765c92a456bSHiroki Sato 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
766c92a456bSHiroki Sato 		IF_CLONE_LOCK(ifc);
767c92a456bSHiroki Sato 		LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
768c92a456bSHiroki Sato 			if (ifp == ifcifp) {
76954712fc4SGleb Smirnoff 				strncpy(ifc_name, ifc->ifc_name, IFCLOSIZ-1);
770c92a456bSHiroki Sato 				break;
771c92a456bSHiroki Sato 			}
772c92a456bSHiroki Sato 		}
773c92a456bSHiroki Sato 		IF_CLONE_UNLOCK(ifc);
77454712fc4SGleb Smirnoff 		if (ifc_name[0] != '\0')
775c92a456bSHiroki Sato 			break;
776c92a456bSHiroki Sato 	}
77754712fc4SGleb Smirnoff 	CURVNET_RESTORE();
77854712fc4SGleb Smirnoff 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
7797ff9ae90SMarius Strobl 		if (strcmp(ifc->ifc_name, ifc_name) == 0)
78054712fc4SGleb Smirnoff 			break;
781c92a456bSHiroki Sato 	IF_CLONERS_UNLOCK();
782c92a456bSHiroki Sato 
78354712fc4SGleb Smirnoff 	if (ifc != NULL)
78454712fc4SGleb Smirnoff 		if_addgroup(ifp, ifc_name);
785c92a456bSHiroki Sato }
78654712fc4SGleb Smirnoff #endif
787c92a456bSHiroki Sato 
788c92a456bSHiroki Sato /*
789f889d2efSBrooks Davis  * A utility function to extract unit numbers from interface names of
79053729367SAlexander V. Chernikov  * the form name###.
791f889d2efSBrooks Davis  *
792f889d2efSBrooks Davis  * Returns 0 on success and an error on failure.
793f889d2efSBrooks Davis  */
794f889d2efSBrooks Davis int
ifc_name2unit(const char * name,int * unit)795f889d2efSBrooks Davis ifc_name2unit(const char *name, int *unit)
796f889d2efSBrooks Davis {
797f889d2efSBrooks Davis 	const char	*cp;
798434dbbb3SRuslan Ermilov 	int		cutoff = INT_MAX / 10;
799434dbbb3SRuslan Ermilov 	int		cutlim = INT_MAX % 10;
800f889d2efSBrooks Davis 
80153729367SAlexander V. Chernikov 	for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++)
80253729367SAlexander V. Chernikov 		;
803f889d2efSBrooks Davis 	if (*cp == '\0') {
804f889d2efSBrooks Davis 		*unit = -1;
805434dbbb3SRuslan Ermilov 	} else if (cp[0] == '0' && cp[1] != '\0') {
806434dbbb3SRuslan Ermilov 		/* Disallow leading zeroes. */
807434dbbb3SRuslan Ermilov 		return (EINVAL);
808f889d2efSBrooks Davis 	} else {
809f889d2efSBrooks Davis 		for (*unit = 0; *cp != '\0'; cp++) {
810f889d2efSBrooks Davis 			if (*cp < '0' || *cp > '9') {
811f889d2efSBrooks Davis 				/* Bogus unit number. */
812f889d2efSBrooks Davis 				return (EINVAL);
813f889d2efSBrooks Davis 			}
814434dbbb3SRuslan Ermilov 			if (*unit > cutoff ||
815434dbbb3SRuslan Ermilov 			    (*unit == cutoff && *cp - '0' > cutlim))
816434dbbb3SRuslan Ermilov 				return (EINVAL);
817f889d2efSBrooks Davis 			*unit = (*unit * 10) + (*cp - '0');
818f889d2efSBrooks Davis 		}
819f889d2efSBrooks Davis 	}
820f889d2efSBrooks Davis 
821f889d2efSBrooks Davis 	return (0);
822f889d2efSBrooks Davis }
823f889d2efSBrooks Davis 
824c64c1f95SAndriy Voskoboinyk static int
ifc_alloc_unit_specific(struct if_clone * ifc,int * unit)825c64c1f95SAndriy Voskoboinyk ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
826f889d2efSBrooks Davis {
8272e9fff5bSGleb Smirnoff 	char name[IFNAMSIZ];
828f889d2efSBrooks Davis 
8293932d760SGleb Smirnoff 	if (*unit > ifc->ifc_maxunit)
8303932d760SGleb Smirnoff 		return (ENOSPC);
831c64c1f95SAndriy Voskoboinyk 
832c64c1f95SAndriy Voskoboinyk 	if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
8332e9fff5bSGleb Smirnoff 		return (EEXIST);
834f889d2efSBrooks Davis 
8352e9fff5bSGleb Smirnoff 	snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
8362e9fff5bSGleb Smirnoff 	if (ifunit(name) != NULL) {
8373932d760SGleb Smirnoff 		free_unr(ifc->ifc_unrhdr, *unit);
8382e9fff5bSGleb Smirnoff 		return (EEXIST);
839f889d2efSBrooks Davis 	}
840f889d2efSBrooks Davis 
8412e9fff5bSGleb Smirnoff 	IF_CLONE_ADDREF(ifc);
842f889d2efSBrooks Davis 
8432e9fff5bSGleb Smirnoff 	return (0);
844f889d2efSBrooks Davis }
845f889d2efSBrooks Davis 
846c64c1f95SAndriy Voskoboinyk static int
ifc_alloc_unit_next(struct if_clone * ifc,int * unit)847c64c1f95SAndriy Voskoboinyk ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
848c64c1f95SAndriy Voskoboinyk {
849c64c1f95SAndriy Voskoboinyk 	int error;
850c64c1f95SAndriy Voskoboinyk 
851c64c1f95SAndriy Voskoboinyk 	*unit = alloc_unr(ifc->ifc_unrhdr);
852c64c1f95SAndriy Voskoboinyk 	if (*unit == -1)
853c64c1f95SAndriy Voskoboinyk 		return (ENOSPC);
854c64c1f95SAndriy Voskoboinyk 
855c64c1f95SAndriy Voskoboinyk 	free_unr(ifc->ifc_unrhdr, *unit);
856c64c1f95SAndriy Voskoboinyk 	for (;;) {
857c64c1f95SAndriy Voskoboinyk 		error = ifc_alloc_unit_specific(ifc, unit);
858c64c1f95SAndriy Voskoboinyk 		if (error != EEXIST)
859c64c1f95SAndriy Voskoboinyk 			break;
860c64c1f95SAndriy Voskoboinyk 
861c64c1f95SAndriy Voskoboinyk 		(*unit)++;
862c64c1f95SAndriy Voskoboinyk 	}
863c64c1f95SAndriy Voskoboinyk 
864c64c1f95SAndriy Voskoboinyk 	return (error);
865c64c1f95SAndriy Voskoboinyk }
866c64c1f95SAndriy Voskoboinyk 
867c64c1f95SAndriy Voskoboinyk int
ifc_alloc_unit(struct if_clone * ifc,int * unit)868c64c1f95SAndriy Voskoboinyk ifc_alloc_unit(struct if_clone *ifc, int *unit)
869c64c1f95SAndriy Voskoboinyk {
870c64c1f95SAndriy Voskoboinyk 	if (*unit < 0)
871c64c1f95SAndriy Voskoboinyk 		return (ifc_alloc_unit_next(ifc, unit));
872c64c1f95SAndriy Voskoboinyk 	else
873c64c1f95SAndriy Voskoboinyk 		return (ifc_alloc_unit_specific(ifc, unit));
874c64c1f95SAndriy Voskoboinyk }
875c64c1f95SAndriy Voskoboinyk 
876f889d2efSBrooks Davis void
ifc_free_unit(struct if_clone * ifc,int unit)877f889d2efSBrooks Davis ifc_free_unit(struct if_clone *ifc, int unit)
878f889d2efSBrooks Davis {
879f889d2efSBrooks Davis 
8802e9fff5bSGleb Smirnoff 	free_unr(ifc->ifc_unrhdr, unit);
8812e9fff5bSGleb Smirnoff 	IF_CLONE_REMREF(ifc);
882f889d2efSBrooks Davis }
883f889d2efSBrooks Davis 
88442a58907SGleb Smirnoff static int
ifc_simple_match(struct if_clone * ifc,const char * name)885f889d2efSBrooks Davis ifc_simple_match(struct if_clone *ifc, const char *name)
886f889d2efSBrooks Davis {
887f889d2efSBrooks Davis 	const char *cp;
888f889d2efSBrooks Davis 	int i;
889f889d2efSBrooks Davis 
890f889d2efSBrooks Davis 	/* Match the name */
891f889d2efSBrooks Davis 	for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
892f889d2efSBrooks Davis 		if (ifc->ifc_name[i] != *cp)
893f889d2efSBrooks Davis 			return (0);
894f889d2efSBrooks Davis 	}
895f889d2efSBrooks Davis 
896f889d2efSBrooks Davis 	/* Make sure there's a unit number or nothing after the name */
897f889d2efSBrooks Davis 	for (; *cp != '\0'; cp++) {
898f889d2efSBrooks Davis 		if (*cp < '0' || *cp > '9')
899f889d2efSBrooks Davis 			return (0);
900f889d2efSBrooks Davis 	}
901f889d2efSBrooks Davis 
902f889d2efSBrooks Davis 	return (1);
903f889d2efSBrooks Davis }
904f889d2efSBrooks Davis 
90542a58907SGleb Smirnoff static int
ifc_handle_unit(struct if_clone * ifc,char * name,size_t len,int * punit)90609ee0fc0SAlexander V. Chernikov ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
907f889d2efSBrooks Davis {
908f889d2efSBrooks Davis 	char *dp;
909f889d2efSBrooks Davis 	int wildcard;
910f889d2efSBrooks Davis 	int unit;
911f889d2efSBrooks Davis 	int err;
912f889d2efSBrooks Davis 
913f889d2efSBrooks Davis 	err = ifc_name2unit(name, &unit);
914f889d2efSBrooks Davis 	if (err != 0)
915f889d2efSBrooks Davis 		return (err);
916f889d2efSBrooks Davis 
917f889d2efSBrooks Davis 	wildcard = (unit < 0);
918f889d2efSBrooks Davis 
919f889d2efSBrooks Davis 	err = ifc_alloc_unit(ifc, &unit);
920f889d2efSBrooks Davis 	if (err != 0)
921f889d2efSBrooks Davis 		return (err);
922f889d2efSBrooks Davis 
923f889d2efSBrooks Davis 	/* In the wildcard case, we need to update the name. */
924f889d2efSBrooks Davis 	if (wildcard) {
925f889d2efSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
926f889d2efSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
927f889d2efSBrooks Davis 		    len - (dp-name) - 1) {
928f889d2efSBrooks Davis 			/*
929f889d2efSBrooks Davis 			 * This can only be a programmer error and
930f889d2efSBrooks Davis 			 * there's no straightforward way to recover if
931f889d2efSBrooks Davis 			 * it happens.
932f889d2efSBrooks Davis 			 */
933f889d2efSBrooks Davis 			panic("if_clone_create(): interface name too long");
934f889d2efSBrooks Davis 		}
935f889d2efSBrooks Davis 	}
93609ee0fc0SAlexander V. Chernikov 	*punit = unit;
937f889d2efSBrooks Davis 
938f889d2efSBrooks Davis 	return (0);
939f889d2efSBrooks Davis }
940f889d2efSBrooks Davis 
94109ee0fc0SAlexander V. Chernikov int
ifc_copyin(const struct ifc_data * ifd,void * target,size_t len)94209ee0fc0SAlexander V. Chernikov ifc_copyin(const struct ifc_data *ifd, void *target, size_t len)
943f889d2efSBrooks Davis {
94409ee0fc0SAlexander V. Chernikov 	if (ifd->params == NULL)
945f889d2efSBrooks Davis 		return (EINVAL);
946f889d2efSBrooks Davis 
94709ee0fc0SAlexander V. Chernikov 	if (ifd->flags & IFC_F_SYSSPACE) {
94809ee0fc0SAlexander V. Chernikov 		memcpy(target, ifd->params, len);
949f889d2efSBrooks Davis 		return (0);
95009ee0fc0SAlexander V. Chernikov 	} else
95109ee0fc0SAlexander V. Chernikov 		return (copyin(ifd->params, target, len));
952f889d2efSBrooks Davis }
953