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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
|
/* $OpenBSD: rtable.c,v 1.71 2020/11/05 10:46:13 denis Exp $ */
/*
* Copyright (c) 2014-2016 Martin Pieuchot
*
* 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.
*/
#ifndef _KERNEL
#include "kern_compat.h"
#else
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/socket.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/domain.h>
#include <sys/srp.h>
#endif
#include <net/rtable.h>
#include <net/route.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_var.h>
/*
* Structures used by rtable_get() to retrieve the corresponding
* routing table for a given pair of ``af'' and ``rtableid''.
*
* Note that once allocated routing table heads are never freed.
* This way we do not need to reference count them.
*
* afmap rtmap/dommp
* ----------- --------- -----
* | 0 |--------> | 0 | 0 | ... | 0 | Array mapping rtableid (=index)
* ----------- --------- ----- to rdomain/loopback (=value).
* | AF_INET |.
* ----------- `. .---------. .---------.
* ... `----> | rtable0 | ... | rtableN | Array of pointers for
* ----------- '---------' '---------' IPv4 routing tables
* | AF_MPLS | indexed by ``rtableid''.
* -----------
*/
struct srp *afmap;
uint8_t af2idx[AF_MAX+1]; /* To only allocate supported AF */
uint8_t af2idx_max;
/* Array of routing table pointers. */
struct rtmap {
unsigned int limit;
void **tbl;
};
/*
* Array of rtableid -> rdomain mapping.
*
* Only used for the first index as describbed above.
*/
struct dommp {
unsigned int limit;
/*
* Array to get the routing domain and loopback interface related to
* a routing table. Format:
*
* 8 unused bits | 16 bits for loopback index | 8 bits for rdomain
*/
unsigned int *value;
};
unsigned int rtmap_limit = 0;
void rtmap_init(void);
void rtmap_grow(unsigned int, sa_family_t);
void rtmap_dtor(void *, void *);
struct srp_gc rtmap_gc = SRP_GC_INITIALIZER(rtmap_dtor, NULL);
void rtable_init_backend(void);
void *rtable_alloc(unsigned int, unsigned int, unsigned int);
void *rtable_get(unsigned int, sa_family_t);
void
rtmap_init(void)
{
struct domain *dp;
int i;
/* Start with a single table for every domain that requires it. */
for (i = 0; (dp = domains[i]) != NULL; i++) {
if (dp->dom_rtoffset == 0)
continue;
rtmap_grow(1, dp->dom_family);
}
/* Initialize the rtableid->rdomain mapping table. */
rtmap_grow(1, 0);
rtmap_limit = 1;
}
/*
* Grow the size of the array of routing table for AF ``af'' to ``nlimit''.
*/
void
rtmap_grow(unsigned int nlimit, sa_family_t af)
{
struct rtmap *map, *nmap;
int i;
KERNEL_ASSERT_LOCKED();
KASSERT(nlimit > rtmap_limit);
nmap = malloc(sizeof(*nmap), M_RTABLE, M_WAITOK);
nmap->limit = nlimit;
nmap->tbl = mallocarray(nlimit, sizeof(*nmap[0].tbl), M_RTABLE,
M_WAITOK|M_ZERO);
map = srp_get_locked(&afmap[af2idx[af]]);
if (map != NULL) {
KASSERT(map->limit == rtmap_limit);
for (i = 0; i < map->limit; i++)
nmap->tbl[i] = map->tbl[i];
}
srp_update_locked(&rtmap_gc, &afmap[af2idx[af]], nmap);
}
void
rtmap_dtor(void *null, void *xmap)
{
struct rtmap *map = xmap;
/*
* doesnt need to be serialized since this is the last reference
* to this map. there's nothing to race against.
*/
free(map->tbl, M_RTABLE, map->limit * sizeof(*map[0].tbl));
free(map, M_RTABLE, sizeof(*map));
}
void
rtable_init(void)
{
struct domain *dp;
int i;
KASSERT(sizeof(struct rtmap) == sizeof(struct dommp));
/* We use index 0 for the rtable/rdomain map. */
af2idx_max = 1;
memset(af2idx, 0, sizeof(af2idx));
/*
* Compute the maximum supported key length in case the routing
* table backend needs it.
*/
for (i = 0; (dp = domains[i]) != NULL; i++) {
if (dp->dom_rtoffset == 0)
continue;
af2idx[dp->dom_family] = af2idx_max++;
}
rtable_init_backend();
/*
* Allocate AF-to-id table now that we now how many AFs this
* kernel supports.
*/
afmap = mallocarray(af2idx_max + 1, sizeof(*afmap), M_RTABLE,
M_WAITOK|M_ZERO);
rtmap_init();
if (rtable_add(0) != 0)
panic("unable to create default routing table");
}
int
rtable_add(unsigned int id)
{
struct domain *dp;
void *tbl;
struct rtmap *map;
struct dommp *dmm;
sa_family_t af;
unsigned int off, alen;
int i;
KERNEL_ASSERT_LOCKED();
if (id > RT_TABLEID_MAX)
return (EINVAL);
if (rtable_exists(id))
return (EEXIST);
for (i = 0; (dp = domains[i]) != NULL; i++) {
if (dp->dom_rtoffset == 0)
continue;
af = dp->dom_family;
off = dp->dom_rtoffset;
alen = dp->dom_maxplen;
if (id >= rtmap_limit)
rtmap_grow(id + 1, af);
tbl = rtable_alloc(id, alen, off);
if (tbl == NULL)
return (ENOMEM);
map = srp_get_locked(&afmap[af2idx[af]]);
map->tbl[id] = tbl;
}
/* Reflect possible growth. */
if (id >= rtmap_limit) {
rtmap_grow(id + 1, 0);
rtmap_limit = id + 1;
}
/* Use main rtable/rdomain by default. */
dmm = srp_get_locked(&afmap[0]);
dmm->value[id] = 0;
return (0);
}
void *
rtable_get(unsigned int rtableid, sa_family_t af)
{
struct rtmap *map;
void *tbl = NULL;
struct srp_ref sr;
if (af >= nitems(af2idx) || af2idx[af] == 0)
return (NULL);
map = srp_enter(&sr, &afmap[af2idx[af]]);
if (rtableid < map->limit)
tbl = map->tbl[rtableid];
srp_leave(&sr);
return (tbl);
}
int
rtable_exists(unsigned int rtableid)
{
struct domain *dp;
void *tbl;
int i;
for (i = 0; (dp = domains[i]) != NULL; i++) {
if (dp->dom_rtoffset == 0)
continue;
tbl = rtable_get(rtableid, dp->dom_family);
if (tbl != NULL)
return (1);
}
return (0);
}
int
rtable_empty(unsigned int rtableid)
{
struct domain *dp;
int i;
struct art_root *tbl;
for (i = 0; (dp = domains[i]) != NULL; i++) {
if (dp->dom_rtoffset == 0)
continue;
tbl = rtable_get(rtableid, dp->dom_family);
if (tbl == NULL)
continue;
if (tbl->ar_root.ref != NULL)
return (0);
}
return (1);
}
unsigned int
rtable_l2(unsigned int rtableid)
{
struct dommp *dmm;
unsigned int rdomain = 0;
struct srp_ref sr;
dmm = srp_enter(&sr, &afmap[0]);
if (rtableid < dmm->limit)
rdomain = (dmm->value[rtableid] & RT_TABLEID_MASK);
srp_leave(&sr);
return (rdomain);
}
unsigned int
rtable_loindex(unsigned int rtableid)
{
struct dommp *dmm;
unsigned int loifidx = 0;
struct srp_ref sr;
dmm = srp_enter(&sr, &afmap[0]);
if (rtableid < dmm->limit)
loifidx = (dmm->value[rtableid] >> RT_TABLEID_BITS);
srp_leave(&sr);
return (loifidx);
}
void
rtable_l2set(unsigned int rtableid, unsigned int rdomain, unsigned int loifidx)
{
struct dommp *dmm;
unsigned int value;
KERNEL_ASSERT_LOCKED();
if (!rtable_exists(rtableid) || !rtable_exists(rdomain))
return;
value = (rdomain & RT_TABLEID_MASK) | (loifidx << RT_TABLEID_BITS);
dmm = srp_get_locked(&afmap[0]);
dmm->value[rtableid] = value;
}
static inline uint8_t *satoaddr(struct art_root *, struct sockaddr *);
int an_match(struct art_node *, struct sockaddr *, int);
void rtentry_ref(void *, void *);
void rtentry_unref(void *, void *);
void rtable_mpath_insert(struct art_node *, struct rtentry *);
struct srpl_rc rt_rc = SRPL_RC_INITIALIZER(rtentry_ref, rtentry_unref, NULL);
void
rtable_init_backend(void)
{
art_init();
}
void *
rtable_alloc(unsigned int rtableid, unsigned int alen, unsigned int off)
{
return (art_alloc(rtableid, alen, off));
}
int
rtable_setsource(unsigned int rtableid, struct sockaddr *src)
{
struct art_root *ar;
if ((ar = rtable_get(rtableid, src->sa_family)) == NULL)
return (EAFNOSUPPORT);
/*
* Check if source address is assigned to an interface in the
* same rdomain
*/
if (ifa_ifwithaddr(src, rtableid) == NULL) {
/*
* If source address is 0.0.0.0 or ::
* use automatic source selection
*/
switch(src->sa_family) {
case AF_INET:
if(satosin(src)->sin_addr.s_addr == INADDR_ANY)
return (EINVAL);
break;
case AF_INET6:
if (IN6_IS_ADDR_UNSPECIFIED(&satosin6(src)->sin6_addr))
return (EINVAL);
break;
default:
return (EAFNOSUPPORT);
}
src = NULL;
}
if (ar->source) {
free(ar->source, M_IFADDR, ar->source->sa_len);
ar->source = NULL;
}
if (src) {
ar->source = malloc(src->sa_len, M_IFADDR, M_WAITOK|M_ZERO);
memcpy(ar->source, src, src->sa_len);
}
return (0);
}
struct sockaddr *
rtable_getsource(unsigned int rtableid, int af)
{
struct art_root *ar;
ar = rtable_get(rtableid, af);
if (ar == NULL)
return (NULL);
return (ar->source);
}
void
rtable_clearsource(unsigned int rtableid, struct sockaddr *src)
{
struct sockaddr *addr;
addr = rtable_getsource(rtableid, src->sa_family);
if (addr && (addr->sa_len == src->sa_len)) {
if (memcmp(src, addr, addr->sa_len) == 0) {
memset(addr->sa_data, 0, addr->sa_len-
sizeof(addr->sa_len)-sizeof(addr->sa_family));
rtable_setsource(rtableid, addr);
}
}
}
struct rtentry *
rtable_lookup(unsigned int rtableid, struct sockaddr *dst,
struct sockaddr *mask, struct sockaddr *gateway, uint8_t prio)
{
struct art_root *ar;
struct art_node *an;
struct rtentry *rt = NULL;
struct srp_ref sr, nsr;
uint8_t *addr;
int plen;
ar = rtable_get(rtableid, dst->sa_family);
if (ar == NULL)
return (NULL);
addr = satoaddr(ar, dst);
/* No need for a perfect match. */
if (mask == NULL) {
an = art_match(ar, addr, &nsr);
if (an == NULL)
goto out;
} else {
plen = rtable_satoplen(dst->sa_family, mask);
if (plen == -1)
return (NULL);
an = art_lookup(ar, addr, plen, &nsr);
/* Make sure we've got a perfect match. */
if (!an_match(an, dst, plen))
goto out;
}
SRPL_FOREACH(rt, &sr, &an->an_rtlist, rt_next) {
if (prio != RTP_ANY &&
(rt->rt_priority & RTP_MASK) != (prio & RTP_MASK))
continue;
if (gateway == NULL)
break;
if (rt->rt_gateway->sa_len == gateway->sa_len &&
memcmp(rt->rt_gateway, gateway, gateway->sa_len) == 0)
break;
}
if (rt != NULL)
rtref(rt);
SRPL_LEAVE(&sr);
out:
srp_leave(&nsr);
return (rt);
}
struct rtentry *
rtable_match(unsigned int rtableid, struct sockaddr *dst, uint32_t *src)
{
struct art_root *ar;
struct art_node *an;
struct rtentry *rt = NULL;
struct srp_ref sr, nsr;
uint8_t *addr;
int hash;
ar = rtable_get(rtableid, dst->sa_family);
if (ar == NULL)
return (NULL);
addr = satoaddr(ar, dst);
an = art_match(ar, addr, &nsr);
if (an == NULL)
goto out;
rt = SRPL_FIRST(&sr, &an->an_rtlist);
rtref(rt);
SRPL_LEAVE(&sr);
/* Gateway selection by Hash-Threshold (RFC 2992) */
if ((hash = rt_hash(rt, dst, src)) != -1) {
struct rtentry *mrt;
int threshold, npaths = 0;
KASSERT(hash <= 0xffff);
SRPL_FOREACH(mrt, &sr, &an->an_rtlist, rt_next) {
/* Only count nexthops with the same priority. */
if (mrt->rt_priority == rt->rt_priority)
npaths++;
}
SRPL_LEAVE(&sr);
threshold = (0xffff / npaths) + 1;
/*
* we have no protection against concurrent modification of the
* route list attached to the node, so we won't necessarily
* have the same number of routes. for most modifications,
* we'll pick a route that we wouldn't have if we only saw the
* list before or after the change. if we were going to use
* the last available route, but it got removed, we'll hit
* the end of the list and then pick the first route.
*/
mrt = SRPL_FIRST(&sr, &an->an_rtlist);
while (hash > threshold && mrt != NULL) {
if (mrt->rt_priority == rt->rt_priority)
hash -= threshold;
mrt = SRPL_FOLLOW(&sr, mrt, rt_next);
}
if (mrt != NULL) {
rtref(mrt);
rtfree(rt);
rt = mrt;
}
SRPL_LEAVE(&sr);
}
out:
srp_leave(&nsr);
return (rt);
}
int
rtable_insert(unsigned int rtableid, struct sockaddr *dst,
struct sockaddr *mask, struct sockaddr *gateway, uint8_t prio,
struct rtentry *rt)
{
struct rtentry *mrt;
struct srp_ref sr;
struct art_root *ar;
struct art_node *an, *prev;
uint8_t *addr;
int plen;
unsigned int rt_flags;
int error = 0;
ar = rtable_get(rtableid, dst->sa_family);
if (ar == NULL)
return (EAFNOSUPPORT);
addr = satoaddr(ar, dst);
plen = rtable_satoplen(dst->sa_family, mask);
if (plen == -1)
return (EINVAL);
rtref(rt); /* guarantee rtfree won't do anything during insert */
rw_enter_write(&ar->ar_lock);
/* Do not permit exactly the same dst/mask/gw pair. */
an = art_lookup(ar, addr, plen, &sr);
srp_leave(&sr); /* an can't go away while we have the lock */
if (an_match(an, dst, plen)) {
struct rtentry *mrt;
int mpathok = ISSET(rt->rt_flags, RTF_MPATH);
SRPL_FOREACH_LOCKED(mrt, &an->an_rtlist, rt_next) {
if (prio != RTP_ANY &&
(mrt->rt_priority & RTP_MASK) != (prio & RTP_MASK))
continue;
if (!mpathok ||
(mrt->rt_gateway->sa_len == gateway->sa_len &&
!memcmp(mrt->rt_gateway, gateway, gateway->sa_len))){
error = EEXIST;
goto leave;
}
}
}
an = art_get(dst, plen);
if (an == NULL) {
error = ENOBUFS;
goto leave;
}
/* prepare for immediate operation if insert succeeds */
rt_flags = rt->rt_flags;
rt->rt_flags &= ~RTF_MPATH;
rt->rt_dest = dst;
rt->rt_plen = plen;
SRPL_INSERT_HEAD_LOCKED(&rt_rc, &an->an_rtlist, rt, rt_next);
prev = art_insert(ar, an, addr, plen);
if (prev != an) {
SRPL_REMOVE_LOCKED(&rt_rc, &an->an_rtlist, rt, rtentry,
rt_next);
rt->rt_flags = rt_flags;
art_put(an);
if (prev == NULL) {
error = ESRCH;
goto leave;
}
an = prev;
mrt = SRPL_FIRST_LOCKED(&an->an_rtlist);
KASSERT(mrt != NULL);
KASSERT((rt->rt_flags & RTF_MPATH) || mrt->rt_priority != prio);
/*
* An ART node with the same destination/netmask already
* exists, MPATH conflict must have been already checked.
*/
if (rt->rt_flags & RTF_MPATH) {
/*
* Only keep the RTF_MPATH flag if two routes have
* the same gateway.
*/
rt->rt_flags &= ~RTF_MPATH;
SRPL_FOREACH_LOCKED(mrt, &an->an_rtlist, rt_next) {
if (mrt->rt_priority == prio) {
mrt->rt_flags |= RTF_MPATH;
rt->rt_flags |= RTF_MPATH;
}
}
}
/* Put newly inserted entry at the right place. */
rtable_mpath_insert(an, rt);
}
leave:
rw_exit_write(&ar->ar_lock);
rtfree(rt);
return (error);
}
int
rtable_delete(unsigned int rtableid, struct sockaddr *dst,
struct sockaddr *mask, struct rtentry *rt)
{
struct art_root *ar;
struct art_node *an;
struct srp_ref sr;
uint8_t *addr;
int plen;
struct rtentry *mrt;
int npaths = 0;
int error = 0;
ar = rtable_get(rtableid, dst->sa_family);
if (ar == NULL)
return (EAFNOSUPPORT);
addr = satoaddr(ar, dst);
plen = rtable_satoplen(dst->sa_family, mask);
if (plen == -1)
return (EINVAL);
rtref(rt); /* guarantee rtfree won't do anything under ar_lock */
rw_enter_write(&ar->ar_lock);
an = art_lookup(ar, addr, plen, &sr);
srp_leave(&sr); /* an can't go away while we have the lock */
/* Make sure we've got a perfect match. */
if (!an_match(an, dst, plen)) {
error = ESRCH;
goto leave;
}
/*
* If other multipath route entries are still attached to
* this ART node we only have to unlink it.
*/
SRPL_FOREACH_LOCKED(mrt, &an->an_rtlist, rt_next)
npaths++;
if (npaths > 1) {
KASSERT(rt->rt_refcnt >= 1);
SRPL_REMOVE_LOCKED(&rt_rc, &an->an_rtlist, rt, rtentry,
rt_next);
mrt = SRPL_FIRST_LOCKED(&an->an_rtlist);
if (npaths == 2)
mrt->rt_flags &= ~RTF_MPATH;
goto leave;
}
if (art_delete(ar, an, addr, plen) == NULL)
panic("art_delete failed to find node %p", an);
KASSERT(rt->rt_refcnt >= 1);
SRPL_REMOVE_LOCKED(&rt_rc, &an->an_rtlist, rt, rtentry, rt_next);
art_put(an);
leave:
rw_exit_write(&ar->ar_lock);
rtfree(rt);
return (error);
}
struct rtable_walk_cookie {
int (*rwc_func)(struct rtentry *, void *, unsigned int);
void *rwc_arg;
struct rtentry **rwc_prt;
unsigned int rwc_rid;
};
/*
* Helper for rtable_walk to keep the ART code free from any "struct rtentry".
*/
int
rtable_walk_helper(struct art_node *an, void *xrwc)
{
struct srp_ref sr;
struct rtable_walk_cookie *rwc = xrwc;
struct rtentry *rt;
int error = 0;
SRPL_FOREACH(rt, &sr, &an->an_rtlist, rt_next) {
error = (*rwc->rwc_func)(rt, rwc->rwc_arg, rwc->rwc_rid);
if (error != 0)
break;
}
if (rwc->rwc_prt != NULL && rt != NULL) {
rtref(rt);
*rwc->rwc_prt = rt;
}
SRPL_LEAVE(&sr);
return (error);
}
int
rtable_walk(unsigned int rtableid, sa_family_t af, struct rtentry **prt,
int (*func)(struct rtentry *, void *, unsigned int), void *arg)
{
struct art_root *ar;
struct rtable_walk_cookie rwc;
int error;
ar = rtable_get(rtableid, af);
if (ar == NULL)
return (EAFNOSUPPORT);
rwc.rwc_func = func;
rwc.rwc_arg = arg;
rwc.rwc_prt = prt;
rwc.rwc_rid = rtableid;
error = art_walk(ar, rtable_walk_helper, &rwc);
return (error);
}
struct rtentry *
rtable_iterate(struct rtentry *rt0)
{
struct rtentry *rt = NULL;
struct srp_ref sr;
rt = SRPL_NEXT(&sr, rt0, rt_next);
if (rt != NULL)
rtref(rt);
SRPL_LEAVE(&sr);
rtfree(rt0);
return (rt);
}
int
rtable_mpath_capable(unsigned int rtableid, sa_family_t af)
{
return (1);
}
int
rtable_mpath_reprio(unsigned int rtableid, struct sockaddr *dst,
int plen, uint8_t prio, struct rtentry *rt)
{
struct art_root *ar;
struct art_node *an;
struct srp_ref sr;
uint8_t *addr;
int error = 0;
ar = rtable_get(rtableid, dst->sa_family);
if (ar == NULL)
return (EAFNOSUPPORT);
addr = satoaddr(ar, dst);
rw_enter_write(&ar->ar_lock);
an = art_lookup(ar, addr, plen, &sr);
srp_leave(&sr); /* an can't go away while we have the lock */
/* Make sure we've got a perfect match. */
if (!an_match(an, dst, plen)) {
error = ESRCH;
} else if (SRPL_FIRST_LOCKED(&an->an_rtlist) == rt &&
SRPL_NEXT_LOCKED(rt, rt_next) == NULL) {
/*
* If there's only one entry on the list do not go
* through an insert/remove cycle. This is done to
* guarantee that ``an->an_rtlist'' is never empty
* when a node is in the tree.
*/
rt->rt_priority = prio;
} else {
rtref(rt); /* keep rt alive in between remove and insert */
SRPL_REMOVE_LOCKED(&rt_rc, &an->an_rtlist,
rt, rtentry, rt_next);
rt->rt_priority = prio;
rtable_mpath_insert(an, rt);
rtfree(rt);
error = EAGAIN;
}
rw_exit_write(&ar->ar_lock);
return (error);
}
void
rtable_mpath_insert(struct art_node *an, struct rtentry *rt)
{
struct rtentry *mrt, *prt = NULL;
uint8_t prio = rt->rt_priority;
if ((mrt = SRPL_FIRST_LOCKED(&an->an_rtlist)) == NULL) {
SRPL_INSERT_HEAD_LOCKED(&rt_rc, &an->an_rtlist, rt, rt_next);
return;
}
/* Iterate until we find the route to be placed after ``rt''. */
while (mrt->rt_priority <= prio && SRPL_NEXT_LOCKED(mrt, rt_next)) {
prt = mrt;
mrt = SRPL_NEXT_LOCKED(mrt, rt_next);
}
if (mrt->rt_priority <= prio) {
SRPL_INSERT_AFTER_LOCKED(&rt_rc, mrt, rt, rt_next);
} else if (prt != NULL) {
SRPL_INSERT_AFTER_LOCKED(&rt_rc, prt, rt, rt_next);
} else {
SRPL_INSERT_HEAD_LOCKED(&rt_rc, &an->an_rtlist, rt, rt_next);
}
}
/*
* Returns 1 if ``an'' perfectly matches (``dst'', ``plen''), 0 otherwise.
*/
int
an_match(struct art_node *an, struct sockaddr *dst, int plen)
{
struct rtentry *rt;
struct srp_ref sr;
int match;
if (an == NULL || an->an_plen != plen)
return (0);
rt = SRPL_FIRST(&sr, &an->an_rtlist);
match = (memcmp(rt->rt_dest, dst, dst->sa_len) == 0);
SRPL_LEAVE(&sr);
return (match);
}
void
rtentry_ref(void *null, void *xrt)
{
struct rtentry *rt = xrt;
rtref(rt);
}
void
rtentry_unref(void *null, void *xrt)
{
struct rtentry *rt = xrt;
rtfree(rt);
}
/*
* Return a pointer to the address (key). This is an heritage from the
* BSD radix tree needed to skip the non-address fields from the flavor
* of "struct sockaddr" used by this routing table.
*/
static inline uint8_t *
satoaddr(struct art_root *at, struct sockaddr *sa)
{
return (((uint8_t *)sa) + at->ar_off);
}
/*
* Return the prefix length of a mask.
*/
int
rtable_satoplen(sa_family_t af, struct sockaddr *mask)
{
struct domain *dp;
uint8_t *ap, *ep;
int mlen, plen = 0;
int i;
for (i = 0; (dp = domains[i]) != NULL; i++) {
if (dp->dom_rtoffset == 0)
continue;
if (af == dp->dom_family)
break;
}
if (dp == NULL)
return (-1);
/* Host route */
if (mask == NULL)
return (dp->dom_maxplen);
mlen = mask->sa_len;
/* Default route */
if (mlen == 0)
return (0);
ap = (uint8_t *)((uint8_t *)mask) + dp->dom_rtoffset;
ep = (uint8_t *)((uint8_t *)mask) + mlen;
if (ap > ep)
return (-1);
/* Trim trailing zeroes. */
while (ap < ep && ep[-1] == 0)
ep--;
if (ap == ep)
return (0);
/* "Beauty" adapted from sbin/route/show.c ... */
while (ap < ep) {
switch (*ap++) {
case 0xff:
plen += 8;
break;
case 0xfe:
plen += 7;
goto out;
case 0xfc:
plen += 6;
goto out;
case 0xf8:
plen += 5;
goto out;
case 0xf0:
plen += 4;
goto out;
case 0xe0:
plen += 3;
goto out;
case 0xc0:
plen += 2;
goto out;
case 0x80:
plen += 1;
goto out;
default:
/* Non contiguous mask. */
return (-1);
}
}
out:
if (plen > dp->dom_maxplen || ap != ep)
return -1;
return (plen);
}
|