blob: 0cde665eb380e4d2585d3d8595d7a41b249d30ca [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* BGP nexthop scan
2 Copyright (C) 2000 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-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
42struct bgp_nexthop_cache *zlookup_query (struct in_addr);
43#ifdef HAVE_IPV6
44struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *);
45#endif /* HAVE_IPV6 */
46
47/* Only one BGP scan thread are activated at the same time. */
paul00d252c2005-05-23 14:19:54 +000048static struct thread *bgp_scan_thread = NULL;
paul718e3742002-12-13 20:15:29 +000049
50/* BGP import thread */
paul00d252c2005-05-23 14:19:54 +000051static struct thread *bgp_import_thread = NULL;
paul718e3742002-12-13 20:15:29 +000052
53/* BGP scan interval. */
paul00d252c2005-05-23 14:19:54 +000054static int bgp_scan_interval;
paul718e3742002-12-13 20:15:29 +000055
56/* BGP import interval. */
paul00d252c2005-05-23 14:19:54 +000057static int bgp_import_interval;
paul718e3742002-12-13 20:15:29 +000058
59/* Route table for next-hop lookup cache. */
paul00d252c2005-05-23 14:19:54 +000060static struct bgp_table *bgp_nexthop_cache_table[AFI_MAX];
61static struct bgp_table *cache1_table[AFI_MAX];
62static struct bgp_table *cache2_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000063
64/* Route table for connected route. */
paul00d252c2005-05-23 14:19:54 +000065static struct bgp_table *bgp_connected_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000066
67/* BGP nexthop lookup query client. */
Chris Caputo228da422009-07-18 05:44:03 +000068struct zclient *zlookup = NULL;
paul718e3742002-12-13 20:15:29 +000069
70/* Add nexthop to the end of the list. */
paul94f2b392005-06-28 12:44:16 +000071static void
paul718e3742002-12-13 20:15:29 +000072bnc_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
paul94f2b392005-06-28 12:44:16 +000085static void
paul718e3742002-12-13 20:15:29 +000086bnc_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
paul94f2b392005-06-28 12:44:16 +000098static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080099bnc_new (void)
paul718e3742002-12-13 20:15:29 +0000100{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700101 return XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
paul718e3742002-12-13 20:15:29 +0000102}
103
paul94f2b392005-06-28 12:44:16 +0000104static void
paul718e3742002-12-13 20:15:29 +0000105bnc_free (struct bgp_nexthop_cache *bnc)
106{
107 bnc_nexthop_free (bnc);
108 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
109}
110
paul94f2b392005-06-28 12:44:16 +0000111static int
paul718e3742002-12-13 20:15:29 +0000112bgp_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 */
hassofa2b17e2004-03-04 17:45:00 +0000141 default:
142 /* do nothing */
143 break;
paul718e3742002-12-13 20:15:29 +0000144 }
145 return 1;
146}
147
paul94f2b392005-06-28 12:44:16 +0000148static int
paul718e3742002-12-13 20:15:29 +0000149bgp_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. */
173int
174bgp_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 {
paul59320202004-11-09 01:54:03 +0000185 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
paul718e3742002-12-13 20:15:29 +0000186 if (rn)
187 {
188 bgp_unlock_node (rn);
189 return 1;
190 }
191 }
192#ifdef HAVE_IPV6
193 else if (afi == AFI_IP6)
194 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000195 if (attr->extra->mp_nexthop_len == 32)
paul718e3742002-12-13 20:15:29 +0000196 return 1;
Paul Jakmafb982c22007-05-04 20:15:47 +0000197 else if (attr->extra->mp_nexthop_len == 16)
paul718e3742002-12-13 20:15:29 +0000198 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000199 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000200 return 1;
201
paul59320202004-11-09 01:54:03 +0000202 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
Paul Jakmafb982c22007-05-04 20:15:47 +0000203 &attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000204 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. */
paul94f2b392005-06-28 12:44:16 +0000217static int
paul718e3742002-12-13 20:15:29 +0000218bgp_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 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000229 if (ri->extra)
230 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000231 return 1;
232 }
233
234 /* Only check IPv6 global address only nexthop. */
235 attr = ri->attr;
236
Paul Jakmafb982c22007-05-04 20:15:47 +0000237 if (attr->extra->mp_nexthop_len != 16
238 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000239 return 1;
240
241 memset (&p, 0, sizeof (struct prefix));
242 p.family = AF_INET6;
243 p.prefixlen = IPV6_MAX_BITLEN;
Paul Jakmafb982c22007-05-04 20:15:47 +0000244 p.u.prefix6 = attr->extra->mp_nexthop_global;
paul718e3742002-12-13 20:15:29 +0000245
246 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000247 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
paul718e3742002-12-13 20:15:29 +0000248
249 if (rn->info)
250 {
251 bnc = rn->info;
252 bgp_unlock_node (rn);
253 }
254 else
255 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000256 bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000257 if (bnc)
258 {
259 struct bgp_table *old;
260 struct bgp_node *oldrn;
261 struct bgp_nexthop_cache *oldbnc;
262
263 if (changed)
264 {
paul59320202004-11-09 01:54:03 +0000265 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
266 old = cache2_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000267 else
paul59320202004-11-09 01:54:03 +0000268 old = cache1_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000269
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
Paul Jakmafb982c22007-05-04 20:15:47 +0000296 if (bnc->valid && bnc->metric)
297 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
298 else if (ri->extra)
299 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000300
301 return bnc->valid;
302}
303#endif /* HAVE_IPV6 */
304
305/* Check specified next-hop is reachable or not. */
306int
307bgp_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 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000318 if (ri->extra)
319 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000320 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 */
paul59320202004-11-09 01:54:03 +0000336 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000337
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 {
paul59320202004-11-09 01:54:03 +0000354 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
355 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000356 else
paul59320202004-11-09 01:54:03 +0000357 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000358
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
Paul Jakmafb982c22007-05-04 20:15:47 +0000385 if (bnc->valid && bnc->metric)
386 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
387 else if (ri->extra)
388 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000389
390 return bnc->valid;
391}
392
393/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000394static void
paul718e3742002-12-13 20:15:29 +0000395bgp_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
paul94f2b392005-06-28 12:44:16 +0000409static void
paul59320202004-11-09 01:54:03 +0000410bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000411{
412 struct bgp_node *rn;
413 struct bgp *bgp;
414 struct bgp_info *bi;
415 struct bgp_info *next;
416 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000417 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000418 int valid;
419 int current;
420 int changed;
421 int metricchanged;
422
423 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000424 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
425 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000426 else
paul59320202004-11-09 01:54:03 +0000427 bgp_nexthop_cache_table[afi] = cache1_table[afi];
paul718e3742002-12-13 20:15:29 +0000428
429 /* Get default bgp. */
430 bgp = bgp_get_default ();
431 if (bgp == NULL)
432 return;
433
434 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000435 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000436 {
437 if (peer->status != Established)
438 continue;
439
paul59320202004-11-09 01:54:03 +0000440 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);
paul718e3742002-12-13 20:15:29 +0000446 }
447
paul59320202004-11-09 01:54:03 +0000448 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000449 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)
paul59320202004-11-09 01:54:03 +0000461 valid = bgp_nexthop_check_ebgp (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000462 else
paul59320202004-11-09 01:54:03 +0000463 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000464 &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,
paul59320202004-11-09 01:54:03 +0000478 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000479 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000480 }
481 else
482 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000483 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000484 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000485 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000486 }
487 }
488
paul59320202004-11-09 01:54:03 +0000489 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000490 BGP_CONFIG_DAMPENING)
Paul Jakmafb982c22007-05-04 20:15:47 +0000491 && bi->extra && bi->extra->damp_info )
paul59320202004-11-09 01:54:03 +0000492 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000493 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000494 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000495 }
496 }
paul59320202004-11-09 01:54:03 +0000497 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000498 }
499
500 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000501 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
502 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000503 else
paul59320202004-11-09 01:54:03 +0000504 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000505
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 }
paul718e3742002-12-13 20:15:29 +0000513}
514
paul718e3742002-12-13 20:15:29 +0000515/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000516static int
paul59320202004-11-09 01:54:03 +0000517bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000518{
519 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +0000520 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000521
hassof4184462005-02-01 20:13:16 +0000522 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000523 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000524
paul59320202004-11-09 01:54:03 +0000525 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000526
527#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000528 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000529#endif /* HAVE_IPV6 */
530
531 return 0;
532}
533
paul59320202004-11-09 01:54:03 +0000534struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000535{
536 unsigned int refcnt;
537};
538
539void
540bgp_connected_add (struct connected *ifc)
541{
542 struct prefix p;
543 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000544 struct interface *ifp;
545 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000546 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000547
548 ifp = ifc->ifp;
549
550 if (! ifp)
551 return;
552
553 if (if_is_loopback (ifp))
554 return;
555
556 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000557
558 if (addr->family == AF_INET)
559 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000560 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000561 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
562
563 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
564 return;
565
paul59320202004-11-09 01:54:03 +0000566 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000567 if (rn->info)
568 {
569 bc = rn->info;
570 bc->refcnt++;
571 }
572 else
573 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700574 bc = XCALLOC (0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000575 bc->refcnt = 1;
576 rn->info = bc;
577 }
578 }
579#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000580 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000581 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000582 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000583 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
paul59320202004-11-09 01:54:03 +0000591 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000592 if (rn->info)
593 {
594 bc = rn->info;
595 bc->refcnt++;
596 }
597 else
598 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700599 bc = XCALLOC (0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000600 bc->refcnt = 1;
601 rn->info = bc;
602 }
603 }
604#endif /* HAVE_IPV6 */
605}
606
607void
608bgp_connected_delete (struct connected *ifc)
609{
610 struct prefix p;
611 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000612 struct interface *ifp;
613 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000614 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000615
616 ifp = ifc->ifp;
617
618 if (if_is_loopback (ifp))
619 return;
620
621 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000622
623 if (addr->family == AF_INET)
624 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000625 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000626 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
627
628 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
629 return;
630
paul59320202004-11-09 01:54:03 +0000631 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000632 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 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000648 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000649 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
paul59320202004-11-09 01:54:03 +0000657 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000658 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
674int
675bgp_nexthop_self (afi_t afi, struct attr *attr)
676{
hasso52dc7ee2004-09-23 19:18:23 +0000677 struct listnode *node;
678 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000679 struct interface *ifp;
680 struct connected *ifc;
681 struct prefix *p;
682
paul1eb8ef22005-04-07 07:30:20 +0000683 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000684 {
paul1eb8ef22005-04-07 07:30:20 +0000685 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000686 {
paul718e3742002-12-13 20:15:29 +0000687 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
paul94f2b392005-06-28 12:44:16 +0000697static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800698zlookup_read (void)
paul718e3742002-12-13 20:15:29 +0000699{
700 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000701 uint16_t length;
702 u_char marker;
703 u_char version;
704 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000705 int nbytes;
706 struct in_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000707 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000708 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);
pauld3092e72006-01-17 17:33:46 +0000720 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
paul718e3742002-12-13 20:15:29 +0000732 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 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700745 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000746 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;
hassofa2b17e2004-03-04 17:45:00 +0000756 default:
757 /* do nothing */
758 break;
paul718e3742002-12-13 20:15:29 +0000759 }
760 bnc_nexthop_add (bnc, nexthop);
761 }
762 }
763 else
764 return NULL;
765
766 return bnc;
767}
768
769struct bgp_nexthop_cache *
770zlookup_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);
pauld3092e72006-01-17 17:33:46 +0000781 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000782 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000783
784 stream_putw_at (s, 0, stream_get_endp (s));
785
786 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000787 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
paul94f2b392005-06-28 12:44:16 +0000806static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800807zlookup_read_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000808{
809 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000810 uint16_t length;
811 u_char version, marker;
812 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000813 int nbytes;
814 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000815 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000816 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);
pauld3092e72006-01-17 17:33:46 +0000828 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
paul718e3742002-12-13 20:15:29 +0000840 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 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700854 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000855 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;
hassofa2b17e2004-03-04 17:45:00 +0000870 default:
871 /* do nothing */
872 break;
paul718e3742002-12-13 20:15:29 +0000873 }
874 bnc_nexthop_add (bnc, nexthop);
875 }
876 }
877 else
878 return NULL;
879
880 return bnc;
881}
882
883struct bgp_nexthop_cache *
884zlookup_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);
pauld3092e72006-01-17 17:33:46 +0000895 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000896 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000897 stream_putw_at (s, 0, stream_get_endp (s));
898
899 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000900 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
paul94f2b392005-06-28 12:44:16 +0000919static int
920bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
921 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000922{
923 struct stream *s;
924 int ret;
pauld3092e72006-01-17 17:33:46 +0000925 u_int16_t length, command;
926 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000927 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);
pauld3092e72006-01-17 17:33:46 +0000945 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
946
paul718e3742002-12-13 20:15:29 +0000947 stream_putc (s, p->prefixlen);
948 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +0000949
950 stream_putw_at (s, 0, stream_get_endp (s));
951
paul718e3742002-12-13 20:15:29 +0000952 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +0000953 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000954
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);
pauld3092e72006-01-17 17:33:46 +0000979 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
paul718e3742002-12-13 20:15:29 +0000991 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. */
paul94f2b392005-06-28 12:44:16 +00001021static int
paul718e3742002-12-13 20:15:29 +00001022bgp_import (struct thread *t)
1023{
1024 struct bgp *bgp;
1025 struct bgp_node *rn;
1026 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001027 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001028 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
hassof4184462005-02-01 20:13:16 +00001037 if (BGP_DEBUG (events, EVENTS))
1038 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001039
paul1eb8ef22005-04-07 07:30:20 +00001040 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001041 {
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)
paul718e3742002-12-13 20:15:29 +00001047 {
paul6cbbc3c2003-04-28 17:11:02 +00001048 if (bgp_static->backdoor)
1049 continue;
paul718e3742002-12-13 20:15:29 +00001050
paul6cbbc3c2003-04-28 17:11:02 +00001051 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);
paul718e3742002-12-13 20:15:29 +00001059 else
paul6cbbc3c2003-04-28 17:11:02 +00001060 {
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 }
paul718e3742002-12-13 20:15:29 +00001080 }
paul6cbbc3c2003-04-28 17:11:02 +00001081 }
paul718e3742002-12-13 20:15:29 +00001082 return 0;
1083}
1084
1085/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001086static int
paul718e3742002-12-13 20:15:29 +00001087zlookup_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
paul718e3742002-12-13 20:15:29 +00001105 return 0;
1106}
1107
1108/* Check specified multiaccess next-hop. */
1109int
1110bgp_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
paul59320202004-11-09 01:54:03 +00001136 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001137 if (! rn1)
1138 return 0;
1139
paul59320202004-11-09 01:54:03 +00001140 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001141 if (! rn2)
1142 return 0;
1143
1144 if (rn1 == rn2)
1145 return 1;
1146
1147 return 0;
1148}
1149
1150DEFUN (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 =
paul59320202004-11-09 01:54:03 +00001163 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001164 }
1165
1166 return CMD_SUCCESS;
1167}
1168
1169DEFUN (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 =
paul59320202004-11-09 01:54:03 +00001182 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001183 }
1184
1185 return CMD_SUCCESS;
1186}
1187
1188ALIAS (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
1196DEFUN (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);
paul59320202004-11-09 01:54:03 +00001214 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001215 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];
paul59320202004-11-09 01:54:03 +00001228 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1229 rn;
1230 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001231 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);
paul59320202004-11-09 01:54:03 +00001246 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1247 rn;
1248 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001249 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
paul59320202004-11-09 01:54:03 +00001257 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1258 rn;
1259 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001260 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
1271int
1272bgp_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
1279void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -08001280bgp_scan_init (void)
paul718e3742002-12-13 20:15:29 +00001281{
1282 zlookup = zclient_new ();
1283 zlookup->sock = -1;
paul718e3742002-12-13 20:15:29 +00001284 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
Paul Jakma64e580a2006-02-21 01:09:01 +00001289 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1290 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001291 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001292
Paul Jakma64e580a2006-02-21 01:09:01 +00001293 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001294
1295#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001296 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1297 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001298 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001299 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001300#endif /* HAVE_IPV6 */
1301
1302 /* Make BGP scan thread. */
paul59320202004-11-09 01:54:03 +00001303 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1304 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001305 /* Make BGP import there. */
1306 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001307
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);
Paul Jakma62687ff2008-08-23 14:27:06 +01001312 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
paul718e3742002-12-13 20:15:29 +00001313 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
1314}
Chris Caputo228da422009-07-18 05:44:03 +00001315
1316void
1317bgp_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}