]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rib.c
Merge branch 'pr2983'
[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 srcdest_rnode_prefixes(rn, &p, &src_p);
890
891 if (rn->p.family == AF_INET)
892 family = AFI_IP;
893 else if (rn->p.family == AF_INET6)
894 family = AFI_IP6;
895 else
896 family = 0;
897 switch (nexthop->type) {
898 case NEXTHOP_TYPE_IFINDEX:
899 ifp = if_lookup_by_index(nexthop->ifindex, nexthop->vrf_id);
900 if (ifp && if_is_operative(ifp))
901 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
902 else
903 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
904 break;
905 case NEXTHOP_TYPE_IPV4:
906 case NEXTHOP_TYPE_IPV4_IFINDEX:
907 family = AFI_IP;
908 if (nexthop_active(AFI_IP, re, nexthop, set, rn))
909 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
910 else
911 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
912 break;
913 case NEXTHOP_TYPE_IPV6:
914 family = AFI_IP6;
915 if (nexthop_active(AFI_IP6, re, nexthop, set, rn))
916 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
917 else
918 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
919 break;
920 case NEXTHOP_TYPE_IPV6_IFINDEX:
921 /* RFC 5549, v4 prefix with v6 NH */
922 if (rn->p.family != AF_INET)
923 family = AFI_IP6;
924 if (IN6_IS_ADDR_LINKLOCAL(&nexthop->gate.ipv6)) {
925 ifp = if_lookup_by_index(nexthop->ifindex,
926 nexthop->vrf_id);
927 if (ifp && if_is_operative(ifp))
928 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
929 else
930 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
931 } else {
932 if (nexthop_active(AFI_IP6, re, nexthop, set, rn))
933 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
934 else
935 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
936 }
937 break;
938 case NEXTHOP_TYPE_BLACKHOLE:
939 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
940 break;
941 default:
942 break;
943 }
944 if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
945 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
946 zlog_debug("\t%s: Unable to find a active nexthop",
947 __PRETTY_FUNCTION__);
948 return 0;
949 }
950
951 /* XXX: What exactly do those checks do? Do we support
952 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
953 if (RIB_SYSTEM_ROUTE(re) || (family == AFI_IP && p->family != AF_INET)
954 || (family == AFI_IP6 && p->family != AF_INET6))
955 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
956
957 /* The original code didn't determine the family correctly
958 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
959 * from the rib_table_info in those cases.
960 * Possibly it may be better to use only the rib_table_info
961 * in every case.
962 */
963 if (!family) {
964 rib_table_info_t *info;
965
966 info = srcdest_rnode_table_info(rn);
967 family = info->afi;
968 }
969
970 memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr));
971
972 /* It'll get set if required inside */
973 ret = zebra_route_map_check(family, re->type, re->instance, p, nexthop,
974 nexthop->vrf_id, re->tag);
975 if (ret == RMAP_DENYMATCH) {
976 if (IS_ZEBRA_DEBUG_RIB) {
977 srcdest_rnode2str(rn, buf, sizeof(buf));
978 zlog_debug(
979 "%u:%s: Filtering out with NH out %s due to route map",
980 re->vrf_id, buf,
981 ifindex2ifname(nexthop->ifindex,
982 nexthop->vrf_id));
983 }
984 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
985 }
986 return CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
987 }
988
989 /* Iterate over all nexthops of the given RIB entry and refresh their
990 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
991 * nexthop is found to toggle the ACTIVE flag, the whole re structure
992 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
993 * transparently passed to nexthop_active_check().
994 *
995 * Return value is the new number of active nexthops.
996 */
997
998 static int nexthop_active_update(struct route_node *rn, struct route_entry *re,
999 int set)
1000 {
1001 struct nexthop *nexthop;
1002 union g_addr prev_src;
1003 unsigned int prev_active, new_active, old_num_nh;
1004 ifindex_t prev_index;
1005 old_num_nh = re->nexthop_active_num;
1006
1007 re->nexthop_active_num = 0;
1008 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1009
1010 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next) {
1011 /* No protocol daemon provides src and so we're skipping
1012 * tracking it */
1013 prev_src = nexthop->rmap_src;
1014 prev_active = CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1015 prev_index = nexthop->ifindex;
1016 if ((new_active = nexthop_active_check(rn, re, nexthop, set)))
1017 re->nexthop_active_num++;
1018 /* Don't allow src setting on IPv6 addr for now */
1019 if (prev_active != new_active || prev_index != nexthop->ifindex
1020 || ((nexthop->type >= NEXTHOP_TYPE_IFINDEX
1021 && nexthop->type < NEXTHOP_TYPE_IPV6)
1022 && prev_src.ipv4.s_addr
1023 != nexthop->rmap_src.ipv4.s_addr)
1024 || ((nexthop->type >= NEXTHOP_TYPE_IPV6
1025 && nexthop->type < NEXTHOP_TYPE_BLACKHOLE)
1026 && !(IPV6_ADDR_SAME(&prev_src.ipv6,
1027 &nexthop->rmap_src.ipv6)))) {
1028 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1029 SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1030 }
1031 }
1032
1033 if (old_num_nh != re->nexthop_active_num)
1034 SET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1035
1036 if (CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)) {
1037 SET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1038 }
1039
1040 return re->nexthop_active_num;
1041 }
1042
1043 /*
1044 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1045 * (nexthops) must have a label.
1046 */
1047 int zebra_rib_labeled_unicast(struct route_entry *re)
1048 {
1049 struct nexthop *nexthop = NULL;
1050
1051 if (re->type != ZEBRA_ROUTE_BGP)
1052 return 0;
1053
1054 for (ALL_NEXTHOPS(re->ng, nexthop))
1055 if (!nexthop->nh_label || !nexthop->nh_label->num_labels)
1056 return 0;
1057
1058 return 1;
1059 }
1060
1061 void kernel_route_rib_pass_fail(struct route_node *rn, const struct prefix *p,
1062 struct route_entry *re,
1063 enum dp_results res)
1064 {
1065 struct nexthop *nexthop;
1066 char buf[PREFIX_STRLEN];
1067 rib_dest_t *dest;
1068
1069 dest = rib_dest_from_rnode(rn);
1070
1071 switch (res) {
1072 case DP_INSTALL_SUCCESS:
1073 dest->selected_fib = re;
1074 for (ALL_NEXTHOPS(re->ng, nexthop)) {
1075 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
1076 continue;
1077
1078 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1079 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1080 else
1081 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1082 }
1083 zsend_route_notify_owner(re, p, ZAPI_ROUTE_INSTALLED);
1084 break;
1085 case DP_INSTALL_FAILURE:
1086 /*
1087 * I am not sure this is the right thing to do here
1088 * but the code always set selected_fib before
1089 * this assignment was moved here.
1090 */
1091 dest->selected_fib = re;
1092
1093 zsend_route_notify_owner(re, p, ZAPI_ROUTE_FAIL_INSTALL);
1094 zlog_warn("%u:%s: Route install failed", re->vrf_id,
1095 prefix2str(p, buf, sizeof(buf)));
1096 break;
1097 case DP_DELETE_SUCCESS:
1098 /*
1099 * The case where selected_fib is not re is
1100 * when we have received a system route
1101 * that is overriding our installed route
1102 * as such we should leave the selected_fib
1103 * pointer alone
1104 */
1105 if (dest->selected_fib == re)
1106 dest->selected_fib = NULL;
1107 for (ALL_NEXTHOPS(re->ng, nexthop))
1108 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1109
1110 zsend_route_notify_owner(re, p, ZAPI_ROUTE_REMOVED);
1111 break;
1112 case DP_DELETE_FAILURE:
1113 /*
1114 * Should we set this to NULL if the
1115 * delete fails?
1116 */
1117 dest->selected_fib = NULL;
1118 zlog_warn("%u:%s: Route Deletion failure", re->vrf_id,
1119 prefix2str(p, buf, sizeof(buf)));
1120
1121 zsend_route_notify_owner(re, p, ZAPI_ROUTE_REMOVE_FAIL);
1122 break;
1123 }
1124 }
1125
1126 /* Update flag indicates whether this is a "replace" or not. Currently, this
1127 * is only used for IPv4.
1128 */
1129 void rib_install_kernel(struct route_node *rn, struct route_entry *re,
1130 struct route_entry *old)
1131 {
1132 struct nexthop *nexthop;
1133 rib_table_info_t *info = srcdest_rnode_table_info(rn);
1134 const struct prefix *p, *src_p;
1135 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
1136
1137 srcdest_rnode_prefixes(rn, &p, &src_p);
1138
1139 if (info->safi != SAFI_UNICAST) {
1140 for (ALL_NEXTHOPS(re->ng, nexthop))
1141 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1142 return;
1143 } else {
1144 struct nexthop *prev;
1145
1146 for (ALL_NEXTHOPS(re->ng, nexthop)) {
1147 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_DUPLICATE);
1148 for (ALL_NEXTHOPS(re->ng, prev)) {
1149 if (prev == nexthop)
1150 break;
1151 if (nexthop_same_firsthop(nexthop, prev)) {
1152 SET_FLAG(nexthop->flags,
1153 NEXTHOP_FLAG_DUPLICATE);
1154 break;
1155 }
1156 }
1157 }
1158 }
1159
1160 /*
1161 * If this is a replace to a new RE let the originator of the RE
1162 * know that they've lost
1163 */
1164 if (old && (old != re) && (old->type != re->type))
1165 zsend_route_notify_owner(old, p, ZAPI_ROUTE_BETTER_ADMIN_WON);
1166
1167 /*
1168 * Make sure we update the FPM any time we send new information to
1169 * the kernel.
1170 */
1171 hook_call(rib_update, rn, "installing in kernel");
1172 switch (kernel_route_rib(rn, p, src_p, old, re)) {
1173 case DP_REQUEST_QUEUED:
1174 flog_err(
1175 ZEBRA_ERR_DP_INVALID_RC,
1176 "No current known DataPlane interfaces can return this, please fix");
1177 break;
1178 case DP_REQUEST_FAILURE:
1179 flog_err(
1180 ZEBRA_ERR_DP_INSTALL_FAIL,
1181 "No current known Rib Install Failure cases, please fix");
1182 break;
1183 case DP_REQUEST_SUCCESS:
1184 zvrf->installs++;
1185 break;
1186 }
1187
1188 return;
1189 }
1190
1191 /* Uninstall the route from kernel. */
1192 void rib_uninstall_kernel(struct route_node *rn, struct route_entry *re)
1193 {
1194 struct nexthop *nexthop;
1195 rib_table_info_t *info = srcdest_rnode_table_info(rn);
1196 const struct prefix *p, *src_p;
1197 struct zebra_vrf *zvrf = vrf_info_lookup(re->vrf_id);
1198
1199 srcdest_rnode_prefixes(rn, &p, &src_p);
1200
1201 if (info->safi != SAFI_UNICAST) {
1202 for (ALL_NEXTHOPS(re->ng, nexthop))
1203 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
1204 return;
1205 }
1206
1207 /*
1208 * Make sure we update the FPM any time we send new information to
1209 * the kernel.
1210 */
1211 hook_call(rib_update, rn, "uninstalling from kernel");
1212 switch (kernel_route_rib(rn, p, src_p, re, NULL)) {
1213 case DP_REQUEST_QUEUED:
1214 flog_err(
1215 ZEBRA_ERR_DP_INVALID_RC,
1216 "No current known DataPlane interfaces can return this, please fix");
1217 break;
1218 case DP_REQUEST_FAILURE:
1219 flog_err(
1220 ZEBRA_ERR_DP_INSTALL_FAIL,
1221 "No current known RIB Install Failure cases, please fix");
1222 break;
1223 case DP_REQUEST_SUCCESS:
1224 if (zvrf)
1225 zvrf->removals++;
1226 break;
1227 }
1228
1229 return;
1230 }
1231
1232 /* Uninstall the route from kernel. */
1233 static void rib_uninstall(struct route_node *rn, struct route_entry *re)
1234 {
1235 rib_table_info_t *info = srcdest_rnode_table_info(rn);
1236 rib_dest_t *dest = rib_dest_from_rnode(rn);
1237
1238 if (dest && dest->selected_fib == re) {
1239 if (info->safi == SAFI_UNICAST)
1240 hook_call(rib_update, rn, "rib_uninstall");
1241
1242 if (!RIB_SYSTEM_ROUTE(re))
1243 rib_uninstall_kernel(rn, re);
1244
1245 /* If labeled-unicast route, uninstall transit LSP. */
1246 if (zebra_rib_labeled_unicast(re))
1247 zebra_mpls_lsp_uninstall(info->zvrf, rn, re);
1248 }
1249
1250 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1251 const struct prefix *p, *src_p;
1252
1253 srcdest_rnode_prefixes(rn, &p, &src_p);
1254
1255 redistribute_delete(p, src_p, re);
1256 UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
1257 }
1258 }
1259
1260 /*
1261 * rib_can_delete_dest
1262 *
1263 * Returns TRUE if the given dest can be deleted from the table.
1264 */
1265 static int rib_can_delete_dest(rib_dest_t *dest)
1266 {
1267 if (dest->routes) {
1268 return 0;
1269 }
1270
1271 /*
1272 * Don't delete the dest if we have to update the FPM about this
1273 * prefix.
1274 */
1275 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)
1276 || CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM))
1277 return 0;
1278
1279 return 1;
1280 }
1281
1282 /*
1283 * rib_gc_dest
1284 *
1285 * Garbage collect the rib dest corresponding to the given route node
1286 * if appropriate.
1287 *
1288 * Returns TRUE if the dest was deleted, FALSE otherwise.
1289 */
1290 int rib_gc_dest(struct route_node *rn)
1291 {
1292 rib_dest_t *dest;
1293
1294 dest = rib_dest_from_rnode(rn);
1295 if (!dest)
1296 return 0;
1297
1298 if (!rib_can_delete_dest(dest))
1299 return 0;
1300
1301 if (IS_ZEBRA_DEBUG_RIB) {
1302 struct zebra_vrf *zvrf;
1303
1304 zvrf = rib_dest_vrf(dest);
1305 rnode_debug(rn, zvrf_id(zvrf), "removing dest from table");
1306 }
1307
1308 dest->rnode = NULL;
1309 XFREE(MTYPE_RIB_DEST, dest);
1310 rn->info = NULL;
1311
1312 /*
1313 * Release the one reference that we keep on the route node.
1314 */
1315 route_unlock_node(rn);
1316 return 1;
1317 }
1318
1319 static void rib_process_add_fib(struct zebra_vrf *zvrf, struct route_node *rn,
1320 struct route_entry *new)
1321 {
1322 rib_dest_t *dest = rib_dest_from_rnode(rn);
1323
1324 hook_call(rib_update, rn, "new route selected");
1325
1326 /* Update real nexthop. This may actually determine if nexthop is active
1327 * or not. */
1328 if (!nexthop_active_update(rn, new, 1)) {
1329 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1330 return;
1331 }
1332
1333 if (IS_ZEBRA_DEBUG_RIB) {
1334 char buf[SRCDEST2STR_BUFFER];
1335 srcdest_rnode2str(rn, buf, sizeof(buf));
1336 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1337 zvrf_id(zvrf), buf, rn, new, new->type);
1338 }
1339
1340 /* If labeled-unicast route, install transit LSP. */
1341 if (zebra_rib_labeled_unicast(new))
1342 zebra_mpls_lsp_install(zvrf, rn, new);
1343
1344 if (!RIB_SYSTEM_ROUTE(new))
1345 rib_install_kernel(rn, new, NULL);
1346 else
1347 dest->selected_fib = new;
1348
1349 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1350 }
1351
1352 static void rib_process_del_fib(struct zebra_vrf *zvrf, struct route_node *rn,
1353 struct route_entry *old)
1354 {
1355 rib_dest_t *dest = rib_dest_from_rnode(rn);
1356 hook_call(rib_update, rn, "removing existing route");
1357
1358 /* Uninstall from kernel. */
1359 if (IS_ZEBRA_DEBUG_RIB) {
1360 char buf[SRCDEST2STR_BUFFER];
1361 srcdest_rnode2str(rn, buf, sizeof(buf));
1362 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1363 zvrf_id(zvrf), buf, rn, old, old->type);
1364 }
1365
1366 /* If labeled-unicast route, uninstall transit LSP. */
1367 if (zebra_rib_labeled_unicast(old))
1368 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1369
1370 if (!RIB_SYSTEM_ROUTE(old))
1371 rib_uninstall_kernel(rn, old);
1372 else {
1373 /*
1374 * We are setting this to NULL here
1375 * because that is what we traditionally
1376 * have been doing. I am not positive
1377 * that this is the right thing to do
1378 * but let's leave the code alone
1379 * for the RIB_SYSTEM_ROUTE case
1380 */
1381 dest->selected_fib = NULL;
1382 }
1383
1384 /* Update nexthop for route, reset changed flag. */
1385 nexthop_active_update(rn, old, 1);
1386 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
1387 }
1388
1389 static void rib_process_update_fib(struct zebra_vrf *zvrf,
1390 struct route_node *rn,
1391 struct route_entry *old,
1392 struct route_entry *new)
1393 {
1394 struct nexthop *nexthop = NULL;
1395 int nh_active = 0;
1396 rib_dest_t *dest = rib_dest_from_rnode(rn);
1397
1398 /*
1399 * We have to install or update if a new route has been selected or
1400 * something has changed.
1401 */
1402 if (new != old || CHECK_FLAG(new->status, ROUTE_ENTRY_CHANGED)) {
1403 hook_call(rib_update, rn, "updating existing route");
1404
1405 /* Update the nexthop; we could determine here that nexthop is
1406 * inactive. */
1407 if (nexthop_active_update(rn, new, 1))
1408 nh_active = 1;
1409
1410 /* If nexthop is active, install the selected route, if
1411 * appropriate. If
1412 * the install succeeds, cleanup flags for prior route, if
1413 * different from
1414 * newly selected.
1415 */
1416 if (nh_active) {
1417 if (IS_ZEBRA_DEBUG_RIB) {
1418 char buf[SRCDEST2STR_BUFFER];
1419 srcdest_rnode2str(rn, buf, sizeof(buf));
1420 if (new != old)
1421 zlog_debug(
1422 "%u:%s: Updating route rn %p, re %p (type %d) "
1423 "old %p (type %d)",
1424 zvrf_id(zvrf), buf, rn, new,
1425 new->type, old, old->type);
1426 else
1427 zlog_debug(
1428 "%u:%s: Updating route rn %p, re %p (type %d)",
1429 zvrf_id(zvrf), buf, rn, new,
1430 new->type);
1431 }
1432
1433 /* If labeled-unicast route, uninstall transit LSP. */
1434 if (zebra_rib_labeled_unicast(old))
1435 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1436
1437 /* Non-system route should be installed. */
1438 if (!RIB_SYSTEM_ROUTE(new)) {
1439 /* If labeled-unicast route, install transit
1440 * LSP. */
1441 if (zebra_rib_labeled_unicast(new))
1442 zebra_mpls_lsp_install(zvrf, rn, new);
1443
1444 rib_install_kernel(rn, new, old);
1445 } else {
1446 /*
1447 * We do not need to install the
1448 * selected route because it
1449 * is already isntalled by
1450 * the system( ie not us )
1451 * so just mark it as winning
1452 * we do need to ensure that
1453 * if we uninstall a route
1454 * from ourselves we don't
1455 * over write this pointer
1456 */
1457 dest->selected_fib = NULL;
1458 }
1459 /* If install succeeded or system route, cleanup flags
1460 * for prior route. */
1461 if (new != old) {
1462 if (RIB_SYSTEM_ROUTE(new)) {
1463 if (!RIB_SYSTEM_ROUTE(old))
1464 rib_uninstall_kernel(rn, old);
1465 } else {
1466 for (nexthop = old->ng.nexthop; nexthop;
1467 nexthop = nexthop->next)
1468 UNSET_FLAG(nexthop->flags,
1469 NEXTHOP_FLAG_FIB);
1470 }
1471 }
1472 }
1473
1474 /*
1475 * If nexthop for selected route is not active or install
1476 * failed, we
1477 * may need to uninstall and delete for redistribution.
1478 */
1479 if (!nh_active) {
1480 if (IS_ZEBRA_DEBUG_RIB) {
1481 char buf[SRCDEST2STR_BUFFER];
1482 srcdest_rnode2str(rn, buf, sizeof(buf));
1483 if (new != old)
1484 zlog_debug(
1485 "%u:%s: Deleting route rn %p, re %p (type %d) "
1486 "old %p (type %d) - nexthop inactive",
1487 zvrf_id(zvrf), buf, rn, new,
1488 new->type, old, old->type);
1489 else
1490 zlog_debug(
1491 "%u:%s: Deleting route rn %p, re %p (type %d) - nexthop inactive",
1492 zvrf_id(zvrf), buf, rn, new,
1493 new->type);
1494 }
1495
1496 /* If labeled-unicast route, uninstall transit LSP. */
1497 if (zebra_rib_labeled_unicast(old))
1498 zebra_mpls_lsp_uninstall(zvrf, rn, old);
1499
1500 if (!RIB_SYSTEM_ROUTE(old))
1501 rib_uninstall_kernel(rn, old);
1502 else
1503 dest->selected_fib = NULL;
1504 }
1505 } else {
1506 /*
1507 * Same route selected; check if in the FIB and if not,
1508 * re-install. This
1509 * is housekeeping code to deal with race conditions in kernel
1510 * with linux
1511 * netlink reporting interface up before IPv4 or IPv6 protocol
1512 * is ready
1513 * to add routes.
1514 */
1515 if (!RIB_SYSTEM_ROUTE(new)) {
1516 bool in_fib = false;
1517
1518 for (ALL_NEXTHOPS(new->ng, nexthop))
1519 if (CHECK_FLAG(nexthop->flags,
1520 NEXTHOP_FLAG_FIB)) {
1521 in_fib = true;
1522 break;
1523 }
1524 if (!in_fib)
1525 rib_install_kernel(rn, new, NULL);
1526 }
1527 }
1528
1529 /* Update prior route. */
1530 if (new != old) {
1531 /* Set real nexthop. */
1532 nexthop_active_update(rn, old, 1);
1533 UNSET_FLAG(old->status, ROUTE_ENTRY_CHANGED);
1534 }
1535
1536 /* Clear changed flag. */
1537 UNSET_FLAG(new->status, ROUTE_ENTRY_CHANGED);
1538 }
1539
1540 /* Check if 'alternate' RIB entry is better than 'current'. */
1541 static struct route_entry *rib_choose_best(struct route_entry *current,
1542 struct route_entry *alternate)
1543 {
1544 if (current == NULL)
1545 return alternate;
1546
1547 /* filter route selection in following order:
1548 * - connected beats other types
1549 * - if both connected, loopback or vrf wins
1550 * - lower distance beats higher
1551 * - lower metric beats higher for equal distance
1552 * - last, hence oldest, route wins tie break.
1553 */
1554
1555 /* Connected routes. Check to see if either are a vrf
1556 * or loopback interface. If not, pick the last connected
1557 * route of the set of lowest metric connected routes.
1558 */
1559 if (alternate->type == ZEBRA_ROUTE_CONNECT) {
1560 if (current->type != ZEBRA_ROUTE_CONNECT)
1561 return alternate;
1562
1563 /* both are connected. are either loop or vrf? */
1564 struct nexthop *nexthop = NULL;
1565
1566 for (ALL_NEXTHOPS(alternate->ng, nexthop)) {
1567 if (if_is_loopback_or_vrf(if_lookup_by_index(
1568 nexthop->ifindex, alternate->vrf_id)))
1569 return alternate;
1570 }
1571
1572 for (ALL_NEXTHOPS(current->ng, nexthop)) {
1573 if (if_is_loopback_or_vrf(if_lookup_by_index(
1574 nexthop->ifindex, current->vrf_id)))
1575 return current;
1576 }
1577
1578 /* Neither are loop or vrf so pick best metric */
1579 if (alternate->metric <= current->metric)
1580 return alternate;
1581
1582 return current;
1583 }
1584
1585 if (current->type == ZEBRA_ROUTE_CONNECT)
1586 return current;
1587
1588 /* higher distance loses */
1589 if (alternate->distance < current->distance)
1590 return alternate;
1591 if (current->distance < alternate->distance)
1592 return current;
1593
1594 /* metric tie-breaks equal distance */
1595 if (alternate->metric <= current->metric)
1596 return alternate;
1597
1598 return current;
1599 }
1600
1601 /* Core function for processing routing information base. */
1602 static void rib_process(struct route_node *rn)
1603 {
1604 struct route_entry *re;
1605 struct route_entry *next;
1606 struct route_entry *old_selected = NULL;
1607 struct route_entry *new_selected = NULL;
1608 struct route_entry *old_fib = NULL;
1609 struct route_entry *new_fib = NULL;
1610 struct route_entry *best = NULL;
1611 char buf[SRCDEST2STR_BUFFER];
1612 rib_dest_t *dest;
1613 struct zebra_vrf *zvrf = NULL;
1614 const struct prefix *p, *src_p;
1615
1616 srcdest_rnode_prefixes(rn, &p, &src_p);
1617 vrf_id_t vrf_id = VRF_UNKNOWN;
1618
1619 assert(rn);
1620
1621 dest = rib_dest_from_rnode(rn);
1622 if (dest) {
1623 zvrf = rib_dest_vrf(dest);
1624 vrf_id = zvrf_id(zvrf);
1625 }
1626
1627 if (IS_ZEBRA_DEBUG_RIB)
1628 srcdest_rnode2str(rn, buf, sizeof(buf));
1629
1630 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1631 zlog_debug("%u:%s: Processing rn %p", vrf_id, buf, rn);
1632
1633 /*
1634 * we can have rn's that have a NULL info pointer
1635 * (dest). As such let's not let the deref happen
1636 * additionally we know RNODE_FOREACH_RE_SAFE
1637 * will not iterate so we are ok.
1638 */
1639 if (dest)
1640 old_fib = dest->selected_fib;
1641
1642 RNODE_FOREACH_RE_SAFE (rn, re, next) {
1643 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1644 zlog_debug(
1645 "%u:%s: Examine re %p (type %d) status %x flags %x "
1646 "dist %d metric %d",
1647 vrf_id, buf, re, re->type, re->status,
1648 re->flags, re->distance, re->metric);
1649
1650 UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
1651
1652 /* Currently selected re. */
1653 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)) {
1654 assert(old_selected == NULL);
1655 old_selected = re;
1656 }
1657
1658 /* Skip deleted entries from selection */
1659 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
1660 continue;
1661
1662 /* Skip unreachable nexthop. */
1663 /* This first call to nexthop_active_update is merely to
1664 * determine if
1665 * there's any change to nexthops associated with this RIB
1666 * entry. Now,
1667 * rib_process() can be invoked due to an external event such as
1668 * link
1669 * down or due to next-hop-tracking evaluation. In the latter
1670 * case,
1671 * a decision has already been made that the NHs have changed.
1672 * So, no
1673 * need to invoke a potentially expensive call again. Further,
1674 * since
1675 * the change might be in a recursive NH which is not caught in
1676 * the nexthop_active_update() code. Thus, we might miss changes
1677 * to
1678 * recursive NHs.
1679 */
1680 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_CHANGED)
1681 && !nexthop_active_update(rn, re, 0)) {
1682 if (re->type == ZEBRA_ROUTE_TABLE) {
1683 /* XXX: HERE BE DRAGONS!!!!!
1684 * In all honesty, I have not yet figured out
1685 * what this part
1686 * does or why the ROUTE_ENTRY_CHANGED test
1687 * above is correct
1688 * or why we need to delete a route here, and
1689 * also not whether
1690 * this concerns both selected and fib route, or
1691 * only selected
1692 * or only fib */
1693 /* This entry was denied by the 'ip protocol
1694 * table' route-map, we
1695 * need to delete it */
1696 if (re != old_selected) {
1697 if (IS_ZEBRA_DEBUG_RIB)
1698 zlog_debug(
1699 "%s: %u:%s: imported via import-table but denied "
1700 "by the ip protocol table route-map",
1701 __func__, vrf_id, buf);
1702 rib_unlink(rn, re);
1703 } else
1704 SET_FLAG(re->status,
1705 ROUTE_ENTRY_REMOVED);
1706 }
1707
1708 continue;
1709 }
1710
1711 /* Infinite distance. */
1712 if (re->distance == DISTANCE_INFINITY) {
1713 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1714 continue;
1715 }
1716
1717 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
1718 best = rib_choose_best(new_fib, re);
1719 if (new_fib && best != new_fib)
1720 UNSET_FLAG(new_fib->status,
1721 ROUTE_ENTRY_CHANGED);
1722 new_fib = best;
1723 } else {
1724 best = rib_choose_best(new_selected, re);
1725 if (new_selected && best != new_selected)
1726 UNSET_FLAG(new_selected->status,
1727 ROUTE_ENTRY_CHANGED);
1728 new_selected = best;
1729 }
1730 if (best != re)
1731 UNSET_FLAG(re->status, ROUTE_ENTRY_CHANGED);
1732 } /* RNODE_FOREACH_RE */
1733
1734 /* If no FIB override route, use the selected route also for FIB */
1735 if (new_fib == NULL)
1736 new_fib = new_selected;
1737
1738 /* After the cycle is finished, the following pointers will be set:
1739 * old_selected --- RE entry currently having SELECTED
1740 * new_selected --- RE entry that is newly SELECTED
1741 * old_fib --- RE entry currently in kernel FIB
1742 * new_fib --- RE entry that is newly to be in kernel FIB
1743 *
1744 * new_selected will get SELECTED flag, and is going to be redistributed
1745 * the zclients. new_fib (which can be new_selected) will be installed
1746 * in kernel.
1747 */
1748
1749 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1750 zlog_debug(
1751 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1752 vrf_id, buf, (void *)old_selected, (void *)new_selected,
1753 (void *)old_fib, (void *)new_fib);
1754 }
1755
1756 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1757 * fib == selected */
1758 bool selected_changed = new_selected && CHECK_FLAG(new_selected->status,
1759 ROUTE_ENTRY_CHANGED);
1760
1761 /* Update fib according to selection results */
1762 if (new_fib && old_fib)
1763 rib_process_update_fib(zvrf, rn, old_fib, new_fib);
1764 else if (new_fib)
1765 rib_process_add_fib(zvrf, rn, new_fib);
1766 else if (old_fib)
1767 rib_process_del_fib(zvrf, rn, old_fib);
1768
1769 /* Redistribute SELECTED entry */
1770 if (old_selected != new_selected || selected_changed) {
1771 struct nexthop *nexthop = NULL;
1772
1773 /* Check if we have a FIB route for the destination, otherwise,
1774 * don't redistribute it */
1775 if (new_fib) {
1776 for (ALL_NEXTHOPS(new_fib->ng, nexthop)) {
1777 if (CHECK_FLAG(nexthop->flags,
1778 NEXTHOP_FLAG_FIB)) {
1779 break;
1780 }
1781 }
1782 }
1783 if (!nexthop)
1784 new_selected = NULL;
1785
1786 if (new_selected && new_selected != new_fib) {
1787 nexthop_active_update(rn, new_selected, 1);
1788 UNSET_FLAG(new_selected->status, ROUTE_ENTRY_CHANGED);
1789 }
1790
1791 if (old_selected) {
1792 if (!new_selected)
1793 redistribute_delete(p, src_p, old_selected);
1794 if (old_selected != new_selected)
1795 UNSET_FLAG(old_selected->flags,
1796 ZEBRA_FLAG_SELECTED);
1797 }
1798
1799 if (new_selected) {
1800 /* Install new or replace existing redistributed entry
1801 */
1802 SET_FLAG(new_selected->flags, ZEBRA_FLAG_SELECTED);
1803 redistribute_update(p, src_p, new_selected,
1804 old_selected);
1805 }
1806 }
1807
1808 /* Remove all RE entries queued for removal */
1809 RNODE_FOREACH_RE_SAFE (rn, re, next) {
1810 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
1811 if (IS_ZEBRA_DEBUG_RIB) {
1812 rnode_debug(rn, vrf_id, "rn %p, removing re %p",
1813 (void *)rn, (void *)re);
1814 }
1815 rib_unlink(rn, re);
1816 }
1817 }
1818
1819 /*
1820 * Check if the dest can be deleted now.
1821 */
1822 rib_gc_dest(rn);
1823 }
1824
1825 /* Take a list of route_node structs and return 1, if there was a record
1826 * picked from it and processed by rib_process(). Don't process more,
1827 * than one RN record; operate only in the specified sub-queue.
1828 */
1829 static unsigned int process_subq(struct list *subq, uint8_t qindex)
1830 {
1831 struct listnode *lnode = listhead(subq);
1832 struct route_node *rnode;
1833 rib_dest_t *dest;
1834 struct zebra_vrf *zvrf = NULL;
1835
1836 if (!lnode)
1837 return 0;
1838
1839 rnode = listgetdata(lnode);
1840 dest = rib_dest_from_rnode(rnode);
1841 if (dest)
1842 zvrf = rib_dest_vrf(dest);
1843
1844 rib_process(rnode);
1845
1846 if (IS_ZEBRA_DEBUG_RIB_DETAILED) {
1847 char buf[SRCDEST2STR_BUFFER];
1848 srcdest_rnode2str(rnode, buf, sizeof(buf));
1849 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1850 zvrf ? zvrf_id(zvrf) : 0, buf, rnode, qindex);
1851 }
1852
1853 if (rnode->info)
1854 UNSET_FLAG(rib_dest_from_rnode(rnode)->flags,
1855 RIB_ROUTE_QUEUED(qindex));
1856
1857 #if 0
1858 else
1859 {
1860 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1861 __func__, rnode, rnode->lock);
1862 zlog_backtrace(LOG_DEBUG);
1863 }
1864 #endif
1865 route_unlock_node(rnode);
1866 list_delete_node(subq, lnode);
1867 return 1;
1868 }
1869
1870 /*
1871 * All meta queues have been processed. Trigger next-hop evaluation.
1872 */
1873 static void meta_queue_process_complete(struct work_queue *dummy)
1874 {
1875 struct vrf *vrf;
1876 struct zebra_vrf *zvrf;
1877
1878 /* Evaluate nexthops for those VRFs which underwent route processing.
1879 * This
1880 * should limit the evaluation to the necessary VRFs in most common
1881 * situations.
1882 */
1883 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1884 zvrf = vrf->info;
1885 if (zvrf == NULL || !(zvrf->flags & ZEBRA_VRF_RIB_SCHEDULED))
1886 continue;
1887
1888 zvrf->flags &= ~ZEBRA_VRF_RIB_SCHEDULED;
1889 zebra_evaluate_rnh(zvrf_id(zvrf), AF_INET, 0, RNH_NEXTHOP_TYPE,
1890 NULL);
1891 zebra_evaluate_rnh(zvrf_id(zvrf), AF_INET, 0,
1892 RNH_IMPORT_CHECK_TYPE, NULL);
1893 zebra_evaluate_rnh(zvrf_id(zvrf), AF_INET6, 0, RNH_NEXTHOP_TYPE,
1894 NULL);
1895 zebra_evaluate_rnh(zvrf_id(zvrf), AF_INET6, 0,
1896 RNH_IMPORT_CHECK_TYPE, NULL);
1897 }
1898
1899 /* Schedule LSPs for processing, if needed. */
1900 zvrf = vrf_info_lookup(VRF_DEFAULT);
1901 if (mpls_should_lsps_be_processed(zvrf)) {
1902 if (IS_ZEBRA_DEBUG_MPLS)
1903 zlog_debug(
1904 "%u: Scheduling all LSPs upon RIB completion",
1905 zvrf_id(zvrf));
1906 zebra_mpls_lsp_schedule(zvrf);
1907 mpls_unmark_lsps_for_processing(zvrf);
1908 }
1909 }
1910
1911 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1912 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
1913 * data
1914 * is pointed to the meta queue structure.
1915 */
1916 static wq_item_status meta_queue_process(struct work_queue *dummy, void *data)
1917 {
1918 struct meta_queue *mq = data;
1919 unsigned i;
1920
1921 for (i = 0; i < MQ_SIZE; i++)
1922 if (process_subq(mq->subq[i], i)) {
1923 mq->size--;
1924 break;
1925 }
1926 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
1927 }
1928
1929 /*
1930 * Map from rib types to queue type (priority) in meta queue
1931 */
1932 static const uint8_t meta_queue_map[ZEBRA_ROUTE_MAX] = {
1933 [ZEBRA_ROUTE_SYSTEM] = 4,
1934 [ZEBRA_ROUTE_KERNEL] = 0,
1935 [ZEBRA_ROUTE_CONNECT] = 0,
1936 [ZEBRA_ROUTE_STATIC] = 1,
1937 [ZEBRA_ROUTE_RIP] = 2,
1938 [ZEBRA_ROUTE_RIPNG] = 2,
1939 [ZEBRA_ROUTE_OSPF] = 2,
1940 [ZEBRA_ROUTE_OSPF6] = 2,
1941 [ZEBRA_ROUTE_ISIS] = 2,
1942 [ZEBRA_ROUTE_BGP] = 3,
1943 [ZEBRA_ROUTE_PIM] = 4, // Shouldn't happen but for safety
1944 [ZEBRA_ROUTE_EIGRP] = 2,
1945 [ZEBRA_ROUTE_NHRP] = 2,
1946 [ZEBRA_ROUTE_HSLS] = 4,
1947 [ZEBRA_ROUTE_OLSR] = 4,
1948 [ZEBRA_ROUTE_TABLE] = 1,
1949 [ZEBRA_ROUTE_LDP] = 4,
1950 [ZEBRA_ROUTE_VNC] = 3,
1951 [ZEBRA_ROUTE_VNC_DIRECT] = 3,
1952 [ZEBRA_ROUTE_VNC_DIRECT_RH] = 3,
1953 [ZEBRA_ROUTE_BGP_DIRECT] = 3,
1954 [ZEBRA_ROUTE_BGP_DIRECT_EXT] = 3,
1955 [ZEBRA_ROUTE_BABEL] = 2,
1956 [ZEBRA_ROUTE_ALL] = 4, // Shouldn't happen but for safety
1957 };
1958
1959 /* Look into the RN and queue it into one or more priority queues,
1960 * increasing the size for each data push done.
1961 */
1962 static void rib_meta_queue_add(struct meta_queue *mq, struct route_node *rn)
1963 {
1964 struct route_entry *re;
1965
1966 RNODE_FOREACH_RE (rn, re) {
1967 uint8_t qindex = meta_queue_map[re->type];
1968 struct zebra_vrf *zvrf;
1969
1970 /* Invariant: at this point we always have rn->info set. */
1971 if (CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
1972 RIB_ROUTE_QUEUED(qindex))) {
1973 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1974 rnode_debug(
1975 rn, re->vrf_id,
1976 "rn %p is already queued in sub-queue %u",
1977 (void *)rn, qindex);
1978 continue;
1979 }
1980
1981 SET_FLAG(rib_dest_from_rnode(rn)->flags,
1982 RIB_ROUTE_QUEUED(qindex));
1983 listnode_add(mq->subq[qindex], rn);
1984 route_lock_node(rn);
1985 mq->size++;
1986
1987 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
1988 rnode_debug(rn, re->vrf_id,
1989 "queued rn %p into sub-queue %u",
1990 (void *)rn, qindex);
1991
1992 zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
1993 if (zvrf)
1994 zvrf->flags |= ZEBRA_VRF_RIB_SCHEDULED;
1995 }
1996 }
1997
1998 /* Add route_node to work queue and schedule processing */
1999 void rib_queue_add(struct route_node *rn)
2000 {
2001 assert(rn);
2002
2003 /* Pointless to queue a route_node with no RIB entries to add or remove
2004 */
2005 if (!rnode_to_ribs(rn)) {
2006 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
2007 __func__, (void *)rn, rn->lock);
2008 zlog_backtrace(LOG_DEBUG);
2009 return;
2010 }
2011
2012 if (zebrad.ribq == NULL) {
2013 flog_err(ZEBRA_ERR_WQ_NONEXISTENT,
2014 "%s: work_queue does not exist!", __func__);
2015 return;
2016 }
2017
2018 /*
2019 * The RIB queue should normally be either empty or holding the only
2020 * work_queue_item element. In the latter case this element would
2021 * hold a pointer to the meta queue structure, which must be used to
2022 * actually queue the route nodes to process. So create the MQ
2023 * holder, if necessary, then push the work into it in any case.
2024 * This semantics was introduced after 0.99.9 release.
2025 */
2026 if (work_queue_empty(zebrad.ribq))
2027 work_queue_add(zebrad.ribq, zebrad.mq);
2028
2029 rib_meta_queue_add(zebrad.mq, rn);
2030
2031 return;
2032 }
2033
2034 /* Create new meta queue.
2035 A destructor function doesn't seem to be necessary here.
2036 */
2037 static struct meta_queue *meta_queue_new(void)
2038 {
2039 struct meta_queue *new;
2040 unsigned i;
2041
2042 new = XCALLOC(MTYPE_WORK_QUEUE, sizeof(struct meta_queue));
2043
2044 for (i = 0; i < MQ_SIZE; i++) {
2045 new->subq[i] = list_new();
2046 assert(new->subq[i]);
2047 }
2048
2049 return new;
2050 }
2051
2052 void meta_queue_free(struct meta_queue *mq)
2053 {
2054 unsigned i;
2055
2056 for (i = 0; i < MQ_SIZE; i++)
2057 list_delete_and_null(&mq->subq[i]);
2058
2059 XFREE(MTYPE_WORK_QUEUE, mq);
2060 }
2061
2062 /* initialise zebra rib work queue */
2063 static void rib_queue_init(struct zebra_t *zebra)
2064 {
2065 assert(zebra);
2066
2067 if (!(zebra->ribq =
2068 work_queue_new(zebra->master, "route_node processing"))) {
2069 flog_err(ZEBRA_ERR_WQ_NONEXISTENT,
2070 "%s: could not initialise work queue!", __func__);
2071 return;
2072 }
2073
2074 /* fill in the work queue spec */
2075 zebra->ribq->spec.workfunc = &meta_queue_process;
2076 zebra->ribq->spec.errorfunc = NULL;
2077 zebra->ribq->spec.completion_func = &meta_queue_process_complete;
2078 /* XXX: TODO: These should be runtime configurable via vty */
2079 zebra->ribq->spec.max_retries = 3;
2080 zebra->ribq->spec.hold = ZEBRA_RIB_PROCESS_HOLD_TIME;
2081
2082 if (!(zebra->mq = meta_queue_new())) {
2083 flog_err(ZEBRA_ERR_WQ_NONEXISTENT,
2084 "%s: could not initialise meta queue!", __func__);
2085 return;
2086 }
2087 return;
2088 }
2089
2090 /* RIB updates are processed via a queue of pointers to route_nodes.
2091 *
2092 * The queue length is bounded by the maximal size of the routing table,
2093 * as a route_node will not be requeued, if already queued.
2094 *
2095 * REs are submitted via rib_addnode or rib_delnode which set minimal
2096 * state, or static_install_route (when an existing RE is updated)
2097 * and then submit route_node to queue for best-path selection later.
2098 * Order of add/delete state changes are preserved for any given RE.
2099 *
2100 * Deleted REs are reaped during best-path selection.
2101 *
2102 * rib_addnode
2103 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
2104 * |-------->| | best RE, if required
2105 * | |
2106 * static_install->|->rib_addqueue...... -> rib_process
2107 * | |
2108 * |-------->| |-> rib_unlink
2109 * |-> set ROUTE_ENTRY_REMOVE |
2110 * rib_delnode (RE freed)
2111 *
2112 * The 'info' pointer of a route_node points to a rib_dest_t
2113 * ('dest'). Queueing state for a route_node is kept on the dest. The
2114 * dest is created on-demand by rib_link() and is kept around at least
2115 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2116 *
2117 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2118 *
2119 * - route_nodes: refcounted by:
2120 * - dest attached to route_node:
2121 * - managed by: rib_link/rib_gc_dest
2122 * - route_node processing queue
2123 * - managed by: rib_addqueue, rib_process.
2124 *
2125 */
2126
2127 /* Add RE to head of the route node. */
2128 static void rib_link(struct route_node *rn, struct route_entry *re, int process)
2129 {
2130 struct route_entry *head;
2131 rib_dest_t *dest;
2132 afi_t afi;
2133 const char *rmap_name;
2134
2135 assert(re && rn);
2136
2137 dest = rib_dest_from_rnode(rn);
2138 if (!dest) {
2139 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2140 rnode_debug(rn, re->vrf_id, "rn %p adding dest", rn);
2141
2142 dest = XCALLOC(MTYPE_RIB_DEST, sizeof(rib_dest_t));
2143 route_lock_node(rn); /* rn route table reference */
2144 rn->info = dest;
2145 dest->rnode = rn;
2146 }
2147
2148 head = dest->routes;
2149 if (head) {
2150 head->prev = re;
2151 }
2152 re->next = head;
2153 dest->routes = re;
2154
2155 afi = (rn->p.family == AF_INET)
2156 ? AFI_IP
2157 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2158 if (is_zebra_import_table_enabled(afi, re->table)) {
2159 rmap_name = zebra_get_import_table_route_map(afi, re->table);
2160 zebra_add_import_table_entry(rn, re, rmap_name);
2161 } else if (process)
2162 rib_queue_add(rn);
2163 }
2164
2165 static void rib_addnode(struct route_node *rn,
2166 struct route_entry *re, int process)
2167 {
2168 /* RE node has been un-removed before route-node is processed.
2169 * route_node must hence already be on the queue for processing..
2170 */
2171 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
2172 if (IS_ZEBRA_DEBUG_RIB)
2173 rnode_debug(rn, re->vrf_id, "rn %p, un-removed re %p",
2174 (void *)rn, (void *)re);
2175
2176 UNSET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2177 return;
2178 }
2179 rib_link(rn, re, process);
2180 }
2181
2182 /*
2183 * rib_unlink
2184 *
2185 * Detach a rib structure from a route_node.
2186 *
2187 * Note that a call to rib_unlink() should be followed by a call to
2188 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2189 * longer required to be deleted.
2190 */
2191 void rib_unlink(struct route_node *rn, struct route_entry *re)
2192 {
2193 rib_dest_t *dest;
2194
2195 assert(rn && re);
2196
2197 if (IS_ZEBRA_DEBUG_RIB)
2198 rnode_debug(rn, re->vrf_id, "rn %p, re %p", (void *)rn,
2199 (void *)re);
2200
2201 dest = rib_dest_from_rnode(rn);
2202
2203 if (re->next)
2204 re->next->prev = re->prev;
2205
2206 if (re->prev)
2207 re->prev->next = re->next;
2208 else {
2209 dest->routes = re->next;
2210 }
2211
2212 if (dest->selected_fib == re)
2213 dest->selected_fib = NULL;
2214
2215 nexthops_free(re->ng.nexthop);
2216 XFREE(MTYPE_RE, re);
2217 }
2218
2219 void rib_delnode(struct route_node *rn, struct route_entry *re)
2220 {
2221 afi_t afi;
2222
2223 if (IS_ZEBRA_DEBUG_RIB)
2224 rnode_debug(rn, re->vrf_id, "rn %p, re %p, removing",
2225 (void *)rn, (void *)re);
2226 SET_FLAG(re->status, ROUTE_ENTRY_REMOVED);
2227
2228 afi = (rn->p.family == AF_INET)
2229 ? AFI_IP
2230 : (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX;
2231 if (is_zebra_import_table_enabled(afi, re->table)) {
2232 zebra_del_import_table_entry(rn, re);
2233 /* Just clean up if non main table */
2234 if (IS_ZEBRA_DEBUG_RIB) {
2235 char buf[SRCDEST2STR_BUFFER];
2236 srcdest_rnode2str(rn, buf, sizeof(buf));
2237 zlog_debug(
2238 "%u:%s: Freeing route rn %p, re %p (type %d)",
2239 re->vrf_id, buf, rn, re, re->type);
2240 }
2241
2242 rib_unlink(rn, re);
2243 } else {
2244 rib_queue_add(rn);
2245 }
2246 }
2247
2248 /* This function dumps the contents of a given RE entry into
2249 * standard debug log. Calling function name and IP prefix in
2250 * question are passed as 1st and 2nd arguments.
2251 */
2252
2253 void _route_entry_dump(const char *func, union prefixconstptr pp,
2254 union prefixconstptr src_pp,
2255 const struct route_entry *re)
2256 {
2257 const struct prefix *src_p = src_pp.p;
2258 bool is_srcdst = src_p && src_p->prefixlen;
2259 char straddr[PREFIX_STRLEN];
2260 char srcaddr[PREFIX_STRLEN];
2261 struct nexthop *nexthop;
2262
2263 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func,
2264 (const void *)re, prefix2str(pp, straddr, sizeof(straddr)),
2265 is_srcdst ? " from " : "",
2266 is_srcdst ? prefix2str(src_pp, srcaddr, sizeof(srcaddr))
2267 : "",
2268 re->vrf_id);
2269 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2270 func, (unsigned long)re->uptime, re->type, re->instance,
2271 re->table);
2272 zlog_debug(
2273 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2274 func, re->metric, re->mtu, re->distance, re->flags, re->status);
2275 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func,
2276 re->nexthop_num, re->nexthop_active_num);
2277
2278 for (ALL_NEXTHOPS(re->ng, nexthop)) {
2279 struct interface *ifp;
2280 struct vrf *vrf = vrf_lookup_by_id(nexthop->vrf_id);
2281
2282 switch (nexthop->type) {
2283 case NEXTHOP_TYPE_BLACKHOLE:
2284 sprintf(straddr, "Blackhole");
2285 break;
2286 case NEXTHOP_TYPE_IFINDEX:
2287 ifp = if_lookup_by_index(nexthop->ifindex,
2288 nexthop->vrf_id);
2289 sprintf(straddr, "%s", ifp ? ifp->name : "Unknown");
2290 break;
2291 case NEXTHOP_TYPE_IPV4:
2292 /* fallthrough */
2293 case NEXTHOP_TYPE_IPV4_IFINDEX:
2294 inet_ntop(AF_INET, &nexthop->gate, straddr,
2295 INET6_ADDRSTRLEN);
2296 break;
2297 case NEXTHOP_TYPE_IPV6:
2298 case NEXTHOP_TYPE_IPV6_IFINDEX:
2299 inet_ntop(AF_INET6, &nexthop->gate, straddr,
2300 INET6_ADDRSTRLEN);
2301 break;
2302 }
2303 zlog_debug("%s: %s %s[%u] vrf %s(%u) with flags %s%s%s", func,
2304 (nexthop->rparent ? " NH" : "NH"), straddr,
2305 nexthop->ifindex, vrf ? vrf->name : "Unknown",
2306 nexthop->vrf_id,
2307 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)
2308 ? "ACTIVE "
2309 : ""),
2310 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
2311 ? "FIB "
2312 : ""),
2313 (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE)
2314 ? "RECURSIVE"
2315 : ""));
2316 }
2317 zlog_debug("%s: dump complete", func);
2318 }
2319
2320 /* This is an exported helper to rtm_read() to dump the strange
2321 * RE entry found by rib_lookup_ipv4_route()
2322 */
2323
2324 void rib_lookup_and_dump(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2325 {
2326 struct route_table *table;
2327 struct route_node *rn;
2328 struct route_entry *re;
2329 char prefix_buf[INET_ADDRSTRLEN];
2330
2331 /* Lookup table. */
2332 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
2333 if (!table) {
2334 flog_err(ZEBRA_ERR_TABLE_LOOKUP_FAILED,
2335 "%s:%u zebra_vrf_table() returned NULL", __func__,
2336 vrf_id);
2337 return;
2338 }
2339
2340 /* Scan the RIB table for exactly matching RE entry. */
2341 rn = route_node_lookup(table, (struct prefix *)p);
2342
2343 /* No route for this prefix. */
2344 if (!rn) {
2345 zlog_debug("%s:%u lookup failed for %s", __func__, vrf_id,
2346 prefix2str((struct prefix *)p, prefix_buf,
2347 sizeof(prefix_buf)));
2348 return;
2349 }
2350
2351 /* Unlock node. */
2352 route_unlock_node(rn);
2353
2354 /* let's go */
2355 RNODE_FOREACH_RE (rn, re) {
2356 zlog_debug("%s:%u rn %p, re %p: %s, %s",
2357 __func__, vrf_id,
2358 (void *)rn, (void *)re,
2359 (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)
2360 ? "removed"
2361 : "NOT removed"),
2362 (CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)
2363 ? "selected"
2364 : "NOT selected"));
2365 route_entry_dump(p, NULL, re);
2366 }
2367 }
2368
2369 /* Check if requested address assignment will fail due to another
2370 * route being installed by zebra in FIB already. Take necessary
2371 * actions, if needed: remove such a route from FIB and deSELECT
2372 * corresponding RE entry. Then put affected RN into RIBQ head.
2373 */
2374 void rib_lookup_and_pushup(struct prefix_ipv4 *p, vrf_id_t vrf_id)
2375 {
2376 struct route_table *table;
2377 struct route_node *rn;
2378 unsigned changed = 0;
2379 rib_dest_t *dest;
2380
2381 if (NULL == (table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id))) {
2382 flog_err(ZEBRA_ERR_TABLE_LOOKUP_FAILED,
2383 "%s:%u zebra_vrf_table() returned NULL", __func__,
2384 vrf_id);
2385 return;
2386 }
2387
2388 /* No matches would be the simplest case. */
2389 if (NULL == (rn = route_node_lookup(table, (struct prefix *)p)))
2390 return;
2391
2392 /* Unlock node. */
2393 route_unlock_node(rn);
2394
2395 dest = rib_dest_from_rnode(rn);
2396 /* Check all RE entries. In case any changes have to be done, requeue
2397 * the RN into RIBQ head. If the routing message about the new connected
2398 * route (generated by the IP address we are going to assign very soon)
2399 * comes before the RIBQ is processed, the new RE entry will join
2400 * RIBQ record already on head. This is necessary for proper
2401 * revalidation
2402 * of the rest of the RE.
2403 */
2404 if (dest->selected_fib && !RIB_SYSTEM_ROUTE(dest->selected_fib)) {
2405 changed = 1;
2406 if (IS_ZEBRA_DEBUG_RIB) {
2407 char buf[PREFIX_STRLEN];
2408
2409 zlog_debug("%u:%s: freeing way for connected prefix",
2410 dest->selected_fib->vrf_id,
2411 prefix2str(&rn->p, buf, sizeof(buf)));
2412 route_entry_dump(&rn->p, NULL, dest->selected_fib);
2413 }
2414 rib_uninstall(rn, dest->selected_fib);
2415 }
2416 if (changed)
2417 rib_queue_add(rn);
2418 }
2419
2420 int rib_add_multipath(afi_t afi, safi_t safi, struct prefix *p,
2421 struct prefix_ipv6 *src_p, struct route_entry *re)
2422 {
2423 struct route_table *table;
2424 struct route_node *rn;
2425 struct route_entry *same = NULL;
2426 struct nexthop *nexthop;
2427 int ret = 0;
2428
2429 if (!re)
2430 return 0;
2431
2432 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
2433
2434 /* Lookup table. */
2435 table = zebra_vrf_table_with_table_id(afi, safi, re->vrf_id, re->table);
2436 if (!table) {
2437 XFREE(MTYPE_RE, re);
2438 return 0;
2439 }
2440
2441 /* Make it sure prefixlen is applied to the prefix. */
2442 apply_mask(p);
2443 if (src_p)
2444 apply_mask_ipv6(src_p);
2445
2446 /* Set default distance by route type. */
2447 if (re->distance == 0) {
2448 re->distance = route_distance(re->type);
2449
2450 /* iBGP distance is 200. */
2451 if (re->type == ZEBRA_ROUTE_BGP
2452 && CHECK_FLAG(re->flags, ZEBRA_FLAG_IBGP))
2453 re->distance = 200;
2454 }
2455
2456 /* Lookup route node.*/
2457 rn = srcdest_rnode_get(table, p, src_p);
2458
2459 /*
2460 * If same type of route are installed, treat it as a implicit
2461 * withdraw.
2462 * If the user has specified the No route replace semantics
2463 * for the install don't do a route replace.
2464 */
2465 RNODE_FOREACH_RE (rn, same) {
2466 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
2467 continue;
2468
2469 if (same->type != re->type)
2470 continue;
2471 if (same->instance != re->instance)
2472 continue;
2473 if (same->type == ZEBRA_ROUTE_KERNEL
2474 && same->metric != re->metric)
2475 continue;
2476
2477 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2478 same->distance != re->distance)
2479 continue;
2480
2481 /*
2482 * We should allow duplicate connected routes
2483 * because of IPv6 link-local routes and unnumbered
2484 * interfaces on Linux.
2485 */
2486 if (same->type != ZEBRA_ROUTE_CONNECT)
2487 break;
2488 }
2489
2490 /* If this route is kernel route, set FIB flag to the route. */
2491 if (RIB_SYSTEM_ROUTE(re))
2492 for (nexthop = re->ng.nexthop; nexthop; nexthop = nexthop->next)
2493 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2494
2495 /* Link new re to node.*/
2496 if (IS_ZEBRA_DEBUG_RIB) {
2497 rnode_debug(
2498 rn, re->vrf_id,
2499 "Inserting route rn %p, re %p (type %d) existing %p",
2500 (void *)rn, (void *)re, re->type, (void *)same);
2501
2502 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
2503 route_entry_dump(p, src_p, re);
2504 }
2505 rib_addnode(rn, re, 1);
2506 ret = 1;
2507
2508 /* Free implicit route.*/
2509 if (same) {
2510 rib_delnode(rn, same);
2511 ret = -1;
2512 }
2513
2514 route_unlock_node(rn);
2515 return ret;
2516 }
2517
2518 void rib_delete(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
2519 unsigned short instance, int flags, struct prefix *p,
2520 struct prefix_ipv6 *src_p, const struct nexthop *nh,
2521 uint32_t table_id, uint32_t metric, uint8_t distance,
2522 bool fromkernel)
2523 {
2524 struct route_table *table;
2525 struct route_node *rn;
2526 struct route_entry *re;
2527 struct route_entry *fib = NULL;
2528 struct route_entry *same = NULL;
2529 struct nexthop *rtnh;
2530 char buf2[INET6_ADDRSTRLEN];
2531 rib_dest_t *dest;
2532
2533 assert(!src_p || !src_p->prefixlen || afi == AFI_IP6);
2534
2535 /* Lookup table. */
2536 table = zebra_vrf_table_with_table_id(afi, safi, vrf_id, table_id);
2537 if (!table)
2538 return;
2539
2540 /* Apply mask. */
2541 apply_mask(p);
2542 if (src_p)
2543 apply_mask_ipv6(src_p);
2544
2545 /* Lookup route node. */
2546 rn = srcdest_rnode_lookup(table, p, src_p);
2547 if (!rn) {
2548 char dst_buf[PREFIX_STRLEN], src_buf[PREFIX_STRLEN];
2549
2550 prefix2str(p, dst_buf, sizeof(dst_buf));
2551 if (src_p && src_p->prefixlen)
2552 prefix2str(src_p, src_buf, sizeof(src_buf));
2553 else
2554 src_buf[0] = '\0';
2555
2556 if (IS_ZEBRA_DEBUG_RIB)
2557 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id,
2558 dst_buf,
2559 (src_buf[0] != '\0') ? " from " : "",
2560 src_buf);
2561 return;
2562 }
2563
2564 dest = rib_dest_from_rnode(rn);
2565 fib = dest->selected_fib;
2566
2567 /* Lookup same type route. */
2568 RNODE_FOREACH_RE (rn, re) {
2569 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2570 continue;
2571
2572 if (re->type != type)
2573 continue;
2574 if (re->instance != instance)
2575 continue;
2576 if (CHECK_FLAG(re->flags, ZEBRA_FLAG_RR_USE_DISTANCE) &&
2577 distance != re->distance)
2578 continue;
2579
2580 if (re->type == ZEBRA_ROUTE_KERNEL && re->metric != metric)
2581 continue;
2582 if (re->type == ZEBRA_ROUTE_CONNECT && (rtnh = re->ng.nexthop)
2583 && rtnh->type == NEXTHOP_TYPE_IFINDEX && nh) {
2584 if (rtnh->ifindex != nh->ifindex)
2585 continue;
2586 same = re;
2587 break;
2588 }
2589 /* Make sure that the route found has the same gateway. */
2590 else {
2591 if (nh == NULL) {
2592 same = re;
2593 break;
2594 }
2595 for (ALL_NEXTHOPS(re->ng, rtnh))
2596 if (nexthop_same_no_recurse(rtnh, nh)) {
2597 same = re;
2598 break;
2599 }
2600 if (same)
2601 break;
2602 }
2603 }
2604 /* If same type of route can't be found and this message is from
2605 kernel. */
2606 if (!same) {
2607 /*
2608 * In the past(HA!) we could get here because
2609 * we were receiving a route delete from the
2610 * kernel and we're not marking the proto
2611 * as coming from it's appropriate originator.
2612 * Now that we are properly noticing the fact
2613 * that the kernel has deleted our route we
2614 * are not going to get called in this path
2615 * I am going to leave this here because
2616 * this might still work this way on non-linux
2617 * platforms as well as some weird state I have
2618 * not properly thought of yet.
2619 * If we can show that this code path is
2620 * dead then we can remove it.
2621 */
2622 if (fib && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) {
2623 if (IS_ZEBRA_DEBUG_RIB) {
2624 rnode_debug(
2625 rn, vrf_id,
2626 "rn %p, re %p (type %d) was deleted from kernel, adding",
2627 rn, fib, fib->type);
2628 }
2629 if (allow_delete) {
2630 /* Unset flags. */
2631 for (rtnh = fib->ng.nexthop; rtnh;
2632 rtnh = rtnh->next)
2633 UNSET_FLAG(rtnh->flags,
2634 NEXTHOP_FLAG_FIB);
2635
2636 /*
2637 * This is a non FRR route
2638 * as such we should mark
2639 * it as deleted
2640 */
2641 dest->selected_fib = NULL;
2642 } else {
2643 /* This means someone else, other than Zebra,
2644 * has deleted
2645 * a Zebra router from the kernel. We will add
2646 * it back */
2647 rib_install_kernel(rn, fib, NULL);
2648 }
2649 } else {
2650 if (IS_ZEBRA_DEBUG_RIB) {
2651 if (nh)
2652 rnode_debug(
2653 rn, vrf_id,
2654 "via %s ifindex %d type %d "
2655 "doesn't exist in rib",
2656 inet_ntop(afi2family(afi),
2657 &nh->gate, buf2,
2658 sizeof(buf2)),
2659 nh->ifindex, type);
2660 else
2661 rnode_debug(
2662 rn, vrf_id,
2663 "type %d doesn't exist in rib",
2664 type);
2665 }
2666 route_unlock_node(rn);
2667 return;
2668 }
2669 }
2670
2671 if (same) {
2672 if (fromkernel && CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)
2673 && !allow_delete) {
2674 rib_install_kernel(rn, same, NULL);
2675 route_unlock_node(rn);
2676
2677 return;
2678 }
2679
2680 if (CHECK_FLAG(flags, ZEBRA_FLAG_EVPN_ROUTE)) {
2681 struct nexthop *tmp_nh;
2682
2683 for (ALL_NEXTHOPS(re->ng, tmp_nh)) {
2684 struct ipaddr vtep_ip;
2685
2686 memset(&vtep_ip, 0, sizeof(struct ipaddr));
2687 if (afi == AFI_IP) {
2688 vtep_ip.ipa_type = IPADDR_V4;
2689 memcpy(&(vtep_ip.ipaddr_v4),
2690 &(tmp_nh->gate.ipv4),
2691 sizeof(struct in_addr));
2692 } else {
2693 vtep_ip.ipa_type = IPADDR_V6;
2694 memcpy(&(vtep_ip.ipaddr_v6),
2695 &(tmp_nh->gate.ipv6),
2696 sizeof(struct in6_addr));
2697 }
2698 zebra_vxlan_evpn_vrf_route_del(re->vrf_id,
2699 &vtep_ip, p);
2700 }
2701 }
2702 rib_delnode(rn, same);
2703 }
2704
2705 route_unlock_node(rn);
2706 return;
2707 }
2708
2709
2710 int rib_add(afi_t afi, safi_t safi, vrf_id_t vrf_id, int type,
2711 unsigned short instance, int flags, struct prefix *p,
2712 struct prefix_ipv6 *src_p, const struct nexthop *nh,
2713 uint32_t table_id, uint32_t metric, uint32_t mtu, uint8_t distance,
2714 route_tag_t tag)
2715 {
2716 struct route_entry *re;
2717 struct nexthop *nexthop;
2718
2719 /* Allocate new route_entry structure. */
2720 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
2721 re->type = type;
2722 re->instance = instance;
2723 re->distance = distance;
2724 re->flags = flags;
2725 re->metric = metric;
2726 re->mtu = mtu;
2727 re->table = table_id;
2728 re->vrf_id = vrf_id;
2729 re->nexthop_num = 0;
2730 re->uptime = time(NULL);
2731 re->tag = tag;
2732
2733 /* Add nexthop. */
2734 nexthop = nexthop_new();
2735 *nexthop = *nh;
2736 route_entry_nexthop_add(re, nexthop);
2737
2738 return rib_add_multipath(afi, safi, p, src_p, re);
2739 }
2740
2741 /* Schedule routes of a particular table (address-family) based on event. */
2742 void rib_update_table(struct route_table *table, rib_update_event_t event)
2743 {
2744 struct route_node *rn;
2745 struct route_entry *re, *next;
2746
2747 /* Walk all routes and queue for processing, if appropriate for
2748 * the trigger event.
2749 */
2750 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
2751 /*
2752 * If we are looking at a route node and the node
2753 * has already been queued we don't
2754 * need to queue it up again
2755 */
2756 if (rn->info && CHECK_FLAG(rib_dest_from_rnode(rn)->flags,
2757 RIB_ROUTE_ANY_QUEUED))
2758 continue;
2759 switch (event) {
2760 case RIB_UPDATE_IF_CHANGE:
2761 /* Examine all routes that won't get processed by the
2762 * protocol or
2763 * triggered by nexthop evaluation (NHT). This would be
2764 * system,
2765 * kernel and certain static routes. Note that NHT will
2766 * get
2767 * triggered upon an interface event as connected routes
2768 * always
2769 * get queued for processing.
2770 */
2771 RNODE_FOREACH_RE_SAFE (rn, re, next) {
2772 struct nexthop *nh;
2773
2774 if (re->type != ZEBRA_ROUTE_SYSTEM
2775 && re->type != ZEBRA_ROUTE_KERNEL
2776 && re->type != ZEBRA_ROUTE_CONNECT
2777 && re->type != ZEBRA_ROUTE_STATIC)
2778 continue;
2779
2780 if (re->type != ZEBRA_ROUTE_STATIC) {
2781 rib_queue_add(rn);
2782 continue;
2783 }
2784
2785 for (nh = re->ng.nexthop; nh; nh = nh->next)
2786 if (!(nh->type == NEXTHOP_TYPE_IPV4
2787 || nh->type == NEXTHOP_TYPE_IPV6))
2788 break;
2789
2790 /* If we only have nexthops to a
2791 * gateway, NHT will
2792 * take care.
2793 */
2794 if (nh)
2795 rib_queue_add(rn);
2796 }
2797 break;
2798
2799 case RIB_UPDATE_RMAP_CHANGE:
2800 case RIB_UPDATE_OTHER:
2801 /* Right now, examine all routes. Can restrict to a
2802 * protocol in
2803 * some cases (TODO).
2804 */
2805 if (rnode_to_ribs(rn))
2806 rib_queue_add(rn);
2807 break;
2808
2809 default:
2810 break;
2811 }
2812 }
2813 }
2814
2815 /* RIB update function. */
2816 void rib_update(vrf_id_t vrf_id, rib_update_event_t event)
2817 {
2818 struct route_table *table;
2819
2820 /* Process routes of interested address-families. */
2821 table = zebra_vrf_table(AFI_IP, SAFI_UNICAST, vrf_id);
2822 if (table) {
2823 if (IS_ZEBRA_DEBUG_EVENT)
2824 zlog_debug("%s : AFI_IP event %d", __func__, event);
2825 rib_update_table(table, event);
2826 }
2827
2828 table = zebra_vrf_table(AFI_IP6, SAFI_UNICAST, vrf_id);
2829 if (table) {
2830 if (IS_ZEBRA_DEBUG_EVENT)
2831 zlog_debug("%s : AFI_IP6 event %d", __func__, event);
2832 rib_update_table(table, event);
2833 }
2834 }
2835
2836 /* Delete self installed routes after zebra is relaunched. */
2837 void rib_sweep_table(struct route_table *table)
2838 {
2839 struct route_node *rn;
2840 struct route_entry *re;
2841 struct route_entry *next;
2842 struct nexthop *nexthop;
2843
2844 if (!table)
2845 return;
2846
2847 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
2848 RNODE_FOREACH_RE_SAFE (rn, re, next) {
2849 if (IS_ZEBRA_DEBUG_RIB)
2850 route_entry_dump(&rn->p, NULL, re);
2851
2852 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2853 continue;
2854
2855 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELFROUTE))
2856 continue;
2857
2858 /*
2859 * So we are starting up and have received
2860 * routes from the kernel that we have installed
2861 * from a previous run of zebra but not cleaned
2862 * up ( say a kill -9 )
2863 * But since we haven't actually installed
2864 * them yet( we received them from the kernel )
2865 * we don't think they are active.
2866 * So let's pretend they are active to actually
2867 * remove them.
2868 * In all honesty I'm not sure if we should
2869 * mark them as active when we receive them
2870 * This is startup only so probably ok.
2871 *
2872 * If we ever decide to move rib_sweep_table
2873 * to a different spot (ie startup )
2874 * this decision needs to be revisited
2875 */
2876 for (ALL_NEXTHOPS(re->ng, nexthop))
2877 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB);
2878
2879 rib_uninstall_kernel(rn, re);
2880 rib_delnode(rn, re);
2881 }
2882 }
2883 }
2884
2885 /* Sweep all RIB tables. */
2886 void rib_sweep_route(void)
2887 {
2888 struct vrf *vrf;
2889 struct zebra_vrf *zvrf;
2890
2891 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
2892 if ((zvrf = vrf->info) == NULL)
2893 continue;
2894
2895 rib_sweep_table(zvrf->table[AFI_IP][SAFI_UNICAST]);
2896 rib_sweep_table(zvrf->table[AFI_IP6][SAFI_UNICAST]);
2897 }
2898
2899 zebra_ns_sweep_route();
2900 }
2901
2902 /* Remove specific by protocol routes from 'table'. */
2903 unsigned long rib_score_proto_table(uint8_t proto, unsigned short instance,
2904 struct route_table *table)
2905 {
2906 struct route_node *rn;
2907 struct route_entry *re;
2908 struct route_entry *next;
2909 unsigned long n = 0;
2910
2911 if (table)
2912 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
2913 RNODE_FOREACH_RE_SAFE (rn, re, next) {
2914 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
2915 continue;
2916 if (re->type == proto
2917 && re->instance == instance) {
2918 rib_delnode(rn, re);
2919 n++;
2920 }
2921 }
2922 return n;
2923 }
2924
2925 /* Remove specific by protocol routes. */
2926 unsigned long rib_score_proto(uint8_t proto, unsigned short instance)
2927 {
2928 struct vrf *vrf;
2929 struct zebra_vrf *zvrf;
2930 unsigned long cnt = 0;
2931
2932 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
2933 if ((zvrf = vrf->info) != NULL)
2934 cnt += rib_score_proto_table(
2935 proto, instance,
2936 zvrf->table[AFI_IP][SAFI_UNICAST])
2937 + rib_score_proto_table(
2938 proto, instance,
2939 zvrf->table[AFI_IP6][SAFI_UNICAST]);
2940
2941 cnt += zebra_ns_score_proto(proto, instance);
2942
2943 return cnt;
2944 }
2945
2946 /* Close RIB and clean up kernel routes. */
2947 void rib_close_table(struct route_table *table)
2948 {
2949 struct route_node *rn;
2950 rib_table_info_t *info;
2951 rib_dest_t *dest;
2952
2953 if (!table)
2954 return;
2955
2956 info = table->info;
2957
2958 for (rn = route_top(table); rn; rn = srcdest_route_next(rn)) {
2959 dest = rib_dest_from_rnode(rn);
2960
2961 if (dest && dest->selected_fib) {
2962 if (info->safi == SAFI_UNICAST)
2963 hook_call(rib_update, rn, NULL);
2964
2965 if (!RIB_SYSTEM_ROUTE(dest->selected_fib))
2966 rib_uninstall_kernel(rn, dest->selected_fib);
2967 }
2968 }
2969 }
2970
2971 /* Routing information base initialize. */
2972 void rib_init(void)
2973 {
2974 rib_queue_init(&zebrad);
2975 }
2976
2977 /*
2978 * vrf_id_get_next
2979 *
2980 * Get the first vrf id that is greater than the given vrf id if any.
2981 *
2982 * Returns TRUE if a vrf id was found, FALSE otherwise.
2983 */
2984 static inline int vrf_id_get_next(vrf_id_t vrf_id, vrf_id_t *next_id_p)
2985 {
2986 struct vrf *vrf;
2987
2988 vrf = vrf_lookup_by_id(vrf_id);
2989 if (vrf) {
2990 vrf = RB_NEXT(vrf_id_head, vrf);
2991 if (vrf) {
2992 *next_id_p = vrf->vrf_id;
2993 return 1;
2994 }
2995 }
2996
2997 return 0;
2998 }
2999
3000 /*
3001 * rib_tables_iter_next
3002 *
3003 * Returns the next table in the iteration.
3004 */
3005 struct route_table *rib_tables_iter_next(rib_tables_iter_t *iter)
3006 {
3007 struct route_table *table;
3008
3009 /*
3010 * Array that helps us go over all AFI/SAFI combinations via one
3011 * index.
3012 */
3013 static struct {
3014 afi_t afi;
3015 safi_t safi;
3016 } afi_safis[] = {
3017 {AFI_IP, SAFI_UNICAST}, {AFI_IP, SAFI_MULTICAST},
3018 {AFI_IP, SAFI_LABELED_UNICAST}, {AFI_IP6, SAFI_UNICAST},
3019 {AFI_IP6, SAFI_MULTICAST}, {AFI_IP6, SAFI_LABELED_UNICAST},
3020 };
3021
3022 table = NULL;
3023
3024 switch (iter->state) {
3025
3026 case RIB_TABLES_ITER_S_INIT:
3027 iter->vrf_id = VRF_DEFAULT;
3028 iter->afi_safi_ix = -1;
3029
3030 /* Fall through */
3031
3032 case RIB_TABLES_ITER_S_ITERATING:
3033 iter->afi_safi_ix++;
3034 while (1) {
3035
3036 while (iter->afi_safi_ix
3037 < (int)ZEBRA_NUM_OF(afi_safis)) {
3038 table = zebra_vrf_table(
3039 afi_safis[iter->afi_safi_ix].afi,
3040 afi_safis[iter->afi_safi_ix].safi,
3041 iter->vrf_id);
3042 if (table)
3043 break;
3044
3045 iter->afi_safi_ix++;
3046 }
3047
3048 /*
3049 * Found another table in this vrf.
3050 */
3051 if (table)
3052 break;
3053
3054 /*
3055 * Done with all tables in the current vrf, go to the
3056 * next
3057 * one.
3058 */
3059 if (!vrf_id_get_next(iter->vrf_id, &iter->vrf_id))
3060 break;
3061
3062 iter->afi_safi_ix = 0;
3063 }
3064
3065 break;
3066
3067 case RIB_TABLES_ITER_S_DONE:
3068 return NULL;
3069 }
3070
3071 if (table)
3072 iter->state = RIB_TABLES_ITER_S_ITERATING;
3073 else
3074 iter->state = RIB_TABLES_ITER_S_DONE;
3075
3076 return table;
3077 }