]>
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 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with GNU Zebra; see the file COPYING. If not, write to the Free | |
18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | * 02111-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "prefix.h" | |
25 | #include "table.h" | |
26 | #include "memory.h" | |
27 | #include "str.h" | |
28 | #include "command.h" | |
29 | #include "if.h" | |
30 | #include "log.h" | |
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" |
718e3744 | 39 | |
40 | #include "zebra/rib.h" | |
41 | #include "zebra/rt.h" | |
7c551956 | 42 | #include "zebra/zebra_ns.h" |
718e3744 | 43 | #include "zebra/zserv.h" |
7c551956 | 44 | #include "zebra/zebra_vrf.h" |
718e3744 | 45 | #include "zebra/redistribute.h" |
46 | #include "zebra/debug.h" | |
5adc2528 | 47 | #include "zebra/zebra_fpm.h" |
fb018d25 | 48 | #include "zebra/zebra_rnh.h" |
88177fe3 | 49 | #include "zebra/interface.h" |
d44ca835 | 50 | #include "zebra/connected.h" |
718e3744 | 51 | |
6baf7bb8 DS |
52 | /* Should we allow non Quagga processes to delete our routes */ |
53 | extern int allow_delete; | |
54 | ||
457eb9af PJ |
55 | /* Hold time for RIB process, should be very minimal. |
56 | * it is useful to able to set it otherwise for testing, hence exported | |
57 | * as global here for test-rig code. | |
58 | */ | |
59 | int rib_process_hold_time = 10; | |
60 | ||
718e3744 | 61 | /* Each route type's string and default distance value. */ |
d145bc00 | 62 | static const struct |
718e3744 | 63 | { |
64 | int key; | |
65 | int distance; | |
5734509c | 66 | } route_info[ZEBRA_ROUTE_MAX] = |
718e3744 | 67 | { |
5734509c PJ |
68 | [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0}, |
69 | [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0}, | |
70 | [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0}, | |
71 | [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1}, | |
72 | [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120}, | |
73 | [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120}, | |
74 | [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110}, | |
75 | [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110}, | |
76 | [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115}, | |
77 | [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */}, | |
7052f228 | 78 | /* no entry/default: 150 */ |
718e3744 | 79 | }; |
6b0655a2 | 80 | |
7a4bb9c5 DS |
81 | int |
82 | is_zebra_valid_kernel_table(u_int32_t table_id) | |
83 | { | |
84 | if ((table_id > ZEBRA_KERNEL_TABLE_MAX) || | |
85 | (table_id == RT_TABLE_UNSPEC) || | |
86 | (table_id == RT_TABLE_LOCAL) || | |
87 | (table_id == RT_TABLE_COMPAT)) | |
88 | return 0; | |
89 | else | |
90 | return 1; | |
91 | } | |
92 | ||
93 | int | |
94 | is_zebra_main_routing_table(u_int32_t table_id) | |
95 | { | |
96 | if ((table_id == RT_TABLE_MAIN) || (table_id == zebrad.rtm_table_default)) | |
97 | return 1; | |
98 | return 0; | |
99 | } | |
100 | ||
0aabccc0 DD |
101 | int |
102 | zebra_check_addr (struct prefix *p) | |
103 | { | |
104 | if (p->family == AF_INET) | |
105 | { | |
106 | u_int32_t addr; | |
107 | ||
108 | addr = p->u.prefix4.s_addr; | |
109 | addr = ntohl (addr); | |
110 | ||
111 | if (IPV4_NET127 (addr) | |
112 | || IN_CLASSD (addr) | |
113 | || IPV4_LINKLOCAL(addr)) | |
114 | return 0; | |
115 | } | |
0aabccc0 DD |
116 | if (p->family == AF_INET6) |
117 | { | |
118 | if (IN6_IS_ADDR_LOOPBACK (&p->u.prefix6)) | |
119 | return 0; | |
120 | if (IN6_IS_ADDR_LINKLOCAL(&p->u.prefix6)) | |
121 | return 0; | |
122 | } | |
0aabccc0 DD |
123 | return 1; |
124 | } | |
125 | ||
fa713d9e | 126 | /* Add nexthop to the end of a rib node's nexthop list */ |
fb018d25 | 127 | void |
a399694f | 128 | rib_nexthop_add (struct rib *rib, struct nexthop *nexthop) |
fa713d9e | 129 | { |
a399694f | 130 | nexthop_add(&rib->nexthop, nexthop); |
718e3744 | 131 | rib->nexthop_num++; |
132 | } | |
133 | ||
6e26278c | 134 | |
6e26278c DS |
135 | |
136 | /** | |
137 | * copy_nexthop - copy a nexthop to the rib structure. | |
138 | */ | |
139 | void | |
a399694f | 140 | rib_copy_nexthops (struct rib *rib, struct nexthop *nh) |
6e26278c DS |
141 | { |
142 | struct nexthop *nexthop; | |
143 | ||
144 | nexthop = nexthop_new(); | |
145 | nexthop->flags = nh->flags; | |
146 | nexthop->type = nh->type; | |
147 | nexthop->ifindex = nh->ifindex; | |
6e26278c DS |
148 | memcpy(&(nexthop->gate), &(nh->gate), sizeof(union g_addr)); |
149 | memcpy(&(nexthop->src), &(nh->src), sizeof(union g_addr)); | |
a399694f | 150 | rib_nexthop_add(rib, nexthop); |
6e26278c | 151 | if (CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)) |
a399694f | 152 | copy_nexthops(&nexthop->resolved, nh->resolved); |
6e26278c DS |
153 | } |
154 | ||
718e3744 | 155 | /* Delete specified nexthop from the list. */ |
a1ac18c4 | 156 | static void |
a399694f | 157 | rib_nexthop_delete (struct rib *rib, struct nexthop *nexthop) |
718e3744 | 158 | { |
159 | if (nexthop->next) | |
160 | nexthop->next->prev = nexthop->prev; | |
161 | if (nexthop->prev) | |
162 | nexthop->prev->next = nexthop->next; | |
163 | else | |
164 | rib->nexthop = nexthop->next; | |
165 | rib->nexthop_num--; | |
166 | } | |
167 | ||
fa713d9e | 168 | |
fa713d9e | 169 | |
718e3744 | 170 | struct nexthop * |
a399694f | 171 | rib_nexthop_ifindex_add (struct rib *rib, unsigned int ifindex) |
718e3744 | 172 | { |
173 | struct nexthop *nexthop; | |
174 | ||
a399694f | 175 | nexthop = nexthop_new(); |
718e3744 | 176 | nexthop->type = NEXTHOP_TYPE_IFINDEX; |
177 | nexthop->ifindex = ifindex; | |
178 | ||
a399694f | 179 | rib_nexthop_add (rib, nexthop); |
718e3744 | 180 | |
181 | return nexthop; | |
182 | } | |
183 | ||
718e3744 | 184 | struct nexthop * |
a399694f | 185 | rib_nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4, struct in_addr *src) |
718e3744 | 186 | { |
187 | struct nexthop *nexthop; | |
188 | ||
a399694f | 189 | nexthop = nexthop_new(); |
718e3744 | 190 | nexthop->type = NEXTHOP_TYPE_IPV4; |
191 | nexthop->gate.ipv4 = *ipv4; | |
7514fb77 PJ |
192 | if (src) |
193 | nexthop->src.ipv4 = *src; | |
718e3744 | 194 | |
a399694f | 195 | rib_nexthop_add (rib, nexthop); |
718e3744 | 196 | |
197 | return nexthop; | |
198 | } | |
199 | ||
26e2ae36 | 200 | struct nexthop * |
a399694f DS |
201 | rib_nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4, |
202 | struct in_addr *src, unsigned int ifindex) | |
718e3744 | 203 | { |
204 | struct nexthop *nexthop; | |
d44ca835 | 205 | struct interface *ifp; |
718e3744 | 206 | |
a399694f | 207 | nexthop = nexthop_new(); |
718e3744 | 208 | nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX; |
209 | nexthop->gate.ipv4 = *ipv4; | |
7514fb77 PJ |
210 | if (src) |
211 | nexthop->src.ipv4 = *src; | |
718e3744 | 212 | nexthop->ifindex = ifindex; |
d44ca835 | 213 | ifp = if_lookup_by_index (nexthop->ifindex); |
12f6fb97 DS |
214 | /*Pending: need to think if null ifp here is ok during bootup? |
215 | There was a crash because ifp here was coming to be NULL */ | |
216 | if (ifp) | |
d44ca835 DS |
217 | if (connected_is_unnumbered(ifp)) { |
218 | SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK); | |
219 | } | |
718e3744 | 220 | |
a399694f | 221 | rib_nexthop_add (rib, nexthop); |
718e3744 | 222 | |
223 | return nexthop; | |
224 | } | |
225 | ||
718e3744 | 226 | struct nexthop * |
a399694f | 227 | rib_nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6) |
718e3744 | 228 | { |
229 | struct nexthop *nexthop; | |
230 | ||
a399694f | 231 | nexthop = nexthop_new(); |
718e3744 | 232 | nexthop->type = NEXTHOP_TYPE_IPV6; |
233 | nexthop->gate.ipv6 = *ipv6; | |
234 | ||
a399694f | 235 | rib_nexthop_add (rib, nexthop); |
718e3744 | 236 | |
237 | return nexthop; | |
238 | } | |
239 | ||
41fc2714 | 240 | struct nexthop * |
a399694f DS |
241 | rib_nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6, |
242 | unsigned int ifindex) | |
718e3744 | 243 | { |
244 | struct nexthop *nexthop; | |
245 | ||
393deb9b | 246 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
718e3744 | 247 | nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX; |
248 | nexthop->gate.ipv6 = *ipv6; | |
249 | nexthop->ifindex = ifindex; | |
250 | ||
a399694f | 251 | rib_nexthop_add (rib, nexthop); |
718e3744 | 252 | |
253 | return nexthop; | |
254 | } | |
718e3744 | 255 | |
595db7f1 | 256 | struct nexthop * |
a399694f | 257 | rib_nexthop_blackhole_add (struct rib *rib) |
595db7f1 | 258 | { |
259 | struct nexthop *nexthop; | |
260 | ||
a399694f | 261 | nexthop = nexthop_new(); |
595db7f1 | 262 | nexthop->type = NEXTHOP_TYPE_BLACKHOLE; |
263 | SET_FLAG (rib->flags, ZEBRA_FLAG_BLACKHOLE); | |
264 | ||
a399694f | 265 | rib_nexthop_add (rib, nexthop); |
595db7f1 | 266 | |
267 | return nexthop; | |
268 | } | |
269 | ||
fa713d9e CF |
270 | /* This method checks whether a recursive nexthop has at |
271 | * least one resolved nexthop in the fib. | |
272 | */ | |
273 | int | |
274 | nexthop_has_fib_child(struct nexthop *nexthop) | |
275 | { | |
276 | struct nexthop *nh; | |
277 | ||
278 | if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) | |
279 | return 0; | |
280 | ||
281 | for (nh = nexthop->resolved; nh; nh = nh->next) | |
282 | if (CHECK_FLAG (nh->flags, NEXTHOP_FLAG_FIB)) | |
283 | return 1; | |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
718e3744 | 288 | /* If force flag is not set, do not modify falgs at all for uninstall |
289 | the route from FIB. */ | |
a1ac18c4 | 290 | static int |
718e3744 | 291 | nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set, |
292 | struct route_node *top) | |
293 | { | |
294 | struct prefix_ipv4 p; | |
295 | struct route_table *table; | |
296 | struct route_node *rn; | |
297 | struct rib *match; | |
fa713d9e | 298 | int resolved; |
6e26278c | 299 | struct nexthop *newhop, *tnewhop; |
fa713d9e | 300 | struct nexthop *resolved_hop; |
6e26278c | 301 | int recursing = 0; |
c8a1cb5c | 302 | struct interface *ifp; |
718e3744 | 303 | |
304 | if (nexthop->type == NEXTHOP_TYPE_IPV4) | |
305 | nexthop->ifindex = 0; | |
306 | ||
307 | if (set) | |
fa713d9e CF |
308 | { |
309 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); | |
d82ae0de | 310 | zebra_deregister_rnh_static_nexthops(rib->vrf_id, nexthop->resolved, top); |
a399694f | 311 | nexthops_free(nexthop->resolved); |
fa713d9e CF |
312 | nexthop->resolved = NULL; |
313 | } | |
718e3744 | 314 | |
6e26278c DS |
315 | /* Skip nexthops that have been filtered out due to route-map */ |
316 | /* The nexthops are specific to this route and so the same */ | |
317 | /* nexthop for a different route may not have this flag set */ | |
318 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED)) | |
319 | return 0; | |
320 | ||
d44ca835 DS |
321 | /* |
322 | * Check to see if we should trust the passed in information | |
323 | * for UNNUMBERED interfaces as that we won't find the GW | |
324 | * address in the routing table. | |
c8a1cb5c DS |
325 | */ |
326 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ONLINK)) | |
327 | { | |
328 | ifp = if_lookup_by_index (nexthop->ifindex); | |
d44ca835 DS |
329 | if (ifp && connected_is_unnumbered(ifp)) |
330 | { | |
331 | if (if_is_operative(ifp)) | |
332 | return 1; | |
333 | else | |
334 | return 0; | |
335 | } | |
c8a1cb5c DS |
336 | else |
337 | return 0; | |
338 | } | |
339 | ||
718e3744 | 340 | /* Make lookup prefix. */ |
341 | memset (&p, 0, sizeof (struct prefix_ipv4)); | |
342 | p.family = AF_INET; | |
343 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
344 | p.prefix = nexthop->gate.ipv4; | |
345 | ||
346 | /* Lookup table. */ | |
78104b9b | 347 | table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, rib->vrf_id); |
718e3744 | 348 | if (! table) |
349 | return 0; | |
350 | ||
351 | rn = route_node_match (table, (struct prefix *) &p); | |
352 | while (rn) | |
353 | { | |
354 | route_unlock_node (rn); | |
355 | ||
a50c107e | 356 | /* If lookup self prefix return immediately. */ |
718e3744 | 357 | if (rn == top) |
358 | return 0; | |
359 | ||
360 | /* Pick up selected route. */ | |
18ff3edd DS |
361 | /* However, do not resolve over default route unless explicitly allowed. */ |
362 | if (is_default_prefix (&rn->p) && | |
363 | !nh_resolve_via_default (p.family)) | |
364 | return 0; | |
365 | ||
9fd92e3c | 366 | RNODE_FOREACH_RIB (rn, match) |
16814f96 SH |
367 | { |
368 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) | |
369 | continue; | |
370 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) | |
371 | break; | |
372 | } | |
718e3744 | 373 | |
374 | /* If there is no selected route or matched route is EGP, go up | |
375 | tree. */ | |
6e26278c | 376 | if (! match) |
718e3744 | 377 | { |
378 | do { | |
379 | rn = rn->parent; | |
380 | } while (rn && rn->info == NULL); | |
381 | if (rn) | |
382 | route_lock_node (rn); | |
383 | } | |
384 | else | |
385 | { | |
48a53dc7 CF |
386 | /* If the longest prefix match for the nexthop yields |
387 | * a blackhole, mark it as inactive. */ | |
388 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE) | |
389 | || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT)) | |
390 | return 0; | |
391 | ||
718e3744 | 392 | if (match->type == ZEBRA_ROUTE_CONNECT) |
393 | { | |
394 | /* Directly point connected route. */ | |
395 | newhop = match->nexthop; | |
396 | if (newhop && nexthop->type == NEXTHOP_TYPE_IPV4) | |
397 | nexthop->ifindex = newhop->ifindex; | |
398 | ||
399 | return 1; | |
400 | } | |
401 | else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL)) | |
402 | { | |
fa713d9e | 403 | resolved = 0; |
718e3744 | 404 | for (newhop = match->nexthop; newhop; newhop = newhop->next) |
405 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB) | |
406 | && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE)) | |
407 | { | |
408 | if (set) | |
409 | { | |
410 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); | |
6e26278c | 411 | SET_FLAG(rib->status, RIB_ENTRY_NEXTHOPS_CHANGED); |
fa713d9e CF |
412 | |
413 | resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop)); | |
414 | SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE); | |
c3e6b595 CF |
415 | /* If the resolving route specifies a gateway, use it */ |
416 | if (newhop->type == NEXTHOP_TYPE_IPV4 | |
3ee39b5b | 417 | || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
c3e6b595 CF |
418 | { |
419 | resolved_hop->type = newhop->type; | |
420 | resolved_hop->gate.ipv4 = newhop->gate.ipv4; | |
421 | ||
422 | if (newhop->ifindex) | |
423 | { | |
424 | resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
425 | resolved_hop->ifindex = newhop->ifindex; | |
f44f6668 DS |
426 | if (newhop->flags & NEXTHOP_FLAG_ONLINK) |
427 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; | |
c3e6b595 CF |
428 | } |
429 | } | |
430 | ||
431 | /* If the resolving route is an interface route, | |
432 | * it means the gateway we are looking up is connected | |
433 | * to that interface. (The actual network is _not_ onlink). | |
434 | * Therefore, the resolved route should have the original | |
435 | * gateway as nexthop as it is directly connected. | |
436 | * | |
437 | * On Linux, we have to set the onlink netlink flag because | |
438 | * otherwise, the kernel won't accept the route. */ | |
3ee39b5b | 439 | if (newhop->type == NEXTHOP_TYPE_IFINDEX) |
c3e6b595 CF |
440 | { |
441 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; | |
442 | resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
443 | resolved_hop->gate.ipv4 = nexthop->gate.ipv4; | |
444 | resolved_hop->ifindex = newhop->ifindex; | |
445 | } | |
fa713d9e | 446 | |
a399694f | 447 | nexthop_add(&nexthop->resolved, resolved_hop); |
6e26278c DS |
448 | } |
449 | resolved = 1; | |
450 | } | |
451 | return resolved; | |
452 | } | |
453 | else if (rib->type == ZEBRA_ROUTE_STATIC) | |
454 | { | |
455 | resolved = 0; | |
456 | for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing)) | |
457 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)) | |
458 | { | |
459 | if (set) | |
460 | { | |
461 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); | |
462 | ||
463 | resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop)); | |
464 | SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE); | |
465 | /* If the resolving route specifies a gateway, use it */ | |
466 | if (newhop->type == NEXTHOP_TYPE_IPV4 | |
3ee39b5b | 467 | || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
6e26278c DS |
468 | { |
469 | resolved_hop->type = newhop->type; | |
470 | resolved_hop->gate.ipv4 = newhop->gate.ipv4; | |
471 | ||
472 | if (newhop->ifindex) | |
473 | { | |
474 | resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
475 | resolved_hop->ifindex = newhop->ifindex; | |
f44f6668 DS |
476 | if (newhop->flags & NEXTHOP_FLAG_ONLINK) |
477 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; | |
6e26278c DS |
478 | } |
479 | } | |
480 | ||
481 | /* If the resolving route is an interface route, | |
482 | * it means the gateway we are looking up is connected | |
483 | * to that interface. (The actual network is _not_ onlink). | |
484 | * Therefore, the resolved route should have the original | |
485 | * gateway as nexthop as it is directly connected. | |
486 | * | |
487 | * On Linux, we have to set the onlink netlink flag because | |
488 | * otherwise, the kernel won't accept the route. | |
489 | */ | |
3ee39b5b | 490 | if (newhop->type == NEXTHOP_TYPE_IFINDEX) |
6e26278c DS |
491 | { |
492 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; | |
493 | resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX; | |
494 | resolved_hop->gate.ipv4 = nexthop->gate.ipv4; | |
495 | resolved_hop->ifindex = newhop->ifindex; | |
496 | } | |
497 | ||
a399694f | 498 | nexthop_add(&nexthop->resolved, resolved_hop); |
718e3744 | 499 | } |
fa713d9e | 500 | resolved = 1; |
718e3744 | 501 | } |
fa713d9e | 502 | return resolved; |
718e3744 | 503 | } |
504 | else | |
505 | { | |
506 | return 0; | |
507 | } | |
508 | } | |
509 | } | |
510 | return 0; | |
511 | } | |
512 | ||
718e3744 | 513 | /* If force flag is not set, do not modify falgs at all for uninstall |
514 | the route from FIB. */ | |
a1ac18c4 | 515 | static int |
718e3744 | 516 | nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set, |
517 | struct route_node *top) | |
518 | { | |
519 | struct prefix_ipv6 p; | |
520 | struct route_table *table; | |
521 | struct route_node *rn; | |
522 | struct rib *match; | |
fa713d9e | 523 | int resolved; |
6e26278c DS |
524 | struct nexthop *newhop, *tnewhop; |
525 | int recursing = 0; | |
fa713d9e | 526 | struct nexthop *resolved_hop; |
718e3744 | 527 | |
528 | if (nexthop->type == NEXTHOP_TYPE_IPV6) | |
529 | nexthop->ifindex = 0; | |
530 | ||
531 | if (set) | |
fa713d9e CF |
532 | { |
533 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); | |
d82ae0de | 534 | zebra_deregister_rnh_static_nexthops (rib->vrf_id, nexthop->resolved, top); |
a399694f | 535 | nexthops_free(nexthop->resolved); |
fa713d9e CF |
536 | nexthop->resolved = NULL; |
537 | } | |
718e3744 | 538 | |
6e26278c DS |
539 | /* Skip nexthops that have been filtered out due to route-map */ |
540 | /* The nexthops are specific to this route and so the same */ | |
541 | /* nexthop for a different route may not have this flag set */ | |
542 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FILTERED)) | |
543 | return 0; | |
544 | ||
718e3744 | 545 | /* Make lookup prefix. */ |
546 | memset (&p, 0, sizeof (struct prefix_ipv6)); | |
547 | p.family = AF_INET6; | |
548 | p.prefixlen = IPV6_MAX_PREFIXLEN; | |
549 | p.prefix = nexthop->gate.ipv6; | |
550 | ||
551 | /* Lookup table. */ | |
78104b9b | 552 | table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, rib->vrf_id); |
718e3744 | 553 | if (! table) |
554 | return 0; | |
555 | ||
556 | rn = route_node_match (table, (struct prefix *) &p); | |
557 | while (rn) | |
558 | { | |
559 | route_unlock_node (rn); | |
560 | ||
a50c107e | 561 | /* If lookup self prefix return immediately. */ |
718e3744 | 562 | if (rn == top) |
563 | return 0; | |
564 | ||
565 | /* Pick up selected route. */ | |
18ff3edd DS |
566 | /* However, do not resolve over default route unless explicitly allowed. */ |
567 | if (is_default_prefix (&rn->p) && | |
568 | !nh_resolve_via_default (p.family)) | |
569 | return 0; | |
570 | ||
9fd92e3c | 571 | RNODE_FOREACH_RIB (rn, match) |
16814f96 SH |
572 | { |
573 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) | |
574 | continue; | |
575 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) | |
576 | break; | |
577 | } | |
718e3744 | 578 | |
579 | /* If there is no selected route or matched route is EGP, go up | |
580 | tree. */ | |
6e26278c | 581 | if (! match) |
718e3744 | 582 | { |
583 | do { | |
584 | rn = rn->parent; | |
585 | } while (rn && rn->info == NULL); | |
586 | if (rn) | |
587 | route_lock_node (rn); | |
588 | } | |
589 | else | |
590 | { | |
48a53dc7 CF |
591 | /* If the longest prefix match for the nexthop yields |
592 | * a blackhole, mark it as inactive. */ | |
593 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE) | |
594 | || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT)) | |
595 | return 0; | |
596 | ||
718e3744 | 597 | if (match->type == ZEBRA_ROUTE_CONNECT) |
598 | { | |
599 | /* Directly point connected route. */ | |
600 | newhop = match->nexthop; | |
601 | ||
602 | if (newhop && nexthop->type == NEXTHOP_TYPE_IPV6) | |
603 | nexthop->ifindex = newhop->ifindex; | |
604 | ||
605 | return 1; | |
606 | } | |
607 | else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL)) | |
608 | { | |
fa713d9e | 609 | resolved = 0; |
718e3744 | 610 | for (newhop = match->nexthop; newhop; newhop = newhop->next) |
611 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB) | |
612 | && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE)) | |
6e26278c DS |
613 | { |
614 | if (set) | |
615 | { | |
616 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); | |
617 | SET_FLAG(rib->status, RIB_ENTRY_NEXTHOPS_CHANGED); | |
618 | ||
619 | resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop)); | |
620 | SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE); | |
621 | /* See nexthop_active_ipv4 for a description how the | |
622 | * resolved nexthop is constructed. */ | |
623 | if (newhop->type == NEXTHOP_TYPE_IPV6 | |
3ee39b5b | 624 | || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
6e26278c DS |
625 | { |
626 | resolved_hop->type = newhop->type; | |
627 | resolved_hop->gate.ipv6 = newhop->gate.ipv6; | |
628 | ||
629 | if (newhop->ifindex) | |
630 | { | |
631 | resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
632 | resolved_hop->ifindex = newhop->ifindex; | |
633 | } | |
634 | } | |
635 | ||
3ee39b5b | 636 | if (newhop->type == NEXTHOP_TYPE_IFINDEX) |
6e26278c DS |
637 | { |
638 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; | |
639 | resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
640 | resolved_hop->gate.ipv6 = nexthop->gate.ipv6; | |
641 | resolved_hop->ifindex = newhop->ifindex; | |
642 | } | |
643 | ||
a399694f | 644 | nexthop_add(&nexthop->resolved, resolved_hop); |
6e26278c DS |
645 | } |
646 | resolved = 1; | |
647 | } | |
648 | return resolved; | |
649 | } | |
650 | else if (rib->type == ZEBRA_ROUTE_STATIC) | |
651 | { | |
652 | resolved = 0; | |
653 | for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing)) | |
654 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)) | |
718e3744 | 655 | { |
656 | if (set) | |
657 | { | |
658 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); | |
fa713d9e CF |
659 | |
660 | resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop)); | |
661 | SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE); | |
c3e6b595 CF |
662 | /* See nexthop_active_ipv4 for a description how the |
663 | * resolved nexthop is constructed. */ | |
718e3744 | 664 | if (newhop->type == NEXTHOP_TYPE_IPV6 |
3ee39b5b | 665 | || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX) |
c3e6b595 CF |
666 | { |
667 | resolved_hop->type = newhop->type; | |
668 | resolved_hop->gate.ipv6 = newhop->gate.ipv6; | |
669 | ||
670 | if (newhop->ifindex) | |
671 | { | |
672 | resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
673 | resolved_hop->ifindex = newhop->ifindex; | |
674 | } | |
675 | } | |
fa713d9e | 676 | |
3ee39b5b | 677 | if (newhop->type == NEXTHOP_TYPE_IFINDEX) |
c3e6b595 CF |
678 | { |
679 | resolved_hop->flags |= NEXTHOP_FLAG_ONLINK; | |
680 | resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX; | |
681 | resolved_hop->gate.ipv6 = nexthop->gate.ipv6; | |
682 | resolved_hop->ifindex = newhop->ifindex; | |
683 | } | |
fa713d9e | 684 | |
a399694f | 685 | nexthop_add(&nexthop->resolved, resolved_hop); |
718e3744 | 686 | } |
fa713d9e | 687 | resolved = 1; |
718e3744 | 688 | } |
fa713d9e | 689 | return resolved; |
718e3744 | 690 | } |
691 | else | |
692 | { | |
693 | return 0; | |
694 | } | |
695 | } | |
696 | } | |
697 | return 0; | |
698 | } | |
718e3744 | 699 | |
700 | struct rib * | |
78104b9b | 701 | rib_match_ipv4 (struct in_addr addr, vrf_id_t vrf_id) |
718e3744 | 702 | { |
703 | struct prefix_ipv4 p; | |
704 | struct route_table *table; | |
705 | struct route_node *rn; | |
706 | struct rib *match; | |
fa713d9e CF |
707 | struct nexthop *newhop, *tnewhop; |
708 | int recursing; | |
718e3744 | 709 | |
710 | /* Lookup table. */ | |
78104b9b | 711 | table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id); |
718e3744 | 712 | if (! table) |
713 | return 0; | |
714 | ||
715 | memset (&p, 0, sizeof (struct prefix_ipv4)); | |
716 | p.family = AF_INET; | |
717 | p.prefixlen = IPV4_MAX_PREFIXLEN; | |
718 | p.prefix = addr; | |
719 | ||
720 | rn = route_node_match (table, (struct prefix *) &p); | |
721 | ||
722 | while (rn) | |
723 | { | |
724 | route_unlock_node (rn); | |
725 | ||
726 | /* Pick up selected route. */ | |
9fd92e3c | 727 | RNODE_FOREACH_RIB (rn, match) |
16814f96 SH |
728 | { |
729 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) | |
730 | continue; | |
731 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) | |
732 | break; | |
733 | } | |
718e3744 | 734 | |
735 | /* If there is no selected route or matched route is EGP, go up | |
736 | tree. */ | |
6e26278c | 737 | if (! match) |
718e3744 | 738 | { |
739 | do { | |
740 | rn = rn->parent; | |
741 | } while (rn && rn->info == NULL); | |
742 | if (rn) | |
743 | route_lock_node (rn); | |
744 | } | |
745 | else | |
746 | { | |
747 | if (match->type == ZEBRA_ROUTE_CONNECT) | |
748 | /* Directly point connected route. */ | |
749 | return match; | |
750 | else | |
751 | { | |
fa713d9e | 752 | for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing)) |
718e3744 | 753 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)) |
754 | return match; | |
755 | return NULL; | |
756 | } | |
757 | } | |
758 | } | |
759 | return NULL; | |
760 | } | |
761 | ||
762 | struct rib * | |
78104b9b | 763 | rib_lookup_ipv4 (struct prefix_ipv4 *p, vrf_id_t vrf_id) |
718e3744 | 764 | { |
765 | struct route_table *table; | |
766 | struct route_node *rn; | |
767 | struct rib *match; | |
fa713d9e CF |
768 | struct nexthop *nexthop, *tnexthop; |
769 | int recursing; | |
718e3744 | 770 | |
771 | /* Lookup table. */ | |
78104b9b | 772 | table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id); |
718e3744 | 773 | if (! table) |
774 | return 0; | |
775 | ||
776 | rn = route_node_lookup (table, (struct prefix *) p); | |
777 | ||
778 | /* No route for this prefix. */ | |
779 | if (! rn) | |
780 | return NULL; | |
781 | ||
782 | /* Unlock node. */ | |
783 | route_unlock_node (rn); | |
784 | ||
9fd92e3c | 785 | RNODE_FOREACH_RIB (rn, match) |
16814f96 SH |
786 | { |
787 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) | |
788 | continue; | |
789 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) | |
790 | break; | |
791 | } | |
718e3744 | 792 | |
6e26278c | 793 | if (! match) |
718e3744 | 794 | return NULL; |
795 | ||
796 | if (match->type == ZEBRA_ROUTE_CONNECT) | |
797 | return match; | |
798 | ||
fa713d9e | 799 | for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing)) |
718e3744 | 800 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
801 | return match; | |
802 | ||
803 | return NULL; | |
804 | } | |
805 | ||
dc95824a DO |
806 | /* |
807 | * This clone function, unlike its original rib_lookup_ipv4(), checks | |
808 | * if specified IPv4 route record (prefix/mask -> gate) exists in | |
809 | * the whole RIB and has ZEBRA_FLAG_SELECTED set. | |
810 | * | |
811 | * Return values: | |
812 | * -1: error | |
813 | * 0: exact match found | |
814 | * 1: a match was found with a different gate | |
815 | * 2: connected route found | |
816 | * 3: no matches found | |
817 | */ | |
818 | int | |
78104b9b FL |
819 | rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate, |
820 | vrf_id_t vrf_id) | |
dc95824a DO |
821 | { |
822 | struct route_table *table; | |
823 | struct route_node *rn; | |
824 | struct rib *match; | |
fa713d9e CF |
825 | struct nexthop *nexthop, *tnexthop; |
826 | int recursing; | |
827 | int nexthops_active; | |
dc95824a DO |
828 | |
829 | /* Lookup table. */ | |
78104b9b | 830 | table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id); |
dc95824a DO |
831 | if (! table) |
832 | return ZEBRA_RIB_LOOKUP_ERROR; | |
833 | ||
834 | /* Scan the RIB table for exactly matching RIB entry. */ | |
835 | rn = route_node_lookup (table, (struct prefix *) p); | |
836 | ||
837 | /* No route for this prefix. */ | |
838 | if (! rn) | |
839 | return ZEBRA_RIB_NOTFOUND; | |
840 | ||
841 | /* Unlock node. */ | |
842 | route_unlock_node (rn); | |
843 | ||
844 | /* Find out if a "selected" RR for the discovered RIB entry exists ever. */ | |
9fd92e3c | 845 | RNODE_FOREACH_RIB (rn, match) |
16814f96 SH |
846 | { |
847 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) | |
848 | continue; | |
849 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) | |
850 | break; | |
851 | } | |
dc95824a DO |
852 | |
853 | /* None such found :( */ | |
854 | if (!match) | |
855 | return ZEBRA_RIB_NOTFOUND; | |
856 | ||
857 | if (match->type == ZEBRA_ROUTE_CONNECT) | |
858 | return ZEBRA_RIB_FOUND_CONNECTED; | |
859 | ||
860 | /* Ok, we have a cood candidate, let's check it's nexthop list... */ | |
fa713d9e CF |
861 | nexthops_active = 0; |
862 | for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing)) | |
dc95824a | 863 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
dc95824a | 864 | { |
fa713d9e CF |
865 | nexthops_active = 1; |
866 | if (nexthop->gate.ipv4.s_addr == sockunion2ip (qgate)) | |
867 | return ZEBRA_RIB_FOUND_EXACT; | |
dc95824a | 868 | if (IS_ZEBRA_DEBUG_RIB) |
fa713d9e CF |
869 | { |
870 | char gate_buf[INET_ADDRSTRLEN], qgate_buf[INET_ADDRSTRLEN]; | |
871 | inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, gate_buf, INET_ADDRSTRLEN); | |
872 | inet_ntop (AF_INET, &sockunion2ip(qgate), qgate_buf, INET_ADDRSTRLEN); | |
873 | zlog_debug ("%s: qgate == %s, %s == %s", __func__, | |
874 | qgate_buf, recursing ? "rgate" : "gate", gate_buf); | |
875 | } | |
dc95824a | 876 | } |
fa713d9e CF |
877 | |
878 | if (nexthops_active) | |
879 | return ZEBRA_RIB_FOUND_NOGATE; | |
dc95824a DO |
880 | |
881 | return ZEBRA_RIB_NOTFOUND; | |
882 | } | |
883 | ||
718e3744 | 884 | struct rib * |
78104b9b | 885 | rib_match_ipv6 (struct in6_addr *addr, vrf_id_t vrf_id) |
718e3744 | 886 | { |
887 | struct prefix_ipv6 p; | |
888 | struct route_table *table; | |
889 | struct route_node *rn; | |
890 | struct rib *match; | |
fa713d9e CF |
891 | struct nexthop *newhop, *tnewhop; |
892 | int recursing; | |
718e3744 | 893 | |
894 | /* Lookup table. */ | |
78104b9b | 895 | table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
718e3744 | 896 | if (! table) |
897 | return 0; | |
898 | ||
899 | memset (&p, 0, sizeof (struct prefix_ipv6)); | |
900 | p.family = AF_INET6; | |
901 | p.prefixlen = IPV6_MAX_PREFIXLEN; | |
902 | IPV6_ADDR_COPY (&p.prefix, addr); | |
903 | ||
904 | rn = route_node_match (table, (struct prefix *) &p); | |
905 | ||
906 | while (rn) | |
907 | { | |
908 | route_unlock_node (rn); | |
909 | ||
910 | /* Pick up selected route. */ | |
9fd92e3c | 911 | RNODE_FOREACH_RIB (rn, match) |
16814f96 SH |
912 | { |
913 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) | |
914 | continue; | |
915 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) | |
916 | break; | |
917 | } | |
718e3744 | 918 | |
919 | /* If there is no selected route or matched route is EGP, go up | |
920 | tree. */ | |
6e26278c | 921 | if (! match) |
718e3744 | 922 | { |
923 | do { | |
924 | rn = rn->parent; | |
925 | } while (rn && rn->info == NULL); | |
926 | if (rn) | |
927 | route_lock_node (rn); | |
928 | } | |
929 | else | |
930 | { | |
931 | if (match->type == ZEBRA_ROUTE_CONNECT) | |
932 | /* Directly point connected route. */ | |
933 | return match; | |
934 | else | |
935 | { | |
fa713d9e | 936 | for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing)) |
718e3744 | 937 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)) |
938 | return match; | |
939 | return NULL; | |
940 | } | |
941 | } | |
942 | } | |
943 | return NULL; | |
944 | } | |
718e3744 | 945 | |
7514fb77 PJ |
946 | #define RIB_SYSTEM_ROUTE(R) \ |
947 | ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT) | |
948 | ||
dc95824a DO |
949 | /* This function verifies reachability of one given nexthop, which can be |
950 | * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored | |
951 | * in nexthop->flags field. If the 4th parameter, 'set', is non-zero, | |
952 | * nexthop->ifindex will be updated appropriately as well. | |
953 | * An existing route map can turn (otherwise active) nexthop into inactive, but | |
954 | * not vice versa. | |
955 | * | |
956 | * The return value is the final value of 'ACTIVE' flag. | |
957 | */ | |
958 | ||
d02c56cd | 959 | static unsigned |
718e3744 | 960 | nexthop_active_check (struct route_node *rn, struct rib *rib, |
961 | struct nexthop *nexthop, int set) | |
962 | { | |
f3a1732e | 963 | rib_table_info_t *info = rn->table->info; |
718e3744 | 964 | struct interface *ifp; |
7514fb77 | 965 | route_map_result_t ret = RMAP_MATCH; |
7514fb77 | 966 | int family; |
518f0eb1 | 967 | char buf[INET6_ADDRSTRLEN+1]; |
718e3744 | 968 | |
7a4bb9c5 DS |
969 | if (rn->p.family == AF_INET) |
970 | family = AFI_IP; | |
971 | else if (rn->p.family == AF_INET6) | |
972 | family = AFI_IP6; | |
973 | else | |
974 | family = 0; | |
718e3744 | 975 | switch (nexthop->type) |
976 | { | |
977 | case NEXTHOP_TYPE_IFINDEX: | |
78104b9b | 978 | ifp = if_lookup_by_index_vrf (nexthop->ifindex, rib->vrf_id); |
3f087670 | 979 | if (ifp && if_is_operative(ifp)) |
718e3744 | 980 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
981 | else | |
982 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
983 | break; | |
718e3744 | 984 | case NEXTHOP_TYPE_IPV4: |
985 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
7514fb77 | 986 | family = AFI_IP; |
718e3744 | 987 | if (nexthop_active_ipv4 (rib, nexthop, set, rn)) |
988 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
989 | else | |
990 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
991 | break; | |
718e3744 | 992 | case NEXTHOP_TYPE_IPV6: |
7514fb77 | 993 | family = AFI_IP6; |
718e3744 | 994 | if (nexthop_active_ipv6 (rib, nexthop, set, rn)) |
995 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
996 | else | |
997 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
998 | break; | |
999 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
fb5d585c | 1000 | /* RFC 5549, v4 prefix with v6 NH */ |
1001 | if (rn->p.family != AF_INET) | |
1002 | family = AFI_IP6; | |
718e3744 | 1003 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop->gate.ipv6)) |
1004 | { | |
78104b9b | 1005 | ifp = if_lookup_by_index_vrf (nexthop->ifindex, rib->vrf_id); |
3f087670 | 1006 | if (ifp && if_is_operative(ifp)) |
718e3744 | 1007 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
1008 | else | |
1009 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
1010 | } | |
1011 | else | |
1012 | { | |
1013 | if (nexthop_active_ipv6 (rib, nexthop, set, rn)) | |
1014 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
1015 | else | |
1016 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
1017 | } | |
1018 | break; | |
595db7f1 | 1019 | case NEXTHOP_TYPE_BLACKHOLE: |
1020 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
1021 | break; | |
718e3744 | 1022 | default: |
1023 | break; | |
1024 | } | |
7514fb77 PJ |
1025 | if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
1026 | return 0; | |
1027 | ||
f3a1732e CF |
1028 | /* XXX: What exactly do those checks do? Do we support |
1029 | * e.g. IPv4 routes with IPv6 nexthops or vice versa? */ | |
7514fb77 PJ |
1030 | if (RIB_SYSTEM_ROUTE(rib) || |
1031 | (family == AFI_IP && rn->p.family != AF_INET) || | |
1032 | (family == AFI_IP6 && rn->p.family != AF_INET6)) | |
1033 | return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
1034 | ||
f3a1732e CF |
1035 | /* The original code didn't determine the family correctly |
1036 | * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi | |
1037 | * from the rib_table_info in those cases. | |
1038 | * Possibly it may be better to use only the rib_table_info | |
1039 | * in every case. | |
1040 | */ | |
1041 | if (!family) | |
1042 | family = info->afi; | |
1043 | ||
c52ef59f | 1044 | memset(&nexthop->rmap_src.ipv6, 0, sizeof(union g_addr)); |
c52ef59f | 1045 | |
ca84c8ef | 1046 | /* It'll get set if required inside */ |
0032dd59 FL |
1047 | ret = zebra_route_map_check(family, rib->type, &rn->p, nexthop, rib->vrf_id, |
1048 | rib->tag); | |
7514fb77 | 1049 | if (ret == RMAP_DENYMATCH) |
518f0eb1 DS |
1050 | { |
1051 | if (IS_ZEBRA_DEBUG_RIB) | |
1052 | { | |
1053 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, sizeof (buf)); | |
41ec9222 | 1054 | zlog_debug("%u:%s/%d: Filtering out with NH out %s due to route map", |
3ee39b5b DS |
1055 | rib->vrf_id, buf, rn->p.prefixlen, |
1056 | ifindex2ifname_vrf (nexthop->ifindex, rib->vrf_id)); | |
518f0eb1 DS |
1057 | } |
1058 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); | |
1059 | } | |
718e3744 | 1060 | return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
1061 | } | |
1062 | ||
03e232a4 DO |
1063 | /* Iterate over all nexthops of the given RIB entry and refresh their |
1064 | * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any | |
1065 | * nexthop is found to toggle the ACTIVE flag, the whole rib structure | |
1066 | * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is | |
1067 | * transparently passed to nexthop_active_check(). | |
1068 | * | |
1069 | * Return value is the new number of active nexthops. | |
1070 | */ | |
1071 | ||
a1ac18c4 | 1072 | static int |
718e3744 | 1073 | nexthop_active_update (struct route_node *rn, struct rib *rib, int set) |
1074 | { | |
1075 | struct nexthop *nexthop; | |
c52ef59f | 1076 | union g_addr prev_src; |
6e26278c DS |
1077 | unsigned int prev_active, prev_index, new_active, old_num_nh; |
1078 | ||
1079 | old_num_nh = rib->nexthop_active_num; | |
718e3744 | 1080 | |
1081 | rib->nexthop_active_num = 0; | |
1082 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); | |
1083 | ||
1084 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) | |
03e232a4 | 1085 | { |
c52ef59f DS |
1086 | /* No protocol daemon provides src and so we're skipping tracking it */ |
1087 | prev_src = nexthop->rmap_src; | |
03e232a4 | 1088 | prev_active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
c3a56063 | 1089 | prev_index = nexthop->ifindex; |
03e232a4 DO |
1090 | if ((new_active = nexthop_active_check (rn, rib, nexthop, set))) |
1091 | rib->nexthop_active_num++; | |
c52ef59f | 1092 | /* Don't allow src setting on IPv6 addr for now */ |
c3a56063 | 1093 | if (prev_active != new_active || |
c52ef59f DS |
1094 | prev_index != nexthop->ifindex || |
1095 | ((nexthop->type >= NEXTHOP_TYPE_IFINDEX && | |
1096 | nexthop->type < NEXTHOP_TYPE_IPV6) && | |
0aabccc0 DD |
1097 | prev_src.ipv4.s_addr != nexthop->rmap_src.ipv4.s_addr) || |
1098 | ((nexthop->type >= NEXTHOP_TYPE_IPV6 && | |
1099 | nexthop->type < NEXTHOP_TYPE_BLACKHOLE) && | |
1100 | !(IPV6_ADDR_SAME (&prev_src.ipv6, &nexthop->rmap_src.ipv6)))) | |
6e26278c DS |
1101 | { |
1102 | SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); | |
1103 | SET_FLAG (rib->status, RIB_ENTRY_NEXTHOPS_CHANGED); | |
1104 | } | |
03e232a4 | 1105 | } |
6e26278c DS |
1106 | |
1107 | if (old_num_nh != rib->nexthop_active_num) | |
1108 | SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); | |
1109 | ||
1110 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_CHANGED)) | |
1111 | { | |
1112 | SET_FLAG (rib->status, RIB_ENTRY_NEXTHOPS_CHANGED); | |
1113 | } | |
1114 | ||
718e3744 | 1115 | return rib->nexthop_active_num; |
1116 | } | |
6baeb988 | 1117 | |
6b0655a2 | 1118 | |
718e3744 | 1119 | |
6ae24471 DS |
1120 | /* Update flag indicates whether this is a "replace" or not. Currently, this |
1121 | * is only used for IPv4. | |
1122 | */ | |
a1ac18c4 | 1123 | static void |
6ae24471 | 1124 | rib_install_kernel (struct route_node *rn, struct rib *rib, int update) |
718e3744 | 1125 | { |
1126 | int ret = 0; | |
fa713d9e CF |
1127 | struct nexthop *nexthop, *tnexthop; |
1128 | int recursing; | |
718e3744 | 1129 | |
5adc2528 AS |
1130 | /* |
1131 | * Make sure we update the FPM any time we send new information to | |
1132 | * the kernel. | |
1133 | */ | |
1134 | zfpm_trigger_update (rn, "installing in kernel"); | |
718e3744 | 1135 | switch (PREFIX_FAMILY (&rn->p)) |
1136 | { | |
1137 | case AF_INET: | |
6ae24471 DS |
1138 | if (update) |
1139 | ret = kernel_update_ipv4 (&rn->p, rib); | |
1140 | else | |
1141 | ret = kernel_add_ipv4 (&rn->p, rib); | |
718e3744 | 1142 | break; |
718e3744 | 1143 | case AF_INET6: |
dccc5225 | 1144 | if (update) |
1145 | ret = kernel_update_ipv6 (&rn->p, rib); | |
1146 | else | |
1147 | ret = kernel_add_ipv6 (&rn->p, rib); | |
718e3744 | 1148 | break; |
718e3744 | 1149 | } |
1150 | ||
dc95824a | 1151 | /* This condition is never met, if we are using rt_socket.c */ |
718e3744 | 1152 | if (ret < 0) |
1153 | { | |
fa713d9e | 1154 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
718e3744 | 1155 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
1156 | } | |
1157 | } | |
1158 | ||
1159 | /* Uninstall the route from kernel. */ | |
a1ac18c4 | 1160 | static int |
718e3744 | 1161 | rib_uninstall_kernel (struct route_node *rn, struct rib *rib) |
1162 | { | |
1163 | int ret = 0; | |
fa713d9e CF |
1164 | struct nexthop *nexthop, *tnexthop; |
1165 | int recursing; | |
718e3744 | 1166 | |
5adc2528 AS |
1167 | /* |
1168 | * Make sure we update the FPM any time we send new information to | |
1169 | * the kernel. | |
1170 | */ | |
1171 | zfpm_trigger_update (rn, "uninstalling from kernel"); | |
1172 | ||
718e3744 | 1173 | switch (PREFIX_FAMILY (&rn->p)) |
1174 | { | |
1175 | case AF_INET: | |
1176 | ret = kernel_delete_ipv4 (&rn->p, rib); | |
1177 | break; | |
718e3744 | 1178 | case AF_INET6: |
1179 | ret = kernel_delete_ipv6 (&rn->p, rib); | |
1180 | break; | |
718e3744 | 1181 | } |
1182 | ||
fa713d9e | 1183 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
718e3744 | 1184 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
1185 | ||
1186 | return ret; | |
1187 | } | |
1188 | ||
1189 | /* Uninstall the route from kernel. */ | |
a1ac18c4 | 1190 | static void |
718e3744 | 1191 | rib_uninstall (struct route_node *rn, struct rib *rib) |
1192 | { | |
1193 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) | |
1194 | { | |
5adc2528 AS |
1195 | zfpm_trigger_update (rn, "rib_uninstall"); |
1196 | ||
718e3744 | 1197 | redistribute_delete (&rn->p, rib); |
1198 | if (! RIB_SYSTEM_ROUTE (rib)) | |
1199 | rib_uninstall_kernel (rn, rib); | |
1200 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_SELECTED); | |
1201 | } | |
1202 | } | |
1203 | ||
6d691129 PJ |
1204 | static void rib_unlink (struct route_node *, struct rib *); |
1205 | ||
9fd92e3c AS |
1206 | /* |
1207 | * rib_can_delete_dest | |
1208 | * | |
1209 | * Returns TRUE if the given dest can be deleted from the table. | |
1210 | */ | |
1211 | static int | |
1212 | rib_can_delete_dest (rib_dest_t *dest) | |
1213 | { | |
1214 | if (dest->routes) | |
1215 | { | |
1216 | return 0; | |
1217 | } | |
1218 | ||
5adc2528 AS |
1219 | /* |
1220 | * Don't delete the dest if we have to update the FPM about this | |
1221 | * prefix. | |
1222 | */ | |
1223 | if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM) || | |
1224 | CHECK_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM)) | |
1225 | return 0; | |
1226 | ||
9fd92e3c AS |
1227 | return 1; |
1228 | } | |
1229 | ||
1230 | /* | |
1231 | * rib_gc_dest | |
1232 | * | |
1233 | * Garbage collect the rib dest corresponding to the given route node | |
1234 | * if appropriate. | |
1235 | * | |
1236 | * Returns TRUE if the dest was deleted, FALSE otherwise. | |
1237 | */ | |
1238 | int | |
1239 | rib_gc_dest (struct route_node *rn) | |
1240 | { | |
1241 | rib_dest_t *dest; | |
1242 | char buf[INET6_ADDRSTRLEN]; | |
41ec9222 | 1243 | struct zebra_vrf *zvrf; |
9fd92e3c AS |
1244 | |
1245 | dest = rib_dest_from_rnode (rn); | |
1246 | if (!dest) | |
1247 | return 0; | |
1248 | ||
1249 | if (!rib_can_delete_dest (dest)) | |
1250 | return 0; | |
1251 | ||
41ec9222 | 1252 | zvrf = rib_dest_vrf (dest); |
1253 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
9fd92e3c AS |
1254 | { |
1255 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, sizeof (buf)); | |
41ec9222 | 1256 | zlog_debug ("%u:%s/%d: rn %p removing dest", |
1257 | zvrf->vrf_id, buf, rn->p.prefixlen, rn); | |
9fd92e3c AS |
1258 | } |
1259 | ||
1260 | dest->rnode = NULL; | |
1261 | XFREE (MTYPE_RIB_DEST, dest); | |
1262 | rn->info = NULL; | |
1263 | ||
1264 | /* | |
1265 | * Release the one reference that we keep on the route node. | |
1266 | */ | |
1267 | route_unlock_node (rn); | |
1268 | return 1; | |
1269 | } | |
1270 | ||
718e3744 | 1271 | /* Core function for processing routing information base. */ |
e96f9203 DO |
1272 | static void |
1273 | rib_process (struct route_node *rn) | |
718e3744 | 1274 | { |
1275 | struct rib *rib; | |
1276 | struct rib *next; | |
1277 | struct rib *fib = NULL; | |
1278 | struct rib *select = NULL; | |
6d691129 | 1279 | struct rib *del = NULL; |
d753e9ee | 1280 | int installed = 0; |
fa713d9e CF |
1281 | struct nexthop *nexthop = NULL, *tnexthop; |
1282 | int recursing; | |
f304cb48 | 1283 | char buf[INET6_ADDRSTRLEN]; |
41ec9222 | 1284 | rib_dest_t *dest; |
1285 | struct zebra_vrf *zvrf = NULL; | |
58255d34 | 1286 | vrf_id_t vrf_id = VRF_UNKNOWN; |
41ec9222 | 1287 | |
4d38fdb4 | 1288 | assert (rn); |
1289 | ||
41ec9222 | 1290 | dest = rib_dest_from_rnode (rn); |
1291 | if (dest) | |
1292 | { | |
1293 | zvrf = rib_dest_vrf (dest); | |
1294 | vrf_id = zvrf->vrf_id; | |
1295 | } | |
1296 | ||
1297 | if (IS_ZEBRA_DEBUG_RIB) | |
f304cb48 | 1298 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); |
93bdadae | 1299 | |
41ec9222 | 1300 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
1301 | zlog_debug ("%u:%s/%d: Processing rn %p", vrf_id, buf, rn->p.prefixlen, rn); | |
1302 | ||
9fd92e3c | 1303 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
718e3744 | 1304 | { |
41ec9222 | 1305 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
1306 | zlog_debug ("%u:%s/%d: Examine rib %p (type %d) status %x flags %x " | |
1307 | "dist %d metric %d", | |
1308 | vrf_id, buf, rn->p.prefixlen, rib, rib->type, rib->status, | |
1309 | rib->flags, rib->distance, rib->metric); | |
1310 | ||
6e26278c DS |
1311 | UNSET_FLAG(rib->status, RIB_ENTRY_NEXTHOPS_CHANGED); |
1312 | ||
718e3744 | 1313 | /* Currently installed rib. */ |
1314 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) | |
6d691129 PJ |
1315 | { |
1316 | assert (fib == NULL); | |
1317 | fib = rib; | |
1318 | } | |
ca657c65 | 1319 | |
6d691129 PJ |
1320 | /* Unlock removed routes, so they'll be freed, bar the FIB entry, |
1321 | * which we need to do do further work with below. | |
1322 | */ | |
1323 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) | |
1324 | { | |
1325 | if (rib != fib) | |
1326 | { | |
1327 | if (IS_ZEBRA_DEBUG_RIB) | |
41ec9222 | 1328 | zlog_debug ("%u:%s/%d: Freeing route rn %p, rib %p (type %d)", |
1329 | vrf_id, buf, rn->p.prefixlen, rn, rib, rib->type); | |
9fd92e3c | 1330 | rib_unlink (rn, rib); |
6d691129 PJ |
1331 | } |
1332 | else | |
1333 | del = rib; | |
1334 | ||
1335 | continue; | |
1336 | } | |
4d38fdb4 | 1337 | |
718e3744 | 1338 | /* Skip unreachable nexthop. */ |
ca657c65 DS |
1339 | /* This first call to nexthop_active_update is merely to determine if |
1340 | * there's any change to nexthops associated with this RIB entry. Now, | |
1341 | * rib_process() can be invoked due to an external event such as link | |
1342 | * down or due to next-hop-tracking evaluation. In the latter case, | |
1343 | * a decision has already been made that the NHs have changed. So, no | |
1344 | * need to invoke a potentially expensive call again. Further, since | |
1345 | * the change might be in a recursive NH which is not caught in | |
1346 | * the nexthop_active_update() code. Thus, we might miss changes to | |
1347 | * recursive NHs. | |
6e26278c | 1348 | */ |
ca657c65 | 1349 | if (!CHECK_FLAG(rib->flags, ZEBRA_FLAG_CHANGED) && |
f857321e DW |
1350 | ! nexthop_active_update (rn, rib, 0)) |
1351 | { | |
1352 | if (rib->type == ZEBRA_ROUTE_TABLE) | |
1353 | { | |
1354 | /* This entry was denied by the 'ip protocol table' route-map, we | |
1355 | * need to delete it */ | |
1356 | if (rib != fib) | |
1357 | { | |
1358 | if (IS_ZEBRA_DEBUG_RIB) | |
1359 | zlog_debug ("%s: %s/%d: imported via import-table but denied " | |
1360 | "by the ip protocol table route-map", | |
1361 | __func__, buf, rn->p.prefixlen); | |
1362 | rib_unlink (rn, rib); | |
1363 | } | |
1364 | else | |
1365 | del = rib; | |
1366 | } | |
1367 | ||
1368 | continue; | |
1369 | } | |
718e3744 | 1370 | |
f857321e | 1371 | /* Infinite distance. */ |
718e3744 | 1372 | if (rib->distance == DISTANCE_INFINITY) |
8733ba72 DS |
1373 | { |
1374 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); | |
1375 | continue; | |
1376 | } | |
718e3744 | 1377 | |
af887b51 | 1378 | /* Newly selected rib, the common case. */ |
1379 | if (!select) | |
1380 | { | |
1381 | select = rib; | |
1382 | continue; | |
1383 | } | |
1384 | ||
1385 | /* filter route selection in following order: | |
af887b51 | 1386 | * - connected beats other types |
a8d9c1f9 | 1387 | * - lower distance beats higher |
af887b51 | 1388 | * - lower metric beats higher for equal distance |
1389 | * - last, hence oldest, route wins tie break. | |
1390 | */ | |
a1038a15 | 1391 | |
1392 | /* Connected routes. Pick the last connected | |
1393 | * route of the set of lowest metric connected routes. | |
1394 | */ | |
a8d9c1f9 | 1395 | if (rib->type == ZEBRA_ROUTE_CONNECT) |
1396 | { | |
a1038a15 | 1397 | if (select->type != ZEBRA_ROUTE_CONNECT |
a8d9c1f9 | 1398 | || rib->metric <= select->metric) |
8733ba72 DS |
1399 | { |
1400 | UNSET_FLAG (select->flags, ZEBRA_FLAG_CHANGED); | |
1401 | select = rib; | |
1402 | } | |
1403 | else | |
1404 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); | |
a1038a15 | 1405 | continue; |
a8d9c1f9 | 1406 | } |
1407 | else if (select->type == ZEBRA_ROUTE_CONNECT) | |
8733ba72 DS |
1408 | { |
1409 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); | |
1410 | continue; | |
1411 | } | |
a8d9c1f9 | 1412 | |
1413 | /* higher distance loses */ | |
1414 | if (rib->distance > select->distance) | |
8733ba72 DS |
1415 | { |
1416 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); | |
1417 | continue; | |
1418 | } | |
a8d9c1f9 | 1419 | |
1420 | /* lower wins */ | |
1421 | if (rib->distance < select->distance) | |
1422 | { | |
8733ba72 | 1423 | UNSET_FLAG (select->flags, ZEBRA_FLAG_CHANGED); |
af887b51 | 1424 | select = rib; |
a8d9c1f9 | 1425 | continue; |
1426 | } | |
1427 | ||
1428 | /* metric tie-breaks equal distance */ | |
1429 | if (rib->metric <= select->metric) | |
8733ba72 DS |
1430 | { |
1431 | UNSET_FLAG (select->flags, ZEBRA_FLAG_CHANGED); | |
1432 | select = rib; | |
1433 | } | |
9fd92e3c | 1434 | } /* RNODE_FOREACH_RIB_SAFE */ |
dc95824a DO |
1435 | |
1436 | /* After the cycle is finished, the following pointers will be set: | |
1437 | * select --- the winner RIB entry, if any was found, otherwise NULL | |
1438 | * fib --- the SELECTED RIB entry, if any, otherwise NULL | |
1439 | * del --- equal to fib, if fib is queued for deletion, NULL otherwise | |
1440 | * rib --- NULL | |
1441 | */ | |
41ec9222 | 1442 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
1443 | zlog_debug ("%u:%s/%d: After processing: select %p fib %p del %p", | |
1444 | vrf_id, buf, rn->p.prefixlen, select, fib, del); | |
dc95824a DO |
1445 | |
1446 | /* Same RIB entry is selected. Update FIB and finish. */ | |
718e3744 | 1447 | if (select && select == fib) |
1448 | { | |
1449 | if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED)) | |
4d38fdb4 | 1450 | { |
5adc2528 AS |
1451 | zfpm_trigger_update (rn, "updating existing route"); |
1452 | ||
4d38fdb4 | 1453 | /* Set real nexthop. */ |
6e26278c DS |
1454 | /* Need to check if any NHs are active to clear the |
1455 | * the selected flag | |
1456 | */ | |
1457 | if (nexthop_active_update (rn, select, 1)) | |
1458 | { | |
41ec9222 | 1459 | if (IS_ZEBRA_DEBUG_RIB) |
1460 | zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)", | |
1461 | vrf_id, buf, rn->p.prefixlen, rn, select, select->type); | |
dccc5225 | 1462 | if (! RIB_SYSTEM_ROUTE (select)) |
6ae24471 | 1463 | { |
dccc5225 | 1464 | /* Clear FIB flag if performing a replace, will get set again |
1465 | * as part of install. | |
1466 | */ | |
6ae24471 DS |
1467 | for (nexthop = select->nexthop; nexthop; nexthop = nexthop->next) |
1468 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
dccc5225 | 1469 | rib_install_kernel (rn, select, 1); |
6ae24471 | 1470 | } |
5048fe14 | 1471 | |
1472 | /* assuming that the receiver knows how to dedup */ | |
c41fc67b | 1473 | redistribute_update (&rn->p, select, NULL); |
6e26278c DS |
1474 | } |
1475 | else | |
1476 | { | |
41ec9222 | 1477 | if (IS_ZEBRA_DEBUG_RIB) |
1478 | zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) " | |
1479 | "- nexthop inactive", | |
1480 | vrf_id, buf, rn->p.prefixlen, rn, select, select->type); | |
1481 | ||
5048fe14 | 1482 | /* Withdraw unreachable redistribute route */ |
1483 | redistribute_delete(&rn->p, select); | |
1484 | ||
dccc5225 | 1485 | /* Do the uninstall here, if not done earlier. */ |
1486 | if (! RIB_SYSTEM_ROUTE (select)) | |
6ae24471 | 1487 | rib_uninstall_kernel (rn, select); |
6e26278c DS |
1488 | UNSET_FLAG (select->flags, ZEBRA_FLAG_SELECTED); |
1489 | } | |
ca657c65 | 1490 | UNSET_FLAG (select->flags, ZEBRA_FLAG_CHANGED); |
6e26278c | 1491 | } |
d753e9ee | 1492 | else if (! RIB_SYSTEM_ROUTE (select)) |
4d38fdb4 | 1493 | { |
1494 | /* Housekeeping code to deal with | |
1495 | race conditions in kernel with linux | |
1496 | netlink reporting interface up before IPv4 or IPv6 protocol | |
1497 | is ready to add routes. | |
1498 | This makes sure the routes are IN the kernel. | |
1499 | */ | |
1500 | ||
fa713d9e | 1501 | for (ALL_NEXTHOPS_RO(select->nexthop, nexthop, tnexthop, recursing)) |
a3aaf5b0 | 1502 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
4d38fdb4 | 1503 | { |
a3aaf5b0 DO |
1504 | installed = 1; |
1505 | break; | |
4d38fdb4 | 1506 | } |
1507 | if (! installed) | |
6ae24471 | 1508 | rib_install_kernel (rn, select, 0); |
4d38fdb4 | 1509 | } |
6d691129 | 1510 | goto end; |
718e3744 | 1511 | } |
1512 | ||
dc95824a DO |
1513 | /* At this point we either haven't found the best RIB entry or it is |
1514 | * different from what we currently intend to flag with SELECTED. In both | |
1515 | * cases, if a RIB block is present in FIB, it should be withdrawn. | |
1516 | */ | |
718e3744 | 1517 | if (fib) |
1518 | { | |
5adc2528 AS |
1519 | zfpm_trigger_update (rn, "removing existing route"); |
1520 | ||
dccc5225 | 1521 | /* If there's no route to replace this with, withdraw redistribute and |
1522 | * uninstall from kernel. | |
1523 | */ | |
5048fe14 | 1524 | if (!select) |
6ae24471 | 1525 | { |
41ec9222 | 1526 | if (IS_ZEBRA_DEBUG_RIB) |
1527 | zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)", | |
1528 | vrf_id, buf, rn->p.prefixlen, rn, fib, fib->type); | |
1529 | ||
dccc5225 | 1530 | redistribute_delete(&rn->p, fib); |
1531 | if (! RIB_SYSTEM_ROUTE (fib)) | |
1532 | rib_uninstall_kernel (rn, fib); | |
6ae24471 | 1533 | } |
dccc5225 | 1534 | |
718e3744 | 1535 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); |
1536 | ||
1537 | /* Set real nexthop. */ | |
1538 | nexthop_active_update (rn, fib, 1); | |
ca657c65 | 1539 | UNSET_FLAG(fib->flags, ZEBRA_FLAG_CHANGED); |
718e3744 | 1540 | } |
1541 | ||
dc95824a DO |
1542 | /* Regardless of some RIB entry being SELECTED or not before, now we can |
1543 | * tell, that if a new winner exists, FIB is still not updated with this | |
1544 | * data, but ready to be. | |
1545 | */ | |
718e3744 | 1546 | if (select) |
1547 | { | |
5adc2528 AS |
1548 | zfpm_trigger_update (rn, "new route selected"); |
1549 | ||
718e3744 | 1550 | /* Set real nexthop. */ |
6e26278c DS |
1551 | if (nexthop_active_update (rn, select, 1)) |
1552 | { | |
41ec9222 | 1553 | if (IS_ZEBRA_DEBUG_RIB) |
1554 | { | |
1555 | if (fib) | |
1556 | zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) " | |
1557 | "old %p (type %d)", vrf_id, buf, rn->p.prefixlen, rn, | |
1558 | select, select->type, fib, fib->type); | |
1559 | else | |
1560 | zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)", | |
1561 | vrf_id, buf, rn->p.prefixlen, rn, select, select->type); | |
1562 | } | |
1563 | ||
dccc5225 | 1564 | if (! RIB_SYSTEM_ROUTE (select)) |
6ae24471 | 1565 | { |
dccc5225 | 1566 | /* Clear FIB flag if performing a replace, will get set again |
1567 | * as part of install. | |
1568 | */ | |
1569 | if (fib) | |
1570 | { | |
1571 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) | |
1572 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
1573 | } | |
1574 | rib_install_kernel (rn, select, fib? 1 : 0); | |
6ae24471 | 1575 | } |
0b560feb | 1576 | else |
1577 | { | |
1578 | /* Uninstall prior route here, if needed. */ | |
1579 | if (fib && !RIB_SYSTEM_ROUTE (fib)) | |
1580 | rib_uninstall_kernel (rn, fib); | |
1581 | } | |
dccc5225 | 1582 | |
6e26278c | 1583 | SET_FLAG (select->flags, ZEBRA_FLAG_SELECTED); |
5048fe14 | 1584 | /* Unconditionally announce, this part is exercised by new routes */ |
c41fc67b | 1585 | /* If we cannot add, for example route added is learnt by the */ |
1586 | /* protocol we're trying to redistribute to, delete the redist */ | |
1587 | /* This is notified by setting the is_update to 1 */ | |
1588 | redistribute_update (&rn->p, select, fib); | |
6e26278c | 1589 | } |
6ae24471 DS |
1590 | else |
1591 | { | |
41ec9222 | 1592 | /* Uninstall prior route here and do redist delete, if needed. */ |
a219b295 | 1593 | if (fib) |
41ec9222 | 1594 | { |
1595 | if (IS_ZEBRA_DEBUG_RIB) | |
1596 | zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) " | |
1597 | "- nexthop inactive", | |
1598 | vrf_id, buf, rn->p.prefixlen, rn, fib, fib->type); | |
1599 | ||
1600 | if (!RIB_SYSTEM_ROUTE (fib)) | |
1601 | rib_uninstall_kernel (rn, fib); | |
1602 | redistribute_delete(&rn->p, fib); | |
1603 | } | |
6ae24471 | 1604 | } |
ca657c65 | 1605 | UNSET_FLAG(select->flags, ZEBRA_FLAG_CHANGED); |
718e3744 | 1606 | } |
4d38fdb4 | 1607 | |
6d691129 PJ |
1608 | /* FIB route was removed, should be deleted */ |
1609 | if (del) | |
1610 | { | |
1611 | if (IS_ZEBRA_DEBUG_RIB) | |
41ec9222 | 1612 | zlog_debug ("%u:%s/%d: Freeing route rn %p, rib %p (type %d)", |
1613 | vrf_id, buf, rn->p.prefixlen, rn, del, del->type); | |
6d691129 PJ |
1614 | rib_unlink (rn, del); |
1615 | } | |
4d38fdb4 | 1616 | |
6d691129 | 1617 | end: |
9fd92e3c AS |
1618 | /* |
1619 | * Check if the dest can be deleted now. | |
1620 | */ | |
1621 | rib_gc_dest (rn); | |
e96f9203 DO |
1622 | } |
1623 | ||
5110a0c6 SH |
1624 | /* Take a list of route_node structs and return 1, if there was a record |
1625 | * picked from it and processed by rib_process(). Don't process more, | |
1626 | * than one RN record; operate only in the specified sub-queue. | |
e96f9203 | 1627 | */ |
ef9b113e | 1628 | static unsigned int |
e96f9203 DO |
1629 | process_subq (struct list * subq, u_char qindex) |
1630 | { | |
5110a0c6 | 1631 | struct listnode *lnode = listhead (subq); |
e96f9203 | 1632 | struct route_node *rnode; |
41ec9222 | 1633 | char buf[INET6_ADDRSTRLEN]; |
1634 | rib_dest_t *dest; | |
1635 | struct zebra_vrf *zvrf = NULL; | |
5110a0c6 SH |
1636 | |
1637 | if (!lnode) | |
e96f9203 | 1638 | return 0; |
5110a0c6 | 1639 | |
e96f9203 | 1640 | rnode = listgetdata (lnode); |
41ec9222 | 1641 | dest = rib_dest_from_rnode (rnode); |
1642 | if (dest) | |
1643 | zvrf = rib_dest_vrf (dest); | |
1644 | ||
e96f9203 | 1645 | rib_process (rnode); |
5110a0c6 | 1646 | |
41ec9222 | 1647 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
1648 | { | |
1649 | inet_ntop (rnode->p.family, &rnode->p.u.prefix, buf, INET6_ADDRSTRLEN); | |
1650 | zlog_debug ("%u:%s/%d: rn %p dequeued from sub-queue %u", | |
1651 | zvrf ? zvrf->vrf_id : 0, buf, rnode->p.prefixlen, rnode, qindex); | |
1652 | } | |
1653 | ||
9fd92e3c AS |
1654 | if (rnode->info) |
1655 | UNSET_FLAG (rib_dest_from_rnode (rnode)->flags, RIB_ROUTE_QUEUED (qindex)); | |
1656 | ||
67b9467f | 1657 | #if 0 |
5110a0c6 SH |
1658 | else |
1659 | { | |
1660 | zlog_debug ("%s: called for route_node (%p, %d) with no ribs", | |
1661 | __func__, rnode, rnode->lock); | |
1662 | zlog_backtrace(LOG_DEBUG); | |
1663 | } | |
67b9467f | 1664 | #endif |
e96f9203 DO |
1665 | route_unlock_node (rnode); |
1666 | list_delete_node (subq, lnode); | |
1667 | return 1; | |
1668 | } | |
1669 | ||
fb018d25 DS |
1670 | /* |
1671 | * All meta queues have been processed. Trigger next-hop evaluation. | |
1672 | */ | |
1673 | static void | |
1674 | meta_queue_process_complete (struct work_queue *dummy) | |
1675 | { | |
9ec6b0bb | 1676 | vrf_iter_t iter; |
1677 | struct zebra_vrf *zvrf; | |
1678 | ||
1679 | /* Evaluate nexthops for those VRFs which underwent route processing. This | |
1680 | * should limit the evaluation to the necessary VRFs in most common | |
1681 | * situations. | |
1682 | */ | |
1683 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) | |
1684 | { | |
1685 | if (((zvrf = vrf_iter2info (iter)) != NULL) && | |
1686 | (zvrf->flags & ZEBRA_VRF_RIB_SCHEDULED)) | |
1687 | { | |
1688 | zvrf->flags &= ~ZEBRA_VRF_RIB_SCHEDULED; | |
1689 | zebra_evaluate_rnh(zvrf->vrf_id, AF_INET, 0, RNH_NEXTHOP_TYPE, NULL); | |
1690 | zebra_evaluate_rnh(zvrf->vrf_id, AF_INET, 0, RNH_IMPORT_CHECK_TYPE, NULL); | |
1691 | zebra_evaluate_rnh(zvrf->vrf_id, AF_INET6, 0, RNH_NEXTHOP_TYPE, NULL); | |
1692 | zebra_evaluate_rnh(zvrf->vrf_id, AF_INET6, 0, RNH_IMPORT_CHECK_TYPE, NULL); | |
1693 | } | |
1694 | } | |
fb018d25 DS |
1695 | } |
1696 | ||
e96f9203 DO |
1697 | /* Dispatch the meta queue by picking, processing and unlocking the next RN from |
1698 | * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data | |
1699 | * is pointed to the meta queue structure. | |
1700 | */ | |
1701 | static wq_item_status | |
1702 | meta_queue_process (struct work_queue *dummy, void *data) | |
1703 | { | |
1704 | struct meta_queue * mq = data; | |
5110a0c6 SH |
1705 | unsigned i; |
1706 | ||
e96f9203 DO |
1707 | for (i = 0; i < MQ_SIZE; i++) |
1708 | if (process_subq (mq->subq[i], i)) | |
5110a0c6 SH |
1709 | { |
1710 | mq->size--; | |
1711 | break; | |
1712 | } | |
e96f9203 DO |
1713 | return mq->size ? WQ_REQUEUE : WQ_SUCCESS; |
1714 | } | |
1715 | ||
9fd92e3c AS |
1716 | /* |
1717 | * Map from rib types to queue type (priority) in meta queue | |
1718 | */ | |
5110a0c6 SH |
1719 | static const u_char meta_queue_map[ZEBRA_ROUTE_MAX] = { |
1720 | [ZEBRA_ROUTE_SYSTEM] = 4, | |
1721 | [ZEBRA_ROUTE_KERNEL] = 0, | |
1722 | [ZEBRA_ROUTE_CONNECT] = 0, | |
1723 | [ZEBRA_ROUTE_STATIC] = 1, | |
1724 | [ZEBRA_ROUTE_RIP] = 2, | |
1725 | [ZEBRA_ROUTE_RIPNG] = 2, | |
1726 | [ZEBRA_ROUTE_OSPF] = 2, | |
1727 | [ZEBRA_ROUTE_OSPF6] = 2, | |
1728 | [ZEBRA_ROUTE_ISIS] = 2, | |
1729 | [ZEBRA_ROUTE_BGP] = 3, | |
1730 | [ZEBRA_ROUTE_HSLS] = 4, | |
7a4bb9c5 | 1731 | [ZEBRA_ROUTE_TABLE] = 1, |
5110a0c6 SH |
1732 | }; |
1733 | ||
1734 | /* Look into the RN and queue it into one or more priority queues, | |
1735 | * increasing the size for each data push done. | |
e96f9203 | 1736 | */ |
ef9b113e SH |
1737 | static void |
1738 | rib_meta_queue_add (struct meta_queue *mq, struct route_node *rn) | |
e96f9203 | 1739 | { |
e96f9203 DO |
1740 | struct rib *rib; |
1741 | char buf[INET6_ADDRSTRLEN]; | |
5110a0c6 | 1742 | |
41ec9222 | 1743 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
e96f9203 | 1744 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); |
5110a0c6 | 1745 | |
9fd92e3c | 1746 | RNODE_FOREACH_RIB (rn, rib) |
e96f9203 | 1747 | { |
5110a0c6 | 1748 | u_char qindex = meta_queue_map[rib->type]; |
9ec6b0bb | 1749 | struct zebra_vrf *zvrf; |
5110a0c6 SH |
1750 | |
1751 | /* Invariant: at this point we always have rn->info set. */ | |
9fd92e3c AS |
1752 | if (CHECK_FLAG (rib_dest_from_rnode (rn)->flags, |
1753 | RIB_ROUTE_QUEUED (qindex))) | |
41ec9222 | 1754 | continue; |
5110a0c6 | 1755 | |
9fd92e3c | 1756 | SET_FLAG (rib_dest_from_rnode (rn)->flags, RIB_ROUTE_QUEUED (qindex)); |
5110a0c6 SH |
1757 | listnode_add (mq->subq[qindex], rn); |
1758 | route_lock_node (rn); | |
1759 | mq->size++; | |
1760 | ||
41ec9222 | 1761 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
1762 | zlog_debug ("%u:%s/%d: rn %p queued into sub-queue %u", | |
1763 | rib->vrf_id, buf, rn->p.prefixlen, rn, qindex); | |
9ec6b0bb | 1764 | |
1765 | zvrf = zebra_vrf_lookup (rib->vrf_id); | |
1766 | if (zvrf) | |
1767 | zvrf->flags |= ZEBRA_VRF_RIB_SCHEDULED; | |
e96f9203 | 1768 | } |
4d38fdb4 | 1769 | } |
1770 | ||
6d691129 | 1771 | /* Add route_node to work queue and schedule processing */ |
6e26278c | 1772 | void |
44e9909d | 1773 | rib_queue_add (struct route_node *rn) |
4d38fdb4 | 1774 | { |
44e9909d | 1775 | assert (rn); |
4d38fdb4 | 1776 | |
fc328ac9 | 1777 | /* Pointless to queue a route_node with no RIB entries to add or remove */ |
9fd92e3c | 1778 | if (!rnode_to_ribs (rn)) |
6d691129 | 1779 | { |
fc328ac9 SV |
1780 | zlog_debug ("%s: called for route_node (%p, %d) with no ribs", |
1781 | __func__, rn, rn->lock); | |
1782 | zlog_backtrace(LOG_DEBUG); | |
1783 | return; | |
1784 | } | |
1785 | ||
44e9909d | 1786 | if (zebrad.ribq == NULL) |
fc328ac9 SV |
1787 | { |
1788 | zlog_err ("%s: work_queue does not exist!", __func__); | |
1789 | return; | |
4d38fdb4 | 1790 | } |
4d38fdb4 | 1791 | |
cc2dd928 SH |
1792 | /* |
1793 | * The RIB queue should normally be either empty or holding the only | |
1794 | * work_queue_item element. In the latter case this element would | |
1795 | * hold a pointer to the meta queue structure, which must be used to | |
1796 | * actually queue the route nodes to process. So create the MQ | |
1797 | * holder, if necessary, then push the work into it in any case. | |
e96f9203 DO |
1798 | * This semantics was introduced after 0.99.9 release. |
1799 | */ | |
44e9909d DS |
1800 | if (!zebrad.ribq->items->count) |
1801 | work_queue_add (zebrad.ribq, zebrad.mq); | |
e96f9203 | 1802 | |
44e9909d | 1803 | rib_meta_queue_add (zebrad.mq, rn); |
fc328ac9 | 1804 | |
fc328ac9 | 1805 | return; |
4d38fdb4 | 1806 | } |
1807 | ||
5110a0c6 SH |
1808 | /* Create new meta queue. |
1809 | A destructor function doesn't seem to be necessary here. | |
1810 | */ | |
ef9b113e SH |
1811 | static struct meta_queue * |
1812 | meta_queue_new (void) | |
e96f9203 DO |
1813 | { |
1814 | struct meta_queue *new; | |
5110a0c6 SH |
1815 | unsigned i; |
1816 | ||
1817 | new = XCALLOC (MTYPE_WORK_QUEUE, sizeof (struct meta_queue)); | |
1818 | assert(new); | |
e96f9203 | 1819 | |
e96f9203 | 1820 | for (i = 0; i < MQ_SIZE; i++) |
5110a0c6 SH |
1821 | { |
1822 | new->subq[i] = list_new (); | |
1823 | assert(new->subq[i]); | |
1824 | } | |
1825 | ||
e96f9203 DO |
1826 | return new; |
1827 | } | |
1828 | ||
4d38fdb4 | 1829 | /* initialise zebra rib work queue */ |
a1ac18c4 | 1830 | static void |
4d38fdb4 | 1831 | rib_queue_init (struct zebra_t *zebra) |
1832 | { | |
fc328ac9 SV |
1833 | assert (zebra); |
1834 | ||
4d38fdb4 | 1835 | if (! (zebra->ribq = work_queue_new (zebra->master, |
6d691129 | 1836 | "route_node processing"))) |
4d38fdb4 | 1837 | { |
6d691129 | 1838 | zlog_err ("%s: could not initialise work queue!", __func__); |
4d38fdb4 | 1839 | return; |
1840 | } | |
1841 | ||
1842 | /* fill in the work queue spec */ | |
e96f9203 | 1843 | zebra->ribq->spec.workfunc = &meta_queue_process; |
4d38fdb4 | 1844 | zebra->ribq->spec.errorfunc = NULL; |
fb018d25 | 1845 | zebra->ribq->spec.completion_func = &meta_queue_process_complete; |
4d38fdb4 | 1846 | /* XXX: TODO: These should be runtime configurable via vty */ |
1847 | zebra->ribq->spec.max_retries = 3; | |
457eb9af | 1848 | zebra->ribq->spec.hold = rib_process_hold_time; |
4d38fdb4 | 1849 | |
e96f9203 | 1850 | if (!(zebra->mq = meta_queue_new ())) |
fc328ac9 | 1851 | { |
e96f9203 | 1852 | zlog_err ("%s: could not initialise meta queue!", __func__); |
fc328ac9 SV |
1853 | return; |
1854 | } | |
1855 | return; | |
718e3744 | 1856 | } |
1857 | ||
6d691129 PJ |
1858 | /* RIB updates are processed via a queue of pointers to route_nodes. |
1859 | * | |
1860 | * The queue length is bounded by the maximal size of the routing table, | |
1861 | * as a route_node will not be requeued, if already queued. | |
1862 | * | |
3c0755dc | 1863 | * RIBs are submitted via rib_addnode or rib_delnode which set minimal |
bcd548ff | 1864 | * state, or static_install_route (when an existing RIB is updated) |
3c0755dc PJ |
1865 | * and then submit route_node to queue for best-path selection later. |
1866 | * Order of add/delete state changes are preserved for any given RIB. | |
6d691129 PJ |
1867 | * |
1868 | * Deleted RIBs are reaped during best-path selection. | |
1869 | * | |
1870 | * rib_addnode | |
1871 | * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with | |
3c0755dc PJ |
1872 | * |-------->| | best RIB, if required |
1873 | * | | | |
1874 | * static_install->|->rib_addqueue...... -> rib_process | |
1875 | * | | | |
1876 | * |-------->| |-> rib_unlink | |
6d691129 PJ |
1877 | * |-> set RIB_ENTRY_REMOVE | |
1878 | * rib_delnode (RIB freed) | |
1879 | * | |
9fd92e3c AS |
1880 | * The 'info' pointer of a route_node points to a rib_dest_t |
1881 | * ('dest'). Queueing state for a route_node is kept on the dest. The | |
1882 | * dest is created on-demand by rib_link() and is kept around at least | |
1883 | * as long as there are ribs hanging off it (@see rib_gc_dest()). | |
6d691129 PJ |
1884 | * |
1885 | * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code): | |
1886 | * | |
1887 | * - route_nodes: refcounted by: | |
9fd92e3c AS |
1888 | * - dest attached to route_node: |
1889 | * - managed by: rib_link/rib_gc_dest | |
6d691129 PJ |
1890 | * - route_node processing queue |
1891 | * - managed by: rib_addqueue, rib_process. | |
1892 | * | |
1893 | */ | |
1894 | ||
718e3744 | 1895 | /* Add RIB to head of the route node. */ |
a1ac18c4 | 1896 | static void |
2bf26d41 | 1897 | rib_link (struct route_node *rn, struct rib *rib, int process) |
718e3744 | 1898 | { |
1899 | struct rib *head; | |
9fd92e3c | 1900 | rib_dest_t *dest; |
f304cb48 | 1901 | char buf[INET6_ADDRSTRLEN]; |
7a4bb9c5 | 1902 | afi_t afi; |
9fd92e3c | 1903 | |
4d38fdb4 | 1904 | assert (rib && rn); |
1905 | ||
9fd92e3c AS |
1906 | dest = rib_dest_from_rnode (rn); |
1907 | if (!dest) | |
6d691129 | 1908 | { |
41ec9222 | 1909 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) |
9fd92e3c | 1910 | { |
41ec9222 | 1911 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); |
1912 | zlog_debug ("%u:%s/%d: rn %p adding dest", | |
1913 | rib->vrf_id, buf, rn->p.prefixlen, rn); | |
9fd92e3c AS |
1914 | } |
1915 | ||
1916 | dest = XCALLOC (MTYPE_RIB_DEST, sizeof (rib_dest_t)); | |
1917 | route_lock_node (rn); /* rn route table reference */ | |
1918 | rn->info = dest; | |
1919 | dest->rnode = rn; | |
1920 | } | |
1921 | ||
1922 | head = dest->routes; | |
1923 | if (head) | |
1924 | { | |
6d691129 | 1925 | head->prev = rib; |
6d691129 | 1926 | } |
718e3744 | 1927 | rib->next = head; |
9fd92e3c | 1928 | dest->routes = rib; |
7a4bb9c5 | 1929 | |
12f6fb97 DS |
1930 | afi = (rn->p.family == AF_INET) ? AFI_IP : |
1931 | (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX; | |
1932 | if (is_zebra_import_table_enabled (afi, rib->table)) | |
1933 | zebra_add_import_table_entry(rn, rib); | |
7a4bb9c5 | 1934 | else |
12f6fb97 | 1935 | if (process) |
44e9909d | 1936 | rib_queue_add (rn); |
718e3744 | 1937 | } |
1938 | ||
a1ac18c4 | 1939 | static void |
2bf26d41 | 1940 | rib_addnode (struct route_node *rn, struct rib *rib, int process) |
718e3744 | 1941 | { |
6d691129 PJ |
1942 | /* RIB node has been un-removed before route-node is processed. |
1943 | * route_node must hence already be on the queue for processing.. | |
1944 | */ | |
1945 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) | |
1946 | { | |
6d691129 PJ |
1947 | UNSET_FLAG (rib->status, RIB_ENTRY_REMOVED); |
1948 | return; | |
1949 | } | |
2bf26d41 | 1950 | rib_link (rn, rib, process); |
6d691129 PJ |
1951 | } |
1952 | ||
9fd92e3c AS |
1953 | /* |
1954 | * rib_unlink | |
1955 | * | |
1956 | * Detach a rib structure from a route_node. | |
1957 | * | |
1958 | * Note that a call to rib_unlink() should be followed by a call to | |
1959 | * rib_gc_dest() at some point. This allows a rib_dest_t that is no | |
1960 | * longer required to be deleted. | |
1961 | */ | |
6d691129 PJ |
1962 | static void |
1963 | rib_unlink (struct route_node *rn, struct rib *rib) | |
1964 | { | |
9fd92e3c | 1965 | rib_dest_t *dest; |
6d691129 | 1966 | |
4d38fdb4 | 1967 | assert (rn && rib); |
6d691129 | 1968 | |
9fd92e3c AS |
1969 | dest = rib_dest_from_rnode (rn); |
1970 | ||
718e3744 | 1971 | if (rib->next) |
1972 | rib->next->prev = rib->prev; | |
6d691129 | 1973 | |
718e3744 | 1974 | if (rib->prev) |
1975 | rib->prev->next = rib->next; | |
1976 | else | |
6d691129 | 1977 | { |
9fd92e3c | 1978 | dest->routes = rib->next; |
6d691129 PJ |
1979 | } |
1980 | ||
1981 | /* free RIB and nexthops */ | |
d82ae0de | 1982 | zebra_deregister_rnh_static_nexthops (rib->vrf_id, rib->nexthop, rn); |
a399694f | 1983 | nexthops_free(rib->nexthop); |
6d691129 PJ |
1984 | XFREE (MTYPE_RIB, rib); |
1985 | ||
6d691129 PJ |
1986 | } |
1987 | ||
1988 | static void | |
1989 | rib_delnode (struct route_node *rn, struct rib *rib) | |
1990 | { | |
7a4bb9c5 DS |
1991 | afi_t afi; |
1992 | ||
6d691129 | 1993 | SET_FLAG (rib->status, RIB_ENTRY_REMOVED); |
7a4bb9c5 | 1994 | |
12f6fb97 DS |
1995 | afi = (rn->p.family == AF_INET) ? AFI_IP : |
1996 | (rn->p.family == AF_INET6) ? AFI_IP6 : AFI_MAX; | |
1997 | if (is_zebra_import_table_enabled (afi, rib->table)) | |
7a4bb9c5 | 1998 | { |
12f6fb97 | 1999 | zebra_del_import_table_entry(rn, rib); |
7a4bb9c5 | 2000 | /* Just clean up if non main table */ |
41ec9222 | 2001 | if (IS_ZEBRA_DEBUG_RIB) |
2002 | { | |
2003 | char buf[INET6_ADDRSTRLEN]; | |
2004 | if (IS_ZEBRA_DEBUG_RIB) | |
2005 | { | |
2006 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); | |
2007 | zlog_debug ("%u:%s/%d: Freeing route rn %p, rib %p (type %d)", | |
2008 | rib->vrf_id, buf, rn->p.prefixlen, rn, rib, rib->type); | |
2009 | } | |
2010 | } | |
2011 | ||
7a4bb9c5 DS |
2012 | rib_unlink(rn, rib); |
2013 | } | |
12f6fb97 DS |
2014 | else |
2015 | { | |
44e9909d | 2016 | rib_queue_add (rn); |
12f6fb97 | 2017 | } |
718e3744 | 2018 | } |
2019 | ||
2020 | int | |
7c8ff89e | 2021 | rib_add_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p, |
7514fb77 | 2022 | struct in_addr *gate, struct in_addr *src, |
78104b9b | 2023 | unsigned int ifindex, vrf_id_t vrf_id, u_int32_t table_id, |
cddf391b | 2024 | u_int32_t metric, u_char distance, safi_t safi) |
718e3744 | 2025 | { |
2026 | struct rib *rib; | |
2027 | struct rib *same = NULL; | |
2028 | struct route_table *table; | |
2029 | struct route_node *rn; | |
2030 | struct nexthop *nexthop; | |
2031 | ||
2032 | /* Lookup table. */ | |
ca46a78e | 2033 | table = zebra_vrf_table_with_table_id (AFI_IP, safi, vrf_id, table_id); |
718e3744 | 2034 | if (! table) |
2035 | return 0; | |
2036 | ||
2037 | /* Make it sure prefixlen is applied to the prefix. */ | |
2038 | apply_mask_ipv4 (p); | |
2039 | ||
2040 | /* Set default distance by route type. */ | |
2041 | if (distance == 0) | |
2042 | { | |
837d16cc | 2043 | if ((unsigned)type >= array_size(route_info)) |
7052f228 DL |
2044 | distance = 150; |
2045 | else | |
2046 | distance = route_info[type].distance; | |
718e3744 | 2047 | |
2048 | /* iBGP distance is 200. */ | |
2049 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) | |
2050 | distance = 200; | |
2051 | } | |
2052 | ||
2053 | /* Lookup route node.*/ | |
2054 | rn = route_node_get (table, (struct prefix *) p); | |
2055 | ||
2056 | /* If same type of route are installed, treat it as a implicit | |
2057 | withdraw. */ | |
9fd92e3c | 2058 | RNODE_FOREACH_RIB (rn, rib) |
718e3744 | 2059 | { |
6d691129 PJ |
2060 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
2061 | continue; | |
2062 | ||
ebf1ead0 | 2063 | if (rib->type != type) |
2064 | continue; | |
7c8ff89e DS |
2065 | if (rib->instance != instance) |
2066 | continue; | |
7a4bb9c5 | 2067 | |
ebf1ead0 | 2068 | if (rib->type != ZEBRA_ROUTE_CONNECT) |
4d38fdb4 | 2069 | { |
2070 | same = rib; | |
2071 | break; | |
2072 | } | |
ebf1ead0 | 2073 | /* Duplicate connected route comes in. */ |
2074 | else if ((nexthop = rib->nexthop) && | |
2075 | nexthop->type == NEXTHOP_TYPE_IFINDEX && | |
6d691129 PJ |
2076 | nexthop->ifindex == ifindex && |
2077 | !CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) | |
ebf1ead0 | 2078 | { |
2079 | rib->refcnt++; | |
2080 | return 0 ; | |
2081 | } | |
718e3744 | 2082 | } |
2083 | ||
2084 | /* Allocate new rib structure. */ | |
4d38fdb4 | 2085 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
718e3744 | 2086 | rib->type = type; |
7c8ff89e | 2087 | rib->instance = instance; |
718e3744 | 2088 | rib->distance = distance; |
2089 | rib->flags = flags; | |
2090 | rib->metric = metric; | |
7a4bb9c5 | 2091 | rib->table = table_id; |
78104b9b | 2092 | rib->vrf_id = vrf_id; |
718e3744 | 2093 | rib->nexthop_num = 0; |
2094 | rib->uptime = time (NULL); | |
2095 | ||
2096 | /* Nexthop settings. */ | |
2097 | if (gate) | |
2098 | { | |
2099 | if (ifindex) | |
a399694f | 2100 | rib_nexthop_ipv4_ifindex_add (rib, gate, src, ifindex); |
718e3744 | 2101 | else |
a399694f | 2102 | rib_nexthop_ipv4_add (rib, gate, src); |
718e3744 | 2103 | } |
2104 | else | |
a399694f | 2105 | rib_nexthop_ifindex_add (rib, ifindex); |
718e3744 | 2106 | |
2107 | /* If this route is kernel route, set FIB flag to the route. */ | |
2108 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) | |
2109 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) | |
2110 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
2111 | ||
2112 | /* Link new rib to node.*/ | |
dc95824a | 2113 | if (IS_ZEBRA_DEBUG_RIB) |
41ec9222 | 2114 | { |
2115 | char buf[INET6_ADDRSTRLEN]; | |
2116 | if (IS_ZEBRA_DEBUG_RIB) | |
2117 | { | |
2118 | inet_ntop (p->family, &p->prefix, buf, INET6_ADDRSTRLEN); | |
2119 | zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) " | |
2120 | "existing %p", | |
2121 | vrf_id, buf, p->prefixlen, rn, rib, rib->type, same); | |
2122 | } | |
2123 | ||
2124 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
2125 | rib_dump ((struct prefix *)p, rib); | |
2126 | } | |
2bf26d41 | 2127 | rib_addnode (rn, rib, 1); |
4d38fdb4 | 2128 | |
718e3744 | 2129 | /* Free implicit route.*/ |
2130 | if (same) | |
4d38fdb4 | 2131 | rib_delnode (rn, same); |
2132 | ||
2133 | route_unlock_node (rn); | |
718e3744 | 2134 | return 0; |
2135 | } | |
2136 | ||
dc95824a DO |
2137 | /* This function dumps the contents of a given RIB entry into |
2138 | * standard debug log. Calling function name and IP prefix in | |
2139 | * question are passed as 1st and 2nd arguments. | |
2140 | */ | |
2141 | ||
f7bf4153 DL |
2142 | void _rib_dump (const char * func, |
2143 | union prefix46constptr pp, const struct rib * rib) | |
dc95824a | 2144 | { |
f7bf4153 | 2145 | const struct prefix *p = pp.p; |
4690c7d7 | 2146 | char straddr[PREFIX2STR_BUFFER]; |
fa713d9e CF |
2147 | struct nexthop *nexthop, *tnexthop; |
2148 | int recursing; | |
dc95824a | 2149 | |
78104b9b FL |
2150 | zlog_debug ("%s: dumping RIB entry %p for %s vrf %u", func, rib, |
2151 | prefix2str(pp, straddr, sizeof(straddr)), rib->vrf_id); | |
dc95824a DO |
2152 | zlog_debug |
2153 | ( | |
7c8ff89e | 2154 | "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d", |
dc95824a DO |
2155 | func, |
2156 | rib->refcnt, | |
d02c56cd | 2157 | (unsigned long) rib->uptime, |
dc95824a | 2158 | rib->type, |
7c8ff89e | 2159 | rib->instance, |
dc95824a DO |
2160 | rib->table |
2161 | ); | |
2162 | zlog_debug | |
2163 | ( | |
2164 | "%s: metric == %u, distance == %u, flags == %u, status == %u", | |
2165 | func, | |
2166 | rib->metric, | |
2167 | rib->distance, | |
2168 | rib->flags, | |
2169 | rib->status | |
2170 | ); | |
2171 | zlog_debug | |
2172 | ( | |
2173 | "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u", | |
2174 | func, | |
2175 | rib->nexthop_num, | |
2176 | rib->nexthop_active_num, | |
2177 | rib->nexthop_fib_num | |
2178 | ); | |
fed643f4 | 2179 | |
fa713d9e CF |
2180 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
2181 | { | |
fed643f4 | 2182 | inet_ntop (p->family, &nexthop->gate, straddr, INET6_ADDRSTRLEN); |
fa713d9e CF |
2183 | zlog_debug |
2184 | ( | |
2185 | "%s: %s %s with flags %s%s%s", | |
2186 | func, | |
2187 | (recursing ? " NH" : "NH"), | |
2188 | straddr, | |
2189 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""), | |
2190 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""), | |
2191 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "") | |
2192 | ); | |
2193 | } | |
dc95824a DO |
2194 | zlog_debug ("%s: dump complete", func); |
2195 | } | |
2196 | ||
2197 | /* This is an exported helper to rtm_read() to dump the strange | |
2198 | * RIB entry found by rib_lookup_ipv4_route() | |
2199 | */ | |
2200 | ||
12f6fb97 | 2201 | void rib_lookup_and_dump (struct prefix_ipv4 * p, vrf_id_t vrf_id) |
dc95824a DO |
2202 | { |
2203 | struct route_table *table; | |
2204 | struct route_node *rn; | |
2205 | struct rib *rib; | |
2206 | char prefix_buf[INET_ADDRSTRLEN]; | |
2207 | ||
2208 | /* Lookup table. */ | |
12f6fb97 | 2209 | table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id); |
dc95824a DO |
2210 | if (! table) |
2211 | { | |
b72ede27 | 2212 | zlog_err ("%s: zebra_vrf_table() returned NULL", __func__); |
dc95824a DO |
2213 | return; |
2214 | } | |
2215 | ||
2216 | inet_ntop (AF_INET, &p->prefix.s_addr, prefix_buf, INET_ADDRSTRLEN); | |
2217 | /* Scan the RIB table for exactly matching RIB entry. */ | |
2218 | rn = route_node_lookup (table, (struct prefix *) p); | |
2219 | ||
2220 | /* No route for this prefix. */ | |
2221 | if (! rn) | |
2222 | { | |
2223 | zlog_debug ("%s: lookup failed for %s/%d", __func__, prefix_buf, p->prefixlen); | |
2224 | return; | |
2225 | } | |
2226 | ||
2227 | /* Unlock node. */ | |
2228 | route_unlock_node (rn); | |
2229 | ||
2230 | /* let's go */ | |
9fd92e3c | 2231 | RNODE_FOREACH_RIB (rn, rib) |
dc95824a DO |
2232 | { |
2233 | zlog_debug | |
2234 | ( | |
2235 | "%s: rn %p, rib %p: %s, %s", | |
2236 | __func__, | |
2237 | rn, | |
2238 | rib, | |
2239 | (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"), | |
2240 | (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected") | |
2241 | ); | |
f7bf4153 | 2242 | rib_dump (p, rib); |
dc95824a DO |
2243 | } |
2244 | } | |
2245 | ||
20e5ff0a DO |
2246 | /* Check if requested address assignment will fail due to another |
2247 | * route being installed by zebra in FIB already. Take necessary | |
2248 | * actions, if needed: remove such a route from FIB and deSELECT | |
2249 | * corresponding RIB entry. Then put affected RN into RIBQ head. | |
2250 | */ | |
12f6fb97 | 2251 | void rib_lookup_and_pushup (struct prefix_ipv4 * p, vrf_id_t vrf_id) |
20e5ff0a DO |
2252 | { |
2253 | struct route_table *table; | |
2254 | struct route_node *rn; | |
2255 | struct rib *rib; | |
2256 | unsigned changed = 0; | |
2257 | ||
12f6fb97 | 2258 | if (NULL == (table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id))) |
20e5ff0a | 2259 | { |
b72ede27 | 2260 | zlog_err ("%s: zebra_vrf_table() returned NULL", __func__); |
20e5ff0a DO |
2261 | return; |
2262 | } | |
2263 | ||
2264 | /* No matches would be the simplest case. */ | |
2265 | if (NULL == (rn = route_node_lookup (table, (struct prefix *) p))) | |
2266 | return; | |
2267 | ||
2268 | /* Unlock node. */ | |
2269 | route_unlock_node (rn); | |
2270 | ||
2271 | /* Check all RIB entries. In case any changes have to be done, requeue | |
2272 | * the RN into RIBQ head. If the routing message about the new connected | |
2273 | * route (generated by the IP address we are going to assign very soon) | |
2274 | * comes before the RIBQ is processed, the new RIB entry will join | |
2275 | * RIBQ record already on head. This is necessary for proper revalidation | |
2276 | * of the rest of the RIB. | |
2277 | */ | |
9fd92e3c | 2278 | RNODE_FOREACH_RIB (rn, rib) |
20e5ff0a DO |
2279 | { |
2280 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) && | |
2281 | ! RIB_SYSTEM_ROUTE (rib)) | |
2282 | { | |
2283 | changed = 1; | |
2284 | if (IS_ZEBRA_DEBUG_RIB) | |
2285 | { | |
2286 | char buf[INET_ADDRSTRLEN]; | |
2287 | inet_ntop (rn->p.family, &p->prefix, buf, INET_ADDRSTRLEN); | |
41ec9222 | 2288 | zlog_debug ("%u:%s/%d: freeing way for connected prefix", |
2289 | rib->vrf_id, buf, p->prefixlen); | |
f7bf4153 | 2290 | rib_dump (&rn->p, rib); |
20e5ff0a DO |
2291 | } |
2292 | rib_uninstall (rn, rib); | |
2293 | } | |
2294 | } | |
2295 | if (changed) | |
44e9909d | 2296 | rib_queue_add (rn); |
20e5ff0a DO |
2297 | } |
2298 | ||
718e3744 | 2299 | int |
cddf391b | 2300 | rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib, safi_t safi) |
718e3744 | 2301 | { |
2302 | struct route_table *table; | |
2303 | struct route_node *rn; | |
2304 | struct rib *same; | |
2305 | struct nexthop *nexthop; | |
04b02fda | 2306 | int ret = 0; |
4d38fdb4 | 2307 | |
718e3744 | 2308 | /* Lookup table. */ |
ca46a78e | 2309 | table = zebra_vrf_table_with_table_id (AFI_IP, safi, rib->vrf_id, rib->table); |
718e3744 | 2310 | if (! table) |
2311 | return 0; | |
cddf391b | 2312 | |
718e3744 | 2313 | /* Make it sure prefixlen is applied to the prefix. */ |
2314 | apply_mask_ipv4 (p); | |
2315 | ||
2316 | /* Set default distance by route type. */ | |
2317 | if (rib->distance == 0) | |
2318 | { | |
2319 | rib->distance = route_info[rib->type].distance; | |
2320 | ||
2321 | /* iBGP distance is 200. */ | |
2322 | if (rib->type == ZEBRA_ROUTE_BGP | |
2323 | && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP)) | |
2324 | rib->distance = 200; | |
2325 | } | |
2326 | ||
2327 | /* Lookup route node.*/ | |
2328 | rn = route_node_get (table, (struct prefix *) p); | |
2329 | ||
2330 | /* If same type of route are installed, treat it as a implicit | |
2331 | withdraw. */ | |
9fd92e3c | 2332 | RNODE_FOREACH_RIB (rn, same) |
718e3744 | 2333 | { |
0b8c4f1d | 2334 | if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED)) |
6d691129 PJ |
2335 | continue; |
2336 | ||
7c8ff89e DS |
2337 | if (same->type == rib->type && same->instance == rib->instance |
2338 | && same->table == rib->table | |
718e3744 | 2339 | && same->type != ZEBRA_ROUTE_CONNECT) |
4d38fdb4 | 2340 | break; |
718e3744 | 2341 | } |
4d38fdb4 | 2342 | |
718e3744 | 2343 | /* If this route is kernel route, set FIB flag to the route. */ |
2344 | if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) | |
2345 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) | |
2346 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
2347 | ||
2348 | /* Link new rib to node.*/ | |
41ec9222 | 2349 | if (IS_ZEBRA_DEBUG_RIB) |
2350 | { | |
2351 | char buf[INET6_ADDRSTRLEN]; | |
2352 | if (IS_ZEBRA_DEBUG_RIB) | |
2353 | { | |
2354 | inet_ntop (p->family, &p->prefix, buf, INET6_ADDRSTRLEN); | |
2355 | zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) " | |
2356 | "existing %p", | |
2357 | rib->vrf_id, buf, p->prefixlen, rn, rib, rib->type, same); | |
2358 | } | |
2359 | ||
2360 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
2361 | rib_dump ((struct prefix *)p, rib); | |
2362 | } | |
2bf26d41 | 2363 | rib_addnode (rn, rib, 1); |
04b02fda | 2364 | ret = 1; |
718e3744 | 2365 | |
718e3744 | 2366 | /* Free implicit route.*/ |
2367 | if (same) | |
dc95824a | 2368 | { |
41ec9222 | 2369 | rib_delnode (rn, same); |
2370 | ret = -1; | |
dc95824a | 2371 | } |
4d38fdb4 | 2372 | |
2373 | route_unlock_node (rn); | |
04b02fda | 2374 | return ret; |
718e3744 | 2375 | } |
2376 | ||
ebf1ead0 | 2377 | /* XXX factor with rib_delete_ipv6 */ |
718e3744 | 2378 | int |
7c8ff89e | 2379 | rib_delete_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p, |
78104b9b | 2380 | struct in_addr *gate, unsigned int ifindex, vrf_id_t vrf_id, |
7a4bb9c5 | 2381 | u_int32_t table_id, safi_t safi) |
718e3744 | 2382 | { |
2383 | struct route_table *table; | |
2384 | struct route_node *rn; | |
2385 | struct rib *rib; | |
2386 | struct rib *fib = NULL; | |
2387 | struct rib *same = NULL; | |
fa713d9e CF |
2388 | struct nexthop *nexthop, *tnexthop; |
2389 | int recursing; | |
4690c7d7 | 2390 | char buf1[PREFIX2STR_BUFFER]; |
41ec9222 | 2391 | char buf2[INET6_ADDRSTRLEN]; |
718e3744 | 2392 | |
2393 | /* Lookup table. */ | |
ca46a78e | 2394 | table = zebra_vrf_table_with_table_id (AFI_IP, safi, vrf_id, table_id); |
718e3744 | 2395 | if (! table) |
2396 | return 0; | |
2397 | ||
2398 | /* Apply mask. */ | |
2399 | apply_mask_ipv4 (p); | |
2400 | ||
2401 | /* Lookup route node. */ | |
2402 | rn = route_node_lookup (table, (struct prefix *) p); | |
2403 | if (! rn) | |
2404 | { | |
41ec9222 | 2405 | if (IS_ZEBRA_DEBUG_RIB) |
2406 | zlog_debug ("%u:%s/%d: doesn't exist in rib", | |
2407 | vrf_id, inet_ntop (p->family, &p->prefix, buf1, INET6_ADDRSTRLEN), | |
2408 | p->prefixlen); | |
718e3744 | 2409 | return ZEBRA_ERR_RTNOEXIST; |
2410 | } | |
2411 | ||
2412 | /* Lookup same type route. */ | |
9fd92e3c | 2413 | RNODE_FOREACH_RIB (rn, rib) |
718e3744 | 2414 | { |
6d691129 PJ |
2415 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
2416 | continue; | |
2417 | ||
718e3744 | 2418 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
2419 | fib = rib; | |
2420 | ||
ebf1ead0 | 2421 | if (rib->type != type) |
2422 | continue; | |
7c8ff89e DS |
2423 | if (rib->instance != instance) |
2424 | continue; | |
ebf1ead0 | 2425 | if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) && |
4f1735fd | 2426 | nexthop->type == NEXTHOP_TYPE_IFINDEX) |
718e3744 | 2427 | { |
4f1735fd MF |
2428 | if (nexthop->ifindex != ifindex) |
2429 | continue; | |
ebf1ead0 | 2430 | if (rib->refcnt) |
718e3744 | 2431 | { |
ebf1ead0 | 2432 | rib->refcnt--; |
2433 | route_unlock_node (rn); | |
2434 | route_unlock_node (rn); | |
2435 | return 0; | |
718e3744 | 2436 | } |
ebf1ead0 | 2437 | same = rib; |
2438 | break; | |
718e3744 | 2439 | } |
ebf1ead0 | 2440 | /* Make sure that the route found has the same gateway. */ |
fa713d9e | 2441 | else |
5ec90d28 | 2442 | { |
fa713d9e CF |
2443 | if (gate == NULL) |
2444 | { | |
2445 | same = rib; | |
2446 | break; | |
2447 | } | |
2448 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) | |
2449 | if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate)) | |
2450 | { | |
2451 | same = rib; | |
2452 | break; | |
2453 | } | |
2454 | if (same) | |
2455 | break; | |
2456 | } | |
718e3744 | 2457 | } |
718e3744 | 2458 | /* If same type of route can't be found and this message is from |
2459 | kernel. */ | |
2460 | if (! same) | |
2461 | { | |
2037f143 DS |
2462 | if (fib && type == ZEBRA_ROUTE_KERNEL && |
2463 | CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) | |
2464 | { | |
41ec9222 | 2465 | if (IS_ZEBRA_DEBUG_RIB) |
2037f143 | 2466 | { |
41ec9222 | 2467 | zlog_debug ("%u:%s/%d: rn %p, rib %p (type %d) was deleted " |
2468 | "from kernel, adding", | |
2469 | vrf_id, inet_ntop (p->family, &p->prefix, buf1, INET6_ADDRSTRLEN), | |
2470 | p->prefixlen, rn, fib, fib->type); | |
2037f143 | 2471 | } |
6baf7bb8 DS |
2472 | if (allow_delete) |
2473 | { | |
2474 | /* Unset flags. */ | |
2475 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) | |
2476 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
2477 | ||
2478 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); | |
2479 | } | |
2480 | else | |
2481 | { | |
2482 | /* This means someone else, other than Zebra, has deleted | |
2483 | * a Zebra router from the kernel. We will add it back */ | |
2484 | rib_install_kernel(rn, fib, 0); | |
2485 | } | |
2037f143 | 2486 | } |
718e3744 | 2487 | else |
2488 | { | |
41ec9222 | 2489 | if (IS_ZEBRA_DEBUG_RIB) |
718e3744 | 2490 | { |
2491 | if (gate) | |
41ec9222 | 2492 | zlog_debug ("%u:%s: via %s ifindex %d type %d " |
78104b9b | 2493 | "doesn't exist in rib", |
41ec9222 | 2494 | vrf_id, prefix2str (p, buf1, sizeof(buf1)), |
81cce018 | 2495 | inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN), |
718e3744 | 2496 | ifindex, |
2497 | type); | |
2498 | else | |
41ec9222 | 2499 | zlog_debug ("%u:%s: ifindex %d type %d doesn't exist in rib", |
2500 | vrf_id, prefix2str (p, buf1, sizeof(buf1)), | |
718e3744 | 2501 | ifindex, |
2502 | type); | |
2503 | } | |
2504 | route_unlock_node (rn); | |
2505 | return ZEBRA_ERR_RTNOEXIST; | |
2506 | } | |
2507 | } | |
4d38fdb4 | 2508 | |
718e3744 | 2509 | if (same) |
4d38fdb4 | 2510 | rib_delnode (rn, same); |
2511 | ||
718e3744 | 2512 | route_unlock_node (rn); |
718e3744 | 2513 | return 0; |
2514 | } | |
6b0655a2 | 2515 | |
718e3744 | 2516 | /* Install static route into rib. */ |
a1ac18c4 | 2517 | static void |
bcd548ff | 2518 | static_install_route (afi_t afi, safi_t safi, struct prefix *p, struct static_route *si) |
718e3744 | 2519 | { |
2520 | struct rib *rib; | |
2521 | struct route_node *rn; | |
2522 | struct route_table *table; | |
6e26278c | 2523 | struct prefix nh_p; |
718e3744 | 2524 | |
2525 | /* Lookup table. */ | |
be5e48ab | 2526 | table = zebra_vrf_table (afi, safi, si->vrf_id); |
718e3744 | 2527 | if (! table) |
2528 | return; | |
2529 | ||
2530 | /* Lookup existing route */ | |
2531 | rn = route_node_get (table, p); | |
9fd92e3c | 2532 | RNODE_FOREACH_RIB (rn, rib) |
6d691129 PJ |
2533 | { |
2534 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) | |
2535 | continue; | |
2536 | ||
2537 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) | |
2538 | break; | |
2539 | } | |
718e3744 | 2540 | |
2541 | if (rib) | |
2542 | { | |
0d9551dc DS |
2543 | /* if tag value changed , update old value in RIB */ |
2544 | if (rib->tag != si->tag) | |
2545 | rib->tag = si->tag; | |
2546 | ||
718e3744 | 2547 | /* Same distance static route is there. Update it with new |
2548 | nexthop. */ | |
718e3744 | 2549 | route_unlock_node (rn); |
718e3744 | 2550 | switch (si->type) |
7021c425 | 2551 | { |
bcd548ff | 2552 | case STATIC_IPV4_GATEWAY: |
a399694f | 2553 | rib_nexthop_ipv4_add (rib, &si->addr.ipv4, NULL); |
bcd548ff DS |
2554 | nh_p.family = AF_INET; |
2555 | nh_p.prefixlen = IPV4_MAX_BITLEN; | |
2556 | nh_p.u.prefix4 = si->addr.ipv4; | |
d82ae0de | 2557 | zebra_register_rnh_static_nh(si->vrf_id, &nh_p, rn); |
bcd548ff | 2558 | break; |
ba779241 DS |
2559 | case STATIC_IFINDEX: |
2560 | rib_nexthop_ifindex_add (rib, si->ifindex); | |
bcd548ff DS |
2561 | break; |
2562 | case STATIC_IPV4_BLACKHOLE: | |
a399694f | 2563 | rib_nexthop_blackhole_add (rib); |
bcd548ff DS |
2564 | break; |
2565 | case STATIC_IPV6_GATEWAY: | |
a399694f | 2566 | rib_nexthop_ipv6_add (rib, &si->addr.ipv6); |
bcd548ff DS |
2567 | nh_p.family = AF_INET6; |
2568 | nh_p.prefixlen = IPV6_MAX_BITLEN; | |
2569 | nh_p.u.prefix6 = si->addr.ipv6; | |
d82ae0de | 2570 | zebra_register_rnh_static_nh(si->vrf_id, &nh_p, rn); |
bcd548ff | 2571 | break; |
ba779241 DS |
2572 | case STATIC_IPV6_GATEWAY_IFINDEX: |
2573 | rib_nexthop_ipv6_ifindex_add (rib, &si->addr.ipv6, si->ifindex); | |
bcd548ff | 2574 | break; |
4d38fdb4 | 2575 | } |
41ec9222 | 2576 | |
2577 | if (IS_ZEBRA_DEBUG_RIB) | |
2578 | { | |
2579 | char buf[INET6_ADDRSTRLEN]; | |
2580 | if (IS_ZEBRA_DEBUG_RIB) | |
2581 | { | |
2582 | inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN); | |
2583 | zlog_debug ("%u:%s/%d: Modifying route rn %p, rib %p (type %d)", | |
2584 | si->vrf_id, buf, p->prefixlen, rn, rib, rib->type); | |
2585 | } | |
2586 | } | |
2bf26d41 | 2587 | /* Schedule route for processing or invoke NHT, as appropriate. */ |
2588 | if (si->type == STATIC_IPV4_GATEWAY || | |
2589 | si->type == STATIC_IPV6_GATEWAY) | |
2590 | zebra_evaluate_rnh(si->vrf_id, nh_p.family, 1, RNH_NEXTHOP_TYPE, &nh_p); | |
2591 | else | |
44e9909d | 2592 | rib_queue_add (rn); |
718e3744 | 2593 | } |
2594 | else | |
2595 | { | |
2596 | /* This is new static route. */ | |
4d38fdb4 | 2597 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
2598 | ||
718e3744 | 2599 | rib->type = ZEBRA_ROUTE_STATIC; |
7c8ff89e | 2600 | rib->instance = 0; |
718e3744 | 2601 | rib->distance = si->distance; |
2602 | rib->metric = 0; | |
8f527c5e | 2603 | rib->vrf_id = si->vrf_id; |
12f6fb97 | 2604 | rib->table = si->vrf_id ? (zebra_vrf_lookup(si->vrf_id))->table_id : zebrad.rtm_table_default; |
718e3744 | 2605 | rib->nexthop_num = 0; |
0d9551dc | 2606 | rib->tag = si->tag; |
718e3744 | 2607 | |
2608 | switch (si->type) | |
7021c425 | 2609 | { |
bcd548ff | 2610 | case STATIC_IPV4_GATEWAY: |
a399694f | 2611 | rib_nexthop_ipv4_add (rib, &si->addr.ipv4, NULL); |
bcd548ff DS |
2612 | nh_p.family = AF_INET; |
2613 | nh_p.prefixlen = IPV4_MAX_BITLEN; | |
2614 | nh_p.u.prefix4 = si->addr.ipv4; | |
d82ae0de | 2615 | zebra_register_rnh_static_nh(si->vrf_id, &nh_p, rn); |
bcd548ff | 2616 | break; |
ba779241 DS |
2617 | case STATIC_IFINDEX: |
2618 | rib_nexthop_ifindex_add (rib, si->ifindex); | |
bcd548ff DS |
2619 | break; |
2620 | case STATIC_IPV4_BLACKHOLE: | |
a399694f | 2621 | rib_nexthop_blackhole_add (rib); |
bcd548ff DS |
2622 | break; |
2623 | case STATIC_IPV6_GATEWAY: | |
a399694f | 2624 | rib_nexthop_ipv6_add (rib, &si->addr.ipv6); |
bcd548ff DS |
2625 | nh_p.family = AF_INET6; |
2626 | nh_p.prefixlen = IPV6_MAX_BITLEN; | |
2627 | nh_p.u.prefix6 = si->addr.ipv6; | |
d82ae0de | 2628 | zebra_register_rnh_static_nh(si->vrf_id, &nh_p, rn); |
bcd548ff | 2629 | break; |
ba779241 DS |
2630 | case STATIC_IPV6_GATEWAY_IFINDEX: |
2631 | rib_nexthop_ipv6_ifindex_add (rib, &si->addr.ipv6, si->ifindex); | |
bcd548ff | 2632 | break; |
7021c425 | 2633 | } |
718e3744 | 2634 | |
81dfcaa2 | 2635 | /* Save the flags of this static routes (reject, blackhole) */ |
2636 | rib->flags = si->flags; | |
2637 | ||
41ec9222 | 2638 | if (IS_ZEBRA_DEBUG_RIB) |
2639 | { | |
2640 | char buf[INET6_ADDRSTRLEN]; | |
2641 | if (IS_ZEBRA_DEBUG_RIB) | |
2642 | { | |
2643 | inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN); | |
2644 | zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d)", | |
2645 | si->vrf_id, buf, p->prefixlen, rn, rib, rib->type); | |
2646 | } | |
2647 | } | |
2bf26d41 | 2648 | /* Link this rib to the tree. Schedule for processing or invoke NHT, |
2649 | * as appropriate. | |
2650 | */ | |
2651 | if (si->type == STATIC_IPV4_GATEWAY || | |
2652 | si->type == STATIC_IPV6_GATEWAY) | |
2653 | { | |
2654 | rib_addnode (rn, rib, 0); | |
2655 | zebra_evaluate_rnh(si->vrf_id, nh_p.family, 1, RNH_NEXTHOP_TYPE, &nh_p); | |
2656 | } | |
2657 | else | |
2658 | rib_addnode (rn, rib, 1); | |
718e3744 | 2659 | } |
2660 | } | |
2661 | ||
a1ac18c4 | 2662 | static int |
f2b49ed0 | 2663 | static_nexthop_same (struct nexthop *nexthop, struct static_route *si) |
718e3744 | 2664 | { |
2665 | if (nexthop->type == NEXTHOP_TYPE_IPV4 | |
2666 | && si->type == STATIC_IPV4_GATEWAY | |
c0551cbb | 2667 | && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->addr.ipv4)) |
718e3744 | 2668 | return 1; |
ba779241 DS |
2669 | if (nexthop->type == NEXTHOP_TYPE_IFINDEX |
2670 | && si->type == STATIC_IFINDEX | |
2671 | && nexthop->ifindex == si->ifindex) | |
718e3744 | 2672 | return 1; |
595db7f1 | 2673 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE |
2674 | && si->type == STATIC_IPV4_BLACKHOLE) | |
2675 | return 1; | |
f2b49ed0 DS |
2676 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
2677 | && si->type == STATIC_IPV6_GATEWAY | |
2678 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6)) | |
2679 | return 1; | |
ba779241 DS |
2680 | if (nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX |
2681 | && si->type == STATIC_IPV6_GATEWAY_IFINDEX | |
f2b49ed0 | 2682 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6) |
ba779241 | 2683 | && nexthop->ifindex == si->ifindex) |
f2b49ed0 | 2684 | return 1; |
e8e1946e | 2685 | return 0; |
718e3744 | 2686 | } |
2687 | ||
2688 | /* Uninstall static route from RIB. */ | |
a1ac18c4 | 2689 | static void |
b8a1effa | 2690 | static_uninstall_route (afi_t afi, safi_t safi, struct prefix *p, struct static_route *si) |
718e3744 | 2691 | { |
2692 | struct route_node *rn; | |
2693 | struct rib *rib; | |
2694 | struct nexthop *nexthop; | |
2695 | struct route_table *table; | |
6e26278c | 2696 | struct prefix nh_p; |
718e3744 | 2697 | |
2698 | /* Lookup table. */ | |
be5e48ab | 2699 | table = zebra_vrf_table (afi, safi, si->vrf_id); |
718e3744 | 2700 | if (! table) |
2701 | return; | |
4d38fdb4 | 2702 | |
718e3744 | 2703 | /* Lookup existing route with type and distance. */ |
2704 | rn = route_node_lookup (table, p); | |
2705 | if (! rn) | |
2706 | return; | |
2707 | ||
9fd92e3c | 2708 | RNODE_FOREACH_RIB (rn, rib) |
6d691129 PJ |
2709 | { |
2710 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) | |
2711 | continue; | |
2712 | ||
0d9551dc DS |
2713 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance && |
2714 | rib->tag == si->tag) | |
6d691129 PJ |
2715 | break; |
2716 | } | |
718e3744 | 2717 | |
2718 | if (! rib) | |
2719 | { | |
2720 | route_unlock_node (rn); | |
2721 | return; | |
2722 | } | |
2723 | ||
2724 | /* Lookup nexthop. */ | |
2725 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) | |
f2b49ed0 | 2726 | if (static_nexthop_same (nexthop, si)) |
718e3744 | 2727 | break; |
2728 | ||
2729 | /* Can't find nexthop. */ | |
2730 | if (! nexthop) | |
2731 | { | |
2732 | route_unlock_node (rn); | |
2733 | return; | |
2734 | } | |
2735 | ||
2736 | /* Check nexthop. */ | |
2737 | if (rib->nexthop_num == 1) | |
6d691129 | 2738 | rib_delnode (rn, rib); |
718e3744 | 2739 | else |
2740 | { | |
94ad353d DS |
2741 | /* Mark this nexthop as inactive and reinstall the route. Then, delete |
2742 | * the nexthop. There is no need to re-evaluate the route for this | |
2743 | * scenario. | |
2744 | */ | |
41ec9222 | 2745 | if (IS_ZEBRA_DEBUG_RIB) |
2746 | { | |
2747 | char buf[INET6_ADDRSTRLEN]; | |
2748 | if (IS_ZEBRA_DEBUG_RIB) | |
2749 | { | |
2750 | inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN); | |
2751 | zlog_debug ("%u:%s/%d: Modifying route rn %p, rib %p (type %d)", | |
2752 | si->vrf_id, buf, p->prefixlen, rn, rib, rib->type); | |
2753 | } | |
2754 | } | |
94ad353d | 2755 | UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
6baeb988 | 2756 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
94ad353d | 2757 | { |
dccc5225 | 2758 | /* If there are other active nexthops, do an update. */ |
2759 | if (rib->nexthop_active_num > 1) | |
2760 | { | |
2761 | rib_install_kernel (rn, rib, 1); | |
2762 | redistribute_update (&rn->p, rib, NULL); | |
2763 | } | |
2764 | else | |
2765 | { | |
2766 | redistribute_delete (&rn->p, rib); | |
2767 | rib_uninstall_kernel (rn, rib); | |
2768 | } | |
94ad353d | 2769 | } |
6e26278c | 2770 | |
b8a1effa DS |
2771 | if (afi == AFI_IP) |
2772 | { | |
2773 | /* Delete the nexthop and dereg from NHT */ | |
2774 | nh_p.family = AF_INET; | |
2775 | nh_p.prefixlen = IPV4_MAX_BITLEN; | |
2776 | nh_p.u.prefix4 = nexthop->gate.ipv4; | |
2777 | } | |
2778 | else | |
2779 | { | |
2780 | nh_p.family = AF_INET6; | |
2781 | nh_p.prefixlen = IPV6_MAX_BITLEN; | |
2782 | nh_p.u.prefix6 = nexthop->gate.ipv6; | |
2783 | } | |
a399694f | 2784 | rib_nexthop_delete (rib, nexthop); |
d82ae0de | 2785 | zebra_deregister_rnh_static_nh(si->vrf_id, &nh_p, rn); |
a399694f | 2786 | nexthop_free (nexthop); |
718e3744 | 2787 | } |
718e3744 | 2788 | /* Unlock node. */ |
2789 | route_unlock_node (rn); | |
2790 | } | |
2791 | ||
2792 | /* Add static route into static route configuration. */ | |
2793 | int | |
ba779241 | 2794 | static_add_ipv4 (struct prefix *p, struct in_addr *gate, unsigned int ifindex, |
78104b9b | 2795 | u_char flags, u_short tag, u_char distance, vrf_id_t vrf_id) |
718e3744 | 2796 | { |
2797 | u_char type = 0; | |
2798 | struct route_node *rn; | |
c0551cbb DS |
2799 | struct static_route *si; |
2800 | struct static_route *pp; | |
2801 | struct static_route *cp; | |
2802 | struct static_route *update = NULL; | |
78104b9b FL |
2803 | struct zebra_vrf *zvrf = vrf_info_get (vrf_id); |
2804 | struct route_table *stable = zvrf->stable[AFI_IP][SAFI_UNICAST]; | |
718e3744 | 2805 | |
718e3744 | 2806 | if (! stable) |
2807 | return -1; | |
2808 | ||
2809 | /* Lookup static route prefix. */ | |
2810 | rn = route_node_get (stable, p); | |
2811 | ||
2812 | /* Make flags. */ | |
2813 | if (gate) | |
2814 | type = STATIC_IPV4_GATEWAY; | |
ba779241 DS |
2815 | else if (ifindex) |
2816 | type = STATIC_IFINDEX; | |
595db7f1 | 2817 | else |
2818 | type = STATIC_IPV4_BLACKHOLE; | |
718e3744 | 2819 | |
2820 | /* Do nothing if there is a same static route. */ | |
2821 | for (si = rn->info; si; si = si->next) | |
2822 | { | |
2823 | if (type == si->type | |
c0551cbb | 2824 | && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4)) |
ba779241 | 2825 | && (! ifindex || ifindex == si->ifindex)) |
718e3744 | 2826 | { |
0d9551dc | 2827 | if ((distance == si->distance) && (tag == si->tag)) |
718e3744 | 2828 | { |
2829 | route_unlock_node (rn); | |
2830 | return 0; | |
2831 | } | |
2832 | else | |
2833 | update = si; | |
2834 | } | |
2835 | } | |
2836 | ||
0d9551dc | 2837 | /* Distance or tag changed. */ |
718e3744 | 2838 | if (update) |
ba779241 | 2839 | static_delete_ipv4 (p, gate, ifindex, update->tag, update->distance, vrf_id); |
718e3744 | 2840 | |
2841 | /* Make new static route structure. */ | |
c0551cbb | 2842 | si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route)); |
718e3744 | 2843 | |
2844 | si->type = type; | |
2845 | si->distance = distance; | |
81dfcaa2 | 2846 | si->flags = flags; |
0d9551dc | 2847 | si->tag = tag; |
8f527c5e | 2848 | si->vrf_id = vrf_id; |
ba779241 | 2849 | si->ifindex = ifindex; |
718e3744 | 2850 | |
2851 | if (gate) | |
c0551cbb | 2852 | si->addr.ipv4 = *gate; |
718e3744 | 2853 | |
2854 | /* Add new static route information to the tree with sort by | |
2855 | distance value and gateway address. */ | |
2856 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) | |
2857 | { | |
2858 | if (si->distance < cp->distance) | |
2859 | break; | |
2860 | if (si->distance > cp->distance) | |
2861 | continue; | |
2862 | if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY) | |
2863 | { | |
c0551cbb | 2864 | if (ntohl (si->addr.ipv4.s_addr) < ntohl (cp->addr.ipv4.s_addr)) |
718e3744 | 2865 | break; |
c0551cbb | 2866 | if (ntohl (si->addr.ipv4.s_addr) > ntohl (cp->addr.ipv4.s_addr)) |
718e3744 | 2867 | continue; |
2868 | } | |
2869 | } | |
2870 | ||
2871 | /* Make linked list. */ | |
2872 | if (pp) | |
2873 | pp->next = si; | |
2874 | else | |
2875 | rn->info = si; | |
2876 | if (cp) | |
2877 | cp->prev = si; | |
2878 | si->prev = pp; | |
2879 | si->next = cp; | |
2880 | ||
2881 | /* Install into rib. */ | |
bcd548ff | 2882 | static_install_route (AFI_IP, SAFI_UNICAST, p, si); |
718e3744 | 2883 | |
2884 | return 1; | |
2885 | } | |
2886 | ||
2887 | /* Delete static route from static route configuration. */ | |
2888 | int | |
ba779241 | 2889 | static_delete_ipv4 (struct prefix *p, struct in_addr *gate, unsigned int ifindex, |
78104b9b | 2890 | u_short tag, u_char distance, vrf_id_t vrf_id) |
718e3744 | 2891 | { |
2892 | u_char type = 0; | |
2893 | struct route_node *rn; | |
c0551cbb | 2894 | struct static_route *si; |
718e3744 | 2895 | struct route_table *stable; |
2896 | ||
2897 | /* Lookup table. */ | |
b72ede27 | 2898 | stable = zebra_vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id); |
718e3744 | 2899 | if (! stable) |
2900 | return -1; | |
2901 | ||
2902 | /* Lookup static route prefix. */ | |
2903 | rn = route_node_lookup (stable, p); | |
2904 | if (! rn) | |
2905 | return 0; | |
2906 | ||
2907 | /* Make flags. */ | |
2908 | if (gate) | |
2909 | type = STATIC_IPV4_GATEWAY; | |
ba779241 DS |
2910 | else if (ifindex) |
2911 | type = STATIC_IFINDEX; | |
595db7f1 | 2912 | else |
2913 | type = STATIC_IPV4_BLACKHOLE; | |
718e3744 | 2914 | |
2915 | /* Find same static route is the tree */ | |
2916 | for (si = rn->info; si; si = si->next) | |
2917 | if (type == si->type | |
c0551cbb | 2918 | && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4)) |
ba779241 | 2919 | && (! ifindex || ifindex == si->ifindex) |
0d9551dc | 2920 | && (! tag || (tag == si->tag))) |
718e3744 | 2921 | break; |
2922 | ||
2923 | /* Can't find static route. */ | |
2924 | if (! si) | |
2925 | { | |
2926 | route_unlock_node (rn); | |
2927 | return 0; | |
2928 | } | |
2929 | ||
2930 | /* Install into rib. */ | |
b8a1effa | 2931 | static_uninstall_route (AFI_IP, SAFI_UNICAST, p, si); |
718e3744 | 2932 | |
2933 | /* Unlink static route from linked list. */ | |
2934 | if (si->prev) | |
2935 | si->prev->next = si->next; | |
2936 | else | |
2937 | rn->info = si->next; | |
2938 | if (si->next) | |
2939 | si->next->prev = si->prev; | |
143a385f | 2940 | route_unlock_node (rn); |
718e3744 | 2941 | |
2942 | /* Free static route configuration. */ | |
c0551cbb | 2943 | XFREE (MTYPE_STATIC_ROUTE, si); |
718e3744 | 2944 | |
143a385f | 2945 | route_unlock_node (rn); |
2946 | ||
718e3744 | 2947 | return 1; |
2948 | } | |
2949 | ||
6b0655a2 | 2950 | |
718e3744 | 2951 | int |
7c8ff89e | 2952 | rib_add_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p, |
78104b9b FL |
2953 | struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id, |
2954 | u_int32_t table_id, u_int32_t metric, u_char distance, safi_t safi) | |
718e3744 | 2955 | { |
2956 | struct rib *rib; | |
2957 | struct rib *same = NULL; | |
2958 | struct route_table *table; | |
2959 | struct route_node *rn; | |
2960 | struct nexthop *nexthop; | |
2961 | ||
718e3744 | 2962 | /* Lookup table. */ |
ca46a78e | 2963 | table = zebra_vrf_table_with_table_id (AFI_IP6, safi, vrf_id, table_id); |
718e3744 | 2964 | if (! table) |
2965 | return 0; | |
2966 | ||
2967 | /* Make sure mask is applied. */ | |
2968 | apply_mask_ipv6 (p); | |
2969 | ||
2970 | /* Set default distance by route type. */ | |
be61c4eb | 2971 | if (!distance) |
2972 | distance = route_info[type].distance; | |
718e3744 | 2973 | |
2974 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) | |
2975 | distance = 200; | |
2976 | ||
718e3744 | 2977 | /* Lookup route node.*/ |
2978 | rn = route_node_get (table, (struct prefix *) p); | |
2979 | ||
2980 | /* If same type of route are installed, treat it as a implicit | |
2981 | withdraw. */ | |
9fd92e3c | 2982 | RNODE_FOREACH_RIB (rn, rib) |
718e3744 | 2983 | { |
6d691129 PJ |
2984 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
2985 | continue; | |
2986 | ||
ebf1ead0 | 2987 | if (rib->type != type) |
2988 | continue; | |
7c8ff89e DS |
2989 | if (rib->instance != instance) |
2990 | continue; | |
ebf1ead0 | 2991 | if (rib->type != ZEBRA_ROUTE_CONNECT) |
718e3744 | 2992 | { |
2993 | same = rib; | |
718e3744 | 2994 | break; |
2995 | } | |
ebf1ead0 | 2996 | else if ((nexthop = rib->nexthop) && |
2997 | nexthop->type == NEXTHOP_TYPE_IFINDEX && | |
2998 | nexthop->ifindex == ifindex) | |
2999 | { | |
3000 | rib->refcnt++; | |
3001 | return 0; | |
3002 | } | |
718e3744 | 3003 | } |
3004 | ||
3005 | /* Allocate new rib structure. */ | |
4d38fdb4 | 3006 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
3007 | ||
718e3744 | 3008 | rib->type = type; |
7c8ff89e | 3009 | rib->instance = instance; |
718e3744 | 3010 | rib->distance = distance; |
3011 | rib->flags = flags; | |
3012 | rib->metric = metric; | |
7a4bb9c5 | 3013 | rib->table = table_id; |
78104b9b | 3014 | rib->vrf_id = vrf_id; |
718e3744 | 3015 | rib->nexthop_num = 0; |
3016 | rib->uptime = time (NULL); | |
3017 | ||
3018 | /* Nexthop settings. */ | |
3019 | if (gate) | |
3020 | { | |
3021 | if (ifindex) | |
a399694f | 3022 | rib_nexthop_ipv6_ifindex_add (rib, gate, ifindex); |
718e3744 | 3023 | else |
a399694f | 3024 | rib_nexthop_ipv6_add (rib, gate); |
718e3744 | 3025 | } |
3026 | else | |
a399694f | 3027 | rib_nexthop_ifindex_add (rib, ifindex); |
718e3744 | 3028 | |
3029 | /* If this route is kernel route, set FIB flag to the route. */ | |
3030 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) | |
3031 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) | |
3032 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
3033 | ||
3034 | /* Link new rib to node.*/ | |
fed643f4 | 3035 | if (IS_ZEBRA_DEBUG_RIB) |
41ec9222 | 3036 | { |
3037 | char buf[INET6_ADDRSTRLEN]; | |
3038 | if (IS_ZEBRA_DEBUG_RIB) | |
3039 | { | |
3040 | inet_ntop (p->family, &p->prefix, buf, INET6_ADDRSTRLEN); | |
3041 | zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) " | |
3042 | "existing %p", | |
3043 | vrf_id, buf, p->prefixlen, rn, rib, rib->type, same); | |
3044 | } | |
3045 | ||
3046 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3047 | rib_dump ((struct prefix *)p, rib); | |
3048 | } | |
2bf26d41 | 3049 | rib_addnode (rn, rib, 1); |
718e3744 | 3050 | |
718e3744 | 3051 | /* Free implicit route.*/ |
3052 | if (same) | |
4d38fdb4 | 3053 | rib_delnode (rn, same); |
3054 | ||
3055 | route_unlock_node (rn); | |
718e3744 | 3056 | return 0; |
3057 | } | |
3058 | ||
41fc2714 | 3059 | int |
8a92a8a0 | 3060 | rib_add_ipv6_multipath (struct prefix *p, struct rib *rib, safi_t safi, |
41fc2714 DS |
3061 | unsigned long ifindex) |
3062 | { | |
3063 | struct route_table *table; | |
3064 | struct route_node *rn; | |
3065 | struct rib *same = NULL; | |
3066 | struct nexthop *nexthop; | |
3067 | int ret = 0; | |
154caaed | 3068 | int family; |
3069 | ||
3070 | if (!rib) | |
3071 | return 0; | |
41fc2714 | 3072 | |
8a92a8a0 | 3073 | if (p->family == AF_INET) |
154caaed | 3074 | family = AFI_IP; |
3075 | else | |
3076 | family = AFI_IP6; | |
8a92a8a0 | 3077 | |
154caaed | 3078 | /* Lookup table. */ |
3079 | table = zebra_vrf_table_with_table_id (family, safi, rib->vrf_id, rib->table); | |
3080 | if (! table) | |
3081 | return 0; | |
3082 | ||
3083 | if (p->family == AF_INET) | |
3084 | { | |
8a92a8a0 | 3085 | /* Make it sure prefixlen is applied to the prefix. */ |
88177fe3 | 3086 | apply_mask_ipv4 ((struct prefix_ipv4 *)p); |
7a4bb9c5 DS |
3087 | } |
3088 | else | |
3089 | { | |
8a92a8a0 | 3090 | /* Make sure mask is applied. */ |
88177fe3 | 3091 | apply_mask_ipv6 ((struct prefix_ipv6 *)p); |
8a92a8a0 | 3092 | } |
41fc2714 DS |
3093 | |
3094 | /* Set default distance by route type. */ | |
3095 | if (rib->distance == 0) | |
3096 | { | |
3097 | rib->distance = route_info[rib->type].distance; | |
3098 | ||
3099 | /* iBGP distance is 200. */ | |
3100 | if (rib->type == ZEBRA_ROUTE_BGP | |
3101 | && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP)) | |
3102 | rib->distance = 200; | |
3103 | } | |
3104 | ||
3105 | /* Lookup route node.*/ | |
3106 | rn = route_node_get (table, (struct prefix *) p); | |
3107 | ||
3108 | /* If same type of route are installed, treat it as a implicit | |
3109 | withdraw. */ | |
3110 | RNODE_FOREACH_RIB (rn, same) { | |
3111 | if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED)) { | |
3112 | continue; | |
3113 | } | |
3114 | if (same->type != rib->type) { | |
3115 | continue; | |
3116 | } | |
3117 | ||
7c8ff89e DS |
3118 | if (same->instance != rib->instance) { |
3119 | continue; | |
3120 | } | |
3121 | ||
41fc2714 DS |
3122 | if (same->table != rib->table) { |
3123 | continue; | |
3124 | } | |
3125 | if (same->type != ZEBRA_ROUTE_CONNECT) { | |
3126 | break; | |
3127 | } | |
3128 | else if ((nexthop = same->nexthop) && | |
3129 | nexthop->type == NEXTHOP_TYPE_IFINDEX && | |
3130 | nexthop->ifindex == ifindex) { | |
3131 | same->refcnt++; | |
3132 | return 0; | |
3133 | } | |
3134 | } | |
3135 | ||
3136 | /* If this route is kernel route, set FIB flag to the route. */ | |
3137 | if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) { | |
3138 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) { | |
3139 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
3140 | } | |
3141 | } | |
3142 | ||
3143 | /* Link new rib to node.*/ | |
41ec9222 | 3144 | if (IS_ZEBRA_DEBUG_RIB) |
3145 | { | |
3146 | char buf[INET6_ADDRSTRLEN]; | |
3147 | if (IS_ZEBRA_DEBUG_RIB) | |
3148 | { | |
3149 | inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN); | |
3150 | zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) " | |
3151 | "existing %p", | |
3152 | rib->vrf_id, buf, p->prefixlen, rn, rib, rib->type, same); | |
3153 | } | |
3154 | ||
3155 | if (IS_ZEBRA_DEBUG_RIB_DETAILED) | |
3156 | rib_dump ((struct prefix *)p, rib); | |
3157 | } | |
2bf26d41 | 3158 | rib_addnode (rn, rib, 1); |
41fc2714 | 3159 | ret = 1; |
41ec9222 | 3160 | |
41fc2714 DS |
3161 | /* Free implicit route.*/ |
3162 | if (same) | |
41fc2714 | 3163 | { |
41ec9222 | 3164 | rib_delnode (rn, same); |
3165 | ret = -1; | |
41fc2714 | 3166 | } |
41fc2714 DS |
3167 | |
3168 | route_unlock_node (rn); | |
3169 | return ret; | |
3170 | } | |
3171 | ||
ebf1ead0 | 3172 | /* XXX factor with rib_delete_ipv6 */ |
12f6fb97 | 3173 | |
718e3744 | 3174 | int |
7c8ff89e | 3175 | rib_delete_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p, |
78104b9b FL |
3176 | struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id, |
3177 | u_int32_t table_id, safi_t safi) | |
718e3744 | 3178 | { |
3179 | struct route_table *table; | |
3180 | struct route_node *rn; | |
3181 | struct rib *rib; | |
3182 | struct rib *fib = NULL; | |
3183 | struct rib *same = NULL; | |
fa713d9e CF |
3184 | struct nexthop *nexthop, *tnexthop; |
3185 | int recursing; | |
4690c7d7 | 3186 | char buf1[PREFIX2STR_BUFFER]; |
81cce018 | 3187 | char buf2[INET6_ADDRSTRLEN]; |
718e3744 | 3188 | |
3189 | /* Apply mask. */ | |
3190 | apply_mask_ipv6 (p); | |
3191 | ||
3192 | /* Lookup table. */ | |
ca46a78e | 3193 | table = zebra_vrf_table_with_table_id (AFI_IP6, safi, vrf_id, table_id); |
718e3744 | 3194 | if (! table) |
3195 | return 0; | |
4d38fdb4 | 3196 | |
718e3744 | 3197 | /* Lookup route node. */ |
3198 | rn = route_node_lookup (table, (struct prefix *) p); | |
3199 | if (! rn) | |
3200 | { | |
41ec9222 | 3201 | if (IS_ZEBRA_DEBUG_RIB) |
3202 | zlog_debug ("%u:%s/%d: doesn't exist in rib", | |
3203 | vrf_id, inet_ntop (p->family, &p->prefix, buf1, INET6_ADDRSTRLEN), | |
3204 | p->prefixlen); | |
718e3744 | 3205 | return ZEBRA_ERR_RTNOEXIST; |
3206 | } | |
3207 | ||
3208 | /* Lookup same type route. */ | |
9fd92e3c | 3209 | RNODE_FOREACH_RIB (rn, rib) |
718e3744 | 3210 | { |
6d691129 PJ |
3211 | if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED)) |
3212 | continue; | |
3213 | ||
718e3744 | 3214 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
3215 | fib = rib; | |
3216 | ||
ebf1ead0 | 3217 | if (rib->type != type) |
3218 | continue; | |
7c8ff89e DS |
3219 | if (rib->instance != instance) |
3220 | continue; | |
ebf1ead0 | 3221 | if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) && |
4f1735fd | 3222 | nexthop->type == NEXTHOP_TYPE_IFINDEX) |
718e3744 | 3223 | { |
4f1735fd MF |
3224 | if (nexthop->ifindex != ifindex) |
3225 | continue; | |
ebf1ead0 | 3226 | if (rib->refcnt) |
718e3744 | 3227 | { |
ebf1ead0 | 3228 | rib->refcnt--; |
3229 | route_unlock_node (rn); | |
3230 | route_unlock_node (rn); | |
3231 | return 0; | |
718e3744 | 3232 | } |
ebf1ead0 | 3233 | same = rib; |
3234 | break; | |
718e3744 | 3235 | } |
ebf1ead0 | 3236 | /* Make sure that the route found has the same gateway. */ |
fa713d9e CF |
3237 | else |
3238 | { | |
3239 | if (gate == NULL) | |
3240 | { | |
3241 | same = rib; | |
3242 | break; | |
3243 | } | |
3244 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) | |
3245 | if (IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate)) | |
3246 | { | |
3247 | same = rib; | |
3248 | break; | |
3249 | } | |
3250 | if (same) | |
3251 | break; | |
3252 | } | |
718e3744 | 3253 | } |
3254 | ||
3255 | /* If same type of route can't be found and this message is from | |
3256 | kernel. */ | |
3257 | if (! same) | |
3258 | { | |
2037f143 DS |
3259 | if (fib && type == ZEBRA_ROUTE_KERNEL && |
3260 | CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE)) | |
3261 | { | |
41ec9222 | 3262 | if (IS_ZEBRA_DEBUG_RIB) |
3263 | zlog_debug ("%u:%s/%d: rn %p, rib %p (type %d) was deleted " | |
3264 | "from kernel, adding", | |
3265 | vrf_id, inet_ntop (p->family, &p->prefix, buf1, INET6_ADDRSTRLEN), | |
3266 | p->prefixlen, rn, fib, fib->type); | |
6baf7bb8 DS |
3267 | if (allow_delete) |
3268 | { | |
3269 | /* Unset flags. */ | |
3270 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) | |
3271 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); | |
3272 | ||
3273 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); | |
3274 | } | |
3275 | else | |
3276 | { | |
3277 | /* This means someone else, other than Zebra, has deleted a Zebra | |
3278 | * route from the kernel. We will add it back */ | |
3279 | rib_install_kernel(rn, fib, 0); | |
3280 | } | |
2037f143 | 3281 | } |
718e3744 | 3282 | else |
3283 | { | |
3284 | if (IS_ZEBRA_DEBUG_KERNEL) | |
3285 | { | |
3286 | if (gate) | |
41ec9222 | 3287 | zlog_debug ("%s: vrf %u via %s ifindex %d type %d " |
78104b9b FL |
3288 | "doesn't exist in rib", |
3289 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, | |
81cce018 | 3290 | inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN), |
718e3744 | 3291 | ifindex, |
3292 | type); | |
3293 | else | |
41ec9222 | 3294 | zlog_debug ("%s: vrf %u ifindex %d type %d doesn't exist in rib", |
78104b9b | 3295 | prefix2str (p, buf1, sizeof(buf1)), vrf_id, |
718e3744 | 3296 | ifindex, |
3297 | type); | |
3298 | } | |
3299 | route_unlock_node (rn); | |
3300 | return ZEBRA_ERR_RTNOEXIST; | |
3301 | } | |
3302 | } | |
3303 | ||
718e3744 | 3304 | if (same) |
4d38fdb4 | 3305 | rib_delnode (rn, same); |
3306 | ||
718e3744 | 3307 | route_unlock_node (rn); |
718e3744 | 3308 | return 0; |
3309 | } | |
6b0655a2 | 3310 | |
718e3744 | 3311 | /* Add static route into static route configuration. */ |
3312 | int | |
3313 | static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, | |
ba779241 | 3314 | unsigned int ifindex, u_char flags, u_short tag, |
78104b9b | 3315 | u_char distance, vrf_id_t vrf_id) |
718e3744 | 3316 | { |
3317 | struct route_node *rn; | |
c0551cbb DS |
3318 | struct static_route *si; |
3319 | struct static_route *pp; | |
3320 | struct static_route *cp; | |
526e1728 | 3321 | struct static_route *update = NULL; |
78104b9b FL |
3322 | struct zebra_vrf *zvrf = vrf_info_get (vrf_id); |
3323 | struct route_table *stable = zvrf->stable[AFI_IP6][SAFI_UNICAST]; | |
718e3744 | 3324 | |
718e3744 | 3325 | if (! stable) |
3326 | return -1; | |
27b47253 PJ |
3327 | |
3328 | if (!gate && | |
ba779241 | 3329 | (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFINDEX)) |
27b47253 PJ |
3330 | return -1; |
3331 | ||
ba779241 DS |
3332 | if (!ifindex && |
3333 | (type == STATIC_IPV6_GATEWAY_IFINDEX || type == STATIC_IFINDEX)) | |
27b47253 | 3334 | return -1; |
718e3744 | 3335 | |
3336 | /* Lookup static route prefix. */ | |
3337 | rn = route_node_get (stable, p); | |
3338 | ||
3339 | /* Do nothing if there is a same static route. */ | |
3340 | for (si = rn->info; si; si = si->next) | |
3341 | { | |
526e1728 | 3342 | if (type == si->type |
c0551cbb | 3343 | && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6)) |
ba779241 | 3344 | && (! ifindex || ifindex == si->ifindex)) |
718e3744 | 3345 | { |
526e1728 DS |
3346 | if ((distance == si->distance) && (tag == si->tag)) |
3347 | { | |
3348 | route_unlock_node (rn); | |
3349 | return 0; | |
3350 | } | |
3351 | else | |
3352 | update = si; | |
718e3744 | 3353 | } |
3354 | } | |
3355 | ||
526e1728 DS |
3356 | /* Distance or tag changed. */ |
3357 | if (update) | |
ba779241 | 3358 | static_delete_ipv6 (p, type, gate, ifindex, update->tag, update->distance, vrf_id); |
526e1728 | 3359 | |
718e3744 | 3360 | /* Make new static route structure. */ |
c0551cbb | 3361 | si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route)); |
718e3744 | 3362 | |
3363 | si->type = type; | |
3364 | si->distance = distance; | |
81dfcaa2 | 3365 | si->flags = flags; |
0d9551dc | 3366 | si->tag = tag; |
8f527c5e | 3367 | si->vrf_id = vrf_id; |
ba779241 | 3368 | si->ifindex = ifindex; |
718e3744 | 3369 | |
3370 | switch (type) | |
3371 | { | |
3372 | case STATIC_IPV6_GATEWAY: | |
c0551cbb | 3373 | si->addr.ipv6 = *gate; |
718e3744 | 3374 | break; |
ba779241 | 3375 | case STATIC_IPV6_GATEWAY_IFINDEX: |
c0551cbb | 3376 | si->addr.ipv6 = *gate; |
718e3744 | 3377 | break; |
3378 | } | |
3379 | ||
3380 | /* Add new static route information to the tree with sort by | |
3381 | distance value and gateway address. */ | |
3382 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) | |
3383 | { | |
3384 | if (si->distance < cp->distance) | |
3385 | break; | |
3386 | if (si->distance > cp->distance) | |
3387 | continue; | |
3388 | } | |
3389 | ||
3390 | /* Make linked list. */ | |
3391 | if (pp) | |
3392 | pp->next = si; | |
3393 | else | |
3394 | rn->info = si; | |
3395 | if (cp) | |
3396 | cp->prev = si; | |
3397 | si->prev = pp; | |
3398 | si->next = cp; | |
3399 | ||
3400 | /* Install into rib. */ | |
bcd548ff | 3401 | static_install_route (AFI_IP6, SAFI_UNICAST, p, si); |
718e3744 | 3402 | |
3403 | return 1; | |
3404 | } | |
3405 | ||
3406 | /* Delete static route from static route configuration. */ | |
3407 | int | |
3408 | static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, | |
ba779241 | 3409 | unsigned int ifindex, u_short tag, u_char distance, |
78104b9b | 3410 | vrf_id_t vrf_id) |
718e3744 | 3411 | { |
3412 | struct route_node *rn; | |
c0551cbb | 3413 | struct static_route *si; |
718e3744 | 3414 | struct route_table *stable; |
3415 | ||
3416 | /* Lookup table. */ | |
b72ede27 | 3417 | stable = zebra_vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
718e3744 | 3418 | if (! stable) |
3419 | return -1; | |
3420 | ||
3421 | /* Lookup static route prefix. */ | |
3422 | rn = route_node_lookup (stable, p); | |
3423 | if (! rn) | |
3424 | return 0; | |
3425 | ||
3426 | /* Find same static route is the tree */ | |
3427 | for (si = rn->info; si; si = si->next) | |
3428 | if (distance == si->distance | |
3429 | && type == si->type | |
c0551cbb | 3430 | && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6)) |
ba779241 | 3431 | && (! ifindex || ifindex == si->ifindex) |
0d9551dc | 3432 | && (! tag || (tag == si->tag))) |
718e3744 | 3433 | break; |
3434 | ||
3435 | /* Can't find static route. */ | |
3436 | if (! si) | |
3437 | { | |
3438 | route_unlock_node (rn); | |
3439 | return 0; | |
3440 | } | |
3441 | ||
3442 | /* Install into rib. */ | |
b8a1effa | 3443 | static_uninstall_route (AFI_IP6, SAFI_UNICAST, p, si); |
718e3744 | 3444 | |
3445 | /* Unlink static route from linked list. */ | |
3446 | if (si->prev) | |
3447 | si->prev->next = si->next; | |
3448 | else | |
3449 | rn->info = si->next; | |
3450 | if (si->next) | |
3451 | si->next->prev = si->prev; | |
3452 | ||
3453 | /* Free static route configuration. */ | |
c0551cbb | 3454 | XFREE (MTYPE_STATIC_ROUTE, si); |
718e3744 | 3455 | |
3456 | return 1; | |
3457 | } | |
6b0655a2 | 3458 | |
1c848137 | 3459 | /* Schedule routes of a particular table (address-family) based on event. */ |
3460 | static void | |
3461 | rib_update_table (struct route_table *table, rib_update_event_t event) | |
b84c7253 | 3462 | { |
3463 | struct route_node *rn; | |
b84c7253 | 3464 | struct rib *rib, *next; |
3465 | ||
1c848137 | 3466 | /* Walk all routes and queue for processing, if appropriate for |
3467 | * the trigger event. | |
3468 | */ | |
3469 | for (rn = route_top (table); rn; rn = route_next (rn)) | |
3470 | { | |
3471 | switch (event) | |
3472 | { | |
3473 | case RIB_UPDATE_IF_CHANGE: | |
3474 | /* Examine all routes that won't get processed by the protocol or | |
3475 | * triggered by nexthop evaluation (NHT). This would be system, | |
3476 | * kernel and certain static routes. Note that NHT will get | |
3477 | * triggered upon an interface event as connected routes always | |
3478 | * get queued for processing. | |
3479 | */ | |
3480 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) | |
3481 | { | |
3482 | if (rib->type == ZEBRA_ROUTE_OSPF || | |
3483 | rib->type == ZEBRA_ROUTE_OSPF6 || | |
3484 | rib->type == ZEBRA_ROUTE_BGP) | |
3485 | continue; /* protocol will handle. */ | |
3486 | else if (rib->type == ZEBRA_ROUTE_STATIC) | |
3487 | { | |
3488 | struct nexthop *nh; | |
3489 | for (nh = rib->nexthop; nh; nh = nh->next) | |
3490 | if (!(nh->type == NEXTHOP_TYPE_IPV4 || | |
3491 | nh->type == NEXTHOP_TYPE_IPV6)) | |
3492 | break; | |
3493 | ||
3494 | /* If we only have nexthops to a gateway, NHT will | |
3495 | * take care. | |
3496 | */ | |
3497 | if (nh) | |
44e9909d | 3498 | rib_queue_add (rn); |
1c848137 | 3499 | } |
3500 | else | |
44e9909d | 3501 | rib_queue_add (rn); |
1c848137 | 3502 | } |
3503 | break; | |
b84c7253 | 3504 | |
1c848137 | 3505 | case RIB_UPDATE_RMAP_CHANGE: |
3506 | case RIB_UPDATE_OTHER: | |
3507 | /* Right now, examine all routes. Can restrict to a protocol in | |
3508 | * some cases (TODO). | |
3509 | */ | |
3510 | if (rnode_to_ribs (rn)) | |
44e9909d | 3511 | rib_queue_add (rn); |
1c848137 | 3512 | break; |
3513 | ||
3514 | default: | |
3515 | break; | |
3516 | } | |
3517 | } | |
b84c7253 | 3518 | } |
3519 | ||
718e3744 | 3520 | /* RIB update function. */ |
3521 | void | |
1c848137 | 3522 | rib_update (vrf_id_t vrf_id, rib_update_event_t event) |
718e3744 | 3523 | { |
718e3744 | 3524 | struct route_table *table; |
1c848137 | 3525 | |
3526 | /* Process routes of interested address-families. */ | |
78104b9b | 3527 | table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id); |
718e3744 | 3528 | if (table) |
1c848137 | 3529 | rib_update_table (table, event); |
718e3744 | 3530 | |
78104b9b | 3531 | table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
718e3744 | 3532 | if (table) |
1c848137 | 3533 | rib_update_table (table, event); |
718e3744 | 3534 | } |
3535 | ||
718e3744 | 3536 | /* Remove all routes which comes from non main table. */ |
a1ac18c4 | 3537 | static void |
718e3744 | 3538 | rib_weed_table (struct route_table *table) |
3539 | { | |
3540 | struct route_node *rn; | |
3541 | struct rib *rib; | |
3542 | struct rib *next; | |
3543 | ||
3544 | if (table) | |
3545 | for (rn = route_top (table); rn; rn = route_next (rn)) | |
9fd92e3c | 3546 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
718e3744 | 3547 | { |
6d691129 PJ |
3548 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
3549 | continue; | |
3550 | ||
b21b19c5 | 3551 | if (rib->table != zebrad.rtm_table_default && |
718e3744 | 3552 | rib->table != RT_TABLE_MAIN) |
4d38fdb4 | 3553 | rib_delnode (rn, rib); |
718e3744 | 3554 | } |
3555 | } | |
3556 | ||
3557 | /* Delete all routes from non main table. */ | |
3558 | void | |
a1ac18c4 | 3559 | rib_weed_tables (void) |
718e3744 | 3560 | { |
78104b9b FL |
3561 | vrf_iter_t iter; |
3562 | struct zebra_vrf *zvrf; | |
3563 | ||
3564 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) | |
3565 | if ((zvrf = vrf_iter2info (iter)) != NULL) | |
3566 | { | |
3567 | rib_weed_table (zvrf->table[AFI_IP][SAFI_UNICAST]); | |
3568 | rib_weed_table (zvrf->table[AFI_IP6][SAFI_UNICAST]); | |
3569 | } | |
718e3744 | 3570 | } |
6b0655a2 | 3571 | |
718e3744 | 3572 | /* Delete self installed routes after zebra is relaunched. */ |
a1ac18c4 | 3573 | static void |
718e3744 | 3574 | rib_sweep_table (struct route_table *table) |
3575 | { | |
3576 | struct route_node *rn; | |
3577 | struct rib *rib; | |
3578 | struct rib *next; | |
3579 | int ret = 0; | |
3580 | ||
3581 | if (table) | |
3582 | for (rn = route_top (table); rn; rn = route_next (rn)) | |
9fd92e3c | 3583 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
718e3744 | 3584 | { |
6d691129 PJ |
3585 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
3586 | continue; | |
3587 | ||
718e3744 | 3588 | if (rib->type == ZEBRA_ROUTE_KERNEL && |
3589 | CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE)) | |
3590 | { | |
3591 | ret = rib_uninstall_kernel (rn, rib); | |
3592 | if (! ret) | |
4d38fdb4 | 3593 | rib_delnode (rn, rib); |
718e3744 | 3594 | } |
3595 | } | |
3596 | } | |
3597 | ||
3598 | /* Sweep all RIB tables. */ | |
3599 | void | |
a1ac18c4 | 3600 | rib_sweep_route (void) |
718e3744 | 3601 | { |
78104b9b FL |
3602 | vrf_iter_t iter; |
3603 | struct zebra_vrf *zvrf; | |
3604 | ||
3605 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) | |
3606 | if ((zvrf = vrf_iter2info (iter)) != NULL) | |
3607 | { | |
3608 | rib_sweep_table (zvrf->table[AFI_IP][SAFI_UNICAST]); | |
3609 | rib_sweep_table (zvrf->table[AFI_IP6][SAFI_UNICAST]); | |
3610 | } | |
718e3744 | 3611 | } |
2ea1ab1c VT |
3612 | |
3613 | /* Remove specific by protocol routes from 'table'. */ | |
3614 | static unsigned long | |
7c8ff89e | 3615 | rib_score_proto_table (u_char proto, u_short instance, struct route_table *table) |
2ea1ab1c VT |
3616 | { |
3617 | struct route_node *rn; | |
3618 | struct rib *rib; | |
3619 | struct rib *next; | |
3620 | unsigned long n = 0; | |
3621 | ||
3622 | if (table) | |
3623 | for (rn = route_top (table); rn; rn = route_next (rn)) | |
9fd92e3c | 3624 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
2ea1ab1c | 3625 | { |
2ea1ab1c VT |
3626 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
3627 | continue; | |
7c8ff89e | 3628 | if (rib->type == proto && rib->instance == instance) |
2ea1ab1c VT |
3629 | { |
3630 | rib_delnode (rn, rib); | |
3631 | n++; | |
3632 | } | |
3633 | } | |
3634 | ||
3635 | return n; | |
3636 | } | |
3637 | ||
3638 | /* Remove specific by protocol routes. */ | |
3639 | unsigned long | |
7c8ff89e | 3640 | rib_score_proto (u_char proto, u_short instance) |
2ea1ab1c | 3641 | { |
78104b9b FL |
3642 | vrf_iter_t iter; |
3643 | struct zebra_vrf *zvrf; | |
3644 | unsigned long cnt = 0; | |
3645 | ||
3646 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) | |
3647 | if ((zvrf = vrf_iter2info (iter)) != NULL) | |
3648 | cnt += rib_score_proto_table (proto, instance, zvrf->table[AFI_IP][SAFI_UNICAST]) | |
3649 | +rib_score_proto_table (proto, instance, zvrf->table[AFI_IP6][SAFI_UNICAST]); | |
3650 | ||
3651 | return cnt; | |
2ea1ab1c VT |
3652 | } |
3653 | ||
718e3744 | 3654 | /* Close RIB and clean up kernel routes. */ |
a31c5886 | 3655 | void |
718e3744 | 3656 | rib_close_table (struct route_table *table) |
3657 | { | |
3658 | struct route_node *rn; | |
3659 | struct rib *rib; | |
3660 | ||
3661 | if (table) | |
3662 | for (rn = route_top (table); rn; rn = route_next (rn)) | |
9fd92e3c | 3663 | RNODE_FOREACH_RIB (rn, rib) |
6d691129 | 3664 | { |
9fd92e3c AS |
3665 | if (!CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
3666 | continue; | |
3667 | ||
5adc2528 AS |
3668 | zfpm_trigger_update (rn, NULL); |
3669 | ||
9fd92e3c AS |
3670 | if (! RIB_SYSTEM_ROUTE (rib)) |
3671 | rib_uninstall_kernel (rn, rib); | |
6d691129 | 3672 | } |
718e3744 | 3673 | } |
3674 | ||
3675 | /* Close all RIB tables. */ | |
3676 | void | |
a1ac18c4 | 3677 | rib_close (void) |
718e3744 | 3678 | { |
78104b9b FL |
3679 | vrf_iter_t iter; |
3680 | struct zebra_vrf *zvrf; | |
3681 | struct listnode *node; | |
5c610faf | 3682 | struct interface *ifp; |
6678277c | 3683 | u_int32_t table_id; |
5c610faf | 3684 | |
78104b9b FL |
3685 | for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter)) |
3686 | { | |
3687 | if ((zvrf = vrf_iter2info (iter)) != NULL) | |
3688 | { | |
3689 | rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]); | |
3690 | rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]); | |
3691 | } | |
3692 | for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter), node, ifp)) | |
3693 | if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp); | |
3694 | } | |
6678277c DS |
3695 | |
3696 | /* If we do multiple tables per vrf, need to move this to loop above */ | |
3697 | zvrf = vrf_info_lookup (VRF_DEFAULT); | |
3698 | ||
3699 | for (table_id = 0; table_id < ZEBRA_KERNEL_TABLE_MAX; table_id++) | |
3700 | { | |
3701 | if (zvrf->other_table[AFI_IP][table_id]) | |
3702 | rib_close_table (zvrf->other_table[AFI_IP][table_id]); | |
3703 | ||
3704 | if (zvrf->other_table[AFI_IP6][table_id]) | |
3705 | rib_close_table (zvrf->other_table[AFI_IP6][table_id]); | |
3706 | } | |
718e3744 | 3707 | } |
6b0655a2 | 3708 | |
718e3744 | 3709 | /* Routing information base initialize. */ |
3710 | void | |
a1ac18c4 | 3711 | rib_init (void) |
718e3744 | 3712 | { |
4d38fdb4 | 3713 | rib_queue_init (&zebrad); |
718e3744 | 3714 | } |
0915bb0c AS |
3715 | |
3716 | /* | |
3717 | * vrf_id_get_next | |
3718 | * | |
3719 | * Get the first vrf id that is greater than the given vrf id if any. | |
3720 | * | |
3721 | * Returns TRUE if a vrf id was found, FALSE otherwise. | |
3722 | */ | |
3723 | static inline int | |
b72ede27 | 3724 | vrf_id_get_next (vrf_id_t vrf_id, vrf_id_t *next_id_p) |
0915bb0c | 3725 | { |
b72ede27 FL |
3726 | vrf_iter_t iter = vrf_iterator (vrf_id); |
3727 | struct zebra_vrf *zvrf = vrf_iter2info (iter); | |
3728 | ||
3729 | /* The same one ? Then find out the next. */ | |
3730 | if (zvrf && (zvrf->vrf_id == vrf_id)) | |
3731 | zvrf = vrf_iter2info (vrf_next (iter)); | |
3732 | ||
3733 | if (zvrf) | |
0915bb0c | 3734 | { |
b72ede27 FL |
3735 | *next_id_p = zvrf->vrf_id; |
3736 | return 1; | |
0915bb0c AS |
3737 | } |
3738 | ||
3739 | return 0; | |
3740 | } | |
3741 | ||
3742 | /* | |
3743 | * rib_tables_iter_next | |
3744 | * | |
3745 | * Returns the next table in the iteration. | |
3746 | */ | |
3747 | struct route_table * | |
3748 | rib_tables_iter_next (rib_tables_iter_t *iter) | |
3749 | { | |
3750 | struct route_table *table; | |
3751 | ||
3752 | /* | |
3753 | * Array that helps us go over all AFI/SAFI combinations via one | |
3754 | * index. | |
3755 | */ | |
3756 | static struct { | |
3757 | afi_t afi; | |
3758 | safi_t safi; | |
3759 | } afi_safis[] = { | |
3760 | { AFI_IP, SAFI_UNICAST }, | |
3761 | { AFI_IP, SAFI_MULTICAST }, | |
3762 | { AFI_IP6, SAFI_UNICAST }, | |
3763 | { AFI_IP6, SAFI_MULTICAST }, | |
3764 | }; | |
3765 | ||
3766 | table = NULL; | |
3767 | ||
3768 | switch (iter->state) | |
3769 | { | |
3770 | ||
3771 | case RIB_TABLES_ITER_S_INIT: | |
9c2bf1cf | 3772 | iter->vrf_id = VRF_DEFAULT; |
0915bb0c AS |
3773 | iter->afi_safi_ix = -1; |
3774 | ||
3775 | /* Fall through */ | |
3776 | ||
3777 | case RIB_TABLES_ITER_S_ITERATING: | |
3778 | iter->afi_safi_ix++; | |
3779 | while (1) | |
3780 | { | |
3781 | ||
3782 | while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis)) | |
3783 | { | |
b72ede27 | 3784 | table = zebra_vrf_table (afi_safis[iter->afi_safi_ix].afi, |
0915bb0c AS |
3785 | afi_safis[iter->afi_safi_ix].safi, |
3786 | iter->vrf_id); | |
3787 | if (table) | |
3788 | break; | |
3789 | ||
3790 | iter->afi_safi_ix++; | |
3791 | } | |
3792 | ||
3793 | /* | |
3794 | * Found another table in this vrf. | |
3795 | */ | |
3796 | if (table) | |
3797 | break; | |
3798 | ||
3799 | /* | |
3800 | * Done with all tables in the current vrf, go to the next | |
3801 | * one. | |
3802 | */ | |
3803 | if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id)) | |
3804 | break; | |
3805 | ||
3806 | iter->afi_safi_ix = 0; | |
3807 | } | |
3808 | ||
3809 | break; | |
3810 | ||
3811 | case RIB_TABLES_ITER_S_DONE: | |
3812 | return NULL; | |
3813 | } | |
3814 | ||
3815 | if (table) | |
3816 | iter->state = RIB_TABLES_ITER_S_ITERATING; | |
3817 | else | |
3818 | iter->state = RIB_TABLES_ITER_S_DONE; | |
3819 | ||
3820 | return table; | |
3821 | } | |
b72ede27 | 3822 |