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__));
486*a2cac544SZhenlei Huang MPASS(maxunit >= 0);
48742a58907SGleb Smirnoff
48842a58907SGleb Smirnoff ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO);
48942a58907SGleb Smirnoff strncpy(ifc->ifc_name, name, IFCLOSIZ-1);
49042a58907SGleb Smirnoff IF_CLONE_LOCK_INIT(ifc);
49142a58907SGleb Smirnoff IF_CLONE_ADDREF(ifc);
492*a2cac544SZhenlei Huang ifc->ifc_maxunit = maxunit;
49342a58907SGleb Smirnoff ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx);
49442a58907SGleb Smirnoff LIST_INIT(&ifc->ifc_iflist);
49542a58907SGleb Smirnoff
496089104e0SAlexander V. Chernikov ifc->create_nl = ifc_create_ifp_nl_default;
497089104e0SAlexander V. Chernikov ifc->modify_nl = ifc_modify_ifp_nl_default;
498089104e0SAlexander V. Chernikov ifc->dump_nl = ifc_dump_ifp_nl_default;
499089104e0SAlexander V. Chernikov
50042a58907SGleb Smirnoff return (ifc);
50142a58907SGleb Smirnoff }
50242a58907SGleb Smirnoff
50342a58907SGleb Smirnoff static int
if_clone_attach(struct if_clone * ifc)504f889d2efSBrooks Davis if_clone_attach(struct if_clone *ifc)
505f889d2efSBrooks Davis {
5062e9fff5bSGleb Smirnoff struct if_clone *ifc1;
507f889d2efSBrooks Davis
508f889d2efSBrooks Davis IF_CLONERS_LOCK();
5092e9fff5bSGleb Smirnoff LIST_FOREACH(ifc1, &V_if_cloners, ifc_list)
5102e9fff5bSGleb Smirnoff if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) {
5112e9fff5bSGleb Smirnoff IF_CLONERS_UNLOCK();
5122e9fff5bSGleb Smirnoff IF_CLONE_REMREF(ifc);
5132e9fff5bSGleb Smirnoff return (EEXIST);
5142e9fff5bSGleb Smirnoff }
51537f17770SMarko Zec LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
51637f17770SMarko Zec V_if_cloners_count++;
517f889d2efSBrooks Davis IF_CLONERS_UNLOCK();
518f889d2efSBrooks Davis
51942a58907SGleb Smirnoff return (0);
52042a58907SGleb Smirnoff }
52142a58907SGleb Smirnoff
52242a58907SGleb Smirnoff struct if_clone *
ifc_attach_cloner(const char * name,struct if_clone_addreq * req)52309ee0fc0SAlexander V. Chernikov ifc_attach_cloner(const char *name, struct if_clone_addreq *req)
52442a58907SGleb Smirnoff {
525*a2cac544SZhenlei Huang int maxunit;
526*a2cac544SZhenlei Huang struct if_clone *ifc;
527*a2cac544SZhenlei Huang
52809ee0fc0SAlexander V. Chernikov if (req->create_f == NULL || req->destroy_f == NULL)
52909ee0fc0SAlexander V. Chernikov return (NULL);
53009ee0fc0SAlexander V. Chernikov if (strnlen(name, IFCLOSIZ) >= (IFCLOSIZ - 1))
53109ee0fc0SAlexander V. Chernikov return (NULL);
53242a58907SGleb Smirnoff
533*a2cac544SZhenlei Huang maxunit = (req->flags & IFC_F_LIMITUNIT) ? req->maxunit : IF_MAXUNIT;
534*a2cac544SZhenlei Huang ifc = if_clone_alloc(name, maxunit);
53509ee0fc0SAlexander V. Chernikov ifc->ifc_match = req->match_f != NULL ? req->match_f : ifc_simple_match;
53609ee0fc0SAlexander V. Chernikov ifc->ifc_create = req->create_f;
53709ee0fc0SAlexander V. Chernikov ifc->ifc_destroy = req->destroy_f;
5387ff9ae90SMarius Strobl ifc->ifc_flags = (req->flags & IFC_F_AUTOUNIT);
53942a58907SGleb Smirnoff
540089104e0SAlexander V. Chernikov if (req->version == 2) {
541089104e0SAlexander V. Chernikov struct if_clone_addreq_v2 *req2 = (struct if_clone_addreq_v2 *)req;
542089104e0SAlexander V. Chernikov
543089104e0SAlexander V. Chernikov ifc->create_nl = req2->create_nl_f;
544089104e0SAlexander V. Chernikov ifc->modify_nl = req2->modify_nl_f;
545089104e0SAlexander V. Chernikov ifc->dump_nl = req2->dump_nl_f;
546089104e0SAlexander V. Chernikov }
547089104e0SAlexander V. Chernikov
548089104e0SAlexander V. Chernikov ifc->dump_nl = ifc_dump_ifp_nl_default;
549089104e0SAlexander V. Chernikov
5503395dd6eSAlexander Kabaev if (if_clone_attach(ifc) != 0)
55142a58907SGleb Smirnoff return (NULL);
55242a58907SGleb Smirnoff
553f889d2efSBrooks Davis EVENTHANDLER_INVOKE(if_clone_event, ifc);
5542e9fff5bSGleb Smirnoff
55542a58907SGleb Smirnoff return (ifc);
55642a58907SGleb Smirnoff }
55742a58907SGleb Smirnoff
55809ee0fc0SAlexander V. Chernikov void
ifc_detach_cloner(struct if_clone * ifc)55909ee0fc0SAlexander V. Chernikov ifc_detach_cloner(struct if_clone *ifc)
56009ee0fc0SAlexander V. Chernikov {
56109ee0fc0SAlexander V. Chernikov if_clone_detach(ifc);
56209ee0fc0SAlexander V. Chernikov }
56309ee0fc0SAlexander V. Chernikov
56409ee0fc0SAlexander V. Chernikov
56509ee0fc0SAlexander V. Chernikov #ifdef CLONE_COMPAT_13
56609ee0fc0SAlexander V. Chernikov
56709ee0fc0SAlexander 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)56809ee0fc0SAlexander V. Chernikov ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
56909ee0fc0SAlexander V. Chernikov struct ifc_data *ifc_data, struct ifnet **ifpp)
57009ee0fc0SAlexander V. Chernikov {
57109ee0fc0SAlexander V. Chernikov int error = ifc->ifca_create(ifc, name, maxlen, ifc_data->params);
57209ee0fc0SAlexander V. Chernikov
57309ee0fc0SAlexander V. Chernikov if (error == 0)
57409ee0fc0SAlexander V. Chernikov *ifpp = ifunit(name);
57509ee0fc0SAlexander V. Chernikov return (error);
57609ee0fc0SAlexander V. Chernikov }
57709ee0fc0SAlexander V. Chernikov
57809ee0fc0SAlexander V. Chernikov static int
ifc_advanced_destroy_wrapper(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)57909ee0fc0SAlexander V. Chernikov ifc_advanced_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
58009ee0fc0SAlexander V. Chernikov {
58109ee0fc0SAlexander V. Chernikov if (ifc->ifca_destroy == NULL)
58209ee0fc0SAlexander V. Chernikov return (ENOTSUP);
58309ee0fc0SAlexander V. Chernikov return (ifc->ifca_destroy(ifc, ifp));
58409ee0fc0SAlexander V. Chernikov }
58509ee0fc0SAlexander V. Chernikov
58609ee0fc0SAlexander 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)58709ee0fc0SAlexander V. Chernikov if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
58809ee0fc0SAlexander V. Chernikov ifc_create_t create, ifc_destroy_t destroy)
58909ee0fc0SAlexander V. Chernikov {
59009ee0fc0SAlexander V. Chernikov struct if_clone *ifc;
59109ee0fc0SAlexander V. Chernikov
592*a2cac544SZhenlei Huang ifc = if_clone_alloc(name, maxunit ? maxunit : IF_MAXUNIT);
59309ee0fc0SAlexander V. Chernikov ifc->ifc_match = match;
59409ee0fc0SAlexander V. Chernikov ifc->ifc_create = ifc_advanced_create_wrapper;
59509ee0fc0SAlexander V. Chernikov ifc->ifc_destroy = ifc_advanced_destroy_wrapper;
59609ee0fc0SAlexander V. Chernikov ifc->ifca_destroy = destroy;
59709ee0fc0SAlexander V. Chernikov ifc->ifca_create = create;
59809ee0fc0SAlexander V. Chernikov
59909ee0fc0SAlexander V. Chernikov if (if_clone_attach(ifc) != 0)
60009ee0fc0SAlexander V. Chernikov return (NULL);
60109ee0fc0SAlexander V. Chernikov
60209ee0fc0SAlexander V. Chernikov EVENTHANDLER_INVOKE(if_clone_event, ifc);
60309ee0fc0SAlexander V. Chernikov
60409ee0fc0SAlexander V. Chernikov return (ifc);
60509ee0fc0SAlexander V. Chernikov }
60609ee0fc0SAlexander V. Chernikov
60709ee0fc0SAlexander 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)60809ee0fc0SAlexander V. Chernikov ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
60909ee0fc0SAlexander V. Chernikov struct ifc_data *ifc_data, struct ifnet **ifpp)
61009ee0fc0SAlexander V. Chernikov {
61109ee0fc0SAlexander V. Chernikov int unit = 0;
61209ee0fc0SAlexander V. Chernikov
61309ee0fc0SAlexander V. Chernikov ifc_name2unit(name, &unit);
61409ee0fc0SAlexander V. Chernikov int error = ifc->ifcs_create(ifc, unit, ifc_data->params);
61509ee0fc0SAlexander V. Chernikov if (error == 0)
61609ee0fc0SAlexander V. Chernikov *ifpp = ifunit(name);
61709ee0fc0SAlexander V. Chernikov return (error);
61809ee0fc0SAlexander V. Chernikov }
61909ee0fc0SAlexander V. Chernikov
62009ee0fc0SAlexander V. Chernikov static int
ifc_simple_destroy_wrapper(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)62109ee0fc0SAlexander V. Chernikov ifc_simple_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
62209ee0fc0SAlexander V. Chernikov {
62309ee0fc0SAlexander V. Chernikov if (ifp->if_dunit < ifc->ifcs_minifs && (flags & IFC_F_FORCE) == 0)
62409ee0fc0SAlexander V. Chernikov return (EINVAL);
62509ee0fc0SAlexander V. Chernikov
62609ee0fc0SAlexander V. Chernikov ifc->ifcs_destroy(ifp);
62709ee0fc0SAlexander V. Chernikov return (0);
62809ee0fc0SAlexander V. Chernikov }
62909ee0fc0SAlexander V. Chernikov
63042a58907SGleb Smirnoff struct if_clone *
if_clone_simple(const char * name,ifcs_create_t create,ifcs_destroy_t destroy,u_int minifs)63142a58907SGleb Smirnoff if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
63242a58907SGleb Smirnoff u_int minifs)
63342a58907SGleb Smirnoff {
63442a58907SGleb Smirnoff struct if_clone *ifc;
63542a58907SGleb Smirnoff u_int unit;
63642a58907SGleb Smirnoff
637*a2cac544SZhenlei Huang ifc = if_clone_alloc(name, IF_MAXUNIT);
63809ee0fc0SAlexander V. Chernikov ifc->ifc_match = ifc_simple_match;
63909ee0fc0SAlexander V. Chernikov ifc->ifc_create = ifc_simple_create_wrapper;
64009ee0fc0SAlexander V. Chernikov ifc->ifc_destroy = ifc_simple_destroy_wrapper;
64142a58907SGleb Smirnoff ifc->ifcs_create = create;
64242a58907SGleb Smirnoff ifc->ifcs_destroy = destroy;
64342a58907SGleb Smirnoff ifc->ifcs_minifs = minifs;
64409ee0fc0SAlexander V. Chernikov ifc->ifc_flags = IFC_F_AUTOUNIT;
64542a58907SGleb Smirnoff
6463395dd6eSAlexander Kabaev if (if_clone_attach(ifc) != 0)
64742a58907SGleb Smirnoff return (NULL);
64842a58907SGleb Smirnoff
64942a58907SGleb Smirnoff for (unit = 0; unit < minifs; unit++) {
65042a58907SGleb Smirnoff char name[IFNAMSIZ];
65146d0f824SMatt Macy int error __unused;
652089104e0SAlexander V. Chernikov struct ifc_data_nl ifd = {};
65342a58907SGleb Smirnoff
65442a58907SGleb Smirnoff snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
655089104e0SAlexander V. Chernikov error = if_clone_createif_nl(ifc, name, &ifd);
65642a58907SGleb Smirnoff KASSERT(error == 0,
65742a58907SGleb Smirnoff ("%s: failed to create required interface %s",
65842a58907SGleb Smirnoff __func__, name));
65942a58907SGleb Smirnoff }
66042a58907SGleb Smirnoff
66142a58907SGleb Smirnoff EVENTHANDLER_INVOKE(if_clone_event, ifc);
66242a58907SGleb Smirnoff
66342a58907SGleb Smirnoff return (ifc);
664f889d2efSBrooks Davis }
66509ee0fc0SAlexander V. Chernikov #endif
666f889d2efSBrooks Davis
667f889d2efSBrooks Davis /*
668f889d2efSBrooks Davis * Unregister a network interface cloner.
669f889d2efSBrooks Davis */
670f889d2efSBrooks Davis void
if_clone_detach(struct if_clone * ifc)671f889d2efSBrooks Davis if_clone_detach(struct if_clone *ifc)
672f889d2efSBrooks Davis {
673f889d2efSBrooks Davis
674f889d2efSBrooks Davis IF_CLONERS_LOCK();
675f889d2efSBrooks Davis LIST_REMOVE(ifc, ifc_list);
67637f17770SMarko Zec V_if_cloners_count--;
677f889d2efSBrooks Davis IF_CLONERS_UNLOCK();
678f889d2efSBrooks Davis
6794e7e0183SAndrew Thompson /* destroy all interfaces for this cloner */
6804e7e0183SAndrew Thompson while (!LIST_EMPTY(&ifc->ifc_iflist))
68109ee0fc0SAlexander V. Chernikov if_clone_destroyif_flags(ifc, LIST_FIRST(&ifc->ifc_iflist), IFC_F_FORCE);
6824e7e0183SAndrew Thompson
683f889d2efSBrooks Davis IF_CLONE_REMREF(ifc);
684f889d2efSBrooks Davis }
685f889d2efSBrooks Davis
686f889d2efSBrooks Davis static void
if_clone_free(struct if_clone * ifc)687f889d2efSBrooks Davis if_clone_free(struct if_clone *ifc)
688f889d2efSBrooks Davis {
689f889d2efSBrooks Davis
6904e7e0183SAndrew Thompson KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
6914e7e0183SAndrew Thompson ("%s: ifc_iflist not empty", __func__));
6924e7e0183SAndrew Thompson
693f889d2efSBrooks Davis IF_CLONE_LOCK_DESTROY(ifc);
6942e9fff5bSGleb Smirnoff delete_unrhdr(ifc->ifc_unrhdr);
69542a58907SGleb Smirnoff free(ifc, M_CLONE);
696f889d2efSBrooks Davis }
697f889d2efSBrooks Davis
698f889d2efSBrooks Davis /*
699f889d2efSBrooks Davis * Provide list of interface cloners to userspace.
700f889d2efSBrooks Davis */
701f889d2efSBrooks Davis int
if_clone_list(struct if_clonereq * ifcr)702f889d2efSBrooks Davis if_clone_list(struct if_clonereq *ifcr)
703f889d2efSBrooks Davis {
704c859ef97SBrooks Davis char *buf, *dst, *outbuf = NULL;
705f889d2efSBrooks Davis struct if_clone *ifc;
706c859ef97SBrooks Davis int buf_count, count, err = 0;
707c859ef97SBrooks Davis
708a6d00835SMaxim Konovalov if (ifcr->ifcr_count < 0)
709a6d00835SMaxim Konovalov return (EINVAL);
710a6d00835SMaxim Konovalov
711c859ef97SBrooks Davis IF_CLONERS_LOCK();
712c859ef97SBrooks Davis /*
713c859ef97SBrooks Davis * Set our internal output buffer size. We could end up not
714c859ef97SBrooks Davis * reporting a cloner that is added between the unlock and lock
715c859ef97SBrooks Davis * below, but that's not a major problem. Not caping our
716c859ef97SBrooks Davis * allocation to the number of cloners actually in the system
717c859ef97SBrooks Davis * could be because that would let arbitrary users cause us to
718a4641f4eSPedro F. Giffuni * allocate arbitrary amounts of kernel memory.
719c859ef97SBrooks Davis */
72037f17770SMarko Zec buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
72137f17770SMarko Zec V_if_cloners_count : ifcr->ifcr_count;
722c859ef97SBrooks Davis IF_CLONERS_UNLOCK();
723c859ef97SBrooks Davis
724c859ef97SBrooks Davis outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
725f889d2efSBrooks Davis
726f889d2efSBrooks Davis IF_CLONERS_LOCK();
727f889d2efSBrooks Davis
72837f17770SMarko Zec ifcr->ifcr_total = V_if_cloners_count;
729f889d2efSBrooks Davis if ((dst = ifcr->ifcr_buffer) == NULL) {
730f889d2efSBrooks Davis /* Just asking how many there are. */
731f889d2efSBrooks Davis goto done;
732f889d2efSBrooks Davis }
73337f17770SMarko Zec count = (V_if_cloners_count < buf_count) ?
73437f17770SMarko Zec V_if_cloners_count : buf_count;
735f889d2efSBrooks Davis
73637f17770SMarko Zec for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
737c859ef97SBrooks Davis ifc != NULL && count != 0;
738c859ef97SBrooks Davis ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
739c859ef97SBrooks Davis strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
740f889d2efSBrooks Davis }
741f889d2efSBrooks Davis
742f889d2efSBrooks Davis done:
743f889d2efSBrooks Davis IF_CLONERS_UNLOCK();
74492f19df4SAlexander Kabaev if (err == 0 && dst != NULL)
745c859ef97SBrooks Davis err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
746c859ef97SBrooks Davis if (outbuf != NULL)
747c859ef97SBrooks Davis free(outbuf, M_CLONE);
748f889d2efSBrooks Davis return (err);
749f889d2efSBrooks Davis }
750f889d2efSBrooks Davis
75154712fc4SGleb Smirnoff #ifdef VIMAGE
752f889d2efSBrooks Davis /*
75354712fc4SGleb Smirnoff * if_clone_restoregroup() is used in context of if_vmove().
75454712fc4SGleb Smirnoff *
75554712fc4SGleb Smirnoff * Since if_detach_internal() has removed the interface from ALL groups, we
75654712fc4SGleb Smirnoff * need to "restore" interface membership in the cloner's group. Note that
75754712fc4SGleb Smirnoff * interface belongs to cloner in its home vnet, so we first find the original
75854712fc4SGleb Smirnoff * cloner, and then we confirm that cloner with the same name exists in the
75954712fc4SGleb Smirnoff * current vnet.
760c92a456bSHiroki Sato */
76154712fc4SGleb Smirnoff void
if_clone_restoregroup(struct ifnet * ifp)76254712fc4SGleb Smirnoff if_clone_restoregroup(struct ifnet *ifp)
763c92a456bSHiroki Sato {
76454712fc4SGleb Smirnoff struct if_clone *ifc;
765c92a456bSHiroki Sato struct ifnet *ifcifp;
76654712fc4SGleb Smirnoff char ifc_name[IFCLOSIZ] = { [0] = '\0' };
767c92a456bSHiroki Sato
76854712fc4SGleb Smirnoff CURVNET_SET_QUIET(ifp->if_home_vnet);
769c92a456bSHiroki Sato IF_CLONERS_LOCK();
770c92a456bSHiroki Sato LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
771c92a456bSHiroki Sato IF_CLONE_LOCK(ifc);
772c92a456bSHiroki Sato LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
773c92a456bSHiroki Sato if (ifp == ifcifp) {
77454712fc4SGleb Smirnoff strncpy(ifc_name, ifc->ifc_name, IFCLOSIZ-1);
775c92a456bSHiroki Sato break;
776c92a456bSHiroki Sato }
777c92a456bSHiroki Sato }
778c92a456bSHiroki Sato IF_CLONE_UNLOCK(ifc);
77954712fc4SGleb Smirnoff if (ifc_name[0] != '\0')
780c92a456bSHiroki Sato break;
781c92a456bSHiroki Sato }
78254712fc4SGleb Smirnoff CURVNET_RESTORE();
78354712fc4SGleb Smirnoff LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
7847ff9ae90SMarius Strobl if (strcmp(ifc->ifc_name, ifc_name) == 0)
78554712fc4SGleb Smirnoff break;
786c92a456bSHiroki Sato IF_CLONERS_UNLOCK();
787c92a456bSHiroki Sato
78854712fc4SGleb Smirnoff if (ifc != NULL)
78954712fc4SGleb Smirnoff if_addgroup(ifp, ifc_name);
790c92a456bSHiroki Sato }
79154712fc4SGleb Smirnoff #endif
792c92a456bSHiroki Sato
793c92a456bSHiroki Sato /*
794f889d2efSBrooks Davis * A utility function to extract unit numbers from interface names of
79553729367SAlexander V. Chernikov * the form name###.
796f889d2efSBrooks Davis *
797f889d2efSBrooks Davis * Returns 0 on success and an error on failure.
798f889d2efSBrooks Davis */
799f889d2efSBrooks Davis int
ifc_name2unit(const char * name,int * unit)800f889d2efSBrooks Davis ifc_name2unit(const char *name, int *unit)
801f889d2efSBrooks Davis {
802f889d2efSBrooks Davis const char *cp;
803434dbbb3SRuslan Ermilov int cutoff = INT_MAX / 10;
804434dbbb3SRuslan Ermilov int cutlim = INT_MAX % 10;
805f889d2efSBrooks Davis
80653729367SAlexander V. Chernikov for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++)
80753729367SAlexander V. Chernikov ;
808f889d2efSBrooks Davis if (*cp == '\0') {
809f889d2efSBrooks Davis *unit = -1;
810434dbbb3SRuslan Ermilov } else if (cp[0] == '0' && cp[1] != '\0') {
811434dbbb3SRuslan Ermilov /* Disallow leading zeroes. */
812434dbbb3SRuslan Ermilov return (EINVAL);
813f889d2efSBrooks Davis } else {
814f889d2efSBrooks Davis for (*unit = 0; *cp != '\0'; cp++) {
815f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') {
816f889d2efSBrooks Davis /* Bogus unit number. */
817f889d2efSBrooks Davis return (EINVAL);
818f889d2efSBrooks Davis }
819434dbbb3SRuslan Ermilov if (*unit > cutoff ||
820434dbbb3SRuslan Ermilov (*unit == cutoff && *cp - '0' > cutlim))
821434dbbb3SRuslan Ermilov return (EINVAL);
822f889d2efSBrooks Davis *unit = (*unit * 10) + (*cp - '0');
823f889d2efSBrooks Davis }
824f889d2efSBrooks Davis }
825f889d2efSBrooks Davis
826f889d2efSBrooks Davis return (0);
827f889d2efSBrooks Davis }
828f889d2efSBrooks Davis
829c64c1f95SAndriy Voskoboinyk static int
ifc_alloc_unit_specific(struct if_clone * ifc,int * unit)830c64c1f95SAndriy Voskoboinyk ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
831f889d2efSBrooks Davis {
8322e9fff5bSGleb Smirnoff char name[IFNAMSIZ];
833f889d2efSBrooks Davis
8343932d760SGleb Smirnoff if (*unit > ifc->ifc_maxunit)
8353932d760SGleb Smirnoff return (ENOSPC);
836c64c1f95SAndriy Voskoboinyk
837c64c1f95SAndriy Voskoboinyk if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
8382e9fff5bSGleb Smirnoff return (EEXIST);
839f889d2efSBrooks Davis
8402e9fff5bSGleb Smirnoff snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
8412e9fff5bSGleb Smirnoff if (ifunit(name) != NULL) {
8423932d760SGleb Smirnoff free_unr(ifc->ifc_unrhdr, *unit);
8432e9fff5bSGleb Smirnoff return (EEXIST);
844f889d2efSBrooks Davis }
845f889d2efSBrooks Davis
8462e9fff5bSGleb Smirnoff IF_CLONE_ADDREF(ifc);
847f889d2efSBrooks Davis
8482e9fff5bSGleb Smirnoff return (0);
849f889d2efSBrooks Davis }
850f889d2efSBrooks Davis
851c64c1f95SAndriy Voskoboinyk static int
ifc_alloc_unit_next(struct if_clone * ifc,int * unit)852c64c1f95SAndriy Voskoboinyk ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
853c64c1f95SAndriy Voskoboinyk {
854c64c1f95SAndriy Voskoboinyk int error;
855c64c1f95SAndriy Voskoboinyk
856c64c1f95SAndriy Voskoboinyk *unit = alloc_unr(ifc->ifc_unrhdr);
857c64c1f95SAndriy Voskoboinyk if (*unit == -1)
858c64c1f95SAndriy Voskoboinyk return (ENOSPC);
859c64c1f95SAndriy Voskoboinyk
860c64c1f95SAndriy Voskoboinyk free_unr(ifc->ifc_unrhdr, *unit);
861c64c1f95SAndriy Voskoboinyk for (;;) {
862c64c1f95SAndriy Voskoboinyk error = ifc_alloc_unit_specific(ifc, unit);
863c64c1f95SAndriy Voskoboinyk if (error != EEXIST)
864c64c1f95SAndriy Voskoboinyk break;
865c64c1f95SAndriy Voskoboinyk
866c64c1f95SAndriy Voskoboinyk (*unit)++;
867c64c1f95SAndriy Voskoboinyk }
868c64c1f95SAndriy Voskoboinyk
869c64c1f95SAndriy Voskoboinyk return (error);
870c64c1f95SAndriy Voskoboinyk }
871c64c1f95SAndriy Voskoboinyk
872c64c1f95SAndriy Voskoboinyk int
ifc_alloc_unit(struct if_clone * ifc,int * unit)873c64c1f95SAndriy Voskoboinyk ifc_alloc_unit(struct if_clone *ifc, int *unit)
874c64c1f95SAndriy Voskoboinyk {
875c64c1f95SAndriy Voskoboinyk if (*unit < 0)
876c64c1f95SAndriy Voskoboinyk return (ifc_alloc_unit_next(ifc, unit));
877c64c1f95SAndriy Voskoboinyk else
878c64c1f95SAndriy Voskoboinyk return (ifc_alloc_unit_specific(ifc, unit));
879c64c1f95SAndriy Voskoboinyk }
880c64c1f95SAndriy Voskoboinyk
881f889d2efSBrooks Davis void
ifc_free_unit(struct if_clone * ifc,int unit)882f889d2efSBrooks Davis ifc_free_unit(struct if_clone *ifc, int unit)
883f889d2efSBrooks Davis {
884f889d2efSBrooks Davis
8852e9fff5bSGleb Smirnoff free_unr(ifc->ifc_unrhdr, unit);
8862e9fff5bSGleb Smirnoff IF_CLONE_REMREF(ifc);
887f889d2efSBrooks Davis }
888f889d2efSBrooks Davis
88942a58907SGleb Smirnoff static int
ifc_simple_match(struct if_clone * ifc,const char * name)890f889d2efSBrooks Davis ifc_simple_match(struct if_clone *ifc, const char *name)
891f889d2efSBrooks Davis {
892f889d2efSBrooks Davis const char *cp;
893f889d2efSBrooks Davis int i;
894f889d2efSBrooks Davis
895f889d2efSBrooks Davis /* Match the name */
896f889d2efSBrooks Davis for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
897f889d2efSBrooks Davis if (ifc->ifc_name[i] != *cp)
898f889d2efSBrooks Davis return (0);
899f889d2efSBrooks Davis }
900f889d2efSBrooks Davis
901f889d2efSBrooks Davis /* Make sure there's a unit number or nothing after the name */
902f889d2efSBrooks Davis for (; *cp != '\0'; cp++) {
903f889d2efSBrooks Davis if (*cp < '0' || *cp > '9')
904f889d2efSBrooks Davis return (0);
905f889d2efSBrooks Davis }
906f889d2efSBrooks Davis
907f889d2efSBrooks Davis return (1);
908f889d2efSBrooks Davis }
909f889d2efSBrooks Davis
91042a58907SGleb Smirnoff static int
ifc_handle_unit(struct if_clone * ifc,char * name,size_t len,int * punit)91109ee0fc0SAlexander V. Chernikov ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
912f889d2efSBrooks Davis {
913f889d2efSBrooks Davis char *dp;
914f889d2efSBrooks Davis int wildcard;
915f889d2efSBrooks Davis int unit;
916f889d2efSBrooks Davis int err;
917f889d2efSBrooks Davis
918f889d2efSBrooks Davis err = ifc_name2unit(name, &unit);
919f889d2efSBrooks Davis if (err != 0)
920f889d2efSBrooks Davis return (err);
921f889d2efSBrooks Davis
922f889d2efSBrooks Davis wildcard = (unit < 0);
923f889d2efSBrooks Davis
924f889d2efSBrooks Davis err = ifc_alloc_unit(ifc, &unit);
925f889d2efSBrooks Davis if (err != 0)
926f889d2efSBrooks Davis return (err);
927f889d2efSBrooks Davis
928f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */
929f889d2efSBrooks Davis if (wildcard) {
930f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++);
931f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) >
932f889d2efSBrooks Davis len - (dp-name) - 1) {
933f889d2efSBrooks Davis /*
934f889d2efSBrooks Davis * This can only be a programmer error and
935f889d2efSBrooks Davis * there's no straightforward way to recover if
936f889d2efSBrooks Davis * it happens.
937f889d2efSBrooks Davis */
938f889d2efSBrooks Davis panic("if_clone_create(): interface name too long");
939f889d2efSBrooks Davis }
940f889d2efSBrooks Davis }
94109ee0fc0SAlexander V. Chernikov *punit = unit;
942f889d2efSBrooks Davis
943f889d2efSBrooks Davis return (0);
944f889d2efSBrooks Davis }
945f889d2efSBrooks Davis
94609ee0fc0SAlexander V. Chernikov int
ifc_copyin(const struct ifc_data * ifd,void * target,size_t len)94709ee0fc0SAlexander V. Chernikov ifc_copyin(const struct ifc_data *ifd, void *target, size_t len)
948f889d2efSBrooks Davis {
94909ee0fc0SAlexander V. Chernikov if (ifd->params == NULL)
950f889d2efSBrooks Davis return (EINVAL);
951f889d2efSBrooks Davis
95209ee0fc0SAlexander V. Chernikov if (ifd->flags & IFC_F_SYSSPACE) {
95309ee0fc0SAlexander V. Chernikov memcpy(target, ifd->params, len);
954f889d2efSBrooks Davis return (0);
95509ee0fc0SAlexander V. Chernikov } else
95609ee0fc0SAlexander V. Chernikov return (copyin(ifd->params, target, len));
957f889d2efSBrooks Davis }
958