blob: 1a2923cc52b5145b871b7a0e069b26c0a3e77e63 [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
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400149bgp_nexthop_cache_different (struct bgp_nexthop_cache *bnc1,
paul718e3742002-12-13 20:15:29 +0000150 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
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400174bgp_nexthop_onlink (afi_t afi, struct attr *attr)
paul718e3742002-12-13 20:15:29 +0000175{
176 struct bgp_node *rn;
177
paul718e3742002-12-13 20:15:29 +0000178 /* Lookup the address is onlink or not. */
179 if (afi == AFI_IP)
180 {
paul59320202004-11-09 01:54:03 +0000181 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
paul718e3742002-12-13 20:15:29 +0000182 if (rn)
183 {
184 bgp_unlock_node (rn);
185 return 1;
186 }
187 }
188#ifdef HAVE_IPV6
189 else if (afi == AFI_IP6)
190 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000191 if (attr->extra->mp_nexthop_len == 32)
paul718e3742002-12-13 20:15:29 +0000192 return 1;
Paul Jakmafb982c22007-05-04 20:15:47 +0000193 else if (attr->extra->mp_nexthop_len == 16)
paul718e3742002-12-13 20:15:29 +0000194 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000195 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000196 return 1;
197
paul59320202004-11-09 01:54:03 +0000198 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
Paul Jakmafb982c22007-05-04 20:15:47 +0000199 &attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000200 if (rn)
201 {
202 bgp_unlock_node (rn);
203 return 1;
204 }
205 }
206 }
207#endif /* HAVE_IPV6 */
208 return 0;
209}
210
211#ifdef HAVE_IPV6
212/* Check specified next-hop is reachable or not. */
paul94f2b392005-06-28 12:44:16 +0000213static int
paul718e3742002-12-13 20:15:29 +0000214bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
215 int *metricchanged)
216{
217 struct bgp_node *rn;
218 struct prefix p;
219 struct bgp_nexthop_cache *bnc;
220 struct attr *attr;
221
paul718e3742002-12-13 20:15:29 +0000222 /* Only check IPv6 global address only nexthop. */
223 attr = ri->attr;
224
Paul Jakmafb982c22007-05-04 20:15:47 +0000225 if (attr->extra->mp_nexthop_len != 16
226 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000227 return 1;
228
229 memset (&p, 0, sizeof (struct prefix));
230 p.family = AF_INET6;
231 p.prefixlen = IPV6_MAX_BITLEN;
Paul Jakmafb982c22007-05-04 20:15:47 +0000232 p.u.prefix6 = attr->extra->mp_nexthop_global;
paul718e3742002-12-13 20:15:29 +0000233
234 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000235 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
paul718e3742002-12-13 20:15:29 +0000236
237 if (rn->info)
238 {
239 bnc = rn->info;
240 bgp_unlock_node (rn);
241 }
242 else
243 {
Denis Ovsienko15b1d9d2011-08-08 19:36:44 +0400244 if (NULL == (bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global)))
245 bnc = bnc_new ();
246 else
paul718e3742002-12-13 20:15:29 +0000247 {
paul718e3742002-12-13 20:15:29 +0000248 if (changed)
249 {
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400250 struct bgp_table *old;
251 struct bgp_node *oldrn;
252
paul59320202004-11-09 01:54:03 +0000253 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
254 old = cache2_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000255 else
paul59320202004-11-09 01:54:03 +0000256 old = cache1_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000257
258 oldrn = bgp_node_lookup (old, &p);
259 if (oldrn)
260 {
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400261 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000262
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400263 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000264
265 if (bnc->metric != oldbnc->metric)
266 bnc->metricchanged = 1;
267 }
268 }
269 }
paul718e3742002-12-13 20:15:29 +0000270 rn->info = bnc;
271 }
272
273 if (changed)
274 *changed = bnc->changed;
275
276 if (metricchanged)
277 *metricchanged = bnc->metricchanged;
278
Paul Jakmafb982c22007-05-04 20:15:47 +0000279 if (bnc->valid && bnc->metric)
280 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
281 else if (ri->extra)
282 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000283
284 return bnc->valid;
285}
286#endif /* HAVE_IPV6 */
287
288/* Check specified next-hop is reachable or not. */
289int
290bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
291 int *changed, int *metricchanged)
292{
293 struct bgp_node *rn;
294 struct prefix p;
295 struct bgp_nexthop_cache *bnc;
296 struct in_addr addr;
297
paul718e3742002-12-13 20:15:29 +0000298#ifdef HAVE_IPV6
299 if (afi == AFI_IP6)
300 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
301#endif /* HAVE_IPV6 */
302
303 addr = ri->attr->nexthop;
304
305 memset (&p, 0, sizeof (struct prefix));
306 p.family = AF_INET;
307 p.prefixlen = IPV4_MAX_BITLEN;
308 p.u.prefix4 = addr;
309
310 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000311 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000312
313 if (rn->info)
314 {
315 bnc = rn->info;
316 bgp_unlock_node (rn);
317 }
318 else
319 {
Denis Ovsienko15b1d9d2011-08-08 19:36:44 +0400320 if (NULL == (bnc = zlookup_query (addr)))
321 bnc = bnc_new ();
322 else
paul718e3742002-12-13 20:15:29 +0000323 {
paul718e3742002-12-13 20:15:29 +0000324 if (changed)
325 {
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400326 struct bgp_table *old;
327 struct bgp_node *oldrn;
328
paul59320202004-11-09 01:54:03 +0000329 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
330 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000331 else
paul59320202004-11-09 01:54:03 +0000332 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000333
334 oldrn = bgp_node_lookup (old, &p);
335 if (oldrn)
336 {
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400337 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000338
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400339 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000340
341 if (bnc->metric != oldbnc->metric)
342 bnc->metricchanged = 1;
343 }
344 }
345 }
paul718e3742002-12-13 20:15:29 +0000346 rn->info = bnc;
347 }
348
349 if (changed)
350 *changed = bnc->changed;
351
352 if (metricchanged)
353 *metricchanged = bnc->metricchanged;
354
Paul Jakmafb982c22007-05-04 20:15:47 +0000355 if (bnc->valid && bnc->metric)
356 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
357 else if (ri->extra)
358 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000359
360 return bnc->valid;
361}
362
363/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000364static void
paul718e3742002-12-13 20:15:29 +0000365bgp_nexthop_cache_reset (struct bgp_table *table)
366{
367 struct bgp_node *rn;
368 struct bgp_nexthop_cache *bnc;
369
370 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
371 if ((bnc = rn->info) != NULL)
372 {
373 bnc_free (bnc);
374 rn->info = NULL;
375 bgp_unlock_node (rn);
376 }
377}
378
paul94f2b392005-06-28 12:44:16 +0000379static void
paul59320202004-11-09 01:54:03 +0000380bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000381{
382 struct bgp_node *rn;
383 struct bgp *bgp;
384 struct bgp_info *bi;
385 struct bgp_info *next;
386 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000387 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000388 int valid;
389 int current;
390 int changed;
391 int metricchanged;
392
393 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000394 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
395 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000396 else
paul59320202004-11-09 01:54:03 +0000397 bgp_nexthop_cache_table[afi] = cache1_table[afi];
paul718e3742002-12-13 20:15:29 +0000398
399 /* Get default bgp. */
400 bgp = bgp_get_default ();
401 if (bgp == NULL)
402 return;
403
404 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000405 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000406 {
407 if (peer->status != Established)
408 continue;
409
paul59320202004-11-09 01:54:03 +0000410 if (peer->afc[afi][SAFI_UNICAST])
411 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
412 if (peer->afc[afi][SAFI_MULTICAST])
413 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
414 if (peer->afc[afi][SAFI_MPLS_VPN])
415 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
paul718e3742002-12-13 20:15:29 +0000416 }
417
paul59320202004-11-09 01:54:03 +0000418 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000419 rn = bgp_route_next (rn))
420 {
421 for (bi = rn->info; bi; bi = next)
422 {
423 next = bi->next;
424
425 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
426 {
427 changed = 0;
428 metricchanged = 0;
429
430 if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400431 valid = bgp_nexthop_onlink (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000432 else
paul59320202004-11-09 01:54:03 +0000433 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000434 &changed, &metricchanged);
435
436 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
437
438 if (changed)
439 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
440 else
441 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
442
443 if (valid != current)
444 {
445 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
446 {
447 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000448 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000449 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000450 }
451 else
452 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000453 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000454 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000455 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000456 }
457 }
458
paul59320202004-11-09 01:54:03 +0000459 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000460 BGP_CONFIG_DAMPENING)
Paul Jakmafb982c22007-05-04 20:15:47 +0000461 && bi->extra && bi->extra->damp_info )
paul59320202004-11-09 01:54:03 +0000462 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000463 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000464 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000465 }
466 }
paul59320202004-11-09 01:54:03 +0000467 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000468 }
469
470 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000471 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
472 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000473 else
paul59320202004-11-09 01:54:03 +0000474 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000475
476 if (BGP_DEBUG (events, EVENTS))
477 {
478 if (afi == AFI_IP)
479 zlog_debug ("scanning IPv4 Unicast routing tables");
480 else if (afi == AFI_IP6)
481 zlog_debug ("scanning IPv6 Unicast routing tables");
482 }
paul718e3742002-12-13 20:15:29 +0000483}
484
paul718e3742002-12-13 20:15:29 +0000485/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000486static int
paul59320202004-11-09 01:54:03 +0000487bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000488{
489 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +0000490 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000491
hassof4184462005-02-01 20:13:16 +0000492 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000493 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000494
paul59320202004-11-09 01:54:03 +0000495 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000496
497#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000498 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000499#endif /* HAVE_IPV6 */
500
501 return 0;
502}
503
paul59320202004-11-09 01:54:03 +0000504struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000505{
506 unsigned int refcnt;
507};
508
509void
510bgp_connected_add (struct connected *ifc)
511{
512 struct prefix p;
513 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000514 struct interface *ifp;
515 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000516 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000517
518 ifp = ifc->ifp;
519
520 if (! ifp)
521 return;
522
523 if (if_is_loopback (ifp))
524 return;
525
526 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000527
528 if (addr->family == AF_INET)
529 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000530 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000531 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
532
533 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
534 return;
535
paul59320202004-11-09 01:54:03 +0000536 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000537 if (rn->info)
538 {
539 bc = rn->info;
540 bc->refcnt++;
541 }
542 else
543 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700544 bc = XCALLOC (0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000545 bc->refcnt = 1;
546 rn->info = bc;
547 }
548 }
549#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000550 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000551 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000552 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000553 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
554
555 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
556 return;
557
558 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
559 return;
560
paul59320202004-11-09 01:54:03 +0000561 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000562 if (rn->info)
563 {
564 bc = rn->info;
565 bc->refcnt++;
566 }
567 else
568 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700569 bc = XCALLOC (0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000570 bc->refcnt = 1;
571 rn->info = bc;
572 }
573 }
574#endif /* HAVE_IPV6 */
575}
576
577void
578bgp_connected_delete (struct connected *ifc)
579{
580 struct prefix p;
581 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000582 struct interface *ifp;
583 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000584 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000585
586 ifp = ifc->ifp;
587
588 if (if_is_loopback (ifp))
589 return;
590
591 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000592
593 if (addr->family == AF_INET)
594 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000595 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000596 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
597
598 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
599 return;
600
paul59320202004-11-09 01:54:03 +0000601 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000602 if (! rn)
603 return;
604
605 bc = rn->info;
606 bc->refcnt--;
607 if (bc->refcnt == 0)
608 {
609 XFREE (0, bc);
610 rn->info = NULL;
611 }
612 bgp_unlock_node (rn);
613 bgp_unlock_node (rn);
614 }
615#ifdef HAVE_IPV6
616 else if (addr->family == AF_INET6)
617 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000618 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000619 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
620
621 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
622 return;
623
624 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
625 return;
626
paul59320202004-11-09 01:54:03 +0000627 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000628 if (! rn)
629 return;
630
631 bc = rn->info;
632 bc->refcnt--;
633 if (bc->refcnt == 0)
634 {
635 XFREE (0, bc);
636 rn->info = NULL;
637 }
638 bgp_unlock_node (rn);
639 bgp_unlock_node (rn);
640 }
641#endif /* HAVE_IPV6 */
642}
643
644int
645bgp_nexthop_self (afi_t afi, struct attr *attr)
646{
hasso52dc7ee2004-09-23 19:18:23 +0000647 struct listnode *node;
648 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000649 struct interface *ifp;
650 struct connected *ifc;
651 struct prefix *p;
652
paul1eb8ef22005-04-07 07:30:20 +0000653 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000654 {
paul1eb8ef22005-04-07 07:30:20 +0000655 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000656 {
paul718e3742002-12-13 20:15:29 +0000657 p = ifc->address;
658
659 if (p && p->family == AF_INET
660 && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
661 return 1;
662 }
663 }
664 return 0;
665}
666
paul94f2b392005-06-28 12:44:16 +0000667static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800668zlookup_read (void)
paul718e3742002-12-13 20:15:29 +0000669{
670 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000671 uint16_t length;
672 u_char marker;
673 u_char version;
674 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000675 int nbytes;
676 struct in_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000677 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000678 int i;
679 u_char nexthop_num;
680 struct nexthop *nexthop;
681 struct bgp_nexthop_cache *bnc;
682
683 s = zlookup->ibuf;
684 stream_reset (s);
685
686 nbytes = stream_read (s, zlookup->sock, 2);
687 length = stream_getw (s);
688
689 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000690 marker = stream_getc (s);
691 version = stream_getc (s);
692
693 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
694 {
695 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
696 __func__, zlookup->sock, marker, version);
697 return NULL;
698 }
699
700 command = stream_getw (s);
701
paul718e3742002-12-13 20:15:29 +0000702 raddr.s_addr = stream_get_ipv4 (s);
703 metric = stream_getl (s);
704 nexthop_num = stream_getc (s);
705
706 if (nexthop_num)
707 {
708 bnc = bnc_new ();
709 bnc->valid = 1;
710 bnc->metric = metric;
711 bnc->nexthop_num = nexthop_num;
712
713 for (i = 0; i < nexthop_num; i++)
714 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700715 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000716 nexthop->type = stream_getc (s);
717 switch (nexthop->type)
718 {
719 case ZEBRA_NEXTHOP_IPV4:
720 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
721 break;
722 case ZEBRA_NEXTHOP_IFINDEX:
723 case ZEBRA_NEXTHOP_IFNAME:
724 nexthop->ifindex = stream_getl (s);
725 break;
hassofa2b17e2004-03-04 17:45:00 +0000726 default:
727 /* do nothing */
728 break;
paul718e3742002-12-13 20:15:29 +0000729 }
730 bnc_nexthop_add (bnc, nexthop);
731 }
732 }
733 else
734 return NULL;
735
736 return bnc;
737}
738
739struct bgp_nexthop_cache *
740zlookup_query (struct in_addr addr)
741{
742 int ret;
743 struct stream *s;
744
745 /* Check socket. */
746 if (zlookup->sock < 0)
747 return NULL;
748
749 s = zlookup->obuf;
750 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000751 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000752 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000753
754 stream_putw_at (s, 0, stream_get_endp (s));
755
756 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000757 if (ret < 0)
758 {
759 zlog_err ("can't write to zlookup->sock");
760 close (zlookup->sock);
761 zlookup->sock = -1;
762 return NULL;
763 }
764 if (ret == 0)
765 {
766 zlog_err ("zlookup->sock connection closed");
767 close (zlookup->sock);
768 zlookup->sock = -1;
769 return NULL;
770 }
771
772 return zlookup_read ();
773}
774
775#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000776static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800777zlookup_read_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000778{
779 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000780 uint16_t length;
781 u_char version, marker;
782 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000783 int nbytes;
784 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000785 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000786 int i;
787 u_char nexthop_num;
788 struct nexthop *nexthop;
789 struct bgp_nexthop_cache *bnc;
790
791 s = zlookup->ibuf;
792 stream_reset (s);
793
794 nbytes = stream_read (s, zlookup->sock, 2);
795 length = stream_getw (s);
796
797 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000798 marker = stream_getc (s);
799 version = stream_getc (s);
800
801 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
802 {
803 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
804 __func__, zlookup->sock, marker, version);
805 return NULL;
806 }
807
808 command = stream_getw (s);
809
paul718e3742002-12-13 20:15:29 +0000810 stream_get (&raddr, s, 16);
811
812 metric = stream_getl (s);
813 nexthop_num = stream_getc (s);
814
815 if (nexthop_num)
816 {
817 bnc = bnc_new ();
818 bnc->valid = 1;
819 bnc->metric = metric;
820 bnc->nexthop_num = nexthop_num;
821
822 for (i = 0; i < nexthop_num; i++)
823 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700824 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000825 nexthop->type = stream_getc (s);
826 switch (nexthop->type)
827 {
828 case ZEBRA_NEXTHOP_IPV6:
829 stream_get (&nexthop->gate.ipv6, s, 16);
830 break;
831 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
832 case ZEBRA_NEXTHOP_IPV6_IFNAME:
833 stream_get (&nexthop->gate.ipv6, s, 16);
834 nexthop->ifindex = stream_getl (s);
835 break;
836 case ZEBRA_NEXTHOP_IFINDEX:
837 case ZEBRA_NEXTHOP_IFNAME:
838 nexthop->ifindex = stream_getl (s);
839 break;
hassofa2b17e2004-03-04 17:45:00 +0000840 default:
841 /* do nothing */
842 break;
paul718e3742002-12-13 20:15:29 +0000843 }
844 bnc_nexthop_add (bnc, nexthop);
845 }
846 }
847 else
848 return NULL;
849
850 return bnc;
851}
852
853struct bgp_nexthop_cache *
854zlookup_query_ipv6 (struct in6_addr *addr)
855{
856 int ret;
857 struct stream *s;
858
859 /* Check socket. */
860 if (zlookup->sock < 0)
861 return NULL;
862
863 s = zlookup->obuf;
864 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000865 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000866 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000867 stream_putw_at (s, 0, stream_get_endp (s));
868
869 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000870 if (ret < 0)
871 {
872 zlog_err ("can't write to zlookup->sock");
873 close (zlookup->sock);
874 zlookup->sock = -1;
875 return NULL;
876 }
877 if (ret == 0)
878 {
879 zlog_err ("zlookup->sock connection closed");
880 close (zlookup->sock);
881 zlookup->sock = -1;
882 return NULL;
883 }
884
885 return zlookup_read_ipv6 ();
886}
887#endif /* HAVE_IPV6 */
888
paul94f2b392005-06-28 12:44:16 +0000889static int
890bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
891 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000892{
893 struct stream *s;
894 int ret;
pauld3092e72006-01-17 17:33:46 +0000895 u_int16_t length, command;
896 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000897 int nbytes;
898 struct in_addr addr;
899 struct in_addr nexthop;
900 u_int32_t metric = 0;
901 u_char nexthop_num;
902 u_char nexthop_type;
903
904 /* If lookup connection is not available return valid. */
905 if (zlookup->sock < 0)
906 {
907 if (igpmetric)
908 *igpmetric = 0;
909 return 1;
910 }
911
912 /* Send query to the lookup connection */
913 s = zlookup->obuf;
914 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000915 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
916
paul718e3742002-12-13 20:15:29 +0000917 stream_putc (s, p->prefixlen);
918 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +0000919
920 stream_putw_at (s, 0, stream_get_endp (s));
921
paul718e3742002-12-13 20:15:29 +0000922 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +0000923 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000924
925 if (ret < 0)
926 {
927 zlog_err ("can't write to zlookup->sock");
928 close (zlookup->sock);
929 zlookup->sock = -1;
930 return 1;
931 }
932 if (ret == 0)
933 {
934 zlog_err ("zlookup->sock connection closed");
935 close (zlookup->sock);
936 zlookup->sock = -1;
937 return 1;
938 }
939
940 /* Get result. */
941 stream_reset (s);
942
943 /* Fetch length. */
944 nbytes = stream_read (s, zlookup->sock, 2);
945 length = stream_getw (s);
946
947 /* Fetch whole data. */
948 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000949 marker = stream_getc (s);
950 version = stream_getc (s);
951
952 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
953 {
954 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
955 __func__, zlookup->sock, marker, version);
956 return 0;
957 }
958
959 command = stream_getw (s);
960
paul718e3742002-12-13 20:15:29 +0000961 addr.s_addr = stream_get_ipv4 (s);
962 metric = stream_getl (s);
963 nexthop_num = stream_getc (s);
964
965 /* Set IGP metric value. */
966 if (igpmetric)
967 *igpmetric = metric;
968
969 /* If there is nexthop then this is active route. */
970 if (nexthop_num)
971 {
972 nexthop.s_addr = 0;
973 nexthop_type = stream_getc (s);
974 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
975 {
976 nexthop.s_addr = stream_get_ipv4 (s);
977 if (igpnexthop)
978 *igpnexthop = nexthop;
979 }
980 else
981 *igpnexthop = nexthop;
982
983 return 1;
984 }
985 else
986 return 0;
987}
988
989/* Scan all configured BGP route then check the route exists in IGP or
990 not. */
paul94f2b392005-06-28 12:44:16 +0000991static int
paul718e3742002-12-13 20:15:29 +0000992bgp_import (struct thread *t)
993{
994 struct bgp *bgp;
995 struct bgp_node *rn;
996 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +0000997 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000998 int valid;
999 u_int32_t metric;
1000 struct in_addr nexthop;
1001 afi_t afi;
1002 safi_t safi;
1003
1004 bgp_import_thread =
1005 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1006
hassof4184462005-02-01 20:13:16 +00001007 if (BGP_DEBUG (events, EVENTS))
1008 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001009
paul1eb8ef22005-04-07 07:30:20 +00001010 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001011 {
1012 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1013 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1014 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1015 rn = bgp_route_next (rn))
1016 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001017 {
paul6cbbc3c2003-04-28 17:11:02 +00001018 if (bgp_static->backdoor)
1019 continue;
paul718e3742002-12-13 20:15:29 +00001020
paul6cbbc3c2003-04-28 17:11:02 +00001021 valid = bgp_static->valid;
1022 metric = bgp_static->igpmetric;
1023 nexthop = bgp_static->igpnexthop;
1024
1025 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1026 && afi == AFI_IP && safi == SAFI_UNICAST)
1027 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1028 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001029 else
paul6cbbc3c2003-04-28 17:11:02 +00001030 {
1031 bgp_static->valid = 1;
1032 bgp_static->igpmetric = 0;
1033 bgp_static->igpnexthop.s_addr = 0;
1034 }
1035
1036 if (bgp_static->valid != valid)
1037 {
1038 if (bgp_static->valid)
1039 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1040 else
1041 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1042 }
1043 else if (bgp_static->valid)
1044 {
1045 if (bgp_static->igpmetric != metric
1046 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1047 || bgp_static->rmap.name)
1048 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1049 }
paul718e3742002-12-13 20:15:29 +00001050 }
paul6cbbc3c2003-04-28 17:11:02 +00001051 }
paul718e3742002-12-13 20:15:29 +00001052 return 0;
1053}
1054
1055/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001056static int
paul718e3742002-12-13 20:15:29 +00001057zlookup_connect (struct thread *t)
1058{
1059 struct zclient *zlookup;
1060
1061 zlookup = THREAD_ARG (t);
1062 zlookup->t_connect = NULL;
1063
1064 if (zlookup->sock != -1)
1065 return 0;
1066
1067#ifdef HAVE_TCP_ZEBRA
1068 zlookup->sock = zclient_socket ();
1069#else
1070 zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH);
1071#endif /* HAVE_TCP_ZEBRA */
1072 if (zlookup->sock < 0)
1073 return -1;
1074
paul718e3742002-12-13 20:15:29 +00001075 return 0;
1076}
1077
1078/* Check specified multiaccess next-hop. */
1079int
1080bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1081{
1082 struct bgp_node *rn1;
1083 struct bgp_node *rn2;
1084 struct prefix p1;
1085 struct prefix p2;
1086 struct in_addr addr;
1087 int ret;
1088
1089 ret = inet_aton (peer, &addr);
1090 if (! ret)
1091 return 0;
1092
1093 memset (&p1, 0, sizeof (struct prefix));
1094 p1.family = AF_INET;
1095 p1.prefixlen = IPV4_MAX_BITLEN;
1096 p1.u.prefix4 = nexthop;
1097 memset (&p2, 0, sizeof (struct prefix));
1098 p2.family = AF_INET;
1099 p2.prefixlen = IPV4_MAX_BITLEN;
1100 p2.u.prefix4 = addr;
1101
1102 /* If bgp scan is not enabled, return invalid. */
1103 if (zlookup->sock < 0)
1104 return 0;
1105
paul59320202004-11-09 01:54:03 +00001106 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001107 if (! rn1)
1108 return 0;
1109
paul59320202004-11-09 01:54:03 +00001110 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001111 if (! rn2)
1112 return 0;
1113
1114 if (rn1 == rn2)
1115 return 1;
1116
1117 return 0;
1118}
1119
1120DEFUN (bgp_scan_time,
1121 bgp_scan_time_cmd,
1122 "bgp scan-time <5-60>",
1123 "BGP specific commands\n"
1124 "Configure background scanner interval\n"
1125 "Scanner interval (seconds)\n")
1126{
1127 bgp_scan_interval = atoi (argv[0]);
1128
1129 if (bgp_scan_thread)
1130 {
1131 thread_cancel (bgp_scan_thread);
1132 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001133 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001134 }
1135
1136 return CMD_SUCCESS;
1137}
1138
1139DEFUN (no_bgp_scan_time,
1140 no_bgp_scan_time_cmd,
1141 "no bgp scan-time",
1142 NO_STR
1143 "BGP specific commands\n"
1144 "Configure background scanner interval\n")
1145{
1146 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1147
1148 if (bgp_scan_thread)
1149 {
1150 thread_cancel (bgp_scan_thread);
1151 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001152 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001153 }
1154
1155 return CMD_SUCCESS;
1156}
1157
1158ALIAS (no_bgp_scan_time,
1159 no_bgp_scan_time_val_cmd,
1160 "no bgp scan-time <5-60>",
1161 NO_STR
1162 "BGP specific commands\n"
1163 "Configure background scanner interval\n"
1164 "Scanner interval (seconds)\n")
1165
Denis Ovsienko59902812011-08-05 21:47:08 +04001166static int
1167show_ip_bgp_scan_tables (struct vty *vty, const char detail)
paul718e3742002-12-13 20:15:29 +00001168{
1169 struct bgp_node *rn;
1170 struct bgp_nexthop_cache *bnc;
Denis Ovsienko59902812011-08-05 21:47:08 +04001171 char buf[INET6_ADDRSTRLEN];
1172 u_char i;
paul718e3742002-12-13 20:15:29 +00001173
1174 if (bgp_scan_thread)
1175 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1176 else
1177 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1178 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1179
1180 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001181 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001182 if ((bnc = rn->info) != NULL)
1183 {
1184 if (bnc->valid)
Denis Ovsienko59902812011-08-05 21:47:08 +04001185 {
paul718e3742002-12-13 20:15:29 +00001186 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko59902812011-08-05 21:47:08 +04001187 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1188 if (detail)
1189 for (i = 0; i < bnc->nexthop_num; i++)
Denis Ovsienko22aefb22011-08-09 14:42:58 +04001190 switch (bnc->nexthop[i].type)
1191 {
1192 case NEXTHOP_TYPE_IPV4:
1193 vty_out (vty, " gate %s%s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1194 break;
1195 case NEXTHOP_TYPE_IFINDEX:
1196 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1197 break;
1198 default:
1199 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1200 }
Denis Ovsienko59902812011-08-05 21:47:08 +04001201 }
paul718e3742002-12-13 20:15:29 +00001202 else
1203 vty_out (vty, " %s invalid%s",
Denis Ovsienko59902812011-08-05 21:47:08 +04001204 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001205 }
1206
1207#ifdef HAVE_IPV6
1208 {
paul59320202004-11-09 01:54:03 +00001209 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1210 rn;
1211 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001212 if ((bnc = rn->info) != NULL)
1213 {
1214 if (bnc->valid)
Denis Ovsienko59902812011-08-05 21:47:08 +04001215 {
paul718e3742002-12-13 20:15:29 +00001216 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko59902812011-08-05 21:47:08 +04001217 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001218 bnc->metric, VTY_NEWLINE);
Denis Ovsienko59902812011-08-05 21:47:08 +04001219 if (detail)
1220 for (i = 0; i < bnc->nexthop_num; i++)
Denis Ovsienko22aefb22011-08-09 14:42:58 +04001221 switch (bnc->nexthop[i].type)
1222 {
1223 case NEXTHOP_TYPE_IPV6:
1224 vty_out (vty, " gate %s%s", inet_ntop (AF_INET6, &bnc->nexthop[i].gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1225 break;
1226 case NEXTHOP_TYPE_IFINDEX:
1227 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1228 break;
1229 default:
1230 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1231 }
Denis Ovsienko59902812011-08-05 21:47:08 +04001232 }
paul718e3742002-12-13 20:15:29 +00001233 else
1234 vty_out (vty, " %s invalid%s",
Denis Ovsienko59902812011-08-05 21:47:08 +04001235 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001236 VTY_NEWLINE);
1237 }
1238 }
1239#endif /* HAVE_IPV6 */
1240
1241 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001242 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1243 rn;
1244 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001245 if (rn->info != NULL)
1246 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1247 VTY_NEWLINE);
1248
1249#ifdef HAVE_IPV6
1250 {
paul59320202004-11-09 01:54:03 +00001251 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1252 rn;
1253 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001254 if (rn->info != NULL)
1255 vty_out (vty, " %s/%d%s",
Denis Ovsienko59902812011-08-05 21:47:08 +04001256 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001257 rn->p.prefixlen,
1258 VTY_NEWLINE);
1259 }
1260#endif /* HAVE_IPV6 */
1261
1262 return CMD_SUCCESS;
1263}
1264
Denis Ovsienko59902812011-08-05 21:47:08 +04001265DEFUN (show_ip_bgp_scan,
1266 show_ip_bgp_scan_cmd,
1267 "show ip bgp scan",
1268 SHOW_STR
1269 IP_STR
1270 BGP_STR
1271 "BGP scan status\n")
1272{
1273 return show_ip_bgp_scan_tables (vty, 0);
1274}
1275
1276DEFUN (show_ip_bgp_scan_detail,
1277 show_ip_bgp_scan_detail_cmd,
1278 "show ip bgp scan detail",
1279 SHOW_STR
1280 IP_STR
1281 BGP_STR
1282 "BGP scan status\n"
1283 "More detailed output\n")
1284{
1285 return show_ip_bgp_scan_tables (vty, 1);
1286}
1287
paul718e3742002-12-13 20:15:29 +00001288int
1289bgp_config_write_scan_time (struct vty *vty)
1290{
1291 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1292 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1293 return CMD_SUCCESS;
1294}
1295
1296void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -08001297bgp_scan_init (void)
paul718e3742002-12-13 20:15:29 +00001298{
1299 zlookup = zclient_new ();
1300 zlookup->sock = -1;
paul718e3742002-12-13 20:15:29 +00001301 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1302
1303 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1304 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1305
Paul Jakma64e580a2006-02-21 01:09:01 +00001306 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1307 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001308 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001309
Paul Jakma64e580a2006-02-21 01:09:01 +00001310 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001311
1312#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001313 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1314 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001315 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001316 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001317#endif /* HAVE_IPV6 */
1318
1319 /* Make BGP scan thread. */
paul59320202004-11-09 01:54:03 +00001320 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1321 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001322 /* Make BGP import there. */
1323 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001324
1325 install_element (BGP_NODE, &bgp_scan_time_cmd);
1326 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1327 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1328 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko59902812011-08-05 21:47:08 +04001329 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01001330 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
paul718e3742002-12-13 20:15:29 +00001331 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko59902812011-08-05 21:47:08 +04001332 install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
paul718e3742002-12-13 20:15:29 +00001333}
Chris Caputo228da422009-07-18 05:44:03 +00001334
1335void
1336bgp_scan_finish (void)
1337{
1338 bgp_table_unlock (cache1_table[AFI_IP]);
1339 cache1_table[AFI_IP] = NULL;
1340
1341 bgp_table_unlock (cache2_table[AFI_IP]);
1342 cache2_table[AFI_IP] = NULL;
1343
1344 bgp_table_unlock (bgp_connected_table[AFI_IP]);
1345 bgp_connected_table[AFI_IP] = NULL;
1346
1347#ifdef HAVE_IPV6
1348 bgp_table_unlock (cache1_table[AFI_IP6]);
1349 cache1_table[AFI_IP6] = NULL;
1350
1351 bgp_table_unlock (cache2_table[AFI_IP6]);
1352 cache2_table[AFI_IP6] = NULL;
1353
1354 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
1355 bgp_connected_table[AFI_IP6] = NULL;
1356#endif /* HAVE_IPV6 */
1357}