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