blob: dc448f9fa278c05ab97c1fb40572d1f25083e5ae [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 Ovsienko8e80bdf2011-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 Ovsienko8e80bdf2011-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 Ovsienkob64bfc12011-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 Ovsienko8e80bdf2011-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 Ovsienko8e80bdf2011-08-05 18:52:52 +0400261 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000262
Denis Ovsienko8e80bdf2011-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;
Chris Caputo6c88b442010-07-27 16:28:55 +0000267
268 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000269 }
270 }
271 }
paul718e3742002-12-13 20:15:29 +0000272 rn->info = bnc;
273 }
274
275 if (changed)
276 *changed = bnc->changed;
277
278 if (metricchanged)
279 *metricchanged = bnc->metricchanged;
280
Paul Jakmafb982c22007-05-04 20:15:47 +0000281 if (bnc->valid && bnc->metric)
282 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
283 else if (ri->extra)
284 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000285
286 return bnc->valid;
287}
288#endif /* HAVE_IPV6 */
289
290/* Check specified next-hop is reachable or not. */
291int
292bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
293 int *changed, int *metricchanged)
294{
295 struct bgp_node *rn;
296 struct prefix p;
297 struct bgp_nexthop_cache *bnc;
298 struct in_addr addr;
299
paul718e3742002-12-13 20:15:29 +0000300#ifdef HAVE_IPV6
301 if (afi == AFI_IP6)
302 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
303#endif /* HAVE_IPV6 */
304
305 addr = ri->attr->nexthop;
306
307 memset (&p, 0, sizeof (struct prefix));
308 p.family = AF_INET;
309 p.prefixlen = IPV4_MAX_BITLEN;
310 p.u.prefix4 = addr;
311
312 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000313 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000314
315 if (rn->info)
316 {
317 bnc = rn->info;
318 bgp_unlock_node (rn);
319 }
320 else
321 {
Denis Ovsienkob64bfc12011-08-08 19:36:44 +0400322 if (NULL == (bnc = zlookup_query (addr)))
323 bnc = bnc_new ();
324 else
paul718e3742002-12-13 20:15:29 +0000325 {
paul718e3742002-12-13 20:15:29 +0000326 if (changed)
327 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400328 struct bgp_table *old;
329 struct bgp_node *oldrn;
330
paul59320202004-11-09 01:54:03 +0000331 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
332 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000333 else
paul59320202004-11-09 01:54:03 +0000334 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000335
336 oldrn = bgp_node_lookup (old, &p);
337 if (oldrn)
338 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400339 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000340
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400341 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000342
343 if (bnc->metric != oldbnc->metric)
344 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000345
346 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000347 }
348 }
349 }
paul718e3742002-12-13 20:15:29 +0000350 rn->info = bnc;
351 }
352
353 if (changed)
354 *changed = bnc->changed;
355
356 if (metricchanged)
357 *metricchanged = bnc->metricchanged;
358
Paul Jakmafb982c22007-05-04 20:15:47 +0000359 if (bnc->valid && bnc->metric)
360 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
361 else if (ri->extra)
362 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000363
364 return bnc->valid;
365}
366
367/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000368static void
paul718e3742002-12-13 20:15:29 +0000369bgp_nexthop_cache_reset (struct bgp_table *table)
370{
371 struct bgp_node *rn;
372 struct bgp_nexthop_cache *bnc;
373
374 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
375 if ((bnc = rn->info) != NULL)
376 {
377 bnc_free (bnc);
378 rn->info = NULL;
379 bgp_unlock_node (rn);
380 }
381}
382
paul94f2b392005-06-28 12:44:16 +0000383static void
paul59320202004-11-09 01:54:03 +0000384bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000385{
386 struct bgp_node *rn;
387 struct bgp *bgp;
388 struct bgp_info *bi;
389 struct bgp_info *next;
390 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000391 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000392 int valid;
393 int current;
394 int changed;
395 int metricchanged;
396
397 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000398 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
399 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000400 else
paul59320202004-11-09 01:54:03 +0000401 bgp_nexthop_cache_table[afi] = cache1_table[afi];
paul718e3742002-12-13 20:15:29 +0000402
403 /* Get default bgp. */
404 bgp = bgp_get_default ();
405 if (bgp == NULL)
406 return;
407
408 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000409 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000410 {
411 if (peer->status != Established)
412 continue;
413
paul59320202004-11-09 01:54:03 +0000414 if (peer->afc[afi][SAFI_UNICAST])
415 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
416 if (peer->afc[afi][SAFI_MULTICAST])
417 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
418 if (peer->afc[afi][SAFI_MPLS_VPN])
419 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
paul718e3742002-12-13 20:15:29 +0000420 }
421
paul59320202004-11-09 01:54:03 +0000422 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000423 rn = bgp_route_next (rn))
424 {
425 for (bi = rn->info; bi; bi = next)
426 {
427 next = bi->next;
428
429 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
430 {
431 changed = 0;
432 metricchanged = 0;
433
434 if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400435 valid = bgp_nexthop_onlink (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000436 else
paul59320202004-11-09 01:54:03 +0000437 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000438 &changed, &metricchanged);
439
440 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
441
442 if (changed)
443 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
444 else
445 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
446
447 if (valid != current)
448 {
449 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
450 {
451 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000452 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000453 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000454 }
455 else
456 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000457 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000458 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000459 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000460 }
461 }
462
paul59320202004-11-09 01:54:03 +0000463 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000464 BGP_CONFIG_DAMPENING)
Paul Jakmafb982c22007-05-04 20:15:47 +0000465 && bi->extra && bi->extra->damp_info )
paul59320202004-11-09 01:54:03 +0000466 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000467 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000468 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000469 }
470 }
paul59320202004-11-09 01:54:03 +0000471 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000472 }
473
474 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000475 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
476 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000477 else
paul59320202004-11-09 01:54:03 +0000478 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000479
480 if (BGP_DEBUG (events, EVENTS))
481 {
482 if (afi == AFI_IP)
483 zlog_debug ("scanning IPv4 Unicast routing tables");
484 else if (afi == AFI_IP6)
485 zlog_debug ("scanning IPv6 Unicast routing tables");
486 }
paul718e3742002-12-13 20:15:29 +0000487}
488
paul718e3742002-12-13 20:15:29 +0000489/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000490static int
paul59320202004-11-09 01:54:03 +0000491bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000492{
493 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +0000494 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000495
hassof4184462005-02-01 20:13:16 +0000496 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000497 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000498
paul59320202004-11-09 01:54:03 +0000499 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000500
501#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000502 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000503#endif /* HAVE_IPV6 */
504
505 return 0;
506}
507
paul59320202004-11-09 01:54:03 +0000508struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000509{
510 unsigned int refcnt;
511};
512
513void
514bgp_connected_add (struct connected *ifc)
515{
516 struct prefix p;
517 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000518 struct interface *ifp;
519 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000520 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000521
522 ifp = ifc->ifp;
523
524 if (! ifp)
525 return;
526
527 if (if_is_loopback (ifp))
528 return;
529
530 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000531
532 if (addr->family == AF_INET)
533 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000534 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000535 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
536
537 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
538 return;
539
paul59320202004-11-09 01:54:03 +0000540 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000541 if (rn->info)
542 {
543 bc = rn->info;
544 bc->refcnt++;
545 }
546 else
547 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000548 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000549 bc->refcnt = 1;
550 rn->info = bc;
551 }
552 }
553#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000554 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000555 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000556 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000557 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
558
559 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
560 return;
561
562 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
563 return;
564
paul59320202004-11-09 01:54:03 +0000565 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000566 if (rn->info)
567 {
568 bc = rn->info;
569 bc->refcnt++;
570 }
571 else
572 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000573 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000574 bc->refcnt = 1;
575 rn->info = bc;
576 }
577 }
578#endif /* HAVE_IPV6 */
579}
580
581void
582bgp_connected_delete (struct connected *ifc)
583{
584 struct prefix p;
585 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000586 struct interface *ifp;
587 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000588 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000589
590 ifp = ifc->ifp;
591
592 if (if_is_loopback (ifp))
593 return;
594
595 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000596
597 if (addr->family == AF_INET)
598 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000599 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000600 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
601
602 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
603 return;
604
paul59320202004-11-09 01:54:03 +0000605 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000606 if (! rn)
607 return;
608
609 bc = rn->info;
610 bc->refcnt--;
611 if (bc->refcnt == 0)
612 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000613 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000614 rn->info = NULL;
615 }
616 bgp_unlock_node (rn);
617 bgp_unlock_node (rn);
618 }
619#ifdef HAVE_IPV6
620 else if (addr->family == AF_INET6)
621 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000622 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000623 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
624
625 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
626 return;
627
628 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
629 return;
630
paul59320202004-11-09 01:54:03 +0000631 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &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 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000639 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000640 rn->info = NULL;
641 }
642 bgp_unlock_node (rn);
643 bgp_unlock_node (rn);
644 }
645#endif /* HAVE_IPV6 */
646}
647
648int
649bgp_nexthop_self (afi_t afi, struct attr *attr)
650{
hasso52dc7ee2004-09-23 19:18:23 +0000651 struct listnode *node;
652 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000653 struct interface *ifp;
654 struct connected *ifc;
655 struct prefix *p;
656
paul1eb8ef22005-04-07 07:30:20 +0000657 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000658 {
paul1eb8ef22005-04-07 07:30:20 +0000659 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000660 {
paul718e3742002-12-13 20:15:29 +0000661 p = ifc->address;
662
663 if (p && p->family == AF_INET
664 && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
665 return 1;
666 }
667 }
668 return 0;
669}
670
paul94f2b392005-06-28 12:44:16 +0000671static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800672zlookup_read (void)
paul718e3742002-12-13 20:15:29 +0000673{
674 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000675 uint16_t length;
676 u_char marker;
677 u_char version;
678 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000679 int nbytes;
680 struct in_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000681 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000682 int i;
683 u_char nexthop_num;
684 struct nexthop *nexthop;
685 struct bgp_nexthop_cache *bnc;
686
687 s = zlookup->ibuf;
688 stream_reset (s);
689
690 nbytes = stream_read (s, zlookup->sock, 2);
691 length = stream_getw (s);
692
693 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000694 marker = stream_getc (s);
695 version = stream_getc (s);
696
697 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
698 {
699 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
700 __func__, zlookup->sock, marker, version);
701 return NULL;
702 }
703
704 command = stream_getw (s);
705
paul718e3742002-12-13 20:15:29 +0000706 raddr.s_addr = stream_get_ipv4 (s);
707 metric = stream_getl (s);
708 nexthop_num = stream_getc (s);
709
710 if (nexthop_num)
711 {
712 bnc = bnc_new ();
713 bnc->valid = 1;
714 bnc->metric = metric;
715 bnc->nexthop_num = nexthop_num;
716
717 for (i = 0; i < nexthop_num; i++)
718 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700719 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000720 nexthop->type = stream_getc (s);
721 switch (nexthop->type)
722 {
723 case ZEBRA_NEXTHOP_IPV4:
724 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
725 break;
726 case ZEBRA_NEXTHOP_IFINDEX:
727 case ZEBRA_NEXTHOP_IFNAME:
728 nexthop->ifindex = stream_getl (s);
729 break;
hassofa2b17e2004-03-04 17:45:00 +0000730 default:
731 /* do nothing */
732 break;
paul718e3742002-12-13 20:15:29 +0000733 }
734 bnc_nexthop_add (bnc, nexthop);
735 }
736 }
737 else
738 return NULL;
739
740 return bnc;
741}
742
743struct bgp_nexthop_cache *
744zlookup_query (struct in_addr addr)
745{
746 int ret;
747 struct stream *s;
748
749 /* Check socket. */
750 if (zlookup->sock < 0)
751 return NULL;
752
753 s = zlookup->obuf;
754 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000755 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000756 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000757
758 stream_putw_at (s, 0, stream_get_endp (s));
759
760 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000761 if (ret < 0)
762 {
763 zlog_err ("can't write to zlookup->sock");
764 close (zlookup->sock);
765 zlookup->sock = -1;
766 return NULL;
767 }
768 if (ret == 0)
769 {
770 zlog_err ("zlookup->sock connection closed");
771 close (zlookup->sock);
772 zlookup->sock = -1;
773 return NULL;
774 }
775
776 return zlookup_read ();
777}
778
779#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000780static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800781zlookup_read_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000782{
783 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000784 uint16_t length;
785 u_char version, marker;
786 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000787 int nbytes;
788 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000789 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000790 int i;
791 u_char nexthop_num;
792 struct nexthop *nexthop;
793 struct bgp_nexthop_cache *bnc;
794
795 s = zlookup->ibuf;
796 stream_reset (s);
797
798 nbytes = stream_read (s, zlookup->sock, 2);
799 length = stream_getw (s);
800
801 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000802 marker = stream_getc (s);
803 version = stream_getc (s);
804
805 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
806 {
807 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
808 __func__, zlookup->sock, marker, version);
809 return NULL;
810 }
811
812 command = stream_getw (s);
813
paul718e3742002-12-13 20:15:29 +0000814 stream_get (&raddr, s, 16);
815
816 metric = stream_getl (s);
817 nexthop_num = stream_getc (s);
818
819 if (nexthop_num)
820 {
821 bnc = bnc_new ();
822 bnc->valid = 1;
823 bnc->metric = metric;
824 bnc->nexthop_num = nexthop_num;
825
826 for (i = 0; i < nexthop_num; i++)
827 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700828 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000829 nexthop->type = stream_getc (s);
830 switch (nexthop->type)
831 {
832 case ZEBRA_NEXTHOP_IPV6:
833 stream_get (&nexthop->gate.ipv6, s, 16);
834 break;
835 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
836 case ZEBRA_NEXTHOP_IPV6_IFNAME:
837 stream_get (&nexthop->gate.ipv6, s, 16);
838 nexthop->ifindex = stream_getl (s);
839 break;
840 case ZEBRA_NEXTHOP_IFINDEX:
841 case ZEBRA_NEXTHOP_IFNAME:
842 nexthop->ifindex = stream_getl (s);
843 break;
hassofa2b17e2004-03-04 17:45:00 +0000844 default:
845 /* do nothing */
846 break;
paul718e3742002-12-13 20:15:29 +0000847 }
848 bnc_nexthop_add (bnc, nexthop);
849 }
850 }
851 else
852 return NULL;
853
854 return bnc;
855}
856
857struct bgp_nexthop_cache *
858zlookup_query_ipv6 (struct in6_addr *addr)
859{
860 int ret;
861 struct stream *s;
862
863 /* Check socket. */
864 if (zlookup->sock < 0)
865 return NULL;
866
867 s = zlookup->obuf;
868 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000869 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000870 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000871 stream_putw_at (s, 0, stream_get_endp (s));
872
873 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000874 if (ret < 0)
875 {
876 zlog_err ("can't write to zlookup->sock");
877 close (zlookup->sock);
878 zlookup->sock = -1;
879 return NULL;
880 }
881 if (ret == 0)
882 {
883 zlog_err ("zlookup->sock connection closed");
884 close (zlookup->sock);
885 zlookup->sock = -1;
886 return NULL;
887 }
888
889 return zlookup_read_ipv6 ();
890}
891#endif /* HAVE_IPV6 */
892
paul94f2b392005-06-28 12:44:16 +0000893static int
894bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
895 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000896{
897 struct stream *s;
898 int ret;
pauld3092e72006-01-17 17:33:46 +0000899 u_int16_t length, command;
900 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000901 int nbytes;
902 struct in_addr addr;
903 struct in_addr nexthop;
904 u_int32_t metric = 0;
905 u_char nexthop_num;
906 u_char nexthop_type;
907
908 /* If lookup connection is not available return valid. */
909 if (zlookup->sock < 0)
910 {
911 if (igpmetric)
912 *igpmetric = 0;
913 return 1;
914 }
915
916 /* Send query to the lookup connection */
917 s = zlookup->obuf;
918 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000919 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
920
paul718e3742002-12-13 20:15:29 +0000921 stream_putc (s, p->prefixlen);
922 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +0000923
924 stream_putw_at (s, 0, stream_get_endp (s));
925
paul718e3742002-12-13 20:15:29 +0000926 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +0000927 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000928
929 if (ret < 0)
930 {
931 zlog_err ("can't write to zlookup->sock");
932 close (zlookup->sock);
933 zlookup->sock = -1;
934 return 1;
935 }
936 if (ret == 0)
937 {
938 zlog_err ("zlookup->sock connection closed");
939 close (zlookup->sock);
940 zlookup->sock = -1;
941 return 1;
942 }
943
944 /* Get result. */
945 stream_reset (s);
946
947 /* Fetch length. */
948 nbytes = stream_read (s, zlookup->sock, 2);
949 length = stream_getw (s);
950
951 /* Fetch whole data. */
952 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000953 marker = stream_getc (s);
954 version = stream_getc (s);
955
956 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
957 {
958 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
959 __func__, zlookup->sock, marker, version);
960 return 0;
961 }
962
963 command = stream_getw (s);
964
paul718e3742002-12-13 20:15:29 +0000965 addr.s_addr = stream_get_ipv4 (s);
966 metric = stream_getl (s);
967 nexthop_num = stream_getc (s);
968
969 /* Set IGP metric value. */
970 if (igpmetric)
971 *igpmetric = metric;
972
973 /* If there is nexthop then this is active route. */
974 if (nexthop_num)
975 {
976 nexthop.s_addr = 0;
977 nexthop_type = stream_getc (s);
978 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
979 {
980 nexthop.s_addr = stream_get_ipv4 (s);
981 if (igpnexthop)
982 *igpnexthop = nexthop;
983 }
984 else
985 *igpnexthop = nexthop;
986
987 return 1;
988 }
989 else
990 return 0;
991}
992
993/* Scan all configured BGP route then check the route exists in IGP or
994 not. */
paul94f2b392005-06-28 12:44:16 +0000995static int
paul718e3742002-12-13 20:15:29 +0000996bgp_import (struct thread *t)
997{
998 struct bgp *bgp;
999 struct bgp_node *rn;
1000 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001001 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001002 int valid;
1003 u_int32_t metric;
1004 struct in_addr nexthop;
1005 afi_t afi;
1006 safi_t safi;
1007
1008 bgp_import_thread =
1009 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1010
hassof4184462005-02-01 20:13:16 +00001011 if (BGP_DEBUG (events, EVENTS))
1012 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001013
paul1eb8ef22005-04-07 07:30:20 +00001014 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001015 {
1016 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1017 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1018 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1019 rn = bgp_route_next (rn))
1020 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001021 {
paul6cbbc3c2003-04-28 17:11:02 +00001022 if (bgp_static->backdoor)
1023 continue;
paul718e3742002-12-13 20:15:29 +00001024
paul6cbbc3c2003-04-28 17:11:02 +00001025 valid = bgp_static->valid;
1026 metric = bgp_static->igpmetric;
1027 nexthop = bgp_static->igpnexthop;
1028
1029 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1030 && afi == AFI_IP && safi == SAFI_UNICAST)
1031 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1032 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001033 else
paul6cbbc3c2003-04-28 17:11:02 +00001034 {
1035 bgp_static->valid = 1;
1036 bgp_static->igpmetric = 0;
1037 bgp_static->igpnexthop.s_addr = 0;
1038 }
1039
1040 if (bgp_static->valid != valid)
1041 {
1042 if (bgp_static->valid)
1043 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1044 else
1045 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1046 }
1047 else if (bgp_static->valid)
1048 {
1049 if (bgp_static->igpmetric != metric
1050 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1051 || bgp_static->rmap.name)
1052 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1053 }
paul718e3742002-12-13 20:15:29 +00001054 }
paul6cbbc3c2003-04-28 17:11:02 +00001055 }
paul718e3742002-12-13 20:15:29 +00001056 return 0;
1057}
1058
1059/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001060static int
paul718e3742002-12-13 20:15:29 +00001061zlookup_connect (struct thread *t)
1062{
1063 struct zclient *zlookup;
1064
1065 zlookup = THREAD_ARG (t);
1066 zlookup->t_connect = NULL;
1067
1068 if (zlookup->sock != -1)
1069 return 0;
1070
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001071 if (zclient_socket_connect (zlookup) < 0)
paul718e3742002-12-13 20:15:29 +00001072 return -1;
1073
paul718e3742002-12-13 20:15:29 +00001074 return 0;
1075}
1076
1077/* Check specified multiaccess next-hop. */
1078int
1079bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1080{
1081 struct bgp_node *rn1;
1082 struct bgp_node *rn2;
1083 struct prefix p1;
1084 struct prefix p2;
1085 struct in_addr addr;
1086 int ret;
1087
1088 ret = inet_aton (peer, &addr);
1089 if (! ret)
1090 return 0;
1091
1092 memset (&p1, 0, sizeof (struct prefix));
1093 p1.family = AF_INET;
1094 p1.prefixlen = IPV4_MAX_BITLEN;
1095 p1.u.prefix4 = nexthop;
1096 memset (&p2, 0, sizeof (struct prefix));
1097 p2.family = AF_INET;
1098 p2.prefixlen = IPV4_MAX_BITLEN;
1099 p2.u.prefix4 = addr;
1100
1101 /* If bgp scan is not enabled, return invalid. */
1102 if (zlookup->sock < 0)
1103 return 0;
1104
paul59320202004-11-09 01:54:03 +00001105 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001106 if (! rn1)
1107 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001108 bgp_unlock_node (rn1);
paul718e3742002-12-13 20:15:29 +00001109
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;
Chris Caputo6c88b442010-07-27 16:28:55 +00001113 bgp_unlock_node (rn2);
paul718e3742002-12-13 20:15:29 +00001114
Chris Caputo6c88b442010-07-27 16:28:55 +00001115 /* This is safe, even with above unlocks, since we are just
1116 comparing pointers to the objects, not the objects themselves. */
paul718e3742002-12-13 20:15:29 +00001117 if (rn1 == rn2)
1118 return 1;
1119
1120 return 0;
1121}
1122
1123DEFUN (bgp_scan_time,
1124 bgp_scan_time_cmd,
1125 "bgp scan-time <5-60>",
1126 "BGP specific commands\n"
1127 "Configure background scanner interval\n"
1128 "Scanner interval (seconds)\n")
1129{
1130 bgp_scan_interval = atoi (argv[0]);
1131
1132 if (bgp_scan_thread)
1133 {
1134 thread_cancel (bgp_scan_thread);
1135 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001136 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001137 }
1138
1139 return CMD_SUCCESS;
1140}
1141
1142DEFUN (no_bgp_scan_time,
1143 no_bgp_scan_time_cmd,
1144 "no bgp scan-time",
1145 NO_STR
1146 "BGP specific commands\n"
1147 "Configure background scanner interval\n")
1148{
1149 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1150
1151 if (bgp_scan_thread)
1152 {
1153 thread_cancel (bgp_scan_thread);
1154 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001155 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001156 }
1157
1158 return CMD_SUCCESS;
1159}
1160
1161ALIAS (no_bgp_scan_time,
1162 no_bgp_scan_time_val_cmd,
1163 "no bgp scan-time <5-60>",
1164 NO_STR
1165 "BGP specific commands\n"
1166 "Configure background scanner interval\n"
1167 "Scanner interval (seconds)\n")
1168
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001169static int
1170show_ip_bgp_scan_tables (struct vty *vty, const char detail)
paul718e3742002-12-13 20:15:29 +00001171{
1172 struct bgp_node *rn;
1173 struct bgp_nexthop_cache *bnc;
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001174 char buf[INET6_ADDRSTRLEN];
1175 u_char i;
paul718e3742002-12-13 20:15:29 +00001176
1177 if (bgp_scan_thread)
1178 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1179 else
1180 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1181 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1182
1183 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001184 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001185 if ((bnc = rn->info) != NULL)
1186 {
1187 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001188 {
paul718e3742002-12-13 20:15:29 +00001189 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001190 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1191 if (detail)
1192 for (i = 0; i < bnc->nexthop_num; i++)
1193 vty_out (vty, " %s%s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1194 }
paul718e3742002-12-13 20:15:29 +00001195 else
1196 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001197 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001198 }
1199
1200#ifdef HAVE_IPV6
1201 {
paul59320202004-11-09 01:54:03 +00001202 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1203 rn;
1204 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001205 if ((bnc = rn->info) != NULL)
1206 {
1207 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001208 {
paul718e3742002-12-13 20:15:29 +00001209 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001210 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001211 bnc->metric, VTY_NEWLINE);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001212 if (detail)
1213 for (i = 0; i < bnc->nexthop_num; i++)
1214 vty_out (vty, " %s%s", inet_ntop (AF_INET6, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1215 }
paul718e3742002-12-13 20:15:29 +00001216 else
1217 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001218 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001219 VTY_NEWLINE);
1220 }
1221 }
1222#endif /* HAVE_IPV6 */
1223
1224 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001225 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1226 rn;
1227 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001228 if (rn->info != NULL)
1229 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1230 VTY_NEWLINE);
1231
1232#ifdef HAVE_IPV6
1233 {
paul59320202004-11-09 01:54:03 +00001234 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1235 rn;
1236 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001237 if (rn->info != NULL)
1238 vty_out (vty, " %s/%d%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001239 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001240 rn->p.prefixlen,
1241 VTY_NEWLINE);
1242 }
1243#endif /* HAVE_IPV6 */
1244
1245 return CMD_SUCCESS;
1246}
1247
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001248DEFUN (show_ip_bgp_scan,
1249 show_ip_bgp_scan_cmd,
1250 "show ip bgp scan",
1251 SHOW_STR
1252 IP_STR
1253 BGP_STR
1254 "BGP scan status\n")
1255{
1256 return show_ip_bgp_scan_tables (vty, 0);
1257}
1258
1259DEFUN (show_ip_bgp_scan_detail,
1260 show_ip_bgp_scan_detail_cmd,
1261 "show ip bgp scan detail",
1262 SHOW_STR
1263 IP_STR
1264 BGP_STR
1265 "BGP scan status\n"
1266 "More detailed output\n")
1267{
1268 return show_ip_bgp_scan_tables (vty, 1);
1269}
1270
paul718e3742002-12-13 20:15:29 +00001271int
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);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001312 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01001313 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
paul718e3742002-12-13 20:15:29 +00001314 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001315 install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
paul718e3742002-12-13 20:15:29 +00001316}
Chris Caputo228da422009-07-18 05:44:03 +00001317
1318void
1319bgp_scan_finish (void)
1320{
Chris Caputo6c88b442010-07-27 16:28:55 +00001321 /* Only the current one needs to be reset. */
1322 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]);
1323
Chris Caputo228da422009-07-18 05:44:03 +00001324 bgp_table_unlock (cache1_table[AFI_IP]);
1325 cache1_table[AFI_IP] = NULL;
1326
1327 bgp_table_unlock (cache2_table[AFI_IP]);
1328 cache2_table[AFI_IP] = NULL;
1329
1330 bgp_table_unlock (bgp_connected_table[AFI_IP]);
1331 bgp_connected_table[AFI_IP] = NULL;
1332
1333#ifdef HAVE_IPV6
Chris Caputo6c88b442010-07-27 16:28:55 +00001334 /* Only the current one needs to be reset. */
1335 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]);
1336
Chris Caputo228da422009-07-18 05:44:03 +00001337 bgp_table_unlock (cache1_table[AFI_IP6]);
1338 cache1_table[AFI_IP6] = NULL;
1339
1340 bgp_table_unlock (cache2_table[AFI_IP6]);
1341 cache2_table[AFI_IP6] = NULL;
1342
1343 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
1344 bgp_connected_table[AFI_IP6] = NULL;
1345#endif /* HAVE_IPV6 */
1346}