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