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