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