blob: cb5a0504d60f763a9b5733065195b59a976b835b [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
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 {
paul718e3742002-12-13 20:15:29 +0000259 if (changed)
260 {
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400261 struct bgp_table *old;
262 struct bgp_node *oldrn;
263
paul59320202004-11-09 01:54:03 +0000264 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
265 old = cache2_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000266 else
paul59320202004-11-09 01:54:03 +0000267 old = cache1_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000268
269 oldrn = bgp_node_lookup (old, &p);
270 if (oldrn)
271 {
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400272 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000273
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400274 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000275
276 if (bnc->metric != oldbnc->metric)
277 bnc->metricchanged = 1;
278 }
279 }
280 }
281 else
282 {
283 bnc = bnc_new ();
paul718e3742002-12-13 20:15:29 +0000284 }
285 rn->info = bnc;
286 }
287
288 if (changed)
289 *changed = bnc->changed;
290
291 if (metricchanged)
292 *metricchanged = bnc->metricchanged;
293
Paul Jakmafb982c22007-05-04 20:15:47 +0000294 if (bnc->valid && bnc->metric)
295 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
296 else if (ri->extra)
297 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000298
299 return bnc->valid;
300}
301#endif /* HAVE_IPV6 */
302
303/* Check specified next-hop is reachable or not. */
304int
305bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
306 int *changed, int *metricchanged)
307{
308 struct bgp_node *rn;
309 struct prefix p;
310 struct bgp_nexthop_cache *bnc;
311 struct in_addr addr;
312
313 /* If lookup is not enabled, return valid. */
314 if (zlookup->sock < 0)
315 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000316 if (ri->extra)
317 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000318 return 1;
319 }
320
321#ifdef HAVE_IPV6
322 if (afi == AFI_IP6)
323 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
324#endif /* HAVE_IPV6 */
325
326 addr = ri->attr->nexthop;
327
328 memset (&p, 0, sizeof (struct prefix));
329 p.family = AF_INET;
330 p.prefixlen = IPV4_MAX_BITLEN;
331 p.u.prefix4 = addr;
332
333 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000334 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000335
336 if (rn->info)
337 {
338 bnc = rn->info;
339 bgp_unlock_node (rn);
340 }
341 else
342 {
343 bnc = zlookup_query (addr);
344 if (bnc)
345 {
paul718e3742002-12-13 20:15:29 +0000346 if (changed)
347 {
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400348 struct bgp_table *old;
349 struct bgp_node *oldrn;
350
paul59320202004-11-09 01:54:03 +0000351 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
352 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000353 else
paul59320202004-11-09 01:54:03 +0000354 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000355
356 oldrn = bgp_node_lookup (old, &p);
357 if (oldrn)
358 {
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400359 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000360
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400361 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000362
363 if (bnc->metric != oldbnc->metric)
364 bnc->metricchanged = 1;
365 }
366 }
367 }
368 else
369 {
370 bnc = bnc_new ();
paul718e3742002-12-13 20:15:29 +0000371 }
372 rn->info = bnc;
373 }
374
375 if (changed)
376 *changed = bnc->changed;
377
378 if (metricchanged)
379 *metricchanged = bnc->metricchanged;
380
Paul Jakmafb982c22007-05-04 20:15:47 +0000381 if (bnc->valid && bnc->metric)
382 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
383 else if (ri->extra)
384 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000385
386 return bnc->valid;
387}
388
389/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000390static void
paul718e3742002-12-13 20:15:29 +0000391bgp_nexthop_cache_reset (struct bgp_table *table)
392{
393 struct bgp_node *rn;
394 struct bgp_nexthop_cache *bnc;
395
396 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
397 if ((bnc = rn->info) != NULL)
398 {
399 bnc_free (bnc);
400 rn->info = NULL;
401 bgp_unlock_node (rn);
402 }
403}
404
paul94f2b392005-06-28 12:44:16 +0000405static void
paul59320202004-11-09 01:54:03 +0000406bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000407{
408 struct bgp_node *rn;
409 struct bgp *bgp;
410 struct bgp_info *bi;
411 struct bgp_info *next;
412 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000413 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000414 int valid;
415 int current;
416 int changed;
417 int metricchanged;
418
419 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000420 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
421 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000422 else
paul59320202004-11-09 01:54:03 +0000423 bgp_nexthop_cache_table[afi] = cache1_table[afi];
paul718e3742002-12-13 20:15:29 +0000424
425 /* Get default bgp. */
426 bgp = bgp_get_default ();
427 if (bgp == NULL)
428 return;
429
430 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000431 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000432 {
433 if (peer->status != Established)
434 continue;
435
paul59320202004-11-09 01:54:03 +0000436 if (peer->afc[afi][SAFI_UNICAST])
437 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
438 if (peer->afc[afi][SAFI_MULTICAST])
439 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
440 if (peer->afc[afi][SAFI_MPLS_VPN])
441 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
paul718e3742002-12-13 20:15:29 +0000442 }
443
paul59320202004-11-09 01:54:03 +0000444 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000445 rn = bgp_route_next (rn))
446 {
447 for (bi = rn->info; bi; bi = next)
448 {
449 next = bi->next;
450
451 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
452 {
453 changed = 0;
454 metricchanged = 0;
455
456 if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
Denis Ovsienko5c98c5a2011-08-05 18:52:52 +0400457 valid = bgp_nexthop_onlink (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000458 else
paul59320202004-11-09 01:54:03 +0000459 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000460 &changed, &metricchanged);
461
462 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
463
464 if (changed)
465 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
466 else
467 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
468
469 if (valid != current)
470 {
471 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
472 {
473 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000474 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000475 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000476 }
477 else
478 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000479 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000480 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000481 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000482 }
483 }
484
paul59320202004-11-09 01:54:03 +0000485 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000486 BGP_CONFIG_DAMPENING)
Paul Jakmafb982c22007-05-04 20:15:47 +0000487 && bi->extra && bi->extra->damp_info )
paul59320202004-11-09 01:54:03 +0000488 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000489 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000490 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000491 }
492 }
paul59320202004-11-09 01:54:03 +0000493 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000494 }
495
496 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000497 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
498 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000499 else
paul59320202004-11-09 01:54:03 +0000500 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000501
502 if (BGP_DEBUG (events, EVENTS))
503 {
504 if (afi == AFI_IP)
505 zlog_debug ("scanning IPv4 Unicast routing tables");
506 else if (afi == AFI_IP6)
507 zlog_debug ("scanning IPv6 Unicast routing tables");
508 }
paul718e3742002-12-13 20:15:29 +0000509}
510
paul718e3742002-12-13 20:15:29 +0000511/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000512static int
paul59320202004-11-09 01:54:03 +0000513bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000514{
515 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +0000516 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000517
hassof4184462005-02-01 20:13:16 +0000518 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000519 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000520
paul59320202004-11-09 01:54:03 +0000521 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000522
523#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000524 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000525#endif /* HAVE_IPV6 */
526
527 return 0;
528}
529
paul59320202004-11-09 01:54:03 +0000530struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000531{
532 unsigned int refcnt;
533};
534
535void
536bgp_connected_add (struct connected *ifc)
537{
538 struct prefix p;
539 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000540 struct interface *ifp;
541 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000542 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000543
544 ifp = ifc->ifp;
545
546 if (! ifp)
547 return;
548
549 if (if_is_loopback (ifp))
550 return;
551
552 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000553
554 if (addr->family == AF_INET)
555 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000556 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000557 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
558
559 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
560 return;
561
paul59320202004-11-09 01:54:03 +0000562 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000563 if (rn->info)
564 {
565 bc = rn->info;
566 bc->refcnt++;
567 }
568 else
569 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700570 bc = XCALLOC (0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000571 bc->refcnt = 1;
572 rn->info = bc;
573 }
574 }
575#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000576 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000577 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000578 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000579 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
580
581 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
582 return;
583
584 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
585 return;
586
paul59320202004-11-09 01:54:03 +0000587 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000588 if (rn->info)
589 {
590 bc = rn->info;
591 bc->refcnt++;
592 }
593 else
594 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700595 bc = XCALLOC (0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000596 bc->refcnt = 1;
597 rn->info = bc;
598 }
599 }
600#endif /* HAVE_IPV6 */
601}
602
603void
604bgp_connected_delete (struct connected *ifc)
605{
606 struct prefix p;
607 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000608 struct interface *ifp;
609 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000610 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000611
612 ifp = ifc->ifp;
613
614 if (if_is_loopback (ifp))
615 return;
616
617 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000618
619 if (addr->family == AF_INET)
620 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000621 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000622 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
623
624 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
625 return;
626
paul59320202004-11-09 01:54:03 +0000627 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &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#ifdef HAVE_IPV6
642 else if (addr->family == AF_INET6)
643 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000644 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000645 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
646
647 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
648 return;
649
650 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
651 return;
652
paul59320202004-11-09 01:54:03 +0000653 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000654 if (! rn)
655 return;
656
657 bc = rn->info;
658 bc->refcnt--;
659 if (bc->refcnt == 0)
660 {
661 XFREE (0, bc);
662 rn->info = NULL;
663 }
664 bgp_unlock_node (rn);
665 bgp_unlock_node (rn);
666 }
667#endif /* HAVE_IPV6 */
668}
669
670int
671bgp_nexthop_self (afi_t afi, struct attr *attr)
672{
hasso52dc7ee2004-09-23 19:18:23 +0000673 struct listnode *node;
674 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000675 struct interface *ifp;
676 struct connected *ifc;
677 struct prefix *p;
678
paul1eb8ef22005-04-07 07:30:20 +0000679 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000680 {
paul1eb8ef22005-04-07 07:30:20 +0000681 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000682 {
paul718e3742002-12-13 20:15:29 +0000683 p = ifc->address;
684
685 if (p && p->family == AF_INET
686 && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
687 return 1;
688 }
689 }
690 return 0;
691}
692
paul94f2b392005-06-28 12:44:16 +0000693static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800694zlookup_read (void)
paul718e3742002-12-13 20:15:29 +0000695{
696 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000697 uint16_t length;
698 u_char marker;
699 u_char version;
700 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000701 int nbytes;
702 struct in_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000703 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000704 int i;
705 u_char nexthop_num;
706 struct nexthop *nexthop;
707 struct bgp_nexthop_cache *bnc;
708
709 s = zlookup->ibuf;
710 stream_reset (s);
711
712 nbytes = stream_read (s, zlookup->sock, 2);
713 length = stream_getw (s);
714
715 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000716 marker = stream_getc (s);
717 version = stream_getc (s);
718
719 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
720 {
721 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
722 __func__, zlookup->sock, marker, version);
723 return NULL;
724 }
725
726 command = stream_getw (s);
727
paul718e3742002-12-13 20:15:29 +0000728 raddr.s_addr = stream_get_ipv4 (s);
729 metric = stream_getl (s);
730 nexthop_num = stream_getc (s);
731
732 if (nexthop_num)
733 {
734 bnc = bnc_new ();
735 bnc->valid = 1;
736 bnc->metric = metric;
737 bnc->nexthop_num = nexthop_num;
738
739 for (i = 0; i < nexthop_num; i++)
740 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700741 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000742 nexthop->type = stream_getc (s);
743 switch (nexthop->type)
744 {
745 case ZEBRA_NEXTHOP_IPV4:
746 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
747 break;
748 case ZEBRA_NEXTHOP_IFINDEX:
749 case ZEBRA_NEXTHOP_IFNAME:
750 nexthop->ifindex = stream_getl (s);
751 break;
hassofa2b17e2004-03-04 17:45:00 +0000752 default:
753 /* do nothing */
754 break;
paul718e3742002-12-13 20:15:29 +0000755 }
756 bnc_nexthop_add (bnc, nexthop);
757 }
758 }
759 else
760 return NULL;
761
762 return bnc;
763}
764
765struct bgp_nexthop_cache *
766zlookup_query (struct in_addr addr)
767{
768 int ret;
769 struct stream *s;
770
771 /* Check socket. */
772 if (zlookup->sock < 0)
773 return NULL;
774
775 s = zlookup->obuf;
776 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000777 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000778 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000779
780 stream_putw_at (s, 0, stream_get_endp (s));
781
782 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000783 if (ret < 0)
784 {
785 zlog_err ("can't write to zlookup->sock");
786 close (zlookup->sock);
787 zlookup->sock = -1;
788 return NULL;
789 }
790 if (ret == 0)
791 {
792 zlog_err ("zlookup->sock connection closed");
793 close (zlookup->sock);
794 zlookup->sock = -1;
795 return NULL;
796 }
797
798 return zlookup_read ();
799}
800
801#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000802static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800803zlookup_read_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000804{
805 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000806 uint16_t length;
807 u_char version, marker;
808 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000809 int nbytes;
810 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000811 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000812 int i;
813 u_char nexthop_num;
814 struct nexthop *nexthop;
815 struct bgp_nexthop_cache *bnc;
816
817 s = zlookup->ibuf;
818 stream_reset (s);
819
820 nbytes = stream_read (s, zlookup->sock, 2);
821 length = stream_getw (s);
822
823 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000824 marker = stream_getc (s);
825 version = stream_getc (s);
826
827 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
828 {
829 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
830 __func__, zlookup->sock, marker, version);
831 return NULL;
832 }
833
834 command = stream_getw (s);
835
paul718e3742002-12-13 20:15:29 +0000836 stream_get (&raddr, s, 16);
837
838 metric = stream_getl (s);
839 nexthop_num = stream_getc (s);
840
841 if (nexthop_num)
842 {
843 bnc = bnc_new ();
844 bnc->valid = 1;
845 bnc->metric = metric;
846 bnc->nexthop_num = nexthop_num;
847
848 for (i = 0; i < nexthop_num; i++)
849 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700850 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000851 nexthop->type = stream_getc (s);
852 switch (nexthop->type)
853 {
854 case ZEBRA_NEXTHOP_IPV6:
855 stream_get (&nexthop->gate.ipv6, s, 16);
856 break;
857 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
858 case ZEBRA_NEXTHOP_IPV6_IFNAME:
859 stream_get (&nexthop->gate.ipv6, s, 16);
860 nexthop->ifindex = stream_getl (s);
861 break;
862 case ZEBRA_NEXTHOP_IFINDEX:
863 case ZEBRA_NEXTHOP_IFNAME:
864 nexthop->ifindex = stream_getl (s);
865 break;
hassofa2b17e2004-03-04 17:45:00 +0000866 default:
867 /* do nothing */
868 break;
paul718e3742002-12-13 20:15:29 +0000869 }
870 bnc_nexthop_add (bnc, nexthop);
871 }
872 }
873 else
874 return NULL;
875
876 return bnc;
877}
878
879struct bgp_nexthop_cache *
880zlookup_query_ipv6 (struct in6_addr *addr)
881{
882 int ret;
883 struct stream *s;
884
885 /* Check socket. */
886 if (zlookup->sock < 0)
887 return NULL;
888
889 s = zlookup->obuf;
890 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000891 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000892 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000893 stream_putw_at (s, 0, stream_get_endp (s));
894
895 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000896 if (ret < 0)
897 {
898 zlog_err ("can't write to zlookup->sock");
899 close (zlookup->sock);
900 zlookup->sock = -1;
901 return NULL;
902 }
903 if (ret == 0)
904 {
905 zlog_err ("zlookup->sock connection closed");
906 close (zlookup->sock);
907 zlookup->sock = -1;
908 return NULL;
909 }
910
911 return zlookup_read_ipv6 ();
912}
913#endif /* HAVE_IPV6 */
914
paul94f2b392005-06-28 12:44:16 +0000915static int
916bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
917 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000918{
919 struct stream *s;
920 int ret;
pauld3092e72006-01-17 17:33:46 +0000921 u_int16_t length, command;
922 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000923 int nbytes;
924 struct in_addr addr;
925 struct in_addr nexthop;
926 u_int32_t metric = 0;
927 u_char nexthop_num;
928 u_char nexthop_type;
929
930 /* If lookup connection is not available return valid. */
931 if (zlookup->sock < 0)
932 {
933 if (igpmetric)
934 *igpmetric = 0;
935 return 1;
936 }
937
938 /* Send query to the lookup connection */
939 s = zlookup->obuf;
940 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000941 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
942
paul718e3742002-12-13 20:15:29 +0000943 stream_putc (s, p->prefixlen);
944 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +0000945
946 stream_putw_at (s, 0, stream_get_endp (s));
947
paul718e3742002-12-13 20:15:29 +0000948 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +0000949 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000950
951 if (ret < 0)
952 {
953 zlog_err ("can't write to zlookup->sock");
954 close (zlookup->sock);
955 zlookup->sock = -1;
956 return 1;
957 }
958 if (ret == 0)
959 {
960 zlog_err ("zlookup->sock connection closed");
961 close (zlookup->sock);
962 zlookup->sock = -1;
963 return 1;
964 }
965
966 /* Get result. */
967 stream_reset (s);
968
969 /* Fetch length. */
970 nbytes = stream_read (s, zlookup->sock, 2);
971 length = stream_getw (s);
972
973 /* Fetch whole data. */
974 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000975 marker = stream_getc (s);
976 version = stream_getc (s);
977
978 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
979 {
980 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
981 __func__, zlookup->sock, marker, version);
982 return 0;
983 }
984
985 command = stream_getw (s);
986
paul718e3742002-12-13 20:15:29 +0000987 addr.s_addr = stream_get_ipv4 (s);
988 metric = stream_getl (s);
989 nexthop_num = stream_getc (s);
990
991 /* Set IGP metric value. */
992 if (igpmetric)
993 *igpmetric = metric;
994
995 /* If there is nexthop then this is active route. */
996 if (nexthop_num)
997 {
998 nexthop.s_addr = 0;
999 nexthop_type = stream_getc (s);
1000 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
1001 {
1002 nexthop.s_addr = stream_get_ipv4 (s);
1003 if (igpnexthop)
1004 *igpnexthop = nexthop;
1005 }
1006 else
1007 *igpnexthop = nexthop;
1008
1009 return 1;
1010 }
1011 else
1012 return 0;
1013}
1014
1015/* Scan all configured BGP route then check the route exists in IGP or
1016 not. */
paul94f2b392005-06-28 12:44:16 +00001017static int
paul718e3742002-12-13 20:15:29 +00001018bgp_import (struct thread *t)
1019{
1020 struct bgp *bgp;
1021 struct bgp_node *rn;
1022 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001023 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001024 int valid;
1025 u_int32_t metric;
1026 struct in_addr nexthop;
1027 afi_t afi;
1028 safi_t safi;
1029
1030 bgp_import_thread =
1031 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1032
hassof4184462005-02-01 20:13:16 +00001033 if (BGP_DEBUG (events, EVENTS))
1034 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001035
paul1eb8ef22005-04-07 07:30:20 +00001036 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001037 {
1038 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1039 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1040 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1041 rn = bgp_route_next (rn))
1042 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001043 {
paul6cbbc3c2003-04-28 17:11:02 +00001044 if (bgp_static->backdoor)
1045 continue;
paul718e3742002-12-13 20:15:29 +00001046
paul6cbbc3c2003-04-28 17:11:02 +00001047 valid = bgp_static->valid;
1048 metric = bgp_static->igpmetric;
1049 nexthop = bgp_static->igpnexthop;
1050
1051 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1052 && afi == AFI_IP && safi == SAFI_UNICAST)
1053 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1054 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001055 else
paul6cbbc3c2003-04-28 17:11:02 +00001056 {
1057 bgp_static->valid = 1;
1058 bgp_static->igpmetric = 0;
1059 bgp_static->igpnexthop.s_addr = 0;
1060 }
1061
1062 if (bgp_static->valid != valid)
1063 {
1064 if (bgp_static->valid)
1065 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1066 else
1067 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1068 }
1069 else if (bgp_static->valid)
1070 {
1071 if (bgp_static->igpmetric != metric
1072 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1073 || bgp_static->rmap.name)
1074 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1075 }
paul718e3742002-12-13 20:15:29 +00001076 }
paul6cbbc3c2003-04-28 17:11:02 +00001077 }
paul718e3742002-12-13 20:15:29 +00001078 return 0;
1079}
1080
1081/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001082static int
paul718e3742002-12-13 20:15:29 +00001083zlookup_connect (struct thread *t)
1084{
1085 struct zclient *zlookup;
1086
1087 zlookup = THREAD_ARG (t);
1088 zlookup->t_connect = NULL;
1089
1090 if (zlookup->sock != -1)
1091 return 0;
1092
1093#ifdef HAVE_TCP_ZEBRA
1094 zlookup->sock = zclient_socket ();
1095#else
1096 zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH);
1097#endif /* HAVE_TCP_ZEBRA */
1098 if (zlookup->sock < 0)
1099 return -1;
1100
paul718e3742002-12-13 20:15:29 +00001101 return 0;
1102}
1103
1104/* Check specified multiaccess next-hop. */
1105int
1106bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1107{
1108 struct bgp_node *rn1;
1109 struct bgp_node *rn2;
1110 struct prefix p1;
1111 struct prefix p2;
1112 struct in_addr addr;
1113 int ret;
1114
1115 ret = inet_aton (peer, &addr);
1116 if (! ret)
1117 return 0;
1118
1119 memset (&p1, 0, sizeof (struct prefix));
1120 p1.family = AF_INET;
1121 p1.prefixlen = IPV4_MAX_BITLEN;
1122 p1.u.prefix4 = nexthop;
1123 memset (&p2, 0, sizeof (struct prefix));
1124 p2.family = AF_INET;
1125 p2.prefixlen = IPV4_MAX_BITLEN;
1126 p2.u.prefix4 = addr;
1127
1128 /* If bgp scan is not enabled, return invalid. */
1129 if (zlookup->sock < 0)
1130 return 0;
1131
paul59320202004-11-09 01:54:03 +00001132 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001133 if (! rn1)
1134 return 0;
1135
paul59320202004-11-09 01:54:03 +00001136 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001137 if (! rn2)
1138 return 0;
1139
1140 if (rn1 == rn2)
1141 return 1;
1142
1143 return 0;
1144}
1145
1146DEFUN (bgp_scan_time,
1147 bgp_scan_time_cmd,
1148 "bgp scan-time <5-60>",
1149 "BGP specific commands\n"
1150 "Configure background scanner interval\n"
1151 "Scanner interval (seconds)\n")
1152{
1153 bgp_scan_interval = atoi (argv[0]);
1154
1155 if (bgp_scan_thread)
1156 {
1157 thread_cancel (bgp_scan_thread);
1158 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001159 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001160 }
1161
1162 return CMD_SUCCESS;
1163}
1164
1165DEFUN (no_bgp_scan_time,
1166 no_bgp_scan_time_cmd,
1167 "no bgp scan-time",
1168 NO_STR
1169 "BGP specific commands\n"
1170 "Configure background scanner interval\n")
1171{
1172 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1173
1174 if (bgp_scan_thread)
1175 {
1176 thread_cancel (bgp_scan_thread);
1177 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001178 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001179 }
1180
1181 return CMD_SUCCESS;
1182}
1183
1184ALIAS (no_bgp_scan_time,
1185 no_bgp_scan_time_val_cmd,
1186 "no bgp scan-time <5-60>",
1187 NO_STR
1188 "BGP specific commands\n"
1189 "Configure background scanner interval\n"
1190 "Scanner interval (seconds)\n")
1191
Denis Ovsienko59902812011-08-05 21:47:08 +04001192static int
1193show_ip_bgp_scan_tables (struct vty *vty, const char detail)
paul718e3742002-12-13 20:15:29 +00001194{
1195 struct bgp_node *rn;
1196 struct bgp_nexthop_cache *bnc;
Denis Ovsienko59902812011-08-05 21:47:08 +04001197 char buf[INET6_ADDRSTRLEN];
1198 u_char i;
paul718e3742002-12-13 20:15:29 +00001199
1200 if (bgp_scan_thread)
1201 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1202 else
1203 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1204 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1205
1206 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001207 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001208 if ((bnc = rn->info) != NULL)
1209 {
1210 if (bnc->valid)
Denis Ovsienko59902812011-08-05 21:47:08 +04001211 {
paul718e3742002-12-13 20:15:29 +00001212 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko59902812011-08-05 21:47:08 +04001213 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1214 if (detail)
1215 for (i = 0; i < bnc->nexthop_num; i++)
1216 vty_out (vty, " %s%s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1217 }
paul718e3742002-12-13 20:15:29 +00001218 else
1219 vty_out (vty, " %s invalid%s",
Denis Ovsienko59902812011-08-05 21:47:08 +04001220 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001221 }
1222
1223#ifdef HAVE_IPV6
1224 {
paul59320202004-11-09 01:54:03 +00001225 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1226 rn;
1227 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001228 if ((bnc = rn->info) != NULL)
1229 {
1230 if (bnc->valid)
Denis Ovsienko59902812011-08-05 21:47:08 +04001231 {
paul718e3742002-12-13 20:15:29 +00001232 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko59902812011-08-05 21:47:08 +04001233 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001234 bnc->metric, VTY_NEWLINE);
Denis Ovsienko59902812011-08-05 21:47:08 +04001235 if (detail)
1236 for (i = 0; i < bnc->nexthop_num; i++)
1237 vty_out (vty, " %s%s", inet_ntop (AF_INET6, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1238 }
paul718e3742002-12-13 20:15:29 +00001239 else
1240 vty_out (vty, " %s invalid%s",
Denis Ovsienko59902812011-08-05 21:47:08 +04001241 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001242 VTY_NEWLINE);
1243 }
1244 }
1245#endif /* HAVE_IPV6 */
1246
1247 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001248 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1249 rn;
1250 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001251 if (rn->info != NULL)
1252 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1253 VTY_NEWLINE);
1254
1255#ifdef HAVE_IPV6
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",
Denis Ovsienko59902812011-08-05 21:47:08 +04001262 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001263 rn->p.prefixlen,
1264 VTY_NEWLINE);
1265 }
1266#endif /* HAVE_IPV6 */
1267
1268 return CMD_SUCCESS;
1269}
1270
Denis Ovsienko59902812011-08-05 21:47:08 +04001271DEFUN (show_ip_bgp_scan,
1272 show_ip_bgp_scan_cmd,
1273 "show ip bgp scan",
1274 SHOW_STR
1275 IP_STR
1276 BGP_STR
1277 "BGP scan status\n")
1278{
1279 return show_ip_bgp_scan_tables (vty, 0);
1280}
1281
1282DEFUN (show_ip_bgp_scan_detail,
1283 show_ip_bgp_scan_detail_cmd,
1284 "show ip bgp scan detail",
1285 SHOW_STR
1286 IP_STR
1287 BGP_STR
1288 "BGP scan status\n"
1289 "More detailed output\n")
1290{
1291 return show_ip_bgp_scan_tables (vty, 1);
1292}
1293
paul718e3742002-12-13 20:15:29 +00001294int
1295bgp_config_write_scan_time (struct vty *vty)
1296{
1297 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1298 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1299 return CMD_SUCCESS;
1300}
1301
1302void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -08001303bgp_scan_init (void)
paul718e3742002-12-13 20:15:29 +00001304{
1305 zlookup = zclient_new ();
1306 zlookup->sock = -1;
paul718e3742002-12-13 20:15:29 +00001307 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1308
1309 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1310 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1311
Paul Jakma64e580a2006-02-21 01:09:01 +00001312 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1313 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001314 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001315
Paul Jakma64e580a2006-02-21 01:09:01 +00001316 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001317
1318#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001319 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1320 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001321 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001322 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001323#endif /* HAVE_IPV6 */
1324
1325 /* Make BGP scan thread. */
paul59320202004-11-09 01:54:03 +00001326 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1327 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001328 /* Make BGP import there. */
1329 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001330
1331 install_element (BGP_NODE, &bgp_scan_time_cmd);
1332 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1333 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1334 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko59902812011-08-05 21:47:08 +04001335 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01001336 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
paul718e3742002-12-13 20:15:29 +00001337 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko59902812011-08-05 21:47:08 +04001338 install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
paul718e3742002-12-13 20:15:29 +00001339}
Chris Caputo228da422009-07-18 05:44:03 +00001340
1341void
1342bgp_scan_finish (void)
1343{
1344 bgp_table_unlock (cache1_table[AFI_IP]);
1345 cache1_table[AFI_IP] = NULL;
1346
1347 bgp_table_unlock (cache2_table[AFI_IP]);
1348 cache2_table[AFI_IP] = NULL;
1349
1350 bgp_table_unlock (bgp_connected_table[AFI_IP]);
1351 bgp_connected_table[AFI_IP] = NULL;
1352
1353#ifdef HAVE_IPV6
1354 bgp_table_unlock (cache1_table[AFI_IP6]);
1355 cache1_table[AFI_IP6] = NULL;
1356
1357 bgp_table_unlock (cache2_table[AFI_IP6]);
1358 cache2_table[AFI_IP6] = NULL;
1359
1360 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
1361 bgp_connected_table[AFI_IP6] = NULL;
1362#endif /* HAVE_IPV6 */
1363}