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
36 #include "opt_netlink.h"
38 #include <sys/param.h>
39 #include <sys/eventhandler.h>
40 #include <sys/malloc.h>
41 #include <sys/limits.h>
43 #include <sys/mutex.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
50 #include <net/if_var.h>
51 #include <net/if_private.h>
52 #include <net/if_clone.h>
53 #include <net/radix.h>
54 #include <net/route.h>
57 #include <netlink/netlink.h>
58 #include <netlink/netlink_ctl.h>
59 #include <netlink/netlink_route.h>
60 #include <netlink/route/route_var.h>
62 /* Current IF_MAXUNIT expands maximum to 5 characters. */
63 #define IFCLOSIZ (IFNAMSIZ - 5)
66 * Structure describing a `cloning' interface.
69 * (c) const until freeing
70 * (d) driver specific data, may need external protection.
71 * (e) locked by if_cloners_mtx
72 * (i) locked by ifc_mtx mtx
75 char ifc_name[IFCLOSIZ]; /* (c) Name of device, e.g. `gif' */
76 struct unrhdr *ifc_unrhdr; /* (c) alloc_unr(9) header */
77 int ifc_maxunit; /* (c) maximum unit number */
79 long ifc_refcnt; /* (i) Reference count. */
80 LIST_HEAD(, ifnet) ifc_iflist; /* (i) List of cloned interfaces */
81 struct mtx ifc_mtx; /* Mutex to protect members. */
83 ifc_match_f *ifc_match; /* (c) Matcher function */
84 ifc_create_f *ifc_create; /* (c) Creates new interface */
85 ifc_destroy_f *ifc_destroy; /* (c) Destroys cloned interface */
87 ifc_create_nl_f *create_nl; /* (c) Netlink creation handler */
88 ifc_modify_nl_f *modify_nl; /* (c) Netlink modification handler */
89 ifc_dump_nl_f *dump_nl; /* (c) Netlink dump handler */
91 #ifdef CLONE_COMPAT_13
92 /* (c) Driver specific cloning functions. Called with no locks held. */
94 struct { /* advanced cloner */
95 ifc_create_t *_ifc_create;
96 ifc_destroy_t *_ifc_destroy;
98 struct { /* simple cloner */
99 ifcs_create_t *_ifcs_create;
100 ifcs_destroy_t *_ifcs_destroy;
101 int _ifcs_minifs; /* minimum ifs */
105 #define ifca_create U.A._ifc_create
106 #define ifca_destroy U.A._ifc_destroy
107 #define ifcs_create U.S._ifcs_create
108 #define ifcs_destroy U.S._ifcs_destroy
109 #define ifcs_minifs U.S._ifcs_minifs
112 LIST_ENTRY(if_clone) ifc_list; /* (e) On list of cloners */
117 static void if_clone_free(struct if_clone *ifc);
118 static int if_clone_createif_nl(struct if_clone *ifc, const char *name,
119 struct ifc_data_nl *ifd);
121 static int ifc_simple_match(struct if_clone *ifc, const char *name);
122 static int ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit);
123 static struct if_clone *ifc_find_cloner(const char *name);
124 static struct if_clone *ifc_find_cloner_match(const char *name);
126 #ifdef CLONE_COMPAT_13
127 static int ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
128 struct ifc_data *ifc_data, struct ifnet **ifpp);
129 static int ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
130 struct ifc_data *ifc_data, struct ifnet **ifpp);
133 static struct mtx if_cloners_mtx;
134 MTX_SYSINIT(if_cloners_lock, &if_cloners_mtx, "if_cloners lock", MTX_DEF);
135 VNET_DEFINE_STATIC(int, if_cloners_count);
136 VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners);
138 #define V_if_cloners_count VNET(if_cloners_count)
139 #define V_if_cloners VNET(if_cloners)
141 #define IF_CLONERS_LOCK_ASSERT() mtx_assert(&if_cloners_mtx, MA_OWNED)
142 #define IF_CLONERS_LOCK() mtx_lock(&if_cloners_mtx)
143 #define IF_CLONERS_UNLOCK() mtx_unlock(&if_cloners_mtx)
145 #define IF_CLONE_LOCK_INIT(ifc) \
146 mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
147 #define IF_CLONE_LOCK_DESTROY(ifc) mtx_destroy(&(ifc)->ifc_mtx)
148 #define IF_CLONE_LOCK_ASSERT(ifc) mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
149 #define IF_CLONE_LOCK(ifc) mtx_lock(&(ifc)->ifc_mtx)
150 #define IF_CLONE_UNLOCK(ifc) mtx_unlock(&(ifc)->ifc_mtx)
152 #define IF_CLONE_ADDREF(ifc) \
154 IF_CLONE_LOCK(ifc); \
155 IF_CLONE_ADDREF_LOCKED(ifc); \
156 IF_CLONE_UNLOCK(ifc); \
158 #define IF_CLONE_ADDREF_LOCKED(ifc) \
160 IF_CLONE_LOCK_ASSERT(ifc); \
161 KASSERT((ifc)->ifc_refcnt >= 0, \
162 ("negative refcnt %ld", (ifc)->ifc_refcnt)); \
163 (ifc)->ifc_refcnt++; \
165 #define IF_CLONE_REMREF(ifc) \
167 IF_CLONE_LOCK(ifc); \
168 IF_CLONE_REMREF_LOCKED(ifc); \
170 #define IF_CLONE_REMREF_LOCKED(ifc) \
172 IF_CLONE_LOCK_ASSERT(ifc); \
173 KASSERT((ifc)->ifc_refcnt > 0, \
174 ("bogus refcnt %ld", (ifc)->ifc_refcnt)); \
175 if (--(ifc)->ifc_refcnt == 0) { \
176 IF_CLONE_UNLOCK(ifc); \
177 if_clone_free(ifc); \
179 /* silently free the lock */ \
180 IF_CLONE_UNLOCK(ifc); \
184 #define IFC_IFLIST_INSERT(_ifc, _ifp) \
185 LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones)
186 #define IFC_IFLIST_REMOVE(_ifc, _ifp) \
187 LIST_REMOVE(_ifp, if_clones)
189 static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
192 vnet_if_clone_init(void)
195 LIST_INIT(&V_if_cloners);
199 * Lookup and create a clone network interface.
202 ifc_create_ifp(const char *name, struct ifc_data *ifd, struct ifnet **ifpp)
204 struct if_clone *ifc = ifc_find_cloner_match(name);
209 struct ifc_data_nl ifd_new = {
212 .params = ifd->params,
215 int error = if_clone_createif_nl(ifc, name, &ifd_new);
224 ifc_create_ifp_nl(const char *name, struct ifc_data_nl *ifd)
226 struct if_clone *ifc = ifc_find_cloner_match(name);
232 ifd->error = if_clone_createif_nl(ifc, name, ifd);
238 if_clone_create(char *name, size_t len, caddr_t params)
240 struct ifc_data ifd = { .params = params };
243 int error = ifc_create_ifp(name, &ifd, &ifp);
246 strlcpy(name, if_name(ifp), len);
252 ifc_modify_ifp_nl(struct ifnet *ifp, struct ifc_data_nl *ifd)
254 struct if_clone *ifc = ifc_find_cloner(ifp->if_dname);
260 ifd->error = (*ifc->modify_nl)(ifp, ifd);
265 ifc_dump_ifp_nl(struct ifnet *ifp, struct nl_writer *nw)
267 struct if_clone *ifc = ifc_find_cloner(ifp->if_dname);
271 (*ifc->dump_nl)(ifp, nw);
276 ifc_create_ifp_nl_default(struct if_clone *ifc, char *name, size_t len,
277 struct ifc_data_nl *ifd)
279 struct ifc_data ifd_new = {
282 .params = ifd->params,
285 return ((*ifc->ifc_create)(ifc, name, len, &ifd_new, &ifd->ifp));
289 ifc_modify_ifp_nl_default(struct ifnet *ifp, struct ifc_data_nl *ifd)
291 if (ifd->lattrs != NULL)
292 return (nl_modify_ifp_generic(ifp, ifd->lattrs, ifd->bm, ifd->npt));
297 ifc_dump_ifp_nl_default(struct ifnet *ifp, struct nl_writer *nw)
299 int off = nlattr_add_nested(nw, IFLA_LINKINFO);
302 nlattr_add_string(nw, IFLA_INFO_KIND, ifp->if_dname);
303 nlattr_set_len(nw, off);
308 ifc_link_ifp(struct if_clone *ifc, struct ifnet *ifp)
311 if ((ifc->ifc_flags & IFC_NOGROUP) == 0)
312 if_addgroup(ifp, ifc->ifc_name);
315 IFC_IFLIST_INSERT(ifc, ifp);
316 IF_CLONE_UNLOCK(ifc);
320 if_clone_addif(struct if_clone *ifc, struct ifnet *ifp)
322 ifc_link_ifp(ifc, ifp);
326 ifc_unlink_ifp(struct if_clone *ifc, struct ifnet *ifp)
328 struct ifnet *ifcifp;
331 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
333 IFC_IFLIST_REMOVE(ifc, ifp);
337 IF_CLONE_UNLOCK(ifc);
339 if (ifcifp != NULL && (ifc->ifc_flags & IFC_F_NOGROUP) == 0)
340 if_delgroup(ifp, ifc->ifc_name);
342 return (ifcifp != NULL);
345 static struct if_clone *
346 ifc_find_cloner_match(const char *name)
348 struct if_clone *ifc;
351 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
352 if (ifc->ifc_match(ifc, name))
360 static struct if_clone *
361 ifc_find_cloner(const char *name)
363 struct if_clone *ifc;
366 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
367 if (strcmp(ifc->ifc_name, name) == 0) {
376 static struct if_clone *
377 ifc_find_cloner_in_vnet(const char *name, struct vnet *vnet)
379 CURVNET_SET_QUIET(vnet);
380 struct if_clone *ifc = ifc_find_cloner(name);
387 * Create a clone network interface.
390 if_clone_createif_nl(struct if_clone *ifc, const char *ifname, struct ifc_data_nl *ifd)
395 strlcpy(name, ifname, sizeof(name));
397 if (ifunit(name) != NULL)
400 if (ifc->ifc_flags & IFC_F_AUTOUNIT) {
401 if ((error = ifc_handle_unit(ifc, name, sizeof(name), &ifd->unit)) != 0)
405 if (ifd->lattrs != NULL)
406 error = (*ifc->create_nl)(ifc, name, sizeof(name), ifd);
408 error = ifc_create_ifp_nl_default(ifc, name, sizeof(name), ifd);
410 if (ifc->ifc_flags & IFC_F_AUTOUNIT)
411 ifc_free_unit(ifc, ifd->unit);
415 MPASS(ifd->ifp != NULL);
416 if_clone_addif(ifc, ifd->ifp);
418 if (ifd->lattrs != NULL)
419 error = (*ifc->modify_nl)(ifd->ifp, ifd);
425 * Lookup and destroy a clone network interface.
428 if_clone_destroy(const char *name)
431 struct if_clone *ifc;
434 ifp = ifunit_ref(name);
438 ifc = ifc_find_cloner_in_vnet(ifp->if_dname, ifp->if_home_vnet);
444 err = if_clone_destroyif(ifc, ifp);
450 * Destroy a clone network interface.
453 if_clone_destroyif_flags(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
458 * Given that the cloned ifnet might be attached to a different
459 * vnet from where its cloner was registered, we have to
460 * switch to the vnet context of the target vnet.
462 CURVNET_SET_QUIET(ifp->if_vnet);
464 if (!ifc_unlink_ifp(ifc, ifp)) {
466 return (ENXIO); /* ifp is not on the list. */
469 int unit = ifp->if_dunit;
470 err = (*ifc->ifc_destroy)(ifc, ifp, flags);
473 ifc_link_ifp(ifc, ifp);
474 else if (ifc->ifc_flags & IFC_F_AUTOUNIT)
475 ifc_free_unit(ifc, unit);
481 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
483 return (if_clone_destroyif_flags(ifc, ifp, 0));
486 static struct if_clone *
487 if_clone_alloc(const char *name, int maxunit)
489 struct if_clone *ifc;
491 KASSERT(name != NULL, ("%s: no name\n", __func__));
493 ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO);
494 strncpy(ifc->ifc_name, name, IFCLOSIZ-1);
495 IF_CLONE_LOCK_INIT(ifc);
496 IF_CLONE_ADDREF(ifc);
497 ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT;
498 ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx);
499 LIST_INIT(&ifc->ifc_iflist);
501 ifc->create_nl = ifc_create_ifp_nl_default;
502 ifc->modify_nl = ifc_modify_ifp_nl_default;
503 ifc->dump_nl = ifc_dump_ifp_nl_default;
509 if_clone_attach(struct if_clone *ifc)
511 struct if_clone *ifc1;
514 LIST_FOREACH(ifc1, &V_if_cloners, ifc_list)
515 if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) {
517 IF_CLONE_REMREF(ifc);
520 LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
521 V_if_cloners_count++;
528 ifc_attach_cloner(const char *name, struct if_clone_addreq *req)
530 if (req->create_f == NULL || req->destroy_f == NULL)
532 if (strnlen(name, IFCLOSIZ) >= (IFCLOSIZ - 1))
535 struct if_clone *ifc = if_clone_alloc(name, req->maxunit);
536 ifc->ifc_match = req->match_f != NULL ? req->match_f : ifc_simple_match;
537 ifc->ifc_create = req->create_f;
538 ifc->ifc_destroy = req->destroy_f;
539 ifc->ifc_flags = (req->flags & (IFC_F_AUTOUNIT | IFC_F_NOGROUP));
541 if (req->version == 2) {
542 struct if_clone_addreq_v2 *req2 = (struct if_clone_addreq_v2 *)req;
544 ifc->create_nl = req2->create_nl_f;
545 ifc->modify_nl = req2->modify_nl_f;
546 ifc->dump_nl = req2->dump_nl_f;
549 ifc->dump_nl = ifc_dump_ifp_nl_default;
551 if (if_clone_attach(ifc) != 0)
554 EVENTHANDLER_INVOKE(if_clone_event, ifc);
560 ifc_detach_cloner(struct if_clone *ifc)
562 if_clone_detach(ifc);
566 #ifdef CLONE_COMPAT_13
569 ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
570 struct ifc_data *ifc_data, struct ifnet **ifpp)
572 int error = ifc->ifca_create(ifc, name, maxlen, ifc_data->params);
575 *ifpp = ifunit(name);
580 ifc_advanced_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
582 if (ifc->ifca_destroy == NULL)
584 return (ifc->ifca_destroy(ifc, ifp));
588 if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
589 ifc_create_t create, ifc_destroy_t destroy)
591 struct if_clone *ifc;
593 ifc = if_clone_alloc(name, maxunit);
594 ifc->ifc_match = match;
595 ifc->ifc_create = ifc_advanced_create_wrapper;
596 ifc->ifc_destroy = ifc_advanced_destroy_wrapper;
597 ifc->ifca_destroy = destroy;
598 ifc->ifca_create = create;
600 if (if_clone_attach(ifc) != 0)
603 EVENTHANDLER_INVOKE(if_clone_event, ifc);
609 ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
610 struct ifc_data *ifc_data, struct ifnet **ifpp)
614 ifc_name2unit(name, &unit);
615 int error = ifc->ifcs_create(ifc, unit, ifc_data->params);
617 *ifpp = ifunit(name);
622 ifc_simple_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
624 if (ifp->if_dunit < ifc->ifcs_minifs && (flags & IFC_F_FORCE) == 0)
627 ifc->ifcs_destroy(ifp);
632 if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
635 struct if_clone *ifc;
638 ifc = if_clone_alloc(name, 0);
639 ifc->ifc_match = ifc_simple_match;
640 ifc->ifc_create = ifc_simple_create_wrapper;
641 ifc->ifc_destroy = ifc_simple_destroy_wrapper;
642 ifc->ifcs_create = create;
643 ifc->ifcs_destroy = destroy;
644 ifc->ifcs_minifs = minifs;
645 ifc->ifc_flags = IFC_F_AUTOUNIT;
647 if (if_clone_attach(ifc) != 0)
650 for (unit = 0; unit < minifs; unit++) {
653 struct ifc_data_nl ifd = {};
655 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
656 error = if_clone_createif_nl(ifc, name, &ifd);
658 ("%s: failed to create required interface %s",
662 EVENTHANDLER_INVOKE(if_clone_event, ifc);
669 * Unregister a network interface cloner.
672 if_clone_detach(struct if_clone *ifc)
676 LIST_REMOVE(ifc, ifc_list);
677 V_if_cloners_count--;
680 /* destroy all interfaces for this cloner */
681 while (!LIST_EMPTY(&ifc->ifc_iflist))
682 if_clone_destroyif_flags(ifc, LIST_FIRST(&ifc->ifc_iflist), IFC_F_FORCE);
684 IF_CLONE_REMREF(ifc);
688 if_clone_free(struct if_clone *ifc)
691 KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
692 ("%s: ifc_iflist not empty", __func__));
694 IF_CLONE_LOCK_DESTROY(ifc);
695 delete_unrhdr(ifc->ifc_unrhdr);
700 * Provide list of interface cloners to userspace.
703 if_clone_list(struct if_clonereq *ifcr)
705 char *buf, *dst, *outbuf = NULL;
706 struct if_clone *ifc;
707 int buf_count, count, err = 0;
709 if (ifcr->ifcr_count < 0)
714 * Set our internal output buffer size. We could end up not
715 * reporting a cloner that is added between the unlock and lock
716 * below, but that's not a major problem. Not caping our
717 * allocation to the number of cloners actually in the system
718 * could be because that would let arbitrary users cause us to
719 * allocate arbitrary amounts of kernel memory.
721 buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
722 V_if_cloners_count : ifcr->ifcr_count;
725 outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
729 ifcr->ifcr_total = V_if_cloners_count;
730 if ((dst = ifcr->ifcr_buffer) == NULL) {
731 /* Just asking how many there are. */
734 count = (V_if_cloners_count < buf_count) ?
735 V_if_cloners_count : buf_count;
737 for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
738 ifc != NULL && count != 0;
739 ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
740 strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
745 if (err == 0 && dst != NULL)
746 err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
748 free(outbuf, M_CLONE);
754 * if_clone_restoregroup() is used in context of if_vmove().
756 * Since if_detach_internal() has removed the interface from ALL groups, we
757 * need to "restore" interface membership in the cloner's group. Note that
758 * interface belongs to cloner in its home vnet, so we first find the original
759 * cloner, and then we confirm that cloner with the same name exists in the
763 if_clone_restoregroup(struct ifnet *ifp)
765 struct if_clone *ifc;
766 struct ifnet *ifcifp;
767 char ifc_name[IFCLOSIZ] = { [0] = '\0' };
769 CURVNET_SET_QUIET(ifp->if_home_vnet);
771 LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
773 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
775 strncpy(ifc_name, ifc->ifc_name, IFCLOSIZ-1);
779 IF_CLONE_UNLOCK(ifc);
780 if (ifc_name[0] != '\0')
784 LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
785 if (strcmp(ifc->ifc_name, ifc_name) == 0 &&
786 ((ifc->ifc_flags & IFC_NOGROUP) == 0))
791 if_addgroup(ifp, ifc_name);
796 * A utility function to extract unit numbers from interface names of
799 * Returns 0 on success and an error on failure.
802 ifc_name2unit(const char *name, int *unit)
805 int cutoff = INT_MAX / 10;
806 int cutlim = INT_MAX % 10;
808 for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++)
812 } else if (cp[0] == '0' && cp[1] != '\0') {
813 /* Disallow leading zeroes. */
816 for (*unit = 0; *cp != '\0'; cp++) {
817 if (*cp < '0' || *cp > '9') {
818 /* Bogus unit number. */
821 if (*unit > cutoff ||
822 (*unit == cutoff && *cp - '0' > cutlim))
824 *unit = (*unit * 10) + (*cp - '0');
832 ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
836 if (*unit > ifc->ifc_maxunit)
839 if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
842 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
843 if (ifunit(name) != NULL) {
844 free_unr(ifc->ifc_unrhdr, *unit);
848 IF_CLONE_ADDREF(ifc);
854 ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
858 *unit = alloc_unr(ifc->ifc_unrhdr);
862 free_unr(ifc->ifc_unrhdr, *unit);
864 error = ifc_alloc_unit_specific(ifc, unit);
875 ifc_alloc_unit(struct if_clone *ifc, int *unit)
878 return (ifc_alloc_unit_next(ifc, unit));
880 return (ifc_alloc_unit_specific(ifc, unit));
884 ifc_free_unit(struct if_clone *ifc, int unit)
887 free_unr(ifc->ifc_unrhdr, unit);
888 IF_CLONE_REMREF(ifc);
892 ifc_simple_match(struct if_clone *ifc, const char *name)
898 for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
899 if (ifc->ifc_name[i] != *cp)
903 /* Make sure there's a unit number or nothing after the name */
904 for (; *cp != '\0'; cp++) {
905 if (*cp < '0' || *cp > '9')
913 ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
920 err = ifc_name2unit(name, &unit);
924 wildcard = (unit < 0);
926 err = ifc_alloc_unit(ifc, &unit);
930 /* In the wildcard case, we need to update the name. */
932 for (dp = name; *dp != '\0'; dp++);
933 if (snprintf(dp, len - (dp-name), "%d", unit) >
934 len - (dp-name) - 1) {
936 * This can only be a programmer error and
937 * there's no straightforward way to recover if
940 panic("if_clone_create(): interface name too long");
949 ifc_copyin(const struct ifc_data *ifd, void *target, size_t len)
951 if (ifd->params == NULL)
954 if (ifd->flags & IFC_F_SYSSPACE) {
955 memcpy(target, ifd->params, len);
958 return (copyin(ifd->params, target, len));
962 ifc_name(struct if_clone *ifc)
964 return (ifc->ifc_name);
968 ifc_flags_set(struct if_clone *ifc, int flags)
970 ifc->ifc_flags = flags;
974 ifc_flags_get(struct if_clone *ifc)
976 return (ifc->ifc_flags);