blob: 723057b68cf2408552c938c9e3ebdc18bd1e422a [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
456 if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
Denis Ovsienko8e80bdf2011-08-05 18:52:52 +0400457 valid = bgp_nexthop_onlink (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000458 else
paul59320202004-11-09 01:54:03 +0000459 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000460 &changed, &metricchanged);
461
462 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
463
464 if (changed)
465 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
466 else
467 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
468
469 if (valid != current)
470 {
471 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
472 {
473 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000474 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000475 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000476 }
477 else
478 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000479 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000480 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000481 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000482 }
483 }
484
paul59320202004-11-09 01:54:03 +0000485 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000486 BGP_CONFIG_DAMPENING)
Paul Jakmafb982c22007-05-04 20:15:47 +0000487 && bi->extra && bi->extra->damp_info )
paul59320202004-11-09 01:54:03 +0000488 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000489 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000490 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000491 }
492 }
paul59320202004-11-09 01:54:03 +0000493 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000494 }
495
496 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000497 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
498 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000499 else
paul59320202004-11-09 01:54:03 +0000500 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000501
502 if (BGP_DEBUG (events, EVENTS))
503 {
504 if (afi == AFI_IP)
505 zlog_debug ("scanning IPv4 Unicast routing tables");
506 else if (afi == AFI_IP6)
507 zlog_debug ("scanning IPv6 Unicast routing tables");
508 }
paul718e3742002-12-13 20:15:29 +0000509}
510
paul718e3742002-12-13 20:15:29 +0000511/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000512static int
paul59320202004-11-09 01:54:03 +0000513bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000514{
515 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +0000516 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000517
hassof4184462005-02-01 20:13:16 +0000518 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000519 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000520
paul59320202004-11-09 01:54:03 +0000521 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000522
523#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000524 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000525#endif /* HAVE_IPV6 */
526
527 return 0;
528}
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000529
530/* BGP own address structure */
531struct bgp_addr
532{
533 struct in_addr addr;
534 int refcnt;
535};
536
537static struct hash *bgp_address_hash;
538
539static void *
540bgp_address_hash_alloc (void *p)
541{
542 struct in_addr *val = p;
543 struct bgp_addr *addr;
544
545 addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr));
546 addr->refcnt = 0;
547 addr->addr.s_addr = val->s_addr;
548
549 return addr;
550}
551
552static unsigned int
553bgp_address_hash_key_make (void *p)
554{
555 const struct bgp_addr *addr = p;
556
557 return jhash_1word(addr->addr.s_addr, 0);
558}
559
560static int
561bgp_address_hash_cmp (const void *p1, const void *p2)
562{
563 const struct bgp_addr *addr1 = p1;
564 const struct bgp_addr *addr2 = p2;
565
566 return addr1->addr.s_addr == addr2->addr.s_addr;
567}
568
569void
570bgp_address_init (void)
571{
572 bgp_address_hash = hash_create (bgp_address_hash_key_make,
573 bgp_address_hash_cmp);
574}
575
576static void
577bgp_address_add (struct prefix *p)
578{
579 struct bgp_addr tmp;
580 struct bgp_addr *addr;
581
582 tmp.addr = p->u.prefix4;
583
584 addr = hash_get (bgp_address_hash, &tmp, bgp_address_hash_alloc);
585 addr->refcnt++;
586}
587
588static void
589bgp_address_del (struct prefix *p)
590{
591 struct bgp_addr tmp;
592 struct bgp_addr *addr;
593
594 tmp.addr = p->u.prefix4;
595
596 addr = hash_lookup (bgp_address_hash, &tmp);
597 addr->refcnt--;
598
599 if (addr->refcnt == 0)
600 {
601 hash_release (bgp_address_hash, addr);
602 XFREE (MTYPE_BGP_ADDR, addr);
603 }
604}
605
paul718e3742002-12-13 20:15:29 +0000606
paul59320202004-11-09 01:54:03 +0000607struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000608{
609 unsigned int refcnt;
610};
611
612void
613bgp_connected_add (struct connected *ifc)
614{
615 struct prefix p;
616 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000617 struct interface *ifp;
618 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000619 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000620
621 ifp = ifc->ifp;
622
623 if (! ifp)
624 return;
625
626 if (if_is_loopback (ifp))
627 return;
628
629 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000630
631 if (addr->family == AF_INET)
632 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000633 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000634 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
635
636 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
637 return;
638
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000639 bgp_address_add (addr);
640
paul59320202004-11-09 01:54:03 +0000641 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000642 if (rn->info)
643 {
644 bc = rn->info;
645 bc->refcnt++;
646 }
647 else
648 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000649 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000650 bc->refcnt = 1;
651 rn->info = bc;
652 }
653 }
654#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000655 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000656 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000657 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000658 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
659
660 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
661 return;
662
663 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
664 return;
665
paul59320202004-11-09 01:54:03 +0000666 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000667 if (rn->info)
668 {
669 bc = rn->info;
670 bc->refcnt++;
671 }
672 else
673 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000674 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000675 bc->refcnt = 1;
676 rn->info = bc;
677 }
678 }
679#endif /* HAVE_IPV6 */
680}
681
682void
683bgp_connected_delete (struct connected *ifc)
684{
685 struct prefix p;
686 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000687 struct interface *ifp;
688 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000689 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000690
691 ifp = ifc->ifp;
692
693 if (if_is_loopback (ifp))
694 return;
695
696 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000697
698 if (addr->family == AF_INET)
699 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000700 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000701 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
702
703 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
704 return;
705
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000706 bgp_address_del (addr);
707
paul59320202004-11-09 01:54:03 +0000708 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000709 if (! rn)
710 return;
711
712 bc = rn->info;
713 bc->refcnt--;
714 if (bc->refcnt == 0)
715 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000716 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000717 rn->info = NULL;
718 }
719 bgp_unlock_node (rn);
720 bgp_unlock_node (rn);
721 }
722#ifdef HAVE_IPV6
723 else if (addr->family == AF_INET6)
724 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000725 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000726 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
727
728 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
729 return;
730
731 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
732 return;
733
paul59320202004-11-09 01:54:03 +0000734 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000735 if (! rn)
736 return;
737
738 bc = rn->info;
739 bc->refcnt--;
740 if (bc->refcnt == 0)
741 {
Chris Caputo6c88b442010-07-27 16:28:55 +0000742 XFREE (MTYPE_BGP_CONN, bc);
paul718e3742002-12-13 20:15:29 +0000743 rn->info = NULL;
744 }
745 bgp_unlock_node (rn);
746 bgp_unlock_node (rn);
747 }
748#endif /* HAVE_IPV6 */
749}
750
751int
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000752bgp_nexthop_self (struct attr *attr)
paul718e3742002-12-13 20:15:29 +0000753{
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000754 struct bgp_addr tmp, *addr;
paul718e3742002-12-13 20:15:29 +0000755
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000756 tmp.addr = attr->nexthop;
paul718e3742002-12-13 20:15:29 +0000757
Jorge Boncompte [DTI2]10f9bf32012-05-07 16:52:52 +0000758 addr = hash_lookup (bgp_address_hash, &tmp);
759 if (addr)
760 return 1;
761
paul718e3742002-12-13 20:15:29 +0000762 return 0;
763}
764
paul94f2b392005-06-28 12:44:16 +0000765static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800766zlookup_read (void)
paul718e3742002-12-13 20:15:29 +0000767{
768 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000769 uint16_t length;
770 u_char marker;
771 u_char version;
772 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000773 int nbytes;
774 struct in_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000775 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000776 int i;
777 u_char nexthop_num;
778 struct nexthop *nexthop;
779 struct bgp_nexthop_cache *bnc;
780
781 s = zlookup->ibuf;
782 stream_reset (s);
783
784 nbytes = stream_read (s, zlookup->sock, 2);
785 length = stream_getw (s);
786
787 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000788 marker = stream_getc (s);
789 version = stream_getc (s);
790
791 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
792 {
793 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
794 __func__, zlookup->sock, marker, version);
795 return NULL;
796 }
797
798 command = stream_getw (s);
799
paul718e3742002-12-13 20:15:29 +0000800 raddr.s_addr = stream_get_ipv4 (s);
801 metric = stream_getl (s);
802 nexthop_num = stream_getc (s);
803
804 if (nexthop_num)
805 {
806 bnc = bnc_new ();
807 bnc->valid = 1;
808 bnc->metric = metric;
809 bnc->nexthop_num = nexthop_num;
810
811 for (i = 0; i < nexthop_num; i++)
812 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700813 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000814 nexthop->type = stream_getc (s);
815 switch (nexthop->type)
816 {
817 case ZEBRA_NEXTHOP_IPV4:
818 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
819 break;
820 case ZEBRA_NEXTHOP_IFINDEX:
821 case ZEBRA_NEXTHOP_IFNAME:
822 nexthop->ifindex = stream_getl (s);
823 break;
hassofa2b17e2004-03-04 17:45:00 +0000824 default:
825 /* do nothing */
826 break;
paul718e3742002-12-13 20:15:29 +0000827 }
828 bnc_nexthop_add (bnc, nexthop);
829 }
830 }
831 else
832 return NULL;
833
834 return bnc;
835}
836
837struct bgp_nexthop_cache *
838zlookup_query (struct in_addr addr)
839{
840 int ret;
841 struct stream *s;
842
843 /* Check socket. */
844 if (zlookup->sock < 0)
845 return NULL;
846
847 s = zlookup->obuf;
848 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000849 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000850 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000851
852 stream_putw_at (s, 0, stream_get_endp (s));
853
854 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000855 if (ret < 0)
856 {
857 zlog_err ("can't write to zlookup->sock");
858 close (zlookup->sock);
859 zlookup->sock = -1;
860 return NULL;
861 }
862 if (ret == 0)
863 {
864 zlog_err ("zlookup->sock connection closed");
865 close (zlookup->sock);
866 zlookup->sock = -1;
867 return NULL;
868 }
869
870 return zlookup_read ();
871}
872
873#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000874static struct bgp_nexthop_cache *
Stephen Hemminger66e5cd82009-02-09 10:14:16 -0800875zlookup_read_ipv6 (void)
paul718e3742002-12-13 20:15:29 +0000876{
877 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000878 uint16_t length;
879 u_char version, marker;
880 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000881 int nbytes;
882 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000883 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000884 int i;
885 u_char nexthop_num;
886 struct nexthop *nexthop;
887 struct bgp_nexthop_cache *bnc;
888
889 s = zlookup->ibuf;
890 stream_reset (s);
891
892 nbytes = stream_read (s, zlookup->sock, 2);
893 length = stream_getw (s);
894
895 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000896 marker = stream_getc (s);
897 version = stream_getc (s);
898
899 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
900 {
901 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
902 __func__, zlookup->sock, marker, version);
903 return NULL;
904 }
905
906 command = stream_getw (s);
907
paul718e3742002-12-13 20:15:29 +0000908 stream_get (&raddr, s, 16);
909
910 metric = stream_getl (s);
911 nexthop_num = stream_getc (s);
912
913 if (nexthop_num)
914 {
915 bnc = bnc_new ();
916 bnc->valid = 1;
917 bnc->metric = metric;
918 bnc->nexthop_num = nexthop_num;
919
920 for (i = 0; i < nexthop_num; i++)
921 {
Stephen Hemminger393deb92008-08-18 14:13:29 -0700922 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
paul718e3742002-12-13 20:15:29 +0000923 nexthop->type = stream_getc (s);
924 switch (nexthop->type)
925 {
926 case ZEBRA_NEXTHOP_IPV6:
927 stream_get (&nexthop->gate.ipv6, s, 16);
928 break;
929 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
930 case ZEBRA_NEXTHOP_IPV6_IFNAME:
931 stream_get (&nexthop->gate.ipv6, s, 16);
932 nexthop->ifindex = stream_getl (s);
933 break;
934 case ZEBRA_NEXTHOP_IFINDEX:
935 case ZEBRA_NEXTHOP_IFNAME:
936 nexthop->ifindex = stream_getl (s);
937 break;
hassofa2b17e2004-03-04 17:45:00 +0000938 default:
939 /* do nothing */
940 break;
paul718e3742002-12-13 20:15:29 +0000941 }
942 bnc_nexthop_add (bnc, nexthop);
943 }
944 }
945 else
946 return NULL;
947
948 return bnc;
949}
950
951struct bgp_nexthop_cache *
952zlookup_query_ipv6 (struct in6_addr *addr)
953{
954 int ret;
955 struct stream *s;
956
957 /* Check socket. */
958 if (zlookup->sock < 0)
959 return NULL;
960
961 s = zlookup->obuf;
962 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000963 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000964 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000965 stream_putw_at (s, 0, stream_get_endp (s));
966
967 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000968 if (ret < 0)
969 {
970 zlog_err ("can't write to zlookup->sock");
971 close (zlookup->sock);
972 zlookup->sock = -1;
973 return NULL;
974 }
975 if (ret == 0)
976 {
977 zlog_err ("zlookup->sock connection closed");
978 close (zlookup->sock);
979 zlookup->sock = -1;
980 return NULL;
981 }
982
983 return zlookup_read_ipv6 ();
984}
985#endif /* HAVE_IPV6 */
986
paul94f2b392005-06-28 12:44:16 +0000987static int
988bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
989 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000990{
991 struct stream *s;
992 int ret;
pauld3092e72006-01-17 17:33:46 +0000993 u_int16_t length, command;
994 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000995 int nbytes;
996 struct in_addr addr;
997 struct in_addr nexthop;
998 u_int32_t metric = 0;
999 u_char nexthop_num;
1000 u_char nexthop_type;
1001
1002 /* If lookup connection is not available return valid. */
1003 if (zlookup->sock < 0)
1004 {
1005 if (igpmetric)
1006 *igpmetric = 0;
1007 return 1;
1008 }
1009
1010 /* Send query to the lookup connection */
1011 s = zlookup->obuf;
1012 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +00001013 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
1014
paul718e3742002-12-13 20:15:29 +00001015 stream_putc (s, p->prefixlen);
1016 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +00001017
1018 stream_putw_at (s, 0, stream_get_endp (s));
1019
paul718e3742002-12-13 20:15:29 +00001020 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +00001021 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +00001022
1023 if (ret < 0)
1024 {
1025 zlog_err ("can't write to zlookup->sock");
1026 close (zlookup->sock);
1027 zlookup->sock = -1;
1028 return 1;
1029 }
1030 if (ret == 0)
1031 {
1032 zlog_err ("zlookup->sock connection closed");
1033 close (zlookup->sock);
1034 zlookup->sock = -1;
1035 return 1;
1036 }
1037
1038 /* Get result. */
1039 stream_reset (s);
1040
1041 /* Fetch length. */
1042 nbytes = stream_read (s, zlookup->sock, 2);
1043 length = stream_getw (s);
1044
1045 /* Fetch whole data. */
1046 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +00001047 marker = stream_getc (s);
1048 version = stream_getc (s);
1049
1050 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
1051 {
1052 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1053 __func__, zlookup->sock, marker, version);
1054 return 0;
1055 }
1056
1057 command = stream_getw (s);
1058
paul718e3742002-12-13 20:15:29 +00001059 addr.s_addr = stream_get_ipv4 (s);
1060 metric = stream_getl (s);
1061 nexthop_num = stream_getc (s);
1062
1063 /* Set IGP metric value. */
1064 if (igpmetric)
1065 *igpmetric = metric;
1066
1067 /* If there is nexthop then this is active route. */
1068 if (nexthop_num)
1069 {
1070 nexthop.s_addr = 0;
1071 nexthop_type = stream_getc (s);
1072 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
1073 {
1074 nexthop.s_addr = stream_get_ipv4 (s);
1075 if (igpnexthop)
1076 *igpnexthop = nexthop;
1077 }
1078 else
1079 *igpnexthop = nexthop;
1080
1081 return 1;
1082 }
1083 else
1084 return 0;
1085}
1086
1087/* Scan all configured BGP route then check the route exists in IGP or
1088 not. */
paul94f2b392005-06-28 12:44:16 +00001089static int
paul718e3742002-12-13 20:15:29 +00001090bgp_import (struct thread *t)
1091{
1092 struct bgp *bgp;
1093 struct bgp_node *rn;
1094 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001095 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001096 int valid;
1097 u_int32_t metric;
1098 struct in_addr nexthop;
1099 afi_t afi;
1100 safi_t safi;
1101
1102 bgp_import_thread =
1103 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1104
hassof4184462005-02-01 20:13:16 +00001105 if (BGP_DEBUG (events, EVENTS))
1106 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001107
paul1eb8ef22005-04-07 07:30:20 +00001108 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001109 {
1110 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1111 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1112 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1113 rn = bgp_route_next (rn))
1114 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001115 {
paul6cbbc3c2003-04-28 17:11:02 +00001116 if (bgp_static->backdoor)
1117 continue;
paul718e3742002-12-13 20:15:29 +00001118
paul6cbbc3c2003-04-28 17:11:02 +00001119 valid = bgp_static->valid;
1120 metric = bgp_static->igpmetric;
1121 nexthop = bgp_static->igpnexthop;
1122
1123 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1124 && afi == AFI_IP && safi == SAFI_UNICAST)
1125 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1126 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001127 else
paul6cbbc3c2003-04-28 17:11:02 +00001128 {
1129 bgp_static->valid = 1;
1130 bgp_static->igpmetric = 0;
1131 bgp_static->igpnexthop.s_addr = 0;
1132 }
1133
1134 if (bgp_static->valid != valid)
1135 {
1136 if (bgp_static->valid)
1137 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1138 else
1139 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1140 }
1141 else if (bgp_static->valid)
1142 {
1143 if (bgp_static->igpmetric != metric
1144 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1145 || bgp_static->rmap.name)
1146 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1147 }
paul718e3742002-12-13 20:15:29 +00001148 }
paul6cbbc3c2003-04-28 17:11:02 +00001149 }
paul718e3742002-12-13 20:15:29 +00001150 return 0;
1151}
1152
1153/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001154static int
paul718e3742002-12-13 20:15:29 +00001155zlookup_connect (struct thread *t)
1156{
1157 struct zclient *zlookup;
1158
1159 zlookup = THREAD_ARG (t);
1160 zlookup->t_connect = NULL;
1161
1162 if (zlookup->sock != -1)
1163 return 0;
1164
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001165 if (zclient_socket_connect (zlookup) < 0)
paul718e3742002-12-13 20:15:29 +00001166 return -1;
1167
paul718e3742002-12-13 20:15:29 +00001168 return 0;
1169}
1170
1171/* Check specified multiaccess next-hop. */
1172int
1173bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1174{
1175 struct bgp_node *rn1;
1176 struct bgp_node *rn2;
1177 struct prefix p1;
1178 struct prefix p2;
1179 struct in_addr addr;
1180 int ret;
1181
1182 ret = inet_aton (peer, &addr);
1183 if (! ret)
1184 return 0;
1185
1186 memset (&p1, 0, sizeof (struct prefix));
1187 p1.family = AF_INET;
1188 p1.prefixlen = IPV4_MAX_BITLEN;
1189 p1.u.prefix4 = nexthop;
1190 memset (&p2, 0, sizeof (struct prefix));
1191 p2.family = AF_INET;
1192 p2.prefixlen = IPV4_MAX_BITLEN;
1193 p2.u.prefix4 = addr;
1194
1195 /* If bgp scan is not enabled, return invalid. */
1196 if (zlookup->sock < 0)
1197 return 0;
1198
paul59320202004-11-09 01:54:03 +00001199 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001200 if (! rn1)
1201 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001202 bgp_unlock_node (rn1);
paul718e3742002-12-13 20:15:29 +00001203
paul59320202004-11-09 01:54:03 +00001204 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001205 if (! rn2)
1206 return 0;
Chris Caputo6c88b442010-07-27 16:28:55 +00001207 bgp_unlock_node (rn2);
paul718e3742002-12-13 20:15:29 +00001208
Chris Caputo6c88b442010-07-27 16:28:55 +00001209 /* This is safe, even with above unlocks, since we are just
1210 comparing pointers to the objects, not the objects themselves. */
paul718e3742002-12-13 20:15:29 +00001211 if (rn1 == rn2)
1212 return 1;
1213
1214 return 0;
1215}
1216
1217DEFUN (bgp_scan_time,
1218 bgp_scan_time_cmd,
1219 "bgp scan-time <5-60>",
1220 "BGP specific commands\n"
1221 "Configure background scanner interval\n"
1222 "Scanner interval (seconds)\n")
1223{
1224 bgp_scan_interval = atoi (argv[0]);
1225
1226 if (bgp_scan_thread)
1227 {
1228 thread_cancel (bgp_scan_thread);
1229 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001230 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001231 }
1232
1233 return CMD_SUCCESS;
1234}
1235
1236DEFUN (no_bgp_scan_time,
1237 no_bgp_scan_time_cmd,
1238 "no bgp scan-time",
1239 NO_STR
1240 "BGP specific commands\n"
1241 "Configure background scanner interval\n")
1242{
1243 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1244
1245 if (bgp_scan_thread)
1246 {
1247 thread_cancel (bgp_scan_thread);
1248 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001249 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001250 }
1251
1252 return CMD_SUCCESS;
1253}
1254
1255ALIAS (no_bgp_scan_time,
1256 no_bgp_scan_time_val_cmd,
1257 "no bgp scan-time <5-60>",
1258 NO_STR
1259 "BGP specific commands\n"
1260 "Configure background scanner interval\n"
1261 "Scanner interval (seconds)\n")
1262
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001263static int
1264show_ip_bgp_scan_tables (struct vty *vty, const char detail)
paul718e3742002-12-13 20:15:29 +00001265{
1266 struct bgp_node *rn;
1267 struct bgp_nexthop_cache *bnc;
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001268 char buf[INET6_ADDRSTRLEN];
1269 u_char i;
paul718e3742002-12-13 20:15:29 +00001270
1271 if (bgp_scan_thread)
1272 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1273 else
1274 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1275 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1276
1277 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001278 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001279 if ((bnc = rn->info) != NULL)
1280 {
1281 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001282 {
paul718e3742002-12-13 20:15:29 +00001283 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001284 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1285 if (detail)
1286 for (i = 0; i < bnc->nexthop_num; i++)
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001287 switch (bnc->nexthop[i].type)
1288 {
1289 case NEXTHOP_TYPE_IPV4:
1290 vty_out (vty, " gate %s%s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1291 break;
1292 case NEXTHOP_TYPE_IFINDEX:
1293 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1294 break;
1295 default:
1296 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1297 }
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001298 }
paul718e3742002-12-13 20:15:29 +00001299 else
1300 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001301 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001302 }
1303
1304#ifdef HAVE_IPV6
1305 {
paul59320202004-11-09 01:54:03 +00001306 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1307 rn;
1308 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001309 if ((bnc = rn->info) != NULL)
1310 {
1311 if (bnc->valid)
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001312 {
paul718e3742002-12-13 20:15:29 +00001313 vty_out (vty, " %s valid [IGP metric %d]%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001314 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001315 bnc->metric, VTY_NEWLINE);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001316 if (detail)
1317 for (i = 0; i < bnc->nexthop_num; i++)
Denis Ovsienko0e8032d2011-08-09 14:42:58 +04001318 switch (bnc->nexthop[i].type)
1319 {
1320 case NEXTHOP_TYPE_IPV6:
1321 vty_out (vty, " gate %s%s", inet_ntop (AF_INET6, &bnc->nexthop[i].gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1322 break;
1323 case NEXTHOP_TYPE_IFINDEX:
1324 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1325 break;
1326 default:
1327 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1328 }
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001329 }
paul718e3742002-12-13 20:15:29 +00001330 else
1331 vty_out (vty, " %s invalid%s",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001332 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001333 VTY_NEWLINE);
1334 }
1335 }
1336#endif /* HAVE_IPV6 */
1337
1338 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001339 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1340 rn;
1341 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001342 if (rn->info != NULL)
1343 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1344 VTY_NEWLINE);
1345
1346#ifdef HAVE_IPV6
1347 {
paul59320202004-11-09 01:54:03 +00001348 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
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",
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001353 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
paul718e3742002-12-13 20:15:29 +00001354 rn->p.prefixlen,
1355 VTY_NEWLINE);
1356 }
1357#endif /* HAVE_IPV6 */
1358
1359 return CMD_SUCCESS;
1360}
1361
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001362DEFUN (show_ip_bgp_scan,
1363 show_ip_bgp_scan_cmd,
1364 "show ip bgp scan",
1365 SHOW_STR
1366 IP_STR
1367 BGP_STR
1368 "BGP scan status\n")
1369{
1370 return show_ip_bgp_scan_tables (vty, 0);
1371}
1372
1373DEFUN (show_ip_bgp_scan_detail,
1374 show_ip_bgp_scan_detail_cmd,
1375 "show ip bgp scan detail",
1376 SHOW_STR
1377 IP_STR
1378 BGP_STR
1379 "BGP scan status\n"
1380 "More detailed output\n")
1381{
1382 return show_ip_bgp_scan_tables (vty, 1);
1383}
1384
paul718e3742002-12-13 20:15:29 +00001385int
1386bgp_config_write_scan_time (struct vty *vty)
1387{
1388 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1389 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1390 return CMD_SUCCESS;
1391}
1392
1393void
Stephen Hemminger66e5cd82009-02-09 10:14:16 -08001394bgp_scan_init (void)
paul718e3742002-12-13 20:15:29 +00001395{
1396 zlookup = zclient_new ();
1397 zlookup->sock = -1;
paul718e3742002-12-13 20:15:29 +00001398 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1399
1400 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1401 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1402
Paul Jakma64e580a2006-02-21 01:09:01 +00001403 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1404 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001405 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001406
Paul Jakma64e580a2006-02-21 01:09:01 +00001407 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001408
1409#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001410 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1411 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001412 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001413 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001414#endif /* HAVE_IPV6 */
1415
1416 /* Make BGP scan thread. */
paul59320202004-11-09 01:54:03 +00001417 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1418 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001419 /* Make BGP import there. */
1420 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001421
1422 install_element (BGP_NODE, &bgp_scan_time_cmd);
1423 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1424 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1425 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001426 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
Paul Jakma62687ff2008-08-23 14:27:06 +01001427 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
paul718e3742002-12-13 20:15:29 +00001428 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
Denis Ovsienko318f0d82011-08-05 21:47:08 +04001429 install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
paul718e3742002-12-13 20:15:29 +00001430}
Chris Caputo228da422009-07-18 05:44:03 +00001431
1432void
1433bgp_scan_finish (void)
1434{
Chris Caputo6c88b442010-07-27 16:28:55 +00001435 /* Only the current one needs to be reset. */
1436 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]);
1437
Chris Caputo228da422009-07-18 05:44:03 +00001438 bgp_table_unlock (cache1_table[AFI_IP]);
1439 cache1_table[AFI_IP] = NULL;
1440
1441 bgp_table_unlock (cache2_table[AFI_IP]);
1442 cache2_table[AFI_IP] = NULL;
1443
1444 bgp_table_unlock (bgp_connected_table[AFI_IP]);
1445 bgp_connected_table[AFI_IP] = NULL;
1446
1447#ifdef HAVE_IPV6
Chris Caputo6c88b442010-07-27 16:28:55 +00001448 /* Only the current one needs to be reset. */
1449 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]);
1450
Chris Caputo228da422009-07-18 05:44:03 +00001451 bgp_table_unlock (cache1_table[AFI_IP6]);
1452 cache1_table[AFI_IP6] = NULL;
1453
1454 bgp_table_unlock (cache2_table[AFI_IP6]);
1455 cache2_table[AFI_IP6] = NULL;
1456
1457 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
1458 bgp_connected_table[AFI_IP6] = NULL;
1459#endif /* HAVE_IPV6 */
1460}