635
* This clone function, unlike its original rib_lookup_ipv4(), checks
636
* if specified IPv4 route record (prefix/mask -> gate) exists in
637
* the whole RIB and has ZEBRA_FLAG_SELECTED set.
641
* 0: exact match found
642
* 1: a match was found with a different gate
643
* 2: connected route found
644
* 3: no matches found
647
rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate)
649
struct route_table *table;
650
struct route_node *rn;
652
struct nexthop *nexthop;
655
table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
657
return ZEBRA_RIB_LOOKUP_ERROR;
659
/* Scan the RIB table for exactly matching RIB entry. */
660
rn = route_node_lookup (table, (struct prefix *) p);
662
/* No route for this prefix. */
664
return ZEBRA_RIB_NOTFOUND;
667
route_unlock_node (rn);
669
/* Find out if a "selected" RR for the discovered RIB entry exists ever. */
670
for (match = rn->info; match; match = match->next)
672
if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
674
if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED))
678
/* None such found :( */
680
return ZEBRA_RIB_NOTFOUND;
682
if (match->type == ZEBRA_ROUTE_CONNECT)
683
return ZEBRA_RIB_FOUND_CONNECTED;
685
/* Ok, we have a cood candidate, let's check it's nexthop list... */
686
for (nexthop = match->nexthop; nexthop; nexthop = nexthop->next)
687
if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
689
/* We are happy with either direct or recursive hexthop */
690
if (nexthop->gate.ipv4.s_addr == qgate->sin.sin_addr.s_addr ||
691
nexthop->rgate.ipv4.s_addr == qgate->sin.sin_addr.s_addr)
692
return ZEBRA_RIB_FOUND_EXACT;
695
if (IS_ZEBRA_DEBUG_RIB)
697
char gate_buf[INET_ADDRSTRLEN], rgate_buf[INET_ADDRSTRLEN], qgate_buf[INET_ADDRSTRLEN];
698
inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, gate_buf, INET_ADDRSTRLEN);
699
inet_ntop (AF_INET, &nexthop->rgate.ipv4.s_addr, rgate_buf, INET_ADDRSTRLEN);
700
inet_ntop (AF_INET, &qgate->sin.sin_addr.s_addr, qgate_buf, INET_ADDRSTRLEN);
701
zlog_debug ("%s: qgate == %s, gate == %s, rgate == %s", __func__, qgate_buf, gate_buf, rgate_buf);
703
return ZEBRA_RIB_FOUND_NOGATE;
707
return ZEBRA_RIB_NOTFOUND;
630
712
rib_match_ipv6 (struct in6_addr *addr)
686
768
#endif /* HAVE_IPV6 */
770
#define RIB_SYSTEM_ROUTE(R) \
771
((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
773
/* This function verifies reachability of one given nexthop, which can be
774
* numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
775
* in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
776
* nexthop->ifindex will be updated appropriately as well.
777
* An existing route map can turn (otherwise active) nexthop into inactive, but
780
* The return value is the final value of 'ACTIVE' flag.
689
784
nexthop_active_check (struct route_node *rn, struct rib *rib,
690
785
struct nexthop *nexthop, int set)
692
787
struct interface *ifp;
788
route_map_result_t ret = RMAP_MATCH;
789
extern char *proto_rm[AFI_MAX][ZEBRA_ROUTE_MAX+1];
790
struct route_map *rmap;
694
794
switch (nexthop->type)
696
796
case NEXTHOP_TYPE_IFINDEX:
861
if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
864
if (RIB_SYSTEM_ROUTE(rib) ||
865
(family == AFI_IP && rn->p.family != AF_INET) ||
866
(family == AFI_IP6 && rn->p.family != AF_INET6))
867
return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
870
if (rib->type >= 0 && rib->type < ZEBRA_ROUTE_MAX &&
871
proto_rm[family][rib->type])
872
rmap = route_map_lookup_by_name (proto_rm[family][rib->type]);
873
if (!rmap && proto_rm[family][ZEBRA_ROUTE_MAX])
874
rmap = route_map_lookup_by_name (proto_rm[family][ZEBRA_ROUTE_MAX]);
876
ret = route_map_apply(rmap, &rn->p, RMAP_ZEBRA, nexthop);
879
if (ret == RMAP_DENYMATCH)
880
UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
757
881
return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
884
/* Iterate over all nexthops of the given RIB entry and refresh their
885
* ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
886
* nexthop is found to toggle the ACTIVE flag, the whole rib structure
887
* is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
888
* transparently passed to nexthop_active_check().
890
* Return value is the new number of active nexthops.
761
894
nexthop_active_update (struct route_node *rn, struct rib *rib, int set)
763
896
struct nexthop *nexthop;
897
int prev_active, new_active;
766
899
rib->nexthop_active_num = 0;
767
900
UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
769
902
for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
771
active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
773
nexthop_active_check (rn, rib, nexthop, set);
774
if ((MULTIPATH_NUM == 0 || rib->nexthop_active_num < MULTIPATH_NUM)
775
&& active != CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
776
SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
778
if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
779
rib->nexthop_active_num++;
904
prev_active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
905
if ((new_active = nexthop_active_check (rn, rib, nexthop, set)))
906
rib->nexthop_active_num++;
907
if (prev_active != new_active)
908
SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED);
781
910
return rib->nexthop_active_num;
785
#define RIB_SYSTEM_ROUTE(R) \
786
((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
789
916
rib_install_kernel (struct route_node *rn, struct rib *rib)
862
992
struct route_node *rn = data;
863
993
int installed = 0;
864
994
struct nexthop *nexthop = NULL;
995
char buf[INET_ADDRSTRLEN];
999
if (IS_ZEBRA_DEBUG_RIB || IS_ZEBRA_DEBUG_RIB_Q)
1000
inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
868
1002
for (rib = rn->info; rib; rib = next)
1004
/* The next pointer is saved, because current pointer
1005
* may be passed to rib_unlink() in the middle of iteration.
870
1007
next = rib->next;
872
1009
/* Currently installed rib. */
942
1080
/* metric tie-breaks equal distance */
943
1081
if (rib->metric <= select->metric)
947
/* Same route is selected. */
1083
} /* for (rib = rn->info; rib; rib = next) */
1085
/* After the cycle is finished, the following pointers will be set:
1086
* select --- the winner RIB entry, if any was found, otherwise NULL
1087
* fib --- the SELECTED RIB entry, if any, otherwise NULL
1088
* del --- equal to fib, if fib is queued for deletion, NULL otherwise
1092
/* Same RIB entry is selected. Update FIB and finish. */
948
1093
if (select && select == fib)
950
1095
if (IS_ZEBRA_DEBUG_RIB)
951
zlog_debug ("%s: Updating existing route, select %p, fib %p",
952
__func__, select, fib);
1096
zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
1097
__func__, buf, rn->p.prefixlen, select, fib);
953
1098
if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED))
955
1100
redistribute_delete (&rn->p, select);
986
/* Uninstall old rib from forwarding table. */
1131
/* At this point we either haven't found the best RIB entry or it is
1132
* different from what we currently intend to flag with SELECTED. In both
1133
* cases, if a RIB block is present in FIB, it should be withdrawn.
989
1137
if (IS_ZEBRA_DEBUG_RIB)
990
zlog_debug ("%s: Removing existing route, fib %p", __func__, fib);
1138
zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__,
1139
buf, rn->p.prefixlen, fib);
991
1140
redistribute_delete (&rn->p, fib);
992
1141
if (! RIB_SYSTEM_ROUTE (fib))
993
1142
rib_uninstall_kernel (rn, fib);
997
1146
nexthop_active_update (rn, fib, 1);
1000
/* Install new rib into forwarding table. */
1149
/* Regardless of some RIB entry being SELECTED or not before, now we can
1150
* tell, that if a new winner exists, FIB is still not updated with this
1151
* data, but ready to be.
1003
1155
if (IS_ZEBRA_DEBUG_RIB)
1004
zlog_debug ("%s: Adding route, select %p", __func__, select);
1156
zlog_debug ("%s: %s/%d: Adding route, select %p", __func__, buf,
1157
rn->p.prefixlen, select);
1005
1158
/* Set real nexthop. */
1006
1159
nexthop_active_update (rn, select, 1);
1017
1170
if (IS_ZEBRA_DEBUG_RIB)
1018
zlog_debug ("%s: Deleting fib %p, rn %p", __func__, del, rn);
1171
zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__, buf,
1172
rn->p.prefixlen, del, rn);
1019
1173
rib_unlink (rn, del);
1023
1177
if (IS_ZEBRA_DEBUG_RIB_Q)
1024
zlog_debug ("%s: rn %p dequeued", __func__, rn);
1178
zlog_debug ("%s: %s/%d: rn %p dequeued", __func__, buf, rn->p.prefixlen, rn);
1026
1180
UNSET_FLAG (((struct rib *)rn->info)->rn_status, RIB_ROUTE_QUEUED);
1027
1181
route_unlock_node (rn); /* rib queue lock */
1047
1205
if (CHECK_FLAG (((struct rib *)rn->info)->rn_status, RIB_ROUTE_QUEUED))
1049
1207
if (IS_ZEBRA_DEBUG_RIB_Q)
1050
zlog_debug ("%s: rn %p already queued", __func__, rn);
1208
zlog_debug ("%s: %s/%d: rn %p already queued", __func__, buf,
1209
rn->p.prefixlen, rn);
1054
1213
route_lock_node (rn); /* rib queue lock */
1056
1215
if (IS_ZEBRA_DEBUG_RIB_Q)
1057
zlog_info ("%s: work queue added", __func__);
1216
zlog_info ("%s: %s/%d: work queue added", __func__, buf, rn->p.prefixlen);
1059
1218
assert (zebra);
1141
1300
rib_link (struct route_node *rn, struct rib *rib)
1143
1302
struct rib *head;
1303
char buf[INET_ADDRSTRLEN];
1145
1305
assert (rib && rn);
1147
1307
route_lock_node (rn); /* rn route table reference */
1149
1309
if (IS_ZEBRA_DEBUG_RIB)
1150
zlog_debug ("%s: rn %p, rib %p", __func__, rn, rib);
1311
inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
1312
zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__,
1313
buf, rn->p.prefixlen, rn, rib);
1152
1316
head = rn->info;
1155
1319
if (IS_ZEBRA_DEBUG_RIB)
1156
zlog_debug ("%s: new head, rn_status copied over", __func__);
1320
zlog_debug ("%s: %s/%d: new head, rn_status copied over", __func__,
1321
buf, rn->p.prefixlen);
1157
1322
head->prev = rib;
1158
1323
/* Transfer the rn status flags to the new head RIB */
1159
1324
rib->rn_status = head->rn_status;
1172
1337
if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
1174
1339
if (IS_ZEBRA_DEBUG_RIB)
1175
zlog_debug ("%s: rn %p, un-removed rib %p",
1341
char buf[INET_ADDRSTRLEN];
1342
inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
1343
zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
1344
__func__, buf, rn->p.prefixlen, rn, rib);
1177
1346
UNSET_FLAG (rib->status, RIB_ENTRY_REMOVED);
1184
1353
rib_unlink (struct route_node *rn, struct rib *rib)
1186
1355
struct nexthop *nexthop, *next;
1356
char buf[INET_ADDRSTRLEN];
1188
1358
assert (rn && rib);
1190
1360
if (IS_ZEBRA_DEBUG_RIB)
1191
zlog_debug ("%s: rn %p, rib %p",
1362
inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
1363
zlog_debug ("%s: %s/%d: rn %p, rib %p",
1364
__func__, buf, rn->p.prefixlen, rn, rib);
1195
1368
rib->next->prev = rib->prev;
1224
1397
rib_delnode (struct route_node *rn, struct rib *rib)
1226
1399
if (IS_ZEBRA_DEBUG_RIB)
1227
zlog_debug ("%s: rn %p, rib %p, removing", __func__, rn, rib);
1401
char buf[INET_ADDRSTRLEN];
1402
inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
1403
zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__,
1404
buf, rn->p.prefixlen, rn, rib);
1228
1406
SET_FLAG (rib->status, RIB_ENTRY_REMOVED);
1229
1407
rib_queue_add (&zebrad, rn);
1233
1411
rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
1234
struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
1412
struct in_addr *gate, struct in_addr *src,
1413
unsigned int ifindex, u_int32_t vrf_id,
1235
1414
u_int32_t metric, u_char distance)
1237
1416
struct rib *rib;
1313
1492
SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1315
1494
/* Link new rib to node.*/
1495
if (IS_ZEBRA_DEBUG_RIB)
1496
zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__, rn, rib);
1316
1497
rib_addnode (rn, rib);
1318
1499
/* Free implicit route.*/
1502
if (IS_ZEBRA_DEBUG_RIB)
1503
zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__, rn, rib);
1320
1504
rib_delnode (rn, same);
1322
1507
route_unlock_node (rn);
1511
/* This function dumps the contents of a given RIB entry into
1512
* standard debug log. Calling function name and IP prefix in
1513
* question are passed as 1st and 2nd arguments.
1516
void rib_dump (const char * func, const struct prefix_ipv4 * p, const struct rib * rib)
1518
char straddr1[INET_ADDRSTRLEN], straddr2[INET_ADDRSTRLEN];
1519
struct nexthop *nexthop;
1521
inet_ntop (AF_INET, &p->prefix, straddr1, INET_ADDRSTRLEN);
1522
zlog_debug ("%s: dumping RIB entry %p for %s/%d", func, rib, straddr1, p->prefixlen);
1525
"%s: refcnt == %lu, uptime == %u, type == %u, table == %d",
1534
"%s: metric == %u, distance == %u, flags == %u, status == %u",
1543
"%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
1546
rib->nexthop_active_num,
1547
rib->nexthop_fib_num
1549
for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
1551
inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, straddr1, INET_ADDRSTRLEN);
1552
inet_ntop (AF_INET, &nexthop->rgate.ipv4.s_addr, straddr2, INET_ADDRSTRLEN);
1555
"%s: NH %s (%s) with flags %s%s%s",
1559
(CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""),
1560
(CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""),
1561
(CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "")
1564
zlog_debug ("%s: dump complete", func);
1567
/* This is an exported helper to rtm_read() to dump the strange
1568
* RIB entry found by rib_lookup_ipv4_route()
1571
void rib_lookup_and_dump (struct prefix_ipv4 * p)
1573
struct route_table *table;
1574
struct route_node *rn;
1576
char prefix_buf[INET_ADDRSTRLEN];
1579
table = vrf_table (AFI_IP, SAFI_UNICAST, 0);
1582
zlog_err ("%s: vrf_table() returned NULL", __func__);
1586
inet_ntop (AF_INET, &p->prefix.s_addr, prefix_buf, INET_ADDRSTRLEN);
1587
/* Scan the RIB table for exactly matching RIB entry. */
1588
rn = route_node_lookup (table, (struct prefix *) p);
1590
/* No route for this prefix. */
1593
zlog_debug ("%s: lookup failed for %s/%d", __func__, prefix_buf, p->prefixlen);
1598
route_unlock_node (rn);
1601
for (rib = rn->info; rib; rib = rib->next)
1605
"%s: rn %p, rib %p: %s, %s",
1609
(CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"),
1610
(CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected")
1612
rib_dump (__func__, p, rib);
1327
1617
rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib)