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