forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathzebra_rib.c
4212 lines (3679 loc) · 113 KB
/
zebra_rib.c
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
/* Routing Information Base.
* Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <zebra.h>
#include "prefix.h"
#include "table.h"
#include "memory.h"
#include "str.h"
#include "command.h"
#include "if.h"
#include "log.h"
#include "sockunion.h"
#include "linklist.h"
#include "thread.h"
#include "workqueue.h"
#include "prefix.h"
#include "routemap.h"
#include "nexthop.h"
#include "vrf.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
#include "zebra/zebra_ns.h"
#include "zebra/zserv.h"
#include "zebra/zebra_vrf.h"
#include "zebra/redistribute.h"
#include "zebra/zebra_routemap.h"
#include "zebra/debug.h"
#include "zebra/zebra_fpm.h"
#include "zebra/zebra_rnh.h"
#include "zebra/interface.h"
#include "zebra/connected.h"
/* Should we allow non Quagga processes to delete our routes */
extern int allow_delete;
/* Hold time for RIB process, should be very minimal.
* it is useful to able to set it otherwise for testing, hence exported
* as global here for test-rig code.
*/
int rib_process_hold_time = 10;
/* Each route type's string and default distance value. */
static const struct
{
int key;
int distance;
} route_info[ZEBRA_ROUTE_MAX] =
{
[ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0},
[ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0},
[ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0},
[ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1},
[ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120},
[ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120},
[ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110},
[ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110},
[ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115},
[ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */},
/* no entry/default: 150 */
};
/* RPF lookup behaviour */
static enum multicast_mode ipv4_multicast_mode = MCAST_NO_CONFIG;
static void __attribute__((format (printf, 5, 6)))
_rnode_zlog(const char *_func, vrf_id_t vrf_id, struct route_node *rn, int priority,
const char *msgfmt, ...)
{
char buf[PREFIX_STRLEN + 8];
char msgbuf[512];
va_list ap;
va_start(ap, msgfmt);
vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap);
va_end(ap);
if (rn)
{
rib_table_info_t *info = rn->table->info;
prefix2str(&rn->p, buf, sizeof(buf));
if (info->safi == SAFI_MULTICAST)
strcat(buf, " (MRIB)");
}
else
{
snprintf(buf, sizeof(buf), "{(route_node *) NULL}");
}
zlog (NULL, priority, "%s: %d:%s: %s", _func, vrf_id, buf, msgbuf);
}
#define rnode_debug(node, vrf_id, ...) \
_rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
#define rnode_info(node, ...) \
_rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
int
is_zebra_valid_kernel_table(u_int32_t table_id)
{
if ((table_id > ZEBRA_KERNEL_TABLE_MAX) ||
(table_id == RT_TABLE_UNSPEC) ||
(table_id == RT_TABLE_LOCAL) ||
(table_id == RT_TABLE_COMPAT))
return 0;
else
return 1;
}
int
is_zebra_main_routing_table(u_int32_t table_id)
{
if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default))
return 1;
return 0;
}
int
zebra_check_addr (struct prefix *p)
{
if (p->family == AF_INET)
{
u_int32_t addr;
addr = p->u.prefix4.s_addr;
addr = ntohl (addr);
if (IPV4_NET127 (addr)
|| IN_CLASSD (addr)
|| IPV4_LINKLOCAL(addr))
return 0;
}
if (p->family == AF_INET6)
{
if (IN6_IS_ADDR_LOOPBACK (&p->u.prefix6))
return 0;
if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
return 0;
}
return 1;
}
/* Add nexthop to the end of a rib node's nexthop list */
void
rib_nexthop_add (struct rib *rib, struct nexthop *nexthop)
{
nexthop_add(&rib->nexthop, nexthop);
rib->nexthop_num++;
}
/**
* copy_nexthop - copy a nexthop to the rib structure.
*/
void
rib_copy_nexthops (struct rib *rib, struct nexthop *nh)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->flags = nh->flags;
nexthop->type = nh->type;
nexthop->ifindex = nh->ifindex;
memcpy(&(nexthop->gate), &(nh->gate), sizeof(union g_addr));
memcpy(&(nexthop->src), &(nh->src), sizeof(union g_addr));
rib_nexthop_add(rib, nexthop);
if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE))
copy_nexthops(&nexthop->resolved, nh->resolved);
}
/* Delete specified nexthop from the list. */
static void
rib_nexthop_delete (struct rib *rib, struct nexthop *nexthop)
{
if (nexthop->next)
nexthop->next->prev = nexthop->prev;
if (nexthop->prev)
nexthop->prev->next = nexthop->next;
else
rib->nexthop = nexthop->next;
rib->nexthop_num--;
}
struct nexthop *
rib_nexthop_ifindex_add (struct rib *rib, unsigned int ifindex)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->type = NEXTHOP_TYPE_IFINDEX;
nexthop->ifindex = ifindex;
rib_nexthop_add (rib, nexthop);
return nexthop;
}
struct nexthop *
rib_nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4, struct in_addr *src)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->type = NEXTHOP_TYPE_IPV4;
nexthop->gate.ipv4 = *ipv4;
if (src)
nexthop->src.ipv4 = *src;
rib_nexthop_add (rib, nexthop);
return nexthop;
}
struct nexthop *
rib_nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4,
struct in_addr *src, unsigned int ifindex)
{
struct nexthop *nexthop;
struct interface *ifp;
nexthop = nexthop_new();
nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
nexthop->gate.ipv4 = *ipv4;
if (src)
nexthop->src.ipv4 = *src;
nexthop->ifindex = ifindex;
ifp = if_lookup_by_index (nexthop->ifindex);
/*Pending: need to think if null ifp here is ok during bootup?
There was a crash because ifp here was coming to be NULL */
if (ifp)
if (connected_is_unnumbered(ifp)) {
SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
}
rib_nexthop_add (rib, nexthop);
return nexthop;
}
struct nexthop *
rib_nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->type = NEXTHOP_TYPE_IPV6;
nexthop->gate.ipv6 = *ipv6;
rib_nexthop_add (rib, nexthop);
return nexthop;
}
struct nexthop *
rib_nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6,
unsigned int ifindex)
{
struct nexthop *nexthop;
nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
nexthop->gate.ipv6 = *ipv6;
nexthop->ifindex = ifindex;
rib_nexthop_add (rib, nexthop);
return nexthop;
}
struct nexthop *
rib_nexthop_blackhole_add (struct rib *rib)
{
struct nexthop *nexthop;
nexthop = nexthop_new();
nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
SET_FLAG (rib->flags, ZEBRA_FLAG_BLACKHOLE);
rib_nexthop_add (rib, nexthop);
return nexthop;
}
/* This method checks whether a recursive nexthop has at
* least one resolved nexthop in the fib.
*/
int
nexthop_has_fib_child(struct nexthop *nexthop)
{
struct nexthop *nh;
if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
return 0;
for (nh = nexthop->resolved; nh; nh = nh->next)
if (CHECK_FLAG (nh->flags, NEXTHOP_FLAG_FIB))
return 1;
return 0;
}
/* If force flag is not set, do not modify falgs at all for uninstall
the route from FIB. */
static int
nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set,
struct route_node *top)
{
struct prefix_ipv4 p;
struct route_table *table;
struct route_node *rn;
struct rib *match;
int resolved;
struct nexthop *newhop, *tnewhop;
struct nexthop *resolved_hop;
int recursing = 0;
struct interface *ifp;
if (nexthop->type == NEXTHOP_TYPE_IPV4)
nexthop->ifindex = 0;
if (set)
{
UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
zebra_deregister_rnh_static_nexthops(rib->vrf_id, nexthop->resolved, top);
nexthops_free(nexthop->resolved);
nexthop->resolved = NULL;
}
/* Skip nexthops that have been filtered out due to route-map */
/* The nexthops are specific to this route and so the same */
/* nexthop for a different route may not have this flag set */
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED))
return 0;
/*
* Check to see if we should trust the passed in information
* for UNNUMBERED interfaces as that we won't find the GW
* address in the routing table.
*/
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK))
{
ifp = if_lookup_by_index (nexthop->ifindex);
if (ifp && connected_is_unnumbered(ifp))
{
if (if_is_operative(ifp))
return 1;
else
return 0;
}
else
return 0;
}
/* Make lookup prefix. */
memset (&p, 0, sizeof (struct prefix_ipv4));
p.family = AF_INET;
p.prefixlen = IPV4_MAX_PREFIXLEN;
p.prefix = nexthop->gate.ipv4;
/* Lookup table. */
table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, rib->vrf_id);
if (! table)
return 0;
rn = route_node_match (table, (struct prefix *) &p);
while (rn)
{
route_unlock_node (rn);
/* If lookup self prefix return immediately. */
if (rn == top)
return 0;
/* Pick up selected route. */
/* However, do not resolve over default route unless explicitly allowed. */
if (is_default_prefix (&rn->p) &&
!nh_resolve_via_default (p.family))
return 0;
RNODE_FOREACH_RIB (rn, match)
{
if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
continue;
/* if the next hop is imported from another table, skip it */
if (match->type == ZEBRA_ROUTE_TABLE)
continue;
if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
break;
}
/* If there is no selected route or matched route is EGP, go up
tree. */
if (! match)
{
do {
rn = rn->parent;
} while (rn && rn->info == NULL);
if (rn)
route_lock_node (rn);
}
else
{
/* If the longest prefix match for the nexthop yields
* a blackhole, mark it as inactive. */
if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE)
|| CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT))
return 0;
if (match->type == ZEBRA_ROUTE_CONNECT)
{
/* Directly point connected route. */
newhop = match->nexthop;
if (newhop && nexthop->type == NEXTHOP_TYPE_IPV4)
nexthop->ifindex = newhop->ifindex;
return 1;
}
else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
{
resolved = 0;
for (newhop = match->nexthop; newhop; newhop = newhop->next)
if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
&& ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
{
if (set)
{
SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
SET_FLAG(rib->status, RIB_ENTRY_NEXTHOPS_CHANGED);
resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
/* If the resolving route specifies a gateway, use it */
if (newhop->type == NEXTHOP_TYPE_IPV4
|| newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
{
resolved_hop->type = newhop->type;
resolved_hop->gate.ipv4 = newhop->gate.ipv4;
if (newhop->ifindex)
{
resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
resolved_hop->ifindex = newhop->ifindex;
if (newhop->flags & NEXTHOP_FLAG_ONLINK)
resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
}
}
/* If the resolving route is an interface route,
* it means the gateway we are looking up is connected
* to that interface. (The actual network is _not_ onlink).
* Therefore, the resolved route should have the original
* gateway as nexthop as it is directly connected.
*
* On Linux, we have to set the onlink netlink flag because
* otherwise, the kernel won't accept the route. */
if (newhop->type == NEXTHOP_TYPE_IFINDEX)
{
resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
resolved_hop->ifindex = newhop->ifindex;
}
nexthop_add(&nexthop->resolved, resolved_hop);
}
resolved = 1;
}
return resolved;
}
else if (rib->type == ZEBRA_ROUTE_STATIC)
{
resolved = 0;
for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
{
if (set)
{
SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
/* If the resolving route specifies a gateway, use it */
if (newhop->type == NEXTHOP_TYPE_IPV4
|| newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
{
resolved_hop->type = newhop->type;
resolved_hop->gate.ipv4 = newhop->gate.ipv4;
if (newhop->ifindex)
{
resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
resolved_hop->ifindex = newhop->ifindex;
if (newhop->flags & NEXTHOP_FLAG_ONLINK)
resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
}
}
/* If the resolving route is an interface route,
* it means the gateway we are looking up is connected
* to that interface. (The actual network is _not_ onlink).
* Therefore, the resolved route should have the original
* gateway as nexthop as it is directly connected.
*
* On Linux, we have to set the onlink netlink flag because
* otherwise, the kernel won't accept the route.
*/
if (newhop->type == NEXTHOP_TYPE_IFINDEX)
{
resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
resolved_hop->ifindex = newhop->ifindex;
}
nexthop_add(&nexthop->resolved, resolved_hop);
}
resolved = 1;
}
return resolved;
}
else
{
return 0;
}
}
}
return 0;
}
/* If force flag is not set, do not modify falgs at all for uninstall
the route from FIB. */
static int
nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set,
struct route_node *top)
{
struct prefix_ipv6 p;
struct route_table *table;
struct route_node *rn;
struct rib *match;
int resolved;
struct nexthop *newhop, *tnewhop;
int recursing = 0;
struct nexthop *resolved_hop;
if (nexthop->type == NEXTHOP_TYPE_IPV6)
nexthop->ifindex = 0;
if (set)
{
UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
zebra_deregister_rnh_static_nexthops (rib->vrf_id, nexthop->resolved, top);
nexthops_free(nexthop->resolved);
nexthop->resolved = NULL;
}
/* Skip nexthops that have been filtered out due to route-map */
/* The nexthops are specific to this route and so the same */
/* nexthop for a different route may not have this flag set */
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED))
return 0;
/* Make lookup prefix. */
memset (&p, 0, sizeof (struct prefix_ipv6));
p.family = AF_INET6;
p.prefixlen = IPV6_MAX_PREFIXLEN;
p.prefix = nexthop->gate.ipv6;
/* Lookup table. */
table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, rib->vrf_id);
if (! table)
return 0;
rn = route_node_match (table, (struct prefix *) &p);
while (rn)
{
route_unlock_node (rn);
/* If lookup self prefix return immediately. */
if (rn == top)
return 0;
/* Pick up selected route. */
/* However, do not resolve over default route unless explicitly allowed. */
if (is_default_prefix (&rn->p) &&
!nh_resolve_via_default (p.family))
return 0;
RNODE_FOREACH_RIB (rn, match)
{
if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
continue;
if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
break;
}
/* If there is no selected route or matched route is EGP, go up
tree. */
if (! match)
{
do {
rn = rn->parent;
} while (rn && rn->info == NULL);
if (rn)
route_lock_node (rn);
}
else
{
/* If the longest prefix match for the nexthop yields
* a blackhole, mark it as inactive. */
if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE)
|| CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT))
return 0;
if (match->type == ZEBRA_ROUTE_CONNECT)
{
/* Directly point connected route. */
newhop = match->nexthop;
if (newhop && nexthop->type == NEXTHOP_TYPE_IPV6)
nexthop->ifindex = newhop->ifindex;
return 1;
}
else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
{
resolved = 0;
for (newhop = match->nexthop; newhop; newhop = newhop->next)
if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
&& ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
{
if (set)
{
SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
SET_FLAG(rib->status, RIB_ENTRY_NEXTHOPS_CHANGED);
resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
/* See nexthop_active_ipv4 for a description how the
* resolved nexthop is constructed. */
if (newhop->type == NEXTHOP_TYPE_IPV6
|| newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
{
resolved_hop->type = newhop->type;
resolved_hop->gate.ipv6 = newhop->gate.ipv6;
if (newhop->ifindex)
{
resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
resolved_hop->ifindex = newhop->ifindex;
}
}
if (newhop->type == NEXTHOP_TYPE_IFINDEX)
{
resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
resolved_hop->ifindex = newhop->ifindex;
}
nexthop_add(&nexthop->resolved, resolved_hop);
}
resolved = 1;
}
return resolved;
}
else if (rib->type == ZEBRA_ROUTE_STATIC)
{
resolved = 0;
for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
{
if (set)
{
SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
/* See nexthop_active_ipv4 for a description how the
* resolved nexthop is constructed. */
if (newhop->type == NEXTHOP_TYPE_IPV6
|| newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
{
resolved_hop->type = newhop->type;
resolved_hop->gate.ipv6 = newhop->gate.ipv6;
if (newhop->ifindex)
{
resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
resolved_hop->ifindex = newhop->ifindex;
}
}
if (newhop->type == NEXTHOP_TYPE_IFINDEX)
{
resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
resolved_hop->ifindex = newhop->ifindex;
}
nexthop_add(&nexthop->resolved, resolved_hop);
}
resolved = 1;
}
return resolved;
}
else
{
return 0;
}
}
}
return 0;
}
struct rib *
rib_match_ipv4 (struct in_addr addr, safi_t safi, vrf_id_t vrf_id,
struct route_node **rn_out)
{
struct prefix_ipv4 p;
struct route_table *table;
struct route_node *rn;
struct rib *match;
struct nexthop *newhop, *tnewhop;
int recursing;
/* Lookup table. */
table = zebra_vrf_table (AFI_IP, safi, vrf_id);
if (! table)
return 0;
memset (&p, 0, sizeof (struct prefix_ipv4));
p.family = AF_INET;
p.prefixlen = IPV4_MAX_PREFIXLEN;
p.prefix = addr;
rn = route_node_match (table, (struct prefix *) &p);
while (rn)
{
route_unlock_node (rn);
/* Pick up selected route. */
RNODE_FOREACH_RIB (rn, match)
{
if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
continue;
if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
break;
}
/* If there is no selected route or matched route is EGP, go up
tree. */
if (! match)
{
do {
rn = rn->parent;
} while (rn && rn->info == NULL);
if (rn)
route_lock_node (rn);
}
else
{
if (match->type != ZEBRA_ROUTE_CONNECT)
{
int found = 0;
for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
{
found = 1;
break;
}
if (!found)
return NULL;
}
if (rn_out)
*rn_out = rn;
return match;
}
}
return NULL;
}
struct rib *
rib_match_ipv4_multicast (struct in_addr addr, struct route_node **rn_out)
{
struct rib *rib = NULL, *mrib = NULL, *urib = NULL;
struct route_node *m_rn = NULL, *u_rn = NULL;
int skip_bgp = 0; /* bool */
switch (ipv4_multicast_mode)
{
case MCAST_MRIB_ONLY:
return rib_match_ipv4 (addr, SAFI_MULTICAST, skip_bgp, rn_out);
case MCAST_URIB_ONLY:
return rib_match_ipv4 (addr, SAFI_UNICAST, skip_bgp, rn_out);
case MCAST_NO_CONFIG:
case MCAST_MIX_MRIB_FIRST:
rib = mrib = rib_match_ipv4 (addr, SAFI_MULTICAST, skip_bgp, &m_rn);
if (!mrib)
rib = urib = rib_match_ipv4 (addr, SAFI_UNICAST, skip_bgp, &u_rn);
break;
case MCAST_MIX_DISTANCE:
mrib = rib_match_ipv4 (addr, SAFI_MULTICAST, skip_bgp, &m_rn);
urib = rib_match_ipv4 (addr, SAFI_UNICAST, skip_bgp, &u_rn);
if (mrib && urib)
rib = urib->distance < mrib->distance ? urib : mrib;
else if (mrib)
rib = mrib;
else if (urib)
rib = urib;
break;
case MCAST_MIX_PFXLEN:
mrib = rib_match_ipv4 (addr, SAFI_MULTICAST, skip_bgp, &m_rn);
urib = rib_match_ipv4 (addr, SAFI_UNICAST, skip_bgp, &u_rn);
if (mrib && urib)
rib = u_rn->p.prefixlen > m_rn->p.prefixlen ? urib : mrib;
else if (mrib)
rib = mrib;
else if (urib)
rib = urib;
break;
}
if (rn_out)
*rn_out = (rib == mrib) ? m_rn : u_rn;
if (IS_ZEBRA_DEBUG_RIB)
{
char buf[BUFSIZ];
inet_ntop (AF_INET, &addr, buf, BUFSIZ);
zlog_debug("%s: %s: found %s, using %s",
__func__, buf,
mrib ? (urib ? "MRIB+URIB" : "MRIB") :
urib ? "URIB" : "nothing",
rib == urib ? "URIB" : rib == mrib ? "MRIB" : "none");
}
return rib;
}
void
multicast_mode_ipv4_set (enum multicast_mode mode)
{
if (IS_ZEBRA_DEBUG_RIB)
zlog_debug("%s: multicast lookup mode set (%d)", __func__, mode);
ipv4_multicast_mode = mode;
}
enum multicast_mode
multicast_mode_ipv4_get (void)
{
return ipv4_multicast_mode;
}
struct rib *
rib_lookup_ipv4 (struct prefix_ipv4 *p, vrf_id_t vrf_id)
{
struct route_table *table;
struct route_node *rn;
struct rib *match;
struct nexthop *nexthop, *tnexthop;
int recursing;
/* Lookup table. */
table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
if (! table)
return 0;
rn = route_node_lookup (table, (struct prefix *) p);
/* No route for this prefix. */
if (! rn)
return NULL;
/* Unlock node. */
route_unlock_node (rn);
RNODE_FOREACH_RIB (rn, match)
{
if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
continue;
if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
break;
}
if (! match)
return NULL;
if (match->type == ZEBRA_ROUTE_CONNECT)
return match;
for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing))
if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
return match;
return NULL;
}
/*
* This clone function, unlike its original rib_lookup_ipv4(), checks
* if specified IPv4 route record (prefix/mask -> gate) exists in
* the whole RIB and has ZEBRA_FLAG_SELECTED set.
*
* Return values:
* -1: error
* 0: exact match found
* 1: a match was found with a different gate
* 2: connected route found
* 3: no matches found
*/
int
rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate,
vrf_id_t vrf_id)
{
struct route_table *table;
struct route_node *rn;
struct rib *match;
struct nexthop *nexthop, *tnexthop;
int recursing;
int nexthops_active;
/* Lookup table. */
table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
if (! table)
return ZEBRA_RIB_LOOKUP_ERROR;
/* Scan the RIB table for exactly matching RIB entry. */
rn = route_node_lookup (table, (struct prefix *) p);
/* No route for this prefix. */
if (! rn)
return ZEBRA_RIB_NOTFOUND;
/* Unlock node. */
route_unlock_node (rn);
/* Find out if a "selected" RR for the discovered RIB entry exists ever. */
RNODE_FOREACH_RIB (rn, match)
{
if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
continue;
if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
break;
}
/* None such found :( */
if (!match)
return ZEBRA_RIB_NOTFOUND;
if (match->type == ZEBRA_ROUTE_CONNECT)
return ZEBRA_RIB_FOUND_CONNECTED;
/* Ok, we have a cood candidate, let's check it's nexthop list... */
nexthops_active = 0;
for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing))
if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
{
nexthops_active = 1;
if (nexthop->gate.ipv4.s_addr == sockunion2ip (qgate))
return ZEBRA_RIB_FOUND_EXACT;
if (IS_ZEBRA_DEBUG_RIB)
{
char gate_buf[INET_ADDRSTRLEN], qgate_buf[INET_ADDRSTRLEN];
inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, gate_buf, INET_ADDRSTRLEN);
inet_ntop (AF_INET, &sockunion2ip(qgate), qgate_buf, INET_ADDRSTRLEN);
zlog_debug ("%s: qgate == %s, %s == %s", __func__,
qgate_buf, recursing ? "rgate" : "gate", gate_buf);
}
}
if (nexthops_active)
return ZEBRA_RIB_FOUND_NOGATE;