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