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