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