blob: 60efa1953afc724cc0e5363fef521118bd98b556 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* BGP nexthop scan
2 Copyright (C) 2000 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "command.h"
24#include "thread.h"
25#include "prefix.h"
26#include "zclient.h"
27#include "stream.h"
28#include "network.h"
29#include "log.h"
30#include "memory.h"
31
32#include "bgpd/bgpd.h"
33#include "bgpd/bgp_table.h"
34#include "bgpd/bgp_route.h"
35#include "bgpd/bgp_attr.h"
36#include "bgpd/bgp_nexthop.h"
37#include "bgpd/bgp_debug.h"
38#include "bgpd/bgp_damp.h"
39#include "zebra/rib.h"
40#include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
41
42struct bgp_nexthop_cache *zlookup_query (struct in_addr);
43#ifdef HAVE_IPV6
44struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *);
45#endif /* HAVE_IPV6 */
46
47/* Only one BGP scan thread are activated at the same time. */
paul00d252c2005-05-23 14:19:54 +000048static struct thread *bgp_scan_thread = NULL;
paul718e3742002-12-13 20:15:29 +000049
50/* BGP import thread */
paul00d252c2005-05-23 14:19:54 +000051static struct thread *bgp_import_thread = NULL;
paul718e3742002-12-13 20:15:29 +000052
53/* BGP scan interval. */
paul00d252c2005-05-23 14:19:54 +000054static int bgp_scan_interval;
paul718e3742002-12-13 20:15:29 +000055
56/* BGP import interval. */
paul00d252c2005-05-23 14:19:54 +000057static int bgp_import_interval;
paul718e3742002-12-13 20:15:29 +000058
59/* Route table for next-hop lookup cache. */
paul00d252c2005-05-23 14:19:54 +000060static struct bgp_table *bgp_nexthop_cache_table[AFI_MAX];
61static struct bgp_table *cache1_table[AFI_MAX];
62static struct bgp_table *cache2_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000063
64/* Route table for connected route. */
paul00d252c2005-05-23 14:19:54 +000065static struct bgp_table *bgp_connected_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000066
67/* BGP nexthop lookup query client. */
Chris Caputo228da422009-07-18 05:44:03 +000068struct zclient *zlookup = NULL;
paul718e3742002-12-13 20:15:29 +000069
70/* Add nexthop to the end of the list. */
paul94f2b392005-06-28 12:44:16 +000071static void
paul718e3742002-12-13 20:15:29 +000072bnc_nexthop_add (struct bgp_nexthop_cache *bnc, struct nexthop *nexthop)
73{
74 struct nexthop *last;
75
76 for (last = bnc->nexthop; last && last->next; last = last->next)
77 ;
78 if (last)
79 last->next = nexthop;
80 else
81 bnc->nexthop = nexthop;
82 nexthop->prev = last;
83}
84
paul94f2b392005-06-28 12:44:16 +000085static void
paul718e3742002-12-13 20:15:29 +000086bnc_nexthop_free (struct bgp_nexthop_cache *bnc)
87{
88 struct nexthop *nexthop;
89 struct nexthop *next = NULL;
90
91 for (nexthop = bnc->nexthop; nexthop; nexthop = next)
92 {
93 next = nexthop->next;
94 XFREE (MTYPE_NEXTHOP, nexthop);
95 }
96}
97
paul94f2b392005-06-28 12:44:16 +000098static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -080099bnc_new (void)
paul718e3742002-12-13 20:15:29 +0000100{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700101 return XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
paul718e3742002-12-13 20:15:29 +0000102}
103
paul94f2b392005-06-28 12:44:16 +0000104static void
paul718e3742002-12-13 20:15:29 +0000105bnc_free (struct bgp_nexthop_cache *bnc)
106{
107 bnc_nexthop_free (bnc);
108 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
109}
110
paul94f2b392005-06-28 12:44:16 +0000111static int
paul718e3742002-12-13 20:15:29 +0000112bgp_nexthop_same (struct nexthop *next1, struct nexthop *next2)
113{
114 if (next1->type != next2->type)
115 return 0;
116
117 switch (next1->type)
118 {
119 case ZEBRA_NEXTHOP_IPV4:
120 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
121 return 0;
122 break;
123 case ZEBRA_NEXTHOP_IFINDEX:
124 case ZEBRA_NEXTHOP_IFNAME:
125 if (next1->ifindex != next2->ifindex)
126 return 0;
127 break;
128#ifdef HAVE_IPV6
129 case ZEBRA_NEXTHOP_IPV6:
130 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
131 return 0;
132 break;
133 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
134 case ZEBRA_NEXTHOP_IPV6_IFNAME:
135 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
136 return 0;
137 if (next1->ifindex != next2->ifindex)
138 return 0;
139 break;
140#endif /* HAVE_IPV6 */
hassofa2b17e2004-03-04 17:45:00 +0000141 default:
142 /* do nothing */
143 break;
paul718e3742002-12-13 20:15:29 +0000144 }
145 return 1;
146}
147
paul94f2b392005-06-28 12:44:16 +0000148static int
paul718e3742002-12-13 20:15:29 +0000149bgp_nexthop_cache_changed (struct bgp_nexthop_cache *bnc1,
150 struct bgp_nexthop_cache *bnc2)
151{
152 int i;
153 struct nexthop *next1, *next2;
154
155 if (bnc1->nexthop_num != bnc2->nexthop_num)
156 return 1;
157
158 next1 = bnc1->nexthop;
159 next2 = bnc2->nexthop;
160
161 for (i = 0; i < bnc1->nexthop_num; i++)
162 {
163 if (! bgp_nexthop_same (next1, next2))
164 return 1;
165
166 next1 = next1->next;
167 next2 = next2->next;
168 }
169 return 0;
170}
171
172/* If nexthop exists on connected network return 1. */
173int
174bgp_nexthop_check_ebgp (afi_t afi, struct attr *attr)
175{
176 struct bgp_node *rn;
177
178 /* If zebra is not enabled return */
179 if (zlookup->sock < 0)
180 return 1;
181
182 /* Lookup the address is onlink or not. */
183 if (afi == AFI_IP)
184 {
paul59320202004-11-09 01:54:03 +0000185 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
paul718e3742002-12-13 20:15:29 +0000186 if (rn)
187 {
188 bgp_unlock_node (rn);
189 return 1;
190 }
191 }
192#ifdef HAVE_IPV6
193 else if (afi == AFI_IP6)
194 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000195 if (attr->extra->mp_nexthop_len == 32)
paul718e3742002-12-13 20:15:29 +0000196 return 1;
Paul Jakmafb982c22007-05-04 20:15:47 +0000197 else if (attr->extra->mp_nexthop_len == 16)
paul718e3742002-12-13 20:15:29 +0000198 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000199 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000200 return 1;
201
paul59320202004-11-09 01:54:03 +0000202 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
Paul Jakmafb982c22007-05-04 20:15:47 +0000203 &attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000204 if (rn)
205 {
206 bgp_unlock_node (rn);
207 return 1;
208 }
209 }
210 }
211#endif /* HAVE_IPV6 */
212 return 0;
213}
214
215#ifdef HAVE_IPV6
216/* Check specified next-hop is reachable or not. */
paul94f2b392005-06-28 12:44:16 +0000217static int
paul718e3742002-12-13 20:15:29 +0000218bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
219 int *metricchanged)
220{
221 struct bgp_node *rn;
222 struct prefix p;
223 struct bgp_nexthop_cache *bnc;
224 struct attr *attr;
225
226 /* If lookup is not enabled, return valid. */
227 if (zlookup->sock < 0)
228 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000229 if (ri->extra)
230 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000231 return 1;
232 }
233
234 /* Only check IPv6 global address only nexthop. */
235 attr = ri->attr;
236
Paul Jakmafb982c22007-05-04 20:15:47 +0000237 if (attr->extra->mp_nexthop_len != 16
238 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000239 return 1;
240
241 memset (&p, 0, sizeof (struct prefix));
242 p.family = AF_INET6;
243 p.prefixlen = IPV6_MAX_BITLEN;
Paul Jakmafb982c22007-05-04 20:15:47 +0000244 p.u.prefix6 = attr->extra->mp_nexthop_global;
paul718e3742002-12-13 20:15:29 +0000245
246 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000247 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
paul718e3742002-12-13 20:15:29 +0000248
249 if (rn->info)
250 {
251 bnc = rn->info;
252 bgp_unlock_node (rn);
253 }
254 else
255 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000256 bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000257 if (bnc)
258 {
259 struct bgp_table *old;
260 struct bgp_node *oldrn;
261 struct bgp_nexthop_cache *oldbnc;
262
263 if (changed)
264 {
paul59320202004-11-09 01:54:03 +0000265 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
266 old = cache2_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000267 else
paul59320202004-11-09 01:54:03 +0000268 old = cache1_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000269
270 oldrn = bgp_node_lookup (old, &p);
271 if (oldrn)
272 {
273 oldbnc = oldrn->info;
274
275 bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
276
277 if (bnc->metric != oldbnc->metric)
278 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000279
280 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000281 }
282 }
283 }
284 else
285 {
286 bnc = bnc_new ();
287 bnc->valid = 0;
288 }
289 rn->info = bnc;
290 }
291
292 if (changed)
293 *changed = bnc->changed;
294
295 if (metricchanged)
296 *metricchanged = bnc->metricchanged;
297
Paul Jakmafb982c22007-05-04 20:15:47 +0000298 if (bnc->valid && bnc->metric)
299 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
300 else if (ri->extra)
301 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000302
303 return bnc->valid;
304}
305#endif /* HAVE_IPV6 */
306
307/* Check specified next-hop is reachable or not. */
308int
309bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
310 int *changed, int *metricchanged)
311{
312 struct bgp_node *rn;
313 struct prefix p;
314 struct bgp_nexthop_cache *bnc;
315 struct in_addr addr;
316
317 /* If lookup is not enabled, return valid. */
318 if (zlookup->sock < 0)
319 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000320 if (ri->extra)
321 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000322 return 1;
323 }
324
325#ifdef HAVE_IPV6
326 if (afi == AFI_IP6)
327 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
328#endif /* HAVE_IPV6 */
329
330 addr = ri->attr->nexthop;
331
332 memset (&p, 0, sizeof (struct prefix));
333 p.family = AF_INET;
334 p.prefixlen = IPV4_MAX_BITLEN;
335 p.u.prefix4 = addr;
336
337 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000338 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000339
340 if (rn->info)
341 {
342 bnc = rn->info;
343 bgp_unlock_node (rn);
344 }
345 else
346 {
347 bnc = zlookup_query (addr);
348 if (bnc)
349 {
350 struct bgp_table *old;
351 struct bgp_node *oldrn;
352 struct bgp_nexthop_cache *oldbnc;
353
354 if (changed)
355 {
paul59320202004-11-09 01:54:03 +0000356 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
357 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000358 else
paul59320202004-11-09 01:54:03 +0000359 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000360
361 oldrn = bgp_node_lookup (old, &p);
362 if (oldrn)
363 {
364 oldbnc = oldrn->info;
365
366 bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
367
368 if (bnc->metric != oldbnc->metric)
369 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000370
371 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000372 }
373 }
374 }
375 else
376 {
377 bnc = bnc_new ();
378 bnc->valid = 0;
379 }
380 rn->info = bnc;
381 }
382
383 if (changed)
384 *changed = bnc->changed;
385
386 if (metricchanged)
387 *metricchanged = bnc->metricchanged;
388
Paul Jakmafb982c22007-05-04 20:15:47 +0000389 if (bnc->valid && bnc->metric)
390 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
391 else if (ri->extra)
392 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000393
394 return bnc->valid;
395}
396
397/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000398static void
paul718e3742002-12-13 20:15:29 +0000399bgp_nexthop_cache_reset (struct bgp_table *table)
400{
401 struct bgp_node *rn;
402 struct bgp_nexthop_cache *bnc;
403
404 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
405 if ((bnc = rn->info) != NULL)
406 {
407 bnc_free (bnc);
408 rn->info = NULL;
409 bgp_unlock_node (rn);
410 }
411}
412
paul94f2b392005-06-28 12:44:16 +0000413static void
paul59320202004-11-09 01:54:03 +0000414bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000415{
416 struct bgp_node *rn;
417 struct bgp *bgp;
418 struct bgp_info *bi;
419 struct bgp_info *next;
420 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000421 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000422 int valid;
423 int current;
424 int changed;
425 int metricchanged;
426
427 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000428 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
429 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000430 else
paul59320202004-11-09 01:54:03 +0000431 bgp_nexthop_cache_table[afi] = cache1_table[afi];
paul718e3742002-12-13 20:15:29 +0000432
433 /* Get default bgp. */
434 bgp = bgp_get_default ();
435 if (bgp == NULL)
436 return;
437
438 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000439 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000440 {
441 if (peer->status != Established)
442 continue;
443
paul59320202004-11-09 01:54:03 +0000444 if (peer->afc[afi][SAFI_UNICAST])
445 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
446 if (peer->afc[afi][SAFI_MULTICAST])
447 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
448 if (peer->afc[afi][SAFI_MPLS_VPN])
449 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
paul718e3742002-12-13 20:15:29 +0000450 }
451
paul59320202004-11-09 01:54:03 +0000452 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000453 rn = bgp_route_next (rn))
454 {
455 for (bi = rn->info; bi; bi = next)
456 {
457 next = bi->next;
458
459 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
460 {
461 changed = 0;
462 metricchanged = 0;
463
464 if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
paul59320202004-11-09 01:54:03 +0000465 valid = bgp_nexthop_check_ebgp (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000466 else
paul59320202004-11-09 01:54:03 +0000467 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000468 &changed, &metricchanged);
469
470 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
471
472 if (changed)
473 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
474 else
475 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
476
477 if (valid != current)
478 {
479 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
480 {
481 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000482 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000483 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000484 }
485 else
486 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000487 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000488 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000489 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000490 }
491 }
492
paul59320202004-11-09 01:54:03 +0000493 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000494 BGP_CONFIG_DAMPENING)
Paul Jakmafb982c22007-05-04 20:15:47 +0000495 && bi->extra && bi->extra->damp_info )
paul59320202004-11-09 01:54:03 +0000496 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000497 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000498 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000499 }
500 }
paul59320202004-11-09 01:54:03 +0000501 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000502 }
503
504 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000505 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
506 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000507 else
paul59320202004-11-09 01:54:03 +0000508 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000509
510 if (BGP_DEBUG (events, EVENTS))
511 {
512 if (afi == AFI_IP)
513 zlog_debug ("scanning IPv4 Unicast routing tables");
514 else if (afi == AFI_IP6)
515 zlog_debug ("scanning IPv6 Unicast routing tables");
516 }
paul718e3742002-12-13 20:15:29 +0000517}
518
paul718e3742002-12-13 20:15:29 +0000519/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000520static int
paul59320202004-11-09 01:54:03 +0000521bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000522{
523 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +0000524 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000525
hassof4184462005-02-01 20:13:16 +0000526 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000527 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000528
paul59320202004-11-09 01:54:03 +0000529 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000530
531#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000532 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000533#endif /* HAVE_IPV6 */
534
535 return 0;
536}
537
paul59320202004-11-09 01:54:03 +0000538struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000539{
540 unsigned int refcnt;
541};
542
543void
544bgp_connected_add (struct connected *ifc)
545{
546 struct prefix p;
547 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000548 struct interface *ifp;
549 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000550 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000551
552 ifp = ifc->ifp;
553
554 if (! ifp)
555 return;
556
557 if (if_is_loopback (ifp))
558 return;
559
560 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000561
562 if (addr->family == AF_INET)
563 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000564 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000565 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
566
567 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
568 return;
569
paul59320202004-11-09 01:54:03 +0000570 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000571 if (rn->info)
572 {
573 bc = rn->info;
574 bc->refcnt++;
575 }
576 else
577 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000578 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000579 bc->refcnt = 1;
580 rn->info = bc;
581 }
582 }
583#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000584 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000585 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000586 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000587 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
588
589 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
590 return;
591
592 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
593 return;
594
paul59320202004-11-09 01:54:03 +0000595 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000596 if (rn->info)
597 {
598 bc = rn->info;
599 bc->refcnt++;
600 }
601 else
602 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000603 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000604 bc->refcnt = 1;
605 rn->info = bc;
606 }
607 }
608#endif /* HAVE_IPV6 */
609}
610
611void
612bgp_connected_delete (struct connected *ifc)
613{
614 struct prefix p;
615 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000616 struct interface *ifp;
617 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000618 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000619
620 ifp = ifc->ifp;
621
622 if (if_is_loopback (ifp))
623 return;
624
625 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000626
627 if (addr->family == AF_INET)
628 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000629 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000630 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
631
632 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
633 return;
634
paul59320202004-11-09 01:54:03 +0000635 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000636 if (! rn)
637 return;
638
639 bc = rn->info;
640 bc->refcnt--;
641 if (bc->refcnt == 0)
642 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000643 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000644 rn->info = NULL;
645 }
646 bgp_unlock_node (rn);
647 bgp_unlock_node (rn);
648 }
649#ifdef HAVE_IPV6
650 else if (addr->family == AF_INET6)
651 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000652 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000653 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
654
655 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
656 return;
657
658 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
659 return;
660
paul59320202004-11-09 01:54:03 +0000661 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000662 if (! rn)
663 return;
664
665 bc = rn->info;
666 bc->refcnt--;
667 if (bc->refcnt == 0)
668 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000669 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000670 rn->info = NULL;
671 }
672 bgp_unlock_node (rn);
673 bgp_unlock_node (rn);
674 }
675#endif /* HAVE_IPV6 */
676}
677
678int
679bgp_nexthop_self (afi_t afi, struct attr *attr)
680{
hasso52dc7ee2004-09-23 19:18:23 +0000681 struct listnode *node;
682 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000683 struct interface *ifp;
684 struct connected *ifc;
685 struct prefix *p;
686
paul1eb8ef22005-04-07 07:30:20 +0000687 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000688 {
paul1eb8ef22005-04-07 07:30:20 +0000689 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000690 {
paul718e3742002-12-13 20:15:29 +0000691 p = ifc->address;
692
693 if (p && p->family == AF_INET
694 && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
695 return 1;
696 }
697 }
698 return 0;
699}
700
paul94f2b392005-06-28 12:44:16 +0000701static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800702zlookup_read (void)
paul718e3742002-12-13 20:15:29 +0000703{
704 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000705 uint16_t length;
706 u_char marker;
707 u_char version;
708 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000709 int nbytes;
710 struct in_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000711 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000712 int i;
713 u_char nexthop_num;
714 struct nexthop *nexthop;
715 struct bgp_nexthop_cache *bnc;
716
717 s = zlookup->ibuf;
718 stream_reset (s);
719
720 nbytes = stream_read (s, zlookup->sock, 2);
721 length = stream_getw (s);
722
723 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000724 marker = stream_getc (s);
725 version = stream_getc (s);
726
727 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
728 {
729 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
730 __func__, zlookup->sock, marker, version);
731 return NULL;
732 }
733
734 command = stream_getw (s);
735
paul718e3742002-12-13 20:15:29 +0000736 raddr.s_addr = stream_get_ipv4 (s);
737 metric = stream_getl (s);
738 nexthop_num = stream_getc (s);
739
740 if (nexthop_num)
741 {
742 bnc = bnc_new ();
743 bnc->valid = 1;
744 bnc->metric = metric;
745 bnc->nexthop_num = nexthop_num;
746
747 for (i = 0; i < nexthop_num; i++)
748 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700749 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000750 nexthop->type = stream_getc (s);
751 switch (nexthop->type)
752 {
753 case ZEBRA_NEXTHOP_IPV4:
754 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
755 break;
756 case ZEBRA_NEXTHOP_IFINDEX:
757 case ZEBRA_NEXTHOP_IFNAME:
758 nexthop->ifindex = stream_getl (s);
759 break;
hassofa2b17e2004-03-04 17:45:00 +0000760 default:
761 /* do nothing */
762 break;
paul718e3742002-12-13 20:15:29 +0000763 }
764 bnc_nexthop_add (bnc, nexthop);
765 }
766 }
767 else
768 return NULL;
769
770 return bnc;
771}
772
773struct bgp_nexthop_cache *
774zlookup_query (struct in_addr addr)
775{
776 int ret;
777 struct stream *s;
778
779 /* Check socket. */
780 if (zlookup->sock < 0)
781 return NULL;
782
783 s = zlookup->obuf;
784 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000785 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000786 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000787
788 stream_putw_at (s, 0, stream_get_endp (s));
789
790 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000791 if (ret < 0)
792 {
793 zlog_err ("can't write to zlookup->sock");
794 close (zlookup->sock);
795 zlookup->sock = -1;
796 return NULL;
797 }
798 if (ret == 0)
799 {
800 zlog_err ("zlookup->sock connection closed");
801 close (zlookup->sock);
802 zlookup->sock = -1;
803 return NULL;
804 }
805
806 return zlookup_read ();
807}
808
809#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000810static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800811zlookup_read_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000812{
813 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000814 uint16_t length;
815 u_char version, marker;
816 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000817 int nbytes;
818 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000819 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000820 int i;
821 u_char nexthop_num;
822 struct nexthop *nexthop;
823 struct bgp_nexthop_cache *bnc;
824
825 s = zlookup->ibuf;
826 stream_reset (s);
827
828 nbytes = stream_read (s, zlookup->sock, 2);
829 length = stream_getw (s);
830
831 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000832 marker = stream_getc (s);
833 version = stream_getc (s);
834
835 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
836 {
837 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
838 __func__, zlookup->sock, marker, version);
839 return NULL;
840 }
841
842 command = stream_getw (s);
843
paul718e3742002-12-13 20:15:29 +0000844 stream_get (&raddr, s, 16);
845
846 metric = stream_getl (s);
847 nexthop_num = stream_getc (s);
848
849 if (nexthop_num)
850 {
851 bnc = bnc_new ();
852 bnc->valid = 1;
853 bnc->metric = metric;
854 bnc->nexthop_num = nexthop_num;
855
856 for (i = 0; i < nexthop_num; i++)
857 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700858 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000859 nexthop->type = stream_getc (s);
860 switch (nexthop->type)
861 {
862 case ZEBRA_NEXTHOP_IPV6:
863 stream_get (&nexthop->gate.ipv6, s, 16);
864 break;
865 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
866 case ZEBRA_NEXTHOP_IPV6_IFNAME:
867 stream_get (&nexthop->gate.ipv6, s, 16);
868 nexthop->ifindex = stream_getl (s);
869 break;
870 case ZEBRA_NEXTHOP_IFINDEX:
871 case ZEBRA_NEXTHOP_IFNAME:
872 nexthop->ifindex = stream_getl (s);
873 break;
hassofa2b17e2004-03-04 17:45:00 +0000874 default:
875 /* do nothing */
876 break;
paul718e3742002-12-13 20:15:29 +0000877 }
878 bnc_nexthop_add (bnc, nexthop);
879 }
880 }
881 else
882 return NULL;
883
884 return bnc;
885}
886
887struct bgp_nexthop_cache *
888zlookup_query_ipv6 (struct in6_addr *addr)
889{
890 int ret;
891 struct stream *s;
892
893 /* Check socket. */
894 if (zlookup->sock < 0)
895 return NULL;
896
897 s = zlookup->obuf;
898 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000899 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000900 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000901 stream_putw_at (s, 0, stream_get_endp (s));
902
903 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000904 if (ret < 0)
905 {
906 zlog_err ("can't write to zlookup->sock");
907 close (zlookup->sock);
908 zlookup->sock = -1;
909 return NULL;
910 }
911 if (ret == 0)
912 {
913 zlog_err ("zlookup->sock connection closed");
914 close (zlookup->sock);
915 zlookup->sock = -1;
916 return NULL;
917 }
918
919 return zlookup_read_ipv6 ();
920}
921#endif /* HAVE_IPV6 */
922
paul94f2b392005-06-28 12:44:16 +0000923static int
924bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
925 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000926{
927 struct stream *s;
928 int ret;
pauld3092e72006-01-17 17:33:46 +0000929 u_int16_t length, command;
930 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000931 int nbytes;
932 struct in_addr addr;
933 struct in_addr nexthop;
934 u_int32_t metric = 0;
935 u_char nexthop_num;
936 u_char nexthop_type;
937
938 /* If lookup connection is not available return valid. */
939 if (zlookup->sock < 0)
940 {
941 if (igpmetric)
942 *igpmetric = 0;
943 return 1;
944 }
945
946 /* Send query to the lookup connection */
947 s = zlookup->obuf;
948 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000949 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
950
paul718e3742002-12-13 20:15:29 +0000951 stream_putc (s, p->prefixlen);
952 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +0000953
954 stream_putw_at (s, 0, stream_get_endp (s));
955
paul718e3742002-12-13 20:15:29 +0000956 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +0000957 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000958
959 if (ret < 0)
960 {
961 zlog_err ("can't write to zlookup->sock");
962 close (zlookup->sock);
963 zlookup->sock = -1;
964 return 1;
965 }
966 if (ret == 0)
967 {
968 zlog_err ("zlookup->sock connection closed");
969 close (zlookup->sock);
970 zlookup->sock = -1;
971 return 1;
972 }
973
974 /* Get result. */
975 stream_reset (s);
976
977 /* Fetch length. */
978 nbytes = stream_read (s, zlookup->sock, 2);
979 length = stream_getw (s);
980
981 /* Fetch whole data. */
982 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000983 marker = stream_getc (s);
984 version = stream_getc (s);
985
986 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
987 {
988 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
989 __func__, zlookup->sock, marker, version);
990 return 0;
991 }
992
993 command = stream_getw (s);
994
paul718e3742002-12-13 20:15:29 +0000995 addr.s_addr = stream_get_ipv4 (s);
996 metric = stream_getl (s);
997 nexthop_num = stream_getc (s);
998
999 /* Set IGP metric value. */
1000 if (igpmetric)
1001 *igpmetric = metric;
1002
1003 /* If there is nexthop then this is active route. */
1004 if (nexthop_num)
1005 {
1006 nexthop.s_addr = 0;
1007 nexthop_type = stream_getc (s);
1008 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
1009 {
1010 nexthop.s_addr = stream_get_ipv4 (s);
1011 if (igpnexthop)
1012 *igpnexthop = nexthop;
1013 }
1014 else
1015 *igpnexthop = nexthop;
1016
1017 return 1;
1018 }
1019 else
1020 return 0;
1021}
1022
1023/* Scan all configured BGP route then check the route exists in IGP or
1024 not. */
paul94f2b392005-06-28 12:44:16 +00001025static int
paul718e3742002-12-13 20:15:29 +00001026bgp_import (struct thread *t)
1027{
1028 struct bgp *bgp;
1029 struct bgp_node *rn;
1030 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001031 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001032 int valid;
1033 u_int32_t metric;
1034 struct in_addr nexthop;
1035 afi_t afi;
1036 safi_t safi;
1037
1038 bgp_import_thread =
1039 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1040
hassof4184462005-02-01 20:13:16 +00001041 if (BGP_DEBUG (events, EVENTS))
1042 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001043
paul1eb8ef22005-04-07 07:30:20 +00001044 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001045 {
1046 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1047 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1048 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1049 rn = bgp_route_next (rn))
1050 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001051 {
paul6cbbc3c2003-04-28 17:11:02 +00001052 if (bgp_static->backdoor)
1053 continue;
paul718e3742002-12-13 20:15:29 +00001054
paul6cbbc3c2003-04-28 17:11:02 +00001055 valid = bgp_static->valid;
1056 metric = bgp_static->igpmetric;
1057 nexthop = bgp_static->igpnexthop;
1058
1059 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1060 && afi == AFI_IP && safi == SAFI_UNICAST)
1061 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1062 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001063 else
paul6cbbc3c2003-04-28 17:11:02 +00001064 {
1065 bgp_static->valid = 1;
1066 bgp_static->igpmetric = 0;
1067 bgp_static->igpnexthop.s_addr = 0;
1068 }
1069
1070 if (bgp_static->valid != valid)
1071 {
1072 if (bgp_static->valid)
1073 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1074 else
1075 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1076 }
1077 else if (bgp_static->valid)
1078 {
1079 if (bgp_static->igpmetric != metric
1080 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1081 || bgp_static->rmap.name)
1082 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1083 }
paul718e3742002-12-13 20:15:29 +00001084 }
paul6cbbc3c2003-04-28 17:11:02 +00001085 }
paul718e3742002-12-13 20:15:29 +00001086 return 0;
1087}
1088
1089/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001090static int
paul718e3742002-12-13 20:15:29 +00001091zlookup_connect (struct thread *t)
1092{
1093 struct zclient *zlookup;
1094
1095 zlookup = THREAD_ARG (t);
1096 zlookup->t_connect = NULL;
1097
1098 if (zlookup->sock != -1)
1099 return 0;
1100
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001101 if (zclient_socket_connect (zlookup) < 0)
paul718e3742002-12-13 20:15:29 +00001102 return -1;
1103
paul718e3742002-12-13 20:15:29 +00001104 return 0;
1105}
1106
1107/* Check specified multiaccess next-hop. */
1108int
1109bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1110{
1111 struct bgp_node *rn1;
1112 struct bgp_node *rn2;
1113 struct prefix p1;
1114 struct prefix p2;
1115 struct in_addr addr;
1116 int ret;
1117
1118 ret = inet_aton (peer, &addr);
1119 if (! ret)
1120 return 0;
1121
1122 memset (&p1, 0, sizeof (struct prefix));
1123 p1.family = AF_INET;
1124 p1.prefixlen = IPV4_MAX_BITLEN;
1125 p1.u.prefix4 = nexthop;
1126 memset (&p2, 0, sizeof (struct prefix));
1127 p2.family = AF_INET;
1128 p2.prefixlen = IPV4_MAX_BITLEN;
1129 p2.u.prefix4 = addr;
1130
1131 /* If bgp scan is not enabled, return invalid. */
1132 if (zlookup->sock < 0)
1133 return 0;
1134
paul59320202004-11-09 01:54:03 +00001135 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001136 if (! rn1)
1137 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001138 bgp_unlock_node (rn1);
paul718e3742002-12-13 20:15:29 +00001139
paul59320202004-11-09 01:54:03 +00001140 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001141 if (! rn2)
1142 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001143 bgp_unlock_node (rn2);
paul718e3742002-12-13 20:15:29 +00001144
Chris Caputo6c88b442010-07-27 16:28:55 +00001145 /* This is safe, even with above unlocks, since we are just
1146 comparing pointers to the objects, not the objects themselves. */
paul718e3742002-12-13 20:15:29 +00001147 if (rn1 == rn2)
1148 return 1;
1149
1150 return 0;
1151}
1152
1153DEFUN (bgp_scan_time,
1154 bgp_scan_time_cmd,
1155 "bgp scan-time <5-60>",
1156 "BGP specific commands\n"
1157 "Configure background scanner interval\n"
1158 "Scanner interval (seconds)\n")
1159{
1160 bgp_scan_interval = atoi (argv[0]);
1161
1162 if (bgp_scan_thread)
1163 {
1164 thread_cancel (bgp_scan_thread);
1165 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001166 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001167 }
1168
1169 return CMD_SUCCESS;
1170}
1171
1172DEFUN (no_bgp_scan_time,
1173 no_bgp_scan_time_cmd,
1174 "no bgp scan-time",
1175 NO_STR
1176 "BGP specific commands\n"
1177 "Configure background scanner interval\n")
1178{
1179 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1180
1181 if (bgp_scan_thread)
1182 {
1183 thread_cancel (bgp_scan_thread);
1184 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001185 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001186 }
1187
1188 return CMD_SUCCESS;
1189}
1190
1191ALIAS (no_bgp_scan_time,
1192 no_bgp_scan_time_val_cmd,
1193 "no bgp scan-time <5-60>",
1194 NO_STR
1195 "BGP specific commands\n"
1196 "Configure background scanner interval\n"
1197 "Scanner interval (seconds)\n")
1198
1199DEFUN (show_ip_bgp_scan,
1200 show_ip_bgp_scan_cmd,
1201 "show ip bgp scan",
1202 SHOW_STR
1203 IP_STR
1204 BGP_STR
1205 "BGP scan status\n")
1206{
1207 struct bgp_node *rn;
1208 struct bgp_nexthop_cache *bnc;
1209
1210 if (bgp_scan_thread)
1211 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1212 else
1213 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1214 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1215
1216 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001217 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001218 if ((bnc = rn->info) != NULL)
1219 {
1220 if (bnc->valid)
1221 vty_out (vty, " %s valid [IGP metric %d]%s",
1222 inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE);
1223 else
1224 vty_out (vty, " %s invalid%s",
1225 inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE);
1226 }
1227
1228#ifdef HAVE_IPV6
1229 {
1230 char buf[BUFSIZ];
paul59320202004-11-09 01:54:03 +00001231 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1232 rn;
1233 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001234 if ((bnc = rn->info) != NULL)
1235 {
1236 if (bnc->valid)
1237 vty_out (vty, " %s valid [IGP metric %d]%s",
1238 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1239 bnc->metric, VTY_NEWLINE);
1240 else
1241 vty_out (vty, " %s invalid%s",
1242 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1243 VTY_NEWLINE);
1244 }
1245 }
1246#endif /* HAVE_IPV6 */
1247
1248 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001249 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1250 rn;
1251 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001252 if (rn->info != NULL)
1253 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1254 VTY_NEWLINE);
1255
1256#ifdef HAVE_IPV6
1257 {
1258 char buf[BUFSIZ];
1259
paul59320202004-11-09 01:54:03 +00001260 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1261 rn;
1262 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001263 if (rn->info != NULL)
1264 vty_out (vty, " %s/%d%s",
1265 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1266 rn->p.prefixlen,
1267 VTY_NEWLINE);
1268 }
1269#endif /* HAVE_IPV6 */
1270
1271 return CMD_SUCCESS;
1272}
1273
1274int
1275bgp_config_write_scan_time (struct vty *vty)
1276{
1277 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1278 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1279 return CMD_SUCCESS;
1280}
1281
1282void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -08001283bgp_scan_init (void)
paul718e3742002-12-13 20:15:29 +00001284{
1285 zlookup = zclient_new ();
1286 zlookup->sock = -1;
paul718e3742002-12-13 20:15:29 +00001287 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1288
1289 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1290 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1291
Paul Jakma64e580a2006-02-21 01:09:01 +00001292 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1293 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001294 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001295
Paul Jakma64e580a2006-02-21 01:09:01 +00001296 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001297
1298#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001299 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1300 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001301 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001302 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001303#endif /* HAVE_IPV6 */
1304
1305 /* Make BGP scan thread. */
paul59320202004-11-09 01:54:03 +00001306 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1307 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001308 /* Make BGP import there. */
1309 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001310
1311 install_element (BGP_NODE, &bgp_scan_time_cmd);
1312 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1313 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1314 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01001315 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
paul718e3742002-12-13 20:15:29 +00001316 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
1317}
Chris Caputo228da422009-07-18 05:44:03 +00001318
1319void
1320bgp_scan_finish (void)
1321{
Chris Caputo6c88b442010-07-27 16:28:55 +00001322 /* Only the current one needs to be reset. */
1323 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]);
1324
Chris Caputo228da422009-07-18 05:44:03 +00001325 bgp_table_unlock (cache1_table[AFI_IP]);
1326 cache1_table[AFI_IP] = NULL;
1327
1328 bgp_table_unlock (cache2_table[AFI_IP]);
1329 cache2_table[AFI_IP] = NULL;
1330
1331 bgp_table_unlock (bgp_connected_table[AFI_IP]);
1332 bgp_connected_table[AFI_IP] = NULL;
1333
1334#ifdef HAVE_IPV6
Chris Caputo6c88b442010-07-27 16:28:55 +00001335 /* Only the current one needs to be reset. */
1336 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]);
1337
Chris Caputo228da422009-07-18 05:44:03 +00001338 bgp_table_unlock (cache1_table[AFI_IP6]);
1339 cache1_table[AFI_IP6] = NULL;
1340
1341 bgp_table_unlock (cache2_table[AFI_IP6]);
1342 cache2_table[AFI_IP6] = NULL;
1343
1344 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
1345 bgp_connected_table[AFI_IP6] = NULL;
1346#endif /* HAVE_IPV6 */
1347}