blob: 183a28b192687476e68665a6cb818edb858deb10 [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"
paul718e3742002-12-13 20:15:29 +000033
34#include "bgpd/bgpd.h"
35#include "bgpd/bgp_table.h"
36#include "bgpd/bgp_route.h"
37#include "bgpd/bgp_attr.h"
38#include "bgpd/bgp_nexthop.h"
39#include "bgpd/bgp_debug.h"
40#include "bgpd/bgp_damp.h"
41#include "zebra/rib.h"
42#include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
43
44struct bgp_nexthop_cache *zlookup_query (struct in_addr);
45#ifdef HAVE_IPV6
46struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *);
47#endif /* HAVE_IPV6 */
David Lamparter6b0655a2014-06-04 06:53:35 +020048
paul718e3742002-12-13 20:15:29 +000049/* Only one BGP scan thread are activated at the same time. */
paul00d252c2005-05-23 14:19:54 +000050static struct thread *bgp_scan_thread = NULL;
paul718e3742002-12-13 20:15:29 +000051
52/* BGP import thread */
paul00d252c2005-05-23 14:19:54 +000053static struct thread *bgp_import_thread = NULL;
paul718e3742002-12-13 20:15:29 +000054
55/* BGP scan interval. */
paul00d252c2005-05-23 14:19:54 +000056static int bgp_scan_interval;
paul718e3742002-12-13 20:15:29 +000057
58/* BGP import interval. */
paul00d252c2005-05-23 14:19:54 +000059static int bgp_import_interval;
paul718e3742002-12-13 20:15:29 +000060
61/* Route table for next-hop lookup cache. */
paul00d252c2005-05-23 14:19:54 +000062static struct bgp_table *bgp_nexthop_cache_table[AFI_MAX];
63static struct bgp_table *cache1_table[AFI_MAX];
64static struct bgp_table *cache2_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000065
66/* Route table for connected route. */
paul00d252c2005-05-23 14:19:54 +000067static struct bgp_table *bgp_connected_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000068
69/* BGP nexthop lookup query client. */
Chris Caputo228da422009-07-18 05:44:03 +000070struct zclient *zlookup = NULL;
David Lamparter6b0655a2014-06-04 06:53:35 +020071
paul718e3742002-12-13 20:15:29 +000072/* Add nexthop to the end of the list. */
paul94f2b392005-06-28 12:44:16 +000073static void
paul718e3742002-12-13 20:15:29 +000074bnc_nexthop_add (struct bgp_nexthop_cache *bnc, struct nexthop *nexthop)
75{
76 struct nexthop *last;
77
78 for (last = bnc->nexthop; last && last->next; last = last->next)
79 ;
80 if (last)
81 last->next = nexthop;
82 else
83 bnc->nexthop = nexthop;
84 nexthop->prev = last;
85}
86
paul94f2b392005-06-28 12:44:16 +000087static void
paul718e3742002-12-13 20:15:29 +000088bnc_nexthop_free (struct bgp_nexthop_cache *bnc)
89{
90 struct nexthop *nexthop;
91 struct nexthop *next = NULL;
92
93 for (nexthop = bnc->nexthop; nexthop; nexthop = next)
94 {
95 next = nexthop->next;
96 XFREE (MTYPE_NEXTHOP, nexthop);
97 }
98}
99
paul94f2b392005-06-28 12:44:16 +0000100static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800101bnc_new (void)
paul718e3742002-12-13 20:15:29 +0000102{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700103 return XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
paul718e3742002-12-13 20:15:29 +0000104}
105
paul94f2b392005-06-28 12:44:16 +0000106static void
paul718e3742002-12-13 20:15:29 +0000107bnc_free (struct bgp_nexthop_cache *bnc)
108{
109 bnc_nexthop_free (bnc);
110 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
111}
David Lamparter6b0655a2014-06-04 06:53:35 +0200112
paul94f2b392005-06-28 12:44:16 +0000113static int
paul718e3742002-12-13 20:15:29 +0000114bgp_nexthop_same (struct nexthop *next1, struct nexthop *next2)
115{
116 if (next1->type != next2->type)
117 return 0;
118
119 switch (next1->type)
120 {
121 case ZEBRA_NEXTHOP_IPV4:
122 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
123 return 0;
124 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000125 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
126 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4)
127 || next1->ifindex != next2->ifindex)
128 return 0;
129 break;
paul718e3742002-12-13 20:15:29 +0000130 case ZEBRA_NEXTHOP_IFINDEX:
131 case ZEBRA_NEXTHOP_IFNAME:
132 if (next1->ifindex != next2->ifindex)
133 return 0;
134 break;
135#ifdef HAVE_IPV6
136 case ZEBRA_NEXTHOP_IPV6:
137 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
138 return 0;
139 break;
140 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
141 case ZEBRA_NEXTHOP_IPV6_IFNAME:
142 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
143 return 0;
144 if (next1->ifindex != next2->ifindex)
145 return 0;
146 break;
147#endif /* HAVE_IPV6 */
hassofa2b17e2004-03-04 17:45:00 +0000148 default:
149 /* do nothing */
150 break;
paul718e3742002-12-13 20:15:29 +0000151 }
152 return 1;
153}
154
paul94f2b392005-06-28 12:44:16 +0000155static int
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400156bgp_nexthop_cache_different (struct bgp_nexthop_cache *bnc1,
paul718e3742002-12-13 20:15:29 +0000157 struct bgp_nexthop_cache *bnc2)
158{
159 int i;
160 struct nexthop *next1, *next2;
161
162 if (bnc1->nexthop_num != bnc2->nexthop_num)
163 return 1;
164
165 next1 = bnc1->nexthop;
166 next2 = bnc2->nexthop;
167
168 for (i = 0; i < bnc1->nexthop_num; i++)
169 {
170 if (! bgp_nexthop_same (next1, next2))
171 return 1;
172
173 next1 = next1->next;
174 next2 = next2->next;
175 }
176 return 0;
177}
178
179/* If nexthop exists on connected network return 1. */
180int
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400181bgp_nexthop_onlink (afi_t afi, struct attr *attr)
paul718e3742002-12-13 20:15:29 +0000182{
183 struct bgp_node *rn;
Paul Jakmafc98d162012-01-09 11:36:23 +0000184
185 /* If zebra is not enabled return */
186 if (zlookup->sock < 0)
187 return 1;
188
paul718e3742002-12-13 20:15:29 +0000189 /* Lookup the address is onlink or not. */
190 if (afi == AFI_IP)
191 {
paul59320202004-11-09 01:54:03 +0000192 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
paul718e3742002-12-13 20:15:29 +0000193 if (rn)
194 {
195 bgp_unlock_node (rn);
196 return 1;
197 }
198 }
199#ifdef HAVE_IPV6
200 else if (afi == AFI_IP6)
201 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000202 if (attr->extra->mp_nexthop_len == 32)
paul718e3742002-12-13 20:15:29 +0000203 return 1;
Paul Jakmafb982c22007-05-04 20:15:47 +0000204 else if (attr->extra->mp_nexthop_len == 16)
paul718e3742002-12-13 20:15:29 +0000205 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000206 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000207 return 1;
208
paul59320202004-11-09 01:54:03 +0000209 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
Paul Jakmafb982c22007-05-04 20:15:47 +0000210 &attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000211 if (rn)
212 {
213 bgp_unlock_node (rn);
214 return 1;
215 }
216 }
217 }
218#endif /* HAVE_IPV6 */
219 return 0;
220}
221
222#ifdef HAVE_IPV6
223/* Check specified next-hop is reachable or not. */
paul94f2b392005-06-28 12:44:16 +0000224static int
paul718e3742002-12-13 20:15:29 +0000225bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
226 int *metricchanged)
227{
228 struct bgp_node *rn;
229 struct prefix p;
230 struct bgp_nexthop_cache *bnc;
231 struct attr *attr;
Paul Jakmafc98d162012-01-09 11:36:23 +0000232
233 /* If lookup is not enabled, return valid. */
234 if (zlookup->sock < 0)
235 {
236 if (ri->extra)
237 ri->extra->igpmetric = 0;
238 return 1;
239 }
240
paul718e3742002-12-13 20:15:29 +0000241 /* Only check IPv6 global address only nexthop. */
242 attr = ri->attr;
243
Paul Jakmafb982c22007-05-04 20:15:47 +0000244 if (attr->extra->mp_nexthop_len != 16
245 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000246 return 1;
247
248 memset (&p, 0, sizeof (struct prefix));
249 p.family = AF_INET6;
250 p.prefixlen = IPV6_MAX_BITLEN;
Paul Jakmafb982c22007-05-04 20:15:47 +0000251 p.u.prefix6 = attr->extra->mp_nexthop_global;
paul718e3742002-12-13 20:15:29 +0000252
253 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000254 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
paul718e3742002-12-13 20:15:29 +0000255
256 if (rn->info)
257 {
258 bnc = rn->info;
259 bgp_unlock_node (rn);
260 }
261 else
262 {
Denis Ovsienkob64bfc12011-08-08 19:36:44 +0400263 if (NULL == (bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global)))
264 bnc = bnc_new ();
265 else
paul718e3742002-12-13 20:15:29 +0000266 {
paul718e3742002-12-13 20:15:29 +0000267 if (changed)
268 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400269 struct bgp_table *old;
270 struct bgp_node *oldrn;
271
paul59320202004-11-09 01:54:03 +0000272 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
273 old = cache2_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000274 else
paul59320202004-11-09 01:54:03 +0000275 old = cache1_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000276
277 oldrn = bgp_node_lookup (old, &p);
278 if (oldrn)
279 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400280 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000281
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400282 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000283
284 if (bnc->metric != oldbnc->metric)
285 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000286
287 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000288 }
289 }
290 }
paul718e3742002-12-13 20:15:29 +0000291 rn->info = bnc;
292 }
293
294 if (changed)
295 *changed = bnc->changed;
296
297 if (metricchanged)
298 *metricchanged = bnc->metricchanged;
299
Paul Jakmafb982c22007-05-04 20:15:47 +0000300 if (bnc->valid && bnc->metric)
301 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
302 else if (ri->extra)
303 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000304
305 return bnc->valid;
306}
307#endif /* HAVE_IPV6 */
308
309/* Check specified next-hop is reachable or not. */
310int
311bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
312 int *changed, int *metricchanged)
313{
314 struct bgp_node *rn;
315 struct prefix p;
316 struct bgp_nexthop_cache *bnc;
317 struct in_addr addr;
Paul Jakmafc98d162012-01-09 11:36:23 +0000318
319 /* If lookup is not enabled, return valid. */
320 if (zlookup->sock < 0)
321 {
322 if (ri->extra)
323 ri->extra->igpmetric = 0;
324 return 1;
325 }
326
paul718e3742002-12-13 20:15:29 +0000327#ifdef HAVE_IPV6
328 if (afi == AFI_IP6)
329 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
330#endif /* HAVE_IPV6 */
331
332 addr = ri->attr->nexthop;
333
334 memset (&p, 0, sizeof (struct prefix));
335 p.family = AF_INET;
336 p.prefixlen = IPV4_MAX_BITLEN;
337 p.u.prefix4 = addr;
338
339 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000340 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000341
342 if (rn->info)
343 {
344 bnc = rn->info;
345 bgp_unlock_node (rn);
346 }
347 else
348 {
Denis Ovsienkob64bfc12011-08-08 19:36:44 +0400349 if (NULL == (bnc = zlookup_query (addr)))
350 bnc = bnc_new ();
351 else
paul718e3742002-12-13 20:15:29 +0000352 {
paul718e3742002-12-13 20:15:29 +0000353 if (changed)
354 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400355 struct bgp_table *old;
356 struct bgp_node *oldrn;
357
paul59320202004-11-09 01:54:03 +0000358 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
359 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000360 else
paul59320202004-11-09 01:54:03 +0000361 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000362
363 oldrn = bgp_node_lookup (old, &p);
364 if (oldrn)
365 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400366 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000367
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400368 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000369
370 if (bnc->metric != oldbnc->metric)
371 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000372
373 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000374 }
375 }
376 }
paul718e3742002-12-13 20:15:29 +0000377 rn->info = bnc;
378 }
379
380 if (changed)
381 *changed = bnc->changed;
382
383 if (metricchanged)
384 *metricchanged = bnc->metricchanged;
385
Paul Jakmafb982c22007-05-04 20:15:47 +0000386 if (bnc->valid && bnc->metric)
387 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
388 else if (ri->extra)
389 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000390
391 return bnc->valid;
392}
393
394/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000395static void
paul718e3742002-12-13 20:15:29 +0000396bgp_nexthop_cache_reset (struct bgp_table *table)
397{
398 struct bgp_node *rn;
399 struct bgp_nexthop_cache *bnc;
400
401 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
402 if ((bnc = rn->info) != NULL)
403 {
404 bnc_free (bnc);
405 rn->info = NULL;
406 bgp_unlock_node (rn);
407 }
408}
409
paul94f2b392005-06-28 12:44:16 +0000410static void
paul59320202004-11-09 01:54:03 +0000411bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000412{
413 struct bgp_node *rn;
414 struct bgp *bgp;
415 struct bgp_info *bi;
416 struct bgp_info *next;
417 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000418 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000419 int valid;
420 int current;
421 int changed;
422 int metricchanged;
423
424 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000425 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
426 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000427 else
paul59320202004-11-09 01:54:03 +0000428 bgp_nexthop_cache_table[afi] = cache1_table[afi];
paul718e3742002-12-13 20:15:29 +0000429
430 /* Get default bgp. */
431 bgp = bgp_get_default ();
432 if (bgp == NULL)
433 return;
434
435 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000436 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000437 {
438 if (peer->status != Established)
439 continue;
440
paul59320202004-11-09 01:54:03 +0000441 if (peer->afc[afi][SAFI_UNICAST])
442 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
443 if (peer->afc[afi][SAFI_MULTICAST])
444 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
445 if (peer->afc[afi][SAFI_MPLS_VPN])
446 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
paul718e3742002-12-13 20:15:29 +0000447 }
448
paul59320202004-11-09 01:54:03 +0000449 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000450 rn = bgp_route_next (rn))
451 {
452 for (bi = rn->info; bi; bi = next)
453 {
454 next = bi->next;
455
456 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
457 {
458 changed = 0;
459 metricchanged = 0;
460
Christian Frankea0f6ce52013-04-11 08:24:30 +0000461 if (bi->peer->sort == BGP_PEER_EBGP && bi->peer->ttl == 1
462 && !CHECK_FLAG(bi->peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400463 valid = bgp_nexthop_onlink (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000464 else
paul59320202004-11-09 01:54:03 +0000465 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000466 &changed, &metricchanged);
467
468 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
469
470 if (changed)
471 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
472 else
473 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
474
475 if (valid != current)
476 {
477 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
478 {
479 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000480 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000481 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000482 }
483 else
484 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000485 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000486 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000487 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000488 }
489 }
490
paul59320202004-11-09 01:54:03 +0000491 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000492 BGP_CONFIG_DAMPENING)
Paul Jakmafb982c22007-05-04 20:15:47 +0000493 && bi->extra && bi->extra->damp_info )
paul59320202004-11-09 01:54:03 +0000494 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000495 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000496 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000497 }
498 }
paul59320202004-11-09 01:54:03 +0000499 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000500 }
501
502 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000503 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
504 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000505 else
paul59320202004-11-09 01:54:03 +0000506 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000507
508 if (BGP_DEBUG (events, EVENTS))
509 {
510 if (afi == AFI_IP)
511 zlog_debug ("scanning IPv4 Unicast routing tables");
512 else if (afi == AFI_IP6)
513 zlog_debug ("scanning IPv6 Unicast routing tables");
514 }
Christian Frankedcab1bb2012-12-07 16:45:52 +0000515
516 /* Reevaluate default-originate route-maps and announce/withdraw
517 * default route if neccesary. */
518 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
519 {
520 if (peer->status == Established
521 && CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE)
522 && peer->default_rmap[afi][safi].name)
523 bgp_default_originate (peer, afi, safi, 0);
524 }
paul718e3742002-12-13 20:15:29 +0000525}
526
paul718e3742002-12-13 20:15:29 +0000527/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000528static int
paul59320202004-11-09 01:54:03 +0000529bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000530{
531 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +0000532 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000533
hassof4184462005-02-01 20:13:16 +0000534 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000535 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000536
paul59320202004-11-09 01:54:03 +0000537 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000538
539#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000540 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000541#endif /* HAVE_IPV6 */
542
543 return 0;
544}
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000545
546/* BGP own address structure */
547struct bgp_addr
548{
549 struct in_addr addr;
550 int refcnt;
551};
552
553static struct hash *bgp_address_hash;
554
555static void *
556bgp_address_hash_alloc (void *p)
557{
558 struct in_addr *val = p;
559 struct bgp_addr *addr;
560
561 addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr));
562 addr->refcnt = 0;
563 addr->addr.s_addr = val->s_addr;
564
565 return addr;
566}
567
568static unsigned int
569bgp_address_hash_key_make (void *p)
570{
571 const struct bgp_addr *addr = p;
572
573 return jhash_1word(addr->addr.s_addr, 0);
574}
575
576static int
577bgp_address_hash_cmp (const void *p1, const void *p2)
578{
579 const struct bgp_addr *addr1 = p1;
580 const struct bgp_addr *addr2 = p2;
581
582 return addr1->addr.s_addr == addr2->addr.s_addr;
583}
584
585void
586bgp_address_init (void)
587{
588 bgp_address_hash = hash_create (bgp_address_hash_key_make,
589 bgp_address_hash_cmp);
590}
591
592static void
593bgp_address_add (struct prefix *p)
594{
595 struct bgp_addr tmp;
596 struct bgp_addr *addr;
597
598 tmp.addr = p->u.prefix4;
599
600 addr = hash_get (bgp_address_hash, &tmp, bgp_address_hash_alloc);
601 addr->refcnt++;
602}
603
604static void
605bgp_address_del (struct prefix *p)
606{
607 struct bgp_addr tmp;
608 struct bgp_addr *addr;
609
610 tmp.addr = p->u.prefix4;
611
612 addr = hash_lookup (bgp_address_hash, &tmp);
Rakesh Garimella9e47abd2013-03-11 12:38:31 +0000613 /* may have been deleted earlier by bgp_interface_down() */
614 if (addr == NULL)
615 return;
616
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000617 addr->refcnt--;
618
619 if (addr->refcnt == 0)
620 {
621 hash_release (bgp_address_hash, addr);
622 XFREE (MTYPE_BGP_ADDR, addr);
623 }
624}
625
David Lamparter6b0655a2014-06-04 06:53:35 +0200626
paul59320202004-11-09 01:54:03 +0000627struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000628{
629 unsigned int refcnt;
630};
631
632void
633bgp_connected_add (struct connected *ifc)
634{
635 struct prefix p;
636 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000637 struct interface *ifp;
638 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000639 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000640
641 ifp = ifc->ifp;
642
643 if (! ifp)
644 return;
645
646 if (if_is_loopback (ifp))
647 return;
648
649 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000650
Donald Sharp26a18eb2015-09-29 09:25:10 -0400651 p = *(CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000652 if (addr->family == AF_INET)
653 {
paul718e3742002-12-13 20:15:29 +0000654 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
655
656 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
657 return;
658
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000659 bgp_address_add (addr);
660
paul59320202004-11-09 01:54:03 +0000661 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000662 if (rn->info)
663 {
664 bc = rn->info;
665 bc->refcnt++;
666 }
667 else
668 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000669 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000670 bc->refcnt = 1;
671 rn->info = bc;
672 }
673 }
674#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000675 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000676 {
paul718e3742002-12-13 20:15:29 +0000677 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
678
679 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
680 return;
681
682 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
683 return;
684
paul59320202004-11-09 01:54:03 +0000685 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000686 if (rn->info)
687 {
688 bc = rn->info;
689 bc->refcnt++;
690 }
691 else
692 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000693 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000694 bc->refcnt = 1;
695 rn->info = bc;
696 }
697 }
698#endif /* HAVE_IPV6 */
699}
700
701void
702bgp_connected_delete (struct connected *ifc)
703{
704 struct prefix p;
705 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000706 struct interface *ifp;
707 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000708 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000709
710 ifp = ifc->ifp;
711
712 if (if_is_loopback (ifp))
713 return;
714
715 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000716
Donald Sharp26a18eb2015-09-29 09:25:10 -0400717 p = *(CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000718 if (addr->family == AF_INET)
719 {
paul718e3742002-12-13 20:15:29 +0000720 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
721
722 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
723 return;
724
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000725 bgp_address_del (addr);
726
paul59320202004-11-09 01:54:03 +0000727 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000728 if (! rn)
729 return;
730
731 bc = rn->info;
732 bc->refcnt--;
733 if (bc->refcnt == 0)
734 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000735 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000736 rn->info = NULL;
737 }
738 bgp_unlock_node (rn);
739 bgp_unlock_node (rn);
740 }
741#ifdef HAVE_IPV6
742 else if (addr->family == AF_INET6)
743 {
paul718e3742002-12-13 20:15:29 +0000744 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
745
746 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
747 return;
748
749 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
750 return;
751
paul59320202004-11-09 01:54:03 +0000752 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000753 if (! rn)
754 return;
755
756 bc = rn->info;
757 bc->refcnt--;
758 if (bc->refcnt == 0)
759 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000760 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000761 rn->info = NULL;
762 }
763 bgp_unlock_node (rn);
764 bgp_unlock_node (rn);
765 }
766#endif /* HAVE_IPV6 */
767}
768
769int
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000770bgp_nexthop_self (struct attr *attr)
paul718e3742002-12-13 20:15:29 +0000771{
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000772 struct bgp_addr tmp, *addr;
paul718e3742002-12-13 20:15:29 +0000773
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000774 tmp.addr = attr->nexthop;
paul718e3742002-12-13 20:15:29 +0000775
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000776 addr = hash_lookup (bgp_address_hash, &tmp);
777 if (addr)
778 return 1;
779
paul718e3742002-12-13 20:15:29 +0000780 return 0;
781}
David Lamparter6b0655a2014-06-04 06:53:35 +0200782
paul94f2b392005-06-28 12:44:16 +0000783static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800784zlookup_read (void)
paul718e3742002-12-13 20:15:29 +0000785{
786 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000787 uint16_t length;
788 u_char marker;
789 u_char version;
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200790 uint16_t vrf_id;
791 uint16_t command;
792 int err;
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100793 struct in_addr raddr __attribute__((unused));
pauld3092e72006-01-17 17:33:46 +0000794 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000795 int i;
796 u_char nexthop_num;
797 struct nexthop *nexthop;
798 struct bgp_nexthop_cache *bnc;
799
800 s = zlookup->ibuf;
801 stream_reset (s);
802
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200803 err = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
804 &vrf_id, &command);
805 if (err < 0)
806 {
807 zlog_err("%s: zserv_read_header() failed", __func__);
808 return NULL;
809 }
Donald Sharpa9d4cb32015-09-17 10:54:25 -0400810
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100811 /* XXX: not doing anything with raddr */
paul718e3742002-12-13 20:15:29 +0000812 raddr.s_addr = stream_get_ipv4 (s);
813 metric = stream_getl (s);
814 nexthop_num = stream_getc (s);
815
816 if (nexthop_num)
817 {
818 bnc = bnc_new ();
819 bnc->valid = 1;
820 bnc->metric = metric;
821 bnc->nexthop_num = nexthop_num;
822
823 for (i = 0; i < nexthop_num; i++)
824 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700825 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000826 nexthop->type = stream_getc (s);
827 switch (nexthop->type)
828 {
829 case ZEBRA_NEXTHOP_IPV4:
830 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
831 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000832 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
833 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
834 nexthop->ifindex = stream_getl (s);
835 break;
paul718e3742002-12-13 20:15:29 +0000836 case ZEBRA_NEXTHOP_IFINDEX:
837 case ZEBRA_NEXTHOP_IFNAME:
838 nexthop->ifindex = stream_getl (s);
839 break;
hassofa2b17e2004-03-04 17:45:00 +0000840 default:
841 /* do nothing */
842 break;
paul718e3742002-12-13 20:15:29 +0000843 }
844 bnc_nexthop_add (bnc, nexthop);
845 }
846 }
847 else
848 return NULL;
849
850 return bnc;
851}
852
853struct bgp_nexthop_cache *
854zlookup_query (struct in_addr addr)
855{
856 int ret;
857 struct stream *s;
858
859 /* Check socket. */
860 if (zlookup->sock < 0)
861 return NULL;
862
863 s = zlookup->obuf;
864 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +0800865 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000866 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000867
868 stream_putw_at (s, 0, stream_get_endp (s));
869
870 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000871 if (ret < 0)
872 {
873 zlog_err ("can't write to zlookup->sock");
874 close (zlookup->sock);
875 zlookup->sock = -1;
876 return NULL;
877 }
878 if (ret == 0)
879 {
880 zlog_err ("zlookup->sock connection closed");
881 close (zlookup->sock);
882 zlookup->sock = -1;
883 return NULL;
884 }
885
886 return zlookup_read ();
887}
888
889#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000890static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800891zlookup_read_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000892{
893 struct stream *s;
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200894 uint16_t length, vrf_id, cmd;
pauld3092e72006-01-17 17:33:46 +0000895 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000896 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000897 uint32_t metric;
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200898 int i, err;
paul718e3742002-12-13 20:15:29 +0000899 u_char nexthop_num;
900 struct nexthop *nexthop;
901 struct bgp_nexthop_cache *bnc;
902
903 s = zlookup->ibuf;
904 stream_reset (s);
905
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200906 err = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
907 &vrf_id, &cmd);
908 if (err < 0)
909 {
910 zlog_err("%s: zserv_read_header() failed", __func__);
911 return NULL;
912 }
Donald Sharpa9d4cb32015-09-17 10:54:25 -0400913
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100914 /* XXX: not actually doing anything with raddr */
paul718e3742002-12-13 20:15:29 +0000915 stream_get (&raddr, s, 16);
916
917 metric = stream_getl (s);
918 nexthop_num = stream_getc (s);
919
920 if (nexthop_num)
921 {
922 bnc = bnc_new ();
923 bnc->valid = 1;
924 bnc->metric = metric;
925 bnc->nexthop_num = nexthop_num;
926
927 for (i = 0; i < nexthop_num; i++)
928 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700929 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000930 nexthop->type = stream_getc (s);
931 switch (nexthop->type)
932 {
933 case ZEBRA_NEXTHOP_IPV6:
934 stream_get (&nexthop->gate.ipv6, s, 16);
935 break;
936 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
937 case ZEBRA_NEXTHOP_IPV6_IFNAME:
938 stream_get (&nexthop->gate.ipv6, s, 16);
939 nexthop->ifindex = stream_getl (s);
940 break;
941 case ZEBRA_NEXTHOP_IFINDEX:
942 case ZEBRA_NEXTHOP_IFNAME:
943 nexthop->ifindex = stream_getl (s);
944 break;
hassofa2b17e2004-03-04 17:45:00 +0000945 default:
946 /* do nothing */
947 break;
paul718e3742002-12-13 20:15:29 +0000948 }
949 bnc_nexthop_add (bnc, nexthop);
950 }
951 }
952 else
953 return NULL;
954
955 return bnc;
956}
957
958struct bgp_nexthop_cache *
959zlookup_query_ipv6 (struct in6_addr *addr)
960{
961 int ret;
962 struct stream *s;
963
964 /* Check socket. */
965 if (zlookup->sock < 0)
966 return NULL;
967
968 s = zlookup->obuf;
969 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +0800970 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000971 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000972 stream_putw_at (s, 0, stream_get_endp (s));
973
974 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000975 if (ret < 0)
976 {
977 zlog_err ("can't write to zlookup->sock");
978 close (zlookup->sock);
979 zlookup->sock = -1;
980 return NULL;
981 }
982 if (ret == 0)
983 {
984 zlog_err ("zlookup->sock connection closed");
985 close (zlookup->sock);
986 zlookup->sock = -1;
987 return NULL;
988 }
989
990 return zlookup_read_ipv6 ();
991}
992#endif /* HAVE_IPV6 */
993
paul94f2b392005-06-28 12:44:16 +0000994static int
995bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
996 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000997{
998 struct stream *s;
999 int ret;
Nicolas Dichtel794c4732015-09-16 09:42:36 +02001000 u_int16_t length, vrf_id, command;
pauld3092e72006-01-17 17:33:46 +00001001 u_char version, marker;
Paul Jakma7aa9dce2014-09-19 14:42:23 +01001002 struct in_addr addr __attribute__((unused));
paul718e3742002-12-13 20:15:29 +00001003 struct in_addr nexthop;
1004 u_int32_t metric = 0;
1005 u_char nexthop_num;
1006 u_char nexthop_type;
1007
1008 /* If lookup connection is not available return valid. */
1009 if (zlookup->sock < 0)
1010 {
1011 if (igpmetric)
1012 *igpmetric = 0;
1013 return 1;
1014 }
1015
1016 /* Send query to the lookup connection */
1017 s = zlookup->obuf;
1018 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +08001019 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, VRF_DEFAULT);
pauld3092e72006-01-17 17:33:46 +00001020
paul718e3742002-12-13 20:15:29 +00001021 stream_putc (s, p->prefixlen);
1022 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +00001023
1024 stream_putw_at (s, 0, stream_get_endp (s));
1025
paul718e3742002-12-13 20:15:29 +00001026 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +00001027 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +00001028
1029 if (ret < 0)
1030 {
1031 zlog_err ("can't write to zlookup->sock");
1032 close (zlookup->sock);
1033 zlookup->sock = -1;
1034 return 1;
1035 }
1036 if (ret == 0)
1037 {
1038 zlog_err ("zlookup->sock connection closed");
1039 close (zlookup->sock);
1040 zlookup->sock = -1;
1041 return 1;
1042 }
1043
1044 /* Get result. */
1045 stream_reset (s);
1046
Nicolas Dichtel794c4732015-09-16 09:42:36 +02001047 ret = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
1048 &vrf_id, &command);
1049 if (ret < 0)
1050 {
1051 zlog_err("%s: zserv_read_header() failed", __func__);
1052 return 0;
1053 }
Donald Sharpa9d4cb32015-09-17 10:54:25 -04001054
Paul Jakma7aa9dce2014-09-19 14:42:23 +01001055 /* XXX: not using addr */
paul718e3742002-12-13 20:15:29 +00001056 addr.s_addr = stream_get_ipv4 (s);
1057 metric = stream_getl (s);
1058 nexthop_num = stream_getc (s);
1059
1060 /* Set IGP metric value. */
1061 if (igpmetric)
1062 *igpmetric = metric;
1063
1064 /* If there is nexthop then this is active route. */
1065 if (nexthop_num)
1066 {
1067 nexthop.s_addr = 0;
1068 nexthop_type = stream_getc (s);
Christian Frankea12afd52013-05-25 14:01:36 +00001069 switch (nexthop_type)
paul718e3742002-12-13 20:15:29 +00001070 {
Christian Frankea12afd52013-05-25 14:01:36 +00001071 case ZEBRA_NEXTHOP_IPV4:
paul718e3742002-12-13 20:15:29 +00001072 nexthop.s_addr = stream_get_ipv4 (s);
Christian Frankea12afd52013-05-25 14:01:36 +00001073 break;
1074 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1075 nexthop.s_addr = stream_get_ipv4 (s);
1076 /* ifindex */ (void)stream_getl (s);
1077 break;
1078 default:
1079 /* do nothing */
1080 break;
paul718e3742002-12-13 20:15:29 +00001081 }
Christian Frankea12afd52013-05-25 14:01:36 +00001082 *igpnexthop = nexthop;
paul718e3742002-12-13 20:15:29 +00001083
1084 return 1;
1085 }
1086 else
1087 return 0;
1088}
1089
1090/* Scan all configured BGP route then check the route exists in IGP or
1091 not. */
paul94f2b392005-06-28 12:44:16 +00001092static int
paul718e3742002-12-13 20:15:29 +00001093bgp_import (struct thread *t)
1094{
1095 struct bgp *bgp;
1096 struct bgp_node *rn;
1097 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001098 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001099 int valid;
1100 u_int32_t metric;
1101 struct in_addr nexthop;
1102 afi_t afi;
1103 safi_t safi;
1104
1105 bgp_import_thread =
1106 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1107
hassof4184462005-02-01 20:13:16 +00001108 if (BGP_DEBUG (events, EVENTS))
1109 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001110
paul1eb8ef22005-04-07 07:30:20 +00001111 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001112 {
1113 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1114 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1115 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1116 rn = bgp_route_next (rn))
1117 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001118 {
paul6cbbc3c2003-04-28 17:11:02 +00001119 if (bgp_static->backdoor)
1120 continue;
paul718e3742002-12-13 20:15:29 +00001121
paul6cbbc3c2003-04-28 17:11:02 +00001122 valid = bgp_static->valid;
1123 metric = bgp_static->igpmetric;
1124 nexthop = bgp_static->igpnexthop;
1125
1126 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1127 && afi == AFI_IP && safi == SAFI_UNICAST)
1128 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1129 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001130 else
paul6cbbc3c2003-04-28 17:11:02 +00001131 {
1132 bgp_static->valid = 1;
1133 bgp_static->igpmetric = 0;
1134 bgp_static->igpnexthop.s_addr = 0;
1135 }
1136
1137 if (bgp_static->valid != valid)
1138 {
1139 if (bgp_static->valid)
1140 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1141 else
1142 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1143 }
1144 else if (bgp_static->valid)
1145 {
1146 if (bgp_static->igpmetric != metric
1147 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1148 || bgp_static->rmap.name)
1149 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1150 }
paul718e3742002-12-13 20:15:29 +00001151 }
paul6cbbc3c2003-04-28 17:11:02 +00001152 }
paul718e3742002-12-13 20:15:29 +00001153 return 0;
1154}
1155
1156/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001157static int
paul718e3742002-12-13 20:15:29 +00001158zlookup_connect (struct thread *t)
1159{
1160 struct zclient *zlookup;
1161
1162 zlookup = THREAD_ARG (t);
1163 zlookup->t_connect = NULL;
1164
1165 if (zlookup->sock != -1)
1166 return 0;
1167
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001168 if (zclient_socket_connect (zlookup) < 0)
paul718e3742002-12-13 20:15:29 +00001169 return -1;
1170
paul718e3742002-12-13 20:15:29 +00001171 return 0;
1172}
1173
1174/* Check specified multiaccess next-hop. */
1175int
1176bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1177{
1178 struct bgp_node *rn1;
1179 struct bgp_node *rn2;
1180 struct prefix p1;
1181 struct prefix p2;
1182 struct in_addr addr;
1183 int ret;
1184
1185 ret = inet_aton (peer, &addr);
1186 if (! ret)
1187 return 0;
1188
1189 memset (&p1, 0, sizeof (struct prefix));
1190 p1.family = AF_INET;
1191 p1.prefixlen = IPV4_MAX_BITLEN;
1192 p1.u.prefix4 = nexthop;
1193 memset (&p2, 0, sizeof (struct prefix));
1194 p2.family = AF_INET;
1195 p2.prefixlen = IPV4_MAX_BITLEN;
1196 p2.u.prefix4 = addr;
1197
1198 /* If bgp scan is not enabled, return invalid. */
1199 if (zlookup->sock < 0)
1200 return 0;
1201
paul59320202004-11-09 01:54:03 +00001202 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001203 if (! rn1)
1204 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001205 bgp_unlock_node (rn1);
paul718e3742002-12-13 20:15:29 +00001206
paul59320202004-11-09 01:54:03 +00001207 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001208 if (! rn2)
1209 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001210 bgp_unlock_node (rn2);
paul718e3742002-12-13 20:15:29 +00001211
Chris Caputo6c88b442010-07-27 16:28:55 +00001212 /* This is safe, even with above unlocks, since we are just
1213 comparing pointers to the objects, not the objects themselves. */
paul718e3742002-12-13 20:15:29 +00001214 if (rn1 == rn2)
1215 return 1;
1216
1217 return 0;
1218}
David Lamparter6b0655a2014-06-04 06:53:35 +02001219
paul718e3742002-12-13 20:15:29 +00001220DEFUN (bgp_scan_time,
1221 bgp_scan_time_cmd,
1222 "bgp scan-time <5-60>",
1223 "BGP specific commands\n"
1224 "Configure background scanner interval\n"
1225 "Scanner interval (seconds)\n")
1226{
1227 bgp_scan_interval = atoi (argv[0]);
1228
1229 if (bgp_scan_thread)
1230 {
1231 thread_cancel (bgp_scan_thread);
1232 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001233 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001234 }
1235
1236 return CMD_SUCCESS;
1237}
1238
1239DEFUN (no_bgp_scan_time,
1240 no_bgp_scan_time_cmd,
1241 "no bgp scan-time",
1242 NO_STR
1243 "BGP specific commands\n"
1244 "Configure background scanner interval\n")
1245{
1246 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1247
1248 if (bgp_scan_thread)
1249 {
1250 thread_cancel (bgp_scan_thread);
1251 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001252 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001253 }
1254
1255 return CMD_SUCCESS;
1256}
1257
1258ALIAS (no_bgp_scan_time,
1259 no_bgp_scan_time_val_cmd,
1260 "no bgp scan-time <5-60>",
1261 NO_STR
1262 "BGP specific commands\n"
1263 "Configure background scanner interval\n"
1264 "Scanner interval (seconds)\n")
1265
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001266static int
1267show_ip_bgp_scan_tables (struct vty *vty, const char detail)
paul718e3742002-12-13 20:15:29 +00001268{
1269 struct bgp_node *rn;
1270 struct bgp_nexthop_cache *bnc;
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001271 char buf[INET6_ADDRSTRLEN];
1272 u_char i;
paul718e3742002-12-13 20:15:29 +00001273
1274 if (bgp_scan_thread)
1275 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1276 else
1277 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1278 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1279
1280 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001281 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001282 if ((bnc = rn->info) != NULL)
1283 {
1284 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001285 {
paul718e3742002-12-13 20:15:29 +00001286 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001287 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1288 if (detail)
1289 for (i = 0; i < bnc->nexthop_num; i++)
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001290 switch (bnc->nexthop[i].type)
1291 {
1292 case NEXTHOP_TYPE_IPV4:
1293 vty_out (vty, " gate %s%s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1294 break;
Christian Frankebb97e462013-05-25 14:01:35 +00001295 case NEXTHOP_TYPE_IPV4_IFINDEX:
1296 vty_out (vty, " gate %s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN));
1297 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1298 break;
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001299 case NEXTHOP_TYPE_IFINDEX:
1300 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1301 break;
1302 default:
1303 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1304 }
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001305 }
paul718e3742002-12-13 20:15:29 +00001306 else
1307 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001308 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001309 }
1310
1311#ifdef HAVE_IPV6
1312 {
paul59320202004-11-09 01:54:03 +00001313 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1314 rn;
1315 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001316 if ((bnc = rn->info) != NULL)
1317 {
1318 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001319 {
paul718e3742002-12-13 20:15:29 +00001320 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001321 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001322 bnc->metric, VTY_NEWLINE);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001323 if (detail)
1324 for (i = 0; i < bnc->nexthop_num; i++)
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001325 switch (bnc->nexthop[i].type)
1326 {
1327 case NEXTHOP_TYPE_IPV6:
1328 vty_out (vty, " gate %s%s", inet_ntop (AF_INET6, &bnc->nexthop[i].gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1329 break;
1330 case NEXTHOP_TYPE_IFINDEX:
1331 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1332 break;
1333 default:
1334 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1335 }
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001336 }
paul718e3742002-12-13 20:15:29 +00001337 else
1338 vty_out (vty, " %s invalid%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 VTY_NEWLINE);
1341 }
1342 }
1343#endif /* HAVE_IPV6 */
1344
1345 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001346 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1347 rn;
1348 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001349 if (rn->info != NULL)
1350 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1351 VTY_NEWLINE);
1352
1353#ifdef HAVE_IPV6
1354 {
paul59320202004-11-09 01:54:03 +00001355 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1356 rn;
1357 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001358 if (rn->info != NULL)
1359 vty_out (vty, " %s/%d%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001360 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001361 rn->p.prefixlen,
1362 VTY_NEWLINE);
1363 }
1364#endif /* HAVE_IPV6 */
1365
1366 return CMD_SUCCESS;
1367}
1368
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001369DEFUN (show_ip_bgp_scan,
1370 show_ip_bgp_scan_cmd,
1371 "show ip bgp scan",
1372 SHOW_STR
1373 IP_STR
1374 BGP_STR
1375 "BGP scan status\n")
1376{
1377 return show_ip_bgp_scan_tables (vty, 0);
1378}
1379
1380DEFUN (show_ip_bgp_scan_detail,
1381 show_ip_bgp_scan_detail_cmd,
1382 "show ip bgp scan detail",
1383 SHOW_STR
1384 IP_STR
1385 BGP_STR
1386 "BGP scan status\n"
1387 "More detailed output\n")
1388{
1389 return show_ip_bgp_scan_tables (vty, 1);
1390}
1391
paul718e3742002-12-13 20:15:29 +00001392int
1393bgp_config_write_scan_time (struct vty *vty)
1394{
1395 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1396 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1397 return CMD_SUCCESS;
1398}
1399
1400void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -08001401bgp_scan_init (void)
paul718e3742002-12-13 20:15:29 +00001402{
Donald Sharp71252932015-09-24 09:25:19 -04001403 zlookup = zclient_new (master);
paul718e3742002-12-13 20:15:29 +00001404 zlookup->sock = -1;
paul718e3742002-12-13 20:15:29 +00001405 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1406
1407 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1408 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1409
Paul Jakma64e580a2006-02-21 01:09:01 +00001410 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1411 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001412 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001413
Paul Jakma64e580a2006-02-21 01:09:01 +00001414 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001415
1416#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001417 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1418 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001419 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001420 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001421#endif /* HAVE_IPV6 */
1422
1423 /* Make BGP scan thread. */
paul59320202004-11-09 01:54:03 +00001424 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1425 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001426 /* Make BGP import there. */
1427 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001428
1429 install_element (BGP_NODE, &bgp_scan_time_cmd);
1430 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1431 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1432 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001433 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01001434 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
paul718e3742002-12-13 20:15:29 +00001435 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001436 install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
paul718e3742002-12-13 20:15:29 +00001437}
Chris Caputo228da422009-07-18 05:44:03 +00001438
1439void
1440bgp_scan_finish (void)
1441{
Chris Caputo6c88b442010-07-27 16:28:55 +00001442 /* Only the current one needs to be reset. */
1443 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]);
1444
Chris Caputo228da422009-07-18 05:44:03 +00001445 bgp_table_unlock (cache1_table[AFI_IP]);
1446 cache1_table[AFI_IP] = NULL;
1447
1448 bgp_table_unlock (cache2_table[AFI_IP]);
1449 cache2_table[AFI_IP] = NULL;
1450
1451 bgp_table_unlock (bgp_connected_table[AFI_IP]);
1452 bgp_connected_table[AFI_IP] = NULL;
1453
1454#ifdef HAVE_IPV6
Chris Caputo6c88b442010-07-27 16:28:55 +00001455 /* Only the current one needs to be reset. */
1456 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]);
1457
Chris Caputo228da422009-07-18 05:44:03 +00001458 bgp_table_unlock (cache1_table[AFI_IP6]);
1459 cache1_table[AFI_IP6] = NULL;
1460
1461 bgp_table_unlock (cache2_table[AFI_IP6]);
1462 cache2_table[AFI_IP6] = NULL;
1463
1464 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
1465 bgp_connected_table[AFI_IP6] = NULL;
1466#endif /* HAVE_IPV6 */
1467}