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