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