blob: 4dd5d946340e6cff3c64325b3cf2ab04943d2677 [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"
31
32#include "bgpd/bgpd.h"
33#include "bgpd/bgp_table.h"
34#include "bgpd/bgp_route.h"
35#include "bgpd/bgp_attr.h"
36#include "bgpd/bgp_nexthop.h"
37#include "bgpd/bgp_debug.h"
38#include "bgpd/bgp_damp.h"
39#include "zebra/rib.h"
40#include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
41
42struct bgp_nexthop_cache *zlookup_query (struct in_addr);
43#ifdef HAVE_IPV6
44struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *);
45#endif /* HAVE_IPV6 */
46
47/* Only one BGP scan thread are activated at the same time. */
paul00d252c2005-05-23 14:19:54 +000048static struct thread *bgp_scan_thread = NULL;
paul718e3742002-12-13 20:15:29 +000049
50/* BGP import thread */
paul00d252c2005-05-23 14:19:54 +000051static struct thread *bgp_import_thread = NULL;
paul718e3742002-12-13 20:15:29 +000052
53/* BGP scan interval. */
paul00d252c2005-05-23 14:19:54 +000054static int bgp_scan_interval;
paul718e3742002-12-13 20:15:29 +000055
56/* BGP import interval. */
paul00d252c2005-05-23 14:19:54 +000057static int bgp_import_interval;
paul718e3742002-12-13 20:15:29 +000058
59/* Route table for next-hop lookup cache. */
paul00d252c2005-05-23 14:19:54 +000060static struct bgp_table *bgp_nexthop_cache_table[AFI_MAX];
61static struct bgp_table *cache1_table[AFI_MAX];
62static struct bgp_table *cache2_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000063
64/* Route table for connected route. */
paul00d252c2005-05-23 14:19:54 +000065static struct bgp_table *bgp_connected_table[AFI_MAX];
paul718e3742002-12-13 20:15:29 +000066
67/* BGP nexthop lookup query client. */
68static struct zclient *zlookup = NULL;
paul718e3742002-12-13 20:15:29 +000069
70/* Add nexthop to the end of the list. */
paul94f2b392005-06-28 12:44:16 +000071static void
paul718e3742002-12-13 20:15:29 +000072bnc_nexthop_add (struct bgp_nexthop_cache *bnc, struct nexthop *nexthop)
73{
74 struct nexthop *last;
75
76 for (last = bnc->nexthop; last && last->next; last = last->next)
77 ;
78 if (last)
79 last->next = nexthop;
80 else
81 bnc->nexthop = nexthop;
82 nexthop->prev = last;
83}
84
paul94f2b392005-06-28 12:44:16 +000085static void
paul718e3742002-12-13 20:15:29 +000086bnc_nexthop_free (struct bgp_nexthop_cache *bnc)
87{
88 struct nexthop *nexthop;
89 struct nexthop *next = NULL;
90
91 for (nexthop = bnc->nexthop; nexthop; nexthop = next)
92 {
93 next = nexthop->next;
94 XFREE (MTYPE_NEXTHOP, nexthop);
95 }
96}
97
paul94f2b392005-06-28 12:44:16 +000098static struct bgp_nexthop_cache *
paul718e3742002-12-13 20:15:29 +000099bnc_new ()
100{
101 struct bgp_nexthop_cache *new;
102
103 new = XMALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
104 memset (new, 0, sizeof (struct bgp_nexthop_cache));
105 return new;
106}
107
paul94f2b392005-06-28 12:44:16 +0000108static void
paul718e3742002-12-13 20:15:29 +0000109bnc_free (struct bgp_nexthop_cache *bnc)
110{
111 bnc_nexthop_free (bnc);
112 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
113}
114
paul94f2b392005-06-28 12:44:16 +0000115static int
paul718e3742002-12-13 20:15:29 +0000116bgp_nexthop_same (struct nexthop *next1, struct nexthop *next2)
117{
118 if (next1->type != next2->type)
119 return 0;
120
121 switch (next1->type)
122 {
123 case ZEBRA_NEXTHOP_IPV4:
124 if (! IPV4_ADDR_SAME (&next1->gate.ipv4, &next2->gate.ipv4))
125 return 0;
126 break;
127 case ZEBRA_NEXTHOP_IFINDEX:
128 case ZEBRA_NEXTHOP_IFNAME:
129 if (next1->ifindex != next2->ifindex)
130 return 0;
131 break;
132#ifdef HAVE_IPV6
133 case ZEBRA_NEXTHOP_IPV6:
134 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
135 return 0;
136 break;
137 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
138 case ZEBRA_NEXTHOP_IPV6_IFNAME:
139 if (! IPV6_ADDR_SAME (&next1->gate.ipv6, &next2->gate.ipv6))
140 return 0;
141 if (next1->ifindex != next2->ifindex)
142 return 0;
143 break;
144#endif /* HAVE_IPV6 */
hassofa2b17e2004-03-04 17:45:00 +0000145 default:
146 /* do nothing */
147 break;
paul718e3742002-12-13 20:15:29 +0000148 }
149 return 1;
150}
151
paul94f2b392005-06-28 12:44:16 +0000152static int
paul718e3742002-12-13 20:15:29 +0000153bgp_nexthop_cache_changed (struct bgp_nexthop_cache *bnc1,
154 struct bgp_nexthop_cache *bnc2)
155{
156 int i;
157 struct nexthop *next1, *next2;
158
159 if (bnc1->nexthop_num != bnc2->nexthop_num)
160 return 1;
161
162 next1 = bnc1->nexthop;
163 next2 = bnc2->nexthop;
164
165 for (i = 0; i < bnc1->nexthop_num; i++)
166 {
167 if (! bgp_nexthop_same (next1, next2))
168 return 1;
169
170 next1 = next1->next;
171 next2 = next2->next;
172 }
173 return 0;
174}
175
176/* If nexthop exists on connected network return 1. */
177int
178bgp_nexthop_check_ebgp (afi_t afi, struct attr *attr)
179{
180 struct bgp_node *rn;
181
182 /* If zebra is not enabled return */
183 if (zlookup->sock < 0)
184 return 1;
185
186 /* Lookup the address is onlink or not. */
187 if (afi == AFI_IP)
188 {
paul59320202004-11-09 01:54:03 +0000189 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
paul718e3742002-12-13 20:15:29 +0000190 if (rn)
191 {
192 bgp_unlock_node (rn);
193 return 1;
194 }
195 }
196#ifdef HAVE_IPV6
197 else if (afi == AFI_IP6)
198 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000199 if (attr->extra->mp_nexthop_len == 32)
paul718e3742002-12-13 20:15:29 +0000200 return 1;
Paul Jakmafb982c22007-05-04 20:15:47 +0000201 else if (attr->extra->mp_nexthop_len == 16)
paul718e3742002-12-13 20:15:29 +0000202 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000203 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000204 return 1;
205
paul59320202004-11-09 01:54:03 +0000206 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
Paul Jakmafb982c22007-05-04 20:15:47 +0000207 &attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000208 if (rn)
209 {
210 bgp_unlock_node (rn);
211 return 1;
212 }
213 }
214 }
215#endif /* HAVE_IPV6 */
216 return 0;
217}
218
219#ifdef HAVE_IPV6
220/* Check specified next-hop is reachable or not. */
paul94f2b392005-06-28 12:44:16 +0000221static int
paul718e3742002-12-13 20:15:29 +0000222bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
223 int *metricchanged)
224{
225 struct bgp_node *rn;
226 struct prefix p;
227 struct bgp_nexthop_cache *bnc;
228 struct attr *attr;
229
230 /* If lookup is not enabled, return valid. */
231 if (zlookup->sock < 0)
232 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000233 if (ri->extra)
234 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000235 return 1;
236 }
237
238 /* Only check IPv6 global address only nexthop. */
239 attr = ri->attr;
240
Paul Jakmafb982c22007-05-04 20:15:47 +0000241 if (attr->extra->mp_nexthop_len != 16
242 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
paul718e3742002-12-13 20:15:29 +0000243 return 1;
244
245 memset (&p, 0, sizeof (struct prefix));
246 p.family = AF_INET6;
247 p.prefixlen = IPV6_MAX_BITLEN;
Paul Jakmafb982c22007-05-04 20:15:47 +0000248 p.u.prefix6 = attr->extra->mp_nexthop_global;
paul718e3742002-12-13 20:15:29 +0000249
250 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000251 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
paul718e3742002-12-13 20:15:29 +0000252
253 if (rn->info)
254 {
255 bnc = rn->info;
256 bgp_unlock_node (rn);
257 }
258 else
259 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000260 bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global);
paul718e3742002-12-13 20:15:29 +0000261 if (bnc)
262 {
263 struct bgp_table *old;
264 struct bgp_node *oldrn;
265 struct bgp_nexthop_cache *oldbnc;
266
267 if (changed)
268 {
paul59320202004-11-09 01:54:03 +0000269 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
270 old = cache2_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000271 else
paul59320202004-11-09 01:54:03 +0000272 old = cache1_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000273
274 oldrn = bgp_node_lookup (old, &p);
275 if (oldrn)
276 {
277 oldbnc = oldrn->info;
278
279 bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
280
281 if (bnc->metric != oldbnc->metric)
282 bnc->metricchanged = 1;
283 }
284 }
285 }
286 else
287 {
288 bnc = bnc_new ();
289 bnc->valid = 0;
290 }
291 rn->info = bnc;
292 }
293
294 if (changed)
295 *changed = bnc->changed;
296
297 if (metricchanged)
298 *metricchanged = bnc->metricchanged;
299
Paul Jakmafb982c22007-05-04 20:15:47 +0000300 if (bnc->valid && bnc->metric)
301 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
302 else if (ri->extra)
303 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000304
305 return bnc->valid;
306}
307#endif /* HAVE_IPV6 */
308
309/* Check specified next-hop is reachable or not. */
310int
311bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
312 int *changed, int *metricchanged)
313{
314 struct bgp_node *rn;
315 struct prefix p;
316 struct bgp_nexthop_cache *bnc;
317 struct in_addr addr;
318
319 /* If lookup is not enabled, return valid. */
320 if (zlookup->sock < 0)
321 {
Paul Jakmafb982c22007-05-04 20:15:47 +0000322 if (ri->extra)
323 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000324 return 1;
325 }
326
327#ifdef HAVE_IPV6
328 if (afi == AFI_IP6)
329 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
330#endif /* HAVE_IPV6 */
331
332 addr = ri->attr->nexthop;
333
334 memset (&p, 0, sizeof (struct prefix));
335 p.family = AF_INET;
336 p.prefixlen = IPV4_MAX_BITLEN;
337 p.u.prefix4 = addr;
338
339 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000340 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000341
342 if (rn->info)
343 {
344 bnc = rn->info;
345 bgp_unlock_node (rn);
346 }
347 else
348 {
349 bnc = zlookup_query (addr);
350 if (bnc)
351 {
352 struct bgp_table *old;
353 struct bgp_node *oldrn;
354 struct bgp_nexthop_cache *oldbnc;
355
356 if (changed)
357 {
paul59320202004-11-09 01:54:03 +0000358 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
359 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000360 else
paul59320202004-11-09 01:54:03 +0000361 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000362
363 oldrn = bgp_node_lookup (old, &p);
364 if (oldrn)
365 {
366 oldbnc = oldrn->info;
367
368 bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
369
370 if (bnc->metric != oldbnc->metric)
371 bnc->metricchanged = 1;
372 }
373 }
374 }
375 else
376 {
377 bnc = bnc_new ();
378 bnc->valid = 0;
379 }
380 rn->info = bnc;
381 }
382
383 if (changed)
384 *changed = bnc->changed;
385
386 if (metricchanged)
387 *metricchanged = bnc->metricchanged;
388
Paul Jakmafb982c22007-05-04 20:15:47 +0000389 if (bnc->valid && bnc->metric)
390 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
391 else if (ri->extra)
392 ri->extra->igpmetric = 0;
paul718e3742002-12-13 20:15:29 +0000393
394 return bnc->valid;
395}
396
397/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000398static void
paul718e3742002-12-13 20:15:29 +0000399bgp_nexthop_cache_reset (struct bgp_table *table)
400{
401 struct bgp_node *rn;
402 struct bgp_nexthop_cache *bnc;
403
404 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
405 if ((bnc = rn->info) != NULL)
406 {
407 bnc_free (bnc);
408 rn->info = NULL;
409 bgp_unlock_node (rn);
410 }
411}
412
paul94f2b392005-06-28 12:44:16 +0000413static void
paul59320202004-11-09 01:54:03 +0000414bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000415{
416 struct bgp_node *rn;
417 struct bgp *bgp;
418 struct bgp_info *bi;
419 struct bgp_info *next;
420 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000421 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000422 int valid;
423 int current;
424 int changed;
425 int metricchanged;
426
427 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000428 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
429 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000430 else
paul59320202004-11-09 01:54:03 +0000431 bgp_nexthop_cache_table[afi] = cache1_table[afi];
paul718e3742002-12-13 20:15:29 +0000432
433 /* Get default bgp. */
434 bgp = bgp_get_default ();
435 if (bgp == NULL)
436 return;
437
438 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000439 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000440 {
441 if (peer->status != Established)
442 continue;
443
paul59320202004-11-09 01:54:03 +0000444 if (peer->afc[afi][SAFI_UNICAST])
445 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
446 if (peer->afc[afi][SAFI_MULTICAST])
447 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
448 if (peer->afc[afi][SAFI_MPLS_VPN])
449 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
paul718e3742002-12-13 20:15:29 +0000450 }
451
paul59320202004-11-09 01:54:03 +0000452 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000453 rn = bgp_route_next (rn))
454 {
455 for (bi = rn->info; bi; bi = next)
456 {
457 next = bi->next;
458
459 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
460 {
461 changed = 0;
462 metricchanged = 0;
463
464 if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
paul59320202004-11-09 01:54:03 +0000465 valid = bgp_nexthop_check_ebgp (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000466 else
paul59320202004-11-09 01:54:03 +0000467 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000468 &changed, &metricchanged);
469
470 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
471
472 if (changed)
473 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
474 else
475 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
476
477 if (valid != current)
478 {
479 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
480 {
481 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000482 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000483 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000484 }
485 else
486 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000487 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000488 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000489 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000490 }
491 }
492
paul59320202004-11-09 01:54:03 +0000493 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000494 BGP_CONFIG_DAMPENING)
Paul Jakmafb982c22007-05-04 20:15:47 +0000495 && bi->extra && bi->extra->damp_info )
paul59320202004-11-09 01:54:03 +0000496 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000497 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000498 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000499 }
500 }
paul59320202004-11-09 01:54:03 +0000501 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000502 }
503
504 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000505 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
506 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000507 else
paul59320202004-11-09 01:54:03 +0000508 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000509
510 if (BGP_DEBUG (events, EVENTS))
511 {
512 if (afi == AFI_IP)
513 zlog_debug ("scanning IPv4 Unicast routing tables");
514 else if (afi == AFI_IP6)
515 zlog_debug ("scanning IPv6 Unicast routing tables");
516 }
paul718e3742002-12-13 20:15:29 +0000517}
518
paul718e3742002-12-13 20:15:29 +0000519/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000520static int
paul59320202004-11-09 01:54:03 +0000521bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000522{
523 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +0000524 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000525
hassof4184462005-02-01 20:13:16 +0000526 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000527 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000528
paul59320202004-11-09 01:54:03 +0000529 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000530
531#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000532 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000533#endif /* HAVE_IPV6 */
534
535 return 0;
536}
537
paul59320202004-11-09 01:54:03 +0000538struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000539{
540 unsigned int refcnt;
541};
542
543void
544bgp_connected_add (struct connected *ifc)
545{
546 struct prefix p;
547 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000548 struct interface *ifp;
549 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000550 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000551
552 ifp = ifc->ifp;
553
554 if (! ifp)
555 return;
556
557 if (if_is_loopback (ifp))
558 return;
559
560 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000561
562 if (addr->family == AF_INET)
563 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000564 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000565 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
566
567 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
568 return;
569
paul59320202004-11-09 01:54:03 +0000570 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000571 if (rn->info)
572 {
573 bc = rn->info;
574 bc->refcnt++;
575 }
576 else
577 {
paul59320202004-11-09 01:54:03 +0000578 bc = XMALLOC (0, sizeof (struct bgp_connected_ref));
579 memset (bc, 0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000580 bc->refcnt = 1;
581 rn->info = bc;
582 }
583 }
584#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000585 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000586 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000587 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000588 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
589
590 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
591 return;
592
593 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
594 return;
595
paul59320202004-11-09 01:54:03 +0000596 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000597 if (rn->info)
598 {
599 bc = rn->info;
600 bc->refcnt++;
601 }
602 else
603 {
paul59320202004-11-09 01:54:03 +0000604 bc = XMALLOC (0, sizeof (struct bgp_connected_ref));
605 memset (bc, 0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000606 bc->refcnt = 1;
607 rn->info = bc;
608 }
609 }
610#endif /* HAVE_IPV6 */
611}
612
613void
614bgp_connected_delete (struct connected *ifc)
615{
616 struct prefix p;
617 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000618 struct interface *ifp;
619 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000620 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000621
622 ifp = ifc->ifp;
623
624 if (if_is_loopback (ifp))
625 return;
626
627 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000628
629 if (addr->family == AF_INET)
630 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000631 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000632 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
633
634 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
635 return;
636
paul59320202004-11-09 01:54:03 +0000637 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000638 if (! rn)
639 return;
640
641 bc = rn->info;
642 bc->refcnt--;
643 if (bc->refcnt == 0)
644 {
645 XFREE (0, bc);
646 rn->info = NULL;
647 }
648 bgp_unlock_node (rn);
649 bgp_unlock_node (rn);
650 }
651#ifdef HAVE_IPV6
652 else if (addr->family == AF_INET6)
653 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000654 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000655 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
656
657 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
658 return;
659
660 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
661 return;
662
paul59320202004-11-09 01:54:03 +0000663 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000664 if (! rn)
665 return;
666
667 bc = rn->info;
668 bc->refcnt--;
669 if (bc->refcnt == 0)
670 {
671 XFREE (0, bc);
672 rn->info = NULL;
673 }
674 bgp_unlock_node (rn);
675 bgp_unlock_node (rn);
676 }
677#endif /* HAVE_IPV6 */
678}
679
680int
681bgp_nexthop_self (afi_t afi, struct attr *attr)
682{
hasso52dc7ee2004-09-23 19:18:23 +0000683 struct listnode *node;
684 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000685 struct interface *ifp;
686 struct connected *ifc;
687 struct prefix *p;
688
paul1eb8ef22005-04-07 07:30:20 +0000689 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000690 {
paul1eb8ef22005-04-07 07:30:20 +0000691 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000692 {
paul718e3742002-12-13 20:15:29 +0000693 p = ifc->address;
694
695 if (p && p->family == AF_INET
696 && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
697 return 1;
698 }
699 }
700 return 0;
701}
702
paul94f2b392005-06-28 12:44:16 +0000703static struct bgp_nexthop_cache *
paul718e3742002-12-13 20:15:29 +0000704zlookup_read ()
705{
706 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000707 uint16_t length;
708 u_char marker;
709 u_char version;
710 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000711 int nbytes;
712 struct in_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000713 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000714 int i;
715 u_char nexthop_num;
716 struct nexthop *nexthop;
717 struct bgp_nexthop_cache *bnc;
718
719 s = zlookup->ibuf;
720 stream_reset (s);
721
722 nbytes = stream_read (s, zlookup->sock, 2);
723 length = stream_getw (s);
724
725 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000726 marker = stream_getc (s);
727 version = stream_getc (s);
728
729 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
730 {
731 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
732 __func__, zlookup->sock, marker, version);
733 return NULL;
734 }
735
736 command = stream_getw (s);
737
paul718e3742002-12-13 20:15:29 +0000738 raddr.s_addr = stream_get_ipv4 (s);
739 metric = stream_getl (s);
740 nexthop_num = stream_getc (s);
741
742 if (nexthop_num)
743 {
744 bnc = bnc_new ();
745 bnc->valid = 1;
746 bnc->metric = metric;
747 bnc->nexthop_num = nexthop_num;
748
749 for (i = 0; i < nexthop_num; i++)
750 {
751 nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
752 memset (nexthop, 0, sizeof (struct nexthop));
753 nexthop->type = stream_getc (s);
754 switch (nexthop->type)
755 {
756 case ZEBRA_NEXTHOP_IPV4:
757 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
758 break;
759 case ZEBRA_NEXTHOP_IFINDEX:
760 case ZEBRA_NEXTHOP_IFNAME:
761 nexthop->ifindex = stream_getl (s);
762 break;
hassofa2b17e2004-03-04 17:45:00 +0000763 default:
764 /* do nothing */
765 break;
paul718e3742002-12-13 20:15:29 +0000766 }
767 bnc_nexthop_add (bnc, nexthop);
768 }
769 }
770 else
771 return NULL;
772
773 return bnc;
774}
775
776struct bgp_nexthop_cache *
777zlookup_query (struct in_addr addr)
778{
779 int ret;
780 struct stream *s;
781
782 /* Check socket. */
783 if (zlookup->sock < 0)
784 return NULL;
785
786 s = zlookup->obuf;
787 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000788 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000789 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000790
791 stream_putw_at (s, 0, stream_get_endp (s));
792
793 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000794 if (ret < 0)
795 {
796 zlog_err ("can't write to zlookup->sock");
797 close (zlookup->sock);
798 zlookup->sock = -1;
799 return NULL;
800 }
801 if (ret == 0)
802 {
803 zlog_err ("zlookup->sock connection closed");
804 close (zlookup->sock);
805 zlookup->sock = -1;
806 return NULL;
807 }
808
809 return zlookup_read ();
810}
811
812#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000813static struct bgp_nexthop_cache *
paul718e3742002-12-13 20:15:29 +0000814zlookup_read_ipv6 ()
815{
816 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000817 uint16_t length;
818 u_char version, marker;
819 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000820 int nbytes;
821 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000822 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000823 int i;
824 u_char nexthop_num;
825 struct nexthop *nexthop;
826 struct bgp_nexthop_cache *bnc;
827
828 s = zlookup->ibuf;
829 stream_reset (s);
830
831 nbytes = stream_read (s, zlookup->sock, 2);
832 length = stream_getw (s);
833
834 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000835 marker = stream_getc (s);
836 version = stream_getc (s);
837
838 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
839 {
840 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
841 __func__, zlookup->sock, marker, version);
842 return NULL;
843 }
844
845 command = stream_getw (s);
846
paul718e3742002-12-13 20:15:29 +0000847 stream_get (&raddr, s, 16);
848
849 metric = stream_getl (s);
850 nexthop_num = stream_getc (s);
851
852 if (nexthop_num)
853 {
854 bnc = bnc_new ();
855 bnc->valid = 1;
856 bnc->metric = metric;
857 bnc->nexthop_num = nexthop_num;
858
859 for (i = 0; i < nexthop_num; i++)
860 {
861 nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
862 memset (nexthop, 0, sizeof (struct nexthop));
863 nexthop->type = stream_getc (s);
864 switch (nexthop->type)
865 {
866 case ZEBRA_NEXTHOP_IPV6:
867 stream_get (&nexthop->gate.ipv6, s, 16);
868 break;
869 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
870 case ZEBRA_NEXTHOP_IPV6_IFNAME:
871 stream_get (&nexthop->gate.ipv6, s, 16);
872 nexthop->ifindex = stream_getl (s);
873 break;
874 case ZEBRA_NEXTHOP_IFINDEX:
875 case ZEBRA_NEXTHOP_IFNAME:
876 nexthop->ifindex = stream_getl (s);
877 break;
hassofa2b17e2004-03-04 17:45:00 +0000878 default:
879 /* do nothing */
880 break;
paul718e3742002-12-13 20:15:29 +0000881 }
882 bnc_nexthop_add (bnc, nexthop);
883 }
884 }
885 else
886 return NULL;
887
888 return bnc;
889}
890
891struct bgp_nexthop_cache *
892zlookup_query_ipv6 (struct in6_addr *addr)
893{
894 int ret;
895 struct stream *s;
896
897 /* Check socket. */
898 if (zlookup->sock < 0)
899 return NULL;
900
901 s = zlookup->obuf;
902 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000903 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000904 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000905 stream_putw_at (s, 0, stream_get_endp (s));
906
907 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000908 if (ret < 0)
909 {
910 zlog_err ("can't write to zlookup->sock");
911 close (zlookup->sock);
912 zlookup->sock = -1;
913 return NULL;
914 }
915 if (ret == 0)
916 {
917 zlog_err ("zlookup->sock connection closed");
918 close (zlookup->sock);
919 zlookup->sock = -1;
920 return NULL;
921 }
922
923 return zlookup_read_ipv6 ();
924}
925#endif /* HAVE_IPV6 */
926
paul94f2b392005-06-28 12:44:16 +0000927static int
928bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
929 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000930{
931 struct stream *s;
932 int ret;
pauld3092e72006-01-17 17:33:46 +0000933 u_int16_t length, command;
934 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000935 int nbytes;
936 struct in_addr addr;
937 struct in_addr nexthop;
938 u_int32_t metric = 0;
939 u_char nexthop_num;
940 u_char nexthop_type;
941
942 /* If lookup connection is not available return valid. */
943 if (zlookup->sock < 0)
944 {
945 if (igpmetric)
946 *igpmetric = 0;
947 return 1;
948 }
949
950 /* Send query to the lookup connection */
951 s = zlookup->obuf;
952 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000953 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
954
paul718e3742002-12-13 20:15:29 +0000955 stream_putc (s, p->prefixlen);
956 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +0000957
958 stream_putw_at (s, 0, stream_get_endp (s));
959
paul718e3742002-12-13 20:15:29 +0000960 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +0000961 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000962
963 if (ret < 0)
964 {
965 zlog_err ("can't write to zlookup->sock");
966 close (zlookup->sock);
967 zlookup->sock = -1;
968 return 1;
969 }
970 if (ret == 0)
971 {
972 zlog_err ("zlookup->sock connection closed");
973 close (zlookup->sock);
974 zlookup->sock = -1;
975 return 1;
976 }
977
978 /* Get result. */
979 stream_reset (s);
980
981 /* Fetch length. */
982 nbytes = stream_read (s, zlookup->sock, 2);
983 length = stream_getw (s);
984
985 /* Fetch whole data. */
986 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000987 marker = stream_getc (s);
988 version = stream_getc (s);
989
990 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
991 {
992 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
993 __func__, zlookup->sock, marker, version);
994 return 0;
995 }
996
997 command = stream_getw (s);
998
paul718e3742002-12-13 20:15:29 +0000999 addr.s_addr = stream_get_ipv4 (s);
1000 metric = stream_getl (s);
1001 nexthop_num = stream_getc (s);
1002
1003 /* Set IGP metric value. */
1004 if (igpmetric)
1005 *igpmetric = metric;
1006
1007 /* If there is nexthop then this is active route. */
1008 if (nexthop_num)
1009 {
1010 nexthop.s_addr = 0;
1011 nexthop_type = stream_getc (s);
1012 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
1013 {
1014 nexthop.s_addr = stream_get_ipv4 (s);
1015 if (igpnexthop)
1016 *igpnexthop = nexthop;
1017 }
1018 else
1019 *igpnexthop = nexthop;
1020
1021 return 1;
1022 }
1023 else
1024 return 0;
1025}
1026
1027/* Scan all configured BGP route then check the route exists in IGP or
1028 not. */
paul94f2b392005-06-28 12:44:16 +00001029static int
paul718e3742002-12-13 20:15:29 +00001030bgp_import (struct thread *t)
1031{
1032 struct bgp *bgp;
1033 struct bgp_node *rn;
1034 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001035 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001036 int valid;
1037 u_int32_t metric;
1038 struct in_addr nexthop;
1039 afi_t afi;
1040 safi_t safi;
1041
1042 bgp_import_thread =
1043 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1044
hassof4184462005-02-01 20:13:16 +00001045 if (BGP_DEBUG (events, EVENTS))
1046 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001047
paul1eb8ef22005-04-07 07:30:20 +00001048 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001049 {
1050 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1051 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1052 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1053 rn = bgp_route_next (rn))
1054 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001055 {
paul6cbbc3c2003-04-28 17:11:02 +00001056 if (bgp_static->backdoor)
1057 continue;
paul718e3742002-12-13 20:15:29 +00001058
paul6cbbc3c2003-04-28 17:11:02 +00001059 valid = bgp_static->valid;
1060 metric = bgp_static->igpmetric;
1061 nexthop = bgp_static->igpnexthop;
1062
1063 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1064 && afi == AFI_IP && safi == SAFI_UNICAST)
1065 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1066 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001067 else
paul6cbbc3c2003-04-28 17:11:02 +00001068 {
1069 bgp_static->valid = 1;
1070 bgp_static->igpmetric = 0;
1071 bgp_static->igpnexthop.s_addr = 0;
1072 }
1073
1074 if (bgp_static->valid != valid)
1075 {
1076 if (bgp_static->valid)
1077 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1078 else
1079 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1080 }
1081 else if (bgp_static->valid)
1082 {
1083 if (bgp_static->igpmetric != metric
1084 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1085 || bgp_static->rmap.name)
1086 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1087 }
paul718e3742002-12-13 20:15:29 +00001088 }
paul6cbbc3c2003-04-28 17:11:02 +00001089 }
paul718e3742002-12-13 20:15:29 +00001090 return 0;
1091}
1092
1093/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001094static int
paul718e3742002-12-13 20:15:29 +00001095zlookup_connect (struct thread *t)
1096{
1097 struct zclient *zlookup;
1098
1099 zlookup = THREAD_ARG (t);
1100 zlookup->t_connect = NULL;
1101
1102 if (zlookup->sock != -1)
1103 return 0;
1104
1105#ifdef HAVE_TCP_ZEBRA
1106 zlookup->sock = zclient_socket ();
1107#else
1108 zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH);
1109#endif /* HAVE_TCP_ZEBRA */
1110 if (zlookup->sock < 0)
1111 return -1;
1112
paul718e3742002-12-13 20:15:29 +00001113 return 0;
1114}
1115
1116/* Check specified multiaccess next-hop. */
1117int
1118bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1119{
1120 struct bgp_node *rn1;
1121 struct bgp_node *rn2;
1122 struct prefix p1;
1123 struct prefix p2;
1124 struct in_addr addr;
1125 int ret;
1126
1127 ret = inet_aton (peer, &addr);
1128 if (! ret)
1129 return 0;
1130
1131 memset (&p1, 0, sizeof (struct prefix));
1132 p1.family = AF_INET;
1133 p1.prefixlen = IPV4_MAX_BITLEN;
1134 p1.u.prefix4 = nexthop;
1135 memset (&p2, 0, sizeof (struct prefix));
1136 p2.family = AF_INET;
1137 p2.prefixlen = IPV4_MAX_BITLEN;
1138 p2.u.prefix4 = addr;
1139
1140 /* If bgp scan is not enabled, return invalid. */
1141 if (zlookup->sock < 0)
1142 return 0;
1143
paul59320202004-11-09 01:54:03 +00001144 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001145 if (! rn1)
1146 return 0;
1147
paul59320202004-11-09 01:54:03 +00001148 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001149 if (! rn2)
1150 return 0;
1151
1152 if (rn1 == rn2)
1153 return 1;
1154
1155 return 0;
1156}
1157
1158DEFUN (bgp_scan_time,
1159 bgp_scan_time_cmd,
1160 "bgp scan-time <5-60>",
1161 "BGP specific commands\n"
1162 "Configure background scanner interval\n"
1163 "Scanner interval (seconds)\n")
1164{
1165 bgp_scan_interval = atoi (argv[0]);
1166
1167 if (bgp_scan_thread)
1168 {
1169 thread_cancel (bgp_scan_thread);
1170 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001171 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001172 }
1173
1174 return CMD_SUCCESS;
1175}
1176
1177DEFUN (no_bgp_scan_time,
1178 no_bgp_scan_time_cmd,
1179 "no bgp scan-time",
1180 NO_STR
1181 "BGP specific commands\n"
1182 "Configure background scanner interval\n")
1183{
1184 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1185
1186 if (bgp_scan_thread)
1187 {
1188 thread_cancel (bgp_scan_thread);
1189 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001190 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001191 }
1192
1193 return CMD_SUCCESS;
1194}
1195
1196ALIAS (no_bgp_scan_time,
1197 no_bgp_scan_time_val_cmd,
1198 "no bgp scan-time <5-60>",
1199 NO_STR
1200 "BGP specific commands\n"
1201 "Configure background scanner interval\n"
1202 "Scanner interval (seconds)\n")
1203
1204DEFUN (show_ip_bgp_scan,
1205 show_ip_bgp_scan_cmd,
1206 "show ip bgp scan",
1207 SHOW_STR
1208 IP_STR
1209 BGP_STR
1210 "BGP scan status\n")
1211{
1212 struct bgp_node *rn;
1213 struct bgp_nexthop_cache *bnc;
1214
1215 if (bgp_scan_thread)
1216 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1217 else
1218 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1219 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1220
1221 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001222 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001223 if ((bnc = rn->info) != NULL)
1224 {
1225 if (bnc->valid)
1226 vty_out (vty, " %s valid [IGP metric %d]%s",
1227 inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE);
1228 else
1229 vty_out (vty, " %s invalid%s",
1230 inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE);
1231 }
1232
1233#ifdef HAVE_IPV6
1234 {
1235 char buf[BUFSIZ];
paul59320202004-11-09 01:54:03 +00001236 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1237 rn;
1238 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001239 if ((bnc = rn->info) != NULL)
1240 {
1241 if (bnc->valid)
1242 vty_out (vty, " %s valid [IGP metric %d]%s",
1243 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1244 bnc->metric, VTY_NEWLINE);
1245 else
1246 vty_out (vty, " %s invalid%s",
1247 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1248 VTY_NEWLINE);
1249 }
1250 }
1251#endif /* HAVE_IPV6 */
1252
1253 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001254 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1255 rn;
1256 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001257 if (rn->info != NULL)
1258 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1259 VTY_NEWLINE);
1260
1261#ifdef HAVE_IPV6
1262 {
1263 char buf[BUFSIZ];
1264
paul59320202004-11-09 01:54:03 +00001265 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1266 rn;
1267 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001268 if (rn->info != NULL)
1269 vty_out (vty, " %s/%d%s",
1270 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1271 rn->p.prefixlen,
1272 VTY_NEWLINE);
1273 }
1274#endif /* HAVE_IPV6 */
1275
1276 return CMD_SUCCESS;
1277}
1278
1279int
1280bgp_config_write_scan_time (struct vty *vty)
1281{
1282 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1283 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1284 return CMD_SUCCESS;
1285}
1286
1287void
1288bgp_scan_init ()
1289{
1290 zlookup = zclient_new ();
1291 zlookup->sock = -1;
1292 zlookup->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1293 zlookup->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1294 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1295
1296 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1297 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1298
Paul Jakma64e580a2006-02-21 01:09:01 +00001299 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1300 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001301 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001302
Paul Jakma64e580a2006-02-21 01:09:01 +00001303 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001304
1305#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001306 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1307 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001308 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001309 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001310#endif /* HAVE_IPV6 */
1311
1312 /* Make BGP scan thread. */
paul59320202004-11-09 01:54:03 +00001313 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1314 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001315 /* Make BGP import there. */
1316 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001317
1318 install_element (BGP_NODE, &bgp_scan_time_cmd);
1319 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1320 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1321 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
1322 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
1323}