xref: /freebsd/sys/net/if_clone.c (revision 833a452e9f082a7982a31c21f0da437dbbe0a39d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
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.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  *
32  *	@(#)if.c	8.5 (Berkeley) 1/9/95
33  * $FreeBSD$
34  */
35 
36 #include <sys/param.h>
37 #include <sys/eventhandler.h>
38 #include <sys/malloc.h>
39 #include <sys/limits.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_clone.h>
50 #include <net/radix.h>
51 #include <net/route.h>
52 #include <net/vnet.h>
53 
54 /* Current IF_MAXUNIT expands maximum to 5 characters. */
55 #define	IFCLOSIZ	(IFNAMSIZ - 5)
56 
57 /*
58  * Structure describing a `cloning' interface.
59  *
60  * List of locks
61  * (c)		const until freeing
62  * (d)		driver specific data, may need external protection.
63  * (e)		locked by if_cloners_mtx
64  * (i)		locked by ifc_mtx mtx
65  */
66 struct if_clone {
67 	char ifc_name[IFCLOSIZ];	/* (c) Name of device, e.g. `gif' */
68 	struct unrhdr *ifc_unrhdr;	/* (c) alloc_unr(9) header */
69 	int ifc_maxunit;		/* (c) maximum unit number */
70 	int ifc_flags;
71 	long ifc_refcnt;		/* (i) Reference count. */
72 	LIST_HEAD(, ifnet) ifc_iflist;	/* (i) List of cloned interfaces */
73 	struct mtx ifc_mtx;		/* Mutex to protect members. */
74 
75 	enum { SIMPLE, ADVANCED } ifc_type; /* (c) */
76 
77 	/* (c) Driver specific cloning functions.  Called with no locks held. */
78 	union {
79 		struct {	/* advanced cloner */
80 			ifc_match_t	*_ifc_match;
81 			ifc_create_t	*_ifc_create;
82 			ifc_destroy_t	*_ifc_destroy;
83 		} A;
84 		struct {	/* simple cloner */
85 			ifcs_create_t	*_ifcs_create;
86 			ifcs_destroy_t	*_ifcs_destroy;
87 			int		_ifcs_minifs;	/* minimum ifs */
88 
89 		} S;
90 	} U;
91 #define	ifc_match	U.A._ifc_match
92 #define	ifc_create	U.A._ifc_create
93 #define	ifc_destroy	U.A._ifc_destroy
94 #define	ifcs_create	U.S._ifcs_create
95 #define	ifcs_destroy	U.S._ifcs_destroy
96 #define	ifcs_minifs	U.S._ifcs_minifs
97 
98 	LIST_ENTRY(if_clone) ifc_list;	/* (e) On list of cloners */
99 };
100 
101 static void	if_clone_free(struct if_clone *ifc);
102 static int	if_clone_createif(struct if_clone *ifc, char *name, size_t len,
103 		    caddr_t params);
104 
105 static int     ifc_simple_match(struct if_clone *, const char *);
106 static int     ifc_simple_create(struct if_clone *, char *, size_t, caddr_t);
107 static int     ifc_simple_destroy(struct if_clone *, struct ifnet *);
108 
109 static struct mtx if_cloners_mtx;
110 MTX_SYSINIT(if_cloners_lock, &if_cloners_mtx, "if_cloners lock", MTX_DEF);
111 VNET_DEFINE_STATIC(int, if_cloners_count);
112 VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners);
113 
114 #define	V_if_cloners_count	VNET(if_cloners_count)
115 #define	V_if_cloners		VNET(if_cloners)
116 
117 #define IF_CLONERS_LOCK_ASSERT()	mtx_assert(&if_cloners_mtx, MA_OWNED)
118 #define IF_CLONERS_LOCK()		mtx_lock(&if_cloners_mtx)
119 #define IF_CLONERS_UNLOCK()		mtx_unlock(&if_cloners_mtx)
120 
121 #define IF_CLONE_LOCK_INIT(ifc)		\
122     mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
123 #define IF_CLONE_LOCK_DESTROY(ifc)	mtx_destroy(&(ifc)->ifc_mtx)
124 #define IF_CLONE_LOCK_ASSERT(ifc)	mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
125 #define IF_CLONE_LOCK(ifc)		mtx_lock(&(ifc)->ifc_mtx)
126 #define IF_CLONE_UNLOCK(ifc)		mtx_unlock(&(ifc)->ifc_mtx)
127 
128 #define IF_CLONE_ADDREF(ifc)						\
129 	do {								\
130 		IF_CLONE_LOCK(ifc);					\
131 		IF_CLONE_ADDREF_LOCKED(ifc);				\
132 		IF_CLONE_UNLOCK(ifc);					\
133 	} while (0)
134 #define IF_CLONE_ADDREF_LOCKED(ifc)					\
135 	do {								\
136 		IF_CLONE_LOCK_ASSERT(ifc);				\
137 		KASSERT((ifc)->ifc_refcnt >= 0,				\
138 		    ("negative refcnt %ld", (ifc)->ifc_refcnt));	\
139 		(ifc)->ifc_refcnt++;					\
140 	} while (0)
141 #define IF_CLONE_REMREF(ifc)						\
142 	do {								\
143 		IF_CLONE_LOCK(ifc);					\
144 		IF_CLONE_REMREF_LOCKED(ifc);				\
145 	} while (0)
146 #define IF_CLONE_REMREF_LOCKED(ifc)					\
147 	do {								\
148 		IF_CLONE_LOCK_ASSERT(ifc);				\
149 		KASSERT((ifc)->ifc_refcnt > 0,				\
150 		    ("bogus refcnt %ld", (ifc)->ifc_refcnt));		\
151 		if (--(ifc)->ifc_refcnt == 0) {				\
152 			IF_CLONE_UNLOCK(ifc);				\
153 			if_clone_free(ifc);				\
154 		} else {						\
155 			/* silently free the lock */			\
156 			IF_CLONE_UNLOCK(ifc);				\
157 		}							\
158 	} while (0)
159 
160 #define IFC_IFLIST_INSERT(_ifc, _ifp)					\
161 	LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones)
162 #define IFC_IFLIST_REMOVE(_ifc, _ifp)					\
163 	LIST_REMOVE(_ifp, if_clones)
164 
165 static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
166 
167 void
168 vnet_if_clone_init(void)
169 {
170 
171 	LIST_INIT(&V_if_cloners);
172 }
173 
174 /*
175  * Lookup and create a clone network interface.
176  */
177 int
178 if_clone_create(char *name, size_t len, caddr_t params)
179 {
180 	struct if_clone *ifc;
181 
182 	/* Try to find an applicable cloner for this request */
183 	IF_CLONERS_LOCK();
184 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
185 		if (ifc->ifc_type == SIMPLE) {
186 			if (ifc_simple_match(ifc, name))
187 				break;
188 		} else {
189 			if (ifc->ifc_match(ifc, name))
190 				break;
191 		}
192 	IF_CLONERS_UNLOCK();
193 
194 	if (ifc == NULL)
195 		return (EINVAL);
196 
197 	return (if_clone_createif(ifc, name, len, params));
198 }
199 
200 void
201 if_clone_addif(struct if_clone *ifc, struct ifnet *ifp)
202 {
203 
204 	if ((ifc->ifc_flags & IFC_NOGROUP) == 0)
205 		if_addgroup(ifp, ifc->ifc_name);
206 
207 	IF_CLONE_LOCK(ifc);
208 	IFC_IFLIST_INSERT(ifc, ifp);
209 	IF_CLONE_UNLOCK(ifc);
210 }
211 
212 /*
213  * Create a clone network interface.
214  */
215 static int
216 if_clone_createif(struct if_clone *ifc, char *name, size_t len, caddr_t params)
217 {
218 	int err;
219 	struct ifnet *ifp;
220 
221 	if (ifunit(name) != NULL)
222 		return (EEXIST);
223 
224 	if (ifc->ifc_type == SIMPLE)
225 		err = ifc_simple_create(ifc, name, len, params);
226 	else
227 		err = (*ifc->ifc_create)(ifc, name, len, params);
228 
229 	if (!err) {
230 		ifp = ifunit(name);
231 		if (ifp == NULL)
232 			panic("%s: lookup failed for %s", __func__, name);
233 
234 		if_clone_addif(ifc, ifp);
235 	}
236 
237 	return (err);
238 }
239 
240 /*
241  * Lookup and destroy a clone network interface.
242  */
243 int
244 if_clone_destroy(const char *name)
245 {
246 	int err;
247 	struct if_clone *ifc;
248 	struct ifnet *ifp;
249 
250 	ifp = ifunit_ref(name);
251 	if (ifp == NULL)
252 		return (ENXIO);
253 
254 	/* Find the cloner for this interface */
255 	CURVNET_SET_QUIET(ifp->if_home_vnet);
256 	IF_CLONERS_LOCK();
257 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
258 		if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) {
259 			break;
260 		}
261 	}
262 	IF_CLONERS_UNLOCK();
263 	CURVNET_RESTORE();
264 	if (ifc == NULL) {
265 		if_rele(ifp);
266 		return (EINVAL);
267 	}
268 
269 	err = if_clone_destroyif(ifc, ifp);
270 	if_rele(ifp);
271 	return err;
272 }
273 
274 /*
275  * Destroy a clone network interface.
276  */
277 int
278 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
279 {
280 	int err;
281 	struct ifnet *ifcifp;
282 
283 	if (ifc->ifc_type == ADVANCED && ifc->ifc_destroy == NULL)
284 		return(EOPNOTSUPP);
285 
286 	/*
287 	 * Given that the cloned ifnet might be attached to a different
288 	 * vnet from where its cloner was registered, we have to
289 	 * switch to the vnet context of the target vnet.
290 	 */
291 	CURVNET_SET_QUIET(ifp->if_vnet);
292 
293 	IF_CLONE_LOCK(ifc);
294 	LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
295 		if (ifcifp == ifp) {
296 			IFC_IFLIST_REMOVE(ifc, ifp);
297 			break;
298 		}
299 	}
300 	IF_CLONE_UNLOCK(ifc);
301 	if (ifcifp == NULL) {
302 		CURVNET_RESTORE();
303 		return (ENXIO);		/* ifp is not on the list. */
304 	}
305 	if ((ifc->ifc_flags & IFC_NOGROUP) == 0)
306 		if_delgroup(ifp, ifc->ifc_name);
307 
308 	if (ifc->ifc_type == SIMPLE)
309 		err = ifc_simple_destroy(ifc, ifp);
310 	else
311 		err = (*ifc->ifc_destroy)(ifc, ifp);
312 
313 	if (err != 0) {
314 		if ((ifc->ifc_flags & IFC_NOGROUP) == 0)
315 			if_addgroup(ifp, ifc->ifc_name);
316 
317 		IF_CLONE_LOCK(ifc);
318 		IFC_IFLIST_INSERT(ifc, ifp);
319 		IF_CLONE_UNLOCK(ifc);
320 	}
321 	CURVNET_RESTORE();
322 	return (err);
323 }
324 
325 static struct if_clone *
326 if_clone_alloc(const char *name, int maxunit)
327 {
328 	struct if_clone *ifc;
329 
330 	KASSERT(name != NULL, ("%s: no name\n", __func__));
331 
332 	ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO);
333 	strncpy(ifc->ifc_name, name, IFCLOSIZ-1);
334 	IF_CLONE_LOCK_INIT(ifc);
335 	IF_CLONE_ADDREF(ifc);
336 	ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT;
337 	ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx);
338 	LIST_INIT(&ifc->ifc_iflist);
339 
340 	return (ifc);
341 }
342 
343 static int
344 if_clone_attach(struct if_clone *ifc)
345 {
346 	struct if_clone *ifc1;
347 
348 	IF_CLONERS_LOCK();
349 	LIST_FOREACH(ifc1, &V_if_cloners, ifc_list)
350 		if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) {
351 			IF_CLONERS_UNLOCK();
352 			IF_CLONE_REMREF(ifc);
353 			return (EEXIST);
354 		}
355 	LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
356 	V_if_cloners_count++;
357 	IF_CLONERS_UNLOCK();
358 
359 	return (0);
360 }
361 
362 struct if_clone *
363 if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
364 	ifc_create_t create, ifc_destroy_t destroy)
365 {
366 	struct if_clone *ifc;
367 
368 	ifc = if_clone_alloc(name, maxunit);
369 	ifc->ifc_type = ADVANCED;
370 	ifc->ifc_match = match;
371 	ifc->ifc_create = create;
372 	ifc->ifc_destroy = destroy;
373 
374 	if (if_clone_attach(ifc) != 0)
375 		return (NULL);
376 
377 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
378 
379 	return (ifc);
380 }
381 
382 struct if_clone *
383 if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
384 	u_int minifs)
385 {
386 	struct if_clone *ifc;
387 	u_int unit;
388 
389 	ifc = if_clone_alloc(name, 0);
390 	ifc->ifc_type = SIMPLE;
391 	ifc->ifcs_create = create;
392 	ifc->ifcs_destroy = destroy;
393 	ifc->ifcs_minifs = minifs;
394 
395 	if (if_clone_attach(ifc) != 0)
396 		return (NULL);
397 
398 	for (unit = 0; unit < minifs; unit++) {
399 		char name[IFNAMSIZ];
400 		int error __unused;
401 
402 		snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
403 		error = if_clone_createif(ifc, name, IFNAMSIZ, NULL);
404 		KASSERT(error == 0,
405 		    ("%s: failed to create required interface %s",
406 		    __func__, name));
407 	}
408 
409 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
410 
411 	return (ifc);
412 }
413 
414 /*
415  * Unregister a network interface cloner.
416  */
417 void
418 if_clone_detach(struct if_clone *ifc)
419 {
420 
421 	IF_CLONERS_LOCK();
422 	LIST_REMOVE(ifc, ifc_list);
423 	V_if_cloners_count--;
424 	IF_CLONERS_UNLOCK();
425 
426 	/* Allow all simples to be destroyed */
427 	if (ifc->ifc_type == SIMPLE)
428 		ifc->ifcs_minifs = 0;
429 
430 	/* destroy all interfaces for this cloner */
431 	while (!LIST_EMPTY(&ifc->ifc_iflist))
432 		if_clone_destroyif(ifc, LIST_FIRST(&ifc->ifc_iflist));
433 
434 	IF_CLONE_REMREF(ifc);
435 }
436 
437 static void
438 if_clone_free(struct if_clone *ifc)
439 {
440 
441 	KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
442 	    ("%s: ifc_iflist not empty", __func__));
443 
444 	IF_CLONE_LOCK_DESTROY(ifc);
445 	delete_unrhdr(ifc->ifc_unrhdr);
446 	free(ifc, M_CLONE);
447 }
448 
449 /*
450  * Provide list of interface cloners to userspace.
451  */
452 int
453 if_clone_list(struct if_clonereq *ifcr)
454 {
455 	char *buf, *dst, *outbuf = NULL;
456 	struct if_clone *ifc;
457 	int buf_count, count, err = 0;
458 
459 	if (ifcr->ifcr_count < 0)
460 		return (EINVAL);
461 
462 	IF_CLONERS_LOCK();
463 	/*
464 	 * Set our internal output buffer size.  We could end up not
465 	 * reporting a cloner that is added between the unlock and lock
466 	 * below, but that's not a major problem.  Not caping our
467 	 * allocation to the number of cloners actually in the system
468 	 * could be because that would let arbitrary users cause us to
469 	 * allocate arbitrary amounts of kernel memory.
470 	 */
471 	buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
472 	    V_if_cloners_count : ifcr->ifcr_count;
473 	IF_CLONERS_UNLOCK();
474 
475 	outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
476 
477 	IF_CLONERS_LOCK();
478 
479 	ifcr->ifcr_total = V_if_cloners_count;
480 	if ((dst = ifcr->ifcr_buffer) == NULL) {
481 		/* Just asking how many there are. */
482 		goto done;
483 	}
484 	count = (V_if_cloners_count < buf_count) ?
485 	    V_if_cloners_count : buf_count;
486 
487 	for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
488 	    ifc != NULL && count != 0;
489 	    ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
490 		strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
491 	}
492 
493 done:
494 	IF_CLONERS_UNLOCK();
495 	if (err == 0 && dst != NULL)
496 		err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
497 	if (outbuf != NULL)
498 		free(outbuf, M_CLONE);
499 	return (err);
500 }
501 
502 #ifdef VIMAGE
503 /*
504  * if_clone_restoregroup() is used in context of if_vmove().
505  *
506  * Since if_detach_internal() has removed the interface from ALL groups, we
507  * need to "restore" interface membership in the cloner's group.  Note that
508  * interface belongs to cloner in its home vnet, so we first find the original
509  * cloner, and then we confirm that cloner with the same name exists in the
510  * current vnet.
511  */
512 void
513 if_clone_restoregroup(struct ifnet *ifp)
514 {
515 	struct if_clone *ifc;
516 	struct ifnet *ifcifp;
517 	char ifc_name[IFCLOSIZ] = { [0] = '\0' };
518 
519 	CURVNET_SET_QUIET(ifp->if_home_vnet);
520 	IF_CLONERS_LOCK();
521 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
522 		IF_CLONE_LOCK(ifc);
523 		LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
524 			if (ifp == ifcifp) {
525 				strncpy(ifc_name, ifc->ifc_name, IFCLOSIZ-1);
526 				break;
527 			}
528 		}
529 		IF_CLONE_UNLOCK(ifc);
530 		if (ifc_name[0] != '\0')
531 			break;
532 	}
533 	CURVNET_RESTORE();
534 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
535 		if (strcmp(ifc->ifc_name, ifc_name) == 0 &&
536 		    ((ifc->ifc_flags & IFC_NOGROUP) == 0))
537 			break;
538 	IF_CLONERS_UNLOCK();
539 
540 	if (ifc != NULL)
541 		if_addgroup(ifp, ifc_name);
542 }
543 #endif
544 
545 /*
546  * A utility function to extract unit numbers from interface names of
547  * the form name###.
548  *
549  * Returns 0 on success and an error on failure.
550  */
551 int
552 ifc_name2unit(const char *name, int *unit)
553 {
554 	const char	*cp;
555 	int		cutoff = INT_MAX / 10;
556 	int		cutlim = INT_MAX % 10;
557 
558 	for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++)
559 		;
560 	if (*cp == '\0') {
561 		*unit = -1;
562 	} else if (cp[0] == '0' && cp[1] != '\0') {
563 		/* Disallow leading zeroes. */
564 		return (EINVAL);
565 	} else {
566 		for (*unit = 0; *cp != '\0'; cp++) {
567 			if (*cp < '0' || *cp > '9') {
568 				/* Bogus unit number. */
569 				return (EINVAL);
570 			}
571 			if (*unit > cutoff ||
572 			    (*unit == cutoff && *cp - '0' > cutlim))
573 				return (EINVAL);
574 			*unit = (*unit * 10) + (*cp - '0');
575 		}
576 	}
577 
578 	return (0);
579 }
580 
581 static int
582 ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
583 {
584 	char name[IFNAMSIZ];
585 
586 	if (*unit > ifc->ifc_maxunit)
587 		return (ENOSPC);
588 
589 	if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
590 		return (EEXIST);
591 
592 	snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
593 	if (ifunit(name) != NULL) {
594 		free_unr(ifc->ifc_unrhdr, *unit);
595 		return (EEXIST);
596 	}
597 
598 	IF_CLONE_ADDREF(ifc);
599 
600 	return (0);
601 }
602 
603 static int
604 ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
605 {
606 	int error;
607 
608 	*unit = alloc_unr(ifc->ifc_unrhdr);
609 	if (*unit == -1)
610 		return (ENOSPC);
611 
612 	free_unr(ifc->ifc_unrhdr, *unit);
613 	for (;;) {
614 		error = ifc_alloc_unit_specific(ifc, unit);
615 		if (error != EEXIST)
616 			break;
617 
618 		(*unit)++;
619 	}
620 
621 	return (error);
622 }
623 
624 int
625 ifc_alloc_unit(struct if_clone *ifc, int *unit)
626 {
627 	if (*unit < 0)
628 		return (ifc_alloc_unit_next(ifc, unit));
629 	else
630 		return (ifc_alloc_unit_specific(ifc, unit));
631 }
632 
633 void
634 ifc_free_unit(struct if_clone *ifc, int unit)
635 {
636 
637 	free_unr(ifc->ifc_unrhdr, unit);
638 	IF_CLONE_REMREF(ifc);
639 }
640 
641 static int
642 ifc_simple_match(struct if_clone *ifc, const char *name)
643 {
644 	const char *cp;
645 	int i;
646 
647 	/* Match the name */
648 	for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
649 		if (ifc->ifc_name[i] != *cp)
650 			return (0);
651 	}
652 
653 	/* Make sure there's a unit number or nothing after the name */
654 	for (; *cp != '\0'; cp++) {
655 		if (*cp < '0' || *cp > '9')
656 			return (0);
657 	}
658 
659 	return (1);
660 }
661 
662 static int
663 ifc_simple_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
664 {
665 	char *dp;
666 	int wildcard;
667 	int unit;
668 	int err;
669 
670 	err = ifc_name2unit(name, &unit);
671 	if (err != 0)
672 		return (err);
673 
674 	wildcard = (unit < 0);
675 
676 	err = ifc_alloc_unit(ifc, &unit);
677 	if (err != 0)
678 		return (err);
679 
680 	err = ifc->ifcs_create(ifc, unit, params);
681 	if (err != 0) {
682 		ifc_free_unit(ifc, unit);
683 		return (err);
684 	}
685 
686 	/* In the wildcard case, we need to update the name. */
687 	if (wildcard) {
688 		for (dp = name; *dp != '\0'; dp++);
689 		if (snprintf(dp, len - (dp-name), "%d", unit) >
690 		    len - (dp-name) - 1) {
691 			/*
692 			 * This can only be a programmer error and
693 			 * there's no straightforward way to recover if
694 			 * it happens.
695 			 */
696 			panic("if_clone_create(): interface name too long");
697 		}
698 	}
699 
700 	return (0);
701 }
702 
703 static int
704 ifc_simple_destroy(struct if_clone *ifc, struct ifnet *ifp)
705 {
706 	int unit;
707 
708 	unit = ifp->if_dunit;
709 
710 	if (unit < ifc->ifcs_minifs)
711 		return (EINVAL);
712 
713 	ifc->ifcs_destroy(ifp);
714 
715 	ifc_free_unit(ifc, unit);
716 
717 	return (0);
718 }
719 
720 const char *
721 ifc_name(struct if_clone *ifc)
722 {
723 	return (ifc->ifc_name);
724 }
725 
726 void
727 ifc_flags_set(struct if_clone *ifc, int flags)
728 {
729 	ifc->ifc_flags = flags;
730 }
731 
732 int
733 ifc_flags_get(struct if_clone *ifc)
734 {
735 	return (ifc->ifc_flags);
736 }
737