]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_clone.c
pf: improve SCTP state validation
[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  */
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
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
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 #ifdef VIMAGE
201         if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
202                 CURVNET_SET_QUIET(vnet0);
203                 LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
204                         if (ifc->ifc_match(ifc, name))
205                                 break;
206                 CURVNET_RESTORE();
207         }
208 #endif
209         IF_CLONERS_UNLOCK();
210
211         if (ifc == NULL)
212                 return (EINVAL);
213
214         strlcpy(ifname, name, IFNAMSIZ);
215         error = if_clone_createif(ifc, ifname, IFNAMSIZ, ifd, &ifp);
216         if (ifpp != NULL)
217                 *ifpp = ifp;
218
219         return (error);
220 }
221
222 int
223 if_clone_create(char *name, size_t len, caddr_t params)
224 {
225         struct ifc_data ifd = { .params = params };
226         struct ifnet *ifp;
227
228         int error = ifc_create_ifp(name, &ifd, &ifp);
229
230         if (error == 0)
231                 strlcpy(name, if_name(ifp), len);
232
233         return (error);
234 }
235
236 void
237 if_clone_addif(struct if_clone *ifc, struct ifnet *ifp)
238 {
239
240         if ((ifc->ifc_flags & IFC_NOGROUP) == 0)
241                 if_addgroup(ifp, ifc->ifc_name);
242
243         IF_CLONE_LOCK(ifc);
244         IFC_IFLIST_INSERT(ifc, ifp);
245         IF_CLONE_UNLOCK(ifc);
246 }
247
248 /*
249  * Create a clone network interface.
250  */
251 static int
252 if_clone_createif(struct if_clone *ifc, char *name, size_t len,
253     struct ifc_data *ifd, struct ifnet **ifpp)
254 {
255         int err, unit = 0;
256
257         if (ifunit(name) != NULL)
258                 return (EEXIST);
259
260         if (ifc->ifc_flags & IFC_F_AUTOUNIT) {
261                 if ((err = ifc_handle_unit(ifc, name, len, &unit)) != 0)
262                         return (err);
263                 ifd->unit = unit;
264         }
265         *ifpp = NULL;
266         err = (*ifc->ifc_create)(ifc, name, len, ifd, ifpp);
267
268         if (err == 0) {
269                 MPASS(*ifpp != NULL);
270                 if_clone_addif(ifc, *ifpp);
271         } else if (ifc->ifc_flags & IFC_F_AUTOUNIT)
272                 ifc_free_unit(ifc, unit);
273
274         return (err);
275 }
276
277 /*
278  * Lookup and destroy a clone network interface.
279  */
280 int
281 if_clone_destroy(const char *name)
282 {
283         int err;
284         struct if_clone *ifc;
285         struct ifnet *ifp;
286
287         ifp = ifunit_ref(name);
288         if (ifp == NULL)
289                 return (ENXIO);
290
291         /* Find the cloner for this interface */
292         IF_CLONERS_LOCK();
293         LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
294                 if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) {
295                         break;
296                 }
297         }
298 #ifdef VIMAGE
299         if (ifc == NULL && !IS_DEFAULT_VNET(curvnet)) {
300                 CURVNET_SET_QUIET(vnet0);
301                 LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
302                         if (ifc->ifc_match(ifc, name))
303                                 break;
304                 CURVNET_RESTORE();
305         }
306 #endif
307         IF_CLONERS_UNLOCK();
308         if (ifc == NULL) {
309                 if_rele(ifp);
310                 return (EINVAL);
311         }
312
313         err = if_clone_destroyif(ifc, ifp);
314         if_rele(ifp);
315         return err;
316 }
317
318 /*
319  * Destroy a clone network interface.
320  */
321 static int
322 if_clone_destroyif_flags(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
323 {
324         int err;
325         struct ifnet *ifcifp;
326
327         /*
328          * Given that the cloned ifnet might be attached to a different
329          * vnet from where its cloner was registered, we have to
330          * switch to the vnet context of the target vnet.
331          */
332         CURVNET_SET_QUIET(ifp->if_vnet);
333
334         IF_CLONE_LOCK(ifc);
335         LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
336                 if (ifcifp == ifp) {
337                         IFC_IFLIST_REMOVE(ifc, ifp);
338                         break;
339                 }
340         }
341         IF_CLONE_UNLOCK(ifc);
342         if (ifcifp == NULL) {
343                 CURVNET_RESTORE();
344                 return (ENXIO);         /* ifp is not on the list. */
345         }
346         if ((ifc->ifc_flags & IFC_F_NOGROUP) == 0)
347                 if_delgroup(ifp, ifc->ifc_name);
348
349         int unit = ifp->if_dunit;
350         err = (*ifc->ifc_destroy)(ifc, ifp, flags);
351
352         if (err != 0) {
353                 if ((ifc->ifc_flags & IFC_F_NOGROUP) == 0)
354                         if_addgroup(ifp, ifc->ifc_name);
355
356                 IF_CLONE_LOCK(ifc);
357                 IFC_IFLIST_INSERT(ifc, ifp);
358                 IF_CLONE_UNLOCK(ifc);
359         } else if (ifc->ifc_flags & IFC_F_AUTOUNIT)
360                 ifc_free_unit(ifc, unit);
361         CURVNET_RESTORE();
362         return (err);
363 }
364
365 int
366 if_clone_destroyif(struct if_clone *ifc, struct ifnet *ifp)
367 {
368         return (if_clone_destroyif_flags(ifc, ifp, 0));
369 }
370
371 static struct if_clone *
372 if_clone_alloc(const char *name, int maxunit)
373 {
374         struct if_clone *ifc;
375
376         KASSERT(name != NULL, ("%s: no name\n", __func__));
377
378         ifc = malloc(sizeof(struct if_clone), M_CLONE, M_WAITOK | M_ZERO);
379         strncpy(ifc->ifc_name, name, IFCLOSIZ-1);
380         IF_CLONE_LOCK_INIT(ifc);
381         IF_CLONE_ADDREF(ifc);
382         ifc->ifc_maxunit = maxunit ? maxunit : IF_MAXUNIT;
383         ifc->ifc_unrhdr = new_unrhdr(0, ifc->ifc_maxunit, &ifc->ifc_mtx);
384         LIST_INIT(&ifc->ifc_iflist);
385
386         return (ifc);
387 }
388
389 static int
390 if_clone_attach(struct if_clone *ifc)
391 {
392         struct if_clone *ifc1;
393
394         IF_CLONERS_LOCK();
395         LIST_FOREACH(ifc1, &V_if_cloners, ifc_list)
396                 if (strcmp(ifc->ifc_name, ifc1->ifc_name) == 0) {
397                         IF_CLONERS_UNLOCK();
398                         IF_CLONE_REMREF(ifc);
399                         return (EEXIST);
400                 }
401         LIST_INSERT_HEAD(&V_if_cloners, ifc, ifc_list);
402         V_if_cloners_count++;
403         IF_CLONERS_UNLOCK();
404
405         return (0);
406 }
407
408 struct if_clone *
409 ifc_attach_cloner(const char *name, struct if_clone_addreq *req)
410 {
411         if (req->create_f == NULL || req->destroy_f == NULL)
412                 return (NULL);
413         if (strnlen(name, IFCLOSIZ) >= (IFCLOSIZ - 1))
414                 return (NULL);
415
416         struct if_clone *ifc = if_clone_alloc(name, req->maxunit);
417         ifc->ifc_match = req->match_f != NULL ? req->match_f : ifc_simple_match;
418         ifc->ifc_create = req->create_f;
419         ifc->ifc_destroy = req->destroy_f;
420         ifc->ifc_flags = (req->flags & (IFC_F_AUTOUNIT | IFC_F_NOGROUP));
421
422         if (if_clone_attach(ifc) != 0)
423                 return (NULL);
424
425         EVENTHANDLER_INVOKE(if_clone_event, ifc);
426
427         return (ifc);
428 }
429
430 void
431 ifc_detach_cloner(struct if_clone *ifc)
432 {
433         if_clone_detach(ifc);
434 }
435
436
437 #ifdef CLONE_COMPAT_13
438
439 static int
440 ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
441     struct ifc_data *ifc_data, struct ifnet **ifpp)
442 {
443         int error = ifc->ifca_create(ifc, name, maxlen, ifc_data->params);
444
445         if (error == 0)
446                 *ifpp = ifunit(name);
447         return (error);
448 }
449
450 static int
451 ifc_advanced_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
452 {
453         if (ifc->ifca_destroy == NULL)
454                 return (ENOTSUP);
455         return (ifc->ifca_destroy(ifc, ifp));
456 }
457
458 struct if_clone *
459 if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
460         ifc_create_t create, ifc_destroy_t destroy)
461 {
462         struct if_clone *ifc;
463
464         ifc = if_clone_alloc(name, maxunit);
465         ifc->ifc_match = match;
466         ifc->ifc_create = ifc_advanced_create_wrapper;
467         ifc->ifc_destroy = ifc_advanced_destroy_wrapper;
468         ifc->ifca_destroy = destroy;
469         ifc->ifca_create = create;
470
471         if (if_clone_attach(ifc) != 0)
472                 return (NULL);
473
474         EVENTHANDLER_INVOKE(if_clone_event, ifc);
475
476         return (ifc);
477 }
478
479 static int
480 ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
481     struct ifc_data *ifc_data, struct ifnet **ifpp)
482 {
483         int unit = 0;
484
485         ifc_name2unit(name, &unit);
486         int error = ifc->ifcs_create(ifc, unit, ifc_data->params);
487         if (error == 0)
488                 *ifpp = ifunit(name);
489         return (error);
490 }
491
492 static int
493 ifc_simple_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
494 {
495         if (ifp->if_dunit < ifc->ifcs_minifs && (flags & IFC_F_FORCE) == 0)
496                 return (EINVAL);
497
498         ifc->ifcs_destroy(ifp);
499         return (0);
500 }
501
502 struct if_clone *
503 if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
504         u_int minifs)
505 {
506         struct if_clone *ifc;
507         u_int unit;
508
509         ifc = if_clone_alloc(name, 0);
510         ifc->ifc_match = ifc_simple_match;
511         ifc->ifc_create = ifc_simple_create_wrapper;
512         ifc->ifc_destroy = ifc_simple_destroy_wrapper;
513         ifc->ifcs_create = create;
514         ifc->ifcs_destroy = destroy;
515         ifc->ifcs_minifs = minifs;
516         ifc->ifc_flags = IFC_F_AUTOUNIT;
517
518         if (if_clone_attach(ifc) != 0)
519                 return (NULL);
520
521         for (unit = 0; unit < minifs; unit++) {
522                 char name[IFNAMSIZ];
523                 int error __unused;
524                 struct ifc_data ifd = {};
525                 struct ifnet *ifp;
526
527                 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
528                 error = if_clone_createif(ifc, name, IFNAMSIZ, &ifd, &ifp);
529                 KASSERT(error == 0,
530                     ("%s: failed to create required interface %s",
531                     __func__, name));
532         }
533
534         EVENTHANDLER_INVOKE(if_clone_event, ifc);
535
536         return (ifc);
537 }
538 #endif
539
540 /*
541  * Unregister a network interface cloner.
542  */
543 void
544 if_clone_detach(struct if_clone *ifc)
545 {
546
547         IF_CLONERS_LOCK();
548         LIST_REMOVE(ifc, ifc_list);
549         V_if_cloners_count--;
550         IF_CLONERS_UNLOCK();
551
552         /* destroy all interfaces for this cloner */
553         while (!LIST_EMPTY(&ifc->ifc_iflist))
554                 if_clone_destroyif_flags(ifc, LIST_FIRST(&ifc->ifc_iflist), IFC_F_FORCE);
555
556         IF_CLONE_REMREF(ifc);
557 }
558
559 static void
560 if_clone_free(struct if_clone *ifc)
561 {
562
563         KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
564             ("%s: ifc_iflist not empty", __func__));
565
566         IF_CLONE_LOCK_DESTROY(ifc);
567         delete_unrhdr(ifc->ifc_unrhdr);
568         free(ifc, M_CLONE);
569 }
570
571 /*
572  * Provide list of interface cloners to userspace.
573  */
574 int
575 if_clone_list(struct if_clonereq *ifcr)
576 {
577         char *buf, *dst, *outbuf = NULL;
578         struct if_clone *ifc;
579         int buf_count, count, err = 0;
580
581         if (ifcr->ifcr_count < 0)
582                 return (EINVAL);
583
584         IF_CLONERS_LOCK();
585         /*
586          * Set our internal output buffer size.  We could end up not
587          * reporting a cloner that is added between the unlock and lock
588          * below, but that's not a major problem.  Not caping our
589          * allocation to the number of cloners actually in the system
590          * could be because that would let arbitrary users cause us to
591          * allocate arbitrary amounts of kernel memory.
592          */
593         buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
594             V_if_cloners_count : ifcr->ifcr_count;
595         IF_CLONERS_UNLOCK();
596
597         outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
598
599         IF_CLONERS_LOCK();
600
601         ifcr->ifcr_total = V_if_cloners_count;
602         if ((dst = ifcr->ifcr_buffer) == NULL) {
603                 /* Just asking how many there are. */
604                 goto done;
605         }
606         count = (V_if_cloners_count < buf_count) ?
607             V_if_cloners_count : buf_count;
608
609         for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
610             ifc != NULL && count != 0;
611             ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
612                 strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
613         }
614
615 done:
616         IF_CLONERS_UNLOCK();
617         if (err == 0 && dst != NULL)
618                 err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
619         if (outbuf != NULL)
620                 free(outbuf, M_CLONE);
621         return (err);
622 }
623
624 /*
625  * if_clone_findifc() looks up ifnet from the current
626  * cloner list, and returns ifc if found.  Note that ifc_refcnt
627  * is incremented.
628  */
629 struct if_clone *
630 if_clone_findifc(struct ifnet *ifp)
631 {
632         struct if_clone *ifc, *ifc0;
633         struct ifnet *ifcifp;
634
635         ifc0 = NULL;
636         IF_CLONERS_LOCK();
637         LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
638                 IF_CLONE_LOCK(ifc);
639                 LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
640                         if (ifp == ifcifp) {
641                                 ifc0 = ifc;
642                                 IF_CLONE_ADDREF_LOCKED(ifc);
643                                 break;
644                         }
645                 }
646                 IF_CLONE_UNLOCK(ifc);
647                 if (ifc0 != NULL)
648                         break;
649         }
650         IF_CLONERS_UNLOCK();
651
652         return (ifc0);
653 }
654
655 /*
656  * if_clone_addgroup() decrements ifc_refcnt because it is called after
657  * if_clone_findifc().
658  */
659 void
660 if_clone_addgroup(struct ifnet *ifp, struct if_clone *ifc)
661 {
662         if ((ifc->ifc_flags & IFC_NOGROUP) == 0) {
663                 if_addgroup(ifp, ifc->ifc_name);
664                 IF_CLONE_REMREF(ifc);
665         }
666 }
667
668 /*
669  * A utility function to extract unit numbers from interface names of
670  * the form name###.
671  *
672  * Returns 0 on success and an error on failure.
673  */
674 int
675 ifc_name2unit(const char *name, int *unit)
676 {
677         const char      *cp;
678         int             cutoff = INT_MAX / 10;
679         int             cutlim = INT_MAX % 10;
680
681         for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++)
682                 ;
683         if (*cp == '\0') {
684                 *unit = -1;
685         } else if (cp[0] == '0' && cp[1] != '\0') {
686                 /* Disallow leading zeroes. */
687                 return (EINVAL);
688         } else {
689                 for (*unit = 0; *cp != '\0'; cp++) {
690                         if (*cp < '0' || *cp > '9') {
691                                 /* Bogus unit number. */
692                                 return (EINVAL);
693                         }
694                         if (*unit > cutoff ||
695                             (*unit == cutoff && *cp - '0' > cutlim))
696                                 return (EINVAL);
697                         *unit = (*unit * 10) + (*cp - '0');
698                 }
699         }
700
701         return (0);
702 }
703
704 static int
705 ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
706 {
707         char name[IFNAMSIZ];
708
709         if (*unit > ifc->ifc_maxunit)
710                 return (ENOSPC);
711
712         if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
713                 return (EEXIST);
714
715         snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
716         if (ifunit(name) != NULL) {
717                 free_unr(ifc->ifc_unrhdr, *unit);
718                 return (EEXIST);
719         }
720
721         IF_CLONE_ADDREF(ifc);
722
723         return (0);
724 }
725
726 static int
727 ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
728 {
729         int error;
730
731         *unit = alloc_unr(ifc->ifc_unrhdr);
732         if (*unit == -1)
733                 return (ENOSPC);
734
735         free_unr(ifc->ifc_unrhdr, *unit);
736         for (;;) {
737                 error = ifc_alloc_unit_specific(ifc, unit);
738                 if (error != EEXIST)
739                         break;
740
741                 (*unit)++;
742         }
743
744         return (error);
745 }
746
747 int
748 ifc_alloc_unit(struct if_clone *ifc, int *unit)
749 {
750         if (*unit < 0)
751                 return (ifc_alloc_unit_next(ifc, unit));
752         else
753                 return (ifc_alloc_unit_specific(ifc, unit));
754 }
755
756 void
757 ifc_free_unit(struct if_clone *ifc, int unit)
758 {
759
760         free_unr(ifc->ifc_unrhdr, unit);
761         IF_CLONE_REMREF(ifc);
762 }
763
764 static int
765 ifc_simple_match(struct if_clone *ifc, const char *name)
766 {
767         const char *cp;
768         int i;
769
770         /* Match the name */
771         for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
772                 if (ifc->ifc_name[i] != *cp)
773                         return (0);
774         }
775
776         /* Make sure there's a unit number or nothing after the name */
777         for (; *cp != '\0'; cp++) {
778                 if (*cp < '0' || *cp > '9')
779                         return (0);
780         }
781
782         return (1);
783 }
784
785 static int
786 ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
787 {
788         char *dp;
789         int wildcard;
790         int unit;
791         int err;
792
793         err = ifc_name2unit(name, &unit);
794         if (err != 0)
795                 return (err);
796
797         wildcard = (unit < 0);
798
799         err = ifc_alloc_unit(ifc, &unit);
800         if (err != 0)
801                 return (err);
802
803         /* In the wildcard case, we need to update the name. */
804         if (wildcard) {
805                 for (dp = name; *dp != '\0'; dp++);
806                 if (snprintf(dp, len - (dp-name), "%d", unit) >
807                     len - (dp-name) - 1) {
808                         /*
809                          * This can only be a programmer error and
810                          * there's no straightforward way to recover if
811                          * it happens.
812                          */
813                         panic("if_clone_create(): interface name too long");
814                 }
815         }
816         *punit = unit;
817
818         return (0);
819 }
820
821 int
822 ifc_copyin(const struct ifc_data *ifd, void *target, size_t len)
823 {
824         if (ifd->params == NULL)
825                 return (EINVAL);
826
827         if (ifd->flags & IFC_F_SYSSPACE) {
828                 memcpy(target, ifd->params, len);
829                 return (0);
830         } else
831                 return (copyin(ifd->params, target, len));
832 }
833
834 const char *
835 ifc_name(struct if_clone *ifc)
836 {
837         return (ifc->ifc_name);
838 }
839
840 void
841 ifc_flags_set(struct if_clone *ifc, int flags)
842 {
843         ifc->ifc_flags = flags;
844 }
845
846 int
847 ifc_flags_get(struct if_clone *ifc)
848 {
849         return (ifc->ifc_flags);
850 }