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