1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
/* $OpenBSD: if_etherbridge.c,v 1.7 2021/07/05 04:17:41 dlg Exp $ */
/*
* Copyright (c) 2018, 2021 David Gwynne <dlg@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "bpfilter.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/timeout.h>
#include <sys/pool.h>
#include <sys/tree.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <net/rtable.h>
#include <net/toeplitz.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
/* for bridge stuff */
#include <net/if_bridge.h>
#include <net/if_etherbridge.h>
static inline void ebe_rele(struct eb_entry *);
static void ebe_free(void *);
static void etherbridge_age(void *);
RBT_PROTOTYPE(eb_tree, eb_entry, ebe_tentry, ebt_cmp);
static struct pool eb_entry_pool;
static inline int
eb_port_eq(struct etherbridge *eb, void *a, void *b)
{
return ((*eb->eb_ops->eb_op_port_eq)(eb->eb_cookie, a, b));
}
static inline void *
eb_port_take(struct etherbridge *eb, void *port)
{
return ((*eb->eb_ops->eb_op_port_take)(eb->eb_cookie, port));
}
static inline void
eb_port_rele(struct etherbridge *eb, void *port)
{
return ((*eb->eb_ops->eb_op_port_rele)(eb->eb_cookie, port));
}
static inline size_t
eb_port_ifname(struct etherbridge *eb, char *dst, size_t len, void *port)
{
return ((*eb->eb_ops->eb_op_port_ifname)(eb->eb_cookie, dst, len,
port));
}
static inline void
eb_port_sa(struct etherbridge *eb, struct sockaddr_storage *ss, void *port)
{
(*eb->eb_ops->eb_op_port_sa)(eb->eb_cookie, ss, port);
}
int
etherbridge_init(struct etherbridge *eb, const char *name,
const struct etherbridge_ops *ops, void *cookie)
{
size_t i;
if (eb_entry_pool.pr_size == 0) {
pool_init(&eb_entry_pool, sizeof(struct eb_entry),
0, IPL_SOFTNET, 0, "ebepl", NULL);
}
eb->eb_table = mallocarray(ETHERBRIDGE_TABLE_SIZE,
sizeof(*eb->eb_table), M_DEVBUF, M_WAITOK|M_CANFAIL);
if (eb->eb_table == NULL)
return (ENOMEM);
eb->eb_name = name;
eb->eb_ops = ops;
eb->eb_cookie = cookie;
mtx_init(&eb->eb_lock, IPL_SOFTNET);
RBT_INIT(eb_tree, &eb->eb_tree);
eb->eb_num = 0;
eb->eb_max = 100;
eb->eb_max_age = 240;
timeout_set(&eb->eb_tmo_age, etherbridge_age, eb);
for (i = 0; i < ETHERBRIDGE_TABLE_SIZE; i++) {
struct eb_list *ebl = &eb->eb_table[i];
SMR_TAILQ_INIT(ebl);
}
return (0);
}
int
etherbridge_up(struct etherbridge *eb)
{
etherbridge_age(eb);
return (0);
}
int
etherbridge_down(struct etherbridge *eb)
{
smr_barrier();
return (0);
}
void
etherbridge_destroy(struct etherbridge *eb)
{
struct eb_entry *ebe, *nebe;
/* XXX assume that nothing will calling etherbridge_map now */
timeout_del_barrier(&eb->eb_tmo_age);
free(eb->eb_table, M_DEVBUF,
ETHERBRIDGE_TABLE_SIZE * sizeof(*eb->eb_table));
RBT_FOREACH_SAFE(ebe, eb_tree, &eb->eb_tree, nebe) {
RBT_REMOVE(eb_tree, &eb->eb_tree, ebe);
ebe_free(ebe);
}
}
static struct eb_list *
etherbridge_list(struct etherbridge *eb, uint64_t eba)
{
uint16_t hash;
hash = stoeplitz_h64(eba) & ETHERBRIDGE_TABLE_MASK;
return (&eb->eb_table[hash]);
}
static struct eb_entry *
ebl_find(struct eb_list *ebl, uint64_t eba)
{
struct eb_entry *ebe;
SMR_TAILQ_FOREACH(ebe, ebl, ebe_lentry) {
if (ebe->ebe_addr == eba)
return (ebe);
}
return (NULL);
}
static inline void
ebl_insert(struct eb_list *ebl, struct eb_entry *ebe)
{
SMR_TAILQ_INSERT_TAIL_LOCKED(ebl, ebe, ebe_lentry);
}
static inline void
ebl_remove(struct eb_list *ebl, struct eb_entry *ebe)
{
SMR_TAILQ_REMOVE_LOCKED(ebl, ebe, ebe_lentry);
}
static inline int
ebt_cmp(const struct eb_entry *aebe, const struct eb_entry *bebe)
{
if (aebe->ebe_addr > bebe->ebe_addr)
return (1);
if (aebe->ebe_addr < bebe->ebe_addr)
return (-1);
return (0);
}
RBT_GENERATE(eb_tree, eb_entry, ebe_tentry, ebt_cmp);
static inline struct eb_entry *
ebt_insert(struct etherbridge *eb, struct eb_entry *ebe)
{
return (RBT_INSERT(eb_tree, &eb->eb_tree, ebe));
}
static inline struct eb_entry *
ebt_find(struct etherbridge *eb, const struct eb_entry *ebe)
{
return (RBT_FIND(eb_tree, &eb->eb_tree, ebe));
}
static inline void
ebt_replace(struct etherbridge *eb, struct eb_entry *oebe,
struct eb_entry *nebe)
{
struct eb_entry *rvebe;
RBT_REMOVE(eb_tree, &eb->eb_tree, oebe);
rvebe = RBT_INSERT(eb_tree, &eb->eb_tree, nebe);
KASSERTMSG(rvebe == NULL, "ebt_replace eb %p nebe %p rvebe %p",
eb, nebe, rvebe);
}
static inline void
ebt_remove(struct etherbridge *eb, struct eb_entry *ebe)
{
RBT_REMOVE(eb_tree, &eb->eb_tree, ebe);
}
static inline void
ebe_rele(struct eb_entry *ebe)
{
smr_call(&ebe->ebe_smr_entry, ebe_free, ebe);
}
static void
ebe_free(void *arg)
{
struct eb_entry *ebe = arg;
struct etherbridge *eb = ebe->ebe_etherbridge;
eb_port_rele(eb, ebe->ebe_port);
pool_put(&eb_entry_pool, ebe);
}
void *
etherbridge_resolve_ea(struct etherbridge *eb,
const struct ether_addr *ea)
{
return (etherbridge_resolve(eb, ether_addr_to_e64(ea)));
}
void *
etherbridge_resolve(struct etherbridge *eb, uint64_t eba)
{
struct eb_list *ebl = etherbridge_list(eb, eba);
struct eb_entry *ebe;
SMR_ASSERT_CRITICAL();
ebe = ebl_find(ebl, eba);
if (ebe != NULL) {
if (ebe->ebe_type == EBE_DYNAMIC) {
int diff = getuptime() - ebe->ebe_age;
if (diff > eb->eb_max_age)
return (NULL);
}
return (ebe->ebe_port);
}
return (NULL);
}
void
etherbridge_map_ea(struct etherbridge *eb, void *port,
const struct ether_addr *ea)
{
etherbridge_map(eb, port, ether_addr_to_e64(ea));
}
void
etherbridge_map(struct etherbridge *eb, void *port, uint64_t eba)
{
struct eb_list *ebl;
struct eb_entry *oebe, *nebe;
unsigned int num;
void *nport;
int new = 0;
time_t now;
if (ETH64_IS_MULTICAST(eba) || ETH64_IS_ANYADDR(eba))
return;
now = getuptime();
ebl = etherbridge_list(eb, eba);
smr_read_enter();
oebe = ebl_find(ebl, eba);
if (oebe == NULL) {
/*
* peek at the space to see if it's worth trying
* to make a new entry.
*/
if (eb->eb_num < eb->eb_max)
new = 1;
} else {
if (oebe->ebe_age != now)
oebe->ebe_age = now;
/* does this entry need to be replaced? */
if (oebe->ebe_type == EBE_DYNAMIC &&
!eb_port_eq(eb, oebe->ebe_port, port))
new = 1;
}
smr_read_leave();
if (!new)
return;
nport = eb_port_take(eb, port);
if (nport == NULL) {
/* XXX should we remove the old one and flood? */
return;
}
nebe = pool_get(&eb_entry_pool, PR_NOWAIT);
if (nebe == NULL) {
/* XXX should we remove the old one and flood? */
eb_port_rele(eb, nport);
return;
}
smr_init(&nebe->ebe_smr_entry);
nebe->ebe_etherbridge = eb;
nebe->ebe_addr = eba;
nebe->ebe_port = nport;
nebe->ebe_type = EBE_DYNAMIC;
nebe->ebe_age = now;
mtx_enter(&eb->eb_lock);
oebe = ebt_find(eb, nebe);
if (oebe == NULL) {
num = eb->eb_num + 1;
if (num <= eb->eb_max) {
ebl_insert(ebl, nebe);
oebe = ebt_insert(eb, nebe);
if (oebe != NULL) {
panic("etherbridge %p changed while locked",
eb);
}
/* great success */
eb->eb_num = num;
nebe = NULL; /* give ref to table */
}
} else if (oebe->ebe_type == EBE_DYNAMIC) {
/* do the update */
ebl_insert(ebl, nebe);
ebl_remove(ebl, oebe);
ebt_replace(eb, oebe, nebe);
nebe = NULL; /* give ref to table */
} else {
/*
* oebe is not a dynamic entry, so don't replace it.
*/
oebe = NULL;
}
mtx_leave(&eb->eb_lock);
if (nebe != NULL) {
/*
* the new entry didn't make it into the
* table so it can be freed directly.
*/
ebe_free(nebe);
}
if (oebe != NULL) {
/*
* we replaced this entry, it needs to be released.
*/
ebe_rele(oebe);
}
}
int
etherbridge_add_addr(struct etherbridge *eb, void *port,
const struct ether_addr *ea, unsigned int type)
{
uint64_t eba = ether_addr_to_e64(ea);
struct eb_list *ebl;
struct eb_entry *nebe;
unsigned int num;
void *nport;
int error = 0;
if (ETH64_IS_MULTICAST(eba) || ETH64_IS_ANYADDR(eba))
return (EADDRNOTAVAIL);
nport = eb_port_take(eb, port);
if (nport == NULL)
return (ENOMEM);
nebe = pool_get(&eb_entry_pool, PR_NOWAIT);
if (nebe == NULL) {
eb_port_rele(eb, nport);
return (ENOMEM);
}
smr_init(&nebe->ebe_smr_entry);
nebe->ebe_etherbridge = eb;
nebe->ebe_addr = eba;
nebe->ebe_port = nport;
nebe->ebe_type = type;
nebe->ebe_age = getuptime();
ebl = etherbridge_list(eb, eba);
mtx_enter(&eb->eb_lock);
num = eb->eb_num + 1;
if (num >= eb->eb_max)
error = ENOSPC;
else if (ebt_insert(eb, nebe) != NULL)
error = EADDRINUSE;
else {
/* we win, do the insert */
ebl_insert(ebl, nebe); /* give the ref to etherbridge */
eb->eb_num = num;
}
mtx_leave(&eb->eb_lock);
if (error != 0) {
/*
* the new entry didn't make it into the
* table, so it can be freed directly.
*/
ebe_free(nebe);
}
return (error);
}
int
etherbridge_del_addr(struct etherbridge *eb, const struct ether_addr *ea)
{
uint64_t eba = ether_addr_to_e64(ea);
struct eb_list *ebl;
struct eb_entry *oebe;
const struct eb_entry key = {
.ebe_addr = eba,
};
int error = 0;
ebl = etherbridge_list(eb, eba);
mtx_enter(&eb->eb_lock);
oebe = ebt_find(eb, &key);
if (oebe == NULL)
error = ESRCH;
else {
KASSERT(eb->eb_num > 0);
eb->eb_num--;
ebl_remove(ebl, oebe); /* it's our ref now */
ebt_remove(eb, oebe);
}
mtx_leave(&eb->eb_lock);
if (oebe != NULL)
ebe_rele(oebe);
return (error);
}
static void
etherbridge_age(void *arg)
{
struct etherbridge *eb = arg;
struct eb_entry *ebe, *nebe;
struct eb_queue ebq = TAILQ_HEAD_INITIALIZER(ebq);
int diff;
unsigned int now = getuptime();
size_t i;
timeout_add_sec(&eb->eb_tmo_age, 100);
for (i = 0; i < ETHERBRIDGE_TABLE_SIZE; i++) {
struct eb_list *ebl = &eb->eb_table[i];
#if 0
if (SMR_TAILQ_EMPTY(ebl));
continue;
#endif
mtx_enter(&eb->eb_lock); /* don't block map too much */
SMR_TAILQ_FOREACH_SAFE_LOCKED(ebe, ebl, ebe_lentry, nebe) {
if (ebe->ebe_type != EBE_DYNAMIC)
continue;
diff = now - ebe->ebe_age;
if (diff < eb->eb_max_age)
continue;
ebl_remove(ebl, ebe);
ebt_remove(eb, ebe);
eb->eb_num--;
/* we own the tables ref now */
TAILQ_INSERT_TAIL(&ebq, ebe, ebe_qentry);
}
mtx_leave(&eb->eb_lock);
}
TAILQ_FOREACH_SAFE(ebe, &ebq, ebe_qentry, nebe) {
TAILQ_REMOVE(&ebq, ebe, ebe_qentry);
ebe_rele(ebe);
}
}
void
etherbridge_detach_port(struct etherbridge *eb, void *port)
{
struct eb_entry *ebe, *nebe;
struct eb_queue ebq = TAILQ_HEAD_INITIALIZER(ebq);
size_t i;
for (i = 0; i < ETHERBRIDGE_TABLE_SIZE; i++) {
struct eb_list *ebl = &eb->eb_table[i];
mtx_enter(&eb->eb_lock); /* don't block map too much */
SMR_TAILQ_FOREACH_SAFE_LOCKED(ebe, ebl, ebe_lentry, nebe) {
if (!eb_port_eq(eb, ebe->ebe_port, port))
continue;
ebl_remove(ebl, ebe);
ebt_remove(eb, ebe);
eb->eb_num--;
/* we own the tables ref now */
TAILQ_INSERT_TAIL(&ebq, ebe, ebe_qentry);
}
mtx_leave(&eb->eb_lock);
}
if (TAILQ_EMPTY(&ebq))
return;
/*
* do one smr barrier for all the entries rather than an
* smr_call each.
*/
smr_barrier();
TAILQ_FOREACH_SAFE(ebe, &ebq, ebe_qentry, nebe) {
TAILQ_REMOVE(&ebq, ebe, ebe_qentry);
ebe_free(ebe);
}
}
void
etherbridge_flush(struct etherbridge *eb, uint32_t flags)
{
struct eb_entry *ebe, *nebe;
struct eb_queue ebq = TAILQ_HEAD_INITIALIZER(ebq);
size_t i;
for (i = 0; i < ETHERBRIDGE_TABLE_SIZE; i++) {
struct eb_list *ebl = &eb->eb_table[i];
mtx_enter(&eb->eb_lock); /* don't block map too much */
SMR_TAILQ_FOREACH_SAFE_LOCKED(ebe, ebl, ebe_lentry, nebe) {
if (flags == IFBF_FLUSHDYN &&
ebe->ebe_type != EBE_DYNAMIC)
continue;
ebl_remove(ebl, ebe);
ebt_remove(eb, ebe);
eb->eb_num--;
/* we own the tables ref now */
TAILQ_INSERT_TAIL(&ebq, ebe, ebe_qentry);
}
mtx_leave(&eb->eb_lock);
}
if (TAILQ_EMPTY(&ebq))
return;
/*
* do one smr barrier for all the entries rather than an
* smr_call each.
*/
smr_barrier();
TAILQ_FOREACH_SAFE(ebe, &ebq, ebe_qentry, nebe) {
TAILQ_REMOVE(&ebq, ebe, ebe_qentry);
ebe_free(ebe);
}
}
int
etherbridge_rtfind(struct etherbridge *eb, struct ifbaconf *baconf)
{
struct eb_entry *ebe;
struct ifbareq bareq;
caddr_t buf;
size_t len, nlen;
time_t age, now = getuptime();
int error;
if (baconf->ifbac_len == 0) {
/* single read is atomic */
baconf->ifbac_len = eb->eb_num * sizeof(bareq);
return (0);
}
buf = malloc(baconf->ifbac_len, M_TEMP, M_WAITOK|M_CANFAIL);
if (buf == NULL)
return (ENOMEM);
len = 0;
mtx_enter(&eb->eb_lock);
RBT_FOREACH(ebe, eb_tree, &eb->eb_tree) {
nlen = len + sizeof(bareq);
if (nlen > baconf->ifbac_len)
break;
strlcpy(bareq.ifba_name, eb->eb_name,
sizeof(bareq.ifba_name));
eb_port_ifname(eb,
bareq.ifba_ifsname, sizeof(bareq.ifba_ifsname),
ebe->ebe_port);
ether_e64_to_addr(&bareq.ifba_dst, ebe->ebe_addr);
memset(&bareq.ifba_dstsa, 0, sizeof(bareq.ifba_dstsa));
eb_port_sa(eb, &bareq.ifba_dstsa, ebe->ebe_port);
switch (ebe->ebe_type) {
case EBE_DYNAMIC:
age = now - ebe->ebe_age;
bareq.ifba_age = MIN(age, 0xff);
bareq.ifba_flags = IFBAF_DYNAMIC;
break;
case EBE_STATIC:
bareq.ifba_age = 0;
bareq.ifba_flags = IFBAF_STATIC;
break;
}
memcpy(buf + len, &bareq, sizeof(bareq));
len = nlen;
}
nlen = baconf->ifbac_len;
baconf->ifbac_len = eb->eb_num * sizeof(bareq);
mtx_leave(&eb->eb_lock);
error = copyout(buf, baconf->ifbac_buf, len);
free(buf, M_TEMP, nlen);
return (error);
}
int
etherbridge_set_max(struct etherbridge *eb, struct ifbrparam *bparam)
{
if (bparam->ifbrp_csize < 1 ||
bparam->ifbrp_csize > 4096) /* XXX */
return (EINVAL);
/* commit */
eb->eb_max = bparam->ifbrp_csize;
return (0);
}
int
etherbridge_get_max(struct etherbridge *eb, struct ifbrparam *bparam)
{
bparam->ifbrp_csize = eb->eb_max;
return (0);
}
int
etherbridge_set_tmo(struct etherbridge *eb, struct ifbrparam *bparam)
{
if (bparam->ifbrp_ctime < 8 ||
bparam->ifbrp_ctime > 3600)
return (EINVAL);
/* commit */
eb->eb_max_age = bparam->ifbrp_ctime;
return (0);
}
int
etherbridge_get_tmo(struct etherbridge *eb, struct ifbrparam *bparam)
{
bparam->ifbrp_ctime = eb->eb_max_age;
return (0);
}
|