xref: /freebsd/sys/net/if_clone.c (revision 87569f75a91f298c52a71823c04d41cf53c88889)
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 
53 static void	if_clone_free(struct if_clone *ifc);
54 static int	if_clone_createif(struct if_clone *ifc, char *name, size_t len);
55 static int	if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp);
56 
57 static struct mtx	if_cloners_mtx;
58 static int		if_cloners_count;
59 LIST_HEAD(, if_clone)	if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
60 
61 #define IF_CLONERS_LOCK_INIT()		\
62     mtx_init(&if_cloners_mtx, "if_cloners lock", NULL, MTX_DEF)
63 #define IF_CLONERS_LOCK_ASSERT()	mtx_assert(&if_cloners_mtx, MA_OWNED)
64 #define IF_CLONERS_LOCK()		mtx_lock(&if_cloners_mtx)
65 #define IF_CLONERS_UNLOCK()		mtx_unlock(&if_cloners_mtx)
66 
67 #define IF_CLONE_LOCK_INIT(ifc)		\
68     mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
69 #define IF_CLONE_LOCK_DESTROY(ifc)	mtx_destroy(&(ifc)->ifc_mtx)
70 #define IF_CLONE_LOCK_ASSERT(ifc)	mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
71 #define IF_CLONE_LOCK(ifc)		mtx_lock(&(ifc)->ifc_mtx)
72 #define IF_CLONE_UNLOCK(ifc)		mtx_unlock(&(ifc)->ifc_mtx)
73 
74 #define IF_CLONE_ADDREF(ifc)						\
75 	do {								\
76 		IF_CLONE_LOCK(ifc);					\
77 		IF_CLONE_ADDREF_LOCKED(ifc);				\
78 		IF_CLONE_UNLOCK(ifc);					\
79 	} while (0)
80 #define IF_CLONE_ADDREF_LOCKED(ifc)					\
81 	do {								\
82 		IF_CLONE_LOCK_ASSERT(ifc);				\
83 		KASSERT((ifc)->ifc_refcnt >= 0,				\
84 		    ("negative refcnt %ld", (ifc)->ifc_refcnt));	\
85 		(ifc)->ifc_refcnt++;					\
86 	} while (0)
87 #define IF_CLONE_REMREF(ifc)						\
88 	do {								\
89 		IF_CLONE_LOCK(ifc);					\
90 		IF_CLONE_REMREF_LOCKED(ifc);				\
91 	} while (0)
92 #define IF_CLONE_REMREF_LOCKED(ifc)					\
93 	do {								\
94 		IF_CLONE_LOCK_ASSERT(ifc);				\
95 		KASSERT((ifc)->ifc_refcnt > 0,				\
96 		    ("bogus refcnt %ld", (ifc)->ifc_refcnt));		\
97 		if (--(ifc)->ifc_refcnt == 0) {				\
98 			IF_CLONE_UNLOCK(ifc);				\
99 			if_clone_free(ifc);				\
100 		} else {						\
101 			/* silently free the lock */			\
102 			IF_CLONE_UNLOCK(ifc);				\
103 		}							\
104 	} while (0)
105 
106 #define IFC_IFLIST_INSERT(_ifc, _ifp)					\
107 	LIST_INSERT_HEAD(&_ifc->ifc_iflist, _ifp, if_clones)
108 #define IFC_IFLIST_REMOVE(_ifc, _ifp)					\
109 	LIST_REMOVE(_ifp, if_clones)
110 
111 static MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
112 
113 void
114 if_clone_init(void)
115 {
116 	IF_CLONERS_LOCK_INIT();
117 }
118 
119 /*
120  * Lookup and create a clone network interface.
121  */
122 int
123 if_clone_create(char *name, size_t len)
124 {
125 	struct if_clone *ifc;
126 
127 	/* Try to find an applicable cloner for this request */
128 	IF_CLONERS_LOCK();
129 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
130 		if (ifc->ifc_match(ifc, name)) {
131 			break;
132 		}
133 	}
134 	IF_CLONERS_UNLOCK();
135 
136 	if (ifc == NULL)
137 		return (EINVAL);
138 
139 	return (if_clone_createif(ifc, name, len));
140 }
141 
142 /*
143  * Create a clone network interface.
144  */
145 static int
146 if_clone_createif(struct if_clone *ifc, char *name, size_t len)
147 {
148 	int err;
149 	struct ifnet *ifp;
150 
151 	if (ifunit(name) != NULL)
152 		return (EEXIST);
153 
154 	err = (*ifc->ifc_create)(ifc, name, len);
155 
156 	if (!err) {
157 		ifp = ifunit(name);
158 		if (ifp == NULL)
159 			panic("%s: lookup failed for %s", __func__, name);
160 
161 		IF_CLONE_LOCK(ifc);
162 		IFC_IFLIST_INSERT(ifc, ifp);
163 		IF_CLONE_UNLOCK(ifc);
164 	}
165 
166 	return (err);
167 }
168 
169 /*
170  * Lookup and destroy a clone network interface.
171  */
172 int
173 if_clone_destroy(const char *name)
174 {
175 	struct if_clone *ifc;
176 	struct ifnet *ifp;
177 
178 	ifp = ifunit(name);
179 	if (ifp == NULL)
180 		return (ENXIO);
181 
182 	/* Find the cloner for this interface */
183 	IF_CLONERS_LOCK();
184 	LIST_FOREACH(ifc, &if_cloners, ifc_list) {
185 		if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) {
186 			break;
187 		}
188 	}
189 	IF_CLONERS_UNLOCK();
190 	if (ifc == NULL)
191 		return (EINVAL);
192 
193 	return (if_clone_destroyif(ifc, ifp));
194 }
195 
196 /*
197  * Destroy a clone network interface.
198  */
199 static int
200 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
201 {
202 	int err;
203 
204 	if (ifc->ifc_destroy == NULL) {
205 		err = EOPNOTSUPP;
206 		goto done;
207 	}
208 
209 	IF_CLONE_LOCK(ifc);
210 	IFC_IFLIST_REMOVE(ifc, ifp);
211 	IF_CLONE_UNLOCK(ifc);
212 
213 	err =  (*ifc->ifc_destroy)(ifc, ifp);
214 
215 	if (err != 0) {
216 		IF_CLONE_LOCK(ifc);
217 		IFC_IFLIST_INSERT(ifc, ifp);
218 		IF_CLONE_UNLOCK(ifc);
219 	}
220 
221 done:
222 	return (err);
223 }
224 
225 /*
226  * Register a network interface cloner.
227  */
228 void
229 if_clone_attach(struct if_clone *ifc)
230 {
231 	int len, maxclone;
232 
233 	/*
234 	 * Compute bitmap size and allocate it.
235 	 */
236 	maxclone = ifc->ifc_maxunit + 1;
237 	len = maxclone >> 3;
238 	if ((len << 3) < maxclone)
239 		len++;
240 	ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
241 	ifc->ifc_bmlen = len;
242 	IF_CLONE_LOCK_INIT(ifc);
243 	IF_CLONE_ADDREF(ifc);
244 
245 	IF_CLONERS_LOCK();
246 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
247 	if_cloners_count++;
248 	IF_CLONERS_UNLOCK();
249 
250 	LIST_INIT(&ifc->ifc_iflist);
251 
252 	if (ifc->ifc_attach != NULL)
253 		(*ifc->ifc_attach)(ifc);
254 	EVENTHANDLER_INVOKE(if_clone_event, ifc);
255 }
256 
257 /*
258  * Unregister a network interface cloner.
259  */
260 void
261 if_clone_detach(struct if_clone *ifc)
262 {
263 	struct ifc_simple_data *ifcs = ifc->ifc_data;
264 
265 	IF_CLONERS_LOCK();
266 	LIST_REMOVE(ifc, ifc_list);
267 	if_cloners_count--;
268 	IF_CLONERS_UNLOCK();
269 
270 	/* Allow all simples to be destroyed */
271 	if (ifc->ifc_attach == ifc_simple_attach)
272 		ifcs->ifcs_minifs = 0;
273 
274 	/* destroy all interfaces for this cloner */
275 	while (!LIST_EMPTY(&ifc->ifc_iflist))
276 		if_clone_destroyif(ifc, LIST_FIRST(&ifc->ifc_iflist));
277 
278 	IF_CLONE_REMREF(ifc);
279 }
280 
281 static void
282 if_clone_free(struct if_clone *ifc)
283 {
284 	for (int bytoff = 0; bytoff < ifc->ifc_bmlen; bytoff++) {
285 		KASSERT(ifc->ifc_units[bytoff] == 0x00,
286 		    ("ifc_units[%d] is not empty", bytoff));
287 	}
288 
289 	KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
290 	    ("%s: ifc_iflist not empty", __func__));
291 
292 	IF_CLONE_LOCK_DESTROY(ifc);
293 	free(ifc->ifc_units, M_CLONE);
294 }
295 
296 /*
297  * Provide list of interface cloners to userspace.
298  */
299 int
300 if_clone_list(struct if_clonereq *ifcr)
301 {
302 	char *buf, *dst, *outbuf = NULL;
303 	struct if_clone *ifc;
304 	int buf_count, count, err = 0;
305 
306 	if (ifcr->ifcr_count < 0)
307 		return (EINVAL);
308 
309 	IF_CLONERS_LOCK();
310 	/*
311 	 * Set our internal output buffer size.  We could end up not
312 	 * reporting a cloner that is added between the unlock and lock
313 	 * below, but that's not a major problem.  Not caping our
314 	 * allocation to the number of cloners actually in the system
315 	 * could be because that would let arbitrary users cause us to
316 	 * allocate abritrary amounts of kernel memory.
317 	 */
318 	buf_count = (if_cloners_count < ifcr->ifcr_count) ?
319 	    if_cloners_count : ifcr->ifcr_count;
320 	IF_CLONERS_UNLOCK();
321 
322 	outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
323 
324 	IF_CLONERS_LOCK();
325 
326 	ifcr->ifcr_total = if_cloners_count;
327 	if ((dst = ifcr->ifcr_buffer) == NULL) {
328 		/* Just asking how many there are. */
329 		goto done;
330 	}
331 	count = (if_cloners_count < buf_count) ?
332 	    if_cloners_count : buf_count;
333 
334 	for (ifc = LIST_FIRST(&if_cloners), buf = outbuf;
335 	    ifc != NULL && count != 0;
336 	    ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
337 		strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
338 	}
339 
340 done:
341 	IF_CLONERS_UNLOCK();
342 	if (err == 0)
343 		err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
344 	if (outbuf != NULL)
345 		free(outbuf, M_CLONE);
346 	return (err);
347 }
348 
349 /*
350  * A utility function to extract unit numbers from interface names of
351  * the form name###.
352  *
353  * Returns 0 on success and an error on failure.
354  */
355 int
356 ifc_name2unit(const char *name, int *unit)
357 {
358 	const char	*cp;
359 	int		cutoff = INT_MAX / 10;
360 	int		cutlim = INT_MAX % 10;
361 
362 	for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++);
363 	if (*cp == '\0') {
364 		*unit = -1;
365 	} else if (cp[0] == '0' && cp[1] != '\0') {
366 		/* Disallow leading zeroes. */
367 		return (EINVAL);
368 	} else {
369 		for (*unit = 0; *cp != '\0'; cp++) {
370 			if (*cp < '0' || *cp > '9') {
371 				/* Bogus unit number. */
372 				return (EINVAL);
373 			}
374 			if (*unit > cutoff ||
375 			    (*unit == cutoff && *cp - '0' > cutlim))
376 				return (EINVAL);
377 			*unit = (*unit * 10) + (*cp - '0');
378 		}
379 	}
380 
381 	return (0);
382 }
383 
384 int
385 ifc_alloc_unit(struct if_clone *ifc, int *unit)
386 {
387 	int wildcard, bytoff, bitoff;
388 	int err = 0;
389 
390 	IF_CLONE_LOCK(ifc);
391 
392 	bytoff = bitoff = 0;
393 	wildcard = (*unit < 0);
394 	/*
395 	 * Find a free unit if none was given.
396 	 */
397 	if (wildcard) {
398 		while ((bytoff < ifc->ifc_bmlen)
399 		    && (ifc->ifc_units[bytoff] == 0xff))
400 			bytoff++;
401 		if (bytoff >= ifc->ifc_bmlen) {
402 			err = ENOSPC;
403 			goto done;
404 		}
405 		while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
406 			bitoff++;
407 		*unit = (bytoff << 3) + bitoff;
408 	}
409 
410 	if (*unit > ifc->ifc_maxunit) {
411 		err = ENOSPC;
412 		goto done;
413 	}
414 
415 	if (!wildcard) {
416 		bytoff = *unit >> 3;
417 		bitoff = *unit - (bytoff << 3);
418 	}
419 
420 	if((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) {
421 		err = EEXIST;
422 		goto done;
423 	}
424 	/*
425 	 * Allocate the unit in the bitmap.
426 	 */
427 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
428 	    ("%s: bit is already set", __func__));
429 	ifc->ifc_units[bytoff] |= (1 << bitoff);
430 	IF_CLONE_ADDREF_LOCKED(ifc);
431 
432 done:
433 	IF_CLONE_UNLOCK(ifc);
434 	return (err);
435 }
436 
437 void
438 ifc_free_unit(struct if_clone *ifc, int unit)
439 {
440 	int bytoff, bitoff;
441 
442 
443 	/*
444 	 * Compute offset in the bitmap and deallocate the unit.
445 	 */
446 	bytoff = unit >> 3;
447 	bitoff = unit - (bytoff << 3);
448 
449 	IF_CLONE_LOCK(ifc);
450 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
451 	    ("%s: bit is already cleared", __func__));
452 	ifc->ifc_units[bytoff] &= ~(1 << bitoff);
453 	IF_CLONE_REMREF_LOCKED(ifc);	/* releases lock */
454 }
455 
456 void
457 ifc_simple_attach(struct if_clone *ifc)
458 {
459 	int err;
460 	int unit;
461 	char name[IFNAMSIZ];
462 	struct ifc_simple_data *ifcs = ifc->ifc_data;
463 
464 	KASSERT(ifcs->ifcs_minifs - 1 <= ifc->ifc_maxunit,
465 	    ("%s: %s requested more units than allowed (%d > %d)",
466 	    __func__, ifc->ifc_name, ifcs->ifcs_minifs,
467 	    ifc->ifc_maxunit + 1));
468 
469 	for (unit = 0; unit < ifcs->ifcs_minifs; unit++) {
470 		snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
471 		err = if_clone_createif(ifc, name, IFNAMSIZ);
472 		KASSERT(err == 0,
473 		    ("%s: failed to create required interface %s",
474 		    __func__, name));
475 	}
476 }
477 
478 int
479 ifc_simple_match(struct if_clone *ifc, const char *name)
480 {
481 	const char *cp;
482 	int i;
483 
484 	/* Match the name */
485 	for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
486 		if (ifc->ifc_name[i] != *cp)
487 			return (0);
488 	}
489 
490 	/* Make sure there's a unit number or nothing after the name */
491 	for (; *cp != '\0'; cp++) {
492 		if (*cp < '0' || *cp > '9')
493 			return (0);
494 	}
495 
496 	return (1);
497 }
498 
499 int
500 ifc_simple_create(struct if_clone *ifc, char *name, size_t len)
501 {
502 	char *dp;
503 	int wildcard;
504 	int unit;
505 	int err;
506 	struct ifc_simple_data *ifcs = ifc->ifc_data;
507 
508 	err = ifc_name2unit(name, &unit);
509 	if (err != 0)
510 		return (err);
511 
512 	wildcard = (unit < 0);
513 
514 	err = ifc_alloc_unit(ifc, &unit);
515 	if (err != 0)
516 		return (err);
517 
518 	err = ifcs->ifcs_create(ifc, unit);
519 	if (err != 0) {
520 		ifc_free_unit(ifc, unit);
521 		return (err);
522 	}
523 
524 	/* In the wildcard case, we need to update the name. */
525 	if (wildcard) {
526 		for (dp = name; *dp != '\0'; dp++);
527 		if (snprintf(dp, len - (dp-name), "%d", unit) >
528 		    len - (dp-name) - 1) {
529 			/*
530 			 * This can only be a programmer error and
531 			 * there's no straightforward way to recover if
532 			 * it happens.
533 			 */
534 			panic("if_clone_create(): interface name too long");
535 		}
536 
537 	}
538 
539 	return (0);
540 }
541 
542 int
543 ifc_simple_destroy(struct if_clone *ifc, struct ifnet *ifp)
544 {
545 	int unit;
546 	struct ifc_simple_data *ifcs = ifc->ifc_data;
547 
548 	unit = ifp->if_dunit;
549 
550 	if (unit < ifcs->ifcs_minifs)
551 		return (EINVAL);
552 
553 	ifcs->ifcs_destroy(ifp);
554 
555 	ifc_free_unit(ifc, unit);
556 
557 	return (0);
558 }
559