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