]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* BGP nexthop scan |
2 | Copyright (C) 2000 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 | #include <zebra.h> | |
22 | ||
23 | #include "command.h" | |
24 | #include "thread.h" | |
25 | #include "prefix.h" | |
26 | #include "zclient.h" | |
27 | #include "stream.h" | |
28 | #include "network.h" | |
29 | #include "log.h" | |
30 | #include "memory.h" | |
31 | ||
32 | #include "bgpd/bgpd.h" | |
33 | #include "bgpd/bgp_table.h" | |
34 | #include "bgpd/bgp_route.h" | |
35 | #include "bgpd/bgp_attr.h" | |
36 | #include "bgpd/bgp_nexthop.h" | |
37 | #include "bgpd/bgp_debug.h" | |
38 | #include "bgpd/bgp_damp.h" | |
39 | #include "zebra/rib.h" | |
40 | #include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */ | |
41 | ||
42 | struct bgp_nexthop_cache *zlookup_query (struct in_addr); | |
43 | #ifdef HAVE_IPV6 | |
44 | struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *); | |
45 | #endif /* HAVE_IPV6 */ | |
46 | \f | |
47 | /* Only one BGP scan thread are activated at the same time. */ | |
00d252cb | 48 | static struct thread *bgp_scan_thread = NULL; |
718e3744 | 49 | |
50 | /* BGP import thread */ | |
00d252cb | 51 | static struct thread *bgp_import_thread = NULL; |
718e3744 | 52 | |
53 | /* BGP scan interval. */ | |
00d252cb | 54 | static int bgp_scan_interval; |
718e3744 | 55 | |
56 | /* BGP import interval. */ | |
00d252cb | 57 | static int bgp_import_interval; |
718e3744 | 58 | |
59 | /* Route table for next-hop lookup cache. */ | |
00d252cb | 60 | static struct bgp_table *bgp_nexthop_cache_table[AFI_MAX]; |
61 | static struct bgp_table *cache1_table[AFI_MAX]; | |
62 | static struct bgp_table *cache2_table[AFI_MAX]; | |
718e3744 | 63 | |
64 | /* Route table for connected route. */ | |
00d252cb | 65 | static struct bgp_table *bgp_connected_table[AFI_MAX]; |
718e3744 | 66 | |
67 | /* BGP nexthop lookup query client. */ | |
228da428 | 68 | struct zclient *zlookup = NULL; |
718e3744 | 69 | \f |
70 | /* Add nexthop to the end of the list. */ | |
94f2b392 | 71 | static void |
718e3744 | 72 | bnc_nexthop_add (struct bgp_nexthop_cache *bnc, struct nexthop *nexthop) |
73 | { | |
74 | struct nexthop *last; | |
75 | ||
76 | for (last = bnc->nexthop; last && last->next; last = last->next) | |
77 | ; | |
78 | if (last) | |
79 | last->next = nexthop; | |
80 | else | |
81 | bnc->nexthop = nexthop; | |
82 | nexthop->prev = last; | |
83 | } | |
84 | ||
94f2b392 | 85 | static void |
718e3744 | 86 | bnc_nexthop_free (struct bgp_nexthop_cache *bnc) |
87 | { | |
88 | struct nexthop *nexthop; | |
89 | struct nexthop *next = NULL; | |
90 | ||
91 | for (nexthop = bnc->nexthop; nexthop; nexthop = next) | |
92 | { | |
93 | next = nexthop->next; | |
94 | XFREE (MTYPE_NEXTHOP, nexthop); | |
95 | } | |
96 | } | |
97 | ||
94f2b392 | 98 | static struct bgp_nexthop_cache * |
66e5cd87 | 99 | bnc_new (void) |
718e3744 | 100 | { |
393deb9b | 101 | return XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache)); |
718e3744 | 102 | } |
103 | ||
94f2b392 | 104 | static void |
718e3744 | 105 | bnc_free (struct bgp_nexthop_cache *bnc) |
106 | { | |
107 | bnc_nexthop_free (bnc); | |
108 | XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc); | |
109 | } | |
110 | \f | |
94f2b392 | 111 | static int |
718e3744 | 112 | bgp_nexthop_same (struct nexthop *next1, struct nexthop *next2) |
113 | { | |
114 | if (next1->type != next2->type) | |
115 | return 0; | |
116 | ||
117 | switch (next1->type) | |
118 | { | |
119 | case ZEBRA_NEXTHOP_IPV4: | |
120 | if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4)) | |
121 | return 0; | |
122 | break; | |
123 | case ZEBRA_NEXTHOP_IFINDEX: | |
124 | case ZEBRA_NEXTHOP_IFNAME: | |
125 | if (next1->ifindex != next2->ifindex) | |
126 | return 0; | |
127 | break; | |
128 | #ifdef HAVE_IPV6 | |
129 | case ZEBRA_NEXTHOP_IPV6: | |
130 | if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6)) | |
131 | return 0; | |
132 | break; | |
133 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: | |
134 | case ZEBRA_NEXTHOP_IPV6_IFNAME: | |
135 | if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6)) | |
136 | return 0; | |
137 | if (next1->ifindex != next2->ifindex) | |
138 | return 0; | |
139 | break; | |
140 | #endif /* HAVE_IPV6 */ | |
fa2b17e3 | 141 | default: |
142 | /* do nothing */ | |
143 | break; | |
718e3744 | 144 | } |
145 | return 1; | |
146 | } | |
147 | ||
94f2b392 | 148 | static int |
718e3744 | 149 | bgp_nexthop_cache_changed (struct bgp_nexthop_cache *bnc1, |
150 | struct bgp_nexthop_cache *bnc2) | |
151 | { | |
152 | int i; | |
153 | struct nexthop *next1, *next2; | |
154 | ||
155 | if (bnc1->nexthop_num != bnc2->nexthop_num) | |
156 | return 1; | |
157 | ||
158 | next1 = bnc1->nexthop; | |
159 | next2 = bnc2->nexthop; | |
160 | ||
161 | for (i = 0; i < bnc1->nexthop_num; i++) | |
162 | { | |
163 | if (! bgp_nexthop_same (next1, next2)) | |
164 | return 1; | |
165 | ||
166 | next1 = next1->next; | |
167 | next2 = next2->next; | |
168 | } | |
169 | return 0; | |
170 | } | |
171 | ||
172 | /* If nexthop exists on connected network return 1. */ | |
173 | int | |
174 | bgp_nexthop_check_ebgp (afi_t afi, struct attr *attr) | |
175 | { | |
176 | struct bgp_node *rn; | |
177 | ||
178 | /* If zebra is not enabled return */ | |
179 | if (zlookup->sock < 0) | |
180 | return 1; | |
181 | ||
182 | /* Lookup the address is onlink or not. */ | |
183 | if (afi == AFI_IP) | |
184 | { | |
5932020b | 185 | rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop); |
718e3744 | 186 | if (rn) |
187 | { | |
188 | bgp_unlock_node (rn); | |
189 | return 1; | |
190 | } | |
191 | } | |
192 | #ifdef HAVE_IPV6 | |
193 | else if (afi == AFI_IP6) | |
194 | { | |
fb982c25 | 195 | if (attr->extra->mp_nexthop_len == 32) |
718e3744 | 196 | return 1; |
fb982c25 | 197 | else if (attr->extra->mp_nexthop_len == 16) |
718e3744 | 198 | { |
fb982c25 | 199 | if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global)) |
718e3744 | 200 | return 1; |
201 | ||
5932020b | 202 | rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6], |
fb982c25 | 203 | &attr->extra->mp_nexthop_global); |
718e3744 | 204 | if (rn) |
205 | { | |
206 | bgp_unlock_node (rn); | |
207 | return 1; | |
208 | } | |
209 | } | |
210 | } | |
211 | #endif /* HAVE_IPV6 */ | |
212 | return 0; | |
213 | } | |
214 | ||
215 | #ifdef HAVE_IPV6 | |
216 | /* Check specified next-hop is reachable or not. */ | |
94f2b392 | 217 | static int |
718e3744 | 218 | bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed, |
219 | int *metricchanged) | |
220 | { | |
221 | struct bgp_node *rn; | |
222 | struct prefix p; | |
223 | struct bgp_nexthop_cache *bnc; | |
224 | struct attr *attr; | |
225 | ||
226 | /* If lookup is not enabled, return valid. */ | |
227 | if (zlookup->sock < 0) | |
228 | { | |
fb982c25 PJ |
229 | if (ri->extra) |
230 | ri->extra->igpmetric = 0; | |
718e3744 | 231 | return 1; |
232 | } | |
233 | ||
234 | /* Only check IPv6 global address only nexthop. */ | |
235 | attr = ri->attr; | |
236 | ||
fb982c25 PJ |
237 | if (attr->extra->mp_nexthop_len != 16 |
238 | || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global)) | |
718e3744 | 239 | return 1; |
240 | ||
241 | memset (&p, 0, sizeof (struct prefix)); | |
242 | p.family = AF_INET6; | |
243 | p.prefixlen = IPV6_MAX_BITLEN; | |
fb982c25 | 244 | p.u.prefix6 = attr->extra->mp_nexthop_global; |
718e3744 | 245 | |
246 | /* IBGP or ebgp-multihop */ | |
5932020b | 247 | rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p); |
718e3744 | 248 | |
249 | if (rn->info) | |
250 | { | |
251 | bnc = rn->info; | |
252 | bgp_unlock_node (rn); | |
253 | } | |
254 | else | |
255 | { | |
fb982c25 | 256 | bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global); |
718e3744 | 257 | if (bnc) |
258 | { | |
259 | struct bgp_table *old; | |
260 | struct bgp_node *oldrn; | |
261 | struct bgp_nexthop_cache *oldbnc; | |
262 | ||
263 | if (changed) | |
264 | { | |
5932020b | 265 | if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6]) |
266 | old = cache2_table[AFI_IP6]; | |
718e3744 | 267 | else |
5932020b | 268 | old = cache1_table[AFI_IP6]; |
718e3744 | 269 | |
270 | oldrn = bgp_node_lookup (old, &p); | |
271 | if (oldrn) | |
272 | { | |
273 | oldbnc = oldrn->info; | |
274 | ||
275 | bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc); | |
276 | ||
277 | if (bnc->metric != oldbnc->metric) | |
278 | bnc->metricchanged = 1; | |
279 | } | |
280 | } | |
281 | } | |
282 | else | |
283 | { | |
284 | bnc = bnc_new (); | |
285 | bnc->valid = 0; | |
286 | } | |
287 | rn->info = bnc; | |
288 | } | |
289 | ||
290 | if (changed) | |
291 | *changed = bnc->changed; | |
292 | ||
293 | if (metricchanged) | |
294 | *metricchanged = bnc->metricchanged; | |
295 | ||
fb982c25 PJ |
296 | if (bnc->valid && bnc->metric) |
297 | (bgp_info_extra_get (ri))->igpmetric = bnc->metric; | |
298 | else if (ri->extra) | |
299 | ri->extra->igpmetric = 0; | |
718e3744 | 300 | |
301 | return bnc->valid; | |
302 | } | |
303 | #endif /* HAVE_IPV6 */ | |
304 | ||
305 | /* Check specified next-hop is reachable or not. */ | |
306 | int | |
307 | bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri, | |
308 | int *changed, int *metricchanged) | |
309 | { | |
310 | struct bgp_node *rn; | |
311 | struct prefix p; | |
312 | struct bgp_nexthop_cache *bnc; | |
313 | struct in_addr addr; | |
314 | ||
315 | /* If lookup is not enabled, return valid. */ | |
316 | if (zlookup->sock < 0) | |
317 | { | |
fb982c25 PJ |
318 | if (ri->extra) |
319 | ri->extra->igpmetric = 0; | |
718e3744 | 320 | return 1; |
321 | } | |
322 | ||
323 | #ifdef HAVE_IPV6 | |
324 | if (afi == AFI_IP6) | |
325 | return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged); | |
326 | #endif /* HAVE_IPV6 */ | |
327 | ||
328 | addr = ri->attr->nexthop; | |
329 | ||
330 | memset (&p, 0, sizeof (struct prefix)); | |
331 | p.family = AF_INET; | |
332 | p.prefixlen = IPV4_MAX_BITLEN; | |
333 | p.u.prefix4 = addr; | |
334 | ||
335 | /* IBGP or ebgp-multihop */ | |
5932020b | 336 | rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p); |
718e3744 | 337 | |
338 | if (rn->info) | |
339 | { | |
340 | bnc = rn->info; | |
341 | bgp_unlock_node (rn); | |
342 | } | |
343 | else | |
344 | { | |
345 | bnc = zlookup_query (addr); | |
346 | if (bnc) | |
347 | { | |
348 | struct bgp_table *old; | |
349 | struct bgp_node *oldrn; | |
350 | struct bgp_nexthop_cache *oldbnc; | |
351 | ||
352 | if (changed) | |
353 | { | |
5932020b | 354 | if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP]) |
355 | old = cache2_table[AFI_IP]; | |
718e3744 | 356 | else |
5932020b | 357 | old = cache1_table[AFI_IP]; |
718e3744 | 358 | |
359 | oldrn = bgp_node_lookup (old, &p); | |
360 | if (oldrn) | |
361 | { | |
362 | oldbnc = oldrn->info; | |
363 | ||
364 | bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc); | |
365 | ||
366 | if (bnc->metric != oldbnc->metric) | |
367 | bnc->metricchanged = 1; | |
368 | } | |
369 | } | |
370 | } | |
371 | else | |
372 | { | |
373 | bnc = bnc_new (); | |
374 | bnc->valid = 0; | |
375 | } | |
376 | rn->info = bnc; | |
377 | } | |
378 | ||
379 | if (changed) | |
380 | *changed = bnc->changed; | |
381 | ||
382 | if (metricchanged) | |
383 | *metricchanged = bnc->metricchanged; | |
384 | ||
fb982c25 PJ |
385 | if (bnc->valid && bnc->metric) |
386 | (bgp_info_extra_get(ri))->igpmetric = bnc->metric; | |
387 | else if (ri->extra) | |
388 | ri->extra->igpmetric = 0; | |
718e3744 | 389 | |
390 | return bnc->valid; | |
391 | } | |
392 | ||
393 | /* Reset and free all BGP nexthop cache. */ | |
94f2b392 | 394 | static void |
718e3744 | 395 | bgp_nexthop_cache_reset (struct bgp_table *table) |
396 | { | |
397 | struct bgp_node *rn; | |
398 | struct bgp_nexthop_cache *bnc; | |
399 | ||
400 | for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn)) | |
401 | if ((bnc = rn->info) != NULL) | |
402 | { | |
403 | bnc_free (bnc); | |
404 | rn->info = NULL; | |
405 | bgp_unlock_node (rn); | |
406 | } | |
407 | } | |
408 | ||
94f2b392 | 409 | static void |
5932020b | 410 | bgp_scan (afi_t afi, safi_t safi) |
718e3744 | 411 | { |
412 | struct bgp_node *rn; | |
413 | struct bgp *bgp; | |
414 | struct bgp_info *bi; | |
415 | struct bgp_info *next; | |
416 | struct peer *peer; | |
1eb8ef25 | 417 | struct listnode *node, *nnode; |
718e3744 | 418 | int valid; |
419 | int current; | |
420 | int changed; | |
421 | int metricchanged; | |
422 | ||
423 | /* Change cache. */ | |
5932020b | 424 | if (bgp_nexthop_cache_table[afi] == cache1_table[afi]) |
425 | bgp_nexthop_cache_table[afi] = cache2_table[afi]; | |
718e3744 | 426 | else |
5932020b | 427 | bgp_nexthop_cache_table[afi] = cache1_table[afi]; |
718e3744 | 428 | |
429 | /* Get default bgp. */ | |
430 | bgp = bgp_get_default (); | |
431 | if (bgp == NULL) | |
432 | return; | |
433 | ||
434 | /* Maximum prefix check */ | |
1eb8ef25 | 435 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
718e3744 | 436 | { |
437 | if (peer->status != Established) | |
438 | continue; | |
439 | ||
5932020b | 440 | if (peer->afc[afi][SAFI_UNICAST]) |
441 | bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1); | |
442 | if (peer->afc[afi][SAFI_MULTICAST]) | |
443 | bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1); | |
444 | if (peer->afc[afi][SAFI_MPLS_VPN]) | |
445 | bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1); | |
718e3744 | 446 | } |
447 | ||
5932020b | 448 | for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn; |
718e3744 | 449 | rn = bgp_route_next (rn)) |
450 | { | |
451 | for (bi = rn->info; bi; bi = next) | |
452 | { | |
453 | next = bi->next; | |
454 | ||
455 | if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL) | |
456 | { | |
457 | changed = 0; | |
458 | metricchanged = 0; | |
459 | ||
460 | if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1) | |
5932020b | 461 | valid = bgp_nexthop_check_ebgp (afi, bi->attr); |
718e3744 | 462 | else |
5932020b | 463 | valid = bgp_nexthop_lookup (afi, bi->peer, bi, |
718e3744 | 464 | &changed, &metricchanged); |
465 | ||
466 | current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0; | |
467 | ||
468 | if (changed) | |
469 | SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED); | |
470 | else | |
471 | UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED); | |
472 | ||
473 | if (valid != current) | |
474 | { | |
475 | if (CHECK_FLAG (bi->flags, BGP_INFO_VALID)) | |
476 | { | |
477 | bgp_aggregate_decrement (bgp, &rn->p, bi, | |
5932020b | 478 | afi, SAFI_UNICAST); |
1a392d46 | 479 | bgp_info_unset_flag (rn, bi, BGP_INFO_VALID); |
718e3744 | 480 | } |
481 | else | |
482 | { | |
1a392d46 | 483 | bgp_info_set_flag (rn, bi, BGP_INFO_VALID); |
718e3744 | 484 | bgp_aggregate_increment (bgp, &rn->p, bi, |
5932020b | 485 | afi, SAFI_UNICAST); |
718e3744 | 486 | } |
487 | } | |
488 | ||
5932020b | 489 | if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST], |
718e3744 | 490 | BGP_CONFIG_DAMPENING) |
fb982c25 | 491 | && bi->extra && bi->extra->damp_info ) |
5932020b | 492 | if (bgp_damp_scan (bi, afi, SAFI_UNICAST)) |
718e3744 | 493 | bgp_aggregate_increment (bgp, &rn->p, bi, |
5932020b | 494 | afi, SAFI_UNICAST); |
718e3744 | 495 | } |
496 | } | |
5932020b | 497 | bgp_process (bgp, rn, afi, SAFI_UNICAST); |
718e3744 | 498 | } |
499 | ||
500 | /* Flash old cache. */ | |
5932020b | 501 | if (bgp_nexthop_cache_table[afi] == cache1_table[afi]) |
502 | bgp_nexthop_cache_reset (cache2_table[afi]); | |
718e3744 | 503 | else |
5932020b | 504 | bgp_nexthop_cache_reset (cache1_table[afi]); |
f418446b | 505 | |
506 | if (BGP_DEBUG (events, EVENTS)) | |
507 | { | |
508 | if (afi == AFI_IP) | |
509 | zlog_debug ("scanning IPv4 Unicast routing tables"); | |
510 | else if (afi == AFI_IP6) | |
511 | zlog_debug ("scanning IPv6 Unicast routing tables"); | |
512 | } | |
718e3744 | 513 | } |
718e3744 | 514 | |
515 | /* BGP scan thread. This thread check nexthop reachability. */ | |
94f2b392 | 516 | static int |
5932020b | 517 | bgp_scan_timer (struct thread *t) |
718e3744 | 518 | { |
519 | bgp_scan_thread = | |
5932020b | 520 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
718e3744 | 521 | |
f418446b | 522 | if (BGP_DEBUG (events, EVENTS)) |
478ba054 | 523 | zlog_debug ("Performing BGP general scanning"); |
718e3744 | 524 | |
5932020b | 525 | bgp_scan (AFI_IP, SAFI_UNICAST); |
718e3744 | 526 | |
527 | #ifdef HAVE_IPV6 | |
5932020b | 528 | bgp_scan (AFI_IP6, SAFI_UNICAST); |
718e3744 | 529 | #endif /* HAVE_IPV6 */ |
530 | ||
531 | return 0; | |
532 | } | |
533 | \f | |
5932020b | 534 | struct bgp_connected_ref |
718e3744 | 535 | { |
536 | unsigned int refcnt; | |
537 | }; | |
538 | ||
539 | void | |
540 | bgp_connected_add (struct connected *ifc) | |
541 | { | |
542 | struct prefix p; | |
543 | struct prefix *addr; | |
718e3744 | 544 | struct interface *ifp; |
545 | struct bgp_node *rn; | |
5932020b | 546 | struct bgp_connected_ref *bc; |
718e3744 | 547 | |
548 | ifp = ifc->ifp; | |
549 | ||
550 | if (! ifp) | |
551 | return; | |
552 | ||
553 | if (if_is_loopback (ifp)) | |
554 | return; | |
555 | ||
556 | addr = ifc->address; | |
718e3744 | 557 | |
558 | if (addr->family == AF_INET) | |
559 | { | |
e4529636 | 560 | PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc)); |
718e3744 | 561 | apply_mask_ipv4 ((struct prefix_ipv4 *) &p); |
562 | ||
563 | if (prefix_ipv4_any ((struct prefix_ipv4 *) &p)) | |
564 | return; | |
565 | ||
5932020b | 566 | rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p); |
718e3744 | 567 | if (rn->info) |
568 | { | |
569 | bc = rn->info; | |
570 | bc->refcnt++; | |
571 | } | |
572 | else | |
573 | { | |
393deb9b | 574 | bc = XCALLOC (0, sizeof (struct bgp_connected_ref)); |
718e3744 | 575 | bc->refcnt = 1; |
576 | rn->info = bc; | |
577 | } | |
578 | } | |
579 | #ifdef HAVE_IPV6 | |
e4529636 | 580 | else if (addr->family == AF_INET6) |
718e3744 | 581 | { |
e4529636 | 582 | PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc)); |
718e3744 | 583 | apply_mask_ipv6 ((struct prefix_ipv6 *) &p); |
584 | ||
585 | if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6)) | |
586 | return; | |
587 | ||
588 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) | |
589 | return; | |
590 | ||
5932020b | 591 | rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p); |
718e3744 | 592 | if (rn->info) |
593 | { | |
594 | bc = rn->info; | |
595 | bc->refcnt++; | |
596 | } | |
597 | else | |
598 | { | |
393deb9b | 599 | bc = XCALLOC (0, sizeof (struct bgp_connected_ref)); |
718e3744 | 600 | bc->refcnt = 1; |
601 | rn->info = bc; | |
602 | } | |
603 | } | |
604 | #endif /* HAVE_IPV6 */ | |
605 | } | |
606 | ||
607 | void | |
608 | bgp_connected_delete (struct connected *ifc) | |
609 | { | |
610 | struct prefix p; | |
611 | struct prefix *addr; | |
718e3744 | 612 | struct interface *ifp; |
613 | struct bgp_node *rn; | |
5932020b | 614 | struct bgp_connected_ref *bc; |
718e3744 | 615 | |
616 | ifp = ifc->ifp; | |
617 | ||
618 | if (if_is_loopback (ifp)) | |
619 | return; | |
620 | ||
621 | addr = ifc->address; | |
718e3744 | 622 | |
623 | if (addr->family == AF_INET) | |
624 | { | |
e4529636 | 625 | PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc)); |
718e3744 | 626 | apply_mask_ipv4 ((struct prefix_ipv4 *) &p); |
627 | ||
628 | if (prefix_ipv4_any ((struct prefix_ipv4 *) &p)) | |
629 | return; | |
630 | ||
5932020b | 631 | rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p); |
718e3744 | 632 | if (! rn) |
633 | return; | |
634 | ||
635 | bc = rn->info; | |
636 | bc->refcnt--; | |
637 | if (bc->refcnt == 0) | |
638 | { | |
639 | XFREE (0, bc); | |
640 | rn->info = NULL; | |
641 | } | |
642 | bgp_unlock_node (rn); | |
643 | bgp_unlock_node (rn); | |
644 | } | |
645 | #ifdef HAVE_IPV6 | |
646 | else if (addr->family == AF_INET6) | |
647 | { | |
e4529636 | 648 | PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc)); |
718e3744 | 649 | apply_mask_ipv6 ((struct prefix_ipv6 *) &p); |
650 | ||
651 | if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6)) | |
652 | return; | |
653 | ||
654 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) | |
655 | return; | |
656 | ||
5932020b | 657 | rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p); |
718e3744 | 658 | if (! rn) |
659 | return; | |
660 | ||
661 | bc = rn->info; | |
662 | bc->refcnt--; | |
663 | if (bc->refcnt == 0) | |
664 | { | |
665 | XFREE (0, bc); | |
666 | rn->info = NULL; | |
667 | } | |
668 | bgp_unlock_node (rn); | |
669 | bgp_unlock_node (rn); | |
670 | } | |
671 | #endif /* HAVE_IPV6 */ | |
672 | } | |
673 | ||
674 | int | |
675 | bgp_nexthop_self (afi_t afi, struct attr *attr) | |
676 | { | |
52dc7ee6 | 677 | struct listnode *node; |
678 | struct listnode *node2; | |
718e3744 | 679 | struct interface *ifp; |
680 | struct connected *ifc; | |
681 | struct prefix *p; | |
682 | ||
1eb8ef25 | 683 | for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp)) |
718e3744 | 684 | { |
1eb8ef25 | 685 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc)) |
718e3744 | 686 | { |
718e3744 | 687 | p = ifc->address; |
688 | ||
689 | if (p && p->family == AF_INET | |
690 | && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop)) | |
691 | return 1; | |
692 | } | |
693 | } | |
694 | return 0; | |
695 | } | |
696 | \f | |
94f2b392 | 697 | static struct bgp_nexthop_cache * |
66e5cd87 | 698 | zlookup_read (void) |
718e3744 | 699 | { |
700 | struct stream *s; | |
d3092e7f | 701 | uint16_t length; |
702 | u_char marker; | |
703 | u_char version; | |
704 | uint16_t command; | |
718e3744 | 705 | int nbytes; |
706 | struct in_addr raddr; | |
d3092e7f | 707 | uint32_t metric; |
718e3744 | 708 | int i; |
709 | u_char nexthop_num; | |
710 | struct nexthop *nexthop; | |
711 | struct bgp_nexthop_cache *bnc; | |
712 | ||
713 | s = zlookup->ibuf; | |
714 | stream_reset (s); | |
715 | ||
716 | nbytes = stream_read (s, zlookup->sock, 2); | |
717 | length = stream_getw (s); | |
718 | ||
719 | nbytes = stream_read (s, zlookup->sock, length - 2); | |
d3092e7f | 720 | marker = stream_getc (s); |
721 | version = stream_getc (s); | |
722 | ||
723 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) | |
724 | { | |
725 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", | |
726 | __func__, zlookup->sock, marker, version); | |
727 | return NULL; | |
728 | } | |
729 | ||
730 | command = stream_getw (s); | |
731 | ||
718e3744 | 732 | raddr.s_addr = stream_get_ipv4 (s); |
733 | metric = stream_getl (s); | |
734 | nexthop_num = stream_getc (s); | |
735 | ||
736 | if (nexthop_num) | |
737 | { | |
738 | bnc = bnc_new (); | |
739 | bnc->valid = 1; | |
740 | bnc->metric = metric; | |
741 | bnc->nexthop_num = nexthop_num; | |
742 | ||
743 | for (i = 0; i < nexthop_num; i++) | |
744 | { | |
393deb9b | 745 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
718e3744 | 746 | nexthop->type = stream_getc (s); |
747 | switch (nexthop->type) | |
748 | { | |
749 | case ZEBRA_NEXTHOP_IPV4: | |
750 | nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s); | |
751 | break; | |
752 | case ZEBRA_NEXTHOP_IFINDEX: | |
753 | case ZEBRA_NEXTHOP_IFNAME: | |
754 | nexthop->ifindex = stream_getl (s); | |
755 | break; | |
fa2b17e3 | 756 | default: |
757 | /* do nothing */ | |
758 | break; | |
718e3744 | 759 | } |
760 | bnc_nexthop_add (bnc, nexthop); | |
761 | } | |
762 | } | |
763 | else | |
764 | return NULL; | |
765 | ||
766 | return bnc; | |
767 | } | |
768 | ||
769 | struct bgp_nexthop_cache * | |
770 | zlookup_query (struct in_addr addr) | |
771 | { | |
772 | int ret; | |
773 | struct stream *s; | |
774 | ||
775 | /* Check socket. */ | |
776 | if (zlookup->sock < 0) | |
777 | return NULL; | |
778 | ||
779 | s = zlookup->obuf; | |
780 | stream_reset (s); | |
d3092e7f | 781 | zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP); |
718e3744 | 782 | stream_put_in_addr (s, &addr); |
d3092e7f | 783 | |
784 | stream_putw_at (s, 0, stream_get_endp (s)); | |
785 | ||
786 | ret = writen (zlookup->sock, s->data, stream_get_endp (s)); | |
718e3744 | 787 | if (ret < 0) |
788 | { | |
789 | zlog_err ("can't write to zlookup->sock"); | |
790 | close (zlookup->sock); | |
791 | zlookup->sock = -1; | |
792 | return NULL; | |
793 | } | |
794 | if (ret == 0) | |
795 | { | |
796 | zlog_err ("zlookup->sock connection closed"); | |
797 | close (zlookup->sock); | |
798 | zlookup->sock = -1; | |
799 | return NULL; | |
800 | } | |
801 | ||
802 | return zlookup_read (); | |
803 | } | |
804 | ||
805 | #ifdef HAVE_IPV6 | |
94f2b392 | 806 | static struct bgp_nexthop_cache * |
66e5cd87 | 807 | zlookup_read_ipv6 (void) |
718e3744 | 808 | { |
809 | struct stream *s; | |
d3092e7f | 810 | uint16_t length; |
811 | u_char version, marker; | |
812 | uint16_t command; | |
718e3744 | 813 | int nbytes; |
814 | struct in6_addr raddr; | |
d3092e7f | 815 | uint32_t metric; |
718e3744 | 816 | int i; |
817 | u_char nexthop_num; | |
818 | struct nexthop *nexthop; | |
819 | struct bgp_nexthop_cache *bnc; | |
820 | ||
821 | s = zlookup->ibuf; | |
822 | stream_reset (s); | |
823 | ||
824 | nbytes = stream_read (s, zlookup->sock, 2); | |
825 | length = stream_getw (s); | |
826 | ||
827 | nbytes = stream_read (s, zlookup->sock, length - 2); | |
d3092e7f | 828 | marker = stream_getc (s); |
829 | version = stream_getc (s); | |
830 | ||
831 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) | |
832 | { | |
833 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", | |
834 | __func__, zlookup->sock, marker, version); | |
835 | return NULL; | |
836 | } | |
837 | ||
838 | command = stream_getw (s); | |
839 | ||
718e3744 | 840 | stream_get (&raddr, s, 16); |
841 | ||
842 | metric = stream_getl (s); | |
843 | nexthop_num = stream_getc (s); | |
844 | ||
845 | if (nexthop_num) | |
846 | { | |
847 | bnc = bnc_new (); | |
848 | bnc->valid = 1; | |
849 | bnc->metric = metric; | |
850 | bnc->nexthop_num = nexthop_num; | |
851 | ||
852 | for (i = 0; i < nexthop_num; i++) | |
853 | { | |
393deb9b | 854 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
718e3744 | 855 | nexthop->type = stream_getc (s); |
856 | switch (nexthop->type) | |
857 | { | |
858 | case ZEBRA_NEXTHOP_IPV6: | |
859 | stream_get (&nexthop->gate.ipv6, s, 16); | |
860 | break; | |
861 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: | |
862 | case ZEBRA_NEXTHOP_IPV6_IFNAME: | |
863 | stream_get (&nexthop->gate.ipv6, s, 16); | |
864 | nexthop->ifindex = stream_getl (s); | |
865 | break; | |
866 | case ZEBRA_NEXTHOP_IFINDEX: | |
867 | case ZEBRA_NEXTHOP_IFNAME: | |
868 | nexthop->ifindex = stream_getl (s); | |
869 | break; | |
fa2b17e3 | 870 | default: |
871 | /* do nothing */ | |
872 | break; | |
718e3744 | 873 | } |
874 | bnc_nexthop_add (bnc, nexthop); | |
875 | } | |
876 | } | |
877 | else | |
878 | return NULL; | |
879 | ||
880 | return bnc; | |
881 | } | |
882 | ||
883 | struct bgp_nexthop_cache * | |
884 | zlookup_query_ipv6 (struct in6_addr *addr) | |
885 | { | |
886 | int ret; | |
887 | struct stream *s; | |
888 | ||
889 | /* Check socket. */ | |
890 | if (zlookup->sock < 0) | |
891 | return NULL; | |
892 | ||
893 | s = zlookup->obuf; | |
894 | stream_reset (s); | |
d3092e7f | 895 | zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP); |
718e3744 | 896 | stream_put (s, addr, 16); |
d3092e7f | 897 | stream_putw_at (s, 0, stream_get_endp (s)); |
898 | ||
899 | ret = writen (zlookup->sock, s->data, stream_get_endp (s)); | |
718e3744 | 900 | if (ret < 0) |
901 | { | |
902 | zlog_err ("can't write to zlookup->sock"); | |
903 | close (zlookup->sock); | |
904 | zlookup->sock = -1; | |
905 | return NULL; | |
906 | } | |
907 | if (ret == 0) | |
908 | { | |
909 | zlog_err ("zlookup->sock connection closed"); | |
910 | close (zlookup->sock); | |
911 | zlookup->sock = -1; | |
912 | return NULL; | |
913 | } | |
914 | ||
915 | return zlookup_read_ipv6 (); | |
916 | } | |
917 | #endif /* HAVE_IPV6 */ | |
918 | ||
94f2b392 | 919 | static int |
920 | bgp_import_check (struct prefix *p, u_int32_t *igpmetric, | |
921 | struct in_addr *igpnexthop) | |
718e3744 | 922 | { |
923 | struct stream *s; | |
924 | int ret; | |
d3092e7f | 925 | u_int16_t length, command; |
926 | u_char version, marker; | |
718e3744 | 927 | int nbytes; |
928 | struct in_addr addr; | |
929 | struct in_addr nexthop; | |
930 | u_int32_t metric = 0; | |
931 | u_char nexthop_num; | |
932 | u_char nexthop_type; | |
933 | ||
934 | /* If lookup connection is not available return valid. */ | |
935 | if (zlookup->sock < 0) | |
936 | { | |
937 | if (igpmetric) | |
938 | *igpmetric = 0; | |
939 | return 1; | |
940 | } | |
941 | ||
942 | /* Send query to the lookup connection */ | |
943 | s = zlookup->obuf; | |
944 | stream_reset (s); | |
d3092e7f | 945 | zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP); |
946 | ||
718e3744 | 947 | stream_putc (s, p->prefixlen); |
948 | stream_put_in_addr (s, &p->u.prefix4); | |
d3092e7f | 949 | |
950 | stream_putw_at (s, 0, stream_get_endp (s)); | |
951 | ||
718e3744 | 952 | /* Write the packet. */ |
d3092e7f | 953 | ret = writen (zlookup->sock, s->data, stream_get_endp (s)); |
718e3744 | 954 | |
955 | if (ret < 0) | |
956 | { | |
957 | zlog_err ("can't write to zlookup->sock"); | |
958 | close (zlookup->sock); | |
959 | zlookup->sock = -1; | |
960 | return 1; | |
961 | } | |
962 | if (ret == 0) | |
963 | { | |
964 | zlog_err ("zlookup->sock connection closed"); | |
965 | close (zlookup->sock); | |
966 | zlookup->sock = -1; | |
967 | return 1; | |
968 | } | |
969 | ||
970 | /* Get result. */ | |
971 | stream_reset (s); | |
972 | ||
973 | /* Fetch length. */ | |
974 | nbytes = stream_read (s, zlookup->sock, 2); | |
975 | length = stream_getw (s); | |
976 | ||
977 | /* Fetch whole data. */ | |
978 | nbytes = stream_read (s, zlookup->sock, length - 2); | |
d3092e7f | 979 | marker = stream_getc (s); |
980 | version = stream_getc (s); | |
981 | ||
982 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) | |
983 | { | |
984 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", | |
985 | __func__, zlookup->sock, marker, version); | |
986 | return 0; | |
987 | } | |
988 | ||
989 | command = stream_getw (s); | |
990 | ||
718e3744 | 991 | addr.s_addr = stream_get_ipv4 (s); |
992 | metric = stream_getl (s); | |
993 | nexthop_num = stream_getc (s); | |
994 | ||
995 | /* Set IGP metric value. */ | |
996 | if (igpmetric) | |
997 | *igpmetric = metric; | |
998 | ||
999 | /* If there is nexthop then this is active route. */ | |
1000 | if (nexthop_num) | |
1001 | { | |
1002 | nexthop.s_addr = 0; | |
1003 | nexthop_type = stream_getc (s); | |
1004 | if (nexthop_type == ZEBRA_NEXTHOP_IPV4) | |
1005 | { | |
1006 | nexthop.s_addr = stream_get_ipv4 (s); | |
1007 | if (igpnexthop) | |
1008 | *igpnexthop = nexthop; | |
1009 | } | |
1010 | else | |
1011 | *igpnexthop = nexthop; | |
1012 | ||
1013 | return 1; | |
1014 | } | |
1015 | else | |
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | /* Scan all configured BGP route then check the route exists in IGP or | |
1020 | not. */ | |
94f2b392 | 1021 | static int |
718e3744 | 1022 | bgp_import (struct thread *t) |
1023 | { | |
1024 | struct bgp *bgp; | |
1025 | struct bgp_node *rn; | |
1026 | struct bgp_static *bgp_static; | |
1eb8ef25 | 1027 | struct listnode *node, *nnode; |
718e3744 | 1028 | int valid; |
1029 | u_int32_t metric; | |
1030 | struct in_addr nexthop; | |
1031 | afi_t afi; | |
1032 | safi_t safi; | |
1033 | ||
1034 | bgp_import_thread = | |
1035 | thread_add_timer (master, bgp_import, NULL, bgp_import_interval); | |
1036 | ||
f418446b | 1037 | if (BGP_DEBUG (events, EVENTS)) |
1038 | zlog_debug ("Import timer expired."); | |
718e3744 | 1039 | |
1eb8ef25 | 1040 | for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp)) |
6cbbc3cc | 1041 | { |
1042 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
1043 | for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++) | |
1044 | for (rn = bgp_table_top (bgp->route[afi][safi]); rn; | |
1045 | rn = bgp_route_next (rn)) | |
1046 | if ((bgp_static = rn->info) != NULL) | |
718e3744 | 1047 | { |
6cbbc3cc | 1048 | if (bgp_static->backdoor) |
1049 | continue; | |
718e3744 | 1050 | |
6cbbc3cc | 1051 | valid = bgp_static->valid; |
1052 | metric = bgp_static->igpmetric; | |
1053 | nexthop = bgp_static->igpnexthop; | |
1054 | ||
1055 | if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK) | |
1056 | && afi == AFI_IP && safi == SAFI_UNICAST) | |
1057 | bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric, | |
1058 | &bgp_static->igpnexthop); | |
718e3744 | 1059 | else |
6cbbc3cc | 1060 | { |
1061 | bgp_static->valid = 1; | |
1062 | bgp_static->igpmetric = 0; | |
1063 | bgp_static->igpnexthop.s_addr = 0; | |
1064 | } | |
1065 | ||
1066 | if (bgp_static->valid != valid) | |
1067 | { | |
1068 | if (bgp_static->valid) | |
1069 | bgp_static_update (bgp, &rn->p, bgp_static, afi, safi); | |
1070 | else | |
1071 | bgp_static_withdraw (bgp, &rn->p, afi, safi); | |
1072 | } | |
1073 | else if (bgp_static->valid) | |
1074 | { | |
1075 | if (bgp_static->igpmetric != metric | |
1076 | || bgp_static->igpnexthop.s_addr != nexthop.s_addr | |
1077 | || bgp_static->rmap.name) | |
1078 | bgp_static_update (bgp, &rn->p, bgp_static, afi, safi); | |
1079 | } | |
718e3744 | 1080 | } |
6cbbc3cc | 1081 | } |
718e3744 | 1082 | return 0; |
1083 | } | |
1084 | ||
1085 | /* Connect to zebra for nexthop lookup. */ | |
94f2b392 | 1086 | static int |
718e3744 | 1087 | zlookup_connect (struct thread *t) |
1088 | { | |
1089 | struct zclient *zlookup; | |
1090 | ||
1091 | zlookup = THREAD_ARG (t); | |
1092 | zlookup->t_connect = NULL; | |
1093 | ||
1094 | if (zlookup->sock != -1) | |
1095 | return 0; | |
1096 | ||
1097 | #ifdef HAVE_TCP_ZEBRA | |
1098 | zlookup->sock = zclient_socket (); | |
1099 | #else | |
1100 | zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH); | |
1101 | #endif /* HAVE_TCP_ZEBRA */ | |
1102 | if (zlookup->sock < 0) | |
1103 | return -1; | |
1104 | ||
718e3744 | 1105 | return 0; |
1106 | } | |
1107 | ||
1108 | /* Check specified multiaccess next-hop. */ | |
1109 | int | |
1110 | bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer) | |
1111 | { | |
1112 | struct bgp_node *rn1; | |
1113 | struct bgp_node *rn2; | |
1114 | struct prefix p1; | |
1115 | struct prefix p2; | |
1116 | struct in_addr addr; | |
1117 | int ret; | |
1118 | ||
1119 | ret = inet_aton (peer, &addr); | |
1120 | if (! ret) | |
1121 | return 0; | |
1122 | ||
1123 | memset (&p1, 0, sizeof (struct prefix)); | |
1124 | p1.family = AF_INET; | |
1125 | p1.prefixlen = IPV4_MAX_BITLEN; | |
1126 | p1.u.prefix4 = nexthop; | |
1127 | memset (&p2, 0, sizeof (struct prefix)); | |
1128 | p2.family = AF_INET; | |
1129 | p2.prefixlen = IPV4_MAX_BITLEN; | |
1130 | p2.u.prefix4 = addr; | |
1131 | ||
1132 | /* If bgp scan is not enabled, return invalid. */ | |
1133 | if (zlookup->sock < 0) | |
1134 | return 0; | |
1135 | ||
5932020b | 1136 | rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1); |
718e3744 | 1137 | if (! rn1) |
1138 | return 0; | |
1139 | ||
5932020b | 1140 | rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2); |
718e3744 | 1141 | if (! rn2) |
1142 | return 0; | |
1143 | ||
1144 | if (rn1 == rn2) | |
1145 | return 1; | |
1146 | ||
1147 | return 0; | |
1148 | } | |
1149 | \f | |
1150 | DEFUN (bgp_scan_time, | |
1151 | bgp_scan_time_cmd, | |
1152 | "bgp scan-time <5-60>", | |
1153 | "BGP specific commands\n" | |
1154 | "Configure background scanner interval\n" | |
1155 | "Scanner interval (seconds)\n") | |
1156 | { | |
1157 | bgp_scan_interval = atoi (argv[0]); | |
1158 | ||
1159 | if (bgp_scan_thread) | |
1160 | { | |
1161 | thread_cancel (bgp_scan_thread); | |
1162 | bgp_scan_thread = | |
5932020b | 1163 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
718e3744 | 1164 | } |
1165 | ||
1166 | return CMD_SUCCESS; | |
1167 | } | |
1168 | ||
1169 | DEFUN (no_bgp_scan_time, | |
1170 | no_bgp_scan_time_cmd, | |
1171 | "no bgp scan-time", | |
1172 | NO_STR | |
1173 | "BGP specific commands\n" | |
1174 | "Configure background scanner interval\n") | |
1175 | { | |
1176 | bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT; | |
1177 | ||
1178 | if (bgp_scan_thread) | |
1179 | { | |
1180 | thread_cancel (bgp_scan_thread); | |
1181 | bgp_scan_thread = | |
5932020b | 1182 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
718e3744 | 1183 | } |
1184 | ||
1185 | return CMD_SUCCESS; | |
1186 | } | |
1187 | ||
1188 | ALIAS (no_bgp_scan_time, | |
1189 | no_bgp_scan_time_val_cmd, | |
1190 | "no bgp scan-time <5-60>", | |
1191 | NO_STR | |
1192 | "BGP specific commands\n" | |
1193 | "Configure background scanner interval\n" | |
1194 | "Scanner interval (seconds)\n") | |
1195 | ||
1196 | DEFUN (show_ip_bgp_scan, | |
1197 | show_ip_bgp_scan_cmd, | |
1198 | "show ip bgp scan", | |
1199 | SHOW_STR | |
1200 | IP_STR | |
1201 | BGP_STR | |
1202 | "BGP scan status\n") | |
1203 | { | |
1204 | struct bgp_node *rn; | |
1205 | struct bgp_nexthop_cache *bnc; | |
1206 | ||
1207 | if (bgp_scan_thread) | |
1208 | vty_out (vty, "BGP scan is running%s", VTY_NEWLINE); | |
1209 | else | |
1210 | vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE); | |
1211 | vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE); | |
1212 | ||
1213 | vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE); | |
5932020b | 1214 | for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn)) |
718e3744 | 1215 | if ((bnc = rn->info) != NULL) |
1216 | { | |
1217 | if (bnc->valid) | |
1218 | vty_out (vty, " %s valid [IGP metric %d]%s", | |
1219 | inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE); | |
1220 | else | |
1221 | vty_out (vty, " %s invalid%s", | |
1222 | inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE); | |
1223 | } | |
1224 | ||
1225 | #ifdef HAVE_IPV6 | |
1226 | { | |
1227 | char buf[BUFSIZ]; | |
5932020b | 1228 | for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]); |
1229 | rn; | |
1230 | rn = bgp_route_next (rn)) | |
718e3744 | 1231 | if ((bnc = rn->info) != NULL) |
1232 | { | |
1233 | if (bnc->valid) | |
1234 | vty_out (vty, " %s valid [IGP metric %d]%s", | |
1235 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ), | |
1236 | bnc->metric, VTY_NEWLINE); | |
1237 | else | |
1238 | vty_out (vty, " %s invalid%s", | |
1239 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ), | |
1240 | VTY_NEWLINE); | |
1241 | } | |
1242 | } | |
1243 | #endif /* HAVE_IPV6 */ | |
1244 | ||
1245 | vty_out (vty, "BGP connected route:%s", VTY_NEWLINE); | |
5932020b | 1246 | for (rn = bgp_table_top (bgp_connected_table[AFI_IP]); |
1247 | rn; | |
1248 | rn = bgp_route_next (rn)) | |
718e3744 | 1249 | if (rn->info != NULL) |
1250 | vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen, | |
1251 | VTY_NEWLINE); | |
1252 | ||
1253 | #ifdef HAVE_IPV6 | |
1254 | { | |
1255 | char buf[BUFSIZ]; | |
1256 | ||
5932020b | 1257 | for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]); |
1258 | rn; | |
1259 | rn = bgp_route_next (rn)) | |
718e3744 | 1260 | if (rn->info != NULL) |
1261 | vty_out (vty, " %s/%d%s", | |
1262 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ), | |
1263 | rn->p.prefixlen, | |
1264 | VTY_NEWLINE); | |
1265 | } | |
1266 | #endif /* HAVE_IPV6 */ | |
1267 | ||
1268 | return CMD_SUCCESS; | |
1269 | } | |
1270 | ||
1271 | int | |
1272 | bgp_config_write_scan_time (struct vty *vty) | |
1273 | { | |
1274 | if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT) | |
1275 | vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE); | |
1276 | return CMD_SUCCESS; | |
1277 | } | |
1278 | ||
1279 | void | |
66e5cd87 | 1280 | bgp_scan_init (void) |
718e3744 | 1281 | { |
1282 | zlookup = zclient_new (); | |
1283 | zlookup->sock = -1; | |
718e3744 | 1284 | zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0); |
1285 | ||
1286 | bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT; | |
1287 | bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT; | |
1288 | ||
64e580a7 PJ |
1289 | cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); |
1290 | cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); | |
5932020b | 1291 | bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP]; |
718e3744 | 1292 | |
64e580a7 | 1293 | bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); |
718e3744 | 1294 | |
1295 | #ifdef HAVE_IPV6 | |
64e580a7 PJ |
1296 | cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); |
1297 | cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); | |
5932020b | 1298 | bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6]; |
64e580a7 | 1299 | bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); |
718e3744 | 1300 | #endif /* HAVE_IPV6 */ |
1301 | ||
1302 | /* Make BGP scan thread. */ | |
5932020b | 1303 | bgp_scan_thread = thread_add_timer (master, bgp_scan_timer, |
1304 | NULL, bgp_scan_interval); | |
6cbbc3cc | 1305 | /* Make BGP import there. */ |
1306 | bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0); | |
718e3744 | 1307 | |
1308 | install_element (BGP_NODE, &bgp_scan_time_cmd); | |
1309 | install_element (BGP_NODE, &no_bgp_scan_time_cmd); | |
1310 | install_element (BGP_NODE, &no_bgp_scan_time_val_cmd); | |
1311 | install_element (VIEW_NODE, &show_ip_bgp_scan_cmd); | |
62687ff1 | 1312 | install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd); |
718e3744 | 1313 | install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd); |
1314 | } | |
228da428 CC |
1315 | |
1316 | void | |
1317 | bgp_scan_finish (void) | |
1318 | { | |
1319 | bgp_table_unlock (cache1_table[AFI_IP]); | |
1320 | cache1_table[AFI_IP] = NULL; | |
1321 | ||
1322 | bgp_table_unlock (cache2_table[AFI_IP]); | |
1323 | cache2_table[AFI_IP] = NULL; | |
1324 | ||
1325 | bgp_table_unlock (bgp_connected_table[AFI_IP]); | |
1326 | bgp_connected_table[AFI_IP] = NULL; | |
1327 | ||
1328 | #ifdef HAVE_IPV6 | |
1329 | bgp_table_unlock (cache1_table[AFI_IP6]); | |
1330 | cache1_table[AFI_IP6] = NULL; | |
1331 | ||
1332 | bgp_table_unlock (cache2_table[AFI_IP6]); | |
1333 | cache2_table[AFI_IP6] = NULL; | |
1334 | ||
1335 | bgp_table_unlock (bgp_connected_table[AFI_IP6]); | |
1336 | bgp_connected_table[AFI_IP6] = NULL; | |
1337 | #endif /* HAVE_IPV6 */ | |
1338 | } |