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