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