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