blob: 17586bc816a1ccb08e9a6b312a5c58be4eb4766b [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 */
48
49/* 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;
paul718e3742002-12-13 20:15:29 +000071
72/* 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}
112
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;
125 case ZEBRA_NEXTHOP_IFINDEX:
126 case ZEBRA_NEXTHOP_IFNAME:
127 if (next1->ifindex != next2->ifindex)
128 return 0;
129 break;
130#ifdef HAVE_IPV6
131 case ZEBRA_NEXTHOP_IPV6:
132 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
133 return 0;
134 break;
135 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
136 case ZEBRA_NEXTHOP_IPV6_IFNAME:
137 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
138 return 0;
139 if (next1->ifindex != next2->ifindex)
140 return 0;
141 break;
142#endif /* HAVE_IPV6 */
hassofa2b17e2004-03-04 17:45:00 +0000143 default:
144 /* do nothing */
145 break;
paul718e3742002-12-13 20:15:29 +0000146 }
147 return 1;
148}
149
paul94f2b392005-06-28 12:44:16 +0000150static int
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400151bgp_nexthop_cache_different (struct bgp_nexthop_cache *bnc1,
paul718e3742002-12-13 20:15:29 +0000152 struct bgp_nexthop_cache *bnc2)
153{
154 int i;
155 struct nexthop *next1, *next2;
156
157 if (bnc1->nexthop_num != bnc2->nexthop_num)
158 return 1;
159
160 next1 = bnc1->nexthop;
161 next2 = bnc2->nexthop;
162
163 for (i = 0; i < bnc1->nexthop_num; i++)
164 {
165 if (! bgp_nexthop_same (next1, next2))
166 return 1;
167
168 next1 = next1->next;
169 next2 = next2->next;
170 }
171 return 0;
172}
173
174/* If nexthop exists on connected network return 1. */
175int
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400176bgp_nexthop_onlink (afi_t afi, struct attr *attr)
paul718e3742002-12-13 20:15:29 +0000177{
178 struct bgp_node *rn;
Paul Jakmafc98d162012-01-09 11:36:23 +0000179
180 /* If zebra is not enabled return */
181 if (zlookup->sock < 0)
182 return 1;
183
paul718e3742002-12-13 20:15:29 +0000184 /* Lookup the address is onlink or not. */
185 if (afi == AFI_IP)
186 {
paul59320202004-11-09 01:54:03 +0000187 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
paul718e3742002-12-13 20:15:29 +0000188 if (rn)
189 {
190 bgp_unlock_node (rn);
191 return 1;
192 }
193 }
194#ifdef HAVE_IPV6
195 else if (afi == AFI_IP6)
196 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000197 if (attr->extra->mp_nexthop_len == 32)
paul718e3742002-12-13 20:15:29 +0000198 return 1;
Paul Jakmafb982c22007-05-04 20:15:47 +0000199 else if (attr->extra->mp_nexthop_len == 16)
paul718e3742002-12-13 20:15:29 +0000200 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000201 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000202 return 1;
203
paul59320202004-11-09 01:54:03 +0000204 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
Paul Jakmafb982c22007-05-04 20:15:47 +0000205 &attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000206 if (rn)
207 {
208 bgp_unlock_node (rn);
209 return 1;
210 }
211 }
212 }
213#endif /* HAVE_IPV6 */
214 return 0;
215}
216
217#ifdef HAVE_IPV6
218/* Check specified next-hop is reachable or not. */
paul94f2b392005-06-28 12:44:16 +0000219static int
paul718e3742002-12-13 20:15:29 +0000220bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
221 int *metricchanged)
222{
223 struct bgp_node *rn;
224 struct prefix p;
225 struct bgp_nexthop_cache *bnc;
226 struct attr *attr;
Paul Jakmafc98d162012-01-09 11:36:23 +0000227
228 /* If lookup is not enabled, return valid. */
229 if (zlookup->sock < 0)
230 {
231 if (ri->extra)
232 ri->extra->igpmetric = 0;
233 return 1;
234 }
235
paul718e3742002-12-13 20:15:29 +0000236 /* Only check IPv6 global address only nexthop. */
237 attr = ri->attr;
238
Paul Jakmafb982c22007-05-04 20:15:47 +0000239 if (attr->extra->mp_nexthop_len != 16
240 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000241 return 1;
242
243 memset (&p, 0, sizeof (struct prefix));
244 p.family = AF_INET6;
245 p.prefixlen = IPV6_MAX_BITLEN;
Paul Jakmafb982c22007-05-04 20:15:47 +0000246 p.u.prefix6 = attr->extra->mp_nexthop_global;
paul718e3742002-12-13 20:15:29 +0000247
248 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000249 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
paul718e3742002-12-13 20:15:29 +0000250
251 if (rn->info)
252 {
253 bnc = rn->info;
254 bgp_unlock_node (rn);
255 }
256 else
257 {
Denis Ovsienkob64bfc12011-08-08 19:36:44 +0400258 if (NULL == (bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global)))
259 bnc = bnc_new ();
260 else
paul718e3742002-12-13 20:15:29 +0000261 {
paul718e3742002-12-13 20:15:29 +0000262 if (changed)
263 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400264 struct bgp_table *old;
265 struct bgp_node *oldrn;
266
paul59320202004-11-09 01:54:03 +0000267 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
268 old = cache2_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000269 else
paul59320202004-11-09 01:54:03 +0000270 old = cache1_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000271
272 oldrn = bgp_node_lookup (old, &p);
273 if (oldrn)
274 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400275 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000276
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400277 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000278
279 if (bnc->metric != oldbnc->metric)
280 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000281
282 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000283 }
284 }
285 }
paul718e3742002-12-13 20:15:29 +0000286 rn->info = bnc;
287 }
288
289 if (changed)
290 *changed = bnc->changed;
291
292 if (metricchanged)
293 *metricchanged = bnc->metricchanged;
294
Paul Jakmafb982c22007-05-04 20:15:47 +0000295 if (bnc->valid && bnc->metric)
296 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
297 else if (ri->extra)
298 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000299
300 return bnc->valid;
301}
302#endif /* HAVE_IPV6 */
303
304/* Check specified next-hop is reachable or not. */
305int
306bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
307 int *changed, int *metricchanged)
308{
309 struct bgp_node *rn;
310 struct prefix p;
311 struct bgp_nexthop_cache *bnc;
312 struct in_addr addr;
Paul Jakmafc98d162012-01-09 11:36:23 +0000313
314 /* If lookup is not enabled, return valid. */
315 if (zlookup->sock < 0)
316 {
317 if (ri->extra)
318 ri->extra->igpmetric = 0;
319 return 1;
320 }
321
paul718e3742002-12-13 20:15:29 +0000322#ifdef HAVE_IPV6
323 if (afi == AFI_IP6)
324 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
325#endif /* HAVE_IPV6 */
326
327 addr = ri->attr->nexthop;
328
329 memset (&p, 0, sizeof (struct prefix));
330 p.family = AF_INET;
331 p.prefixlen = IPV4_MAX_BITLEN;
332 p.u.prefix4 = addr;
333
334 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000335 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000336
337 if (rn->info)
338 {
339 bnc = rn->info;
340 bgp_unlock_node (rn);
341 }
342 else
343 {
Denis Ovsienkob64bfc12011-08-08 19:36:44 +0400344 if (NULL == (bnc = zlookup_query (addr)))
345 bnc = bnc_new ();
346 else
paul718e3742002-12-13 20:15:29 +0000347 {
paul718e3742002-12-13 20:15:29 +0000348 if (changed)
349 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400350 struct bgp_table *old;
351 struct bgp_node *oldrn;
352
paul59320202004-11-09 01:54:03 +0000353 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
354 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000355 else
paul59320202004-11-09 01:54:03 +0000356 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000357
358 oldrn = bgp_node_lookup (old, &p);
359 if (oldrn)
360 {
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400361 struct bgp_nexthop_cache *oldbnc = oldrn->info;
paul718e3742002-12-13 20:15:29 +0000362
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400363 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
paul718e3742002-12-13 20:15:29 +0000364
365 if (bnc->metric != oldbnc->metric)
366 bnc->metricchanged = 1;
Chris Caputo6c88b442010-07-27 16:28:55 +0000367
368 bgp_unlock_node (oldrn);
paul718e3742002-12-13 20:15:29 +0000369 }
370 }
371 }
paul718e3742002-12-13 20:15:29 +0000372 rn->info = bnc;
373 }
374
375 if (changed)
376 *changed = bnc->changed;
377
378 if (metricchanged)
379 *metricchanged = bnc->metricchanged;
380
Paul Jakmafb982c22007-05-04 20:15:47 +0000381 if (bnc->valid && bnc->metric)
382 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
383 else if (ri->extra)
384 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000385
386 return bnc->valid;
387}
388
389/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000390static void
paul718e3742002-12-13 20:15:29 +0000391bgp_nexthop_cache_reset (struct bgp_table *table)
392{
393 struct bgp_node *rn;
394 struct bgp_nexthop_cache *bnc;
395
396 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
397 if ((bnc = rn->info) != NULL)
398 {
399 bnc_free (bnc);
400 rn->info = NULL;
401 bgp_unlock_node (rn);
402 }
403}
404
paul94f2b392005-06-28 12:44:16 +0000405static void
paul59320202004-11-09 01:54:03 +0000406bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000407{
408 struct bgp_node *rn;
409 struct bgp *bgp;
410 struct bgp_info *bi;
411 struct bgp_info *next;
412 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000413 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000414 int valid;
415 int current;
416 int changed;
417 int metricchanged;
418
419 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000420 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
421 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000422 else
paul59320202004-11-09 01:54:03 +0000423 bgp_nexthop_cache_table[afi] = cache1_table[afi];
paul718e3742002-12-13 20:15:29 +0000424
425 /* Get default bgp. */
426 bgp = bgp_get_default ();
427 if (bgp == NULL)
428 return;
429
430 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000431 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000432 {
433 if (peer->status != Established)
434 continue;
435
paul59320202004-11-09 01:54:03 +0000436 if (peer->afc[afi][SAFI_UNICAST])
437 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
438 if (peer->afc[afi][SAFI_MULTICAST])
439 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
440 if (peer->afc[afi][SAFI_MPLS_VPN])
441 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
paul718e3742002-12-13 20:15:29 +0000442 }
443
paul59320202004-11-09 01:54:03 +0000444 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000445 rn = bgp_route_next (rn))
446 {
447 for (bi = rn->info; bi; bi = next)
448 {
449 next = bi->next;
450
451 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
452 {
453 changed = 0;
454 metricchanged = 0;
455
Christian Frankea0f6ce52013-04-11 08:24:30 +0000456 if (bi->peer->sort == BGP_PEER_EBGP && bi->peer->ttl == 1
457 && !CHECK_FLAG(bi->peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400458 valid = bgp_nexthop_onlink (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000459 else
paul59320202004-11-09 01:54:03 +0000460 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000461 &changed, &metricchanged);
462
463 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
464
465 if (changed)
466 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
467 else
468 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
469
470 if (valid != current)
471 {
472 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
473 {
474 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000475 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000476 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000477 }
478 else
479 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000480 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000481 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000482 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000483 }
484 }
485
paul59320202004-11-09 01:54:03 +0000486 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000487 BGP_CONFIG_DAMPENING)
Paul Jakmafb982c22007-05-04 20:15:47 +0000488 && bi->extra && bi->extra->damp_info )
paul59320202004-11-09 01:54:03 +0000489 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000490 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000491 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000492 }
493 }
paul59320202004-11-09 01:54:03 +0000494 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000495 }
496
497 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000498 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
499 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000500 else
paul59320202004-11-09 01:54:03 +0000501 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000502
503 if (BGP_DEBUG (events, EVENTS))
504 {
505 if (afi == AFI_IP)
506 zlog_debug ("scanning IPv4 Unicast routing tables");
507 else if (afi == AFI_IP6)
508 zlog_debug ("scanning IPv6 Unicast routing tables");
509 }
Christian Frankedcab1bb2012-12-07 16:45:52 +0000510
511 /* Reevaluate default-originate route-maps and announce/withdraw
512 * default route if neccesary. */
513 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
514 {
515 if (peer->status == Established
516 && CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE)
517 && peer->default_rmap[afi][safi].name)
518 bgp_default_originate (peer, afi, safi, 0);
519 }
paul718e3742002-12-13 20:15:29 +0000520}
521
paul718e3742002-12-13 20:15:29 +0000522/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000523static int
paul59320202004-11-09 01:54:03 +0000524bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000525{
526 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +0000527 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000528
hassof4184462005-02-01 20:13:16 +0000529 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000530 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000531
paul59320202004-11-09 01:54:03 +0000532 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000533
534#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000535 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000536#endif /* HAVE_IPV6 */
537
538 return 0;
539}
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000540
541/* BGP own address structure */
542struct bgp_addr
543{
544 struct in_addr addr;
545 int refcnt;
546};
547
548static struct hash *bgp_address_hash;
549
550static void *
551bgp_address_hash_alloc (void *p)
552{
553 struct in_addr *val = p;
554 struct bgp_addr *addr;
555
556 addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr));
557 addr->refcnt = 0;
558 addr->addr.s_addr = val->s_addr;
559
560 return addr;
561}
562
563static unsigned int
564bgp_address_hash_key_make (void *p)
565{
566 const struct bgp_addr *addr = p;
567
568 return jhash_1word(addr->addr.s_addr, 0);
569}
570
571static int
572bgp_address_hash_cmp (const void *p1, const void *p2)
573{
574 const struct bgp_addr *addr1 = p1;
575 const struct bgp_addr *addr2 = p2;
576
577 return addr1->addr.s_addr == addr2->addr.s_addr;
578}
579
580void
581bgp_address_init (void)
582{
583 bgp_address_hash = hash_create (bgp_address_hash_key_make,
584 bgp_address_hash_cmp);
585}
586
587static void
588bgp_address_add (struct prefix *p)
589{
590 struct bgp_addr tmp;
591 struct bgp_addr *addr;
592
593 tmp.addr = p->u.prefix4;
594
595 addr = hash_get (bgp_address_hash, &tmp, bgp_address_hash_alloc);
596 addr->refcnt++;
597}
598
599static void
600bgp_address_del (struct prefix *p)
601{
602 struct bgp_addr tmp;
603 struct bgp_addr *addr;
604
605 tmp.addr = p->u.prefix4;
606
607 addr = hash_lookup (bgp_address_hash, &tmp);
Rakesh Garimella9e47abd2013-03-11 12:38:31 +0000608 /* may have been deleted earlier by bgp_interface_down() */
609 if (addr == NULL)
610 return;
611
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000612 addr->refcnt--;
613
614 if (addr->refcnt == 0)
615 {
616 hash_release (bgp_address_hash, addr);
617 XFREE (MTYPE_BGP_ADDR, addr);
618 }
619}
620
paul718e3742002-12-13 20:15:29 +0000621
paul59320202004-11-09 01:54:03 +0000622struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000623{
624 unsigned int refcnt;
625};
626
627void
628bgp_connected_add (struct connected *ifc)
629{
630 struct prefix p;
631 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000632 struct interface *ifp;
633 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000634 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000635
636 ifp = ifc->ifp;
637
638 if (! ifp)
639 return;
640
641 if (if_is_loopback (ifp))
642 return;
643
644 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000645
646 if (addr->family == AF_INET)
647 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000648 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000649 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
650
651 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
652 return;
653
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000654 bgp_address_add (addr);
655
paul59320202004-11-09 01:54:03 +0000656 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000657 if (rn->info)
658 {
659 bc = rn->info;
660 bc->refcnt++;
661 }
662 else
663 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000664 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000665 bc->refcnt = 1;
666 rn->info = bc;
667 }
668 }
669#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000670 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000671 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000672 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000673 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
674
675 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
676 return;
677
678 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
679 return;
680
paul59320202004-11-09 01:54:03 +0000681 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000682 if (rn->info)
683 {
684 bc = rn->info;
685 bc->refcnt++;
686 }
687 else
688 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000689 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000690 bc->refcnt = 1;
691 rn->info = bc;
692 }
693 }
694#endif /* HAVE_IPV6 */
695}
696
697void
698bgp_connected_delete (struct connected *ifc)
699{
700 struct prefix p;
701 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000702 struct interface *ifp;
703 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000704 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000705
706 ifp = ifc->ifp;
707
708 if (if_is_loopback (ifp))
709 return;
710
711 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000712
713 if (addr->family == AF_INET)
714 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000715 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000716 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
717
718 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
719 return;
720
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000721 bgp_address_del (addr);
722
paul59320202004-11-09 01:54:03 +0000723 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000724 if (! rn)
725 return;
726
727 bc = rn->info;
728 bc->refcnt--;
729 if (bc->refcnt == 0)
730 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000731 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000732 rn->info = NULL;
733 }
734 bgp_unlock_node (rn);
735 bgp_unlock_node (rn);
736 }
737#ifdef HAVE_IPV6
738 else if (addr->family == AF_INET6)
739 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000740 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000741 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
742
743 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
744 return;
745
746 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
747 return;
748
paul59320202004-11-09 01:54:03 +0000749 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000750 if (! rn)
751 return;
752
753 bc = rn->info;
754 bc->refcnt--;
755 if (bc->refcnt == 0)
756 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000757 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000758 rn->info = NULL;
759 }
760 bgp_unlock_node (rn);
761 bgp_unlock_node (rn);
762 }
763#endif /* HAVE_IPV6 */
764}
765
766int
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000767bgp_nexthop_self (struct attr *attr)
paul718e3742002-12-13 20:15:29 +0000768{
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000769 struct bgp_addr tmp, *addr;
paul718e3742002-12-13 20:15:29 +0000770
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000771 tmp.addr = attr->nexthop;
paul718e3742002-12-13 20:15:29 +0000772
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000773 addr = hash_lookup (bgp_address_hash, &tmp);
774 if (addr)
775 return 1;
776
paul718e3742002-12-13 20:15:29 +0000777 return 0;
778}
779
paul94f2b392005-06-28 12:44:16 +0000780static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800781zlookup_read (void)
paul718e3742002-12-13 20:15:29 +0000782{
783 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000784 uint16_t length;
785 u_char marker;
786 u_char version;
787 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000788 int nbytes;
789 struct in_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000790 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000791 int i;
792 u_char nexthop_num;
793 struct nexthop *nexthop;
794 struct bgp_nexthop_cache *bnc;
795
796 s = zlookup->ibuf;
797 stream_reset (s);
798
799 nbytes = stream_read (s, zlookup->sock, 2);
800 length = stream_getw (s);
801
802 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000803 marker = stream_getc (s);
804 version = stream_getc (s);
805
806 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
807 {
808 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
809 __func__, zlookup->sock, marker, version);
810 return NULL;
811 }
812
813 command = stream_getw (s);
814
paul718e3742002-12-13 20:15:29 +0000815 raddr.s_addr = stream_get_ipv4 (s);
816 metric = stream_getl (s);
817 nexthop_num = stream_getc (s);
818
819 if (nexthop_num)
820 {
821 bnc = bnc_new ();
822 bnc->valid = 1;
823 bnc->metric = metric;
824 bnc->nexthop_num = nexthop_num;
825
826 for (i = 0; i < nexthop_num; i++)
827 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700828 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000829 nexthop->type = stream_getc (s);
830 switch (nexthop->type)
831 {
832 case ZEBRA_NEXTHOP_IPV4:
833 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
834 break;
835 case ZEBRA_NEXTHOP_IFINDEX:
836 case ZEBRA_NEXTHOP_IFNAME:
837 nexthop->ifindex = stream_getl (s);
838 break;
hassofa2b17e2004-03-04 17:45:00 +0000839 default:
840 /* do nothing */
841 break;
paul718e3742002-12-13 20:15:29 +0000842 }
843 bnc_nexthop_add (bnc, nexthop);
844 }
845 }
846 else
847 return NULL;
848
849 return bnc;
850}
851
852struct bgp_nexthop_cache *
853zlookup_query (struct in_addr addr)
854{
855 int ret;
856 struct stream *s;
857
858 /* Check socket. */
859 if (zlookup->sock < 0)
860 return NULL;
861
862 s = zlookup->obuf;
863 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000864 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000865 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000866
867 stream_putw_at (s, 0, stream_get_endp (s));
868
869 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000870 if (ret < 0)
871 {
872 zlog_err ("can't write to zlookup->sock");
873 close (zlookup->sock);
874 zlookup->sock = -1;
875 return NULL;
876 }
877 if (ret == 0)
878 {
879 zlog_err ("zlookup->sock connection closed");
880 close (zlookup->sock);
881 zlookup->sock = -1;
882 return NULL;
883 }
884
885 return zlookup_read ();
886}
887
888#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000889static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800890zlookup_read_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000891{
892 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000893 uint16_t length;
894 u_char version, marker;
895 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000896 int nbytes;
897 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000898 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000899 int i;
900 u_char nexthop_num;
901 struct nexthop *nexthop;
902 struct bgp_nexthop_cache *bnc;
903
904 s = zlookup->ibuf;
905 stream_reset (s);
906
907 nbytes = stream_read (s, zlookup->sock, 2);
908 length = stream_getw (s);
909
910 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000911 marker = stream_getc (s);
912 version = stream_getc (s);
913
914 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
915 {
916 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
917 __func__, zlookup->sock, marker, version);
918 return NULL;
919 }
920
921 command = stream_getw (s);
922
paul718e3742002-12-13 20:15:29 +0000923 stream_get (&raddr, s, 16);
924
925 metric = stream_getl (s);
926 nexthop_num = stream_getc (s);
927
928 if (nexthop_num)
929 {
930 bnc = bnc_new ();
931 bnc->valid = 1;
932 bnc->metric = metric;
933 bnc->nexthop_num = nexthop_num;
934
935 for (i = 0; i < nexthop_num; i++)
936 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700937 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000938 nexthop->type = stream_getc (s);
939 switch (nexthop->type)
940 {
941 case ZEBRA_NEXTHOP_IPV6:
942 stream_get (&nexthop->gate.ipv6, s, 16);
943 break;
944 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
945 case ZEBRA_NEXTHOP_IPV6_IFNAME:
946 stream_get (&nexthop->gate.ipv6, s, 16);
947 nexthop->ifindex = stream_getl (s);
948 break;
949 case ZEBRA_NEXTHOP_IFINDEX:
950 case ZEBRA_NEXTHOP_IFNAME:
951 nexthop->ifindex = stream_getl (s);
952 break;
hassofa2b17e2004-03-04 17:45:00 +0000953 default:
954 /* do nothing */
955 break;
paul718e3742002-12-13 20:15:29 +0000956 }
957 bnc_nexthop_add (bnc, nexthop);
958 }
959 }
960 else
961 return NULL;
962
963 return bnc;
964}
965
966struct bgp_nexthop_cache *
967zlookup_query_ipv6 (struct in6_addr *addr)
968{
969 int ret;
970 struct stream *s;
971
972 /* Check socket. */
973 if (zlookup->sock < 0)
974 return NULL;
975
976 s = zlookup->obuf;
977 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000978 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000979 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000980 stream_putw_at (s, 0, stream_get_endp (s));
981
982 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000983 if (ret < 0)
984 {
985 zlog_err ("can't write to zlookup->sock");
986 close (zlookup->sock);
987 zlookup->sock = -1;
988 return NULL;
989 }
990 if (ret == 0)
991 {
992 zlog_err ("zlookup->sock connection closed");
993 close (zlookup->sock);
994 zlookup->sock = -1;
995 return NULL;
996 }
997
998 return zlookup_read_ipv6 ();
999}
1000#endif /* HAVE_IPV6 */
1001
paul94f2b392005-06-28 12:44:16 +00001002static int
1003bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
1004 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +00001005{
1006 struct stream *s;
1007 int ret;
pauld3092e72006-01-17 17:33:46 +00001008 u_int16_t length, command;
1009 u_char version, marker;
paul718e3742002-12-13 20:15:29 +00001010 int nbytes;
1011 struct in_addr addr;
1012 struct in_addr nexthop;
1013 u_int32_t metric = 0;
1014 u_char nexthop_num;
1015 u_char nexthop_type;
1016
1017 /* If lookup connection is not available return valid. */
1018 if (zlookup->sock < 0)
1019 {
1020 if (igpmetric)
1021 *igpmetric = 0;
1022 return 1;
1023 }
1024
1025 /* Send query to the lookup connection */
1026 s = zlookup->obuf;
1027 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +00001028 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
1029
paul718e3742002-12-13 20:15:29 +00001030 stream_putc (s, p->prefixlen);
1031 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +00001032
1033 stream_putw_at (s, 0, stream_get_endp (s));
1034
paul718e3742002-12-13 20:15:29 +00001035 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +00001036 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +00001037
1038 if (ret < 0)
1039 {
1040 zlog_err ("can't write to zlookup->sock");
1041 close (zlookup->sock);
1042 zlookup->sock = -1;
1043 return 1;
1044 }
1045 if (ret == 0)
1046 {
1047 zlog_err ("zlookup->sock connection closed");
1048 close (zlookup->sock);
1049 zlookup->sock = -1;
1050 return 1;
1051 }
1052
1053 /* Get result. */
1054 stream_reset (s);
1055
1056 /* Fetch length. */
1057 nbytes = stream_read (s, zlookup->sock, 2);
1058 length = stream_getw (s);
1059
1060 /* Fetch whole data. */
1061 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +00001062 marker = stream_getc (s);
1063 version = stream_getc (s);
1064
1065 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
1066 {
1067 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1068 __func__, zlookup->sock, marker, version);
1069 return 0;
1070 }
1071
1072 command = stream_getw (s);
1073
paul718e3742002-12-13 20:15:29 +00001074 addr.s_addr = stream_get_ipv4 (s);
1075 metric = stream_getl (s);
1076 nexthop_num = stream_getc (s);
1077
1078 /* Set IGP metric value. */
1079 if (igpmetric)
1080 *igpmetric = metric;
1081
1082 /* If there is nexthop then this is active route. */
1083 if (nexthop_num)
1084 {
1085 nexthop.s_addr = 0;
1086 nexthop_type = stream_getc (s);
1087 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
1088 {
1089 nexthop.s_addr = stream_get_ipv4 (s);
1090 if (igpnexthop)
1091 *igpnexthop = nexthop;
1092 }
1093 else
1094 *igpnexthop = nexthop;
1095
1096 return 1;
1097 }
1098 else
1099 return 0;
1100}
1101
1102/* Scan all configured BGP route then check the route exists in IGP or
1103 not. */
paul94f2b392005-06-28 12:44:16 +00001104static int
paul718e3742002-12-13 20:15:29 +00001105bgp_import (struct thread *t)
1106{
1107 struct bgp *bgp;
1108 struct bgp_node *rn;
1109 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001110 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001111 int valid;
1112 u_int32_t metric;
1113 struct in_addr nexthop;
1114 afi_t afi;
1115 safi_t safi;
1116
1117 bgp_import_thread =
1118 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1119
hassof4184462005-02-01 20:13:16 +00001120 if (BGP_DEBUG (events, EVENTS))
1121 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001122
paul1eb8ef22005-04-07 07:30:20 +00001123 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001124 {
1125 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1126 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1127 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1128 rn = bgp_route_next (rn))
1129 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001130 {
paul6cbbc3c2003-04-28 17:11:02 +00001131 if (bgp_static->backdoor)
1132 continue;
paul718e3742002-12-13 20:15:29 +00001133
paul6cbbc3c2003-04-28 17:11:02 +00001134 valid = bgp_static->valid;
1135 metric = bgp_static->igpmetric;
1136 nexthop = bgp_static->igpnexthop;
1137
1138 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1139 && afi == AFI_IP && safi == SAFI_UNICAST)
1140 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1141 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001142 else
paul6cbbc3c2003-04-28 17:11:02 +00001143 {
1144 bgp_static->valid = 1;
1145 bgp_static->igpmetric = 0;
1146 bgp_static->igpnexthop.s_addr = 0;
1147 }
1148
1149 if (bgp_static->valid != valid)
1150 {
1151 if (bgp_static->valid)
1152 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1153 else
1154 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1155 }
1156 else if (bgp_static->valid)
1157 {
1158 if (bgp_static->igpmetric != metric
1159 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1160 || bgp_static->rmap.name)
1161 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1162 }
paul718e3742002-12-13 20:15:29 +00001163 }
paul6cbbc3c2003-04-28 17:11:02 +00001164 }
paul718e3742002-12-13 20:15:29 +00001165 return 0;
1166}
1167
1168/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001169static int
paul718e3742002-12-13 20:15:29 +00001170zlookup_connect (struct thread *t)
1171{
1172 struct zclient *zlookup;
1173
1174 zlookup = THREAD_ARG (t);
1175 zlookup->t_connect = NULL;
1176
1177 if (zlookup->sock != -1)
1178 return 0;
1179
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001180 if (zclient_socket_connect (zlookup) < 0)
paul718e3742002-12-13 20:15:29 +00001181 return -1;
1182
paul718e3742002-12-13 20:15:29 +00001183 return 0;
1184}
1185
1186/* Check specified multiaccess next-hop. */
1187int
1188bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1189{
1190 struct bgp_node *rn1;
1191 struct bgp_node *rn2;
1192 struct prefix p1;
1193 struct prefix p2;
1194 struct in_addr addr;
1195 int ret;
1196
1197 ret = inet_aton (peer, &addr);
1198 if (! ret)
1199 return 0;
1200
1201 memset (&p1, 0, sizeof (struct prefix));
1202 p1.family = AF_INET;
1203 p1.prefixlen = IPV4_MAX_BITLEN;
1204 p1.u.prefix4 = nexthop;
1205 memset (&p2, 0, sizeof (struct prefix));
1206 p2.family = AF_INET;
1207 p2.prefixlen = IPV4_MAX_BITLEN;
1208 p2.u.prefix4 = addr;
1209
1210 /* If bgp scan is not enabled, return invalid. */
1211 if (zlookup->sock < 0)
1212 return 0;
1213
paul59320202004-11-09 01:54:03 +00001214 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001215 if (! rn1)
1216 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001217 bgp_unlock_node (rn1);
paul718e3742002-12-13 20:15:29 +00001218
paul59320202004-11-09 01:54:03 +00001219 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001220 if (! rn2)
1221 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001222 bgp_unlock_node (rn2);
paul718e3742002-12-13 20:15:29 +00001223
Chris Caputo6c88b442010-07-27 16:28:55 +00001224 /* This is safe, even with above unlocks, since we are just
1225 comparing pointers to the objects, not the objects themselves. */
paul718e3742002-12-13 20:15:29 +00001226 if (rn1 == rn2)
1227 return 1;
1228
1229 return 0;
1230}
1231
1232DEFUN (bgp_scan_time,
1233 bgp_scan_time_cmd,
1234 "bgp scan-time <5-60>",
1235 "BGP specific commands\n"
1236 "Configure background scanner interval\n"
1237 "Scanner interval (seconds)\n")
1238{
1239 bgp_scan_interval = atoi (argv[0]);
1240
1241 if (bgp_scan_thread)
1242 {
1243 thread_cancel (bgp_scan_thread);
1244 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001245 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001246 }
1247
1248 return CMD_SUCCESS;
1249}
1250
1251DEFUN (no_bgp_scan_time,
1252 no_bgp_scan_time_cmd,
1253 "no bgp scan-time",
1254 NO_STR
1255 "BGP specific commands\n"
1256 "Configure background scanner interval\n")
1257{
1258 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1259
1260 if (bgp_scan_thread)
1261 {
1262 thread_cancel (bgp_scan_thread);
1263 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001264 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001265 }
1266
1267 return CMD_SUCCESS;
1268}
1269
1270ALIAS (no_bgp_scan_time,
1271 no_bgp_scan_time_val_cmd,
1272 "no bgp scan-time <5-60>",
1273 NO_STR
1274 "BGP specific commands\n"
1275 "Configure background scanner interval\n"
1276 "Scanner interval (seconds)\n")
1277
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001278static int
1279show_ip_bgp_scan_tables (struct vty *vty, const char detail)
paul718e3742002-12-13 20:15:29 +00001280{
1281 struct bgp_node *rn;
1282 struct bgp_nexthop_cache *bnc;
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001283 char buf[INET6_ADDRSTRLEN];
1284 u_char i;
paul718e3742002-12-13 20:15:29 +00001285
1286 if (bgp_scan_thread)
1287 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1288 else
1289 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1290 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1291
1292 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001293 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001294 if ((bnc = rn->info) != NULL)
1295 {
1296 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001297 {
paul718e3742002-12-13 20:15:29 +00001298 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001299 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1300 if (detail)
1301 for (i = 0; i < bnc->nexthop_num; i++)
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001302 switch (bnc->nexthop[i].type)
1303 {
1304 case NEXTHOP_TYPE_IPV4:
1305 vty_out (vty, " gate %s%s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1306 break;
1307 case NEXTHOP_TYPE_IFINDEX:
1308 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1309 break;
1310 default:
1311 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1312 }
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001313 }
paul718e3742002-12-13 20:15:29 +00001314 else
1315 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001316 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001317 }
1318
1319#ifdef HAVE_IPV6
1320 {
paul59320202004-11-09 01:54:03 +00001321 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1322 rn;
1323 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001324 if ((bnc = rn->info) != NULL)
1325 {
1326 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001327 {
paul718e3742002-12-13 20:15:29 +00001328 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001329 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001330 bnc->metric, VTY_NEWLINE);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001331 if (detail)
1332 for (i = 0; i < bnc->nexthop_num; i++)
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001333 switch (bnc->nexthop[i].type)
1334 {
1335 case NEXTHOP_TYPE_IPV6:
1336 vty_out (vty, " gate %s%s", inet_ntop (AF_INET6, &bnc->nexthop[i].gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1337 break;
1338 case NEXTHOP_TYPE_IFINDEX:
1339 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1340 break;
1341 default:
1342 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1343 }
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001344 }
paul718e3742002-12-13 20:15:29 +00001345 else
1346 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001347 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001348 VTY_NEWLINE);
1349 }
1350 }
1351#endif /* HAVE_IPV6 */
1352
1353 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001354 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1355 rn;
1356 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001357 if (rn->info != NULL)
1358 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1359 VTY_NEWLINE);
1360
1361#ifdef HAVE_IPV6
1362 {
paul59320202004-11-09 01:54:03 +00001363 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1364 rn;
1365 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001366 if (rn->info != NULL)
1367 vty_out (vty, " %s/%d%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001368 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001369 rn->p.prefixlen,
1370 VTY_NEWLINE);
1371 }
1372#endif /* HAVE_IPV6 */
1373
1374 return CMD_SUCCESS;
1375}
1376
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001377DEFUN (show_ip_bgp_scan,
1378 show_ip_bgp_scan_cmd,
1379 "show ip bgp scan",
1380 SHOW_STR
1381 IP_STR
1382 BGP_STR
1383 "BGP scan status\n")
1384{
1385 return show_ip_bgp_scan_tables (vty, 0);
1386}
1387
1388DEFUN (show_ip_bgp_scan_detail,
1389 show_ip_bgp_scan_detail_cmd,
1390 "show ip bgp scan detail",
1391 SHOW_STR
1392 IP_STR
1393 BGP_STR
1394 "BGP scan status\n"
1395 "More detailed output\n")
1396{
1397 return show_ip_bgp_scan_tables (vty, 1);
1398}
1399
paul718e3742002-12-13 20:15:29 +00001400int
1401bgp_config_write_scan_time (struct vty *vty)
1402{
1403 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1404 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1405 return CMD_SUCCESS;
1406}
1407
1408void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -08001409bgp_scan_init (void)
paul718e3742002-12-13 20:15:29 +00001410{
1411 zlookup = zclient_new ();
1412 zlookup->sock = -1;
paul718e3742002-12-13 20:15:29 +00001413 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1414
1415 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1416 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1417
Paul Jakma64e580a2006-02-21 01:09:01 +00001418 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1419 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001420 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001421
Paul Jakma64e580a2006-02-21 01:09:01 +00001422 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001423
1424#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001425 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1426 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001427 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001428 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001429#endif /* HAVE_IPV6 */
1430
1431 /* Make BGP scan thread. */
paul59320202004-11-09 01:54:03 +00001432 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1433 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001434 /* Make BGP import there. */
1435 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001436
1437 install_element (BGP_NODE, &bgp_scan_time_cmd);
1438 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1439 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1440 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001441 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01001442 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
paul718e3742002-12-13 20:15:29 +00001443 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001444 install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
paul718e3742002-12-13 20:15:29 +00001445}
Chris Caputo228da422009-07-18 05:44:03 +00001446
1447void
1448bgp_scan_finish (void)
1449{
Chris Caputo6c88b442010-07-27 16:28:55 +00001450 /* Only the current one needs to be reset. */
1451 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]);
1452
Chris Caputo228da422009-07-18 05:44:03 +00001453 bgp_table_unlock (cache1_table[AFI_IP]);
1454 cache1_table[AFI_IP] = NULL;
1455
1456 bgp_table_unlock (cache2_table[AFI_IP]);
1457 cache2_table[AFI_IP] = NULL;
1458
1459 bgp_table_unlock (bgp_connected_table[AFI_IP]);
1460 bgp_connected_table[AFI_IP] = NULL;
1461
1462#ifdef HAVE_IPV6
Chris Caputo6c88b442010-07-27 16:28:55 +00001463 /* Only the current one needs to be reset. */
1464 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]);
1465
Chris Caputo228da422009-07-18 05:44:03 +00001466 bgp_table_unlock (cache1_table[AFI_IP6]);
1467 cache1_table[AFI_IP6] = NULL;
1468
1469 bgp_table_unlock (cache2_table[AFI_IP6]);
1470 cache2_table[AFI_IP6] = NULL;
1471
1472 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
1473 bgp_connected_table[AFI_IP6] = NULL;
1474#endif /* HAVE_IPV6 */
1475}