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