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