blob: 434b2cdad836bb5b555c62fd7283fc5ed9759719 [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"
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +000031#include "hash.h"
32#include "jhash.h"
Donald Sharp04907292016-01-07 10:03:01 -050033#include "filter.h"
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050034#include "nexthop.h"
paul718e3742002-12-13 20:15:29 +000035
36#include "bgpd/bgpd.h"
37#include "bgpd/bgp_table.h"
38#include "bgpd/bgp_route.h"
39#include "bgpd/bgp_attr.h"
40#include "bgpd/bgp_nexthop.h"
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050041#include "bgpd/bgp_nht.h"
paul718e3742002-12-13 20:15:29 +000042#include "bgpd/bgp_debug.h"
43#include "bgpd/bgp_damp.h"
44#include "zebra/rib.h"
45#include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
46
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050047extern struct zclient *zclient;
48
paul718e3742002-12-13 20:15:29 +000049struct bgp_nexthop_cache *zlookup_query (struct in_addr);
paul718e3742002-12-13 20:15:29 +000050struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *);
David Lamparter6b0655a2014-06-04 06:53:35 +020051
paul718e3742002-12-13 20:15:29 +000052/* Only one BGP scan thread are activated at the same time. */
paul00d252c2005-05-23 14:19:54 +000053static struct thread *bgp_scan_thread = NULL;
paul718e3742002-12-13 20:15:29 +000054
55/* BGP import thread */
paul00d252c2005-05-23 14:19:54 +000056static struct thread *bgp_import_thread = NULL;
paul718e3742002-12-13 20:15:29 +000057
58/* BGP scan interval. */
paul00d252c2005-05-23 14:19:54 +000059static int bgp_scan_interval;
paul718e3742002-12-13 20:15:29 +000060
61/* BGP import interval. */
paul00d252c2005-05-23 14:19:54 +000062static int bgp_import_interval;
paul718e3742002-12-13 20:15:29 +000063
64/* Route table for next-hop lookup cache. */
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050065struct bgp_table *bgp_nexthop_cache_table[AFI_MAX];
paul00d252c2005-05-23 14:19:54 +000066static struct bgp_table *cache1_table[AFI_MAX];
67static struct bgp_table *cache2_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000068
69/* Route table for connected route. */
paul00d252c2005-05-23 14:19:54 +000070static struct bgp_table *bgp_connected_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000071
72/* BGP nexthop lookup query client. */
Chris Caputo228da422009-07-18 05:44:03 +000073struct zclient *zlookup = NULL;
David Lamparter6b0655a2014-06-04 06:53:35 +020074
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050075char *
76bnc_str (struct bgp_nexthop_cache *bnc, char *buf, int size)
77{
78 prefix2str(&(bnc->node->p), buf, size);
79 return buf;
80}
81
paul718e3742002-12-13 20:15:29 +000082/* Add nexthop to the end of the list. */
paul94f2b392005-06-28 12:44:16 +000083static void
paul718e3742002-12-13 20:15:29 +000084bnc_nexthop_add (struct bgp_nexthop_cache *bnc, struct nexthop *nexthop)
85{
86 struct nexthop *last;
87
88 for (last = bnc->nexthop; last && last->next; last = last->next)
89 ;
90 if (last)
91 last->next = nexthop;
92 else
93 bnc->nexthop = nexthop;
94 nexthop->prev = last;
95}
96
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050097void
paul718e3742002-12-13 20:15:29 +000098bnc_nexthop_free (struct bgp_nexthop_cache *bnc)
99{
100 struct nexthop *nexthop;
101 struct nexthop *next = NULL;
102
103 for (nexthop = bnc->nexthop; nexthop; nexthop = next)
104 {
105 next = nexthop->next;
106 XFREE (MTYPE_NEXTHOP, nexthop);
107 }
108}
109
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500110struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800111bnc_new (void)
paul718e3742002-12-13 20:15:29 +0000112{
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500113 struct bgp_nexthop_cache *bnc;
114
115 bnc = XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
116 LIST_INIT(&(bnc->paths));
117 return bnc;
paul718e3742002-12-13 20:15:29 +0000118}
119
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500120void
paul718e3742002-12-13 20:15:29 +0000121bnc_free (struct bgp_nexthop_cache *bnc)
122{
123 bnc_nexthop_free (bnc);
124 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
125}
David Lamparter6b0655a2014-06-04 06:53:35 +0200126
paul94f2b392005-06-28 12:44:16 +0000127static int
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400128bgp_nexthop_cache_different (struct bgp_nexthop_cache *bnc1,
paul718e3742002-12-13 20:15:29 +0000129 struct bgp_nexthop_cache *bnc2)
130{
131 int i;
132 struct nexthop *next1, *next2;
133
134 if (bnc1->nexthop_num != bnc2->nexthop_num)
135 return 1;
136
137 next1 = bnc1->nexthop;
138 next2 = bnc2->nexthop;
139
140 for (i = 0; i < bnc1->nexthop_num; i++)
141 {
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500142 if (! nexthop_same_no_recurse (next1, next2))
paul718e3742002-12-13 20:15:29 +0000143 return 1;
144
145 next1 = next1->next;
146 next2 = next2->next;
147 }
148 return 0;
149}
150
151/* If nexthop exists on connected network return 1. */
152int
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400153bgp_nexthop_onlink (afi_t afi, struct attr *attr)
paul718e3742002-12-13 20:15:29 +0000154{
155 struct bgp_node *rn;
Paul Jakmafc98d162012-01-09 11:36:23 +0000156
157 /* If zebra is not enabled return */
158 if (zlookup->sock < 0)
159 return 1;
160
paul718e3742002-12-13 20:15:29 +0000161 /* Lookup the address is onlink or not. */
162 if (afi == AFI_IP)
163 {
paul59320202004-11-09 01:54:03 +0000164 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
paul718e3742002-12-13 20:15:29 +0000165 if (rn)
166 {
167 bgp_unlock_node (rn);
168 return 1;
169 }
170 }
paul718e3742002-12-13 20:15:29 +0000171 else if (afi == AFI_IP6)
172 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000173 if (attr->extra->mp_nexthop_len == 32)
paul718e3742002-12-13 20:15:29 +0000174 return 1;
Paul Jakmafb982c22007-05-04 20:15:47 +0000175 else if (attr->extra->mp_nexthop_len == 16)
paul718e3742002-12-13 20:15:29 +0000176 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000177 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000178 return 1;
179
paul59320202004-11-09 01:54:03 +0000180 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
Paul Jakmafb982c22007-05-04 20:15:47 +0000181 &attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000182 if (rn)
183 {
184 bgp_unlock_node (rn);
185 return 1;
186 }
187 }
188 }
paul718e3742002-12-13 20:15:29 +0000189 return 0;
190}
191
paul718e3742002-12-13 20:15:29 +0000192/* Check specified next-hop is reachable or not. */
paul94f2b392005-06-28 12:44:16 +0000193static int
paul718e3742002-12-13 20:15:29 +0000194bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
195 int *metricchanged)
196{
197 struct bgp_node *rn;
198 struct prefix p;
199 struct bgp_nexthop_cache *bnc;
200 struct attr *attr;
Paul Jakmafc98d162012-01-09 11:36:23 +0000201
202 /* If lookup is not enabled, return valid. */
203 if (zlookup->sock < 0)
204 {
205 if (ri->extra)
206 ri->extra->igpmetric = 0;
207 return 1;
208 }
209
paul718e3742002-12-13 20:15:29 +0000210 /* Only check IPv6 global address only nexthop. */
211 attr = ri->attr;
212
Paul Jakmafb982c22007-05-04 20:15:47 +0000213 if (attr->extra->mp_nexthop_len != 16
214 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000215 return 1;
216
217 memset (&p, 0, sizeof (struct prefix));
218 p.family = AF_INET6;
219 p.prefixlen = IPV6_MAX_BITLEN;
Paul Jakmafb982c22007-05-04 20:15:47 +0000220 p.u.prefix6 = attr->extra->mp_nexthop_global;
paul718e3742002-12-13 20:15:29 +0000221
222 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000223 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
paul718e3742002-12-13 20:15:29 +0000224
225 if (rn->info)
226 {
227 bnc = rn->info;
228 bgp_unlock_node (rn);
229 }
230 else
231 {
Denis Ovsienkob64bfc12011-08-08 19:36:44 +0400232 if (NULL == (bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global)))
233 bnc = bnc_new ();
234 else
paul718e3742002-12-13 20:15:29 +0000235 {
paul718e3742002-12-13 20:15:29 +0000236 if (changed)
237 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400238 struct bgp_table *old;
239 struct bgp_node *oldrn;
240
paul59320202004-11-09 01:54:03 +0000241 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
242 old = cache2_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000243 else
paul59320202004-11-09 01:54:03 +0000244 old = cache1_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000245
246 oldrn = bgp_node_lookup (old, &p);
247 if (oldrn)
248 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400249 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000250
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400251 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000252
253 if (bnc->metric != oldbnc->metric)
254 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000255
256 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000257 }
258 }
259 }
paul718e3742002-12-13 20:15:29 +0000260 rn->info = bnc;
261 }
262
263 if (changed)
264 *changed = bnc->changed;
265
266 if (metricchanged)
267 *metricchanged = bnc->metricchanged;
268
Paul Jakmafb982c22007-05-04 20:15:47 +0000269 if (bnc->valid && bnc->metric)
270 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
271 else if (ri->extra)
272 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000273
274 return bnc->valid;
275}
paul718e3742002-12-13 20:15:29 +0000276
277/* Check specified next-hop is reachable or not. */
278int
279bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
280 int *changed, int *metricchanged)
281{
282 struct bgp_node *rn;
283 struct prefix p;
284 struct bgp_nexthop_cache *bnc;
285 struct in_addr addr;
Paul Jakmafc98d162012-01-09 11:36:23 +0000286
287 /* If lookup is not enabled, return valid. */
288 if (zlookup->sock < 0)
289 {
290 if (ri->extra)
291 ri->extra->igpmetric = 0;
292 return 1;
293 }
294
paul718e3742002-12-13 20:15:29 +0000295 if (afi == AFI_IP6)
296 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
paul718e3742002-12-13 20:15:29 +0000297
298 addr = ri->attr->nexthop;
299
300 memset (&p, 0, sizeof (struct prefix));
301 p.family = AF_INET;
302 p.prefixlen = IPV4_MAX_BITLEN;
303 p.u.prefix4 = addr;
304
305 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000306 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000307
308 if (rn->info)
309 {
310 bnc = rn->info;
311 bgp_unlock_node (rn);
312 }
313 else
314 {
Denis Ovsienkob64bfc12011-08-08 19:36:44 +0400315 if (NULL == (bnc = zlookup_query (addr)))
316 bnc = bnc_new ();
317 else
paul718e3742002-12-13 20:15:29 +0000318 {
paul718e3742002-12-13 20:15:29 +0000319 if (changed)
320 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400321 struct bgp_table *old;
322 struct bgp_node *oldrn;
323
paul59320202004-11-09 01:54:03 +0000324 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
325 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000326 else
paul59320202004-11-09 01:54:03 +0000327 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000328
329 oldrn = bgp_node_lookup (old, &p);
330 if (oldrn)
331 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400332 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000333
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400334 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000335
336 if (bnc->metric != oldbnc->metric)
337 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000338
339 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000340 }
341 }
342 }
paul718e3742002-12-13 20:15:29 +0000343 rn->info = bnc;
344 }
345
346 if (changed)
347 *changed = bnc->changed;
348
349 if (metricchanged)
350 *metricchanged = bnc->metricchanged;
351
Paul Jakmafb982c22007-05-04 20:15:47 +0000352 if (bnc->valid && bnc->metric)
353 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
354 else if (ri->extra)
355 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000356
357 return bnc->valid;
358}
359
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500360#if BGP_SCAN_NEXTHOP
paul718e3742002-12-13 20:15:29 +0000361/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000362static void
paul718e3742002-12-13 20:15:29 +0000363bgp_nexthop_cache_reset (struct bgp_table *table)
364{
365 struct bgp_node *rn;
366 struct bgp_nexthop_cache *bnc;
367
368 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
369 if ((bnc = rn->info) != NULL)
370 {
371 bnc_free (bnc);
372 rn->info = NULL;
373 bgp_unlock_node (rn);
374 }
375}
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500376#endif
paul718e3742002-12-13 20:15:29 +0000377
paul94f2b392005-06-28 12:44:16 +0000378static void
paul59320202004-11-09 01:54:03 +0000379bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000380{
381 struct bgp_node *rn;
382 struct bgp *bgp;
383 struct bgp_info *bi;
384 struct bgp_info *next;
385 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000386 struct listnode *node, *nnode;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500387#if BGP_SCAN_NEXTHOP
paul718e3742002-12-13 20:15:29 +0000388 int valid;
389 int current;
390 int changed;
391 int metricchanged;
392
393 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000394 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
395 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000396 else
paul59320202004-11-09 01:54:03 +0000397 bgp_nexthop_cache_table[afi] = cache1_table[afi];
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500398#endif
paul718e3742002-12-13 20:15:29 +0000399
400 /* Get default bgp. */
401 bgp = bgp_get_default ();
402 if (bgp == NULL)
403 return;
404
405 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000406 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000407 {
408 if (peer->status != Established)
409 continue;
410
paul59320202004-11-09 01:54:03 +0000411 if (peer->afc[afi][SAFI_UNICAST])
412 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
413 if (peer->afc[afi][SAFI_MULTICAST])
414 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
415 if (peer->afc[afi][SAFI_MPLS_VPN])
416 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
paul718e3742002-12-13 20:15:29 +0000417 }
418
paul59320202004-11-09 01:54:03 +0000419 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000420 rn = bgp_route_next (rn))
421 {
422 for (bi = rn->info; bi; bi = next)
423 {
424 next = bi->next;
425
426 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
427 {
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500428#if BGP_SCAN_NEXTHOP
paul718e3742002-12-13 20:15:29 +0000429 changed = 0;
430 metricchanged = 0;
431
Christian Frankea0f6ce52013-04-11 08:24:30 +0000432 if (bi->peer->sort == BGP_PEER_EBGP && bi->peer->ttl == 1
433 && !CHECK_FLAG(bi->peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400434 valid = bgp_nexthop_onlink (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000435 else
paul59320202004-11-09 01:54:03 +0000436 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000437 &changed, &metricchanged);
438
439 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
440
441 if (changed)
442 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
443 else
444 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
445
446 if (valid != current)
447 {
448 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
449 {
450 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000451 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000452 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000453 }
454 else
455 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000456 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000457 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000458 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000459 }
460 }
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500461#endif
paul718e3742002-12-13 20:15:29 +0000462
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 }
Paul Jakma91b9e852015-12-01 14:32:11 +0000471 if (rn->info)
472 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000473 }
474
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500475#if BGP_SCAN_NEXTHOP
paul718e3742002-12-13 20:15:29 +0000476 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000477 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
478 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000479 else
paul59320202004-11-09 01:54:03 +0000480 bgp_nexthop_cache_reset (cache1_table[afi]);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500481#endif
hassof4184462005-02-01 20:13:16 +0000482
483 if (BGP_DEBUG (events, EVENTS))
484 {
485 if (afi == AFI_IP)
486 zlog_debug ("scanning IPv4 Unicast routing tables");
487 else if (afi == AFI_IP6)
488 zlog_debug ("scanning IPv6 Unicast routing tables");
489 }
Christian Frankedcab1bb2012-12-07 16:45:52 +0000490
491 /* Reevaluate default-originate route-maps and announce/withdraw
492 * default route if neccesary. */
493 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
494 {
495 if (peer->status == Established
496 && CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE)
497 && peer->default_rmap[afi][safi].name)
498 bgp_default_originate (peer, afi, safi, 0);
499 }
paul718e3742002-12-13 20:15:29 +0000500}
501
paul718e3742002-12-13 20:15:29 +0000502/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000503static int
paul59320202004-11-09 01:54:03 +0000504bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000505{
506 bgp_scan_thread =
Donald Sharp774914f2015-10-14 08:50:39 -0400507 thread_add_timer (bm->master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000508
hassof4184462005-02-01 20:13:16 +0000509 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000510 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000511
paul59320202004-11-09 01:54:03 +0000512 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000513
paul59320202004-11-09 01:54:03 +0000514 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000515
516 return 0;
517}
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000518
519/* BGP own address structure */
520struct bgp_addr
521{
522 struct in_addr addr;
523 int refcnt;
524};
525
526static struct hash *bgp_address_hash;
527
528static void *
529bgp_address_hash_alloc (void *p)
530{
531 struct in_addr *val = p;
532 struct bgp_addr *addr;
533
534 addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr));
535 addr->refcnt = 0;
536 addr->addr.s_addr = val->s_addr;
537
538 return addr;
539}
540
541static unsigned int
542bgp_address_hash_key_make (void *p)
543{
544 const struct bgp_addr *addr = p;
545
546 return jhash_1word(addr->addr.s_addr, 0);
547}
548
549static int
550bgp_address_hash_cmp (const void *p1, const void *p2)
551{
552 const struct bgp_addr *addr1 = p1;
553 const struct bgp_addr *addr2 = p2;
554
555 return addr1->addr.s_addr == addr2->addr.s_addr;
556}
557
558void
559bgp_address_init (void)
560{
561 bgp_address_hash = hash_create (bgp_address_hash_key_make,
562 bgp_address_hash_cmp);
563}
564
Lou Berger82dd7072016-01-12 13:41:57 -0500565void
566bgp_address_destroy (void)
567{
Lou Bergerd5d5e3e2016-01-12 13:41:58 -0500568 if (bgp_address_hash == NULL)
569 return;
570
Lou Berger82dd7072016-01-12 13:41:57 -0500571 hash_clean(bgp_address_hash, NULL);
572 hash_free(bgp_address_hash);
573 bgp_address_hash = NULL;
574}
575
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000576static void
577bgp_address_add (struct prefix *p)
578{
579 struct bgp_addr tmp;
580 struct bgp_addr *addr;
581
582 tmp.addr = p->u.prefix4;
583
584 addr = hash_get (bgp_address_hash, &tmp, bgp_address_hash_alloc);
Pradosh Mohapatra94627e62015-11-09 20:21:45 -0500585 if (!addr)
586 return;
587
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000588 addr->refcnt++;
589}
590
591static void
592bgp_address_del (struct prefix *p)
593{
594 struct bgp_addr tmp;
595 struct bgp_addr *addr;
596
597 tmp.addr = p->u.prefix4;
598
599 addr = hash_lookup (bgp_address_hash, &tmp);
Rakesh Garimella9e47abd2013-03-11 12:38:31 +0000600 /* may have been deleted earlier by bgp_interface_down() */
601 if (addr == NULL)
602 return;
603
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000604 addr->refcnt--;
605
606 if (addr->refcnt == 0)
607 {
608 hash_release (bgp_address_hash, addr);
609 XFREE (MTYPE_BGP_ADDR, addr);
610 }
611}
612
David Lamparter6b0655a2014-06-04 06:53:35 +0200613
paul59320202004-11-09 01:54:03 +0000614struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000615{
616 unsigned int refcnt;
617};
618
619void
620bgp_connected_add (struct connected *ifc)
621{
622 struct prefix p;
623 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000624 struct interface *ifp;
625 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000626 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000627
628 ifp = ifc->ifp;
629
630 if (! ifp)
631 return;
632
633 if (if_is_loopback (ifp))
634 return;
635
636 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000637
Donald Sharp26a18eb2015-09-29 09:25:10 -0400638 p = *(CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000639 if (addr->family == AF_INET)
640 {
paul718e3742002-12-13 20:15:29 +0000641 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
642
643 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
644 return;
645
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000646 bgp_address_add (addr);
647
paul59320202004-11-09 01:54:03 +0000648 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000649 if (rn->info)
650 {
651 bc = rn->info;
652 bc->refcnt++;
653 }
654 else
655 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000656 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000657 bc->refcnt = 1;
658 rn->info = bc;
659 }
660 }
Andrew J. Schorre4529632006-12-12 19:18:21 +0000661 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000662 {
paul718e3742002-12-13 20:15:29 +0000663 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
664
665 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
666 return;
667
668 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
669 return;
670
paul59320202004-11-09 01:54:03 +0000671 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000672 if (rn->info)
673 {
674 bc = rn->info;
675 bc->refcnt++;
676 }
677 else
678 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000679 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000680 bc->refcnt = 1;
681 rn->info = bc;
682 }
683 }
paul718e3742002-12-13 20:15:29 +0000684}
685
686void
687bgp_connected_delete (struct connected *ifc)
688{
689 struct prefix p;
690 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000691 struct interface *ifp;
692 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000693 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000694
695 ifp = ifc->ifp;
696
697 if (if_is_loopback (ifp))
698 return;
699
700 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000701
Donald Sharp26a18eb2015-09-29 09:25:10 -0400702 p = *(CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000703 if (addr->family == AF_INET)
704 {
paul718e3742002-12-13 20:15:29 +0000705 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
706
707 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
708 return;
709
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000710 bgp_address_del (addr);
711
paul59320202004-11-09 01:54:03 +0000712 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000713 if (! rn)
714 return;
715
716 bc = rn->info;
717 bc->refcnt--;
718 if (bc->refcnt == 0)
719 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000720 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000721 rn->info = NULL;
722 }
723 bgp_unlock_node (rn);
724 bgp_unlock_node (rn);
725 }
paul718e3742002-12-13 20:15:29 +0000726 else if (addr->family == AF_INET6)
727 {
paul718e3742002-12-13 20:15:29 +0000728 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
729
730 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
731 return;
732
733 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
734 return;
735
paul59320202004-11-09 01:54:03 +0000736 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000737 if (! rn)
738 return;
739
740 bc = rn->info;
741 bc->refcnt--;
742 if (bc->refcnt == 0)
743 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000744 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000745 rn->info = NULL;
746 }
747 bgp_unlock_node (rn);
748 bgp_unlock_node (rn);
749 }
paul718e3742002-12-13 20:15:29 +0000750}
751
752int
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000753bgp_nexthop_self (struct attr *attr)
paul718e3742002-12-13 20:15:29 +0000754{
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000755 struct bgp_addr tmp, *addr;
paul718e3742002-12-13 20:15:29 +0000756
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000757 tmp.addr = attr->nexthop;
paul718e3742002-12-13 20:15:29 +0000758
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000759 addr = hash_lookup (bgp_address_hash, &tmp);
760 if (addr)
761 return 1;
762
paul718e3742002-12-13 20:15:29 +0000763 return 0;
764}
David Lamparter6b0655a2014-06-04 06:53:35 +0200765
paul94f2b392005-06-28 12:44:16 +0000766static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800767zlookup_read (void)
paul718e3742002-12-13 20:15:29 +0000768{
769 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000770 uint16_t length;
771 u_char marker;
772 u_char version;
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200773 uint16_t vrf_id;
774 uint16_t command;
775 int err;
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100776 struct in_addr raddr __attribute__((unused));
pauld3092e72006-01-17 17:33:46 +0000777 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000778 int i;
779 u_char nexthop_num;
780 struct nexthop *nexthop;
781 struct bgp_nexthop_cache *bnc;
782
783 s = zlookup->ibuf;
784 stream_reset (s);
785
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200786 err = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
787 &vrf_id, &command);
788 if (err < 0)
789 {
790 zlog_err("%s: zserv_read_header() failed", __func__);
791 return NULL;
792 }
Donald Sharpa9d4cb32015-09-17 10:54:25 -0400793
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100794 /* XXX: not doing anything with raddr */
paul718e3742002-12-13 20:15:29 +0000795 raddr.s_addr = stream_get_ipv4 (s);
796 metric = stream_getl (s);
797 nexthop_num = stream_getc (s);
798
799 if (nexthop_num)
800 {
801 bnc = bnc_new ();
802 bnc->valid = 1;
803 bnc->metric = metric;
804 bnc->nexthop_num = nexthop_num;
805
806 for (i = 0; i < nexthop_num; i++)
807 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700808 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000809 nexthop->type = stream_getc (s);
810 switch (nexthop->type)
811 {
812 case ZEBRA_NEXTHOP_IPV4:
813 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
814 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000815 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
816 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
817 nexthop->ifindex = stream_getl (s);
818 break;
paul718e3742002-12-13 20:15:29 +0000819 case ZEBRA_NEXTHOP_IFINDEX:
820 case ZEBRA_NEXTHOP_IFNAME:
821 nexthop->ifindex = stream_getl (s);
822 break;
hassofa2b17e2004-03-04 17:45:00 +0000823 default:
824 /* do nothing */
825 break;
paul718e3742002-12-13 20:15:29 +0000826 }
827 bnc_nexthop_add (bnc, nexthop);
828 }
829 }
830 else
831 return NULL;
832
833 return bnc;
834}
835
836struct bgp_nexthop_cache *
837zlookup_query (struct in_addr addr)
838{
839 int ret;
840 struct stream *s;
841
842 /* Check socket. */
843 if (zlookup->sock < 0)
844 return NULL;
845
846 s = zlookup->obuf;
847 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +0800848 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000849 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000850
851 stream_putw_at (s, 0, stream_get_endp (s));
852
853 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000854 if (ret < 0)
855 {
856 zlog_err ("can't write to zlookup->sock");
857 close (zlookup->sock);
858 zlookup->sock = -1;
859 return NULL;
860 }
861 if (ret == 0)
862 {
863 zlog_err ("zlookup->sock connection closed");
864 close (zlookup->sock);
865 zlookup->sock = -1;
866 return NULL;
867 }
868
869 return zlookup_read ();
870}
871
paul94f2b392005-06-28 12:44:16 +0000872static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800873zlookup_read_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000874{
875 struct stream *s;
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200876 uint16_t length, vrf_id, cmd;
pauld3092e72006-01-17 17:33:46 +0000877 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000878 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000879 uint32_t metric;
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200880 int i, err;
paul718e3742002-12-13 20:15:29 +0000881 u_char nexthop_num;
882 struct nexthop *nexthop;
883 struct bgp_nexthop_cache *bnc;
884
885 s = zlookup->ibuf;
886 stream_reset (s);
887
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200888 err = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
889 &vrf_id, &cmd);
890 if (err < 0)
891 {
892 zlog_err("%s: zserv_read_header() failed", __func__);
893 return NULL;
894 }
Donald Sharpa9d4cb32015-09-17 10:54:25 -0400895
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100896 /* XXX: not actually doing anything with raddr */
paul718e3742002-12-13 20:15:29 +0000897 stream_get (&raddr, s, 16);
898
899 metric = stream_getl (s);
900 nexthop_num = stream_getc (s);
901
902 if (nexthop_num)
903 {
904 bnc = bnc_new ();
905 bnc->valid = 1;
906 bnc->metric = metric;
907 bnc->nexthop_num = nexthop_num;
908
909 for (i = 0; i < nexthop_num; i++)
910 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700911 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000912 nexthop->type = stream_getc (s);
913 switch (nexthop->type)
914 {
915 case ZEBRA_NEXTHOP_IPV6:
916 stream_get (&nexthop->gate.ipv6, s, 16);
917 break;
918 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
919 case ZEBRA_NEXTHOP_IPV6_IFNAME:
920 stream_get (&nexthop->gate.ipv6, s, 16);
921 nexthop->ifindex = stream_getl (s);
922 break;
923 case ZEBRA_NEXTHOP_IFINDEX:
924 case ZEBRA_NEXTHOP_IFNAME:
925 nexthop->ifindex = stream_getl (s);
926 break;
hassofa2b17e2004-03-04 17:45:00 +0000927 default:
928 /* do nothing */
929 break;
paul718e3742002-12-13 20:15:29 +0000930 }
931 bnc_nexthop_add (bnc, nexthop);
932 }
933 }
934 else
935 return NULL;
936
937 return bnc;
938}
939
940struct bgp_nexthop_cache *
941zlookup_query_ipv6 (struct in6_addr *addr)
942{
943 int ret;
944 struct stream *s;
945
946 /* Check socket. */
947 if (zlookup->sock < 0)
948 return NULL;
949
950 s = zlookup->obuf;
951 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +0800952 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000953 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000954 stream_putw_at (s, 0, stream_get_endp (s));
955
956 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000957 if (ret < 0)
958 {
959 zlog_err ("can't write to zlookup->sock");
960 close (zlookup->sock);
961 zlookup->sock = -1;
962 return NULL;
963 }
964 if (ret == 0)
965 {
966 zlog_err ("zlookup->sock connection closed");
967 close (zlookup->sock);
968 zlookup->sock = -1;
969 return NULL;
970 }
971
972 return zlookup_read_ipv6 ();
973}
paul718e3742002-12-13 20:15:29 +0000974
paul94f2b392005-06-28 12:44:16 +0000975static int
976bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
977 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000978{
979 struct stream *s;
980 int ret;
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200981 u_int16_t length, vrf_id, command;
pauld3092e72006-01-17 17:33:46 +0000982 u_char version, marker;
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100983 struct in_addr addr __attribute__((unused));
paul718e3742002-12-13 20:15:29 +0000984 struct in_addr nexthop;
985 u_int32_t metric = 0;
986 u_char nexthop_num;
987 u_char nexthop_type;
988
989 /* If lookup connection is not available return valid. */
990 if (zlookup->sock < 0)
991 {
992 if (igpmetric)
993 *igpmetric = 0;
994 return 1;
995 }
996
997 /* Send query to the lookup connection */
998 s = zlookup->obuf;
999 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +08001000 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, VRF_DEFAULT);
pauld3092e72006-01-17 17:33:46 +00001001
paul718e3742002-12-13 20:15:29 +00001002 stream_putc (s, p->prefixlen);
1003 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +00001004
1005 stream_putw_at (s, 0, stream_get_endp (s));
1006
paul718e3742002-12-13 20:15:29 +00001007 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +00001008 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +00001009
1010 if (ret < 0)
1011 {
1012 zlog_err ("can't write to zlookup->sock");
1013 close (zlookup->sock);
1014 zlookup->sock = -1;
1015 return 1;
1016 }
1017 if (ret == 0)
1018 {
1019 zlog_err ("zlookup->sock connection closed");
1020 close (zlookup->sock);
1021 zlookup->sock = -1;
1022 return 1;
1023 }
1024
1025 /* Get result. */
1026 stream_reset (s);
1027
Nicolas Dichtel794c4732015-09-16 09:42:36 +02001028 ret = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
1029 &vrf_id, &command);
1030 if (ret < 0)
1031 {
1032 zlog_err("%s: zserv_read_header() failed", __func__);
1033 return 0;
1034 }
Donald Sharpa9d4cb32015-09-17 10:54:25 -04001035
Paul Jakma7aa9dce2014-09-19 14:42:23 +01001036 /* XXX: not using addr */
paul718e3742002-12-13 20:15:29 +00001037 addr.s_addr = stream_get_ipv4 (s);
1038 metric = stream_getl (s);
1039 nexthop_num = stream_getc (s);
1040
1041 /* Set IGP metric value. */
1042 if (igpmetric)
1043 *igpmetric = metric;
1044
1045 /* If there is nexthop then this is active route. */
1046 if (nexthop_num)
1047 {
1048 nexthop.s_addr = 0;
1049 nexthop_type = stream_getc (s);
Christian Frankea12afd52013-05-25 14:01:36 +00001050 switch (nexthop_type)
paul718e3742002-12-13 20:15:29 +00001051 {
Christian Frankea12afd52013-05-25 14:01:36 +00001052 case ZEBRA_NEXTHOP_IPV4:
paul718e3742002-12-13 20:15:29 +00001053 nexthop.s_addr = stream_get_ipv4 (s);
Christian Frankea12afd52013-05-25 14:01:36 +00001054 break;
1055 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1056 nexthop.s_addr = stream_get_ipv4 (s);
1057 /* ifindex */ (void)stream_getl (s);
1058 break;
1059 default:
1060 /* do nothing */
1061 break;
paul718e3742002-12-13 20:15:29 +00001062 }
Christian Frankea12afd52013-05-25 14:01:36 +00001063 *igpnexthop = nexthop;
paul718e3742002-12-13 20:15:29 +00001064
1065 return 1;
1066 }
1067 else
1068 return 0;
1069}
1070
1071/* Scan all configured BGP route then check the route exists in IGP or
1072 not. */
paul94f2b392005-06-28 12:44:16 +00001073static int
paul718e3742002-12-13 20:15:29 +00001074bgp_import (struct thread *t)
1075{
1076 struct bgp *bgp;
1077 struct bgp_node *rn;
1078 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001079 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001080 int valid;
1081 u_int32_t metric;
1082 struct in_addr nexthop;
1083 afi_t afi;
1084 safi_t safi;
1085
1086 bgp_import_thread =
Donald Sharp774914f2015-10-14 08:50:39 -04001087 thread_add_timer (bm->master, bgp_import, NULL, bgp_import_interval);
paul718e3742002-12-13 20:15:29 +00001088
hassof4184462005-02-01 20:13:16 +00001089 if (BGP_DEBUG (events, EVENTS))
1090 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001091
paul1eb8ef22005-04-07 07:30:20 +00001092 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001093 {
1094 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1095 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1096 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1097 rn = bgp_route_next (rn))
1098 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001099 {
paul6cbbc3c2003-04-28 17:11:02 +00001100 if (bgp_static->backdoor)
1101 continue;
paul718e3742002-12-13 20:15:29 +00001102
paul6cbbc3c2003-04-28 17:11:02 +00001103 valid = bgp_static->valid;
1104 metric = bgp_static->igpmetric;
1105 nexthop = bgp_static->igpnexthop;
1106
1107 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1108 && afi == AFI_IP && safi == SAFI_UNICAST)
1109 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1110 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001111 else
paul6cbbc3c2003-04-28 17:11:02 +00001112 {
1113 bgp_static->valid = 1;
1114 bgp_static->igpmetric = 0;
1115 bgp_static->igpnexthop.s_addr = 0;
1116 }
1117
1118 if (bgp_static->valid != valid)
1119 {
1120 if (bgp_static->valid)
1121 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1122 else
1123 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1124 }
1125 else if (bgp_static->valid)
1126 {
1127 if (bgp_static->igpmetric != metric
1128 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1129 || bgp_static->rmap.name)
1130 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1131 }
paul718e3742002-12-13 20:15:29 +00001132 }
paul6cbbc3c2003-04-28 17:11:02 +00001133 }
paul718e3742002-12-13 20:15:29 +00001134 return 0;
1135}
1136
1137/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001138static int
paul718e3742002-12-13 20:15:29 +00001139zlookup_connect (struct thread *t)
1140{
1141 struct zclient *zlookup;
1142
1143 zlookup = THREAD_ARG (t);
1144 zlookup->t_connect = NULL;
1145
1146 if (zlookup->sock != -1)
1147 return 0;
1148
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001149 if (zclient_socket_connect (zlookup) < 0)
paul718e3742002-12-13 20:15:29 +00001150 return -1;
1151
paul718e3742002-12-13 20:15:29 +00001152 return 0;
1153}
1154
1155/* Check specified multiaccess next-hop. */
1156int
1157bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1158{
1159 struct bgp_node *rn1;
1160 struct bgp_node *rn2;
1161 struct prefix p1;
1162 struct prefix p2;
1163 struct in_addr addr;
1164 int ret;
1165
1166 ret = inet_aton (peer, &addr);
1167 if (! ret)
1168 return 0;
1169
1170 memset (&p1, 0, sizeof (struct prefix));
1171 p1.family = AF_INET;
1172 p1.prefixlen = IPV4_MAX_BITLEN;
1173 p1.u.prefix4 = nexthop;
1174 memset (&p2, 0, sizeof (struct prefix));
1175 p2.family = AF_INET;
1176 p2.prefixlen = IPV4_MAX_BITLEN;
1177 p2.u.prefix4 = addr;
1178
1179 /* If bgp scan is not enabled, return invalid. */
1180 if (zlookup->sock < 0)
1181 return 0;
1182
paul59320202004-11-09 01:54:03 +00001183 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001184 if (! rn1)
1185 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001186 bgp_unlock_node (rn1);
paul718e3742002-12-13 20:15:29 +00001187
paul59320202004-11-09 01:54:03 +00001188 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001189 if (! rn2)
1190 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001191 bgp_unlock_node (rn2);
paul718e3742002-12-13 20:15:29 +00001192
Chris Caputo6c88b442010-07-27 16:28:55 +00001193 /* This is safe, even with above unlocks, since we are just
1194 comparing pointers to the objects, not the objects themselves. */
paul718e3742002-12-13 20:15:29 +00001195 if (rn1 == rn2)
1196 return 1;
1197
1198 return 0;
1199}
David Lamparter6b0655a2014-06-04 06:53:35 +02001200
paul718e3742002-12-13 20:15:29 +00001201DEFUN (bgp_scan_time,
1202 bgp_scan_time_cmd,
1203 "bgp scan-time <5-60>",
1204 "BGP specific commands\n"
1205 "Configure background scanner interval\n"
1206 "Scanner interval (seconds)\n")
1207{
1208 bgp_scan_interval = atoi (argv[0]);
1209
1210 if (bgp_scan_thread)
1211 {
1212 thread_cancel (bgp_scan_thread);
1213 bgp_scan_thread =
Donald Sharp774914f2015-10-14 08:50:39 -04001214 thread_add_timer (bm->master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001215 }
1216
1217 return CMD_SUCCESS;
1218}
1219
1220DEFUN (no_bgp_scan_time,
1221 no_bgp_scan_time_cmd,
1222 "no bgp scan-time",
1223 NO_STR
1224 "BGP specific commands\n"
1225 "Configure background scanner interval\n")
1226{
1227 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1228
1229 if (bgp_scan_thread)
1230 {
1231 thread_cancel (bgp_scan_thread);
1232 bgp_scan_thread =
Donald Sharp774914f2015-10-14 08:50:39 -04001233 thread_add_timer (bm->master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001234 }
1235
1236 return CMD_SUCCESS;
1237}
1238
1239ALIAS (no_bgp_scan_time,
1240 no_bgp_scan_time_val_cmd,
1241 "no bgp scan-time <5-60>",
1242 NO_STR
1243 "BGP specific commands\n"
1244 "Configure background scanner interval\n"
1245 "Scanner interval (seconds)\n")
1246
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001247static int
1248show_ip_bgp_scan_tables (struct vty *vty, const char detail)
paul718e3742002-12-13 20:15:29 +00001249{
1250 struct bgp_node *rn;
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001251 char buf[INET6_ADDRSTRLEN];
paul718e3742002-12-13 20:15:29 +00001252
1253 if (bgp_scan_thread)
1254 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1255 else
1256 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1257 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1258
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001259#if BGP_SCAN_NEXTHOP
paul718e3742002-12-13 20:15:29 +00001260 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001261 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001262 if ((bnc = rn->info) != NULL)
1263 {
1264 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001265 {
paul718e3742002-12-13 20:15:29 +00001266 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001267 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1268 if (detail)
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001269 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
1270 switch (nexthop->type)
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001271 {
1272 case NEXTHOP_TYPE_IPV4:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001273 vty_out (vty, " gate %s%s", inet_ntop (AF_INET, &nexthop->gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001274 break;
Christian Frankebb97e462013-05-25 14:01:35 +00001275 case NEXTHOP_TYPE_IPV4_IFINDEX:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001276 vty_out (vty, " gate %s", inet_ntop (AF_INET, &nexthop->gate.ipv4, buf, INET6_ADDRSTRLEN));
1277 vty_out (vty, " ifidx %u%s", nexthop->ifindex, VTY_NEWLINE);
Christian Frankebb97e462013-05-25 14:01:35 +00001278 break;
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001279 case NEXTHOP_TYPE_IFINDEX:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001280 vty_out (vty, " ifidx %u%s", nexthop->ifindex, VTY_NEWLINE);
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001281 break;
1282 default:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001283 vty_out (vty, " invalid nexthop type %u%s", nexthop->type, VTY_NEWLINE);
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001284 }
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001285 }
paul718e3742002-12-13 20:15:29 +00001286 else
1287 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001288 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001289 }
1290
paul718e3742002-12-13 20:15:29 +00001291 {
paul59320202004-11-09 01:54:03 +00001292 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1293 rn;
1294 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001295 if ((bnc = rn->info) != NULL)
1296 {
1297 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001298 {
paul718e3742002-12-13 20:15:29 +00001299 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001300 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001301 bnc->metric, VTY_NEWLINE);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001302 if (detail)
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001303 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
1304 switch (nexthop->type)
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001305 {
1306 case NEXTHOP_TYPE_IPV6:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001307 vty_out (vty, " gate %s%s", inet_ntop (AF_INET6, &nexthop->gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001308 break;
1309 case NEXTHOP_TYPE_IFINDEX:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001310 vty_out (vty, " ifidx %u%s", nexthop->ifindex, VTY_NEWLINE);
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001311 break;
1312 default:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001313 vty_out (vty, " invalid nexthop type %u%s", nexthop->type, VTY_NEWLINE);
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001314 }
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001315 }
paul718e3742002-12-13 20:15:29 +00001316 else
1317 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001318 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001319 VTY_NEWLINE);
1320 }
1321 }
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001322#else
1323 vty_out (vty, "BGP next-hop tracking is on%s", VTY_NEWLINE);
1324#endif
paul718e3742002-12-13 20:15:29 +00001325 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001326 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1327 rn;
1328 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001329 if (rn->info != NULL)
1330 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1331 VTY_NEWLINE);
1332
paul718e3742002-12-13 20:15:29 +00001333 {
paul59320202004-11-09 01:54:03 +00001334 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1335 rn;
1336 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001337 if (rn->info != NULL)
1338 vty_out (vty, " %s/%d%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001339 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001340 rn->p.prefixlen,
1341 VTY_NEWLINE);
1342 }
paul718e3742002-12-13 20:15:29 +00001343
1344 return CMD_SUCCESS;
1345}
1346
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001347static int
1348show_ip_bgp_nexthop_table (struct vty *vty, int detail)
1349{
1350 struct bgp_node *rn;
1351 struct bgp_nexthop_cache *bnc;
1352 char buf[INET6_ADDRSTRLEN];
1353 struct nexthop *nexthop;
1354 time_t tbuf;
1355 afi_t afi;
1356
1357 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
1358 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
1359 {
1360 for (rn = bgp_table_top (bgp_nexthop_cache_table[afi]); rn; rn = bgp_route_next (rn))
1361 {
1362 if ((bnc = rn->info) != NULL)
1363 {
1364 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID))
1365 {
1366 vty_out (vty, " %s valid [IGP metric %d], #paths %d%s",
1367 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, sizeof (buf)),
1368 bnc->metric, bnc->path_count, VTY_NEWLINE);
1369 if (detail)
1370 for (nexthop = bnc->nexthop ; nexthop; nexthop = nexthop->next)
1371 switch (nexthop->type)
1372 {
1373 case NEXTHOP_TYPE_IPV6:
1374 vty_out (vty, " gate %s%s",
1375 inet_ntop (AF_INET6, &nexthop->gate.ipv6,
1376 buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1377 break;
1378 case NEXTHOP_TYPE_IPV6_IFINDEX:
1379 vty_out(vty, " gate %s, if %s%s",
1380 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
1381 INET6_ADDRSTRLEN),
1382 ifindex2ifname(nexthop->ifindex),
1383 VTY_NEWLINE);
1384 break;
1385 case NEXTHOP_TYPE_IPV4:
1386 vty_out (vty, " gate %s%s",
1387 inet_ntop (AF_INET, &nexthop->gate.ipv4, buf,
1388 INET6_ADDRSTRLEN), VTY_NEWLINE);
1389 break;
1390 case NEXTHOP_TYPE_IFINDEX:
1391 vty_out (vty, " if %s%s",
1392 ifindex2ifname(nexthop->ifindex), VTY_NEWLINE);
1393 break;
1394 case NEXTHOP_TYPE_IPV4_IFINDEX:
1395 vty_out (vty, " gate %s, if %s%s",
1396 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
1397 INET6_ADDRSTRLEN),
1398 ifindex2ifname(nexthop->ifindex), VTY_NEWLINE);
1399 break;
1400 default:
1401 vty_out (vty, " invalid nexthop type %u%s",
1402 nexthop->type, VTY_NEWLINE);
1403 }
1404 }
1405 else
1406 vty_out (vty, " %s invalid%s",
1407 inet_ntop (AF_INET, &rn->p.u.prefix, buf, sizeof (buf)), VTY_NEWLINE);
1408#ifdef HAVE_CLOCK_MONOTONIC
1409 tbuf = time(NULL) - (bgp_clock() - bnc->last_update);
1410 vty_out (vty, " Last update: %s", ctime(&tbuf));
1411#else
1412 vty_out (vty, " Last update: %s", ctime(&bnc->uptime));
1413#endif /* HAVE_CLOCK_MONOTONIC */
1414 vty_out(vty, "%s", VTY_NEWLINE);
1415 }
1416 }
1417 }
1418 return CMD_SUCCESS;
1419}
1420
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001421DEFUN (show_ip_bgp_scan,
1422 show_ip_bgp_scan_cmd,
1423 "show ip bgp scan",
1424 SHOW_STR
1425 IP_STR
1426 BGP_STR
1427 "BGP scan status\n")
1428{
1429 return show_ip_bgp_scan_tables (vty, 0);
1430}
1431
1432DEFUN (show_ip_bgp_scan_detail,
1433 show_ip_bgp_scan_detail_cmd,
1434 "show ip bgp scan detail",
1435 SHOW_STR
1436 IP_STR
1437 BGP_STR
1438 "BGP scan status\n"
1439 "More detailed output\n")
1440{
1441 return show_ip_bgp_scan_tables (vty, 1);
1442}
1443
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001444DEFUN (show_ip_bgp_nexthop,
1445 show_ip_bgp_nexthop_cmd,
1446 "show ip bgp nexthop",
1447 SHOW_STR
1448 IP_STR
1449 BGP_STR
1450 "BGP nexthop table\n")
1451{
1452 return show_ip_bgp_nexthop_table (vty, 0);
1453}
1454
1455DEFUN (show_ip_bgp_nexthop_detail,
1456 show_ip_bgp_nexthop_detail_cmd,
1457 "show ip bgp nexthop detail",
1458 SHOW_STR
1459 IP_STR
1460 BGP_STR
1461 "BGP nexthop table\n")
1462{
1463 return show_ip_bgp_nexthop_table (vty, 1);
1464}
1465
paul718e3742002-12-13 20:15:29 +00001466int
1467bgp_config_write_scan_time (struct vty *vty)
1468{
1469 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1470 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1471 return CMD_SUCCESS;
1472}
1473
1474void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -08001475bgp_scan_init (void)
paul718e3742002-12-13 20:15:29 +00001476{
Donald Sharp774914f2015-10-14 08:50:39 -04001477 zlookup = zclient_new (bm->master);
paul718e3742002-12-13 20:15:29 +00001478 zlookup->sock = -1;
Donald Sharp774914f2015-10-14 08:50:39 -04001479 zlookup->t_connect = thread_add_event (bm->master, zlookup_connect, zlookup, 0);
paul718e3742002-12-13 20:15:29 +00001480
1481 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1482 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1483
Paul Jakma64e580a2006-02-21 01:09:01 +00001484 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1485 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001486 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001487
Paul Jakma64e580a2006-02-21 01:09:01 +00001488 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001489
Paul Jakma64e580a2006-02-21 01:09:01 +00001490 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1491 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001492 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001493 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001494
1495 /* Make BGP scan thread. */
Donald Sharp774914f2015-10-14 08:50:39 -04001496 bgp_scan_thread = thread_add_timer (bm->master, bgp_scan_timer,
paul59320202004-11-09 01:54:03 +00001497 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001498 /* Make BGP import there. */
Donald Sharp774914f2015-10-14 08:50:39 -04001499 bgp_import_thread = thread_add_timer (bm->master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001500
1501 install_element (BGP_NODE, &bgp_scan_time_cmd);
1502 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1503 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1504 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001505 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001506 install_element (VIEW_NODE, &show_ip_bgp_nexthop_cmd);
1507 install_element (VIEW_NODE, &show_ip_bgp_nexthop_detail_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01001508 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
paul718e3742002-12-13 20:15:29 +00001509}
Chris Caputo228da422009-07-18 05:44:03 +00001510
1511void
1512bgp_scan_finish (void)
1513{
Lou Berger056f3762013-04-10 12:30:04 -07001514 if (cache1_table[AFI_IP])
1515 bgp_table_unlock (cache1_table[AFI_IP]);
Chris Caputo228da422009-07-18 05:44:03 +00001516 cache1_table[AFI_IP] = NULL;
1517
Lou Berger056f3762013-04-10 12:30:04 -07001518 if (cache2_table[AFI_IP])
1519 bgp_table_unlock (cache2_table[AFI_IP]);
Chris Caputo228da422009-07-18 05:44:03 +00001520 cache2_table[AFI_IP] = NULL;
Lou Berger056f3762013-04-10 12:30:04 -07001521
1522 if (bgp_connected_table[AFI_IP])
1523 bgp_table_unlock (bgp_connected_table[AFI_IP]);
Chris Caputo228da422009-07-18 05:44:03 +00001524 bgp_connected_table[AFI_IP] = NULL;
1525
Lou Berger056f3762013-04-10 12:30:04 -07001526 if (cache1_table[AFI_IP6])
1527 bgp_table_unlock (cache1_table[AFI_IP6]);
Chris Caputo228da422009-07-18 05:44:03 +00001528 cache1_table[AFI_IP6] = NULL;
1529
Lou Berger056f3762013-04-10 12:30:04 -07001530 if (cache2_table[AFI_IP6])
1531 bgp_table_unlock (cache2_table[AFI_IP6]);
Chris Caputo228da422009-07-18 05:44:03 +00001532 cache2_table[AFI_IP6] = NULL;
1533
Lou Berger056f3762013-04-10 12:30:04 -07001534 if (bgp_connected_table[AFI_IP6])
1535 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
Chris Caputo228da422009-07-18 05:44:03 +00001536 bgp_connected_table[AFI_IP6] = NULL;
Chris Caputo228da422009-07-18 05:44:03 +00001537}
Lou Berger82dd7072016-01-12 13:41:57 -05001538
1539void
1540bgp_scan_destroy (void)
1541{
1542 if (zlookup == NULL)
1543 return;
1544 THREAD_OFF(bgp_import_thread);
1545 THREAD_OFF(bgp_scan_thread);
1546 THREAD_OFF(zlookup->t_connect);
1547 bgp_scan_finish();
1548 zclient_free (zlookup);
1549 zlookup = NULL;
1550}