Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/net/if_clone.c
103379 views
1
/*-
2
* SPDX-License-Identifier: BSD-3-Clause
3
*
4
* Copyright (c) 2012 Gleb Smirnoff <[email protected]>
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
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
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
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
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
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
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
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
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
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
307
if_clone_addif(struct if_clone *ifc, struct ifnet *ifp)
308
{
309
ifc_link_ifp(ifc, ifp);
310
}
311
312
bool
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 *
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 *
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 *
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
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
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
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
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 *
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
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 *
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
if (req2->dump_nl_f != NULL)
539
ifc->dump_nl = req2->dump_nl_f;
540
else
541
ifc->dump_nl = ifc_dump_ifp_nl_default;
542
}
543
544
if (if_clone_attach(ifc) != 0)
545
return (NULL);
546
547
EVENTHANDLER_INVOKE(if_clone_event, ifc);
548
549
return (ifc);
550
}
551
552
void
553
ifc_detach_cloner(struct if_clone *ifc)
554
{
555
if_clone_detach(ifc);
556
}
557
558
559
#ifdef CLONE_COMPAT_13
560
561
static int
562
ifc_advanced_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
563
struct ifc_data *ifc_data, struct ifnet **ifpp)
564
{
565
int error = ifc->ifca_create(ifc, name, maxlen, ifc_data->params);
566
567
if (error == 0)
568
*ifpp = ifunit(name);
569
return (error);
570
}
571
572
static int
573
ifc_advanced_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
574
{
575
if (ifc->ifca_destroy == NULL)
576
return (ENOTSUP);
577
return (ifc->ifca_destroy(ifc, ifp));
578
}
579
580
struct if_clone *
581
if_clone_advanced(const char *name, u_int maxunit, ifc_match_t match,
582
ifc_create_t create, ifc_destroy_t destroy)
583
{
584
struct if_clone *ifc;
585
586
ifc = if_clone_alloc(name, maxunit ? maxunit : IF_MAXUNIT);
587
ifc->ifc_match = match;
588
ifc->ifc_create = ifc_advanced_create_wrapper;
589
ifc->ifc_destroy = ifc_advanced_destroy_wrapper;
590
ifc->ifca_destroy = destroy;
591
ifc->ifca_create = create;
592
593
if (if_clone_attach(ifc) != 0)
594
return (NULL);
595
596
EVENTHANDLER_INVOKE(if_clone_event, ifc);
597
598
return (ifc);
599
}
600
601
static int
602
ifc_simple_create_wrapper(struct if_clone *ifc, char *name, size_t maxlen,
603
struct ifc_data *ifc_data, struct ifnet **ifpp)
604
{
605
int unit = 0;
606
607
ifc_name2unit(name, &unit);
608
int error = ifc->ifcs_create(ifc, unit, ifc_data->params);
609
if (error == 0)
610
*ifpp = ifunit(name);
611
return (error);
612
}
613
614
static int
615
ifc_simple_destroy_wrapper(struct if_clone *ifc, struct ifnet *ifp, uint32_t flags)
616
{
617
if (ifp->if_dunit < ifc->ifcs_minifs && (flags & IFC_F_FORCE) == 0)
618
return (EINVAL);
619
620
ifc->ifcs_destroy(ifp);
621
return (0);
622
}
623
624
struct if_clone *
625
if_clone_simple(const char *name, ifcs_create_t create, ifcs_destroy_t destroy,
626
u_int minifs)
627
{
628
struct if_clone *ifc;
629
u_int unit;
630
631
ifc = if_clone_alloc(name, IF_MAXUNIT);
632
ifc->ifc_match = ifc_simple_match;
633
ifc->ifc_create = ifc_simple_create_wrapper;
634
ifc->ifc_destroy = ifc_simple_destroy_wrapper;
635
ifc->ifcs_create = create;
636
ifc->ifcs_destroy = destroy;
637
ifc->ifcs_minifs = minifs;
638
ifc->ifc_flags = IFC_F_AUTOUNIT;
639
640
if (if_clone_attach(ifc) != 0)
641
return (NULL);
642
643
for (unit = 0; unit < minifs; unit++) {
644
char name[IFNAMSIZ];
645
int error __unused;
646
struct ifc_data_nl ifd = {};
647
648
snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
649
error = if_clone_createif_nl(ifc, name, &ifd);
650
KASSERT(error == 0,
651
("%s: failed to create required interface %s",
652
__func__, name));
653
}
654
655
EVENTHANDLER_INVOKE(if_clone_event, ifc);
656
657
return (ifc);
658
}
659
#endif
660
661
/*
662
* Unregister a network interface cloner.
663
*/
664
void
665
if_clone_detach(struct if_clone *ifc)
666
{
667
668
IF_CLONERS_LOCK();
669
LIST_REMOVE(ifc, ifc_list);
670
V_if_cloners_count--;
671
IF_CLONERS_UNLOCK();
672
673
/* destroy all interfaces for this cloner */
674
while (!LIST_EMPTY(&ifc->ifc_iflist))
675
if_clone_destroyif_flags(ifc, LIST_FIRST(&ifc->ifc_iflist), IFC_F_FORCE);
676
677
IF_CLONE_REMREF(ifc);
678
}
679
680
static void
681
if_clone_free(struct if_clone *ifc)
682
{
683
684
KASSERT(LIST_EMPTY(&ifc->ifc_iflist),
685
("%s: ifc_iflist not empty", __func__));
686
687
IF_CLONE_LOCK_DESTROY(ifc);
688
delete_unrhdr(ifc->ifc_unrhdr);
689
free(ifc, M_CLONE);
690
}
691
692
/*
693
* Provide list of interface cloners to userspace.
694
*/
695
int
696
if_clone_list(struct if_clonereq *ifcr)
697
{
698
char *buf, *dst, *outbuf = NULL;
699
struct if_clone *ifc;
700
int buf_count, count, err = 0;
701
702
if (ifcr->ifcr_count < 0)
703
return (EINVAL);
704
705
IF_CLONERS_LOCK();
706
/*
707
* Set our internal output buffer size. We could end up not
708
* reporting a cloner that is added between the unlock and lock
709
* below, but that's not a major problem. Not caping our
710
* allocation to the number of cloners actually in the system
711
* could be because that would let arbitrary users cause us to
712
* allocate arbitrary amounts of kernel memory.
713
*/
714
buf_count = (V_if_cloners_count < ifcr->ifcr_count) ?
715
V_if_cloners_count : ifcr->ifcr_count;
716
IF_CLONERS_UNLOCK();
717
718
outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
719
720
IF_CLONERS_LOCK();
721
722
ifcr->ifcr_total = V_if_cloners_count;
723
if ((dst = ifcr->ifcr_buffer) == NULL) {
724
/* Just asking how many there are. */
725
goto done;
726
}
727
count = (V_if_cloners_count < buf_count) ?
728
V_if_cloners_count : buf_count;
729
730
for (ifc = LIST_FIRST(&V_if_cloners), buf = outbuf;
731
ifc != NULL && count != 0;
732
ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
733
strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
734
}
735
736
done:
737
IF_CLONERS_UNLOCK();
738
if (err == 0 && dst != NULL)
739
err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
740
if (outbuf != NULL)
741
free(outbuf, M_CLONE);
742
return (err);
743
}
744
745
#ifdef VIMAGE
746
/*
747
* if_clone_restoregroup() is used in context of if_vmove().
748
*
749
* Since if_detach_internal() has removed the interface from ALL groups, we
750
* need to "restore" interface membership in the cloner's group. Note that
751
* interface belongs to cloner in its home vnet, so we first find the original
752
* cloner, and then we confirm that cloner with the same name exists in the
753
* current vnet.
754
*/
755
void
756
if_clone_restoregroup(struct ifnet *ifp)
757
{
758
struct if_clone *ifc;
759
struct ifnet *ifcifp;
760
char ifc_name[IFCLOSIZ] = { [0] = '\0' };
761
762
CURVNET_SET_QUIET(ifp->if_home_vnet);
763
IF_CLONERS_LOCK();
764
LIST_FOREACH(ifc, &V_if_cloners, ifc_list) {
765
IF_CLONE_LOCK(ifc);
766
LIST_FOREACH(ifcifp, &ifc->ifc_iflist, if_clones) {
767
if (ifp == ifcifp) {
768
strncpy(ifc_name, ifc->ifc_name, IFCLOSIZ-1);
769
break;
770
}
771
}
772
IF_CLONE_UNLOCK(ifc);
773
if (ifc_name[0] != '\0')
774
break;
775
}
776
CURVNET_RESTORE();
777
LIST_FOREACH(ifc, &V_if_cloners, ifc_list)
778
if (strcmp(ifc->ifc_name, ifc_name) == 0)
779
break;
780
IF_CLONERS_UNLOCK();
781
782
if (ifc != NULL)
783
if_addgroup(ifp, ifc_name);
784
}
785
#endif
786
787
/*
788
* A utility function to extract unit numbers from interface names of
789
* the form name###.
790
*
791
* Returns 0 on success and an error on failure.
792
*/
793
int
794
ifc_name2unit(const char *name, int *unit)
795
{
796
const char *cp;
797
int cutoff = INT_MAX / 10;
798
int cutlim = INT_MAX % 10;
799
800
for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++)
801
;
802
if (*cp == '\0') {
803
*unit = -1;
804
} else if (cp[0] == '0' && cp[1] != '\0') {
805
/* Disallow leading zeroes. */
806
return (EINVAL);
807
} else {
808
for (*unit = 0; *cp != '\0'; cp++) {
809
if (*cp < '0' || *cp > '9') {
810
/* Bogus unit number. */
811
return (EINVAL);
812
}
813
if (*unit > cutoff ||
814
(*unit == cutoff && *cp - '0' > cutlim))
815
return (EINVAL);
816
*unit = (*unit * 10) + (*cp - '0');
817
}
818
}
819
820
return (0);
821
}
822
823
static int
824
ifc_alloc_unit_specific(struct if_clone *ifc, int *unit)
825
{
826
char name[IFNAMSIZ];
827
828
if (*unit > ifc->ifc_maxunit)
829
return (ENOSPC);
830
831
if (alloc_unr_specific(ifc->ifc_unrhdr, *unit) == -1)
832
return (EEXIST);
833
834
snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, *unit);
835
if (ifunit(name) != NULL) {
836
free_unr(ifc->ifc_unrhdr, *unit);
837
return (EEXIST);
838
}
839
840
IF_CLONE_ADDREF(ifc);
841
842
return (0);
843
}
844
845
static int
846
ifc_alloc_unit_next(struct if_clone *ifc, int *unit)
847
{
848
int error;
849
850
*unit = alloc_unr(ifc->ifc_unrhdr);
851
if (*unit == -1)
852
return (ENOSPC);
853
854
free_unr(ifc->ifc_unrhdr, *unit);
855
for (;;) {
856
error = ifc_alloc_unit_specific(ifc, unit);
857
if (error != EEXIST)
858
break;
859
860
(*unit)++;
861
}
862
863
return (error);
864
}
865
866
int
867
ifc_alloc_unit(struct if_clone *ifc, int *unit)
868
{
869
if (*unit < 0)
870
return (ifc_alloc_unit_next(ifc, unit));
871
else
872
return (ifc_alloc_unit_specific(ifc, unit));
873
}
874
875
void
876
ifc_free_unit(struct if_clone *ifc, int unit)
877
{
878
879
free_unr(ifc->ifc_unrhdr, unit);
880
IF_CLONE_REMREF(ifc);
881
}
882
883
static int
884
ifc_simple_match(struct if_clone *ifc, const char *name)
885
{
886
const char *cp;
887
int i;
888
889
/* Match the name */
890
for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
891
if (ifc->ifc_name[i] != *cp)
892
return (0);
893
}
894
895
/* Make sure there's a unit number or nothing after the name */
896
for (; *cp != '\0'; cp++) {
897
if (*cp < '0' || *cp > '9')
898
return (0);
899
}
900
901
return (1);
902
}
903
904
static int
905
ifc_handle_unit(struct if_clone *ifc, char *name, size_t len, int *punit)
906
{
907
char *dp;
908
int wildcard;
909
int unit;
910
int err;
911
912
err = ifc_name2unit(name, &unit);
913
if (err != 0)
914
return (err);
915
916
wildcard = (unit < 0);
917
918
err = ifc_alloc_unit(ifc, &unit);
919
if (err != 0)
920
return (err);
921
922
/* In the wildcard case, we need to update the name. */
923
if (wildcard) {
924
for (dp = name; *dp != '\0'; dp++);
925
if (snprintf(dp, len - (dp-name), "%d", unit) >
926
len - (dp-name) - 1) {
927
/*
928
* This can only be a programmer error and
929
* there's no straightforward way to recover if
930
* it happens.
931
*/
932
panic("if_clone_create(): interface name too long");
933
}
934
}
935
*punit = unit;
936
937
return (0);
938
}
939
940
int
941
ifc_copyin(const struct ifc_data *ifd, void *target, size_t len)
942
{
943
if (ifd->params == NULL)
944
return (EINVAL);
945
946
if (ifd->flags & IFC_F_SYSSPACE) {
947
memcpy(target, ifd->params, len);
948
return (0);
949
} else
950
return (copyin(ifd->params, target, len));
951
}
952
953