2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
5 * Copyright (c) 1980, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)if.c 8.5 (Berkeley) 1/9/95
35 #include <sys/param.h>
36 #include <sys/eventhandler.h>
37 #include <sys/malloc.h>
38 #include <sys/limits.h>
40 #include <sys/mutex.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
47 #include <net/if_var.h>
48 #include <net/if_private.h>
49 #include <net/if_clone.h>
50 #include <net/radix.h>
51 #include <net/route.h>
54 #include <netlink/netlink.h>
55 #include <netlink/netlink_ctl.h>
56 #include <netlink/netlink_route.h>
57 #include <netlink/route/route_var.h>
59 /* Current IF_MAXUNIT expands maximum to 5 characters. */
60 #define IFCLOSIZ (IFNAMSIZ - 5)
63 * Structure describing a `cloning' interface.
66 * (c) const until freeing
67 * (d) driver specific data, may need external protection.
68 * (e) locked by if_cloners_mtx
69 * (i) locked by ifc_mtx mtx
72 char ifc_name[IFCLOSIZ]; /* (c) Name of device, e.g. `gif' */
73 struct unrhdr *ifc_unrhdr; /* (c) alloc_unr(9) header */
74 int ifc_maxunit; /* (c) maximum unit number */
76 long ifc_refcnt; /* (i) Reference count. */
77 LIST_HEAD(, ifnet) ifc_iflist; /* (i) List of cloned interfaces */
78 struct mtx ifc_mtx; /* Mutex to protect members. */
80 ifc_match_f *ifc_match; /* (c) Matcher function */
81 ifc_create_f *ifc_create; /* (c) Creates new interface */
82 ifc_destroy_f *ifc_destroy; /* (c) Destroys cloned interface */
84 ifc_create_nl_f *create_nl; /* (c) Netlink creation handler */
85 ifc_modify_nl_f *modify_nl; /* (c) Netlink modification handler */
86 ifc_dump_nl_f *dump_nl; /* (c) Netlink dump handler */
88 #ifdef CLONE_COMPAT_13
89 /* (c) Driver specific cloning functions. Called with no locks held. */
91 struct { /* advanced cloner */
92 ifc_create_t *_ifc_create;
93 ifc_destroy_t *_ifc_destroy;
95 struct { /* simple cloner */
96 ifcs_create_t *_ifcs_create;
97 ifcs_destroy_t *_ifcs_destroy;
98 int _ifcs_minifs; /* minimum ifs */
102 #define ifca_create U.A._ifc_create
103 #define ifca_destroy U.A._ifc_destroy
104 #define ifcs_create U.S._ifcs_create
105 #define ifcs_destroy U.S._ifcs_destroy
106 #define ifcs_minifs U.S._ifcs_minifs
109 LIST_ENTRY(if_clone) ifc_list; /* (e) On list of cloners */
114 static void if_clone_free(struct if_clone *ifc);
115 static int if_clone_createif_nl(struct if_clone *ifc, const char *name,
116 struct ifc_data_nl *ifd);
118 static int ifc_simple_match(struct if_clone *ifc, const char *name);
119 static int ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit);
120 static struct if_clone *ifc_find_cloner(const char *name);
121 static struct if_clone *ifc_find_cloner_match(const char *name);
123 #ifdef CLONE_COMPAT_13
124 static int ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
125 struct ifc_data *ifc_data, struct ifnet **ifpp);
126 static int ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
127 struct ifc_data *ifc_data, struct ifnet **ifpp);
130 static struct mtx if_cloners_mtx;
131 MTX_SYSINIT(if_cloners_lock, &if_cloners_mtx, "if_cloners lock", MTX_DEF);
132 VNET_DEFINE_STATIC(int, if_cloners_count);
133 VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners);
135 #define V_if_cloners_count VNET(if_cloners_count)
136 #define V_if_cloners VNET(if_cloners)
138 #define IF_CLONERS_LOCK_ASSERT() mtx_assert(&if_cloners_mtx, MA_OWNED)
139 #define IF_CLONERS_LOCK() mtx_lock(&if_cloners_mtx)
140 #define IF_CLONERS_UNLOCK() mtx_unlock(&if_cloners_mtx)
142 #define IF_CLONE_LOCK_INIT(ifc) \
143 mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
144 #define IF_CLONE_LOCK_DESTROY(ifc) mtx_destroy(&(ifc)->ifc_mtx)
145 #define IF_CLONE_LOCK_ASSERT(ifc) mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
146 #define IF_CLONE_LOCK(ifc) mtx_lock(&(ifc)->ifc_mtx)
147 #define IF_CLONE_UNLOCK(ifc) mtx_unlock(&(ifc)->ifc_mtx)
149 #define IF_CLONE_ADDREF(ifc) \
151 IF_CLONE_LOCK(ifc); \
152 IF_CLONE_ADDREF_LOCKED(ifc); \
153 IF_CLONE_UNLOCK(ifc); \
155 #define IF_CLONE_ADDREF_LOCKED(ifc) \
157 IF_CLONE_LOCK_ASSERT(ifc); \
158 KASSERT((ifc)->ifc_refcnt >= 0, \
159 ("negative refcnt %ld", (ifc)->ifc_refcnt)); \
160 (ifc)->ifc_refcnt++; \
162 #define IF_CLONE_REMREF(ifc) \
164 IF_CLONE_LOCK(ifc); \
165 IF_CLONE_REMREF_LOCKED(ifc); \
167 #define IF_CLONE_REMREF_LOCKED(ifc) \
169 IF_CLONE_LOCK_ASSERT(ifc); \
170 KASSERT((ifc)->ifc_refcnt > 0, \
171 ("bogus refcnt %ld", (ifc)->ifc_refcnt)); \
172 if (--(ifc)->ifc_refcnt == 0) { \
173 IF_CLONE_UNLOCK(ifc); \
174 if_clone_free(ifc); \
176 /* silently free the lock */ \
177 IF_CLONE_UNLOCK(ifc); \
181 #define IFC_IFLIST_INSERT(_ifc, _ifp) \
182 LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones)
183 #define IFC_IFLIST_REMOVE(_ifc, _ifp) \
184 LIST_REMOVE(_ifp, if_clones)
186 static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
189 vnet_if_clone_init(void)
192 LIST_INIT(&V_if_cloners);
196 * Lookup and create a clone network interface.
199 ifc_create_ifp(const char *name, struct ifc_data *ifd, struct ifnet **ifpp)
201 struct if_clone *ifc = ifc_find_cloner_match(name);
206 struct ifc_data_nl ifd_new = {
209 .params = ifd->params,
212 int error = if_clone_createif_nl(ifc, name, &ifd_new);
221 ifc_create_ifp_nl(const char *name, struct ifc_data_nl *ifd)
223 struct if_clone *ifc = ifc_find_cloner_match(name);
229 ifd->error = if_clone_createif_nl(ifc, name, ifd);
235 if_clone_create(char *name, size_t len, caddr_t params)
237 struct ifc_data ifd = { .params = params };
240 int error = ifc_create_ifp(name, &ifd, &ifp);
243 strlcpy(name, if_name(ifp), len);
249 ifc_modify_ifp_nl(struct ifnet *ifp, struct ifc_data_nl *ifd)
251 struct if_clone *ifc = ifc_find_cloner(ifp->if_dname);
257 ifd->error = (*ifc->modify_nl)(ifp, ifd);
262 ifc_dump_ifp_nl(struct ifnet *ifp, struct nl_writer *nw)
264 struct if_clone *ifc = ifc_find_cloner(ifp->if_dname);
268 (*ifc->dump_nl)(ifp, nw);
273 ifc_create_ifp_nl_default(struct if_clone *ifc, char *name, size_t len,
274 struct ifc_data_nl *ifd)
276 struct ifc_data ifd_new = {
279 .params = ifd->params,
282 return ((*ifc->ifc_create)(ifc, name, len, &ifd_new, &ifd->ifp));
286 ifc_modify_ifp_nl_default(struct ifnet *ifp, struct ifc_data_nl *ifd)
288 if (ifd->lattrs != NULL)
289 return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt));
294 ifc_dump_ifp_nl_default(struct ifnet *ifp, struct nl_writer *nw)
296 int off = nlattr_add_nested(nw, IFLA_LINKINFO);
299 nlattr_add_string(nw, IFLA_INFO_KIND, ifp->if_dname);
300 nlattr_set_len(nw, off);
305 ifc_link_ifp(struct if_clone *ifc, struct ifnet *ifp)
308 if_addgroup(ifp, ifc->ifc_name);
311 IFC_IFLIST_INSERT(ifc, ifp);
312 IF_CLONE_UNLOCK(ifc);
316 if_clone_addif(struct if_clone *ifc, struct ifnet *ifp)
318 ifc_link_ifp(ifc, ifp);
322 ifc_unlink_ifp(struct if_clone *ifc, struct ifnet *ifp)
324 struct ifnet *ifcifp;
327 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
329 IFC_IFLIST_REMOVE(ifc, ifp);
333 IF_CLONE_UNLOCK(ifc);
336 if_delgroup(ifp, ifc->ifc_name);
338 return (ifcifp != NULL);
341 static struct if_clone *
342 ifc_find_cloner_match(const char *name)
344 struct if_clone *ifc;
347 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
348 if (ifc->ifc_match(ifc, name))
356 static struct if_clone *
357 ifc_find_cloner(const char *name)
359 struct if_clone *ifc;
362 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
363 if (strcmp(ifc->ifc_name, name) == 0) {
372 static struct if_clone *
373 ifc_find_cloner_in_vnet(const char *name, struct vnet *vnet)
375 CURVNET_SET_QUIET(vnet);
376 struct if_clone *ifc = ifc_find_cloner(name);
383 * Create a clone network interface.
386 if_clone_createif_nl(struct if_clone *ifc, const char *ifname, struct ifc_data_nl *ifd)
391 strlcpy(name, ifname, sizeof(name));
393 if (ifunit(name) != NULL)
396 if (ifc->ifc_flags & IFC_F_AUTOUNIT) {
397 if ((error = ifc_handle_unit(ifc, name, sizeof(name), &ifd->unit)) != 0)
401 if (ifd->lattrs != NULL)
402 error = (*ifc->create_nl)(ifc, name, sizeof(name), ifd);
404 error = ifc_create_ifp_nl_default(ifc, name, sizeof(name), ifd);
406 if (ifc->ifc_flags & IFC_F_AUTOUNIT)
407 ifc_free_unit(ifc, ifd->unit);
411 MPASS(ifd->ifp != NULL);
412 if_clone_addif(ifc, ifd->ifp);
414 if (ifd->lattrs != NULL)
415 error = (*ifc->modify_nl)(ifd->ifp, ifd);
421 * Lookup and destroy a clone network interface.
424 if_clone_destroy(const char *name)
427 struct if_clone *ifc;
430 ifp = ifunit_ref(name);
434 ifc = ifc_find_cloner_in_vnet(ifp->if_dname, ifp->if_home_vnet);
440 err = if_clone_destroyif(ifc, ifp);
446 * Destroy a clone network interface.
449 if_clone_destroyif_flags(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
454 * Given that the cloned ifnet might be attached to a different
455 * vnet from where its cloner was registered, we have to
456 * switch to the vnet context of the target vnet.
458 CURVNET_SET_QUIET(ifp->if_vnet);
460 if (!ifc_unlink_ifp(ifc, ifp)) {
462 return (ENXIO); /* ifp is not on the list. */
465 int unit = ifp->if_dunit;
466 err = (*ifc->ifc_destroy)(ifc, ifp, flags);
469 ifc_link_ifp(ifc, ifp);
470 else if (ifc->ifc_flags & IFC_F_AUTOUNIT)
471 ifc_free_unit(ifc, unit);
477 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
479 return (if_clone_destroyif_flags(ifc, ifp, 0));
482 static struct if_clone *
483 if_clone_alloc(const char *name, int maxunit)
485 struct if_clone *ifc;
487 KASSERT(name != NULL, ("%s: no name\n", __func__));
489 ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO);
490 strncpy(ifc->ifc_name, name, IFCLOSIZ-1);
491 IF_CLONE_LOCK_INIT(ifc);
492 IF_CLONE_ADDREF(ifc);
493 ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT;
494 ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx);
495 LIST_INIT(&ifc->ifc_iflist);
497 ifc->create_nl = ifc_create_ifp_nl_default;
498 ifc->modify_nl = ifc_modify_ifp_nl_default;
499 ifc->dump_nl = ifc_dump_ifp_nl_default;
505 if_clone_attach(struct if_clone *ifc)
507 struct if_clone *ifc1;
510 LIST_FOREACH(ifc1, &V_if_cloners, ifc_list)
511 if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) {
513 IF_CLONE_REMREF(ifc);
516 LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
517 V_if_cloners_count++;
524 ifc_attach_cloner(const char *name, struct if_clone_addreq *req)
526 if (req->create_f == NULL || req->destroy_f == NULL)
528 if (strnlen(name, IFCLOSIZ) >= (IFCLOSIZ - 1))
531 struct if_clone *ifc = if_clone_alloc(name, req->maxunit);
532 ifc->ifc_match = req->match_f != NULL ? req->match_f : ifc_simple_match;
533 ifc->ifc_create = req->create_f;
534 ifc->ifc_destroy = req->destroy_f;
535 ifc->ifc_flags = (req->flags & IFC_F_AUTOUNIT);
537 if (req->version == 2) {
538 struct if_clone_addreq_v2 *req2 = (struct if_clone_addreq_v2 *)req;
540 ifc->create_nl = req2->create_nl_f;
541 ifc->modify_nl = req2->modify_nl_f;
542 ifc->dump_nl = req2->dump_nl_f;
545 ifc->dump_nl = ifc_dump_ifp_nl_default;
547 if (if_clone_attach(ifc) != 0)
550 EVENTHANDLER_INVOKE(if_clone_event, ifc);
556 ifc_detach_cloner(struct if_clone *ifc)
558 if_clone_detach(ifc);
562 #ifdef CLONE_COMPAT_13
565 ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
566 struct ifc_data *ifc_data, struct ifnet **ifpp)
568 int error = ifc->ifca_create(ifc, name, maxlen, ifc_data->params);
571 *ifpp = ifunit(name);
576 ifc_advanced_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
578 if (ifc->ifca_destroy == NULL)
580 return (ifc->ifca_destroy(ifc, ifp));
584 if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
585 ifc_create_t create, ifc_destroy_t destroy)
587 struct if_clone *ifc;
589 ifc = if_clone_alloc(name, maxunit);
590 ifc->ifc_match = match;
591 ifc->ifc_create = ifc_advanced_create_wrapper;
592 ifc->ifc_destroy = ifc_advanced_destroy_wrapper;
593 ifc->ifca_destroy = destroy;
594 ifc->ifca_create = create;
596 if (if_clone_attach(ifc) != 0)
599 EVENTHANDLER_INVOKE(if_clone_event, ifc);
605 ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
606 struct ifc_data *ifc_data, struct ifnet **ifpp)
610 ifc_name2unit(name, &unit);
611 int error = ifc->ifcs_create(ifc, unit, ifc_data->params);
613 *ifpp = ifunit(name);
618 ifc_simple_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
620 if (ifp->if_dunit < ifc->ifcs_minifs && (flags & IFC_F_FORCE) == 0)
623 ifc->ifcs_destroy(ifp);
628 if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
631 struct if_clone *ifc;
634 ifc = if_clone_alloc(name, 0);
635 ifc->ifc_match = ifc_simple_match;
636 ifc->ifc_create = ifc_simple_create_wrapper;
637 ifc->ifc_destroy = ifc_simple_destroy_wrapper;
638 ifc->ifcs_create = create;
639 ifc->ifcs_destroy = destroy;
640 ifc->ifcs_minifs = minifs;
641 ifc->ifc_flags = IFC_F_AUTOUNIT;
643 if (if_clone_attach(ifc) != 0)
646 for (unit = 0; unit < minifs; unit++) {
649 struct ifc_data_nl ifd = {};
651 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
652 error = if_clone_createif_nl(ifc, name, &ifd);
654 ("%s: failed to create required interface %s",
658 EVENTHANDLER_INVOKE(if_clone_event, ifc);
665 * Unregister a network interface cloner.
668 if_clone_detach(struct if_clone *ifc)
672 LIST_REMOVE(ifc, ifc_list);
673 V_if_cloners_count--;
676 /* destroy all interfaces for this cloner */
677 while (!LIST_EMPTY(&ifc->ifc_iflist))
678 if_clone_destroyif_flags(ifc, LIST_FIRST(&ifc->ifc_iflist), IFC_F_FORCE);
680 IF_CLONE_REMREF(ifc);
684 if_clone_free(struct if_clone *ifc)
687 KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
688 ("%s: ifc_iflist not empty", __func__));
690 IF_CLONE_LOCK_DESTROY(ifc);
691 delete_unrhdr(ifc->ifc_unrhdr);
696 * Provide list of interface cloners to userspace.
699 if_clone_list(struct if_clonereq *ifcr)
701 char *buf, *dst, *outbuf = NULL;
702 struct if_clone *ifc;
703 int buf_count, count, err = 0;
705 if (ifcr->ifcr_count < 0)
710 * Set our internal output buffer size. We could end up not
711 * reporting a cloner that is added between the unlock and lock
712 * below, but that's not a major problem. Not caping our
713 * allocation to the number of cloners actually in the system
714 * could be because that would let arbitrary users cause us to
715 * allocate arbitrary amounts of kernel memory.
717 buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
718 V_if_cloners_count : ifcr->ifcr_count;
721 outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
725 ifcr->ifcr_total = V_if_cloners_count;
726 if ((dst = ifcr->ifcr_buffer) == NULL) {
727 /* Just asking how many there are. */
730 count = (V_if_cloners_count < buf_count) ?
731 V_if_cloners_count : buf_count;
733 for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
734 ifc != NULL && count != 0;
735 ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
736 strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
741 if (err == 0 && dst != NULL)
742 err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
744 free(outbuf, M_CLONE);
750 * if_clone_restoregroup() is used in context of if_vmove().
752 * Since if_detach_internal() has removed the interface from ALL groups, we
753 * need to "restore" interface membership in the cloner's group. Note that
754 * interface belongs to cloner in its home vnet, so we first find the original
755 * cloner, and then we confirm that cloner with the same name exists in the
759 if_clone_restoregroup(struct ifnet *ifp)
761 struct if_clone *ifc;
762 struct ifnet *ifcifp;
763 char ifc_name[IFCLOSIZ] = { [0] = '\0' };
765 CURVNET_SET_QUIET(ifp->if_home_vnet);
767 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
769 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
771 strncpy(ifc_name, ifc->ifc_name, IFCLOSIZ-1);
775 IF_CLONE_UNLOCK(ifc);
776 if (ifc_name[0] != '\0')
780 LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
781 if (strcmp(ifc->ifc_name, ifc_name) == 0)
786 if_addgroup(ifp, ifc_name);
791 * A utility function to extract unit numbers from interface names of
794 * Returns 0 on success and an error on failure.
797 ifc_name2unit(const char *name, int *unit)
800 int cutoff = INT_MAX / 10;
801 int cutlim = INT_MAX % 10;
803 for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++)
807 } else if (cp[0] == '0' && cp[1] != '\0') {
808 /* Disallow leading zeroes. */
811 for (*unit = 0; *cp != '\0'; cp++) {
812 if (*cp < '0' || *cp > '9') {
813 /* Bogus unit number. */
816 if (*unit > cutoff ||
817 (*unit == cutoff && *cp - '0' > cutlim))
819 *unit = (*unit * 10) + (*cp - '0');
827 ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
831 if (*unit > ifc->ifc_maxunit)
834 if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
837 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
838 if (ifunit(name) != NULL) {
839 free_unr(ifc->ifc_unrhdr, *unit);
843 IF_CLONE_ADDREF(ifc);
849 ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
853 *unit = alloc_unr(ifc->ifc_unrhdr);
857 free_unr(ifc->ifc_unrhdr, *unit);
859 error = ifc_alloc_unit_specific(ifc, unit);
870 ifc_alloc_unit(struct if_clone *ifc, int *unit)
873 return (ifc_alloc_unit_next(ifc, unit));
875 return (ifc_alloc_unit_specific(ifc, unit));
879 ifc_free_unit(struct if_clone *ifc, int unit)
882 free_unr(ifc->ifc_unrhdr, unit);
883 IF_CLONE_REMREF(ifc);
887 ifc_simple_match(struct if_clone *ifc, const char *name)
893 for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
894 if (ifc->ifc_name[i] != *cp)
898 /* Make sure there's a unit number or nothing after the name */
899 for (; *cp != '\0'; cp++) {
900 if (*cp < '0' || *cp > '9')
908 ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
915 err = ifc_name2unit(name, &unit);
919 wildcard = (unit < 0);
921 err = ifc_alloc_unit(ifc, &unit);
925 /* In the wildcard case, we need to update the name. */
927 for (dp = name; *dp != '\0'; dp++);
928 if (snprintf(dp, len - (dp-name), "%d", unit) >
929 len - (dp-name) - 1) {
931 * This can only be a programmer error and
932 * there's no straightforward way to recover if
935 panic("if_clone_create(): interface name too long");
944 ifc_copyin(const struct ifc_data *ifd, void *target, size_t len)
946 if (ifd->params == NULL)
949 if (ifd->flags & IFC_F_SYSSPACE) {
950 memcpy(target, ifd->params, len);
953 return (copyin(ifd->params, target, len));