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