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);
131*1ba65514SZhenlei Huang VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners) = LIST_HEAD_INITIALIZER();
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
186f889d2efSBrooks Davis /*
1874e7e0183SAndrew Thompson * Lookup and create a clone network interface.
188f889d2efSBrooks Davis */
189f889d2efSBrooks Davis int
ifc_create_ifp(const char * name,struct ifc_data * ifd,struct ifnet ** ifpp)190089104e0SAlexander V. Chernikov ifc_create_ifp(const char *name, struct ifc_data *ifd, struct ifnet **ifpp)
191f889d2efSBrooks Davis {
192089104e0SAlexander V. Chernikov struct if_clone *ifc = ifc_find_cloner_match(name);
193f889d2efSBrooks Davis
194f889d2efSBrooks Davis if (ifc == NULL)
195f889d2efSBrooks Davis return (EINVAL);
196f889d2efSBrooks Davis
197089104e0SAlexander V. Chernikov struct ifc_data_nl ifd_new = {
198089104e0SAlexander V. Chernikov .flags = ifd->flags,
199089104e0SAlexander V. Chernikov .unit = ifd->unit,
200089104e0SAlexander V. Chernikov .params = ifd->params,
201089104e0SAlexander V. Chernikov };
202089104e0SAlexander V. Chernikov
203089104e0SAlexander V. Chernikov int error = if_clone_createif_nl(ifc, name, &ifd_new);
204089104e0SAlexander V. Chernikov
20509ee0fc0SAlexander V. Chernikov if (ifpp != NULL)
206089104e0SAlexander V. Chernikov *ifpp = ifd_new.ifp;
20709ee0fc0SAlexander V. Chernikov
20809ee0fc0SAlexander V. Chernikov return (error);
20909ee0fc0SAlexander V. Chernikov }
21009ee0fc0SAlexander V. Chernikov
211089104e0SAlexander V. Chernikov bool
ifc_create_ifp_nl(const char * name,struct ifc_data_nl * ifd)212089104e0SAlexander V. Chernikov ifc_create_ifp_nl(const char *name, struct ifc_data_nl *ifd)
213089104e0SAlexander V. Chernikov {
214089104e0SAlexander V. Chernikov struct if_clone *ifc = ifc_find_cloner_match(name);
215089104e0SAlexander V. Chernikov if (ifc == NULL) {
216089104e0SAlexander V. Chernikov ifd->error = EINVAL;
217089104e0SAlexander V. Chernikov return (false);
218089104e0SAlexander V. Chernikov }
219089104e0SAlexander V. Chernikov
220089104e0SAlexander V. Chernikov ifd->error = if_clone_createif_nl(ifc, name, ifd);
221089104e0SAlexander V. Chernikov
222089104e0SAlexander V. Chernikov return (true);
223089104e0SAlexander V. Chernikov }
224089104e0SAlexander V. Chernikov
22509ee0fc0SAlexander V. Chernikov int
if_clone_create(char * name,size_t len,caddr_t params)22609ee0fc0SAlexander V. Chernikov if_clone_create(char *name, size_t len, caddr_t params)
22709ee0fc0SAlexander V. Chernikov {
22809ee0fc0SAlexander V. Chernikov struct ifc_data ifd = { .params = params };
22909ee0fc0SAlexander V. Chernikov struct ifnet *ifp;
23009ee0fc0SAlexander V. Chernikov
23109ee0fc0SAlexander V. Chernikov int error = ifc_create_ifp(name, &ifd, &ifp);
23209ee0fc0SAlexander V. Chernikov
23309ee0fc0SAlexander V. Chernikov if (error == 0)
23409ee0fc0SAlexander V. Chernikov strlcpy(name, if_name(ifp), len);
23509ee0fc0SAlexander V. Chernikov
23609ee0fc0SAlexander V. Chernikov return (error);
2374e7e0183SAndrew Thompson }
2384e7e0183SAndrew Thompson
239089104e0SAlexander V. Chernikov bool
ifc_modify_ifp_nl(struct ifnet * ifp,struct ifc_data_nl * ifd)240089104e0SAlexander V. Chernikov ifc_modify_ifp_nl(struct ifnet *ifp, struct ifc_data_nl *ifd)
241089104e0SAlexander V. Chernikov {
242089104e0SAlexander V. Chernikov struct if_clone *ifc = ifc_find_cloner(ifp->if_dname);
243089104e0SAlexander V. Chernikov if (ifc == NULL) {
244089104e0SAlexander V. Chernikov ifd->error = EINVAL;
245089104e0SAlexander V. Chernikov return (false);
246089104e0SAlexander V. Chernikov }
247089104e0SAlexander V. Chernikov
248089104e0SAlexander V. Chernikov ifd->error = (*ifc->modify_nl)(ifp, ifd);
249089104e0SAlexander V. Chernikov return (true);
250089104e0SAlexander V. Chernikov }
251089104e0SAlexander V. Chernikov
252089104e0SAlexander V. Chernikov bool
ifc_dump_ifp_nl(struct ifnet * ifp,struct nl_writer * nw)253089104e0SAlexander V. Chernikov ifc_dump_ifp_nl(struct ifnet *ifp, struct nl_writer *nw)
254089104e0SAlexander V. Chernikov {
255089104e0SAlexander V. Chernikov struct if_clone *ifc = ifc_find_cloner(ifp->if_dname);
256089104e0SAlexander V. Chernikov if (ifc == NULL)
257089104e0SAlexander V. Chernikov return (false);
258089104e0SAlexander V. Chernikov
259089104e0SAlexander V. Chernikov (*ifc->dump_nl)(ifp, nw);
260089104e0SAlexander V. Chernikov return (true);
261089104e0SAlexander V. Chernikov }
262089104e0SAlexander V. Chernikov
263089104e0SAlexander V. Chernikov static int
ifc_create_ifp_nl_default(struct if_clone * ifc,char * name,size_t len,struct ifc_data_nl * ifd)264089104e0SAlexander V. Chernikov ifc_create_ifp_nl_default(struct if_clone *ifc, char *name, size_t len,
265089104e0SAlexander V. Chernikov struct ifc_data_nl *ifd)
266089104e0SAlexander V. Chernikov {
267089104e0SAlexander V. Chernikov struct ifc_data ifd_new = {
268089104e0SAlexander V. Chernikov .flags = ifd->flags,
269089104e0SAlexander V. Chernikov .unit = ifd->unit,
270089104e0SAlexander V. Chernikov .params = ifd->params,
271089104e0SAlexander V. Chernikov };
272089104e0SAlexander V. Chernikov
273089104e0SAlexander V. Chernikov return ((*ifc->ifc_create)(ifc, name, len, &ifd_new, &ifd->ifp));
274089104e0SAlexander V. Chernikov }
275089104e0SAlexander V. Chernikov
276089104e0SAlexander V. Chernikov static int
ifc_modify_ifp_nl_default(struct ifnet * ifp,struct ifc_data_nl * ifd)277089104e0SAlexander V. Chernikov ifc_modify_ifp_nl_default(struct ifnet *ifp, struct ifc_data_nl *ifd)
278089104e0SAlexander V. Chernikov {
279089104e0SAlexander V. Chernikov if (ifd->lattrs != NULL)
280089104e0SAlexander V. Chernikov return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt));
281089104e0SAlexander V. Chernikov return (0);
282089104e0SAlexander V. Chernikov }
283089104e0SAlexander V. Chernikov
284089104e0SAlexander V. Chernikov static void
ifc_dump_ifp_nl_default(struct ifnet * ifp,struct nl_writer * nw)285089104e0SAlexander V. Chernikov ifc_dump_ifp_nl_default(struct ifnet *ifp, struct nl_writer *nw)
286089104e0SAlexander V. Chernikov {
287089104e0SAlexander V. Chernikov int off = nlattr_add_nested(nw, IFLA_LINKINFO);
288089104e0SAlexander V. Chernikov
289089104e0SAlexander V. Chernikov if (off != 0) {
290089104e0SAlexander V. Chernikov nlattr_add_string(nw, IFLA_INFO_KIND, ifp->if_dname);
291089104e0SAlexander V. Chernikov nlattr_set_len(nw, off);
292089104e0SAlexander V. Chernikov }
293089104e0SAlexander V. Chernikov }
294089104e0SAlexander V. Chernikov
295b02fd8b7SKristof Provost void
ifc_link_ifp(struct if_clone * ifc,struct ifnet * ifp)29626c190d2SAlexander V. Chernikov ifc_link_ifp(struct if_clone *ifc, struct ifnet *ifp)
297b02fd8b7SKristof Provost {
298b02fd8b7SKristof Provost
299b02fd8b7SKristof Provost if_addgroup(ifp, ifc->ifc_name);
300b02fd8b7SKristof Provost
301b02fd8b7SKristof Provost IF_CLONE_LOCK(ifc);
302b02fd8b7SKristof Provost IFC_IFLIST_INSERT(ifc, ifp);
303b02fd8b7SKristof Provost IF_CLONE_UNLOCK(ifc);
304b02fd8b7SKristof Provost }
305b02fd8b7SKristof Provost
30626c190d2SAlexander V. Chernikov void
if_clone_addif(struct if_clone * ifc,struct ifnet * ifp)30726c190d2SAlexander V. Chernikov if_clone_addif(struct if_clone *ifc, struct ifnet *ifp)
30826c190d2SAlexander V. Chernikov {
30926c190d2SAlexander V. Chernikov ifc_link_ifp(ifc, ifp);
31026c190d2SAlexander V. Chernikov }
31126c190d2SAlexander V. Chernikov
31226c190d2SAlexander V. Chernikov bool
ifc_unlink_ifp(struct if_clone * ifc,struct ifnet * ifp)31326c190d2SAlexander V. Chernikov ifc_unlink_ifp(struct if_clone *ifc, struct ifnet *ifp)
31426c190d2SAlexander V. Chernikov {
31526c190d2SAlexander V. Chernikov struct ifnet *ifcifp;
31626c190d2SAlexander V. Chernikov
31726c190d2SAlexander V. Chernikov IF_CLONE_LOCK(ifc);
31826c190d2SAlexander V. Chernikov LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
31926c190d2SAlexander V. Chernikov if (ifcifp == ifp) {
32026c190d2SAlexander V. Chernikov IFC_IFLIST_REMOVE(ifc, ifp);
32126c190d2SAlexander V. Chernikov break;
32226c190d2SAlexander V. Chernikov }
32326c190d2SAlexander V. Chernikov }
32426c190d2SAlexander V. Chernikov IF_CLONE_UNLOCK(ifc);
32526c190d2SAlexander V. Chernikov
3267ff9ae90SMarius Strobl if (ifcifp != NULL)
32726c190d2SAlexander V. Chernikov if_delgroup(ifp, ifc->ifc_name);
32826c190d2SAlexander V. Chernikov
32926c190d2SAlexander V. Chernikov return (ifcifp != NULL);
33026c190d2SAlexander V. Chernikov }
33126c190d2SAlexander V. Chernikov
33226c190d2SAlexander V. Chernikov static struct if_clone *
ifc_find_cloner_match(const char * name)33383b5c80cSAlexander V. Chernikov ifc_find_cloner_match(const char *name)
33426c190d2SAlexander V. Chernikov {
33526c190d2SAlexander V. Chernikov struct if_clone *ifc;
33626c190d2SAlexander V. Chernikov
33783b5c80cSAlexander V. Chernikov IF_CLONERS_LOCK();
33883b5c80cSAlexander V. Chernikov LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
33983b5c80cSAlexander V. Chernikov if (ifc->ifc_match(ifc, name))
34083b5c80cSAlexander V. Chernikov break;
34183b5c80cSAlexander V. Chernikov }
34283b5c80cSAlexander V. Chernikov IF_CLONERS_UNLOCK();
34383b5c80cSAlexander V. Chernikov
34483b5c80cSAlexander V. Chernikov return (ifc);
34583b5c80cSAlexander V. Chernikov }
34683b5c80cSAlexander V. Chernikov
34783b5c80cSAlexander V. Chernikov static struct if_clone *
ifc_find_cloner(const char * name)34883b5c80cSAlexander V. Chernikov ifc_find_cloner(const char *name)
34983b5c80cSAlexander V. Chernikov {
35083b5c80cSAlexander V. Chernikov struct if_clone *ifc;
35183b5c80cSAlexander V. Chernikov
35226c190d2SAlexander V. Chernikov IF_CLONERS_LOCK();
35326c190d2SAlexander V. Chernikov LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
35426c190d2SAlexander V. Chernikov if (strcmp(ifc->ifc_name, name) == 0) {
35526c190d2SAlexander V. Chernikov break;
35626c190d2SAlexander V. Chernikov }
35726c190d2SAlexander V. Chernikov }
35826c190d2SAlexander V. Chernikov IF_CLONERS_UNLOCK();
35983b5c80cSAlexander V. Chernikov
36083b5c80cSAlexander V. Chernikov return (ifc);
36183b5c80cSAlexander V. Chernikov }
36283b5c80cSAlexander V. Chernikov
36383b5c80cSAlexander V. Chernikov static struct if_clone *
ifc_find_cloner_in_vnet(const char * name,struct vnet * vnet)36483b5c80cSAlexander V. Chernikov ifc_find_cloner_in_vnet(const char *name, struct vnet *vnet)
36583b5c80cSAlexander V. Chernikov {
36683b5c80cSAlexander V. Chernikov CURVNET_SET_QUIET(vnet);
36783b5c80cSAlexander V. Chernikov struct if_clone *ifc = ifc_find_cloner(name);
36826c190d2SAlexander V. Chernikov CURVNET_RESTORE();
36926c190d2SAlexander V. Chernikov
37026c190d2SAlexander V. Chernikov return (ifc);
37126c190d2SAlexander V. Chernikov }
37226c190d2SAlexander V. Chernikov
3734e7e0183SAndrew Thompson /*
3744e7e0183SAndrew Thompson * Create a clone network interface.
3754e7e0183SAndrew Thompson */
3764e7e0183SAndrew Thompson static int
if_clone_createif_nl(struct if_clone * ifc,const char * ifname,struct ifc_data_nl * ifd)377089104e0SAlexander V. Chernikov if_clone_createif_nl(struct if_clone *ifc, const char *ifname, struct ifc_data_nl *ifd)
3784e7e0183SAndrew Thompson {
379089104e0SAlexander V. Chernikov char name[IFNAMSIZ];
380089104e0SAlexander V. Chernikov int error;
381089104e0SAlexander V. Chernikov
382089104e0SAlexander V. Chernikov strlcpy(name, ifname, sizeof(name));
3834e7e0183SAndrew Thompson
3844e7e0183SAndrew Thompson if (ifunit(name) != NULL)
3854e7e0183SAndrew Thompson return (EEXIST);
3864e7e0183SAndrew Thompson
38709ee0fc0SAlexander V. Chernikov if (ifc->ifc_flags & IFC_F_AUTOUNIT) {
388089104e0SAlexander V. Chernikov if ((error = ifc_handle_unit(ifc, name, sizeof(name), &ifd->unit)) != 0)
389089104e0SAlexander V. Chernikov return (error);
3904e7e0183SAndrew Thompson }
39109ee0fc0SAlexander V. Chernikov
392089104e0SAlexander V. Chernikov if (ifd->lattrs != NULL)
393089104e0SAlexander V. Chernikov error = (*ifc->create_nl)(ifc, name, sizeof(name), ifd);
394089104e0SAlexander V. Chernikov else
395089104e0SAlexander V. Chernikov error = ifc_create_ifp_nl_default(ifc, name, sizeof(name), ifd);
396089104e0SAlexander V. Chernikov if (error != 0) {
397089104e0SAlexander V. Chernikov if (ifc->ifc_flags & IFC_F_AUTOUNIT)
398089104e0SAlexander V. Chernikov ifc_free_unit(ifc, ifd->unit);
399089104e0SAlexander V. Chernikov return (error);
400089104e0SAlexander V. Chernikov }
4014e7e0183SAndrew Thompson
402089104e0SAlexander V. Chernikov MPASS(ifd->ifp != NULL);
403089104e0SAlexander V. Chernikov if_clone_addif(ifc, ifd->ifp);
404089104e0SAlexander V. Chernikov
405089104e0SAlexander V. Chernikov if (ifd->lattrs != NULL)
406089104e0SAlexander V. Chernikov error = (*ifc->modify_nl)(ifd->ifp, ifd);
407089104e0SAlexander V. Chernikov
408089104e0SAlexander V. Chernikov return (error);
409f889d2efSBrooks Davis }
410f889d2efSBrooks Davis
411f889d2efSBrooks Davis /*
4124e7e0183SAndrew Thompson * Lookup and destroy a clone network interface.
413f889d2efSBrooks Davis */
414f889d2efSBrooks Davis int
if_clone_destroy(const char * name)415f889d2efSBrooks Davis if_clone_destroy(const char *name)
416f889d2efSBrooks Davis {
417d0088cdeSBjoern A. Zeeb int err;
418f889d2efSBrooks Davis struct if_clone *ifc;
419f889d2efSBrooks Davis struct ifnet *ifp;
420f889d2efSBrooks Davis
421d0088cdeSBjoern A. Zeeb ifp = ifunit_ref(name);
422f889d2efSBrooks Davis if (ifp == NULL)
423f889d2efSBrooks Davis return (ENXIO);
424f889d2efSBrooks Davis
42583b5c80cSAlexander V. Chernikov ifc = ifc_find_cloner_in_vnet(ifp->if_dname, ifp->if_home_vnet);
426d0088cdeSBjoern A. Zeeb if (ifc == NULL) {
427d0088cdeSBjoern A. Zeeb if_rele(ifp);
428f889d2efSBrooks Davis return (EINVAL);
429d0088cdeSBjoern A. Zeeb }
430f889d2efSBrooks Davis
431d0088cdeSBjoern A. Zeeb err = if_clone_destroyif(ifc, ifp);
432d0088cdeSBjoern A. Zeeb if_rele(ifp);
433d0088cdeSBjoern A. Zeeb return err;
4344e7e0183SAndrew Thompson }
4354e7e0183SAndrew Thompson
4364e7e0183SAndrew Thompson /*
4374e7e0183SAndrew Thompson * Destroy a clone network interface.
4384e7e0183SAndrew Thompson */
43909ee0fc0SAlexander V. Chernikov static int
if_clone_destroyif_flags(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)44009ee0fc0SAlexander V. Chernikov if_clone_destroyif_flags(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
4414e7e0183SAndrew Thompson {
4424e7e0183SAndrew Thompson int err;
4434e7e0183SAndrew Thompson
44437f17770SMarko Zec /*
44537f17770SMarko Zec * Given that the cloned ifnet might be attached to a different
44637f17770SMarko Zec * vnet from where its cloner was registered, we have to
44737f17770SMarko Zec * switch to the vnet context of the target vnet.
44837f17770SMarko Zec */
44937f17770SMarko Zec CURVNET_SET_QUIET(ifp->if_vnet);
45037f17770SMarko Zec
45126c190d2SAlexander V. Chernikov if (!ifc_unlink_ifp(ifc, ifp)) {
452c769e1beSBjoern A. Zeeb CURVNET_RESTORE();
453c769e1beSBjoern A. Zeeb return (ENXIO); /* ifp is not on the list. */
454c769e1beSBjoern A. Zeeb }
4550dad3f0eSMax Laier
45609ee0fc0SAlexander V. Chernikov int unit = ifp->if_dunit;
45709ee0fc0SAlexander V. Chernikov err = (*ifc->ifc_destroy)(ifc, ifp, flags);
458f889d2efSBrooks Davis
45926c190d2SAlexander V. Chernikov if (err != 0)
46026c190d2SAlexander V. Chernikov ifc_link_ifp(ifc, ifp);
46126c190d2SAlexander V. Chernikov else if (ifc->ifc_flags & IFC_F_AUTOUNIT)
46209ee0fc0SAlexander V. Chernikov ifc_free_unit(ifc, unit);
46321ca7b57SMarko Zec CURVNET_RESTORE();
464f889d2efSBrooks Davis return (err);
465f889d2efSBrooks Davis }
466f889d2efSBrooks Davis
46709ee0fc0SAlexander V. Chernikov int
if_clone_destroyif(struct if_clone * ifc,struct ifnet * ifp)46809ee0fc0SAlexander V. Chernikov if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
46909ee0fc0SAlexander V. Chernikov {
47009ee0fc0SAlexander V. Chernikov return (if_clone_destroyif_flags(ifc, ifp, 0));
47109ee0fc0SAlexander V. Chernikov }
47209ee0fc0SAlexander V. Chernikov
47342a58907SGleb Smirnoff static struct if_clone *
if_clone_alloc(const char * name,int maxunit)47442a58907SGleb Smirnoff if_clone_alloc(const char *name, int maxunit)
47542a58907SGleb Smirnoff {
47642a58907SGleb Smirnoff struct if_clone *ifc;
47742a58907SGleb Smirnoff
47842a58907SGleb Smirnoff KASSERT(name != NULL, ("%s: no name\n", __func__));
479a2cac544SZhenlei Huang MPASS(maxunit >= 0);
48042a58907SGleb Smirnoff
48142a58907SGleb Smirnoff ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO);
48242a58907SGleb Smirnoff strncpy(ifc->ifc_name, name, IFCLOSIZ-1);
48342a58907SGleb Smirnoff IF_CLONE_LOCK_INIT(ifc);
48442a58907SGleb Smirnoff IF_CLONE_ADDREF(ifc);
485a2cac544SZhenlei Huang ifc->ifc_maxunit = maxunit;
48642a58907SGleb Smirnoff ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx);
48742a58907SGleb Smirnoff LIST_INIT(&ifc->ifc_iflist);
48842a58907SGleb Smirnoff
489089104e0SAlexander V. Chernikov ifc->create_nl = ifc_create_ifp_nl_default;
490089104e0SAlexander V. Chernikov ifc->modify_nl = ifc_modify_ifp_nl_default;
491089104e0SAlexander V. Chernikov ifc->dump_nl = ifc_dump_ifp_nl_default;
492089104e0SAlexander V. Chernikov
49342a58907SGleb Smirnoff return (ifc);
49442a58907SGleb Smirnoff }
49542a58907SGleb Smirnoff
49642a58907SGleb Smirnoff static int
if_clone_attach(struct if_clone * ifc)497f889d2efSBrooks Davis if_clone_attach(struct if_clone *ifc)
498f889d2efSBrooks Davis {
4992e9fff5bSGleb Smirnoff struct if_clone *ifc1;
500f889d2efSBrooks Davis
501f889d2efSBrooks Davis IF_CLONERS_LOCK();
5022e9fff5bSGleb Smirnoff LIST_FOREACH(ifc1, &V_if_cloners, ifc_list)
5032e9fff5bSGleb Smirnoff if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) {
5042e9fff5bSGleb Smirnoff IF_CLONERS_UNLOCK();
5052e9fff5bSGleb Smirnoff IF_CLONE_REMREF(ifc);
5062e9fff5bSGleb Smirnoff return (EEXIST);
5072e9fff5bSGleb Smirnoff }
50837f17770SMarko Zec LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
50937f17770SMarko Zec V_if_cloners_count++;
510f889d2efSBrooks Davis IF_CLONERS_UNLOCK();
511f889d2efSBrooks Davis
51242a58907SGleb Smirnoff return (0);
51342a58907SGleb Smirnoff }
51442a58907SGleb Smirnoff
51542a58907SGleb Smirnoff struct if_clone *
ifc_attach_cloner(const char * name,struct if_clone_addreq * req)51609ee0fc0SAlexander V. Chernikov ifc_attach_cloner(const char *name, struct if_clone_addreq *req)
51742a58907SGleb Smirnoff {
518a2cac544SZhenlei Huang int maxunit;
519a2cac544SZhenlei Huang struct if_clone *ifc;
520a2cac544SZhenlei Huang
52109ee0fc0SAlexander V. Chernikov if (req->create_f == NULL || req->destroy_f == NULL)
52209ee0fc0SAlexander V. Chernikov return (NULL);
52309ee0fc0SAlexander V. Chernikov if (strnlen(name, IFCLOSIZ) >= (IFCLOSIZ - 1))
52409ee0fc0SAlexander V. Chernikov return (NULL);
52542a58907SGleb Smirnoff
526a2cac544SZhenlei Huang maxunit = (req->flags & IFC_F_LIMITUNIT) ? req->maxunit : IF_MAXUNIT;
527a2cac544SZhenlei Huang ifc = if_clone_alloc(name, maxunit);
52809ee0fc0SAlexander V. Chernikov ifc->ifc_match = req->match_f != NULL ? req->match_f : ifc_simple_match;
52909ee0fc0SAlexander V. Chernikov ifc->ifc_create = req->create_f;
53009ee0fc0SAlexander V. Chernikov ifc->ifc_destroy = req->destroy_f;
5317ff9ae90SMarius Strobl ifc->ifc_flags = (req->flags & IFC_F_AUTOUNIT);
53242a58907SGleb Smirnoff
533089104e0SAlexander V. Chernikov if (req->version == 2) {
534089104e0SAlexander V. Chernikov struct if_clone_addreq_v2 *req2 = (struct if_clone_addreq_v2 *)req;
535089104e0SAlexander V. Chernikov
536089104e0SAlexander V. Chernikov ifc->create_nl = req2->create_nl_f;
537089104e0SAlexander V. Chernikov ifc->modify_nl = req2->modify_nl_f;
538089104e0SAlexander V. Chernikov ifc->dump_nl = req2->dump_nl_f;
539089104e0SAlexander V. Chernikov }
540089104e0SAlexander V. Chernikov
541089104e0SAlexander V. Chernikov ifc->dump_nl = ifc_dump_ifp_nl_default;
542089104e0SAlexander V. Chernikov
5433395dd6eSAlexander Kabaev if (if_clone_attach(ifc) != 0)
54442a58907SGleb Smirnoff return (NULL);
54542a58907SGleb Smirnoff
546f889d2efSBrooks Davis EVENTHANDLER_INVOKE(if_clone_event, ifc);
5472e9fff5bSGleb Smirnoff
54842a58907SGleb Smirnoff return (ifc);
54942a58907SGleb Smirnoff }
55042a58907SGleb Smirnoff
55109ee0fc0SAlexander V. Chernikov void
ifc_detach_cloner(struct if_clone * ifc)55209ee0fc0SAlexander V. Chernikov ifc_detach_cloner(struct if_clone *ifc)
55309ee0fc0SAlexander V. Chernikov {
55409ee0fc0SAlexander V. Chernikov if_clone_detach(ifc);
55509ee0fc0SAlexander V. Chernikov }
55609ee0fc0SAlexander V. Chernikov
55709ee0fc0SAlexander V. Chernikov
55809ee0fc0SAlexander V. Chernikov #ifdef CLONE_COMPAT_13
55909ee0fc0SAlexander V. Chernikov
56009ee0fc0SAlexander 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)56109ee0fc0SAlexander V. Chernikov ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
56209ee0fc0SAlexander V. Chernikov struct ifc_data *ifc_data, struct ifnet **ifpp)
56309ee0fc0SAlexander V. Chernikov {
56409ee0fc0SAlexander V. Chernikov int error = ifc->ifca_create(ifc, name, maxlen, ifc_data->params);
56509ee0fc0SAlexander V. Chernikov
56609ee0fc0SAlexander V. Chernikov if (error == 0)
56709ee0fc0SAlexander V. Chernikov *ifpp = ifunit(name);
56809ee0fc0SAlexander V. Chernikov return (error);
56909ee0fc0SAlexander V. Chernikov }
57009ee0fc0SAlexander V. Chernikov
57109ee0fc0SAlexander V. Chernikov static int
ifc_advanced_destroy_wrapper(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)57209ee0fc0SAlexander V. Chernikov ifc_advanced_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
57309ee0fc0SAlexander V. Chernikov {
57409ee0fc0SAlexander V. Chernikov if (ifc->ifca_destroy == NULL)
57509ee0fc0SAlexander V. Chernikov return (ENOTSUP);
57609ee0fc0SAlexander V. Chernikov return (ifc->ifca_destroy(ifc, ifp));
57709ee0fc0SAlexander V. Chernikov }
57809ee0fc0SAlexander V. Chernikov
57909ee0fc0SAlexander 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)58009ee0fc0SAlexander V. Chernikov if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
58109ee0fc0SAlexander V. Chernikov ifc_create_t create, ifc_destroy_t destroy)
58209ee0fc0SAlexander V. Chernikov {
58309ee0fc0SAlexander V. Chernikov struct if_clone *ifc;
58409ee0fc0SAlexander V. Chernikov
585a2cac544SZhenlei Huang ifc = if_clone_alloc(name, maxunit ? maxunit : IF_MAXUNIT);
58609ee0fc0SAlexander V. Chernikov ifc->ifc_match = match;
58709ee0fc0SAlexander V. Chernikov ifc->ifc_create = ifc_advanced_create_wrapper;
58809ee0fc0SAlexander V. Chernikov ifc->ifc_destroy = ifc_advanced_destroy_wrapper;
58909ee0fc0SAlexander V. Chernikov ifc->ifca_destroy = destroy;
59009ee0fc0SAlexander V. Chernikov ifc->ifca_create = create;
59109ee0fc0SAlexander V. Chernikov
59209ee0fc0SAlexander V. Chernikov if (if_clone_attach(ifc) != 0)
59309ee0fc0SAlexander V. Chernikov return (NULL);
59409ee0fc0SAlexander V. Chernikov
59509ee0fc0SAlexander V. Chernikov EVENTHANDLER_INVOKE(if_clone_event, ifc);
59609ee0fc0SAlexander V. Chernikov
59709ee0fc0SAlexander V. Chernikov return (ifc);
59809ee0fc0SAlexander V. Chernikov }
59909ee0fc0SAlexander V. Chernikov
60009ee0fc0SAlexander 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)60109ee0fc0SAlexander V. Chernikov ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
60209ee0fc0SAlexander V. Chernikov struct ifc_data *ifc_data, struct ifnet **ifpp)
60309ee0fc0SAlexander V. Chernikov {
60409ee0fc0SAlexander V. Chernikov int unit = 0;
60509ee0fc0SAlexander V. Chernikov
60609ee0fc0SAlexander V. Chernikov ifc_name2unit(name, &unit);
60709ee0fc0SAlexander V. Chernikov int error = ifc->ifcs_create(ifc, unit, ifc_data->params);
60809ee0fc0SAlexander V. Chernikov if (error == 0)
60909ee0fc0SAlexander V. Chernikov *ifpp = ifunit(name);
61009ee0fc0SAlexander V. Chernikov return (error);
61109ee0fc0SAlexander V. Chernikov }
61209ee0fc0SAlexander V. Chernikov
61309ee0fc0SAlexander V. Chernikov static int
ifc_simple_destroy_wrapper(struct if_clone * ifc,struct ifnet * ifp,uint32_t flags)61409ee0fc0SAlexander V. Chernikov ifc_simple_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
61509ee0fc0SAlexander V. Chernikov {
61609ee0fc0SAlexander V. Chernikov if (ifp->if_dunit < ifc->ifcs_minifs && (flags & IFC_F_FORCE) == 0)
61709ee0fc0SAlexander V. Chernikov return (EINVAL);
61809ee0fc0SAlexander V. Chernikov
61909ee0fc0SAlexander V. Chernikov ifc->ifcs_destroy(ifp);
62009ee0fc0SAlexander V. Chernikov return (0);
62109ee0fc0SAlexander V. Chernikov }
62209ee0fc0SAlexander V. Chernikov
62342a58907SGleb Smirnoff struct if_clone *
if_clone_simple(const char * name,ifcs_create_t create,ifcs_destroy_t destroy,u_int minifs)62442a58907SGleb Smirnoff if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
62542a58907SGleb Smirnoff u_int minifs)
62642a58907SGleb Smirnoff {
62742a58907SGleb Smirnoff struct if_clone *ifc;
62842a58907SGleb Smirnoff u_int unit;
62942a58907SGleb Smirnoff
630a2cac544SZhenlei Huang ifc = if_clone_alloc(name, IF_MAXUNIT);
63109ee0fc0SAlexander V. Chernikov ifc->ifc_match = ifc_simple_match;
63209ee0fc0SAlexander V. Chernikov ifc->ifc_create = ifc_simple_create_wrapper;
63309ee0fc0SAlexander V. Chernikov ifc->ifc_destroy = ifc_simple_destroy_wrapper;
63442a58907SGleb Smirnoff ifc->ifcs_create = create;
63542a58907SGleb Smirnoff ifc->ifcs_destroy = destroy;
63642a58907SGleb Smirnoff ifc->ifcs_minifs = minifs;
63709ee0fc0SAlexander V. Chernikov ifc->ifc_flags = IFC_F_AUTOUNIT;
63842a58907SGleb Smirnoff
6393395dd6eSAlexander Kabaev if (if_clone_attach(ifc) != 0)
64042a58907SGleb Smirnoff return (NULL);
64142a58907SGleb Smirnoff
64242a58907SGleb Smirnoff for (unit = 0; unit < minifs; unit++) {
64342a58907SGleb Smirnoff char name[IFNAMSIZ];
64446d0f824SMatt Macy int error __unused;
645089104e0SAlexander V. Chernikov struct ifc_data_nl ifd = {};
64642a58907SGleb Smirnoff
64742a58907SGleb Smirnoff snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
648089104e0SAlexander V. Chernikov error = if_clone_createif_nl(ifc, name, &ifd);
64942a58907SGleb Smirnoff KASSERT(error == 0,
65042a58907SGleb Smirnoff ("%s: failed to create required interface %s",
65142a58907SGleb Smirnoff __func__, name));
65242a58907SGleb Smirnoff }
65342a58907SGleb Smirnoff
65442a58907SGleb Smirnoff EVENTHANDLER_INVOKE(if_clone_event, ifc);
65542a58907SGleb Smirnoff
65642a58907SGleb Smirnoff return (ifc);
657f889d2efSBrooks Davis }
65809ee0fc0SAlexander V. Chernikov #endif
659f889d2efSBrooks Davis
660f889d2efSBrooks Davis /*
661f889d2efSBrooks Davis * Unregister a network interface cloner.
662f889d2efSBrooks Davis */
663f889d2efSBrooks Davis void
if_clone_detach(struct if_clone * ifc)664f889d2efSBrooks Davis if_clone_detach(struct if_clone *ifc)
665f889d2efSBrooks Davis {
666f889d2efSBrooks Davis
667f889d2efSBrooks Davis IF_CLONERS_LOCK();
668f889d2efSBrooks Davis LIST_REMOVE(ifc, ifc_list);
66937f17770SMarko Zec V_if_cloners_count--;
670f889d2efSBrooks Davis IF_CLONERS_UNLOCK();
671f889d2efSBrooks Davis
6724e7e0183SAndrew Thompson /* destroy all interfaces for this cloner */
6734e7e0183SAndrew Thompson while (!LIST_EMPTY(&ifc->ifc_iflist))
67409ee0fc0SAlexander V. Chernikov if_clone_destroyif_flags(ifc, LIST_FIRST(&ifc->ifc_iflist), IFC_F_FORCE);
6754e7e0183SAndrew Thompson
676f889d2efSBrooks Davis IF_CLONE_REMREF(ifc);
677f889d2efSBrooks Davis }
678f889d2efSBrooks Davis
679f889d2efSBrooks Davis static void
if_clone_free(struct if_clone * ifc)680f889d2efSBrooks Davis if_clone_free(struct if_clone *ifc)
681f889d2efSBrooks Davis {
682f889d2efSBrooks Davis
6834e7e0183SAndrew Thompson KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
6844e7e0183SAndrew Thompson ("%s: ifc_iflist not empty", __func__));
6854e7e0183SAndrew Thompson
686f889d2efSBrooks Davis IF_CLONE_LOCK_DESTROY(ifc);
6872e9fff5bSGleb Smirnoff delete_unrhdr(ifc->ifc_unrhdr);
68842a58907SGleb Smirnoff free(ifc, M_CLONE);
689f889d2efSBrooks Davis }
690f889d2efSBrooks Davis
691f889d2efSBrooks Davis /*
692f889d2efSBrooks Davis * Provide list of interface cloners to userspace.
693f889d2efSBrooks Davis */
694f889d2efSBrooks Davis int
if_clone_list(struct if_clonereq * ifcr)695f889d2efSBrooks Davis if_clone_list(struct if_clonereq *ifcr)
696f889d2efSBrooks Davis {
697c859ef97SBrooks Davis char *buf, *dst, *outbuf = NULL;
698f889d2efSBrooks Davis struct if_clone *ifc;
699c859ef97SBrooks Davis int buf_count, count, err = 0;
700c859ef97SBrooks Davis
701a6d00835SMaxim Konovalov if (ifcr->ifcr_count < 0)
702a6d00835SMaxim Konovalov return (EINVAL);
703a6d00835SMaxim Konovalov
704c859ef97SBrooks Davis IF_CLONERS_LOCK();
705c859ef97SBrooks Davis /*
706c859ef97SBrooks Davis * Set our internal output buffer size. We could end up not
707c859ef97SBrooks Davis * reporting a cloner that is added between the unlock and lock
708c859ef97SBrooks Davis * below, but that's not a major problem. Not caping our
709c859ef97SBrooks Davis * allocation to the number of cloners actually in the system
710c859ef97SBrooks Davis * could be because that would let arbitrary users cause us to
711a4641f4eSPedro F. Giffuni * allocate arbitrary amounts of kernel memory.
712c859ef97SBrooks Davis */
71337f17770SMarko Zec buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
71437f17770SMarko Zec V_if_cloners_count : ifcr->ifcr_count;
715c859ef97SBrooks Davis IF_CLONERS_UNLOCK();
716c859ef97SBrooks Davis
717c859ef97SBrooks Davis outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
718f889d2efSBrooks Davis
719f889d2efSBrooks Davis IF_CLONERS_LOCK();
720f889d2efSBrooks Davis
72137f17770SMarko Zec ifcr->ifcr_total = V_if_cloners_count;
722f889d2efSBrooks Davis if ((dst = ifcr->ifcr_buffer) == NULL) {
723f889d2efSBrooks Davis /* Just asking how many there are. */
724f889d2efSBrooks Davis goto done;
725f889d2efSBrooks Davis }
72637f17770SMarko Zec count = (V_if_cloners_count < buf_count) ?
72737f17770SMarko Zec V_if_cloners_count : buf_count;
728f889d2efSBrooks Davis
72937f17770SMarko Zec for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
730c859ef97SBrooks Davis ifc != NULL && count != 0;
731c859ef97SBrooks Davis ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
732c859ef97SBrooks Davis strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
733f889d2efSBrooks Davis }
734f889d2efSBrooks Davis
735f889d2efSBrooks Davis done:
736f889d2efSBrooks Davis IF_CLONERS_UNLOCK();
73792f19df4SAlexander Kabaev if (err == 0 && dst != NULL)
738c859ef97SBrooks Davis err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
739c859ef97SBrooks Davis if (outbuf != NULL)
740c859ef97SBrooks Davis free(outbuf, M_CLONE);
741f889d2efSBrooks Davis return (err);
742f889d2efSBrooks Davis }
743f889d2efSBrooks Davis
74454712fc4SGleb Smirnoff #ifdef VIMAGE
745f889d2efSBrooks Davis /*
74654712fc4SGleb Smirnoff * if_clone_restoregroup() is used in context of if_vmove().
74754712fc4SGleb Smirnoff *
74854712fc4SGleb Smirnoff * Since if_detach_internal() has removed the interface from ALL groups, we
74954712fc4SGleb Smirnoff * need to "restore" interface membership in the cloner's group. Note that
75054712fc4SGleb Smirnoff * interface belongs to cloner in its home vnet, so we first find the original
75154712fc4SGleb Smirnoff * cloner, and then we confirm that cloner with the same name exists in the
75254712fc4SGleb Smirnoff * current vnet.
753c92a456bSHiroki Sato */
75454712fc4SGleb Smirnoff void
if_clone_restoregroup(struct ifnet * ifp)75554712fc4SGleb Smirnoff if_clone_restoregroup(struct ifnet *ifp)
756c92a456bSHiroki Sato {
75754712fc4SGleb Smirnoff struct if_clone *ifc;
758c92a456bSHiroki Sato struct ifnet *ifcifp;
75954712fc4SGleb Smirnoff char ifc_name[IFCLOSIZ] = { [0] = '\0' };
760c92a456bSHiroki Sato
76154712fc4SGleb Smirnoff CURVNET_SET_QUIET(ifp->if_home_vnet);
762c92a456bSHiroki Sato IF_CLONERS_LOCK();
763c92a456bSHiroki Sato LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
764c92a456bSHiroki Sato IF_CLONE_LOCK(ifc);
765c92a456bSHiroki Sato LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
766c92a456bSHiroki Sato if (ifp == ifcifp) {
76754712fc4SGleb Smirnoff strncpy(ifc_name, ifc->ifc_name, IFCLOSIZ-1);
768c92a456bSHiroki Sato break;
769c92a456bSHiroki Sato }
770c92a456bSHiroki Sato }
771c92a456bSHiroki Sato IF_CLONE_UNLOCK(ifc);
77254712fc4SGleb Smirnoff if (ifc_name[0] != '\0')
773c92a456bSHiroki Sato break;
774c92a456bSHiroki Sato }
77554712fc4SGleb Smirnoff CURVNET_RESTORE();
77654712fc4SGleb Smirnoff LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
7777ff9ae90SMarius Strobl if (strcmp(ifc->ifc_name, ifc_name) == 0)
77854712fc4SGleb Smirnoff break;
779c92a456bSHiroki Sato IF_CLONERS_UNLOCK();
780c92a456bSHiroki Sato
78154712fc4SGleb Smirnoff if (ifc != NULL)
78254712fc4SGleb Smirnoff if_addgroup(ifp, ifc_name);
783c92a456bSHiroki Sato }
78454712fc4SGleb Smirnoff #endif
785c92a456bSHiroki Sato
786c92a456bSHiroki Sato /*
787f889d2efSBrooks Davis * A utility function to extract unit numbers from interface names of
78853729367SAlexander V. Chernikov * the form name###.
789f889d2efSBrooks Davis *
790f889d2efSBrooks Davis * Returns 0 on success and an error on failure.
791f889d2efSBrooks Davis */
792f889d2efSBrooks Davis int
ifc_name2unit(const char * name,int * unit)793f889d2efSBrooks Davis ifc_name2unit(const char *name, int *unit)
794f889d2efSBrooks Davis {
795f889d2efSBrooks Davis const char *cp;
796434dbbb3SRuslan Ermilov int cutoff = INT_MAX / 10;
797434dbbb3SRuslan Ermilov int cutlim = INT_MAX % 10;
798f889d2efSBrooks Davis
79953729367SAlexander V. Chernikov for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++)
80053729367SAlexander V. Chernikov ;
801f889d2efSBrooks Davis if (*cp == '\0') {
802f889d2efSBrooks Davis *unit = -1;
803434dbbb3SRuslan Ermilov } else if (cp[0] == '0' && cp[1] != '\0') {
804434dbbb3SRuslan Ermilov /* Disallow leading zeroes. */
805434dbbb3SRuslan Ermilov return (EINVAL);
806f889d2efSBrooks Davis } else {
807f889d2efSBrooks Davis for (*unit = 0; *cp != '\0'; cp++) {
808f889d2efSBrooks Davis if (*cp < '0' || *cp > '9') {
809f889d2efSBrooks Davis /* Bogus unit number. */
810f889d2efSBrooks Davis return (EINVAL);
811f889d2efSBrooks Davis }
812434dbbb3SRuslan Ermilov if (*unit > cutoff ||
813434dbbb3SRuslan Ermilov (*unit == cutoff && *cp - '0' > cutlim))
814434dbbb3SRuslan Ermilov return (EINVAL);
815f889d2efSBrooks Davis *unit = (*unit * 10) + (*cp - '0');
816f889d2efSBrooks Davis }
817f889d2efSBrooks Davis }
818f889d2efSBrooks Davis
819f889d2efSBrooks Davis return (0);
820f889d2efSBrooks Davis }
821f889d2efSBrooks Davis
822c64c1f95SAndriy Voskoboinyk static int
ifc_alloc_unit_specific(struct if_clone * ifc,int * unit)823c64c1f95SAndriy Voskoboinyk ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
824f889d2efSBrooks Davis {
8252e9fff5bSGleb Smirnoff char name[IFNAMSIZ];
826f889d2efSBrooks Davis
8273932d760SGleb Smirnoff if (*unit > ifc->ifc_maxunit)
8283932d760SGleb Smirnoff return (ENOSPC);
829c64c1f95SAndriy Voskoboinyk
830c64c1f95SAndriy Voskoboinyk if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
8312e9fff5bSGleb Smirnoff return (EEXIST);
832f889d2efSBrooks Davis
8332e9fff5bSGleb Smirnoff snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
8342e9fff5bSGleb Smirnoff if (ifunit(name) != NULL) {
8353932d760SGleb Smirnoff free_unr(ifc->ifc_unrhdr, *unit);
8362e9fff5bSGleb Smirnoff return (EEXIST);
837f889d2efSBrooks Davis }
838f889d2efSBrooks Davis
8392e9fff5bSGleb Smirnoff IF_CLONE_ADDREF(ifc);
840f889d2efSBrooks Davis
8412e9fff5bSGleb Smirnoff return (0);
842f889d2efSBrooks Davis }
843f889d2efSBrooks Davis
844c64c1f95SAndriy Voskoboinyk static int
ifc_alloc_unit_next(struct if_clone * ifc,int * unit)845c64c1f95SAndriy Voskoboinyk ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
846c64c1f95SAndriy Voskoboinyk {
847c64c1f95SAndriy Voskoboinyk int error;
848c64c1f95SAndriy Voskoboinyk
849c64c1f95SAndriy Voskoboinyk *unit = alloc_unr(ifc->ifc_unrhdr);
850c64c1f95SAndriy Voskoboinyk if (*unit == -1)
851c64c1f95SAndriy Voskoboinyk return (ENOSPC);
852c64c1f95SAndriy Voskoboinyk
853c64c1f95SAndriy Voskoboinyk free_unr(ifc->ifc_unrhdr, *unit);
854c64c1f95SAndriy Voskoboinyk for (;;) {
855c64c1f95SAndriy Voskoboinyk error = ifc_alloc_unit_specific(ifc, unit);
856c64c1f95SAndriy Voskoboinyk if (error != EEXIST)
857c64c1f95SAndriy Voskoboinyk break;
858c64c1f95SAndriy Voskoboinyk
859c64c1f95SAndriy Voskoboinyk (*unit)++;
860c64c1f95SAndriy Voskoboinyk }
861c64c1f95SAndriy Voskoboinyk
862c64c1f95SAndriy Voskoboinyk return (error);
863c64c1f95SAndriy Voskoboinyk }
864c64c1f95SAndriy Voskoboinyk
865c64c1f95SAndriy Voskoboinyk int
ifc_alloc_unit(struct if_clone * ifc,int * unit)866c64c1f95SAndriy Voskoboinyk ifc_alloc_unit(struct if_clone *ifc, int *unit)
867c64c1f95SAndriy Voskoboinyk {
868c64c1f95SAndriy Voskoboinyk if (*unit < 0)
869c64c1f95SAndriy Voskoboinyk return (ifc_alloc_unit_next(ifc, unit));
870c64c1f95SAndriy Voskoboinyk else
871c64c1f95SAndriy Voskoboinyk return (ifc_alloc_unit_specific(ifc, unit));
872c64c1f95SAndriy Voskoboinyk }
873c64c1f95SAndriy Voskoboinyk
874f889d2efSBrooks Davis void
ifc_free_unit(struct if_clone * ifc,int unit)875f889d2efSBrooks Davis ifc_free_unit(struct if_clone *ifc, int unit)
876f889d2efSBrooks Davis {
877f889d2efSBrooks Davis
8782e9fff5bSGleb Smirnoff free_unr(ifc->ifc_unrhdr, unit);
8792e9fff5bSGleb Smirnoff IF_CLONE_REMREF(ifc);
880f889d2efSBrooks Davis }
881f889d2efSBrooks Davis
88242a58907SGleb Smirnoff static int
ifc_simple_match(struct if_clone * ifc,const char * name)883f889d2efSBrooks Davis ifc_simple_match(struct if_clone *ifc, const char *name)
884f889d2efSBrooks Davis {
885f889d2efSBrooks Davis const char *cp;
886f889d2efSBrooks Davis int i;
887f889d2efSBrooks Davis
888f889d2efSBrooks Davis /* Match the name */
889f889d2efSBrooks Davis for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
890f889d2efSBrooks Davis if (ifc->ifc_name[i] != *cp)
891f889d2efSBrooks Davis return (0);
892f889d2efSBrooks Davis }
893f889d2efSBrooks Davis
894f889d2efSBrooks Davis /* Make sure there's a unit number or nothing after the name */
895f889d2efSBrooks Davis for (; *cp != '\0'; cp++) {
896f889d2efSBrooks Davis if (*cp < '0' || *cp > '9')
897f889d2efSBrooks Davis return (0);
898f889d2efSBrooks Davis }
899f889d2efSBrooks Davis
900f889d2efSBrooks Davis return (1);
901f889d2efSBrooks Davis }
902f889d2efSBrooks Davis
90342a58907SGleb Smirnoff static int
ifc_handle_unit(struct if_clone * ifc,char * name,size_t len,int * punit)90409ee0fc0SAlexander V. Chernikov ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
905f889d2efSBrooks Davis {
906f889d2efSBrooks Davis char *dp;
907f889d2efSBrooks Davis int wildcard;
908f889d2efSBrooks Davis int unit;
909f889d2efSBrooks Davis int err;
910f889d2efSBrooks Davis
911f889d2efSBrooks Davis err = ifc_name2unit(name, &unit);
912f889d2efSBrooks Davis if (err != 0)
913f889d2efSBrooks Davis return (err);
914f889d2efSBrooks Davis
915f889d2efSBrooks Davis wildcard = (unit < 0);
916f889d2efSBrooks Davis
917f889d2efSBrooks Davis err = ifc_alloc_unit(ifc, &unit);
918f889d2efSBrooks Davis if (err != 0)
919f889d2efSBrooks Davis return (err);
920f889d2efSBrooks Davis
921f889d2efSBrooks Davis /* In the wildcard case, we need to update the name. */
922f889d2efSBrooks Davis if (wildcard) {
923f889d2efSBrooks Davis for (dp = name; *dp != '\0'; dp++);
924f889d2efSBrooks Davis if (snprintf(dp, len - (dp-name), "%d", unit) >
925f889d2efSBrooks Davis len - (dp-name) - 1) {
926f889d2efSBrooks Davis /*
927f889d2efSBrooks Davis * This can only be a programmer error and
928f889d2efSBrooks Davis * there's no straightforward way to recover if
929f889d2efSBrooks Davis * it happens.
930f889d2efSBrooks Davis */
931f889d2efSBrooks Davis panic("if_clone_create(): interface name too long");
932f889d2efSBrooks Davis }
933f889d2efSBrooks Davis }
93409ee0fc0SAlexander V. Chernikov *punit = unit;
935f889d2efSBrooks Davis
936f889d2efSBrooks Davis return (0);
937f889d2efSBrooks Davis }
938f889d2efSBrooks Davis
93909ee0fc0SAlexander V. Chernikov int
ifc_copyin(const struct ifc_data * ifd,void * target,size_t len)94009ee0fc0SAlexander V. Chernikov ifc_copyin(const struct ifc_data *ifd, void *target, size_t len)
941f889d2efSBrooks Davis {
94209ee0fc0SAlexander V. Chernikov if (ifd->params == NULL)
943f889d2efSBrooks Davis return (EINVAL);
944f889d2efSBrooks Davis
94509ee0fc0SAlexander V. Chernikov if (ifd->flags & IFC_F_SYSSPACE) {
94609ee0fc0SAlexander V. Chernikov memcpy(target, ifd->params, len);
947f889d2efSBrooks Davis return (0);
94809ee0fc0SAlexander V. Chernikov } else
94909ee0fc0SAlexander V. Chernikov return (copyin(ifd->params, target, len));
950f889d2efSBrooks Davis }
951