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