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