blob: fa2f488224910aefd6ee41204c103c5e04bdb899 [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"
paul718e3742002-12-13 20:15:29 +000034
35#include "bgpd/bgpd.h"
36#include "bgpd/bgp_table.h"
37#include "bgpd/bgp_route.h"
38#include "bgpd/bgp_attr.h"
39#include "bgpd/bgp_nexthop.h"
40#include "bgpd/bgp_debug.h"
41#include "bgpd/bgp_damp.h"
42#include "zebra/rib.h"
43#include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
44
45struct bgp_nexthop_cache *zlookup_query (struct in_addr);
46#ifdef HAVE_IPV6
47struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *);
48#endif /* HAVE_IPV6 */
David Lamparter6b0655a2014-06-04 06:53:35 +020049
paul718e3742002-12-13 20:15:29 +000050/* Only one BGP scan thread are activated at the same time. */
paul00d252c2005-05-23 14:19:54 +000051static struct thread *bgp_scan_thread = NULL;
paul718e3742002-12-13 20:15:29 +000052
53/* BGP import thread */
paul00d252c2005-05-23 14:19:54 +000054static struct thread *bgp_import_thread = NULL;
paul718e3742002-12-13 20:15:29 +000055
56/* BGP scan interval. */
paul00d252c2005-05-23 14:19:54 +000057static int bgp_scan_interval;
paul718e3742002-12-13 20:15:29 +000058
59/* BGP import interval. */
paul00d252c2005-05-23 14:19:54 +000060static int bgp_import_interval;
paul718e3742002-12-13 20:15:29 +000061
62/* Route table for next-hop lookup cache. */
paul00d252c2005-05-23 14:19:54 +000063static struct bgp_table *bgp_nexthop_cache_table[AFI_MAX];
64static struct bgp_table *cache1_table[AFI_MAX];
65static struct bgp_table *cache2_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000066
67/* Route table for connected route. */
paul00d252c2005-05-23 14:19:54 +000068static struct bgp_table *bgp_connected_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000069
70/* BGP nexthop lookup query client. */
Chris Caputo228da422009-07-18 05:44:03 +000071struct zclient *zlookup = NULL;
David Lamparter6b0655a2014-06-04 06:53:35 +020072
paul718e3742002-12-13 20:15:29 +000073/* Add nexthop to the end of the list. */
paul94f2b392005-06-28 12:44:16 +000074static void
paul718e3742002-12-13 20:15:29 +000075bnc_nexthop_add (struct bgp_nexthop_cache *bnc, struct nexthop *nexthop)
76{
77 struct nexthop *last;
78
79 for (last = bnc->nexthop; last && last->next; last = last->next)
80 ;
81 if (last)
82 last->next = nexthop;
83 else
84 bnc->nexthop = nexthop;
85 nexthop->prev = last;
86}
87
paul94f2b392005-06-28 12:44:16 +000088static void
paul718e3742002-12-13 20:15:29 +000089bnc_nexthop_free (struct bgp_nexthop_cache *bnc)
90{
91 struct nexthop *nexthop;
92 struct nexthop *next = NULL;
93
94 for (nexthop = bnc->nexthop; nexthop; nexthop = next)
95 {
96 next = nexthop->next;
97 XFREE (MTYPE_NEXTHOP, nexthop);
98 }
99}
100
paul94f2b392005-06-28 12:44:16 +0000101static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800102bnc_new (void)
paul718e3742002-12-13 20:15:29 +0000103{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700104 return XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
paul718e3742002-12-13 20:15:29 +0000105}
106
paul94f2b392005-06-28 12:44:16 +0000107static void
paul718e3742002-12-13 20:15:29 +0000108bnc_free (struct bgp_nexthop_cache *bnc)
109{
110 bnc_nexthop_free (bnc);
111 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
112}
David Lamparter6b0655a2014-06-04 06:53:35 +0200113
paul94f2b392005-06-28 12:44:16 +0000114static int
paul718e3742002-12-13 20:15:29 +0000115bgp_nexthop_same (struct nexthop *next1, struct nexthop *next2)
116{
117 if (next1->type != next2->type)
118 return 0;
119
120 switch (next1->type)
121 {
122 case ZEBRA_NEXTHOP_IPV4:
123 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
124 return 0;
125 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000126 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
127 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4)
128 || next1->ifindex != next2->ifindex)
129 return 0;
130 break;
paul718e3742002-12-13 20:15:29 +0000131 case ZEBRA_NEXTHOP_IFINDEX:
132 case ZEBRA_NEXTHOP_IFNAME:
133 if (next1->ifindex != next2->ifindex)
134 return 0;
135 break;
136#ifdef HAVE_IPV6
137 case ZEBRA_NEXTHOP_IPV6:
138 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
139 return 0;
140 break;
141 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
142 case ZEBRA_NEXTHOP_IPV6_IFNAME:
143 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
144 return 0;
145 if (next1->ifindex != next2->ifindex)
146 return 0;
147 break;
148#endif /* HAVE_IPV6 */
hassofa2b17e2004-03-04 17:45:00 +0000149 default:
150 /* do nothing */
151 break;
paul718e3742002-12-13 20:15:29 +0000152 }
153 return 1;
154}
155
paul94f2b392005-06-28 12:44:16 +0000156static int
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400157bgp_nexthop_cache_different (struct bgp_nexthop_cache *bnc1,
paul718e3742002-12-13 20:15:29 +0000158 struct bgp_nexthop_cache *bnc2)
159{
160 int i;
161 struct nexthop *next1, *next2;
162
163 if (bnc1->nexthop_num != bnc2->nexthop_num)
164 return 1;
165
166 next1 = bnc1->nexthop;
167 next2 = bnc2->nexthop;
168
169 for (i = 0; i < bnc1->nexthop_num; i++)
170 {
171 if (! bgp_nexthop_same (next1, next2))
172 return 1;
173
174 next1 = next1->next;
175 next2 = next2->next;
176 }
177 return 0;
178}
179
180/* If nexthop exists on connected network return 1. */
181int
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400182bgp_nexthop_onlink (afi_t afi, struct attr *attr)
paul718e3742002-12-13 20:15:29 +0000183{
184 struct bgp_node *rn;
Paul Jakmafc98d162012-01-09 11:36:23 +0000185
186 /* If zebra is not enabled return */
187 if (zlookup->sock < 0)
188 return 1;
189
paul718e3742002-12-13 20:15:29 +0000190 /* Lookup the address is onlink or not. */
191 if (afi == AFI_IP)
192 {
paul59320202004-11-09 01:54:03 +0000193 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
paul718e3742002-12-13 20:15:29 +0000194 if (rn)
195 {
196 bgp_unlock_node (rn);
197 return 1;
198 }
199 }
200#ifdef HAVE_IPV6
201 else if (afi == AFI_IP6)
202 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000203 if (attr->extra->mp_nexthop_len == 32)
paul718e3742002-12-13 20:15:29 +0000204 return 1;
Paul Jakmafb982c22007-05-04 20:15:47 +0000205 else if (attr->extra->mp_nexthop_len == 16)
paul718e3742002-12-13 20:15:29 +0000206 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000207 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000208 return 1;
209
paul59320202004-11-09 01:54:03 +0000210 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
Paul Jakmafb982c22007-05-04 20:15:47 +0000211 &attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000212 if (rn)
213 {
214 bgp_unlock_node (rn);
215 return 1;
216 }
217 }
218 }
219#endif /* HAVE_IPV6 */
220 return 0;
221}
222
223#ifdef HAVE_IPV6
224/* Check specified next-hop is reachable or not. */
paul94f2b392005-06-28 12:44:16 +0000225static int
paul718e3742002-12-13 20:15:29 +0000226bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
227 int *metricchanged)
228{
229 struct bgp_node *rn;
230 struct prefix p;
231 struct bgp_nexthop_cache *bnc;
232 struct attr *attr;
Paul Jakmafc98d162012-01-09 11:36:23 +0000233
234 /* If lookup is not enabled, return valid. */
235 if (zlookup->sock < 0)
236 {
237 if (ri->extra)
238 ri->extra->igpmetric = 0;
239 return 1;
240 }
241
paul718e3742002-12-13 20:15:29 +0000242 /* Only check IPv6 global address only nexthop. */
243 attr = ri->attr;
244
Paul Jakmafb982c22007-05-04 20:15:47 +0000245 if (attr->extra->mp_nexthop_len != 16
246 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000247 return 1;
248
249 memset (&p, 0, sizeof (struct prefix));
250 p.family = AF_INET6;
251 p.prefixlen = IPV6_MAX_BITLEN;
Paul Jakmafb982c22007-05-04 20:15:47 +0000252 p.u.prefix6 = attr->extra->mp_nexthop_global;
paul718e3742002-12-13 20:15:29 +0000253
254 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000255 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
paul718e3742002-12-13 20:15:29 +0000256
257 if (rn->info)
258 {
259 bnc = rn->info;
260 bgp_unlock_node (rn);
261 }
262 else
263 {
Denis Ovsienkob64bfc12011-08-08 19:36:44 +0400264 if (NULL == (bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global)))
265 bnc = bnc_new ();
266 else
paul718e3742002-12-13 20:15:29 +0000267 {
paul718e3742002-12-13 20:15:29 +0000268 if (changed)
269 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400270 struct bgp_table *old;
271 struct bgp_node *oldrn;
272
paul59320202004-11-09 01:54:03 +0000273 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
274 old = cache2_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000275 else
paul59320202004-11-09 01:54:03 +0000276 old = cache1_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000277
278 oldrn = bgp_node_lookup (old, &p);
279 if (oldrn)
280 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400281 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000282
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400283 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000284
285 if (bnc->metric != oldbnc->metric)
286 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000287
288 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000289 }
290 }
291 }
paul718e3742002-12-13 20:15:29 +0000292 rn->info = bnc;
293 }
294
295 if (changed)
296 *changed = bnc->changed;
297
298 if (metricchanged)
299 *metricchanged = bnc->metricchanged;
300
Paul Jakmafb982c22007-05-04 20:15:47 +0000301 if (bnc->valid && bnc->metric)
302 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
303 else if (ri->extra)
304 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000305
306 return bnc->valid;
307}
308#endif /* HAVE_IPV6 */
309
310/* Check specified next-hop is reachable or not. */
311int
312bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
313 int *changed, int *metricchanged)
314{
315 struct bgp_node *rn;
316 struct prefix p;
317 struct bgp_nexthop_cache *bnc;
318 struct in_addr addr;
Paul Jakmafc98d162012-01-09 11:36:23 +0000319
320 /* If lookup is not enabled, return valid. */
321 if (zlookup->sock < 0)
322 {
323 if (ri->extra)
324 ri->extra->igpmetric = 0;
325 return 1;
326 }
327
paul718e3742002-12-13 20:15:29 +0000328#ifdef HAVE_IPV6
329 if (afi == AFI_IP6)
330 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
331#endif /* HAVE_IPV6 */
332
333 addr = ri->attr->nexthop;
334
335 memset (&p, 0, sizeof (struct prefix));
336 p.family = AF_INET;
337 p.prefixlen = IPV4_MAX_BITLEN;
338 p.u.prefix4 = addr;
339
340 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000341 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000342
343 if (rn->info)
344 {
345 bnc = rn->info;
346 bgp_unlock_node (rn);
347 }
348 else
349 {
Denis Ovsienkob64bfc12011-08-08 19:36:44 +0400350 if (NULL == (bnc = zlookup_query (addr)))
351 bnc = bnc_new ();
352 else
paul718e3742002-12-13 20:15:29 +0000353 {
paul718e3742002-12-13 20:15:29 +0000354 if (changed)
355 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400356 struct bgp_table *old;
357 struct bgp_node *oldrn;
358
paul59320202004-11-09 01:54:03 +0000359 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
360 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000361 else
paul59320202004-11-09 01:54:03 +0000362 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000363
364 oldrn = bgp_node_lookup (old, &p);
365 if (oldrn)
366 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400367 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000368
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400369 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000370
371 if (bnc->metric != oldbnc->metric)
372 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000373
374 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000375 }
376 }
377 }
paul718e3742002-12-13 20:15:29 +0000378 rn->info = bnc;
379 }
380
381 if (changed)
382 *changed = bnc->changed;
383
384 if (metricchanged)
385 *metricchanged = bnc->metricchanged;
386
Paul Jakmafb982c22007-05-04 20:15:47 +0000387 if (bnc->valid && bnc->metric)
388 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
389 else if (ri->extra)
390 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000391
392 return bnc->valid;
393}
394
395/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000396static void
paul718e3742002-12-13 20:15:29 +0000397bgp_nexthop_cache_reset (struct bgp_table *table)
398{
399 struct bgp_node *rn;
400 struct bgp_nexthop_cache *bnc;
401
402 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
403 if ((bnc = rn->info) != NULL)
404 {
405 bnc_free (bnc);
406 rn->info = NULL;
407 bgp_unlock_node (rn);
408 }
409}
410
paul94f2b392005-06-28 12:44:16 +0000411static void
paul59320202004-11-09 01:54:03 +0000412bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000413{
414 struct bgp_node *rn;
415 struct bgp *bgp;
416 struct bgp_info *bi;
417 struct bgp_info *next;
418 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000419 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000420 int valid;
421 int current;
422 int changed;
423 int metricchanged;
424
425 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000426 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
427 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000428 else
paul59320202004-11-09 01:54:03 +0000429 bgp_nexthop_cache_table[afi] = cache1_table[afi];
paul718e3742002-12-13 20:15:29 +0000430
431 /* Get default bgp. */
432 bgp = bgp_get_default ();
433 if (bgp == NULL)
434 return;
435
436 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000437 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000438 {
439 if (peer->status != Established)
440 continue;
441
paul59320202004-11-09 01:54:03 +0000442 if (peer->afc[afi][SAFI_UNICAST])
443 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
444 if (peer->afc[afi][SAFI_MULTICAST])
445 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
446 if (peer->afc[afi][SAFI_MPLS_VPN])
447 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
paul718e3742002-12-13 20:15:29 +0000448 }
449
paul59320202004-11-09 01:54:03 +0000450 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000451 rn = bgp_route_next (rn))
452 {
453 for (bi = rn->info; bi; bi = next)
454 {
455 next = bi->next;
456
457 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
458 {
459 changed = 0;
460 metricchanged = 0;
461
Christian Frankea0f6ce52013-04-11 08:24:30 +0000462 if (bi->peer->sort == BGP_PEER_EBGP && bi->peer->ttl == 1
463 && !CHECK_FLAG(bi->peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400464 valid = bgp_nexthop_onlink (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000465 else
paul59320202004-11-09 01:54:03 +0000466 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000467 &changed, &metricchanged);
468
469 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
470
471 if (changed)
472 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
473 else
474 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
475
476 if (valid != current)
477 {
478 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
479 {
480 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000481 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000482 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000483 }
484 else
485 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000486 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000487 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000488 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000489 }
490 }
491
paul59320202004-11-09 01:54:03 +0000492 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000493 BGP_CONFIG_DAMPENING)
Paul Jakmafb982c22007-05-04 20:15:47 +0000494 && bi->extra && bi->extra->damp_info )
paul59320202004-11-09 01:54:03 +0000495 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000496 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000497 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000498 }
499 }
Paul Jakma91b9e852015-12-01 14:32:11 +0000500 if (rn->info)
501 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000502 }
503
504 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000505 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
506 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000507 else
paul59320202004-11-09 01:54:03 +0000508 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000509
510 if (BGP_DEBUG (events, EVENTS))
511 {
512 if (afi == AFI_IP)
513 zlog_debug ("scanning IPv4 Unicast routing tables");
514 else if (afi == AFI_IP6)
515 zlog_debug ("scanning IPv6 Unicast routing tables");
516 }
Christian Frankedcab1bb2012-12-07 16:45:52 +0000517
518 /* Reevaluate default-originate route-maps and announce/withdraw
519 * default route if neccesary. */
520 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
521 {
522 if (peer->status == Established
523 && CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE)
524 && peer->default_rmap[afi][safi].name)
525 bgp_default_originate (peer, afi, safi, 0);
526 }
paul718e3742002-12-13 20:15:29 +0000527}
528
paul718e3742002-12-13 20:15:29 +0000529/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000530static int
paul59320202004-11-09 01:54:03 +0000531bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000532{
533 bgp_scan_thread =
Donald Sharp774914f2015-10-14 08:50:39 -0400534 thread_add_timer (bm->master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000535
hassof4184462005-02-01 20:13:16 +0000536 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000537 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000538
paul59320202004-11-09 01:54:03 +0000539 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000540
541#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000542 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000543#endif /* HAVE_IPV6 */
544
545 return 0;
546}
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000547
548/* BGP own address structure */
549struct bgp_addr
550{
551 struct in_addr addr;
552 int refcnt;
553};
554
555static struct hash *bgp_address_hash;
556
557static void *
558bgp_address_hash_alloc (void *p)
559{
560 struct in_addr *val = p;
561 struct bgp_addr *addr;
562
563 addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr));
564 addr->refcnt = 0;
565 addr->addr.s_addr = val->s_addr;
566
567 return addr;
568}
569
570static unsigned int
571bgp_address_hash_key_make (void *p)
572{
573 const struct bgp_addr *addr = p;
574
575 return jhash_1word(addr->addr.s_addr, 0);
576}
577
578static int
579bgp_address_hash_cmp (const void *p1, const void *p2)
580{
581 const struct bgp_addr *addr1 = p1;
582 const struct bgp_addr *addr2 = p2;
583
584 return addr1->addr.s_addr == addr2->addr.s_addr;
585}
586
587void
588bgp_address_init (void)
589{
590 bgp_address_hash = hash_create (bgp_address_hash_key_make,
591 bgp_address_hash_cmp);
592}
593
594static void
595bgp_address_add (struct prefix *p)
596{
597 struct bgp_addr tmp;
598 struct bgp_addr *addr;
599
600 tmp.addr = p->u.prefix4;
601
602 addr = hash_get (bgp_address_hash, &tmp, bgp_address_hash_alloc);
603 addr->refcnt++;
604}
605
606static void
607bgp_address_del (struct prefix *p)
608{
609 struct bgp_addr tmp;
610 struct bgp_addr *addr;
611
612 tmp.addr = p->u.prefix4;
613
614 addr = hash_lookup (bgp_address_hash, &tmp);
Rakesh Garimella9e47abd2013-03-11 12:38:31 +0000615 /* may have been deleted earlier by bgp_interface_down() */
616 if (addr == NULL)
617 return;
618
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000619 addr->refcnt--;
620
621 if (addr->refcnt == 0)
622 {
623 hash_release (bgp_address_hash, addr);
624 XFREE (MTYPE_BGP_ADDR, addr);
625 }
626}
627
David Lamparter6b0655a2014-06-04 06:53:35 +0200628
paul59320202004-11-09 01:54:03 +0000629struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000630{
631 unsigned int refcnt;
632};
633
634void
635bgp_connected_add (struct connected *ifc)
636{
637 struct prefix p;
638 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000639 struct interface *ifp;
640 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000641 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000642
643 ifp = ifc->ifp;
644
645 if (! ifp)
646 return;
647
648 if (if_is_loopback (ifp))
649 return;
650
651 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000652
Donald Sharp26a18eb2015-09-29 09:25:10 -0400653 p = *(CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000654 if (addr->family == AF_INET)
655 {
paul718e3742002-12-13 20:15:29 +0000656 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
657
658 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
659 return;
660
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000661 bgp_address_add (addr);
662
paul59320202004-11-09 01:54:03 +0000663 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000664 if (rn->info)
665 {
666 bc = rn->info;
667 bc->refcnt++;
668 }
669 else
670 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000671 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000672 bc->refcnt = 1;
673 rn->info = bc;
674 }
675 }
676#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000677 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000678 {
paul718e3742002-12-13 20:15:29 +0000679 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
680
681 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
682 return;
683
684 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
685 return;
686
paul59320202004-11-09 01:54:03 +0000687 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000688 if (rn->info)
689 {
690 bc = rn->info;
691 bc->refcnt++;
692 }
693 else
694 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000695 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000696 bc->refcnt = 1;
697 rn->info = bc;
698 }
699 }
700#endif /* HAVE_IPV6 */
701}
702
703void
704bgp_connected_delete (struct connected *ifc)
705{
706 struct prefix p;
707 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000708 struct interface *ifp;
709 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000710 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000711
712 ifp = ifc->ifp;
713
714 if (if_is_loopback (ifp))
715 return;
716
717 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000718
Donald Sharp26a18eb2015-09-29 09:25:10 -0400719 p = *(CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000720 if (addr->family == AF_INET)
721 {
paul718e3742002-12-13 20:15:29 +0000722 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
723
724 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
725 return;
726
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000727 bgp_address_del (addr);
728
paul59320202004-11-09 01:54:03 +0000729 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000730 if (! rn)
731 return;
732
733 bc = rn->info;
734 bc->refcnt--;
735 if (bc->refcnt == 0)
736 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000737 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000738 rn->info = NULL;
739 }
740 bgp_unlock_node (rn);
741 bgp_unlock_node (rn);
742 }
743#ifdef HAVE_IPV6
744 else if (addr->family == AF_INET6)
745 {
paul718e3742002-12-13 20:15:29 +0000746 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
747
748 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
749 return;
750
751 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
752 return;
753
paul59320202004-11-09 01:54:03 +0000754 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000755 if (! rn)
756 return;
757
758 bc = rn->info;
759 bc->refcnt--;
760 if (bc->refcnt == 0)
761 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000762 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000763 rn->info = NULL;
764 }
765 bgp_unlock_node (rn);
766 bgp_unlock_node (rn);
767 }
768#endif /* HAVE_IPV6 */
769}
770
771int
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000772bgp_nexthop_self (struct attr *attr)
paul718e3742002-12-13 20:15:29 +0000773{
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000774 struct bgp_addr tmp, *addr;
paul718e3742002-12-13 20:15:29 +0000775
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000776 tmp.addr = attr->nexthop;
paul718e3742002-12-13 20:15:29 +0000777
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000778 addr = hash_lookup (bgp_address_hash, &tmp);
779 if (addr)
780 return 1;
781
paul718e3742002-12-13 20:15:29 +0000782 return 0;
783}
David Lamparter6b0655a2014-06-04 06:53:35 +0200784
paul94f2b392005-06-28 12:44:16 +0000785static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800786zlookup_read (void)
paul718e3742002-12-13 20:15:29 +0000787{
788 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000789 uint16_t length;
790 u_char marker;
791 u_char version;
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200792 uint16_t vrf_id;
793 uint16_t command;
794 int err;
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100795 struct in_addr raddr __attribute__((unused));
pauld3092e72006-01-17 17:33:46 +0000796 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000797 int i;
798 u_char nexthop_num;
799 struct nexthop *nexthop;
800 struct bgp_nexthop_cache *bnc;
801
802 s = zlookup->ibuf;
803 stream_reset (s);
804
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200805 err = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
806 &vrf_id, &command);
807 if (err < 0)
808 {
809 zlog_err("%s: zserv_read_header() failed", __func__);
810 return NULL;
811 }
Donald Sharpa9d4cb32015-09-17 10:54:25 -0400812
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100813 /* XXX: not doing anything with raddr */
paul718e3742002-12-13 20:15:29 +0000814 raddr.s_addr = stream_get_ipv4 (s);
815 metric = stream_getl (s);
816 nexthop_num = stream_getc (s);
817
818 if (nexthop_num)
819 {
820 bnc = bnc_new ();
821 bnc->valid = 1;
822 bnc->metric = metric;
823 bnc->nexthop_num = nexthop_num;
824
825 for (i = 0; i < nexthop_num; i++)
826 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700827 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000828 nexthop->type = stream_getc (s);
829 switch (nexthop->type)
830 {
831 case ZEBRA_NEXTHOP_IPV4:
832 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
833 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000834 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
835 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
836 nexthop->ifindex = stream_getl (s);
837 break;
paul718e3742002-12-13 20:15:29 +0000838 case ZEBRA_NEXTHOP_IFINDEX:
839 case ZEBRA_NEXTHOP_IFNAME:
840 nexthop->ifindex = stream_getl (s);
841 break;
hassofa2b17e2004-03-04 17:45:00 +0000842 default:
843 /* do nothing */
844 break;
paul718e3742002-12-13 20:15:29 +0000845 }
846 bnc_nexthop_add (bnc, nexthop);
847 }
848 }
849 else
850 return NULL;
851
852 return bnc;
853}
854
855struct bgp_nexthop_cache *
856zlookup_query (struct in_addr addr)
857{
858 int ret;
859 struct stream *s;
860
861 /* Check socket. */
862 if (zlookup->sock < 0)
863 return NULL;
864
865 s = zlookup->obuf;
866 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +0800867 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000868 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000869
870 stream_putw_at (s, 0, stream_get_endp (s));
871
872 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000873 if (ret < 0)
874 {
875 zlog_err ("can't write to zlookup->sock");
876 close (zlookup->sock);
877 zlookup->sock = -1;
878 return NULL;
879 }
880 if (ret == 0)
881 {
882 zlog_err ("zlookup->sock connection closed");
883 close (zlookup->sock);
884 zlookup->sock = -1;
885 return NULL;
886 }
887
888 return zlookup_read ();
889}
890
891#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000892static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800893zlookup_read_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000894{
895 struct stream *s;
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200896 uint16_t length, vrf_id, cmd;
pauld3092e72006-01-17 17:33:46 +0000897 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000898 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000899 uint32_t metric;
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200900 int i, err;
paul718e3742002-12-13 20:15:29 +0000901 u_char nexthop_num;
902 struct nexthop *nexthop;
903 struct bgp_nexthop_cache *bnc;
904
905 s = zlookup->ibuf;
906 stream_reset (s);
907
Nicolas Dichtel794c4732015-09-16 09:42:36 +0200908 err = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
909 &vrf_id, &cmd);
910 if (err < 0)
911 {
912 zlog_err("%s: zserv_read_header() failed", __func__);
913 return NULL;
914 }
Donald Sharpa9d4cb32015-09-17 10:54:25 -0400915
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100916 /* XXX: not actually doing anything with raddr */
paul718e3742002-12-13 20:15:29 +0000917 stream_get (&raddr, s, 16);
918
919 metric = stream_getl (s);
920 nexthop_num = stream_getc (s);
921
922 if (nexthop_num)
923 {
924 bnc = bnc_new ();
925 bnc->valid = 1;
926 bnc->metric = metric;
927 bnc->nexthop_num = nexthop_num;
928
929 for (i = 0; i < nexthop_num; i++)
930 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700931 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000932 nexthop->type = stream_getc (s);
933 switch (nexthop->type)
934 {
935 case ZEBRA_NEXTHOP_IPV6:
936 stream_get (&nexthop->gate.ipv6, s, 16);
937 break;
938 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
939 case ZEBRA_NEXTHOP_IPV6_IFNAME:
940 stream_get (&nexthop->gate.ipv6, s, 16);
941 nexthop->ifindex = stream_getl (s);
942 break;
943 case ZEBRA_NEXTHOP_IFINDEX:
944 case ZEBRA_NEXTHOP_IFNAME:
945 nexthop->ifindex = stream_getl (s);
946 break;
hassofa2b17e2004-03-04 17:45:00 +0000947 default:
948 /* do nothing */
949 break;
paul718e3742002-12-13 20:15:29 +0000950 }
951 bnc_nexthop_add (bnc, nexthop);
952 }
953 }
954 else
955 return NULL;
956
957 return bnc;
958}
959
960struct bgp_nexthop_cache *
961zlookup_query_ipv6 (struct in6_addr *addr)
962{
963 int ret;
964 struct stream *s;
965
966 /* Check socket. */
967 if (zlookup->sock < 0)
968 return NULL;
969
970 s = zlookup->obuf;
971 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +0800972 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000973 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000974 stream_putw_at (s, 0, stream_get_endp (s));
975
976 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000977 if (ret < 0)
978 {
979 zlog_err ("can't write to zlookup->sock");
980 close (zlookup->sock);
981 zlookup->sock = -1;
982 return NULL;
983 }
984 if (ret == 0)
985 {
986 zlog_err ("zlookup->sock connection closed");
987 close (zlookup->sock);
988 zlookup->sock = -1;
989 return NULL;
990 }
991
992 return zlookup_read_ipv6 ();
993}
994#endif /* HAVE_IPV6 */
995
paul94f2b392005-06-28 12:44:16 +0000996static int
997bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
998 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000999{
1000 struct stream *s;
1001 int ret;
Nicolas Dichtel794c4732015-09-16 09:42:36 +02001002 u_int16_t length, vrf_id, command;
pauld3092e72006-01-17 17:33:46 +00001003 u_char version, marker;
Paul Jakma7aa9dce2014-09-19 14:42:23 +01001004 struct in_addr addr __attribute__((unused));
paul718e3742002-12-13 20:15:29 +00001005 struct in_addr nexthop;
1006 u_int32_t metric = 0;
1007 u_char nexthop_num;
1008 u_char nexthop_type;
1009
1010 /* If lookup connection is not available return valid. */
1011 if (zlookup->sock < 0)
1012 {
1013 if (igpmetric)
1014 *igpmetric = 0;
1015 return 1;
1016 }
1017
1018 /* Send query to the lookup connection */
1019 s = zlookup->obuf;
1020 stream_reset (s);
Feng Luc99f3482014-10-16 09:52:36 +08001021 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, VRF_DEFAULT);
pauld3092e72006-01-17 17:33:46 +00001022
paul718e3742002-12-13 20:15:29 +00001023 stream_putc (s, p->prefixlen);
1024 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +00001025
1026 stream_putw_at (s, 0, stream_get_endp (s));
1027
paul718e3742002-12-13 20:15:29 +00001028 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +00001029 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +00001030
1031 if (ret < 0)
1032 {
1033 zlog_err ("can't write to zlookup->sock");
1034 close (zlookup->sock);
1035 zlookup->sock = -1;
1036 return 1;
1037 }
1038 if (ret == 0)
1039 {
1040 zlog_err ("zlookup->sock connection closed");
1041 close (zlookup->sock);
1042 zlookup->sock = -1;
1043 return 1;
1044 }
1045
1046 /* Get result. */
1047 stream_reset (s);
1048
Nicolas Dichtel794c4732015-09-16 09:42:36 +02001049 ret = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
1050 &vrf_id, &command);
1051 if (ret < 0)
1052 {
1053 zlog_err("%s: zserv_read_header() failed", __func__);
1054 return 0;
1055 }
Donald Sharpa9d4cb32015-09-17 10:54:25 -04001056
Paul Jakma7aa9dce2014-09-19 14:42:23 +01001057 /* XXX: not using addr */
paul718e3742002-12-13 20:15:29 +00001058 addr.s_addr = stream_get_ipv4 (s);
1059 metric = stream_getl (s);
1060 nexthop_num = stream_getc (s);
1061
1062 /* Set IGP metric value. */
1063 if (igpmetric)
1064 *igpmetric = metric;
1065
1066 /* If there is nexthop then this is active route. */
1067 if (nexthop_num)
1068 {
1069 nexthop.s_addr = 0;
1070 nexthop_type = stream_getc (s);
Christian Frankea12afd52013-05-25 14:01:36 +00001071 switch (nexthop_type)
paul718e3742002-12-13 20:15:29 +00001072 {
Christian Frankea12afd52013-05-25 14:01:36 +00001073 case ZEBRA_NEXTHOP_IPV4:
paul718e3742002-12-13 20:15:29 +00001074 nexthop.s_addr = stream_get_ipv4 (s);
Christian Frankea12afd52013-05-25 14:01:36 +00001075 break;
1076 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1077 nexthop.s_addr = stream_get_ipv4 (s);
1078 /* ifindex */ (void)stream_getl (s);
1079 break;
1080 default:
1081 /* do nothing */
1082 break;
paul718e3742002-12-13 20:15:29 +00001083 }
Christian Frankea12afd52013-05-25 14:01:36 +00001084 *igpnexthop = nexthop;
paul718e3742002-12-13 20:15:29 +00001085
1086 return 1;
1087 }
1088 else
1089 return 0;
1090}
1091
1092/* Scan all configured BGP route then check the route exists in IGP or
1093 not. */
paul94f2b392005-06-28 12:44:16 +00001094static int
paul718e3742002-12-13 20:15:29 +00001095bgp_import (struct thread *t)
1096{
1097 struct bgp *bgp;
1098 struct bgp_node *rn;
1099 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001100 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001101 int valid;
1102 u_int32_t metric;
1103 struct in_addr nexthop;
1104 afi_t afi;
1105 safi_t safi;
1106
1107 bgp_import_thread =
Donald Sharp774914f2015-10-14 08:50:39 -04001108 thread_add_timer (bm->master, bgp_import, NULL, bgp_import_interval);
paul718e3742002-12-13 20:15:29 +00001109
hassof4184462005-02-01 20:13:16 +00001110 if (BGP_DEBUG (events, EVENTS))
1111 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001112
paul1eb8ef22005-04-07 07:30:20 +00001113 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001114 {
1115 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1116 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1117 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1118 rn = bgp_route_next (rn))
1119 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001120 {
paul6cbbc3c2003-04-28 17:11:02 +00001121 if (bgp_static->backdoor)
1122 continue;
paul718e3742002-12-13 20:15:29 +00001123
paul6cbbc3c2003-04-28 17:11:02 +00001124 valid = bgp_static->valid;
1125 metric = bgp_static->igpmetric;
1126 nexthop = bgp_static->igpnexthop;
1127
1128 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1129 && afi == AFI_IP && safi == SAFI_UNICAST)
1130 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1131 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001132 else
paul6cbbc3c2003-04-28 17:11:02 +00001133 {
1134 bgp_static->valid = 1;
1135 bgp_static->igpmetric = 0;
1136 bgp_static->igpnexthop.s_addr = 0;
1137 }
1138
1139 if (bgp_static->valid != valid)
1140 {
1141 if (bgp_static->valid)
1142 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1143 else
1144 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1145 }
1146 else if (bgp_static->valid)
1147 {
1148 if (bgp_static->igpmetric != metric
1149 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1150 || bgp_static->rmap.name)
1151 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1152 }
paul718e3742002-12-13 20:15:29 +00001153 }
paul6cbbc3c2003-04-28 17:11:02 +00001154 }
paul718e3742002-12-13 20:15:29 +00001155 return 0;
1156}
1157
1158/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001159static int
paul718e3742002-12-13 20:15:29 +00001160zlookup_connect (struct thread *t)
1161{
1162 struct zclient *zlookup;
1163
1164 zlookup = THREAD_ARG (t);
1165 zlookup->t_connect = NULL;
1166
1167 if (zlookup->sock != -1)
1168 return 0;
1169
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001170 if (zclient_socket_connect (zlookup) < 0)
paul718e3742002-12-13 20:15:29 +00001171 return -1;
1172
paul718e3742002-12-13 20:15:29 +00001173 return 0;
1174}
1175
1176/* Check specified multiaccess next-hop. */
1177int
1178bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1179{
1180 struct bgp_node *rn1;
1181 struct bgp_node *rn2;
1182 struct prefix p1;
1183 struct prefix p2;
1184 struct in_addr addr;
1185 int ret;
1186
1187 ret = inet_aton (peer, &addr);
1188 if (! ret)
1189 return 0;
1190
1191 memset (&p1, 0, sizeof (struct prefix));
1192 p1.family = AF_INET;
1193 p1.prefixlen = IPV4_MAX_BITLEN;
1194 p1.u.prefix4 = nexthop;
1195 memset (&p2, 0, sizeof (struct prefix));
1196 p2.family = AF_INET;
1197 p2.prefixlen = IPV4_MAX_BITLEN;
1198 p2.u.prefix4 = addr;
1199
1200 /* If bgp scan is not enabled, return invalid. */
1201 if (zlookup->sock < 0)
1202 return 0;
1203
paul59320202004-11-09 01:54:03 +00001204 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001205 if (! rn1)
1206 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001207 bgp_unlock_node (rn1);
paul718e3742002-12-13 20:15:29 +00001208
paul59320202004-11-09 01:54:03 +00001209 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001210 if (! rn2)
1211 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001212 bgp_unlock_node (rn2);
paul718e3742002-12-13 20:15:29 +00001213
Chris Caputo6c88b442010-07-27 16:28:55 +00001214 /* This is safe, even with above unlocks, since we are just
1215 comparing pointers to the objects, not the objects themselves. */
paul718e3742002-12-13 20:15:29 +00001216 if (rn1 == rn2)
1217 return 1;
1218
1219 return 0;
1220}
David Lamparter6b0655a2014-06-04 06:53:35 +02001221
paul718e3742002-12-13 20:15:29 +00001222DEFUN (bgp_scan_time,
1223 bgp_scan_time_cmd,
1224 "bgp scan-time <5-60>",
1225 "BGP specific commands\n"
1226 "Configure background scanner interval\n"
1227 "Scanner interval (seconds)\n")
1228{
1229 bgp_scan_interval = atoi (argv[0]);
1230
1231 if (bgp_scan_thread)
1232 {
1233 thread_cancel (bgp_scan_thread);
1234 bgp_scan_thread =
Donald Sharp774914f2015-10-14 08:50:39 -04001235 thread_add_timer (bm->master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001236 }
1237
1238 return CMD_SUCCESS;
1239}
1240
1241DEFUN (no_bgp_scan_time,
1242 no_bgp_scan_time_cmd,
1243 "no bgp scan-time",
1244 NO_STR
1245 "BGP specific commands\n"
1246 "Configure background scanner interval\n")
1247{
1248 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1249
1250 if (bgp_scan_thread)
1251 {
1252 thread_cancel (bgp_scan_thread);
1253 bgp_scan_thread =
Donald Sharp774914f2015-10-14 08:50:39 -04001254 thread_add_timer (bm->master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001255 }
1256
1257 return CMD_SUCCESS;
1258}
1259
1260ALIAS (no_bgp_scan_time,
1261 no_bgp_scan_time_val_cmd,
1262 "no bgp scan-time <5-60>",
1263 NO_STR
1264 "BGP specific commands\n"
1265 "Configure background scanner interval\n"
1266 "Scanner interval (seconds)\n")
1267
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001268static int
1269show_ip_bgp_scan_tables (struct vty *vty, const char detail)
paul718e3742002-12-13 20:15:29 +00001270{
1271 struct bgp_node *rn;
1272 struct bgp_nexthop_cache *bnc;
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001273 char buf[INET6_ADDRSTRLEN];
1274 u_char i;
paul718e3742002-12-13 20:15:29 +00001275
1276 if (bgp_scan_thread)
1277 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1278 else
1279 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1280 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1281
1282 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001283 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001284 if ((bnc = rn->info) != NULL)
1285 {
1286 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001287 {
paul718e3742002-12-13 20:15:29 +00001288 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001289 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1290 if (detail)
1291 for (i = 0; i < bnc->nexthop_num; i++)
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001292 switch (bnc->nexthop[i].type)
1293 {
1294 case NEXTHOP_TYPE_IPV4:
1295 vty_out (vty, " gate %s%s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1296 break;
Christian Frankebb97e462013-05-25 14:01:35 +00001297 case NEXTHOP_TYPE_IPV4_IFINDEX:
1298 vty_out (vty, " gate %s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN));
1299 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1300 break;
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001301 case NEXTHOP_TYPE_IFINDEX:
1302 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1303 break;
1304 default:
1305 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1306 }
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001307 }
paul718e3742002-12-13 20:15:29 +00001308 else
1309 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001310 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001311 }
1312
1313#ifdef HAVE_IPV6
1314 {
paul59320202004-11-09 01:54:03 +00001315 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1316 rn;
1317 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001318 if ((bnc = rn->info) != NULL)
1319 {
1320 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001321 {
paul718e3742002-12-13 20:15:29 +00001322 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001323 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001324 bnc->metric, VTY_NEWLINE);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001325 if (detail)
1326 for (i = 0; i < bnc->nexthop_num; i++)
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001327 switch (bnc->nexthop[i].type)
1328 {
1329 case NEXTHOP_TYPE_IPV6:
1330 vty_out (vty, " gate %s%s", inet_ntop (AF_INET6, &bnc->nexthop[i].gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1331 break;
1332 case NEXTHOP_TYPE_IFINDEX:
1333 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1334 break;
1335 default:
1336 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1337 }
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001338 }
paul718e3742002-12-13 20:15:29 +00001339 else
1340 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001341 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001342 VTY_NEWLINE);
1343 }
1344 }
1345#endif /* HAVE_IPV6 */
1346
1347 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001348 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1349 rn;
1350 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001351 if (rn->info != NULL)
1352 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1353 VTY_NEWLINE);
1354
1355#ifdef HAVE_IPV6
1356 {
paul59320202004-11-09 01:54:03 +00001357 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1358 rn;
1359 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001360 if (rn->info != NULL)
1361 vty_out (vty, " %s/%d%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001362 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001363 rn->p.prefixlen,
1364 VTY_NEWLINE);
1365 }
1366#endif /* HAVE_IPV6 */
1367
1368 return CMD_SUCCESS;
1369}
1370
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001371DEFUN (show_ip_bgp_scan,
1372 show_ip_bgp_scan_cmd,
1373 "show ip bgp scan",
1374 SHOW_STR
1375 IP_STR
1376 BGP_STR
1377 "BGP scan status\n")
1378{
1379 return show_ip_bgp_scan_tables (vty, 0);
1380}
1381
1382DEFUN (show_ip_bgp_scan_detail,
1383 show_ip_bgp_scan_detail_cmd,
1384 "show ip bgp scan detail",
1385 SHOW_STR
1386 IP_STR
1387 BGP_STR
1388 "BGP scan status\n"
1389 "More detailed output\n")
1390{
1391 return show_ip_bgp_scan_tables (vty, 1);
1392}
1393
paul718e3742002-12-13 20:15:29 +00001394int
1395bgp_config_write_scan_time (struct vty *vty)
1396{
1397 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1398 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1399 return CMD_SUCCESS;
1400}
1401
1402void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -08001403bgp_scan_init (void)
paul718e3742002-12-13 20:15:29 +00001404{
Donald Sharp774914f2015-10-14 08:50:39 -04001405 zlookup = zclient_new (bm->master);
paul718e3742002-12-13 20:15:29 +00001406 zlookup->sock = -1;
Donald Sharp774914f2015-10-14 08:50:39 -04001407 zlookup->t_connect = thread_add_event (bm->master, zlookup_connect, zlookup, 0);
paul718e3742002-12-13 20:15:29 +00001408
1409 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1410 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1411
Paul Jakma64e580a2006-02-21 01:09:01 +00001412 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1413 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001414 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001415
Paul Jakma64e580a2006-02-21 01:09:01 +00001416 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001417
1418#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001419 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1420 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001421 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001422 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001423#endif /* HAVE_IPV6 */
1424
1425 /* Make BGP scan thread. */
Donald Sharp774914f2015-10-14 08:50:39 -04001426 bgp_scan_thread = thread_add_timer (bm->master, bgp_scan_timer,
paul59320202004-11-09 01:54:03 +00001427 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001428 /* Make BGP import there. */
Donald Sharp774914f2015-10-14 08:50:39 -04001429 bgp_import_thread = thread_add_timer (bm->master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001430
1431 install_element (BGP_NODE, &bgp_scan_time_cmd);
1432 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1433 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1434 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001435 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01001436 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
paul718e3742002-12-13 20:15:29 +00001437 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001438 install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
paul718e3742002-12-13 20:15:29 +00001439}
Chris Caputo228da422009-07-18 05:44:03 +00001440
1441void
1442bgp_scan_finish (void)
1443{
Lou Berger056f3762013-04-10 12:30:04 -07001444 if (cache1_table[AFI_IP])
1445 bgp_table_unlock (cache1_table[AFI_IP]);
Chris Caputo228da422009-07-18 05:44:03 +00001446 cache1_table[AFI_IP] = NULL;
1447
Lou Berger056f3762013-04-10 12:30:04 -07001448 if (cache2_table[AFI_IP])
1449 bgp_table_unlock (cache2_table[AFI_IP]);
Chris Caputo228da422009-07-18 05:44:03 +00001450 cache2_table[AFI_IP] = NULL;
Lou Berger056f3762013-04-10 12:30:04 -07001451
1452 if (bgp_connected_table[AFI_IP])
1453 bgp_table_unlock (bgp_connected_table[AFI_IP]);
Chris Caputo228da422009-07-18 05:44:03 +00001454 bgp_connected_table[AFI_IP] = NULL;
1455
1456#ifdef HAVE_IPV6
Lou Berger056f3762013-04-10 12:30:04 -07001457 if (cache1_table[AFI_IP6])
1458 bgp_table_unlock (cache1_table[AFI_IP6]);
Chris Caputo228da422009-07-18 05:44:03 +00001459 cache1_table[AFI_IP6] = NULL;
1460
Lou Berger056f3762013-04-10 12:30:04 -07001461 if (cache2_table[AFI_IP6])
1462 bgp_table_unlock (cache2_table[AFI_IP6]);
Chris Caputo228da422009-07-18 05:44:03 +00001463 cache2_table[AFI_IP6] = NULL;
1464
Lou Berger056f3762013-04-10 12:30:04 -07001465 if (bgp_connected_table[AFI_IP6])
1466 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
Chris Caputo228da422009-07-18 05:44:03 +00001467 bgp_connected_table[AFI_IP6] = NULL;
1468#endif /* HAVE_IPV6 */
1469}