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