]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_rib.c
zebra: Add some missing breadcrumbs
[mirror_frr.git] / zebra / zebra_rib.c
CommitLineData
718e3744 1/* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
896014f4
DL
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 19 */
20
21#include <zebra.h>
22
718e3744 23#include "command.h"
43e52561
QY
24#include "if.h"
25#include "linklist.h"
718e3744 26#include "log.h"
deaa50db 27#include "log_int.h"
43e52561
QY
28#include "memory.h"
29#include "mpls.h"
30#include "nexthop.h"
31#include "prefix.h"
7514fb77
PJ
32#include "prefix.h"
33#include "routemap.h"
43e52561 34#include "sockunion.h"
05737783 35#include "srcdest_table.h"
43e52561
QY
36#include "table.h"
37#include "thread.h"
38#include "vrf.h"
39#include "workqueue.h"
718e3744 40
43e52561
QY
41#include "zebra/connected.h"
42#include "zebra/debug.h"
43#include "zebra/interface.h"
44#include "zebra/redistribute.h"
718e3744 45#include "zebra/rib.h"
46#include "zebra/rt.h"
43e52561
QY
47#include "zebra/zapi_msg.h"
48#include "zebra/zebra_errors.h"
49#include "zebra/zebra_memory.h"
7c551956 50#include "zebra/zebra_ns.h"
fb018d25 51#include "zebra/zebra_rnh.h"
43e52561
QY
52#include "zebra/zebra_routemap.h"
53#include "zebra/zebra_vrf.h"
6134fd82 54#include "zebra/zebra_vxlan.h"
718e3744 55
d62a17ae 56DEFINE_HOOK(rib_update, (struct route_node * rn, const char *reason),
57 (rn, reason))
4f8ea50c 58
6baf7bb8
DS
59/* Should we allow non Quagga processes to delete our routes */
60extern int allow_delete;
61
718e3744 62/* Each route type's string and default distance value. */
d62a17ae 63static const struct {
64 int key;
65 int distance;
66} route_info[ZEBRA_ROUTE_MAX] = {
9d303b37
DL
67 [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0},
68 [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0},
69 [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0},
70 [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1},
71 [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120},
72 [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120},
73 [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110},
74 [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110},
75 [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115},
76 [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */},
c710b277
DS
77 [ZEBRA_ROUTE_PIM] = {ZEBRA_ROUTE_PIM, 255},
78 [ZEBRA_ROUTE_EIGRP] = {ZEBRA_ROUTE_EIGRP, 90},
9d303b37 79 [ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, 10},
c710b277
DS
80 [ZEBRA_ROUTE_HSLS] = {ZEBRA_ROUTE_HSLS, 255},
81 [ZEBRA_ROUTE_OLSR] = {ZEBRA_ROUTE_OLSR, 255},
82 [ZEBRA_ROUTE_TABLE] = {ZEBRA_ROUTE_TABLE, 150},
83 [ZEBRA_ROUTE_LDP] = {ZEBRA_ROUTE_LDP, 150},
84 [ZEBRA_ROUTE_VNC] = {ZEBRA_ROUTE_VNC, 20},
85 [ZEBRA_ROUTE_VNC_DIRECT] = {ZEBRA_ROUTE_VNC_DIRECT, 20},
86 [ZEBRA_ROUTE_VNC_DIRECT_RH] = {ZEBRA_ROUTE_VNC_DIRECT_RH, 20},
87 [ZEBRA_ROUTE_BGP_DIRECT] = {ZEBRA_ROUTE_BGP_DIRECT, 20},
88 [ZEBRA_ROUTE_BGP_DIRECT_EXT] = {ZEBRA_ROUTE_BGP_DIRECT_EXT, 20},
89 [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 100},
400a663b 90 [ZEBRA_ROUTE_SHARP] = {ZEBRA_ROUTE_SHARP, 150},
c710b277 91
d62a17ae 92 /* no entry/default: 150 */
718e3744 93};
6b0655a2 94
4623d897
DL
95/* RPF lookup behaviour */
96static enum multicast_mode ipv4_multicast_mode = MCAST_NO_CONFIG;
97
6c4f4e6e 98
d62a17ae 99static void __attribute__((format(printf, 5, 6)))
100_rnode_zlog(const char *_func, vrf_id_t vrf_id, struct route_node *rn,
101 int priority, const char *msgfmt, ...)
2263a412 102{
d62a17ae 103 char buf[SRCDEST2STR_BUFFER + sizeof(" (MRIB)")];
104 char msgbuf[512];
105 va_list ap;
2263a412 106
d62a17ae 107 va_start(ap, msgfmt);
108 vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap);
109 va_end(ap);
2263a412 110
d62a17ae 111 if (rn) {
112 rib_table_info_t *info = srcdest_rnode_table_info(rn);
113 srcdest_rnode2str(rn, buf, sizeof(buf));
cb653491 114
d62a17ae 115 if (info->safi == SAFI_MULTICAST)
116 strcat(buf, " (MRIB)");
117 } else {
118 snprintf(buf, sizeof(buf), "{(route_node *) NULL}");
119 }
2263a412 120
d62a17ae 121 zlog(priority, "%s: %d:%s: %s", _func, vrf_id, buf, msgbuf);
2263a412
DL
122}
123
d62a17ae 124#define rnode_debug(node, vrf_id, ...) \
2263a412 125 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
d62a17ae 126#define rnode_info(node, ...) \
2263a412
DL
127 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
128
fd289fc8 129uint8_t route_distance(int type)
40c7bdb0 130{
fd289fc8 131 uint8_t distance;
40c7bdb0 132
d62a17ae 133 if ((unsigned)type >= array_size(route_info))
134 distance = 150;
135 else
136 distance = route_info[type].distance;
40c7bdb0 137
d62a17ae 138 return distance;
40c7bdb0 139}
140
d7c0a89a 141int is_zebra_valid_kernel_table(uint32_t table_id)
7a4bb9c5 142{
8f500a1c 143#ifdef linux
d62a17ae 144 if ((table_id == RT_TABLE_UNSPEC) || (table_id == RT_TABLE_LOCAL)
145 || (table_id == RT_TABLE_COMPAT))
146 return 0;
8f500a1c
RW
147#endif
148
d62a17ae 149 return 1;
7a4bb9c5
DS
150}
151
d7c0a89a 152int is_zebra_main_routing_table(uint32_t table_id)
7a4bb9c5 153{
d62a17ae 154 if ((table_id == RT_TABLE_MAIN)
155 || (table_id == zebrad.rtm_table_default))
156 return 1;
157 return 0;
7a4bb9c5
DS
158}
159
86391e56 160int zebra_check_addr(const struct prefix *p)
0aabccc0 161{
d62a17ae 162 if (p->family == AF_INET) {
d7c0a89a 163 uint32_t addr;
0aabccc0 164
d62a17ae 165 addr = p->u.prefix4.s_addr;
166 addr = ntohl(addr);
0aabccc0 167
d62a17ae 168 if (IPV4_NET127(addr) || IN_CLASSD(addr)
169 || IPV4_LINKLOCAL(addr))
170 return 0;
171 }
172 if (p->family == AF_INET6) {
173 if (IN6_IS_ADDR_LOOPBACK(&p->u.prefix6))
174 return 0;
175 if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6))
176 return 0;
177 }
178 return 1;
0aabccc0
DD
179}
180
fa713d9e 181/* Add nexthop to the end of a rib node's nexthop list */
d62a17ae 182void route_entry_nexthop_add(struct route_entry *re, struct nexthop *nexthop)
fa713d9e 183{
7ee30f28 184 nexthop_add(&re->ng.nexthop, nexthop);
d62a17ae 185 re->nexthop_num++;
718e3744 186}
187
6e26278c 188
6e26278c
DS
189/**
190 * copy_nexthop - copy a nexthop to the rib structure.
191 */
d62a17ae 192void route_entry_copy_nexthops(struct route_entry *re, struct nexthop *nh)
6e26278c 193{
7ee30f28
DS
194 assert(!re->ng.nexthop);
195 copy_nexthops(&re->ng.nexthop, nh, NULL);
d62a17ae 196 for (struct nexthop *nexthop = nh; nexthop; nexthop = nexthop->next)
197 re->nexthop_num++;
6e26278c
DS
198}
199
718e3744 200/* Delete specified nexthop from the list. */
d62a17ae 201void route_entry_nexthop_delete(struct route_entry *re, struct nexthop *nexthop)
718e3744 202{
d62a17ae 203 if (nexthop->next)
204 nexthop->next->prev = nexthop->prev;
205 if (nexthop->prev)
206 nexthop->prev->next = nexthop->next;
207 else
7ee30f28 208 re->ng.nexthop = nexthop->next;
d62a17ae 209 re->nexthop_num--;
718e3744 210}
211
fa713d9e 212
d62a17ae 213struct nexthop *route_entry_nexthop_ifindex_add(struct route_entry *re,
4a7371e9
DS
214 ifindex_t ifindex,
215 vrf_id_t nh_vrf_id)
718e3744 216{
d62a17ae 217 struct nexthop *nexthop;
718e3744 218
d62a17ae 219 nexthop = nexthop_new();
220 nexthop->type = NEXTHOP_TYPE_IFINDEX;
221 nexthop->ifindex = ifindex;
4a7371e9 222 nexthop->vrf_id = nh_vrf_id;
718e3744 223
d62a17ae 224 route_entry_nexthop_add(re, nexthop);
718e3744 225
d62a17ae 226 return nexthop;
718e3744 227}
228
d62a17ae 229struct nexthop *route_entry_nexthop_ipv4_add(struct route_entry *re,
230 struct in_addr *ipv4,
4a7371e9
DS
231 struct in_addr *src,
232 vrf_id_t nh_vrf_id)
718e3744 233{
d62a17ae 234 struct nexthop *nexthop;
718e3744 235
d62a17ae 236 nexthop = nexthop_new();
237 nexthop->type = NEXTHOP_TYPE_IPV4;
4a7371e9 238 nexthop->vrf_id = nh_vrf_id;
d62a17ae 239 nexthop->gate.ipv4 = *ipv4;
240 if (src)
241 nexthop->src.ipv4 = *src;
718e3744 242
d62a17ae 243 route_entry_nexthop_add(re, nexthop);
718e3744 244
d62a17ae 245 return nexthop;
718e3744 246}
247
d62a17ae 248struct nexthop *route_entry_nexthop_ipv4_ifindex_add(struct route_entry *re,
249 struct in_addr *ipv4,
250 struct in_addr *src,
4a7371e9
DS
251 ifindex_t ifindex,
252 vrf_id_t nh_vrf_id)
718e3744 253{
d62a17ae 254 struct nexthop *nexthop;
255 struct interface *ifp;
718e3744 256
d62a17ae 257 nexthop = nexthop_new();
4a7371e9 258 nexthop->vrf_id = nh_vrf_id;
d62a17ae 259 nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
260 nexthop->gate.ipv4 = *ipv4;
261 if (src)
262 nexthop->src.ipv4 = *src;
263 nexthop->ifindex = ifindex;
4a7371e9 264 ifp = if_lookup_by_index(nexthop->ifindex, nh_vrf_id);
d62a17ae 265 /*Pending: need to think if null ifp here is ok during bootup?
266 There was a crash because ifp here was coming to be NULL */
267 if (ifp)
996c9314 268 if (connected_is_unnumbered(ifp)
103e4a71
CF
269 || CHECK_FLAG(re->flags, ZEBRA_FLAG_EVPN_ROUTE)
270 || CHECK_FLAG(re->flags, ZEBRA_FLAG_ONLINK)) {
d62a17ae 271 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
272 }
718e3744 273
d62a17ae 274 route_entry_nexthop_add(re, nexthop);
718e3744 275
d62a17ae 276 return nexthop;
718e3744 277}
278
d62a17ae 279struct nexthop *route_entry_nexthop_ipv6_add(struct route_entry *re,
4a7371e9
DS
280 struct in6_addr *ipv6,
281 vrf_id_t nh_vrf_id)
718e3744 282{
d62a17ae 283 struct nexthop *nexthop;
718e3744 284
d62a17ae 285 nexthop = nexthop_new();
4a7371e9 286 nexthop->vrf_id = nh_vrf_id;
d62a17ae 287 nexthop->type = NEXTHOP_TYPE_IPV6;
288 nexthop->gate.ipv6 = *ipv6;
718e3744 289
d62a17ae 290 route_entry_nexthop_add(re, nexthop);
718e3744 291
d62a17ae 292 return nexthop;
718e3744 293}
294
d62a17ae 295struct nexthop *route_entry_nexthop_ipv6_ifindex_add(struct route_entry *re,
296 struct in6_addr *ipv6,
4a7371e9
DS
297 ifindex_t ifindex,
298 vrf_id_t nh_vrf_id)
718e3744 299{
d62a17ae 300 struct nexthop *nexthop;
718e3744 301
d62a17ae 302 nexthop = nexthop_new();
4a7371e9 303 nexthop->vrf_id = nh_vrf_id;
d62a17ae 304 nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
305 nexthop->gate.ipv6 = *ipv6;
306 nexthop->ifindex = ifindex;
103e4a71
CF
307 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_EVPN_ROUTE)
308 || CHECK_FLAG(re->flags, ZEBRA_FLAG_ONLINK)) {
1ec31309 309 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK);
103e4a71 310 }
718e3744 311
d62a17ae 312 route_entry_nexthop_add(re, nexthop);
718e3744 313
d62a17ae 314 return nexthop;
718e3744 315}
718e3744 316
a8309422 317struct nexthop *route_entry_nexthop_blackhole_add(struct route_entry *re,
60466a63 318 enum blackhole_type bh_type)
595db7f1 319{
d62a17ae 320 struct nexthop *nexthop;
595db7f1 321
d62a17ae 322 nexthop = nexthop_new();
4a7371e9 323 nexthop->vrf_id = VRF_DEFAULT;
d62a17ae 324 nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
a8309422 325 nexthop->bh_type = bh_type;
595db7f1 326
d62a17ae 327 route_entry_nexthop_add(re, nexthop);
595db7f1 328
d62a17ae 329 return nexthop;
595db7f1 330}
331
86391e56 332static void nexthop_set_resolved(afi_t afi, const struct nexthop *newhop,
d62a17ae 333 struct nexthop *nexthop)
4491a88f 334{
d62a17ae 335 struct nexthop *resolved_hop;
4491a88f 336
d62a17ae 337 resolved_hop = nexthop_new();
338 SET_FLAG(resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
d855d11f 339
4a7371e9 340 resolved_hop->vrf_id = nexthop->vrf_id;
d855d11f
RW
341 switch (newhop->type) {
342 case NEXTHOP_TYPE_IPV4:
343 case NEXTHOP_TYPE_IPV4_IFINDEX:
344 /* If the resolving route specifies a gateway, use it */
d62a17ae 345 resolved_hop->type = newhop->type;
346 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
4491a88f 347
d62a17ae 348 if (newhop->ifindex) {
349 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
350 resolved_hop->ifindex = newhop->ifindex;
351 if (newhop->flags & NEXTHOP_FLAG_ONLINK)
352 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
353 }
d855d11f
RW
354 break;
355 case NEXTHOP_TYPE_IPV6:
356 case NEXTHOP_TYPE_IPV6_IFINDEX:
d62a17ae 357 resolved_hop->type = newhop->type;
358 resolved_hop->gate.ipv6 = newhop->gate.ipv6;
359
360 if (newhop->ifindex) {
361 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
362 resolved_hop->ifindex = newhop->ifindex;
363 }
d855d11f
RW
364 break;
365 case NEXTHOP_TYPE_IFINDEX:
366 /* If the resolving route is an interface route,
367 * it means the gateway we are looking up is connected
368 * to that interface. (The actual network is _not_ onlink).
369 * Therefore, the resolved route should have the original
370 * gateway as nexthop as it is directly connected.
371 *
372 * On Linux, we have to set the onlink netlink flag because
373 * otherwise, the kernel won't accept the route.
374 */
d62a17ae 375 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
376 if (afi == AFI_IP) {
377 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
378 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
379 } else if (afi == AFI_IP6) {
380 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
381 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
382 }
383 resolved_hop->ifindex = newhop->ifindex;
d855d11f
RW
384 break;
385 case NEXTHOP_TYPE_BLACKHOLE:
59693377
DS
386 resolved_hop->type = NEXTHOP_TYPE_BLACKHOLE;
387 resolved_hop->bh_type = nexthop->bh_type;
d855d11f 388 break;
59693377 389 }
d855d11f 390
f674dfe2
RW
391 /* Copy labels of the resolved route */
392 if (newhop->nh_label)
393 nexthop_add_labels(resolved_hop, newhop->nh_label_type,
394 newhop->nh_label->num_labels,
395 &newhop->nh_label->label[0]);
396
d62a17ae 397 resolved_hop->rparent = nexthop;
398 nexthop_add(&nexthop->resolved, resolved_hop);
4491a88f
DS
399}
400
718e3744 401/* If force flag is not set, do not modify falgs at all for uninstall
402 the route from FIB. */
d62a17ae 403static int nexthop_active(afi_t afi, struct route_entry *re,
404 struct nexthop *nexthop, int set,
405 struct route_node *top)
406{
407 struct prefix p;
408 struct route_table *table;
409 struct route_node *rn;
5f7a4718 410 struct route_entry *match = NULL;
d62a17ae 411 int resolved;
412 struct nexthop *newhop;
413 struct interface *ifp;
5f7a4718 414 rib_dest_t *dest;
d62a17ae 415
416 if ((nexthop->type == NEXTHOP_TYPE_IPV4)
417 || nexthop->type == NEXTHOP_TYPE_IPV6)
418 nexthop->ifindex = 0;
419
420 if (set) {
421 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
d62a17ae 422 nexthops_free(nexthop->resolved);
423 nexthop->resolved = NULL;
424 re->nexthop_mtu = 0;
d44ca835 425 }
18ff3edd 426
1ec31309 427 /* Next hops (remote VTEPs) for EVPN routes are fully resolved. */
428 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_EVPN_RVTEP))
429 return 1;
430
d62a17ae 431 /* Skip nexthops that have been filtered out due to route-map */
432 /* The nexthops are specific to this route and so the same */
433 /* nexthop for a different route may not have this flag set */
34815ea3
DS
434 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED)) {
435 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
436 zlog_debug("\t%s: Nexthop Filtered",
437 __PRETTY_FUNCTION__);
d62a17ae 438 return 0;
34815ea3 439 }
d62a17ae 440
441 /*
442 * Check to see if we should trust the passed in information
443 * for UNNUMBERED interfaces as that we won't find the GW
444 * address in the routing table.
445 */
446 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) {
4a7371e9 447 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
103e4a71
CF
448 if ((ifp && connected_is_unnumbered(ifp))
449 || CHECK_FLAG(re->flags, ZEBRA_FLAG_ONLINK)) {
d62a17ae 450 if (if_is_operative(ifp))
451 return 1;
34815ea3
DS
452 else {
453 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
454 zlog_debug(
455 "\t%s: Onlink and interface %s is not operative",
456 __PRETTY_FUNCTION__, ifp->name);
d62a17ae 457 return 0;
34815ea3
DS
458 }
459 } else {
460 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
461 zlog_debug(
462 "\t%s: Interface %s is not unnumbered",
463 __PRETTY_FUNCTION__,
464 ifp ? ifp->name : "Unknown");
d62a17ae 465 return 0;
34815ea3 466 }
16814f96 467 }
718e3744 468
d62a17ae 469 /* Make lookup prefix. */
470 memset(&p, 0, sizeof(struct prefix));
471 switch (afi) {
472 case AFI_IP:
473 p.family = AF_INET;
474 p.prefixlen = IPV4_MAX_PREFIXLEN;
475 p.u.prefix4 = nexthop->gate.ipv4;
476 break;
477 case AFI_IP6:
478 p.family = AF_INET6;
479 p.prefixlen = IPV6_MAX_PREFIXLEN;
480 p.u.prefix6 = nexthop->gate.ipv6;
481 break;
482 default:
483 assert(afi != AFI_IP && afi != AFI_IP6);
484 break;
718e3744 485 }
d62a17ae 486 /* Lookup table. */
4a7371e9 487 table = zebra_vrf_table(afi, SAFI_UNICAST, nexthop->vrf_id);
34815ea3
DS
488 if (!table) {
489 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
490 zlog_debug("\t%s: Table not found",
491 __PRETTY_FUNCTION__);
d62a17ae 492 return 0;
34815ea3 493 }
d62a17ae 494
495 rn = route_node_match(table, (struct prefix *)&p);
496 while (rn) {
497 route_unlock_node(rn);
498
fd7fd9e5
DS
499 /* Lookup should halt if we've matched against ourselves ('top',
500 * if specified) - i.e., we cannot have a nexthop NH1 is
501 * resolved by a route NH1. The exception is if the route is a
502 * host route.
503 */
504 if (top && rn == top)
996c9314 505 if (((afi == AFI_IP) && (rn->p.prefixlen != 32))
34815ea3
DS
506 || ((afi == AFI_IP6) && (rn->p.prefixlen != 128))) {
507 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
508 zlog_debug(
509 "\t%s: Matched against ourself and prefix length is not max bit length",
510 __PRETTY_FUNCTION__);
fd7fd9e5 511 return 0;
34815ea3 512 }
d62a17ae 513
514 /* Pick up selected route. */
515 /* However, do not resolve over default route unless explicitly
516 * allowed. */
517 if (is_default_prefix(&rn->p)
34815ea3
DS
518 && !rnh_resolve_via_default(p.family)) {
519 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
520 zlog_debug(
521 "\t:%s: Resolved against default route",
522 __PRETTY_FUNCTION__);
d62a17ae 523 return 0;
34815ea3 524 }
d62a17ae 525
5f7a4718 526 dest = rib_dest_from_rnode(rn);
996c9314
LB
527 if (dest && dest->selected_fib
528 && !CHECK_FLAG(dest->selected_fib->status,
529 ROUTE_ENTRY_REMOVED)
530 && dest->selected_fib->type != ZEBRA_ROUTE_TABLE)
5f7a4718 531 match = dest->selected_fib;
48a53dc7 532
d62a17ae 533 /* If there is no selected route or matched route is EGP, go up
534 tree. */
535 if (!match) {
536 do {
537 rn = rn->parent;
538 } while (rn && rn->info == NULL);
539 if (rn)
540 route_lock_node(rn);
c87bdd2b 541
d62a17ae 542 continue;
543 }
544
d62a17ae 545 if (match->type == ZEBRA_ROUTE_CONNECT) {
546 /* Directly point connected route. */
7ee30f28 547 newhop = match->ng.nexthop;
d62a17ae 548 if (newhop) {
549 if (nexthop->type == NEXTHOP_TYPE_IPV4
550 || nexthop->type == NEXTHOP_TYPE_IPV6)
551 nexthop->ifindex = newhop->ifindex;
552 }
553 return 1;
4e8b02f4 554 } else if (CHECK_FLAG(re->flags, ZEBRA_FLAG_ALLOW_RECURSION)) {
d62a17ae 555 resolved = 0;
7ee30f28 556 for (ALL_NEXTHOPS(match->ng, newhop)) {
a8309422
DL
557 if (!CHECK_FLAG(newhop->flags,
558 NEXTHOP_FLAG_FIB))
559 continue;
560 if (CHECK_FLAG(newhop->flags,
561 NEXTHOP_FLAG_RECURSIVE))
562 continue;
563
564 if (set) {
565 SET_FLAG(nexthop->flags,
566 NEXTHOP_FLAG_RECURSIVE);
567 SET_FLAG(re->status,
568 ROUTE_ENTRY_NEXTHOPS_CHANGED);
569 nexthop_set_resolved(afi, newhop,
570 nexthop);
d62a17ae 571 }
a8309422
DL
572 resolved = 1;
573 }
43e31305
JB
574 if (resolved && set)
575 re->nexthop_mtu = match->mtu;
34815ea3
DS
576 if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
577 zlog_debug("\t%s: Recursion failed to find",
578 __PRETTY_FUNCTION__);
d62a17ae 579 return resolved;
580 } else if (re->type == ZEBRA_ROUTE_STATIC) {
581 resolved = 0;
7ee30f28 582 for (ALL_NEXTHOPS(match->ng, newhop)) {
a8309422
DL
583 if (!CHECK_FLAG(newhop->flags,
584 NEXTHOP_FLAG_FIB))
585 continue;
586
587 if (set) {
588 SET_FLAG(nexthop->flags,
589 NEXTHOP_FLAG_RECURSIVE);
590 nexthop_set_resolved(afi, newhop,
591 nexthop);
d62a17ae 592 }
a8309422
DL
593 resolved = 1;
594 }
d62a17ae 595 if (resolved && set)
596 re->nexthop_mtu = match->mtu;
34815ea3
DS
597
598 if (!resolved && IS_ZEBRA_DEBUG_RIB_DETAILED)
599 zlog_debug(
600 "\t%s: Static route unable to resolve",
601 __PRETTY_FUNCTION__);
d62a17ae 602 return resolved;
603 } else {
7939ff76
DS
604 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
605 zlog_debug("\t%s: Route Type %s has not turned on recursion",
606 __PRETTY_FUNCTION__,
607 zebra_route_string(re->type));
608 if (re->type == ZEBRA_ROUTE_BGP &&
609 !CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
610 zlog_debug("\tEBGP: see \"disable-ebgp-connected-route-check\" or \"disable-connected-check\"");
611 }
d62a17ae 612 return 0;
613 }
718e3744 614 }
7939ff76
DS
615 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
616 zlog_debug("\t%s: Nexthop did not lookup in table",
617 __PRETTY_FUNCTION__);
d62a17ae 618 return 0;
718e3744 619}
718e3744 620
d62a17ae 621struct route_entry *rib_match(afi_t afi, safi_t safi, vrf_id_t vrf_id,
622 union g_addr *addr, struct route_node **rn_out)
623{
624 struct prefix p;
625 struct route_table *table;
626 struct route_node *rn;
5f7a4718 627 struct route_entry *match = NULL;
d62a17ae 628 struct nexthop *newhop;
629
630 /* Lookup table. */
631 table = zebra_vrf_table(afi, safi, vrf_id);
632 if (!table)
633 return 0;
634
635 memset(&p, 0, sizeof(struct prefix));
636 p.family = afi;
637 if (afi == AFI_IP) {
638 p.u.prefix4 = addr->ipv4;
639 p.prefixlen = IPV4_MAX_PREFIXLEN;
640 } else {
641 p.u.prefix6 = addr->ipv6;
642 p.prefixlen = IPV6_MAX_PREFIXLEN;
643 }
718e3744 644
d62a17ae 645 rn = route_node_match(table, (struct prefix *)&p);
718e3744 646
d62a17ae 647 while (rn) {
5f7a4718
DS
648 rib_dest_t *dest;
649
d62a17ae 650 route_unlock_node(rn);
718e3744 651
5f7a4718 652 dest = rib_dest_from_rnode(rn);
996c9314
LB
653 if (dest && dest->selected_fib
654 && !CHECK_FLAG(dest->selected_fib->status,
655 ROUTE_ENTRY_REMOVED))
5f7a4718 656 match = dest->selected_fib;
718e3744 657
d62a17ae 658 /* If there is no selected route or matched route is EGP, go up
659 tree. */
660 if (!match) {
661 do {
662 rn = rn->parent;
663 } while (rn && rn->info == NULL);
664 if (rn)
665 route_lock_node(rn);
666 } else {
667 if (match->type != ZEBRA_ROUTE_CONNECT) {
668 int found = 0;
7ee30f28 669 for (ALL_NEXTHOPS(match->ng, newhop))
d62a17ae 670 if (CHECK_FLAG(newhop->flags,
671 NEXTHOP_FLAG_FIB)) {
672 found = 1;
673 break;
674 }
675 if (!found)
676 return NULL;
677 }
678
679 if (rn_out)
680 *rn_out = rn;
681 return match;
682 }
16814f96 683 }
d62a17ae 684 return NULL;
685}
686
687struct route_entry *rib_match_ipv4_multicast(vrf_id_t vrf_id,
688 struct in_addr addr,
689 struct route_node **rn_out)
690{
691 struct route_entry *re = NULL, *mre = NULL, *ure = NULL;
692 struct route_node *m_rn = NULL, *u_rn = NULL;
693 union g_addr gaddr = {.ipv4 = addr};
694
695 switch (ipv4_multicast_mode) {
696 case MCAST_MRIB_ONLY:
697 return rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
698 rn_out);
699 case MCAST_URIB_ONLY:
700 return rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, rn_out);
701 case MCAST_NO_CONFIG:
702 case MCAST_MIX_MRIB_FIRST:
703 re = mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr,
704 &m_rn);
705 if (!mre)
706 re = ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id,
707 &gaddr, &u_rn);
708 break;
709 case MCAST_MIX_DISTANCE:
710 mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
711 ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
712 if (mre && ure)
713 re = ure->distance < mre->distance ? ure : mre;
714 else if (mre)
715 re = mre;
716 else if (ure)
717 re = ure;
718 break;
719 case MCAST_MIX_PFXLEN:
720 mre = rib_match(AFI_IP, SAFI_MULTICAST, vrf_id, &gaddr, &m_rn);
721 ure = rib_match(AFI_IP, SAFI_UNICAST, vrf_id, &gaddr, &u_rn);
722 if (mre && ure)
723 re = u_rn->p.prefixlen > m_rn->p.prefixlen ? ure : mre;
724 else if (mre)
725 re = mre;
726 else if (ure)
727 re = ure;
728 break;
718e3744 729 }
33550aa8 730
d62a17ae 731 if (rn_out)
732 *rn_out = (re == mre) ? m_rn : u_rn;
718e3744 733
d62a17ae 734 if (IS_ZEBRA_DEBUG_RIB) {
735 char buf[BUFSIZ];
736 inet_ntop(AF_INET, &addr, buf, BUFSIZ);
4623d897 737
32391aff
DS
738 zlog_debug("%s: %s: vrf: %u found %s, using %s",
739 __func__, buf, vrf_id,
d62a17ae 740 mre ? (ure ? "MRIB+URIB" : "MRIB")
741 : ure ? "URIB" : "nothing",
742 re == ure ? "URIB" : re == mre ? "MRIB" : "none");
743 }
744 return re;
4623d897
DL
745}
746
d62a17ae 747void multicast_mode_ipv4_set(enum multicast_mode mode)
4623d897 748{
d62a17ae 749 if (IS_ZEBRA_DEBUG_RIB)
750 zlog_debug("%s: multicast lookup mode set (%d)", __func__,
751 mode);
752 ipv4_multicast_mode = mode;
4623d897
DL
753}
754
d62a17ae 755enum multicast_mode multicast_mode_ipv4_get(void)
4623d897 756{
d62a17ae 757 return ipv4_multicast_mode;
4623d897
DL
758}
759
d62a17ae 760struct route_entry *rib_lookup_ipv4(struct prefix_ipv4 *p, vrf_id_t vrf_id)
718e3744 761{
d62a17ae 762 struct route_table *table;
763 struct route_node *rn;
5f7a4718 764 struct route_entry *match = NULL;
d62a17ae 765 struct nexthop *nexthop;
5f7a4718 766 rib_dest_t *dest;
718e3744 767
d62a17ae 768 /* Lookup table. */
769 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
770 if (!table)
771 return 0;
718e3744 772
d62a17ae 773 rn = route_node_lookup(table, (struct prefix *)p);
718e3744 774
d62a17ae 775 /* No route for this prefix. */
776 if (!rn)
777 return NULL;
718e3744 778
d62a17ae 779 /* Unlock node. */
780 route_unlock_node(rn);
5f7a4718 781 dest = rib_dest_from_rnode(rn);
718e3744 782
996c9314
LB
783 if (dest && dest->selected_fib
784 && !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
5f7a4718 785 match = dest->selected_fib;
718e3744 786
d62a17ae 787 if (!match)
788 return NULL;
718e3744 789
d62a17ae 790 if (match->type == ZEBRA_ROUTE_CONNECT)
791 return match;
f9e1b38e 792
7ee30f28 793 for (ALL_NEXTHOPS(match->ng, nexthop))
d62a17ae 794 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB))
795 return match;
718e3744 796
d62a17ae 797 return NULL;
718e3744 798}
799
dc95824a
DO
800/*
801 * This clone function, unlike its original rib_lookup_ipv4(), checks
802 * if specified IPv4 route record (prefix/mask -> gate) exists in
f0f77c9a 803 * the whole RIB and has ROUTE_ENTRY_SELECTED_FIB set.
dc95824a
DO
804 *
805 * Return values:
806 * -1: error
807 * 0: exact match found
808 * 1: a match was found with a different gate
809 * 2: connected route found
810 * 3: no matches found
811 */
d62a17ae 812int rib_lookup_ipv4_route(struct prefix_ipv4 *p, union sockunion *qgate,
813 vrf_id_t vrf_id)
dc95824a 814{
d62a17ae 815 struct route_table *table;
816 struct route_node *rn;
5f7a4718 817 struct route_entry *match = NULL;
d62a17ae 818 struct nexthop *nexthop;
819 int nexthops_active;
5f7a4718 820 rib_dest_t *dest;
dc95824a 821
d62a17ae 822 /* Lookup table. */
823 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
824 if (!table)
825 return ZEBRA_RIB_LOOKUP_ERROR;
dc95824a 826
d62a17ae 827 /* Scan the RIB table for exactly matching RIB entry. */
828 rn = route_node_lookup(table, (struct prefix *)p);
dc95824a 829
d62a17ae 830 /* No route for this prefix. */
831 if (!rn)
832 return ZEBRA_RIB_NOTFOUND;
dc95824a 833
d62a17ae 834 /* Unlock node. */
835 route_unlock_node(rn);
5f7a4718 836 dest = rib_dest_from_rnode(rn);
dc95824a 837
d62a17ae 838 /* Find out if a "selected" RR for the discovered RIB entry exists ever.
839 */
996c9314
LB
840 if (dest && dest->selected_fib
841 && !CHECK_FLAG(dest->selected_fib->status, ROUTE_ENTRY_REMOVED))
5f7a4718 842 match = dest->selected_fib;
dc95824a 843
d62a17ae 844 /* None such found :( */
845 if (!match)
846 return ZEBRA_RIB_NOTFOUND;
847
848 if (match->type == ZEBRA_ROUTE_CONNECT)
849 return ZEBRA_RIB_FOUND_CONNECTED;
850
851 /* Ok, we have a cood candidate, let's check it's nexthop list... */
852 nexthops_active = 0;
7ee30f28 853 for (ALL_NEXTHOPS(match->ng, nexthop))
d62a17ae 854 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
855 nexthops_active = 1;
856 if (nexthop->gate.ipv4.s_addr == sockunion2ip(qgate))
857 return ZEBRA_RIB_FOUND_EXACT;
858 if (IS_ZEBRA_DEBUG_RIB) {
859 char gate_buf[INET_ADDRSTRLEN],
860 qgate_buf[INET_ADDRSTRLEN];
861 inet_ntop(AF_INET, &nexthop->gate.ipv4.s_addr,
862 gate_buf, INET_ADDRSTRLEN);
863 inet_ntop(AF_INET, &sockunion2ip(qgate),
864 qgate_buf, INET_ADDRSTRLEN);
865 zlog_debug("%s: qgate == %s, %s == %s",
866 __func__, qgate_buf,
867 nexthop->rparent ? "rgate" : "gate",
868 gate_buf);
869 }
870 }
871
872 if (nexthops_active)
873 return ZEBRA_RIB_FOUND_NOGATE;
874
875 return ZEBRA_RIB_NOTFOUND;
876}
877
878#define RIB_SYSTEM_ROUTE(R) \
879 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
7514fb77 880
dc95824a
DO
881/* This function verifies reachability of one given nexthop, which can be
882 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
883 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
884 * nexthop->ifindex will be updated appropriately as well.
885 * An existing route map can turn (otherwise active) nexthop into inactive, but
886 * not vice versa.
887 *
888 * The return value is the final value of 'ACTIVE' flag.
889 */
890
d62a17ae 891static unsigned nexthop_active_check(struct route_node *rn,
892 struct route_entry *re,
893 struct nexthop *nexthop, int set)
894{
895 struct interface *ifp;
896 route_map_result_t ret = RMAP_MATCH;
897 int family;
898 char buf[SRCDEST2STR_BUFFER];
86391e56 899 const struct prefix *p, *src_p;
ac6eebce 900 struct zebra_vrf *zvrf;
901
d62a17ae 902 srcdest_rnode_prefixes(rn, &p, &src_p);
903
904 if (rn->p.family == AF_INET)
905 family = AFI_IP;
906 else if (rn->p.family == AF_INET6)
907 family = AFI_IP6;
908 else
909 family = 0;
910 switch (nexthop->type) {
911 case NEXTHOP_TYPE_IFINDEX:
4a7371e9 912 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
d62a17ae 913 if (ifp && if_is_operative(ifp))
914 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
915 else
916 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
917 break;
918 case NEXTHOP_TYPE_IPV4:
919 case NEXTHOP_TYPE_IPV4_IFINDEX:
920 family = AFI_IP;
1ec31309 921 if (nexthop_active(AFI_IP, re, nexthop, set, rn))
d62a17ae 922 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
923 else
924 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
925 break;
926 case NEXTHOP_TYPE_IPV6:
927 family = AFI_IP6;
928 if (nexthop_active(AFI_IP6, re, nexthop, set, rn))
929 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
930 else
931 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
932 break;
933 case NEXTHOP_TYPE_IPV6_IFINDEX:
934 /* RFC 5549, v4 prefix with v6 NH */
935 if (rn->p.family != AF_INET)
936 family = AFI_IP6;
937 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
007dbee6 938 ifp = if_lookup_by_index(nexthop->ifindex,
4a7371e9 939 nexthop->vrf_id);
d62a17ae 940 if (ifp && if_is_operative(ifp))
941 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
942 else
943 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
944 } else {
945 if (nexthop_active(AFI_IP6, re, nexthop, set, rn))
946 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
947 else
948 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
949 }
950 break;
951 case NEXTHOP_TYPE_BLACKHOLE:
952 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
953 break;
954 default:
955 break;
956 }
34815ea3
DS
957 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
958 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
959 zlog_debug("\t%s: Unable to find a active nexthop",
960 __PRETTY_FUNCTION__);
d62a17ae 961 return 0;
34815ea3 962 }
d62a17ae 963
964 /* XXX: What exactly do those checks do? Do we support
ac6eebce 965 * e.g. IPv4 routes with IPv6 nexthops or vice versa?
966 */
d62a17ae 967 if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET)
968 || (family == AFI_IP6 && p->family != AF_INET6))
969 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
970
971 /* The original code didn't determine the family correctly
972 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
973 * from the rib_table_info in those cases.
974 * Possibly it may be better to use only the rib_table_info
975 * in every case.
976 */
977 if (!family) {
978 rib_table_info_t *info;
979
980 info = srcdest_rnode_table_info(rn);
981 family = info->afi;
718e3744 982 }
c52ef59f 983
d62a17ae 984 memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
985
ac6eebce 986 zvrf = zebra_vrf_lookup_by_id(nexthop->vrf_id);
987 if (!zvrf) {
988 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
989 zlog_debug("\t%s: zvrf is NULL", __PRETTY_FUNCTION__);
990 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
991 }
992
d62a17ae 993 /* It'll get set if required inside */
ac6eebce 994 ret = zebra_route_map_check(family, re->type, re->instance, p,
995 nexthop, zvrf, re->tag);
d62a17ae 996 if (ret == RMAP_DENYMATCH) {
997 if (IS_ZEBRA_DEBUG_RIB) {
998 srcdest_rnode2str(rn, buf, sizeof(buf));
999 zlog_debug(
1000 "%u:%s: Filtering out with NH out %s due to route map",
1001 re->vrf_id, buf,
99b9d960 1002 ifindex2ifname(nexthop->ifindex,
4a7371e9 1003 nexthop->vrf_id));
d62a17ae 1004 }
1005 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
518f0eb1 1006 }
d62a17ae 1007 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
718e3744 1008}
1009
03e232a4 1010/* Iterate over all nexthops of the given RIB entry and refresh their
f0f77c9a
DS
1011 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
1012 * nexthop is found to toggle the ACTIVE flag, the whole re structure
1013 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
03e232a4
DO
1014 * transparently passed to nexthop_active_check().
1015 *
1016 * Return value is the new number of active nexthops.
1017 */
1018
d62a17ae 1019static int nexthop_active_update(struct route_node *rn, struct route_entry *re,
1020 int set)
1021{
1022 struct nexthop *nexthop;
1023 union g_addr prev_src;
1024 unsigned int prev_active, new_active, old_num_nh;
1025 ifindex_t prev_index;
ac6eebce 1026
d62a17ae 1027 old_num_nh = re->nexthop_active_num;
1028
1029 re->nexthop_active_num = 0;
1030 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1031
7ee30f28 1032 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
d62a17ae 1033 /* No protocol daemon provides src and so we're skipping
1034 * tracking it */
1035 prev_src = nexthop->rmap_src;
1036 prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1037 prev_index = nexthop->ifindex;
1038 if ((new_active = nexthop_active_check(rn, re, nexthop, set)))
1039 re->nexthop_active_num++;
1040 /* Don't allow src setting on IPv6 addr for now */
1041 if (prev_active != new_active || prev_index != nexthop->ifindex
1042 || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX
1043 && nexthop->type < NEXTHOP_TYPE_IPV6)
1044 && prev_src.ipv4.s_addr
1045 != nexthop->rmap_src.ipv4.s_addr)
1046 || ((nexthop->type >= NEXTHOP_TYPE_IPV6
1047 && nexthop->type < NEXTHOP_TYPE_BLACKHOLE)
1048 && !(IPV6_ADDR_SAME(&prev_src.ipv6,
1049 &nexthop->rmap_src.ipv6)))) {
1050 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1051 SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1052 }
1053 }
1054
1055 if (old_num_nh != re->nexthop_active_num)
1056 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
6e26278c 1057
d62a17ae 1058 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
1059 SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1060 }
1061
1062 return re->nexthop_active_num;
718e3744 1063}
6baeb988 1064
a64448ba
DS
1065/*
1066 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1067 * (nexthops) must have a label.
1068 */
d62a17ae 1069int zebra_rib_labeled_unicast(struct route_entry *re)
a64448ba 1070{
d62a17ae 1071 struct nexthop *nexthop = NULL;
a64448ba 1072
d62a17ae 1073 if (re->type != ZEBRA_ROUTE_BGP)
1074 return 0;
a64448ba 1075
7ee30f28 1076 for (ALL_NEXTHOPS(re->ng, nexthop))
d62a17ae 1077 if (!nexthop->nh_label || !nexthop->nh_label->num_labels)
1078 return 0;
6b0655a2 1079
d62a17ae 1080 return 1;
a64448ba 1081}
718e3744 1082
86391e56 1083void kernel_route_rib_pass_fail(struct route_node *rn, const struct prefix *p,
7d974ba3 1084 struct route_entry *re,
ea1c14f6 1085 enum zebra_dplane_status res)
0c555cc6
DS
1086{
1087 struct nexthop *nexthop;
1088 char buf[PREFIX_STRLEN];
ed216282
DS
1089 rib_dest_t *dest;
1090
1091 dest = rib_dest_from_rnode(rn);
0c555cc6
DS
1092
1093 switch (res) {
ea1c14f6 1094 case ZEBRA_DPLANE_INSTALL_SUCCESS:
ed216282 1095 dest->selected_fib = re;
7ee30f28 1096 for (ALL_NEXTHOPS(re->ng, nexthop)) {
0c555cc6
DS
1097 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1098 continue;
1099
1100 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1101 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1102 else
1103 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1104 }
28610f7e 1105 zsend_route_notify_owner(re, p, ZAPI_ROUTE_INSTALLED);
0c555cc6 1106 break;
ea1c14f6 1107 case ZEBRA_DPLANE_INSTALL_FAILURE:
ed216282
DS
1108 /*
1109 * I am not sure this is the right thing to do here
1110 * but the code always set selected_fib before
1111 * this assignment was moved here.
1112 */
1113 dest->selected_fib = re;
1114
28610f7e 1115 zsend_route_notify_owner(re, p, ZAPI_ROUTE_FAIL_INSTALL);
e914ccbe 1116 flog_err(EC_ZEBRA_DP_INSTALL_FAIL,
9df414fe
QY
1117 "%u:%s: Route install failed", re->vrf_id,
1118 prefix2str(p, buf, sizeof(buf)));
0c555cc6 1119 break;
ea1c14f6 1120 case ZEBRA_DPLANE_DELETE_SUCCESS:
ed216282
DS
1121 /*
1122 * The case where selected_fib is not re is
1123 * when we have received a system route
1124 * that is overriding our installed route
1125 * as such we should leave the selected_fib
1126 * pointer alone
1127 */
1128 if (dest->selected_fib == re)
1129 dest->selected_fib = NULL;
7ee30f28 1130 for (ALL_NEXTHOPS(re->ng, nexthop))
0c555cc6 1131 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
5e54c602
DS
1132
1133 zsend_route_notify_owner(re, p, ZAPI_ROUTE_REMOVED);
0c555cc6 1134 break;
ea1c14f6 1135 case ZEBRA_DPLANE_DELETE_FAILURE:
ed216282
DS
1136 /*
1137 * Should we set this to NULL if the
1138 * delete fails?
1139 */
1140 dest->selected_fib = NULL;
e914ccbe 1141 flog_err(EC_ZEBRA_DP_DELETE_FAIL,
9df414fe
QY
1142 "%u:%s: Route Deletion failure", re->vrf_id,
1143 prefix2str(p, buf, sizeof(buf)));
5e54c602
DS
1144
1145 zsend_route_notify_owner(re, p, ZAPI_ROUTE_REMOVE_FAIL);
0c555cc6 1146 break;
ea1c14f6
MS
1147 case ZEBRA_DPLANE_STATUS_NONE:
1148 break;
0c555cc6
DS
1149 }
1150}
1151
6ae24471
DS
1152/* Update flag indicates whether this is a "replace" or not. Currently, this
1153 * is only used for IPv4.
1154 */
0c555cc6
DS
1155void rib_install_kernel(struct route_node *rn, struct route_entry *re,
1156 struct route_entry *old)
718e3744 1157{
d62a17ae 1158 struct nexthop *nexthop;
1159 rib_table_info_t *info = srcdest_rnode_table_info(rn);
86391e56 1160 const struct prefix *p, *src_p;
d62a17ae 1161 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
718e3744 1162
d62a17ae 1163 srcdest_rnode_prefixes(rn, &p, &src_p);
416ec78d 1164
d62a17ae 1165 if (info->safi != SAFI_UNICAST) {
7ee30f28 1166 for (ALL_NEXTHOPS(re->ng, nexthop))
d62a17ae 1167 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
0c555cc6 1168 return;
25b9cb0c
DL
1169 } else {
1170 struct nexthop *prev;
1171
7ee30f28 1172 for (ALL_NEXTHOPS(re->ng, nexthop)) {
25b9cb0c 1173 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_DUPLICATE);
7ee30f28 1174 for (ALL_NEXTHOPS(re->ng, prev)) {
25b9cb0c
DL
1175 if (prev == nexthop)
1176 break;
996c9314
LB
1177 if (nexthop_same_firsthop(nexthop, prev)) {
1178 SET_FLAG(nexthop->flags,
1179 NEXTHOP_FLAG_DUPLICATE);
25b9cb0c
DL
1180 break;
1181 }
1182 }
1183 }
d62a17ae 1184 }
718e3744 1185
2063a814
DS
1186 /*
1187 * If this is a replace to a new RE let the originator of the RE
1188 * know that they've lost
1189 */
9a9f8926 1190 if (old && (old != re) && (old->type != re->type))
28610f7e 1191 zsend_route_notify_owner(old, p, ZAPI_ROUTE_BETTER_ADMIN_WON);
25b9cb0c 1192
d62a17ae 1193 /*
1194 * Make sure we update the FPM any time we send new information to
1195 * the kernel.
1196 */
1197 hook_call(rib_update, rn, "installing in kernel");
1e885672 1198 switch (kernel_route_rib(rn, p, src_p, old, re)) {
ea1c14f6 1199 case ZEBRA_DPLANE_REQUEST_QUEUED:
af4c2728 1200 flog_err(
e914ccbe 1201 EC_ZEBRA_DP_INVALID_RC,
43e52561 1202 "No current known DataPlane interfaces can return this, please fix");
1e885672 1203 break;
ea1c14f6 1204 case ZEBRA_DPLANE_REQUEST_FAILURE:
af4c2728 1205 flog_err(
e914ccbe 1206 EC_ZEBRA_DP_INSTALL_FAIL,
43e52561 1207 "No current known Rib Install Failure cases, please fix");
1e885672 1208 break;
ea1c14f6 1209 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1e885672
DS
1210 zvrf->installs++;
1211 break;
1212 }
d62a17ae 1213
0c555cc6 1214 return;
718e3744 1215}
1216
1217/* Uninstall the route from kernel. */
0c555cc6 1218void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re)
718e3744 1219{
d62a17ae 1220 struct nexthop *nexthop;
1221 rib_table_info_t *info = srcdest_rnode_table_info(rn);
86391e56 1222 const struct prefix *p, *src_p;
d62a17ae 1223 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
05737783 1224
d62a17ae 1225 srcdest_rnode_prefixes(rn, &p, &src_p);
718e3744 1226
d62a17ae 1227 if (info->safi != SAFI_UNICAST) {
7ee30f28 1228 for (ALL_NEXTHOPS(re->ng, nexthop))
d6792f9d 1229 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
0c555cc6 1230 return;
d62a17ae 1231 }
416ec78d 1232
d62a17ae 1233 /*
1234 * Make sure we update the FPM any time we send new information to
1235 * the kernel.
1236 */
1237 hook_call(rib_update, rn, "uninstalling from kernel");
1e885672 1238 switch (kernel_route_rib(rn, p, src_p, re, NULL)) {
ea1c14f6 1239 case ZEBRA_DPLANE_REQUEST_QUEUED:
af4c2728 1240 flog_err(
e914ccbe 1241 EC_ZEBRA_DP_INVALID_RC,
43e52561 1242 "No current known DataPlane interfaces can return this, please fix");
1e885672 1243 break;
ea1c14f6 1244 case ZEBRA_DPLANE_REQUEST_FAILURE:
af4c2728 1245 flog_err(
e914ccbe 1246 EC_ZEBRA_DP_INSTALL_FAIL,
43e52561 1247 "No current known RIB Install Failure cases, please fix");
1e885672 1248 break;
ea1c14f6 1249 case ZEBRA_DPLANE_REQUEST_SUCCESS:
1e885672
DS
1250 if (zvrf)
1251 zvrf->removals++;
1252 break;
1253 }
718e3744 1254
0c555cc6 1255 return;
718e3744 1256}
1257
1258/* Uninstall the route from kernel. */
d62a17ae 1259static void rib_uninstall(struct route_node *rn, struct route_entry *re)
718e3744 1260{
d62a17ae 1261 rib_table_info_t *info = srcdest_rnode_table_info(rn);
5f7a4718 1262 rib_dest_t *dest = rib_dest_from_rnode(rn);
416ec78d 1263
5f7a4718 1264 if (dest && dest->selected_fib == re) {
d62a17ae 1265 if (info->safi == SAFI_UNICAST)
1266 hook_call(rib_update, rn, "rib_uninstall");
5adc2528 1267
d62a17ae 1268 if (!RIB_SYSTEM_ROUTE(re))
1269 rib_uninstall_kernel(rn, re);
a64448ba 1270
d62a17ae 1271 /* If labeled-unicast route, uninstall transit LSP. */
1272 if (zebra_rib_labeled_unicast(re))
1273 zebra_mpls_lsp_uninstall(info->zvrf, rn, re);
d62a17ae 1274 }
446bb95e 1275
d62a17ae 1276 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
86391e56
MS
1277 const struct prefix *p, *src_p;
1278
d62a17ae 1279 srcdest_rnode_prefixes(rn, &p, &src_p);
05737783 1280
d62a17ae 1281 redistribute_delete(p, src_p, re);
1282 UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
1283 }
718e3744 1284}
1285
9fd92e3c
AS
1286/*
1287 * rib_can_delete_dest
1288 *
1289 * Returns TRUE if the given dest can be deleted from the table.
1290 */
d62a17ae 1291static int rib_can_delete_dest(rib_dest_t *dest)
9fd92e3c 1292{
d62a17ae 1293 if (dest->routes) {
1294 return 0;
1295 }
9fd92e3c 1296
d62a17ae 1297 /*
1298 * Don't delete the dest if we have to update the FPM about this
1299 * prefix.
1300 */
1301 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)
1302 || CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM))
1303 return 0;
5adc2528 1304
d62a17ae 1305 return 1;
9fd92e3c
AS
1306}
1307
1308/*
1309 * rib_gc_dest
1310 *
1311 * Garbage collect the rib dest corresponding to the given route node
1312 * if appropriate.
1313 *
1314 * Returns TRUE if the dest was deleted, FALSE otherwise.
1315 */
d62a17ae 1316int rib_gc_dest(struct route_node *rn)
9fd92e3c 1317{
d62a17ae 1318 rib_dest_t *dest;
9fd92e3c 1319
d62a17ae 1320 dest = rib_dest_from_rnode(rn);
1321 if (!dest)
1322 return 0;
9fd92e3c 1323
d62a17ae 1324 if (!rib_can_delete_dest(dest))
1325 return 0;
9fd92e3c 1326
c9abf558
DS
1327 if (IS_ZEBRA_DEBUG_RIB) {
1328 struct zebra_vrf *zvrf;
1329
1330 zvrf = rib_dest_vrf(dest);
d62a17ae 1331 rnode_debug(rn, zvrf_id(zvrf), "removing dest from table");
c9abf558 1332 }
9fd92e3c 1333
d62a17ae 1334 dest->rnode = NULL;
1335 XFREE(MTYPE_RIB_DEST, dest);
1336 rn->info = NULL;
9fd92e3c 1337
d62a17ae 1338 /*
1339 * Release the one reference that we keep on the route node.
1340 */
1341 route_unlock_node(rn);
1342 return 1;
9fd92e3c
AS
1343}
1344
d62a17ae 1345static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn,
1346 struct route_entry *new)
3e5c6e00 1347{
5f7a4718
DS
1348 rib_dest_t *dest = rib_dest_from_rnode(rn);
1349
d62a17ae 1350 hook_call(rib_update, rn, "new route selected");
3e5c6e00 1351
d62a17ae 1352 /* Update real nexthop. This may actually determine if nexthop is active
1353 * or not. */
1354 if (!nexthop_active_update(rn, new, 1)) {
1355 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1356 return;
1357 }
3e5c6e00 1358
d62a17ae 1359 if (IS_ZEBRA_DEBUG_RIB) {
1360 char buf[SRCDEST2STR_BUFFER];
1361 srcdest_rnode2str(rn, buf, sizeof(buf));
1362 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1363 zvrf_id(zvrf), buf, rn, new, new->type);
1364 }
3e5c6e00 1365
d62a17ae 1366 /* If labeled-unicast route, install transit LSP. */
1367 if (zebra_rib_labeled_unicast(new))
1368 zebra_mpls_lsp_install(zvrf, rn, new);
a64448ba 1369
0c555cc6
DS
1370 if (!RIB_SYSTEM_ROUTE(new))
1371 rib_install_kernel(rn, new, NULL);
ed216282
DS
1372 else
1373 dest->selected_fib = new;
3e5c6e00 1374
d62a17ae 1375 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 1376}
1377
d62a17ae 1378static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn,
1379 struct route_entry *old)
3e5c6e00 1380{
5f7a4718 1381 rib_dest_t *dest = rib_dest_from_rnode(rn);
d62a17ae 1382 hook_call(rib_update, rn, "removing existing route");
3e5c6e00 1383
d62a17ae 1384 /* Uninstall from kernel. */
1385 if (IS_ZEBRA_DEBUG_RIB) {
1386 char buf[SRCDEST2STR_BUFFER];
1387 srcdest_rnode2str(rn, buf, sizeof(buf));
1388 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1389 zvrf_id(zvrf), buf, rn, old, old->type);
1390 }
3e5c6e00 1391
d62a17ae 1392 /* If labeled-unicast route, uninstall transit LSP. */
1393 if (zebra_rib_labeled_unicast(old))
1394 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1395
1396 if (!RIB_SYSTEM_ROUTE(old))
1397 rib_uninstall_kernel(rn, old);
ed216282
DS
1398 else {
1399 /*
1400 * We are setting this to NULL here
1401 * because that is what we traditionally
1402 * have been doing. I am not positive
1403 * that this is the right thing to do
1404 * but let's leave the code alone
1405 * for the RIB_SYSTEM_ROUTE case
1406 */
1407 dest->selected_fib = NULL;
1408 }
d62a17ae 1409
1410 /* Update nexthop for route, reset changed flag. */
1411 nexthop_active_update(rn, old, 1);
1412 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
1413}
1414
1415static void rib_process_update_fib(struct zebra_vrf *zvrf,
1416 struct route_node *rn,
1417 struct route_entry *old,
1418 struct route_entry *new)
1419{
1420 struct nexthop *nexthop = NULL;
1421 int nh_active = 0;
5f7a4718 1422 rib_dest_t *dest = rib_dest_from_rnode(rn);
d62a17ae 1423
1424 /*
1425 * We have to install or update if a new route has been selected or
1426 * something has changed.
1427 */
1428 if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) {
1429 hook_call(rib_update, rn, "updating existing route");
1430
1431 /* Update the nexthop; we could determine here that nexthop is
1432 * inactive. */
1433 if (nexthop_active_update(rn, new, 1))
1434 nh_active = 1;
1435
1436 /* If nexthop is active, install the selected route, if
1437 * appropriate. If
1438 * the install succeeds, cleanup flags for prior route, if
1439 * different from
1440 * newly selected.
1441 */
1442 if (nh_active) {
1443 if (IS_ZEBRA_DEBUG_RIB) {
1444 char buf[SRCDEST2STR_BUFFER];
1445 srcdest_rnode2str(rn, buf, sizeof(buf));
1446 if (new != old)
1447 zlog_debug(
1448 "%u:%s: Updating route rn %p, re %p (type %d) "
1449 "old %p (type %d)",
1450 zvrf_id(zvrf), buf, rn, new,
1451 new->type, old, old->type);
1452 else
1453 zlog_debug(
1454 "%u:%s: Updating route rn %p, re %p (type %d)",
1455 zvrf_id(zvrf), buf, rn, new,
1456 new->type);
1457 }
1458
1459 /* If labeled-unicast route, uninstall transit LSP. */
1460 if (zebra_rib_labeled_unicast(old))
1461 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1462
1463 /* Non-system route should be installed. */
1464 if (!RIB_SYSTEM_ROUTE(new)) {
1465 /* If labeled-unicast route, install transit
1466 * LSP. */
1467 if (zebra_rib_labeled_unicast(new))
1468 zebra_mpls_lsp_install(zvrf, rn, new);
1469
0c555cc6 1470 rib_install_kernel(rn, new, old);
ed216282
DS
1471 } else {
1472 /*
1473 * We do not need to install the
1474 * selected route because it
1475 * is already isntalled by
1476 * the system( ie not us )
1477 * so just mark it as winning
1478 * we do need to ensure that
1479 * if we uninstall a route
1480 * from ourselves we don't
1481 * over write this pointer
1482 */
1483 dest->selected_fib = NULL;
d62a17ae 1484 }
d62a17ae 1485 /* If install succeeded or system route, cleanup flags
1486 * for prior route. */
ed216282 1487 if (new != old) {
d62a17ae 1488 if (RIB_SYSTEM_ROUTE(new)) {
1489 if (!RIB_SYSTEM_ROUTE(old))
1490 rib_uninstall_kernel(rn, old);
1491 } else {
7ee30f28 1492 for (nexthop = old->ng.nexthop; nexthop;
d62a17ae 1493 nexthop = nexthop->next)
1494 UNSET_FLAG(nexthop->flags,
1495 NEXTHOP_FLAG_FIB);
1496 }
1497 }
d62a17ae 1498 }
a64448ba 1499
d62a17ae 1500 /*
1501 * If nexthop for selected route is not active or install
1502 * failed, we
1503 * may need to uninstall and delete for redistribution.
1504 */
ed216282 1505 if (!nh_active) {
d62a17ae 1506 if (IS_ZEBRA_DEBUG_RIB) {
1507 char buf[SRCDEST2STR_BUFFER];
1508 srcdest_rnode2str(rn, buf, sizeof(buf));
1509 if (new != old)
1510 zlog_debug(
1511 "%u:%s: Deleting route rn %p, re %p (type %d) "
0cfbff74 1512 "old %p (type %d) - nexthop inactive",
d62a17ae 1513 zvrf_id(zvrf), buf, rn, new,
0cfbff74 1514 new->type, old, old->type);
d62a17ae 1515 else
1516 zlog_debug(
0cfbff74 1517 "%u:%s: Deleting route rn %p, re %p (type %d) - nexthop inactive",
d62a17ae 1518 zvrf_id(zvrf), buf, rn, new,
0cfbff74 1519 new->type);
d62a17ae 1520 }
1521
1522 /* If labeled-unicast route, uninstall transit LSP. */
1523 if (zebra_rib_labeled_unicast(old))
1524 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1525
1526 if (!RIB_SYSTEM_ROUTE(old))
1527 rib_uninstall_kernel(rn, old);
ed216282
DS
1528 else
1529 dest->selected_fib = NULL;
d62a17ae 1530 }
1531 } else {
1532 /*
1533 * Same route selected; check if in the FIB and if not,
1534 * re-install. This
1535 * is housekeeping code to deal with race conditions in kernel
1536 * with linux
1537 * netlink reporting interface up before IPv4 or IPv6 protocol
1538 * is ready
1539 * to add routes.
1540 */
1541 if (!RIB_SYSTEM_ROUTE(new)) {
ed216282 1542 bool in_fib = false;
d62a17ae 1543
7ee30f28 1544 for (ALL_NEXTHOPS(new->ng, nexthop))
d62a17ae 1545 if (CHECK_FLAG(nexthop->flags,
1546 NEXTHOP_FLAG_FIB)) {
ed216282 1547 in_fib = true;
d62a17ae 1548 break;
1549 }
1550 if (!in_fib)
1551 rib_install_kernel(rn, new, NULL);
1552 }
1553 }
1554
1555 /* Update prior route. */
1556 if (new != old) {
d62a17ae 1557 /* Set real nexthop. */
1558 nexthop_active_update(rn, old, 1);
1559 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
1560 }
3e5c6e00 1561
d62a17ae 1562 /* Clear changed flag. */
1563 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
3e5c6e00 1564}
1565
d62a17ae 1566/* Check if 'alternate' RIB entry is better than 'current'. */
1567static struct route_entry *rib_choose_best(struct route_entry *current,
1568 struct route_entry *alternate)
1569{
1570 if (current == NULL)
1571 return alternate;
1572
1573 /* filter route selection in following order:
1574 * - connected beats other types
fec4ca19 1575 * - if both connected, loopback or vrf wins
d62a17ae 1576 * - lower distance beats higher
1577 * - lower metric beats higher for equal distance
1578 * - last, hence oldest, route wins tie break.
1579 */
1580
fec4ca19
DS
1581 /* Connected routes. Check to see if either are a vrf
1582 * or loopback interface. If not, pick the last connected
d62a17ae 1583 * route of the set of lowest metric connected routes.
1584 */
1585 if (alternate->type == ZEBRA_ROUTE_CONNECT) {
fec4ca19
DS
1586 if (current->type != ZEBRA_ROUTE_CONNECT)
1587 return alternate;
1588
1589 /* both are connected. are either loop or vrf? */
1590 struct nexthop *nexthop = NULL;
1591
1592 for (ALL_NEXTHOPS(alternate->ng, nexthop)) {
1593 if (if_is_loopback_or_vrf(if_lookup_by_index(
1594 nexthop->ifindex, alternate->vrf_id)))
1595 return alternate;
1596 }
1597
1598 for (ALL_NEXTHOPS(current->ng, nexthop)) {
1599 if (if_is_loopback_or_vrf(if_lookup_by_index(
1600 nexthop->ifindex, current->vrf_id)))
1601 return current;
1602 }
1603
1604 /* Neither are loop or vrf so pick best metric */
1605 if (alternate->metric <= current->metric)
d62a17ae 1606 return alternate;
1607
1608 return current;
1609 }
3e5c6e00 1610
d62a17ae 1611 if (current->type == ZEBRA_ROUTE_CONNECT)
1612 return current;
3e5c6e00 1613
d62a17ae 1614 /* higher distance loses */
1615 if (alternate->distance < current->distance)
1616 return alternate;
1617 if (current->distance < alternate->distance)
1618 return current;
3e5c6e00 1619
d62a17ae 1620 /* metric tie-breaks equal distance */
1621 if (alternate->metric <= current->metric)
1622 return alternate;
3e5c6e00 1623
d62a17ae 1624 return current;
3e5c6e00 1625}
1626
d62a17ae 1627/* Core function for processing routing information base. */
1628static void rib_process(struct route_node *rn)
1629{
1630 struct route_entry *re;
1631 struct route_entry *next;
1632 struct route_entry *old_selected = NULL;
1633 struct route_entry *new_selected = NULL;
1634 struct route_entry *old_fib = NULL;
1635 struct route_entry *new_fib = NULL;
1636 struct route_entry *best = NULL;
1637 char buf[SRCDEST2STR_BUFFER];
1638 rib_dest_t *dest;
1639 struct zebra_vrf *zvrf = NULL;
86391e56
MS
1640 const struct prefix *p, *src_p;
1641
d62a17ae 1642 srcdest_rnode_prefixes(rn, &p, &src_p);
1643 vrf_id_t vrf_id = VRF_UNKNOWN;
1644
1645 assert(rn);
1646
1647 dest = rib_dest_from_rnode(rn);
1648 if (dest) {
1649 zvrf = rib_dest_vrf(dest);
1650 vrf_id = zvrf_id(zvrf);
1651 }
bab85d4f 1652
d62a17ae 1653 if (IS_ZEBRA_DEBUG_RIB)
1654 srcdest_rnode2str(rn, buf, sizeof(buf));
bab85d4f 1655
d62a17ae 1656 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1657 zlog_debug("%u:%s: Processing rn %p", vrf_id, buf, rn);
bab85d4f 1658
607425e5
DS
1659 /*
1660 * we can have rn's that have a NULL info pointer
1661 * (dest). As such let's not let the deref happen
1662 * additionally we know RNODE_FOREACH_RE_SAFE
1663 * will not iterate so we are ok.
1664 */
1665 if (dest)
1666 old_fib = dest->selected_fib;
5f7a4718 1667
a2addae8 1668 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 1669 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1670 zlog_debug(
1671 "%u:%s: Examine re %p (type %d) status %x flags %x "
1672 "dist %d metric %d",
1673 vrf_id, buf, re, re->type, re->status,
1674 re->flags, re->distance, re->metric);
1675
1676 UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1677
1678 /* Currently selected re. */
1679 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1680 assert(old_selected == NULL);
1681 old_selected = re;
1682 }
bab85d4f 1683
d62a17ae 1684 /* Skip deleted entries from selection */
1685 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1686 continue;
1687
1688 /* Skip unreachable nexthop. */
1689 /* This first call to nexthop_active_update is merely to
1690 * determine if
1691 * there's any change to nexthops associated with this RIB
1692 * entry. Now,
1693 * rib_process() can be invoked due to an external event such as
1694 * link
1695 * down or due to next-hop-tracking evaluation. In the latter
1696 * case,
1697 * a decision has already been made that the NHs have changed.
1698 * So, no
1699 * need to invoke a potentially expensive call again. Further,
1700 * since
1701 * the change might be in a recursive NH which is not caught in
1702 * the nexthop_active_update() code. Thus, we might miss changes
1703 * to
1704 * recursive NHs.
1705 */
1706 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)
1707 && !nexthop_active_update(rn, re, 0)) {
1708 if (re->type == ZEBRA_ROUTE_TABLE) {
1709 /* XXX: HERE BE DRAGONS!!!!!
1710 * In all honesty, I have not yet figured out
1711 * what this part
1712 * does or why the ROUTE_ENTRY_CHANGED test
1713 * above is correct
1714 * or why we need to delete a route here, and
1715 * also not whether
1716 * this concerns both selected and fib route, or
1717 * only selected
1718 * or only fib */
1719 /* This entry was denied by the 'ip protocol
1720 * table' route-map, we
1721 * need to delete it */
1722 if (re != old_selected) {
1723 if (IS_ZEBRA_DEBUG_RIB)
1724 zlog_debug(
32391aff 1725 "%s: %u:%s: imported via import-table but denied "
d62a17ae 1726 "by the ip protocol table route-map",
32391aff 1727 __func__, vrf_id, buf);
d62a17ae 1728 rib_unlink(rn, re);
1729 } else
1730 SET_FLAG(re->status,
1731 ROUTE_ENTRY_REMOVED);
1732 }
1733
1734 continue;
1735 }
bab85d4f 1736
d62a17ae 1737 /* Infinite distance. */
1738 if (re->distance == DISTANCE_INFINITY) {
1739 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1740 continue;
1741 }
bab85d4f 1742
d62a17ae 1743 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1744 best = rib_choose_best(new_fib, re);
1745 if (new_fib && best != new_fib)
1746 UNSET_FLAG(new_fib->status,
1747 ROUTE_ENTRY_CHANGED);
1748 new_fib = best;
1749 } else {
1750 best = rib_choose_best(new_selected, re);
1751 if (new_selected && best != new_selected)
1752 UNSET_FLAG(new_selected->status,
1753 ROUTE_ENTRY_CHANGED);
1754 new_selected = best;
1755 }
1756 if (best != re)
1757 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1758 } /* RNODE_FOREACH_RE */
1759
1760 /* If no FIB override route, use the selected route also for FIB */
1761 if (new_fib == NULL)
1762 new_fib = new_selected;
1763
1764 /* After the cycle is finished, the following pointers will be set:
1765 * old_selected --- RE entry currently having SELECTED
1766 * new_selected --- RE entry that is newly SELECTED
1767 * old_fib --- RE entry currently in kernel FIB
1768 * new_fib --- RE entry that is newly to be in kernel FIB
1769 *
1770 * new_selected will get SELECTED flag, and is going to be redistributed
1771 * the zclients. new_fib (which can be new_selected) will be installed
1772 * in kernel.
1773 */
1774
1775 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1776 zlog_debug(
1777 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1778 vrf_id, buf, (void *)old_selected, (void *)new_selected,
1779 (void *)old_fib, (void *)new_fib);
1780 }
446bb95e 1781
d62a17ae 1782 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1783 * fib == selected */
9d303b37
DL
1784 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1785 ROUTE_ENTRY_CHANGED);
d62a17ae 1786
1787 /* Update fib according to selection results */
1788 if (new_fib && old_fib)
1789 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1790 else if (new_fib)
1791 rib_process_add_fib(zvrf, rn, new_fib);
1792 else if (old_fib)
1793 rib_process_del_fib(zvrf, rn, old_fib);
1794
1795 /* Redistribute SELECTED entry */
1796 if (old_selected != new_selected || selected_changed) {
7ee30f28 1797 struct nexthop *nexthop = NULL;
d62a17ae 1798
1799 /* Check if we have a FIB route for the destination, otherwise,
1800 * don't redistribute it */
7ee30f28
DS
1801 if (new_fib) {
1802 for (ALL_NEXTHOPS(new_fib->ng, nexthop)) {
1803 if (CHECK_FLAG(nexthop->flags,
1804 NEXTHOP_FLAG_FIB)) {
1805 break;
1806 }
d62a17ae 1807 }
1808 }
1809 if (!nexthop)
1810 new_selected = NULL;
93bdadae 1811
d62a17ae 1812 if (new_selected && new_selected != new_fib) {
1813 nexthop_active_update(rn, new_selected, 1);
1814 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
1815 }
41ec9222 1816
d62a17ae 1817 if (old_selected) {
1818 if (!new_selected)
1819 redistribute_delete(p, src_p, old_selected);
1820 if (old_selected != new_selected)
1821 UNSET_FLAG(old_selected->flags,
1822 ZEBRA_FLAG_SELECTED);
f857321e 1823 }
446bb95e 1824
d62a17ae 1825 if (new_selected) {
1826 /* Install new or replace existing redistributed entry
1827 */
1828 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1829 redistribute_update(p, src_p, new_selected,
1830 old_selected);
1831 }
1832 }
3e5c6e00 1833
d62a17ae 1834 /* Remove all RE entries queued for removal */
a2addae8 1835 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 1836 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1837 if (IS_ZEBRA_DEBUG_RIB) {
1838 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1839 (void *)rn, (void *)re);
1840 }
1841 rib_unlink(rn, re);
1842 }
1843 }
4d38fdb4 1844
d62a17ae 1845 /*
1846 * Check if the dest can be deleted now.
1847 */
1848 rib_gc_dest(rn);
e96f9203
DO
1849}
1850
5110a0c6 1851/* Take a list of route_node structs and return 1, if there was a record
d62a17ae 1852 * picked from it and processed by rib_process(). Don't process more,
5110a0c6 1853 * than one RN record; operate only in the specified sub-queue.
e96f9203 1854 */
d7c0a89a 1855static unsigned int process_subq(struct list *subq, uint8_t qindex)
e96f9203 1856{
d62a17ae 1857 struct listnode *lnode = listhead(subq);
1858 struct route_node *rnode;
1859 rib_dest_t *dest;
1860 struct zebra_vrf *zvrf = NULL;
5110a0c6 1861
d62a17ae 1862 if (!lnode)
1863 return 0;
5110a0c6 1864
d62a17ae 1865 rnode = listgetdata(lnode);
1866 dest = rib_dest_from_rnode(rnode);
1867 if (dest)
1868 zvrf = rib_dest_vrf(dest);
41ec9222 1869
d62a17ae 1870 rib_process(rnode);
5110a0c6 1871
d62a17ae 1872 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1873 char buf[SRCDEST2STR_BUFFER];
1874 srcdest_rnode2str(rnode, buf, sizeof(buf));
1875 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1876 zvrf ? zvrf_id(zvrf) : 0, buf, rnode, qindex);
1877 }
41ec9222 1878
d62a17ae 1879 if (rnode->info)
1880 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
1881 RIB_ROUTE_QUEUED(qindex));
9fd92e3c 1882
67b9467f 1883#if 0
5110a0c6
SH
1884 else
1885 {
1886 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1887 __func__, rnode, rnode->lock);
1888 zlog_backtrace(LOG_DEBUG);
1889 }
67b9467f 1890#endif
d62a17ae 1891 route_unlock_node(rnode);
1892 list_delete_node(subq, lnode);
1893 return 1;
e96f9203
DO
1894}
1895
fb018d25
DS
1896/*
1897 * All meta queues have been processed. Trigger next-hop evaluation.
1898 */
d62a17ae 1899static void meta_queue_process_complete(struct work_queue *dummy)
fb018d25 1900{
d62a17ae 1901 struct vrf *vrf;
1902 struct zebra_vrf *zvrf;
9ec6b0bb 1903
d62a17ae 1904 /* Evaluate nexthops for those VRFs which underwent route processing.
1905 * This
1906 * should limit the evaluation to the necessary VRFs in most common
1907 * situations.
1908 */
a2addae8 1909 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
d62a17ae 1910 zvrf = vrf->info;
1911 if (zvrf == NULL || !(zvrf->flags & ZEBRA_VRF_RIB_SCHEDULED))
1912 continue;
1913
1914 zvrf->flags &= ~ZEBRA_VRF_RIB_SCHEDULED;
6d53d7b1 1915 zebra_evaluate_rnh(zvrf, AF_INET, 0, RNH_NEXTHOP_TYPE, NULL);
1916 zebra_evaluate_rnh(zvrf, AF_INET, 0, RNH_IMPORT_CHECK_TYPE,
d62a17ae 1917 NULL);
6d53d7b1 1918 zebra_evaluate_rnh(zvrf, AF_INET6, 0, RNH_NEXTHOP_TYPE, NULL);
1919 zebra_evaluate_rnh(zvrf, AF_INET6, 0, RNH_IMPORT_CHECK_TYPE,
d62a17ae 1920 NULL);
d62a17ae 1921 }
939fba27 1922
d62a17ae 1923 /* Schedule LSPs for processing, if needed. */
1924 zvrf = vrf_info_lookup(VRF_DEFAULT);
1925 if (mpls_should_lsps_be_processed(zvrf)) {
1926 if (IS_ZEBRA_DEBUG_MPLS)
1927 zlog_debug(
1928 "%u: Scheduling all LSPs upon RIB completion",
1929 zvrf_id(zvrf));
1930 zebra_mpls_lsp_schedule(zvrf);
1931 mpls_unmark_lsps_for_processing(zvrf);
1932 }
fb018d25
DS
1933}
1934
e96f9203 1935/* Dispatch the meta queue by picking, processing and unlocking the next RN from
d62a17ae 1936 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
1937 * data
e96f9203
DO
1938 * is pointed to the meta queue structure.
1939 */
d62a17ae 1940static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
e96f9203 1941{
d62a17ae 1942 struct meta_queue *mq = data;
1943 unsigned i;
5110a0c6 1944
d62a17ae 1945 for (i = 0; i < MQ_SIZE; i++)
1946 if (process_subq(mq->subq[i], i)) {
1947 mq->size--;
1948 break;
1949 }
1950 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
e96f9203
DO
1951}
1952
9fd92e3c
AS
1953/*
1954 * Map from rib types to queue type (priority) in meta queue
1955 */
d7c0a89a
QY
1956static const uint8_t meta_queue_map[ZEBRA_ROUTE_MAX] = {
1957 [ZEBRA_ROUTE_SYSTEM] = 4,
1958 [ZEBRA_ROUTE_KERNEL] = 0,
1959 [ZEBRA_ROUTE_CONNECT] = 0,
1960 [ZEBRA_ROUTE_STATIC] = 1,
1961 [ZEBRA_ROUTE_RIP] = 2,
1962 [ZEBRA_ROUTE_RIPNG] = 2,
1963 [ZEBRA_ROUTE_OSPF] = 2,
1964 [ZEBRA_ROUTE_OSPF6] = 2,
1965 [ZEBRA_ROUTE_ISIS] = 2,
1966 [ZEBRA_ROUTE_BGP] = 3,
1967 [ZEBRA_ROUTE_PIM] = 4, // Shouldn't happen but for safety
1968 [ZEBRA_ROUTE_EIGRP] = 2,
1969 [ZEBRA_ROUTE_NHRP] = 2,
1970 [ZEBRA_ROUTE_HSLS] = 4,
1971 [ZEBRA_ROUTE_OLSR] = 4,
1972 [ZEBRA_ROUTE_TABLE] = 1,
1973 [ZEBRA_ROUTE_LDP] = 4,
1974 [ZEBRA_ROUTE_VNC] = 3,
1975 [ZEBRA_ROUTE_VNC_DIRECT] = 3,
1976 [ZEBRA_ROUTE_VNC_DIRECT_RH] = 3,
1977 [ZEBRA_ROUTE_BGP_DIRECT] = 3,
1978 [ZEBRA_ROUTE_BGP_DIRECT_EXT] = 3,
1979 [ZEBRA_ROUTE_BABEL] = 2,
1980 [ZEBRA_ROUTE_ALL] = 4, // Shouldn't happen but for safety
5110a0c6
SH
1981};
1982
1983/* Look into the RN and queue it into one or more priority queues,
1984 * increasing the size for each data push done.
e96f9203 1985 */
d62a17ae 1986static void rib_meta_queue_add(struct meta_queue *mq, struct route_node *rn)
e96f9203 1987{
d62a17ae 1988 struct route_entry *re;
5110a0c6 1989
a2addae8 1990 RNODE_FOREACH_RE (rn, re) {
d7c0a89a 1991 uint8_t qindex = meta_queue_map[re->type];
d62a17ae 1992 struct zebra_vrf *zvrf;
1993
1994 /* Invariant: at this point we always have rn->info set. */
1995 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
1996 RIB_ROUTE_QUEUED(qindex))) {
1997 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1998 rnode_debug(
1999 rn, re->vrf_id,
2000 "rn %p is already queued in sub-queue %u",
2001 (void *)rn, qindex);
2002 continue;
2003 }
5110a0c6 2004
d62a17ae 2005 SET_FLAG(rib_dest_from_rnode(rn)->flags,
2006 RIB_ROUTE_QUEUED(qindex));
2007 listnode_add(mq->subq[qindex], rn);
2008 route_lock_node(rn);
2009 mq->size++;
5110a0c6 2010
d62a17ae 2011 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2012 rnode_debug(rn, re->vrf_id,
2013 "queued rn %p into sub-queue %u",
2014 (void *)rn, qindex);
9ec6b0bb 2015
d62a17ae 2016 zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
2017 if (zvrf)
2018 zvrf->flags |= ZEBRA_VRF_RIB_SCHEDULED;
2019 }
4d38fdb4 2020}
2021
6d691129 2022/* Add route_node to work queue and schedule processing */
d62a17ae 2023void rib_queue_add(struct route_node *rn)
4d38fdb4 2024{
d62a17ae 2025 assert(rn);
fc328ac9 2026
d62a17ae 2027 /* Pointless to queue a route_node with no RIB entries to add or remove
2028 */
2029 if (!rnode_to_ribs(rn)) {
2030 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2031 __func__, (void *)rn, rn->lock);
2032 zlog_backtrace(LOG_DEBUG);
2033 return;
2034 }
4d38fdb4 2035
d62a17ae 2036 if (zebrad.ribq == NULL) {
e914ccbe 2037 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2038 "%s: work_queue does not exist!", __func__);
d62a17ae 2039 return;
2040 }
2041
2042 /*
2043 * The RIB queue should normally be either empty or holding the only
2044 * work_queue_item element. In the latter case this element would
2045 * hold a pointer to the meta queue structure, which must be used to
2046 * actually queue the route nodes to process. So create the MQ
2047 * holder, if necessary, then push the work into it in any case.
2048 * This semantics was introduced after 0.99.9 release.
2049 */
f104f6c1 2050 if (work_queue_empty(zebrad.ribq))
d62a17ae 2051 work_queue_add(zebrad.ribq, zebrad.mq);
e96f9203 2052
d62a17ae 2053 rib_meta_queue_add(zebrad.mq, rn);
fc328ac9 2054
d62a17ae 2055 return;
4d38fdb4 2056}
2057
5110a0c6
SH
2058/* Create new meta queue.
2059 A destructor function doesn't seem to be necessary here.
2060 */
d62a17ae 2061static struct meta_queue *meta_queue_new(void)
e96f9203 2062{
d62a17ae 2063 struct meta_queue *new;
2064 unsigned i;
5110a0c6 2065
d62a17ae 2066 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
e96f9203 2067
d62a17ae 2068 for (i = 0; i < MQ_SIZE; i++) {
2069 new->subq[i] = list_new();
2070 assert(new->subq[i]);
2071 }
5110a0c6 2072
d62a17ae 2073 return new;
e96f9203
DO
2074}
2075
d62a17ae 2076void meta_queue_free(struct meta_queue *mq)
5a8dfcd8 2077{
d62a17ae 2078 unsigned i;
5a8dfcd8 2079
d62a17ae 2080 for (i = 0; i < MQ_SIZE; i++)
6a154c88 2081 list_delete(&mq->subq[i]);
5a8dfcd8 2082
d62a17ae 2083 XFREE(MTYPE_WORK_QUEUE, mq);
5a8dfcd8
RW
2084}
2085
4d38fdb4 2086/* initialise zebra rib work queue */
d62a17ae 2087static void rib_queue_init(struct zebra_t *zebra)
4d38fdb4 2088{
d62a17ae 2089 assert(zebra);
2090
2091 if (!(zebra->ribq =
2092 work_queue_new(zebra->master, "route_node processing"))) {
e914ccbe 2093 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2094 "%s: could not initialise work queue!", __func__);
d62a17ae 2095 return;
2096 }
4d38fdb4 2097
d62a17ae 2098 /* fill in the work queue spec */
2099 zebra->ribq->spec.workfunc = &meta_queue_process;
2100 zebra->ribq->spec.errorfunc = NULL;
2101 zebra->ribq->spec.completion_func = &meta_queue_process_complete;
2102 /* XXX: TODO: These should be runtime configurable via vty */
2103 zebra->ribq->spec.max_retries = 3;
3a30f50f 2104 zebra->ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
d62a17ae 2105
2106 if (!(zebra->mq = meta_queue_new())) {
e914ccbe 2107 flog_err(EC_ZEBRA_WQ_NONEXISTENT,
1c50c1c0 2108 "%s: could not initialise meta queue!", __func__);
d62a17ae 2109 return;
2110 }
2111 return;
718e3744 2112}
2113
6d691129
PJ
2114/* RIB updates are processed via a queue of pointers to route_nodes.
2115 *
2116 * The queue length is bounded by the maximal size of the routing table,
2117 * as a route_node will not be requeued, if already queued.
2118 *
f0f77c9a
DS
2119 * REs are submitted via rib_addnode or rib_delnode which set minimal
2120 * state, or static_install_route (when an existing RE is updated)
3c0755dc 2121 * and then submit route_node to queue for best-path selection later.
f0f77c9a 2122 * Order of add/delete state changes are preserved for any given RE.
6d691129 2123 *
f0f77c9a 2124 * Deleted REs are reaped during best-path selection.
6d691129
PJ
2125 *
2126 * rib_addnode
f0f77c9a
DS
2127 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2128 * |-------->| | best RE, if required
3c0755dc
PJ
2129 * | |
2130 * static_install->|->rib_addqueue...... -> rib_process
2131 * | |
2132 * |-------->| |-> rib_unlink
f0f77c9a
DS
2133 * |-> set ROUTE_ENTRY_REMOVE |
2134 * rib_delnode (RE freed)
6d691129 2135 *
9fd92e3c
AS
2136 * The 'info' pointer of a route_node points to a rib_dest_t
2137 * ('dest'). Queueing state for a route_node is kept on the dest. The
2138 * dest is created on-demand by rib_link() and is kept around at least
2139 * as long as there are ribs hanging off it (@see rib_gc_dest()).
d62a17ae 2140 *
6d691129
PJ
2141 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2142 *
2143 * - route_nodes: refcounted by:
9fd92e3c
AS
2144 * - dest attached to route_node:
2145 * - managed by: rib_link/rib_gc_dest
6d691129
PJ
2146 * - route_node processing queue
2147 * - managed by: rib_addqueue, rib_process.
2148 *
2149 */
d62a17ae 2150
f0f77c9a 2151/* Add RE to head of the route node. */
d62a17ae 2152static void rib_link(struct route_node *rn, struct route_entry *re, int process)
2153{
2154 struct route_entry *head;
2155 rib_dest_t *dest;
2156 afi_t afi;
2157 const char *rmap_name;
9fd92e3c 2158
d62a17ae 2159 assert(re && rn);
9fd92e3c 2160
d62a17ae 2161 dest = rib_dest_from_rnode(rn);
2162 if (!dest) {
2163 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2164 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
7a4bb9c5 2165
d62a17ae 2166 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
2167 route_lock_node(rn); /* rn route table reference */
2168 rn->info = dest;
2169 dest->rnode = rn;
2170 }
2263a412 2171
d62a17ae 2172 head = dest->routes;
2173 if (head) {
2174 head->prev = re;
2175 }
2176 re->next = head;
2177 dest->routes = re;
2178
2179 afi = (rn->p.family == AF_INET)
2180 ? AFI_IP
2181 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2182 if (is_zebra_import_table_enabled(afi, re->table)) {
2183 rmap_name = zebra_get_import_table_route_map(afi, re->table);
2184 zebra_add_import_table_entry(rn, re, rmap_name);
2185 } else if (process)
2186 rib_queue_add(rn);
2187}
2188
7e24fdf3
DS
2189static void rib_addnode(struct route_node *rn,
2190 struct route_entry *re, int process)
d62a17ae 2191{
2192 /* RE node has been un-removed before route-node is processed.
2193 * route_node must hence already be on the queue for processing..
2194 */
2195 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
2196 if (IS_ZEBRA_DEBUG_RIB)
2197 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
2198 (void *)rn, (void *)re);
2199
2200 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2201 return;
2202 }
2203 rib_link(rn, re, process);
6d691129
PJ
2204}
2205
9fd92e3c
AS
2206/*
2207 * rib_unlink
2208 *
2209 * Detach a rib structure from a route_node.
2210 *
2211 * Note that a call to rib_unlink() should be followed by a call to
2212 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2213 * longer required to be deleted.
2214 */
d62a17ae 2215void rib_unlink(struct route_node *rn, struct route_entry *re)
6d691129 2216{
d62a17ae 2217 rib_dest_t *dest;
9fd92e3c 2218
d62a17ae 2219 assert(rn && re);
6d691129 2220
d62a17ae 2221 if (IS_ZEBRA_DEBUG_RIB)
2222 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
2223 (void *)re);
6d691129 2224
d62a17ae 2225 dest = rib_dest_from_rnode(rn);
6d691129 2226
d62a17ae 2227 if (re->next)
2228 re->next->prev = re->prev;
6d691129 2229
d62a17ae 2230 if (re->prev)
2231 re->prev->next = re->next;
2232 else {
2233 dest->routes = re->next;
2234 }
7a4bb9c5 2235
2eb07de3
DS
2236 if (dest->selected_fib == re)
2237 dest->selected_fib = NULL;
2238
7ee30f28 2239 nexthops_free(re->ng.nexthop);
d62a17ae 2240 XFREE(MTYPE_RE, re);
2241}
2242
2243void rib_delnode(struct route_node *rn, struct route_entry *re)
2244{
2245 afi_t afi;
2246
2247 if (IS_ZEBRA_DEBUG_RIB)
2248 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
2249 (void *)rn, (void *)re);
2250 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2251
2252 afi = (rn->p.family == AF_INET)
2253 ? AFI_IP
2254 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2255 if (is_zebra_import_table_enabled(afi, re->table)) {
2256 zebra_del_import_table_entry(rn, re);
2257 /* Just clean up if non main table */
2258 if (IS_ZEBRA_DEBUG_RIB) {
2259 char buf[SRCDEST2STR_BUFFER];
2260 srcdest_rnode2str(rn, buf, sizeof(buf));
2261 zlog_debug(
2262 "%u:%s: Freeing route rn %p, re %p (type %d)",
2263 re->vrf_id, buf, rn, re, re->type);
2264 }
7a4bb9c5 2265
d62a17ae 2266 rib_unlink(rn, re);
2267 } else {
2268 rib_queue_add(rn);
2269 }
718e3744 2270}
2271
f0f77c9a 2272/* This function dumps the contents of a given RE entry into
dc95824a
DO
2273 * standard debug log. Calling function name and IP prefix in
2274 * question are passed as 1st and 2nd arguments.
2275 */
2276
d62a17ae 2277void _route_entry_dump(const char *func, union prefixconstptr pp,
2278 union prefixconstptr src_pp,
2279 const struct route_entry *re)
2280{
d62a17ae 2281 const struct prefix *src_p = src_pp.p;
2282 bool is_srcdst = src_p && src_p->prefixlen;
2283 char straddr[PREFIX_STRLEN];
2284 char srcaddr[PREFIX_STRLEN];
2285 struct nexthop *nexthop;
2286
2287 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func,
2288 (const void *)re, prefix2str(pp, straddr, sizeof(straddr)),
2289 is_srcdst ? " from " : "",
2290 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
2291 : "",
2292 re->vrf_id);
cc54cfee
RW
2293 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2294 func, (unsigned long)re->uptime, re->type, re->instance,
2295 re->table);
d62a17ae 2296 zlog_debug(
2297 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2298 func, re->metric, re->mtu, re->distance, re->flags, re->status);
2299 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func,
2300 re->nexthop_num, re->nexthop_active_num);
2301
7ee30f28 2302 for (ALL_NEXTHOPS(re->ng, nexthop)) {
2d68a0f2
DS
2303 struct interface *ifp;
2304 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
2305
2306 switch (nexthop->type) {
2307 case NEXTHOP_TYPE_BLACKHOLE:
2308 sprintf(straddr, "Blackhole");
2309 break;
2310 case NEXTHOP_TYPE_IFINDEX:
2311 ifp = if_lookup_by_index(nexthop->ifindex,
2312 nexthop->vrf_id);
2313 sprintf(straddr, "%s", ifp ? ifp->name : "Unknown");
2314 break;
2315 case NEXTHOP_TYPE_IPV4:
2316 /* fallthrough */
2317 case NEXTHOP_TYPE_IPV4_IFINDEX:
2318 inet_ntop(AF_INET, &nexthop->gate, straddr,
2319 INET6_ADDRSTRLEN);
2320 break;
2321 case NEXTHOP_TYPE_IPV6:
2322 case NEXTHOP_TYPE_IPV6_IFINDEX:
2323 inet_ntop(AF_INET6, &nexthop->gate, straddr,
2324 INET6_ADDRSTRLEN);
2325 break;
2326 }
2327 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func,
d62a17ae 2328 (nexthop->rparent ? " NH" : "NH"), straddr,
2d68a0f2
DS
2329 nexthop->ifindex, vrf ? vrf->name : "Unknown",
2330 nexthop->vrf_id,
d62a17ae 2331 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
2332 ? "ACTIVE "
2333 : ""),
2334 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
2335 ? "FIB "
2336 : ""),
2337 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
2338 ? "RECURSIVE"
2339 : ""));
2340 }
2341 zlog_debug("%s: dump complete", func);
dc95824a
DO
2342}
2343
2344/* This is an exported helper to rtm_read() to dump the strange
f0f77c9a 2345 * RE entry found by rib_lookup_ipv4_route()
dc95824a
DO
2346 */
2347
d62a17ae 2348void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2349{
2350 struct route_table *table;
2351 struct route_node *rn;
2352 struct route_entry *re;
2353 char prefix_buf[INET_ADDRSTRLEN];
2354
2355 /* Lookup table. */
2356 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
2357 if (!table) {
e914ccbe 2358 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1c50c1c0
QY
2359 "%s:%u zebra_vrf_table() returned NULL", __func__,
2360 vrf_id);
d62a17ae 2361 return;
2362 }
2363
2364 /* Scan the RIB table for exactly matching RE entry. */
2365 rn = route_node_lookup(table, (struct prefix *)p);
2366
2367 /* No route for this prefix. */
2368 if (!rn) {
32391aff 2369 zlog_debug("%s:%u lookup failed for %s", __func__, vrf_id,
d62a17ae 2370 prefix2str((struct prefix *)p, prefix_buf,
2371 sizeof(prefix_buf)));
2372 return;
2373 }
2374
2375 /* Unlock node. */
2376 route_unlock_node(rn);
2377
2378 /* let's go */
a2addae8 2379 RNODE_FOREACH_RE (rn, re) {
32391aff
DS
2380 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2381 __func__, vrf_id,
2382 (void *)rn, (void *)re,
d62a17ae 2383 (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)
2384 ? "removed"
2385 : "NOT removed"),
2386 (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)
2387 ? "selected"
2388 : "NOT selected"));
2389 route_entry_dump(p, NULL, re);
2390 }
dc95824a
DO
2391}
2392
20e5ff0a
DO
2393/* Check if requested address assignment will fail due to another
2394 * route being installed by zebra in FIB already. Take necessary
2395 * actions, if needed: remove such a route from FIB and deSELECT
f0f77c9a 2396 * corresponding RE entry. Then put affected RN into RIBQ head.
20e5ff0a 2397 */
d62a17ae 2398void rib_lookup_and_pushup(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2399{
2400 struct route_table *table;
2401 struct route_node *rn;
d62a17ae 2402 unsigned changed = 0;
5f7a4718 2403 rib_dest_t *dest;
d62a17ae 2404
2405 if (NULL == (table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id))) {
e914ccbe 2406 flog_err(EC_ZEBRA_TABLE_LOOKUP_FAILED,
1c50c1c0
QY
2407 "%s:%u zebra_vrf_table() returned NULL", __func__,
2408 vrf_id);
d62a17ae 2409 return;
2410 }
2411
2412 /* No matches would be the simplest case. */
2413 if (NULL == (rn = route_node_lookup(table, (struct prefix *)p)))
2414 return;
2415
2416 /* Unlock node. */
2417 route_unlock_node(rn);
2418
5f7a4718 2419 dest = rib_dest_from_rnode(rn);
d62a17ae 2420 /* Check all RE entries. In case any changes have to be done, requeue
2421 * the RN into RIBQ head. If the routing message about the new connected
2422 * route (generated by the IP address we are going to assign very soon)
2423 * comes before the RIBQ is processed, the new RE entry will join
2424 * RIBQ record already on head. This is necessary for proper
2425 * revalidation
2426 * of the rest of the RE.
2427 */
5f7a4718
DS
2428 if (dest->selected_fib && !RIB_SYSTEM_ROUTE(dest->selected_fib)) {
2429 changed = 1;
2430 if (IS_ZEBRA_DEBUG_RIB) {
2431 char buf[PREFIX_STRLEN];
2432
2433 zlog_debug("%u:%s: freeing way for connected prefix",
2434 dest->selected_fib->vrf_id,
2435 prefix2str(&rn->p, buf, sizeof(buf)));
2436 route_entry_dump(&rn->p, NULL, dest->selected_fib);
d62a17ae 2437 }
5f7a4718 2438 rib_uninstall(rn, dest->selected_fib);
d62a17ae 2439 }
2440 if (changed)
2441 rib_queue_add(rn);
20e5ff0a
DO
2442}
2443
d62a17ae 2444int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
2445 struct prefix_ipv6 *src_p, struct route_entry *re)
718e3744 2446{
d62a17ae 2447 struct route_table *table;
2448 struct route_node *rn;
40ecd8e4 2449 struct route_entry *same = NULL;
d62a17ae 2450 struct nexthop *nexthop;
2451 int ret = 0;
b4c034b0 2452
d62a17ae 2453 if (!re)
2454 return 0;
b4c034b0 2455
1f610a1f 2456 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
05737783 2457
d62a17ae 2458 /* Lookup table. */
7865c65d
RW
2459 table = zebra_vrf_table_with_table_id(afi, safi, re->vrf_id, re->table);
2460 if (!table) {
2461 XFREE(MTYPE_RE, re);
d62a17ae 2462 return 0;
7865c65d 2463 }
cddf391b 2464
d62a17ae 2465 /* Make it sure prefixlen is applied to the prefix. */
2466 apply_mask(p);
2467 if (src_p)
2468 apply_mask_ipv6(src_p);
718e3744 2469
d62a17ae 2470 /* Set default distance by route type. */
2471 if (re->distance == 0) {
0492eea0 2472 re->distance = route_distance(re->type);
718e3744 2473
d62a17ae 2474 /* iBGP distance is 200. */
2475 if (re->type == ZEBRA_ROUTE_BGP
2476 && CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
2477 re->distance = 200;
2478 }
718e3744 2479
d62a17ae 2480 /* Lookup route node.*/
2481 rn = srcdest_rnode_get(table, p, src_p);
718e3744 2482
40ecd8e4
DS
2483 /*
2484 * If same type of route are installed, treat it as a implicit
2485 * withdraw.
2486 * If the user has specified the No route replace semantics
2487 * for the install don't do a route replace.
2488 */
a2addae8 2489 RNODE_FOREACH_RE (rn, same) {
d62a17ae 2490 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
2491 continue;
41ec9222 2492
eb327fa5
RW
2493 if (same->type != re->type)
2494 continue;
2495 if (same->instance != re->instance)
2496 continue;
996c9314
LB
2497 if (same->type == ZEBRA_ROUTE_KERNEL
2498 && same->metric != re->metric)
eb327fa5 2499 continue;
40ecd8e4
DS
2500
2501 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2502 same->distance != re->distance)
2503 continue;
2504
844b3a87 2505 /*
40ecd8e4
DS
2506 * We should allow duplicate connected routes
2507 * because of IPv6 link-local routes and unnumbered
2508 * interfaces on Linux.
844b3a87
RW
2509 */
2510 if (same->type != ZEBRA_ROUTE_CONNECT)
d62a17ae 2511 break;
2512 }
718e3744 2513
d62a17ae 2514 /* If this route is kernel route, set FIB flag to the route. */
8628fc61 2515 if (RIB_SYSTEM_ROUTE(re))
7ee30f28 2516 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next)
d62a17ae 2517 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
718e3744 2518
d62a17ae 2519 /* Link new re to node.*/
2520 if (IS_ZEBRA_DEBUG_RIB) {
2521 rnode_debug(
2522 rn, re->vrf_id,
2523 "Inserting route rn %p, re %p (type %d) existing %p",
2524 (void *)rn, (void *)re, re->type, (void *)same);
718e3744 2525
d62a17ae 2526 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2527 route_entry_dump(p, src_p, re);
718e3744 2528 }
d62a17ae 2529 rib_addnode(rn, re, 1);
2530 ret = 1;
6b0655a2 2531
d62a17ae 2532 /* Free implicit route.*/
2533 if (same) {
2534 rib_delnode(rn, same);
2535 ret = -1;
2536 }
718e3744 2537
d62a17ae 2538 route_unlock_node(rn);
2539 return ret;
2540}
2541
2542void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
d7c0a89a 2543 unsigned short instance, int flags, struct prefix *p,
fd36be7e 2544 struct prefix_ipv6 *src_p, const struct nexthop *nh,
40ecd8e4
DS
2545 uint32_t table_id, uint32_t metric, uint8_t distance,
2546 bool fromkernel)
d62a17ae 2547{
2548 struct route_table *table;
2549 struct route_node *rn;
2550 struct route_entry *re;
2551 struct route_entry *fib = NULL;
2552 struct route_entry *same = NULL;
fd36be7e 2553 struct nexthop *rtnh;
d62a17ae 2554 char buf2[INET6_ADDRSTRLEN];
5f7a4718 2555 rib_dest_t *dest;
d62a17ae 2556
1f610a1f 2557 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
d62a17ae 2558
2559 /* Lookup table. */
2560 table = zebra_vrf_table_with_table_id(afi, safi, vrf_id, table_id);
2561 if (!table)
2562 return;
2563
2564 /* Apply mask. */
2565 apply_mask(p);
2566 if (src_p)
2567 apply_mask_ipv6(src_p);
2568
2569 /* Lookup route node. */
2570 rn = srcdest_rnode_lookup(table, p, src_p);
2571 if (!rn) {
2572 char dst_buf[PREFIX_STRLEN], src_buf[PREFIX_STRLEN];
2573
2574 prefix2str(p, dst_buf, sizeof(dst_buf));
2575 if (src_p && src_p->prefixlen)
2576 prefix2str(src_p, src_buf, sizeof(src_buf));
2577 else
2578 src_buf[0] = '\0';
2579
2580 if (IS_ZEBRA_DEBUG_RIB)
2581 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id,
2582 dst_buf,
2583 (src_buf[0] != '\0') ? " from " : "",
2584 src_buf);
2585 return;
2586 }
718e3744 2587
5f7a4718
DS
2588 dest = rib_dest_from_rnode(rn);
2589 fib = dest->selected_fib;
2590
d62a17ae 2591 /* Lookup same type route. */
a2addae8 2592 RNODE_FOREACH_RE (rn, re) {
d62a17ae 2593 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2594 continue;
2595
d62a17ae 2596 if (re->type != type)
2597 continue;
2598 if (re->instance != instance)
2599 continue;
40ecd8e4
DS
2600 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2601 distance != re->distance)
2602 continue;
2603
996c9314 2604 if (re->type == ZEBRA_ROUTE_KERNEL && re->metric != metric)
f19435a8 2605 continue;
7ee30f28 2606 if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = re->ng.nexthop)
fd36be7e
DL
2607 && rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
2608 if (rtnh->ifindex != nh->ifindex)
d62a17ae 2609 continue;
d62a17ae 2610 same = re;
2611 break;
2612 }
2613 /* Make sure that the route found has the same gateway. */
2614 else {
fd36be7e 2615 if (nh == NULL) {
d62a17ae 2616 same = re;
2617 break;
2618 }
7ee30f28 2619 for (ALL_NEXTHOPS(re->ng, rtnh))
fd36be7e 2620 if (nexthop_same_no_recurse(rtnh, nh)) {
d62a17ae 2621 same = re;
2622 break;
2623 }
2624 if (same)
2625 break;
2626 }
2627 }
2628 /* If same type of route can't be found and this message is from
2629 kernel. */
2630 if (!same) {
5dfeba19
DS
2631 /*
2632 * In the past(HA!) we could get here because
2633 * we were receiving a route delete from the
2634 * kernel and we're not marking the proto
2635 * as coming from it's appropriate originator.
2636 * Now that we are properly noticing the fact
2637 * that the kernel has deleted our route we
2638 * are not going to get called in this path
2639 * I am going to leave this here because
2640 * this might still work this way on non-linux
2641 * platforms as well as some weird state I have
2642 * not properly thought of yet.
2643 * If we can show that this code path is
2644 * dead then we can remove it.
2645 */
b8faa875 2646 if (fib && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) {
d62a17ae 2647 if (IS_ZEBRA_DEBUG_RIB) {
2648 rnode_debug(
2649 rn, vrf_id,
2650 "rn %p, re %p (type %d) was deleted from kernel, adding",
2651 rn, fib, fib->type);
2652 }
2653 if (allow_delete) {
2654 /* Unset flags. */
7ee30f28 2655 for (rtnh = fib->ng.nexthop; rtnh;
fd36be7e
DL
2656 rtnh = rtnh->next)
2657 UNSET_FLAG(rtnh->flags,
d62a17ae 2658 NEXTHOP_FLAG_FIB);
2659
ed216282
DS
2660 /*
2661 * This is a non FRR route
2662 * as such we should mark
2663 * it as deleted
2664 */
5f7a4718 2665 dest->selected_fib = NULL;
d62a17ae 2666 } else {
2667 /* This means someone else, other than Zebra,
2668 * has deleted
2669 * a Zebra router from the kernel. We will add
2670 * it back */
2671 rib_install_kernel(rn, fib, NULL);
2672 }
2673 } else {
2674 if (IS_ZEBRA_DEBUG_RIB) {
fd36be7e 2675 if (nh)
d62a17ae 2676 rnode_debug(
2677 rn, vrf_id,
2678 "via %s ifindex %d type %d "
2679 "doesn't exist in rib",
36228974 2680 inet_ntop(afi2family(afi),
2681 &nh->gate, buf2,
2682 sizeof(buf2)),
2683 nh->ifindex, type);
d62a17ae 2684 else
2685 rnode_debug(
2686 rn, vrf_id,
fd36be7e
DL
2687 "type %d doesn't exist in rib",
2688 type);
d62a17ae 2689 }
2690 route_unlock_node(rn);
2691 return;
2692 }
2693 }
718e3744 2694
5dfeba19 2695 if (same) {
996c9314
LB
2696 if (fromkernel && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)
2697 && !allow_delete) {
5dfeba19
DS
2698 rib_install_kernel(rn, same, NULL);
2699 route_unlock_node(rn);
2700
2701 return;
2702 }
6134fd82 2703
90264d64 2704 if (CHECK_FLAG(flags, ZEBRA_FLAG_EVPN_ROUTE)) {
6134fd82 2705 struct nexthop *tmp_nh;
2706
7ee30f28 2707 for (ALL_NEXTHOPS(re->ng, tmp_nh)) {
6134fd82 2708 struct ipaddr vtep_ip;
2709
2710 memset(&vtep_ip, 0, sizeof(struct ipaddr));
1ec31309 2711 if (afi == AFI_IP) {
2712 vtep_ip.ipa_type = IPADDR_V4;
2713 memcpy(&(vtep_ip.ipaddr_v4),
2714 &(tmp_nh->gate.ipv4),
2715 sizeof(struct in_addr));
2716 } else {
2717 vtep_ip.ipa_type = IPADDR_V6;
2718 memcpy(&(vtep_ip.ipaddr_v6),
2719 &(tmp_nh->gate.ipv6),
2720 sizeof(struct in6_addr));
2721 }
a317a9b9 2722 zebra_vxlan_evpn_vrf_route_del(re->vrf_id,
6134fd82 2723 &vtep_ip, p);
2724 }
2725 }
d62a17ae 2726 rib_delnode(rn, same);
5dfeba19 2727 }
05737783 2728
d62a17ae 2729 route_unlock_node(rn);
2730 return;
2731}
718e3744 2732
718e3744 2733
d7c0a89a
QY
2734int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
2735 unsigned short instance, int flags, struct prefix *p,
2736 struct prefix_ipv6 *src_p, const struct nexthop *nh,
2737 uint32_t table_id, uint32_t metric, uint32_t mtu, uint8_t distance,
2738 route_tag_t tag)
d62a17ae 2739{
2740 struct route_entry *re;
66af6845 2741 struct nexthop *nexthop;
718e3744 2742
66af6845 2743 /* Allocate new route_entry structure. */
d62a17ae 2744 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
d62a17ae 2745 re->type = type;
2746 re->instance = instance;
2747 re->distance = distance;
2748 re->flags = flags;
2749 re->metric = metric;
2750 re->mtu = mtu;
2751 re->table = table_id;
2752 re->vrf_id = vrf_id;
2753 re->nexthop_num = 0;
2754 re->uptime = time(NULL);
4e40b6d6 2755 re->tag = tag;
d62a17ae 2756
66af6845
RW
2757 /* Add nexthop. */
2758 nexthop = nexthop_new();
2759 *nexthop = *nh;
2760 route_entry_nexthop_add(re, nexthop);
718e3744 2761
66af6845 2762 return rib_add_multipath(afi, safi, p, src_p, re);
718e3744 2763}
2764
1c848137 2765/* Schedule routes of a particular table (address-family) based on event. */
d5b8c216 2766void rib_update_table(struct route_table *table, rib_update_event_t event)
d62a17ae 2767{
2768 struct route_node *rn;
2769 struct route_entry *re, *next;
2770
2771 /* Walk all routes and queue for processing, if appropriate for
2772 * the trigger event.
2773 */
2774 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
1ca60f2c
DS
2775 /*
2776 * If we are looking at a route node and the node
2777 * has already been queued we don't
2778 * need to queue it up again
2779 */
996c9314
LB
2780 if (rn->info && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
2781 RIB_ROUTE_ANY_QUEUED))
1ca60f2c 2782 continue;
d62a17ae 2783 switch (event) {
2784 case RIB_UPDATE_IF_CHANGE:
2785 /* Examine all routes that won't get processed by the
2786 * protocol or
2787 * triggered by nexthop evaluation (NHT). This would be
2788 * system,
2789 * kernel and certain static routes. Note that NHT will
2790 * get
2791 * triggered upon an interface event as connected routes
2792 * always
2793 * get queued for processing.
2794 */
a2addae8 2795 RNODE_FOREACH_RE_SAFE (rn, re, next) {
0a16efff
DS
2796 struct nexthop *nh;
2797
996c9314
LB
2798 if (re->type != ZEBRA_ROUTE_SYSTEM
2799 && re->type != ZEBRA_ROUTE_KERNEL
2800 && re->type != ZEBRA_ROUTE_CONNECT
2801 && re->type != ZEBRA_ROUTE_STATIC)
0a16efff
DS
2802 continue;
2803
2804 if (re->type != ZEBRA_ROUTE_STATIC) {
2805 rib_queue_add(rn);
2806 continue;
2807 }
2808
7ee30f28 2809 for (nh = re->ng.nexthop; nh; nh = nh->next)
0a16efff
DS
2810 if (!(nh->type == NEXTHOP_TYPE_IPV4
2811 || nh->type == NEXTHOP_TYPE_IPV6))
2812 break;
2813
2814 /* If we only have nexthops to a
2815 * gateway, NHT will
2816 * take care.
2817 */
2818 if (nh)
d62a17ae 2819 rib_queue_add(rn);
2820 }
2821 break;
2822
2823 case RIB_UPDATE_RMAP_CHANGE:
2824 case RIB_UPDATE_OTHER:
2825 /* Right now, examine all routes. Can restrict to a
2826 * protocol in
2827 * some cases (TODO).
2828 */
2829 if (rnode_to_ribs(rn))
2830 rib_queue_add(rn);
2831 break;
2832
2833 default:
2834 break;
2835 }
2836 }
b84c7253 2837}
2838
718e3744 2839/* RIB update function. */
d62a17ae 2840void rib_update(vrf_id_t vrf_id, rib_update_event_t event)
718e3744 2841{
d62a17ae 2842 struct route_table *table;
1c848137 2843
d62a17ae 2844 /* Process routes of interested address-families. */
2845 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
d5b8c216 2846 if (table) {
2847 if (IS_ZEBRA_DEBUG_EVENT)
2848 zlog_debug("%s : AFI_IP event %d", __func__, event);
d62a17ae 2849 rib_update_table(table, event);
d5b8c216 2850 }
718e3744 2851
d62a17ae 2852 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
d5b8c216 2853 if (table) {
2854 if (IS_ZEBRA_DEBUG_EVENT)
2855 zlog_debug("%s : AFI_IP6 event %d", __func__, event);
d62a17ae 2856 rib_update_table(table, event);
d5b8c216 2857 }
718e3744 2858}
2859
718e3744 2860/* Delete self installed routes after zebra is relaunched. */
95a29032 2861void rib_sweep_table(struct route_table *table)
d62a17ae 2862{
2863 struct route_node *rn;
2864 struct route_entry *re;
2865 struct route_entry *next;
915902cb 2866 struct nexthop *nexthop;
d62a17ae 2867
915902cb
DS
2868 if (!table)
2869 return;
d62a17ae 2870
915902cb 2871 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
a2addae8 2872 RNODE_FOREACH_RE_SAFE (rn, re, next) {
915902cb
DS
2873 if (IS_ZEBRA_DEBUG_RIB)
2874 route_entry_dump(&rn->p, NULL, re);
2875
2876 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2877 continue;
2878
2879 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
2880 continue;
2881
2882 /*
2883 * So we are starting up and have received
2884 * routes from the kernel that we have installed
2885 * from a previous run of zebra but not cleaned
2886 * up ( say a kill -9 )
2887 * But since we haven't actually installed
2888 * them yet( we received them from the kernel )
2889 * we don't think they are active.
2890 * So let's pretend they are active to actually
2891 * remove them.
2892 * In all honesty I'm not sure if we should
2893 * mark them as active when we receive them
2894 * This is startup only so probably ok.
2895 *
2896 * If we ever decide to move rib_sweep_table
2897 * to a different spot (ie startup )
2898 * this decision needs to be revisited
2899 */
7ee30f28 2900 for (ALL_NEXTHOPS(re->ng, nexthop))
915902cb
DS
2901 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2902
0c555cc6
DS
2903 rib_uninstall_kernel(rn, re);
2904 rib_delnode(rn, re);
915902cb
DS
2905 }
2906 }
718e3744 2907}
2908
2909/* Sweep all RIB tables. */
d62a17ae 2910void rib_sweep_route(void)
718e3744 2911{
d62a17ae 2912 struct vrf *vrf;
2913 struct zebra_vrf *zvrf;
78104b9b 2914
a2addae8 2915 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
915902cb
DS
2916 if ((zvrf = vrf->info) == NULL)
2917 continue;
2918
d62a17ae 2919 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
2920 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
2921 }
95a29032
DS
2922
2923 zebra_ns_sweep_route();
718e3744 2924}
2ea1ab1c
VT
2925
2926/* Remove specific by protocol routes from 'table'. */
d7c0a89a 2927unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
47a08aa9 2928 struct route_table *table)
d62a17ae 2929{
2930 struct route_node *rn;
2931 struct route_entry *re;
2932 struct route_entry *next;
2933 unsigned long n = 0;
2934
2935 if (table)
2936 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
a2addae8 2937 RNODE_FOREACH_RE_SAFE (rn, re, next) {
d62a17ae 2938 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2939 continue;
2940 if (re->type == proto
2941 && re->instance == instance) {
2942 rib_delnode(rn, re);
2943 n++;
2944 }
2945 }
2946 return n;
2ea1ab1c
VT
2947}
2948
2949/* Remove specific by protocol routes. */
d7c0a89a 2950unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
2ea1ab1c 2951{
d62a17ae 2952 struct vrf *vrf;
2953 struct zebra_vrf *zvrf;
2954 unsigned long cnt = 0;
78104b9b 2955
a2addae8
RW
2956 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
2957 if ((zvrf = vrf->info) != NULL)
2958 cnt += rib_score_proto_table(
2959 proto, instance,
2960 zvrf->table[AFI_IP][SAFI_UNICAST])
2961 + rib_score_proto_table(
2962 proto, instance,
2963 zvrf->table[AFI_IP6][SAFI_UNICAST]);
78104b9b 2964
47a08aa9
DS
2965 cnt += zebra_ns_score_proto(proto, instance);
2966
d62a17ae 2967 return cnt;
2ea1ab1c
VT
2968}
2969
718e3744 2970/* Close RIB and clean up kernel routes. */
d62a17ae 2971void rib_close_table(struct route_table *table)
718e3744 2972{
d62a17ae 2973 struct route_node *rn;
1e9f448f 2974 rib_table_info_t *info;
5f7a4718 2975 rib_dest_t *dest;
718e3744 2976
1e9f448f
DS
2977 if (!table)
2978 return;
9fd92e3c 2979
6ca30e9e 2980 info = route_table_get_info(table);
5adc2528 2981
5f7a4718
DS
2982 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
2983 dest = rib_dest_from_rnode(rn);
1e9f448f 2984
5f7a4718 2985 if (dest && dest->selected_fib) {
1e9f448f
DS
2986 if (info->safi == SAFI_UNICAST)
2987 hook_call(rib_update, rn, NULL);
2988
5f7a4718
DS
2989 if (!RIB_SYSTEM_ROUTE(dest->selected_fib))
2990 rib_uninstall_kernel(rn, dest->selected_fib);
1e9f448f 2991 }
5f7a4718 2992 }
718e3744 2993}
2994
718e3744 2995/* Routing information base initialize. */
d62a17ae 2996void rib_init(void)
718e3744 2997{
d62a17ae 2998 rib_queue_init(&zebrad);
718e3744 2999}
0915bb0c
AS
3000
3001/*
3002 * vrf_id_get_next
3003 *
3004 * Get the first vrf id that is greater than the given vrf id if any.
3005 *
3006 * Returns TRUE if a vrf id was found, FALSE otherwise.
3007 */
d62a17ae 3008static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
0915bb0c 3009{
d62a17ae 3010 struct vrf *vrf;
b72ede27 3011
d62a17ae 3012 vrf = vrf_lookup_by_id(vrf_id);
3013 if (vrf) {
3014 vrf = RB_NEXT(vrf_id_head, vrf);
3015 if (vrf) {
3016 *next_id_p = vrf->vrf_id;
3017 return 1;
3018 }
3019 }
0915bb0c 3020
d62a17ae 3021 return 0;
0915bb0c
AS
3022}
3023
3024/*
3025 * rib_tables_iter_next
3026 *
3027 * Returns the next table in the iteration.
3028 */
d62a17ae 3029struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
3030{
3031 struct route_table *table;
3032
3033 /*
3034 * Array that helps us go over all AFI/SAFI combinations via one
3035 * index.
3036 */
3037 static struct {
3038 afi_t afi;
3039 safi_t safi;
3040 } afi_safis[] = {
3041 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
3042 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
3043 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
3044 };
3045
3046 table = NULL;
3047
3048 switch (iter->state) {
3049
3050 case RIB_TABLES_ITER_S_INIT:
3051 iter->vrf_id = VRF_DEFAULT;
3052 iter->afi_safi_ix = -1;
3053
3054 /* Fall through */
3055
3056 case RIB_TABLES_ITER_S_ITERATING:
3057 iter->afi_safi_ix++;
3058 while (1) {
3059
3060 while (iter->afi_safi_ix
3061 < (int)ZEBRA_NUM_OF(afi_safis)) {
3062 table = zebra_vrf_table(
3063 afi_safis[iter->afi_safi_ix].afi,
3064 afi_safis[iter->afi_safi_ix].safi,
3065 iter->vrf_id);
3066 if (table)
3067 break;
3068
3069 iter->afi_safi_ix++;
3070 }
3071
3072 /*
3073 * Found another table in this vrf.
3074 */
3075 if (table)
3076 break;
3077
3078 /*
3079 * Done with all tables in the current vrf, go to the
3080 * next
3081 * one.
3082 */
3083 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
3084 break;
3085
3086 iter->afi_safi_ix = 0;
3087 }
0915bb0c 3088
0915bb0c
AS
3089 break;
3090
d62a17ae 3091 case RIB_TABLES_ITER_S_DONE:
3092 return NULL;
0915bb0c
AS
3093 }
3094
d62a17ae 3095 if (table)
3096 iter->state = RIB_TABLES_ITER_S_ITERATING;
3097 else
3098 iter->state = RIB_TABLES_ITER_S_DONE;
0915bb0c 3099
d62a17ae 3100 return table;
0915bb0c 3101}