xref: /freebsd/sys/net/if_clone.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*-
2  * Copyright (c) 1980, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)if.c	8.5 (Berkeley) 1/9/95
30  * $FreeBSD$
31  */
32 
33 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/limits.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 
43 #include <net/if.h>
44 #include <net/if_clone.h>
45 #if 0
46 #include <net/if_dl.h>
47 #endif
48 #include <net/if_types.h>
49 #include <net/if_var.h>
50 #include <net/radix.h>
51 #include <net/route.h>
52 #include <net/vnet.h>
53 
54 static void	if_clone_free(struct if_clone *ifc);
55 static int	if_clone_createif(struct if_clone *ifc, char *name, size_t len,
56 		    caddr_t params);
57 
58 static struct mtx	if_cloners_mtx;
59 static VNET_DEFINE(int, if_cloners_count);
60 VNET_DEFINE(LIST_HEAD(, if_clone), if_cloners);
61 
62 #define	V_if_cloners_count	VNET(if_cloners_count)
63 #define	V_if_cloners		VNET(if_cloners)
64 
65 #define IF_CLONERS_LOCK_INIT()		\
66     mtx_init(&if_cloners_mtx, "if_cloners lock", NULL, MTX_DEF)
67 #define IF_CLONERS_LOCK_ASSERT()	mtx_assert(&if_cloners_mtx, MA_OWNED)
68 #define IF_CLONERS_LOCK()		mtx_lock(&if_cloners_mtx)
69 #define IF_CLONERS_UNLOCK()		mtx_unlock(&if_cloners_mtx)
70 
71 #define IF_CLONE_LOCK_INIT(ifc)		\
72     mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
73 #define IF_CLONE_LOCK_DESTROY(ifc)	mtx_destroy(&(ifc)->ifc_mtx)
74 #define IF_CLONE_LOCK_ASSERT(ifc)	mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
75 #define IF_CLONE_LOCK(ifc)		mtx_lock(&(ifc)->ifc_mtx)
76 #define IF_CLONE_UNLOCK(ifc)		mtx_unlock(&(ifc)->ifc_mtx)
77 
78 #define IF_CLONE_ADDREF(ifc)						\
79 	do {								\
80 		IF_CLONE_LOCK(ifc);					\
81 		IF_CLONE_ADDREF_LOCKED(ifc);				\
82 		IF_CLONE_UNLOCK(ifc);					\
83 	} while (0)
84 #define IF_CLONE_ADDREF_LOCKED(ifc)					\
85 	do {								\
86 		IF_CLONE_LOCK_ASSERT(ifc);				\
87 		KASSERT((ifc)->ifc_refcnt >= 0,				\
88 		    ("negative refcnt %ld", (ifc)->ifc_refcnt));	\
89 		(ifc)->ifc_refcnt++;					\
90 	} while (0)
91 #define IF_CLONE_REMREF(ifc)						\
92 	do {								\
93 		IF_CLONE_LOCK(ifc);					\
94 		IF_CLONE_REMREF_LOCKED(ifc);				\
95 	} while (0)
96 #define IF_CLONE_REMREF_LOCKED(ifc)					\
97 	do {								\
98 		IF_CLONE_LOCK_ASSERT(ifc);				\
99 		KASSERT((ifc)->ifc_refcnt > 0,				\
100 		    ("bogus refcnt %ld", (ifc)->ifc_refcnt));		\
101 		if (--(ifc)->ifc_refcnt == 0) {				\
102 			IF_CLONE_UNLOCK(ifc);				\
103 			if_clone_free(ifc);				\
104 		} else {						\
105 			/* silently free the lock */			\
106 			IF_CLONE_UNLOCK(ifc);				\
107 		}							\
108 	} while (0)
109 
110 #define IFC_IFLIST_INSERT(_ifc, _ifp)					\
111 	LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones)
112 #define IFC_IFLIST_REMOVE(_ifc, _ifp)					\
113 	LIST_REMOVE(_ifp, if_clones)
114 
115 static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
116 
117 void
118 vnet_if_clone_init(void)
119 {
120 
121 	LIST_INIT(&V_if_cloners);
122 }
123 
124 void
125 if_clone_init(void)
126 {
127 
128 	IF_CLONERS_LOCK_INIT();
129 }
130 
131 /*
132  * Lookup and create a clone network interface.
133  */
134 int
135 if_clone_create(char *name, size_t len, caddr_t params)
136 {
137 	struct if_clone *ifc;
138 
139 	/* Try to find an applicable cloner for this request */
140 	IF_CLONERS_LOCK();
141 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
142 		if (ifc->ifc_match(ifc, name)) {
143 			break;
144 		}
145 	}
146 #ifdef VIMAGE
147 	if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
148 		CURVNET_SET_QUIET(vnet0);
149 		LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
150 			if (ifc->ifc_match(ifc, name))
151 				break;
152 		}
153 		CURVNET_RESTORE();
154 	}
155 #endif
156 	IF_CLONERS_UNLOCK();
157 
158 	if (ifc == NULL)
159 		return (EINVAL);
160 
161 	return (if_clone_createif(ifc, name, len, params));
162 }
163 
164 /*
165  * Create a clone network interface.
166  */
167 static int
168 if_clone_createif(struct if_clone *ifc, char *name, size_t len, caddr_t params)
169 {
170 	int err;
171 	struct ifnet *ifp;
172 
173 	if (ifunit(name) != NULL)
174 		return (EEXIST);
175 
176 	err = (*ifc->ifc_create)(ifc, name, len, params);
177 
178 	if (!err) {
179 		ifp = ifunit(name);
180 		if (ifp == NULL)
181 			panic("%s: lookup failed for %s", __func__, name);
182 
183 		if_addgroup(ifp, ifc->ifc_name);
184 
185 		IF_CLONE_LOCK(ifc);
186 		IFC_IFLIST_INSERT(ifc, ifp);
187 		IF_CLONE_UNLOCK(ifc);
188 	}
189 
190 	return (err);
191 }
192 
193 /*
194  * Lookup and destroy a clone network interface.
195  */
196 int
197 if_clone_destroy(const char *name)
198 {
199 	struct if_clone *ifc;
200 	struct ifnet *ifp;
201 
202 	ifp = ifunit(name);
203 	if (ifp == NULL)
204 		return (ENXIO);
205 
206 	/* Find the cloner for this interface */
207 	IF_CLONERS_LOCK();
208 	LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
209 		if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) {
210 			break;
211 		}
212 	}
213 #ifdef VIMAGE
214 	if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
215 		CURVNET_SET_QUIET(vnet0);
216 		LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
217 			if (ifc->ifc_match(ifc, name))
218 				break;
219 		}
220 		CURVNET_RESTORE();
221 	}
222 #endif
223 	IF_CLONERS_UNLOCK();
224 	if (ifc == NULL)
225 		return (EINVAL);
226 
227 	return (if_clone_destroyif(ifc, ifp));
228 }
229 
230 /*
231  * Destroy a clone network interface.
232  */
233 int
234 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
235 {
236 	int err;
237 
238 	if (ifc->ifc_destroy == NULL)
239 		return(EOPNOTSUPP);
240 
241 	/*
242 	 * Given that the cloned ifnet might be attached to a different
243 	 * vnet from where its cloner was registered, we have to
244 	 * switch to the vnet context of the target vnet.
245 	 */
246 	CURVNET_SET_QUIET(ifp->if_vnet);
247 
248 	IF_CLONE_LOCK(ifc);
249 	IFC_IFLIST_REMOVE(ifc, ifp);
250 	IF_CLONE_UNLOCK(ifc);
251 
252 	if_delgroup(ifp, ifc->ifc_name);
253 
254 	err =  (*ifc->ifc_destroy)(ifc, ifp);
255 
256 	if (err != 0) {
257 		if_addgroup(ifp, ifc->ifc_name);
258 
259 		IF_CLONE_LOCK(ifc);
260 		IFC_IFLIST_INSERT(ifc, ifp);
261 		IF_CLONE_UNLOCK(ifc);
262 	}
263 	CURVNET_RESTORE();
264 	return (err);
265 }
266 
267 /*
268  * Register a network interface cloner.
269  */
270 void
271 if_clone_attach(struct if_clone *ifc)
272 {
273 	int len, maxclone;
274 
275 	/*
276 	 * Compute bitmap size and allocate it.
277 	 */
278 	maxclone = ifc->ifc_maxunit + 1;
279 	len = maxclone >> 3;
280 	if ((len << 3) < maxclone)
281 		len++;
282 	ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
283 	ifc->ifc_bmlen = len;
284 	IF_CLONE_LOCK_INIT(ifc);
285 	IF_CLONE_ADDREF(ifc);
286 
287 	IF_CLONERS_LOCK();
288 	LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
289 	V_if_cloners_count++;
290 	IF_CLONERS_UNLOCK();
291 
292 	LIST_INIT(&ifc->ifc_iflist);
293 
294 	if (ifc->ifc_attach != NULL)
295 		(*ifc->ifc_attach)(ifc);
296 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
297 }
298 
299 /*
300  * Unregister a network interface cloner.
301  */
302 void
303 if_clone_detach(struct if_clone *ifc)
304 {
305 	struct ifc_simple_data *ifcs = ifc->ifc_data;
306 
307 	IF_CLONERS_LOCK();
308 	LIST_REMOVE(ifc, ifc_list);
309 	V_if_cloners_count--;
310 	IF_CLONERS_UNLOCK();
311 
312 	/* Allow all simples to be destroyed */
313 	if (ifc->ifc_attach == ifc_simple_attach)
314 		ifcs->ifcs_minifs = 0;
315 
316 	/* destroy all interfaces for this cloner */
317 	while (!LIST_EMPTY(&ifc->ifc_iflist))
318 		if_clone_destroyif(ifc, LIST_FIRST(&ifc->ifc_iflist));
319 
320 	IF_CLONE_REMREF(ifc);
321 }
322 
323 static void
324 if_clone_free(struct if_clone *ifc)
325 {
326 	for (int bytoff = 0; bytoff < ifc->ifc_bmlen; bytoff++) {
327 		KASSERT(ifc->ifc_units[bytoff] == 0x00,
328 		    ("ifc_units[%d] is not empty", bytoff));
329 	}
330 
331 	KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
332 	    ("%s: ifc_iflist not empty", __func__));
333 
334 	IF_CLONE_LOCK_DESTROY(ifc);
335 	free(ifc->ifc_units, M_CLONE);
336 }
337 
338 /*
339  * Provide list of interface cloners to userspace.
340  */
341 int
342 if_clone_list(struct if_clonereq *ifcr)
343 {
344 	char *buf, *dst, *outbuf = NULL;
345 	struct if_clone *ifc;
346 	int buf_count, count, err = 0;
347 
348 	if (ifcr->ifcr_count < 0)
349 		return (EINVAL);
350 
351 	IF_CLONERS_LOCK();
352 	/*
353 	 * Set our internal output buffer size.  We could end up not
354 	 * reporting a cloner that is added between the unlock and lock
355 	 * below, but that's not a major problem.  Not caping our
356 	 * allocation to the number of cloners actually in the system
357 	 * could be because that would let arbitrary users cause us to
358 	 * allocate abritrary amounts of kernel memory.
359 	 */
360 	buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
361 	    V_if_cloners_count : ifcr->ifcr_count;
362 	IF_CLONERS_UNLOCK();
363 
364 	outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
365 
366 	IF_CLONERS_LOCK();
367 
368 	ifcr->ifcr_total = V_if_cloners_count;
369 	if ((dst = ifcr->ifcr_buffer) == NULL) {
370 		/* Just asking how many there are. */
371 		goto done;
372 	}
373 	count = (V_if_cloners_count < buf_count) ?
374 	    V_if_cloners_count : buf_count;
375 
376 	for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
377 	    ifc != NULL && count != 0;
378 	    ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
379 		strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
380 	}
381 
382 done:
383 	IF_CLONERS_UNLOCK();
384 	if (err == 0)
385 		err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
386 	if (outbuf != NULL)
387 		free(outbuf, M_CLONE);
388 	return (err);
389 }
390 
391 /*
392  * A utility function to extract unit numbers from interface names of
393  * the form name###.
394  *
395  * Returns 0 on success and an error on failure.
396  */
397 int
398 ifc_name2unit(const char *name, int *unit)
399 {
400 	const char	*cp;
401 	int		cutoff = INT_MAX / 10;
402 	int		cutlim = INT_MAX % 10;
403 
404 	for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++);
405 	if (*cp == '\0') {
406 		*unit = -1;
407 	} else if (cp[0] == '0' && cp[1] != '\0') {
408 		/* Disallow leading zeroes. */
409 		return (EINVAL);
410 	} else {
411 		for (*unit = 0; *cp != '\0'; cp++) {
412 			if (*cp < '0' || *cp > '9') {
413 				/* Bogus unit number. */
414 				return (EINVAL);
415 			}
416 			if (*unit > cutoff ||
417 			    (*unit == cutoff && *cp - '0' > cutlim))
418 				return (EINVAL);
419 			*unit = (*unit * 10) + (*cp - '0');
420 		}
421 	}
422 
423 	return (0);
424 }
425 
426 int
427 ifc_alloc_unit(struct if_clone *ifc, int *unit)
428 {
429 	int wildcard, bytoff, bitoff;
430 	int err = 0;
431 
432 	IF_CLONE_LOCK(ifc);
433 
434 	bytoff = bitoff = 0;
435 	wildcard = (*unit < 0);
436 	/*
437 	 * Find a free unit if none was given.
438 	 */
439 	if (wildcard) {
440 		while ((bytoff < ifc->ifc_bmlen)
441 		    && (ifc->ifc_units[bytoff] == 0xff))
442 			bytoff++;
443 		if (bytoff >= ifc->ifc_bmlen) {
444 			err = ENOSPC;
445 			goto done;
446 		}
447 		while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
448 			bitoff++;
449 		*unit = (bytoff << 3) + bitoff;
450 	}
451 
452 	if (*unit > ifc->ifc_maxunit) {
453 		err = ENOSPC;
454 		goto done;
455 	}
456 
457 	if (!wildcard) {
458 		bytoff = *unit >> 3;
459 		bitoff = *unit - (bytoff << 3);
460 	}
461 
462 	if((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) {
463 		err = EEXIST;
464 		goto done;
465 	}
466 	/*
467 	 * Allocate the unit in the bitmap.
468 	 */
469 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
470 	    ("%s: bit is already set", __func__));
471 	ifc->ifc_units[bytoff] |= (1 << bitoff);
472 	IF_CLONE_ADDREF_LOCKED(ifc);
473 
474 done:
475 	IF_CLONE_UNLOCK(ifc);
476 	return (err);
477 }
478 
479 void
480 ifc_free_unit(struct if_clone *ifc, int unit)
481 {
482 	int bytoff, bitoff;
483 
484 
485 	/*
486 	 * Compute offset in the bitmap and deallocate the unit.
487 	 */
488 	bytoff = unit >> 3;
489 	bitoff = unit - (bytoff << 3);
490 
491 	IF_CLONE_LOCK(ifc);
492 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
493 	    ("%s: bit is already cleared", __func__));
494 	ifc->ifc_units[bytoff] &= ~(1 << bitoff);
495 	IF_CLONE_REMREF_LOCKED(ifc);	/* releases lock */
496 }
497 
498 void
499 ifc_simple_attach(struct if_clone *ifc)
500 {
501 	int err;
502 	int unit;
503 	char name[IFNAMSIZ];
504 	struct ifc_simple_data *ifcs = ifc->ifc_data;
505 
506 	KASSERT(ifcs->ifcs_minifs - 1 <= ifc->ifc_maxunit,
507 	    ("%s: %s requested more units than allowed (%d > %d)",
508 	    __func__, ifc->ifc_name, ifcs->ifcs_minifs,
509 	    ifc->ifc_maxunit + 1));
510 
511 	for (unit = 0; unit < ifcs->ifcs_minifs; unit++) {
512 		snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
513 		err = if_clone_createif(ifc, name, IFNAMSIZ, NULL);
514 		KASSERT(err == 0,
515 		    ("%s: failed to create required interface %s",
516 		    __func__, name));
517 	}
518 }
519 
520 int
521 ifc_simple_match(struct if_clone *ifc, const char *name)
522 {
523 	const char *cp;
524 	int i;
525 
526 	/* Match the name */
527 	for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
528 		if (ifc->ifc_name[i] != *cp)
529 			return (0);
530 	}
531 
532 	/* Make sure there's a unit number or nothing after the name */
533 	for (; *cp != '\0'; cp++) {
534 		if (*cp < '0' || *cp > '9')
535 			return (0);
536 	}
537 
538 	return (1);
539 }
540 
541 int
542 ifc_simple_create(struct if_clone *ifc, char *name, size_t len, caddr_t params)
543 {
544 	char *dp;
545 	int wildcard;
546 	int unit;
547 	int err;
548 	struct ifc_simple_data *ifcs = ifc->ifc_data;
549 
550 	err = ifc_name2unit(name, &unit);
551 	if (err != 0)
552 		return (err);
553 
554 	wildcard = (unit < 0);
555 
556 	err = ifc_alloc_unit(ifc, &unit);
557 	if (err != 0)
558 		return (err);
559 
560 	err = ifcs->ifcs_create(ifc, unit, params);
561 	if (err != 0) {
562 		ifc_free_unit(ifc, unit);
563 		return (err);
564 	}
565 
566 	/* In the wildcard case, we need to update the name. */
567 	if (wildcard) {
568 		for (dp = name; *dp != '\0'; dp++);
569 		if (snprintf(dp, len - (dp-name), "%d", unit) >
570 		    len - (dp-name) - 1) {
571 			/*
572 			 * This can only be a programmer error and
573 			 * there's no straightforward way to recover if
574 			 * it happens.
575 			 */
576 			panic("if_clone_create(): interface name too long");
577 		}
578 
579 	}
580 
581 	return (0);
582 }
583 
584 int
585 ifc_simple_destroy(struct if_clone *ifc, struct ifnet *ifp)
586 {
587 	int unit;
588 	struct ifc_simple_data *ifcs = ifc->ifc_data;
589 
590 	unit = ifp->if_dunit;
591 
592 	if (unit < ifcs->ifcs_minifs)
593 		return (EINVAL);
594 
595 	ifcs->ifcs_destroy(ifp);
596 
597 	ifc_free_unit(ifc, unit);
598 
599 	return (0);
600 }
601