blob: bfc41a1842379cf7f5c198fad569a919ecd97419 [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 {
199 if (attr->mp_nexthop_len == 32)
200 return 1;
201 else if (attr->mp_nexthop_len == 16)
202 {
203 if (IN6_IS_ADDR_LINKLOCAL (&attr->mp_nexthop_global))
204 return 1;
205
paul59320202004-11-09 01:54:03 +0000206 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
paul718e3742002-12-13 20:15:29 +0000207 &attr->mp_nexthop_global);
208 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 {
233 ri->igpmetric = 0;
234 return 1;
235 }
236
237 /* Only check IPv6 global address only nexthop. */
238 attr = ri->attr;
239
240 if (attr->mp_nexthop_len != 16
241 || IN6_IS_ADDR_LINKLOCAL (&attr->mp_nexthop_global))
242 return 1;
243
244 memset (&p, 0, sizeof (struct prefix));
245 p.family = AF_INET6;
246 p.prefixlen = IPV6_MAX_BITLEN;
247 p.u.prefix6 = attr->mp_nexthop_global;
248
249 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000250 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
paul718e3742002-12-13 20:15:29 +0000251
252 if (rn->info)
253 {
254 bnc = rn->info;
255 bgp_unlock_node (rn);
256 }
257 else
258 {
259 bnc = zlookup_query_ipv6 (&attr->mp_nexthop_global);
260 if (bnc)
261 {
262 struct bgp_table *old;
263 struct bgp_node *oldrn;
264 struct bgp_nexthop_cache *oldbnc;
265
266 if (changed)
267 {
paul59320202004-11-09 01:54:03 +0000268 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
269 old = cache2_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000270 else
paul59320202004-11-09 01:54:03 +0000271 old = cache1_table[AFI_IP6];
paul718e3742002-12-13 20:15:29 +0000272
273 oldrn = bgp_node_lookup (old, &p);
274 if (oldrn)
275 {
276 oldbnc = oldrn->info;
277
278 bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
279
280 if (bnc->metric != oldbnc->metric)
281 bnc->metricchanged = 1;
282 }
283 }
284 }
285 else
286 {
287 bnc = bnc_new ();
288 bnc->valid = 0;
289 }
290 rn->info = bnc;
291 }
292
293 if (changed)
294 *changed = bnc->changed;
295
296 if (metricchanged)
297 *metricchanged = bnc->metricchanged;
298
299 if (bnc->valid)
300 ri->igpmetric = bnc->metric;
301 else
302 ri->igpmetric = 0;
303
304 return bnc->valid;
305}
306#endif /* HAVE_IPV6 */
307
308/* Check specified next-hop is reachable or not. */
309int
310bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
311 int *changed, int *metricchanged)
312{
313 struct bgp_node *rn;
314 struct prefix p;
315 struct bgp_nexthop_cache *bnc;
316 struct in_addr addr;
317
318 /* If lookup is not enabled, return valid. */
319 if (zlookup->sock < 0)
320 {
321 ri->igpmetric = 0;
322 return 1;
323 }
324
325#ifdef HAVE_IPV6
326 if (afi == AFI_IP6)
327 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
328#endif /* HAVE_IPV6 */
329
330 addr = ri->attr->nexthop;
331
332 memset (&p, 0, sizeof (struct prefix));
333 p.family = AF_INET;
334 p.prefixlen = IPV4_MAX_BITLEN;
335 p.u.prefix4 = addr;
336
337 /* IBGP or ebgp-multihop */
paul59320202004-11-09 01:54:03 +0000338 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000339
340 if (rn->info)
341 {
342 bnc = rn->info;
343 bgp_unlock_node (rn);
344 }
345 else
346 {
347 bnc = zlookup_query (addr);
348 if (bnc)
349 {
350 struct bgp_table *old;
351 struct bgp_node *oldrn;
352 struct bgp_nexthop_cache *oldbnc;
353
354 if (changed)
355 {
paul59320202004-11-09 01:54:03 +0000356 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
357 old = cache2_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000358 else
paul59320202004-11-09 01:54:03 +0000359 old = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +0000360
361 oldrn = bgp_node_lookup (old, &p);
362 if (oldrn)
363 {
364 oldbnc = oldrn->info;
365
366 bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc);
367
368 if (bnc->metric != oldbnc->metric)
369 bnc->metricchanged = 1;
370 }
371 }
372 }
373 else
374 {
375 bnc = bnc_new ();
376 bnc->valid = 0;
377 }
378 rn->info = bnc;
379 }
380
381 if (changed)
382 *changed = bnc->changed;
383
384 if (metricchanged)
385 *metricchanged = bnc->metricchanged;
386
387 if (bnc->valid)
388 ri->igpmetric = bnc->metric;
389 else
390 ri->igpmetric = 0;
391
392 return bnc->valid;
393}
394
395/* Reset and free all BGP nexthop cache. */
paul94f2b392005-06-28 12:44:16 +0000396static void
paul718e3742002-12-13 20:15:29 +0000397bgp_nexthop_cache_reset (struct bgp_table *table)
398{
399 struct bgp_node *rn;
400 struct bgp_nexthop_cache *bnc;
401
402 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
403 if ((bnc = rn->info) != NULL)
404 {
405 bnc_free (bnc);
406 rn->info = NULL;
407 bgp_unlock_node (rn);
408 }
409}
410
paul94f2b392005-06-28 12:44:16 +0000411static void
paul59320202004-11-09 01:54:03 +0000412bgp_scan (afi_t afi, safi_t safi)
paul718e3742002-12-13 20:15:29 +0000413{
414 struct bgp_node *rn;
415 struct bgp *bgp;
416 struct bgp_info *bi;
417 struct bgp_info *next;
418 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +0000419 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +0000420 int valid;
421 int current;
422 int changed;
423 int metricchanged;
424
425 /* Change cache. */
paul59320202004-11-09 01:54:03 +0000426 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
427 bgp_nexthop_cache_table[afi] = cache2_table[afi];
paul718e3742002-12-13 20:15:29 +0000428 else
paul59320202004-11-09 01:54:03 +0000429 bgp_nexthop_cache_table[afi] = cache1_table[afi];
paul718e3742002-12-13 20:15:29 +0000430
431 /* Get default bgp. */
432 bgp = bgp_get_default ();
433 if (bgp == NULL)
434 return;
435
436 /* Maximum prefix check */
paul1eb8ef22005-04-07 07:30:20 +0000437 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +0000438 {
439 if (peer->status != Established)
440 continue;
441
paul59320202004-11-09 01:54:03 +0000442 if (peer->afc[afi][SAFI_UNICAST])
443 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
444 if (peer->afc[afi][SAFI_MULTICAST])
445 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
446 if (peer->afc[afi][SAFI_MPLS_VPN])
447 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
paul718e3742002-12-13 20:15:29 +0000448 }
449
paul59320202004-11-09 01:54:03 +0000450 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
paul718e3742002-12-13 20:15:29 +0000451 rn = bgp_route_next (rn))
452 {
453 for (bi = rn->info; bi; bi = next)
454 {
455 next = bi->next;
456
457 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
458 {
459 changed = 0;
460 metricchanged = 0;
461
462 if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
paul59320202004-11-09 01:54:03 +0000463 valid = bgp_nexthop_check_ebgp (afi, bi->attr);
paul718e3742002-12-13 20:15:29 +0000464 else
paul59320202004-11-09 01:54:03 +0000465 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
paul718e3742002-12-13 20:15:29 +0000466 &changed, &metricchanged);
467
468 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
469
470 if (changed)
471 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
472 else
473 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
474
475 if (valid != current)
476 {
477 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
478 {
479 bgp_aggregate_decrement (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000480 afi, SAFI_UNICAST);
Paul Jakma1a392d42006-09-07 00:24:49 +0000481 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000482 }
483 else
484 {
Paul Jakma1a392d42006-09-07 00:24:49 +0000485 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
paul718e3742002-12-13 20:15:29 +0000486 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000487 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000488 }
489 }
490
paul59320202004-11-09 01:54:03 +0000491 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
paul718e3742002-12-13 20:15:29 +0000492 BGP_CONFIG_DAMPENING)
493 && bi->damp_info )
paul59320202004-11-09 01:54:03 +0000494 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
paul718e3742002-12-13 20:15:29 +0000495 bgp_aggregate_increment (bgp, &rn->p, bi,
paul59320202004-11-09 01:54:03 +0000496 afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000497 }
498 }
paul59320202004-11-09 01:54:03 +0000499 bgp_process (bgp, rn, afi, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000500 }
501
502 /* Flash old cache. */
paul59320202004-11-09 01:54:03 +0000503 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
504 bgp_nexthop_cache_reset (cache2_table[afi]);
paul718e3742002-12-13 20:15:29 +0000505 else
paul59320202004-11-09 01:54:03 +0000506 bgp_nexthop_cache_reset (cache1_table[afi]);
hassof4184462005-02-01 20:13:16 +0000507
508 if (BGP_DEBUG (events, EVENTS))
509 {
510 if (afi == AFI_IP)
511 zlog_debug ("scanning IPv4 Unicast routing tables");
512 else if (afi == AFI_IP6)
513 zlog_debug ("scanning IPv6 Unicast routing tables");
514 }
paul718e3742002-12-13 20:15:29 +0000515}
516
paul718e3742002-12-13 20:15:29 +0000517/* BGP scan thread. This thread check nexthop reachability. */
paul94f2b392005-06-28 12:44:16 +0000518static int
paul59320202004-11-09 01:54:03 +0000519bgp_scan_timer (struct thread *t)
paul718e3742002-12-13 20:15:29 +0000520{
521 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +0000522 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +0000523
hassof4184462005-02-01 20:13:16 +0000524 if (BGP_DEBUG (events, EVENTS))
ajs478ba052004-12-08 20:41:23 +0000525 zlog_debug ("Performing BGP general scanning");
paul718e3742002-12-13 20:15:29 +0000526
paul59320202004-11-09 01:54:03 +0000527 bgp_scan (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000528
529#ifdef HAVE_IPV6
paul59320202004-11-09 01:54:03 +0000530 bgp_scan (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +0000531#endif /* HAVE_IPV6 */
532
533 return 0;
534}
535
paul59320202004-11-09 01:54:03 +0000536struct bgp_connected_ref
paul718e3742002-12-13 20:15:29 +0000537{
538 unsigned int refcnt;
539};
540
541void
542bgp_connected_add (struct connected *ifc)
543{
544 struct prefix p;
545 struct prefix *addr;
546 struct prefix *dest;
547 struct interface *ifp;
548 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000549 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000550
551 ifp = ifc->ifp;
552
553 if (! ifp)
554 return;
555
556 if (if_is_loopback (ifp))
557 return;
558
559 addr = ifc->address;
560 dest = ifc->destination;
561
562 if (addr->family == AF_INET)
563 {
564 memset (&p, 0, sizeof (struct prefix));
565 p.family = AF_INET;
566 p.prefixlen = addr->prefixlen;
567
hasso3fb9cd62004-10-19 19:44:43 +0000568 if (CONNECTED_POINTOPOINT_HOST(ifc))
paul718e3742002-12-13 20:15:29 +0000569 p.u.prefix4 = dest->u.prefix4;
570 else
571 p.u.prefix4 = addr->u.prefix4;
572
573 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
574
575 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
576 return;
577
paul59320202004-11-09 01:54:03 +0000578 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000579 if (rn->info)
580 {
581 bc = rn->info;
582 bc->refcnt++;
583 }
584 else
585 {
paul59320202004-11-09 01:54:03 +0000586 bc = XMALLOC (0, sizeof (struct bgp_connected_ref));
587 memset (bc, 0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000588 bc->refcnt = 1;
589 rn->info = bc;
590 }
591 }
592#ifdef HAVE_IPV6
593 if (addr->family == AF_INET6)
594 {
595 memset (&p, 0, sizeof (struct prefix));
596 p.family = AF_INET6;
597 p.prefixlen = addr->prefixlen;
598
hasso3fb9cd62004-10-19 19:44:43 +0000599 if (if_is_pointopoint (ifp) && dest)
paul718e3742002-12-13 20:15:29 +0000600 p.u.prefix6 = dest->u.prefix6;
601 else
602 p.u.prefix6 = addr->u.prefix6;
603
604 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
605
606 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
607 return;
608
609 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
610 return;
611
paul59320202004-11-09 01:54:03 +0000612 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000613 if (rn->info)
614 {
615 bc = rn->info;
616 bc->refcnt++;
617 }
618 else
619 {
paul59320202004-11-09 01:54:03 +0000620 bc = XMALLOC (0, sizeof (struct bgp_connected_ref));
621 memset (bc, 0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000622 bc->refcnt = 1;
623 rn->info = bc;
624 }
625 }
626#endif /* HAVE_IPV6 */
627}
628
629void
630bgp_connected_delete (struct connected *ifc)
631{
632 struct prefix p;
633 struct prefix *addr;
634 struct prefix *dest;
635 struct interface *ifp;
636 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000637 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000638
639 ifp = ifc->ifp;
640
641 if (if_is_loopback (ifp))
642 return;
643
644 addr = ifc->address;
645 dest = ifc->destination;
646
647 if (addr->family == AF_INET)
648 {
649 memset (&p, 0, sizeof (struct prefix));
650 p.family = AF_INET;
651 p.prefixlen = addr->prefixlen;
652
hasso3fb9cd62004-10-19 19:44:43 +0000653 if (CONNECTED_POINTOPOINT_HOST(ifc))
paul718e3742002-12-13 20:15:29 +0000654 p.u.prefix4 = dest->u.prefix4;
655 else
656 p.u.prefix4 = addr->u.prefix4;
657
658 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
659
660 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
661 return;
662
paul59320202004-11-09 01:54:03 +0000663 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &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#ifdef HAVE_IPV6
678 else if (addr->family == AF_INET6)
679 {
680 memset (&p, 0, sizeof (struct prefix));
681 p.family = AF_INET6;
682 p.prefixlen = addr->prefixlen;
683
hasso3fb9cd62004-10-19 19:44:43 +0000684 if (if_is_pointopoint (ifp) && dest)
paul718e3742002-12-13 20:15:29 +0000685 p.u.prefix6 = dest->u.prefix6;
686 else
687 p.u.prefix6 = addr->u.prefix6;
688
689 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
690
691 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
692 return;
693
694 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
695 return;
696
paul59320202004-11-09 01:54:03 +0000697 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000698 if (! rn)
699 return;
700
701 bc = rn->info;
702 bc->refcnt--;
703 if (bc->refcnt == 0)
704 {
705 XFREE (0, bc);
706 rn->info = NULL;
707 }
708 bgp_unlock_node (rn);
709 bgp_unlock_node (rn);
710 }
711#endif /* HAVE_IPV6 */
712}
713
714int
715bgp_nexthop_self (afi_t afi, struct attr *attr)
716{
hasso52dc7ee2004-09-23 19:18:23 +0000717 struct listnode *node;
718 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000719 struct interface *ifp;
720 struct connected *ifc;
721 struct prefix *p;
722
paul1eb8ef22005-04-07 07:30:20 +0000723 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000724 {
paul1eb8ef22005-04-07 07:30:20 +0000725 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000726 {
paul718e3742002-12-13 20:15:29 +0000727 p = ifc->address;
728
729 if (p && p->family == AF_INET
730 && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
731 return 1;
732 }
733 }
734 return 0;
735}
736
paul94f2b392005-06-28 12:44:16 +0000737static struct bgp_nexthop_cache *
paul718e3742002-12-13 20:15:29 +0000738zlookup_read ()
739{
740 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000741 uint16_t length;
742 u_char marker;
743 u_char version;
744 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000745 int nbytes;
746 struct in_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000747 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000748 int i;
749 u_char nexthop_num;
750 struct nexthop *nexthop;
751 struct bgp_nexthop_cache *bnc;
752
753 s = zlookup->ibuf;
754 stream_reset (s);
755
756 nbytes = stream_read (s, zlookup->sock, 2);
757 length = stream_getw (s);
758
759 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000760 marker = stream_getc (s);
761 version = stream_getc (s);
762
763 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
764 {
765 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
766 __func__, zlookup->sock, marker, version);
767 return NULL;
768 }
769
770 command = stream_getw (s);
771
paul718e3742002-12-13 20:15:29 +0000772 raddr.s_addr = stream_get_ipv4 (s);
773 metric = stream_getl (s);
774 nexthop_num = stream_getc (s);
775
776 if (nexthop_num)
777 {
778 bnc = bnc_new ();
779 bnc->valid = 1;
780 bnc->metric = metric;
781 bnc->nexthop_num = nexthop_num;
782
783 for (i = 0; i < nexthop_num; i++)
784 {
785 nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
786 memset (nexthop, 0, sizeof (struct nexthop));
787 nexthop->type = stream_getc (s);
788 switch (nexthop->type)
789 {
790 case ZEBRA_NEXTHOP_IPV4:
791 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
792 break;
793 case ZEBRA_NEXTHOP_IFINDEX:
794 case ZEBRA_NEXTHOP_IFNAME:
795 nexthop->ifindex = stream_getl (s);
796 break;
hassofa2b17e2004-03-04 17:45:00 +0000797 default:
798 /* do nothing */
799 break;
paul718e3742002-12-13 20:15:29 +0000800 }
801 bnc_nexthop_add (bnc, nexthop);
802 }
803 }
804 else
805 return NULL;
806
807 return bnc;
808}
809
810struct bgp_nexthop_cache *
811zlookup_query (struct in_addr addr)
812{
813 int ret;
814 struct stream *s;
815
816 /* Check socket. */
817 if (zlookup->sock < 0)
818 return NULL;
819
820 s = zlookup->obuf;
821 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000822 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000823 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000824
825 stream_putw_at (s, 0, stream_get_endp (s));
826
827 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000828 if (ret < 0)
829 {
830 zlog_err ("can't write to zlookup->sock");
831 close (zlookup->sock);
832 zlookup->sock = -1;
833 return NULL;
834 }
835 if (ret == 0)
836 {
837 zlog_err ("zlookup->sock connection closed");
838 close (zlookup->sock);
839 zlookup->sock = -1;
840 return NULL;
841 }
842
843 return zlookup_read ();
844}
845
846#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000847static struct bgp_nexthop_cache *
paul718e3742002-12-13 20:15:29 +0000848zlookup_read_ipv6 ()
849{
850 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000851 uint16_t length;
852 u_char version, marker;
853 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000854 int nbytes;
855 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000856 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000857 int i;
858 u_char nexthop_num;
859 struct nexthop *nexthop;
860 struct bgp_nexthop_cache *bnc;
861
862 s = zlookup->ibuf;
863 stream_reset (s);
864
865 nbytes = stream_read (s, zlookup->sock, 2);
866 length = stream_getw (s);
867
868 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000869 marker = stream_getc (s);
870 version = stream_getc (s);
871
872 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
873 {
874 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
875 __func__, zlookup->sock, marker, version);
876 return NULL;
877 }
878
879 command = stream_getw (s);
880
paul718e3742002-12-13 20:15:29 +0000881 stream_get (&raddr, s, 16);
882
883 metric = stream_getl (s);
884 nexthop_num = stream_getc (s);
885
886 if (nexthop_num)
887 {
888 bnc = bnc_new ();
889 bnc->valid = 1;
890 bnc->metric = metric;
891 bnc->nexthop_num = nexthop_num;
892
893 for (i = 0; i < nexthop_num; i++)
894 {
895 nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
896 memset (nexthop, 0, sizeof (struct nexthop));
897 nexthop->type = stream_getc (s);
898 switch (nexthop->type)
899 {
900 case ZEBRA_NEXTHOP_IPV6:
901 stream_get (&nexthop->gate.ipv6, s, 16);
902 break;
903 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
904 case ZEBRA_NEXTHOP_IPV6_IFNAME:
905 stream_get (&nexthop->gate.ipv6, s, 16);
906 nexthop->ifindex = stream_getl (s);
907 break;
908 case ZEBRA_NEXTHOP_IFINDEX:
909 case ZEBRA_NEXTHOP_IFNAME:
910 nexthop->ifindex = stream_getl (s);
911 break;
hassofa2b17e2004-03-04 17:45:00 +0000912 default:
913 /* do nothing */
914 break;
paul718e3742002-12-13 20:15:29 +0000915 }
916 bnc_nexthop_add (bnc, nexthop);
917 }
918 }
919 else
920 return NULL;
921
922 return bnc;
923}
924
925struct bgp_nexthop_cache *
926zlookup_query_ipv6 (struct in6_addr *addr)
927{
928 int ret;
929 struct stream *s;
930
931 /* Check socket. */
932 if (zlookup->sock < 0)
933 return NULL;
934
935 s = zlookup->obuf;
936 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000937 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000938 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000939 stream_putw_at (s, 0, stream_get_endp (s));
940
941 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000942 if (ret < 0)
943 {
944 zlog_err ("can't write to zlookup->sock");
945 close (zlookup->sock);
946 zlookup->sock = -1;
947 return NULL;
948 }
949 if (ret == 0)
950 {
951 zlog_err ("zlookup->sock connection closed");
952 close (zlookup->sock);
953 zlookup->sock = -1;
954 return NULL;
955 }
956
957 return zlookup_read_ipv6 ();
958}
959#endif /* HAVE_IPV6 */
960
paul94f2b392005-06-28 12:44:16 +0000961static int
962bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
963 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000964{
965 struct stream *s;
966 int ret;
pauld3092e72006-01-17 17:33:46 +0000967 u_int16_t length, command;
968 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000969 int nbytes;
970 struct in_addr addr;
971 struct in_addr nexthop;
972 u_int32_t metric = 0;
973 u_char nexthop_num;
974 u_char nexthop_type;
975
976 /* If lookup connection is not available return valid. */
977 if (zlookup->sock < 0)
978 {
979 if (igpmetric)
980 *igpmetric = 0;
981 return 1;
982 }
983
984 /* Send query to the lookup connection */
985 s = zlookup->obuf;
986 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000987 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
988
paul718e3742002-12-13 20:15:29 +0000989 stream_putc (s, p->prefixlen);
990 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +0000991
992 stream_putw_at (s, 0, stream_get_endp (s));
993
paul718e3742002-12-13 20:15:29 +0000994 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +0000995 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000996
997 if (ret < 0)
998 {
999 zlog_err ("can't write to zlookup->sock");
1000 close (zlookup->sock);
1001 zlookup->sock = -1;
1002 return 1;
1003 }
1004 if (ret == 0)
1005 {
1006 zlog_err ("zlookup->sock connection closed");
1007 close (zlookup->sock);
1008 zlookup->sock = -1;
1009 return 1;
1010 }
1011
1012 /* Get result. */
1013 stream_reset (s);
1014
1015 /* Fetch length. */
1016 nbytes = stream_read (s, zlookup->sock, 2);
1017 length = stream_getw (s);
1018
1019 /* Fetch whole data. */
1020 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +00001021 marker = stream_getc (s);
1022 version = stream_getc (s);
1023
1024 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
1025 {
1026 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1027 __func__, zlookup->sock, marker, version);
1028 return 0;
1029 }
1030
1031 command = stream_getw (s);
1032
paul718e3742002-12-13 20:15:29 +00001033 addr.s_addr = stream_get_ipv4 (s);
1034 metric = stream_getl (s);
1035 nexthop_num = stream_getc (s);
1036
1037 /* Set IGP metric value. */
1038 if (igpmetric)
1039 *igpmetric = metric;
1040
1041 /* If there is nexthop then this is active route. */
1042 if (nexthop_num)
1043 {
1044 nexthop.s_addr = 0;
1045 nexthop_type = stream_getc (s);
1046 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
1047 {
1048 nexthop.s_addr = stream_get_ipv4 (s);
1049 if (igpnexthop)
1050 *igpnexthop = nexthop;
1051 }
1052 else
1053 *igpnexthop = nexthop;
1054
1055 return 1;
1056 }
1057 else
1058 return 0;
1059}
1060
1061/* Scan all configured BGP route then check the route exists in IGP or
1062 not. */
paul94f2b392005-06-28 12:44:16 +00001063static int
paul718e3742002-12-13 20:15:29 +00001064bgp_import (struct thread *t)
1065{
1066 struct bgp *bgp;
1067 struct bgp_node *rn;
1068 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001069 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001070 int valid;
1071 u_int32_t metric;
1072 struct in_addr nexthop;
1073 afi_t afi;
1074 safi_t safi;
1075
1076 bgp_import_thread =
1077 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1078
hassof4184462005-02-01 20:13:16 +00001079 if (BGP_DEBUG (events, EVENTS))
1080 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001081
paul1eb8ef22005-04-07 07:30:20 +00001082 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001083 {
1084 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1085 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1086 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1087 rn = bgp_route_next (rn))
1088 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001089 {
paul6cbbc3c2003-04-28 17:11:02 +00001090 if (bgp_static->backdoor)
1091 continue;
paul718e3742002-12-13 20:15:29 +00001092
paul6cbbc3c2003-04-28 17:11:02 +00001093 valid = bgp_static->valid;
1094 metric = bgp_static->igpmetric;
1095 nexthop = bgp_static->igpnexthop;
1096
1097 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1098 && afi == AFI_IP && safi == SAFI_UNICAST)
1099 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1100 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001101 else
paul6cbbc3c2003-04-28 17:11:02 +00001102 {
1103 bgp_static->valid = 1;
1104 bgp_static->igpmetric = 0;
1105 bgp_static->igpnexthop.s_addr = 0;
1106 }
1107
1108 if (bgp_static->valid != valid)
1109 {
1110 if (bgp_static->valid)
1111 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1112 else
1113 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1114 }
1115 else if (bgp_static->valid)
1116 {
1117 if (bgp_static->igpmetric != metric
1118 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1119 || bgp_static->rmap.name)
1120 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1121 }
paul718e3742002-12-13 20:15:29 +00001122 }
paul6cbbc3c2003-04-28 17:11:02 +00001123 }
paul718e3742002-12-13 20:15:29 +00001124 return 0;
1125}
1126
1127/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001128static int
paul718e3742002-12-13 20:15:29 +00001129zlookup_connect (struct thread *t)
1130{
1131 struct zclient *zlookup;
1132
1133 zlookup = THREAD_ARG (t);
1134 zlookup->t_connect = NULL;
1135
1136 if (zlookup->sock != -1)
1137 return 0;
1138
1139#ifdef HAVE_TCP_ZEBRA
1140 zlookup->sock = zclient_socket ();
1141#else
1142 zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH);
1143#endif /* HAVE_TCP_ZEBRA */
1144 if (zlookup->sock < 0)
1145 return -1;
1146
paul718e3742002-12-13 20:15:29 +00001147 return 0;
1148}
1149
1150/* Check specified multiaccess next-hop. */
1151int
1152bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1153{
1154 struct bgp_node *rn1;
1155 struct bgp_node *rn2;
1156 struct prefix p1;
1157 struct prefix p2;
1158 struct in_addr addr;
1159 int ret;
1160
1161 ret = inet_aton (peer, &addr);
1162 if (! ret)
1163 return 0;
1164
1165 memset (&p1, 0, sizeof (struct prefix));
1166 p1.family = AF_INET;
1167 p1.prefixlen = IPV4_MAX_BITLEN;
1168 p1.u.prefix4 = nexthop;
1169 memset (&p2, 0, sizeof (struct prefix));
1170 p2.family = AF_INET;
1171 p2.prefixlen = IPV4_MAX_BITLEN;
1172 p2.u.prefix4 = addr;
1173
1174 /* If bgp scan is not enabled, return invalid. */
1175 if (zlookup->sock < 0)
1176 return 0;
1177
paul59320202004-11-09 01:54:03 +00001178 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001179 if (! rn1)
1180 return 0;
1181
paul59320202004-11-09 01:54:03 +00001182 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001183 if (! rn2)
1184 return 0;
1185
1186 if (rn1 == rn2)
1187 return 1;
1188
1189 return 0;
1190}
1191
1192DEFUN (bgp_scan_time,
1193 bgp_scan_time_cmd,
1194 "bgp scan-time <5-60>",
1195 "BGP specific commands\n"
1196 "Configure background scanner interval\n"
1197 "Scanner interval (seconds)\n")
1198{
1199 bgp_scan_interval = atoi (argv[0]);
1200
1201 if (bgp_scan_thread)
1202 {
1203 thread_cancel (bgp_scan_thread);
1204 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001205 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001206 }
1207
1208 return CMD_SUCCESS;
1209}
1210
1211DEFUN (no_bgp_scan_time,
1212 no_bgp_scan_time_cmd,
1213 "no bgp scan-time",
1214 NO_STR
1215 "BGP specific commands\n"
1216 "Configure background scanner interval\n")
1217{
1218 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1219
1220 if (bgp_scan_thread)
1221 {
1222 thread_cancel (bgp_scan_thread);
1223 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001224 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001225 }
1226
1227 return CMD_SUCCESS;
1228}
1229
1230ALIAS (no_bgp_scan_time,
1231 no_bgp_scan_time_val_cmd,
1232 "no bgp scan-time <5-60>",
1233 NO_STR
1234 "BGP specific commands\n"
1235 "Configure background scanner interval\n"
1236 "Scanner interval (seconds)\n")
1237
1238DEFUN (show_ip_bgp_scan,
1239 show_ip_bgp_scan_cmd,
1240 "show ip bgp scan",
1241 SHOW_STR
1242 IP_STR
1243 BGP_STR
1244 "BGP scan status\n")
1245{
1246 struct bgp_node *rn;
1247 struct bgp_nexthop_cache *bnc;
1248
1249 if (bgp_scan_thread)
1250 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1251 else
1252 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1253 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1254
1255 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001256 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001257 if ((bnc = rn->info) != NULL)
1258 {
1259 if (bnc->valid)
1260 vty_out (vty, " %s valid [IGP metric %d]%s",
1261 inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE);
1262 else
1263 vty_out (vty, " %s invalid%s",
1264 inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE);
1265 }
1266
1267#ifdef HAVE_IPV6
1268 {
1269 char buf[BUFSIZ];
paul59320202004-11-09 01:54:03 +00001270 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1271 rn;
1272 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001273 if ((bnc = rn->info) != NULL)
1274 {
1275 if (bnc->valid)
1276 vty_out (vty, " %s valid [IGP metric %d]%s",
1277 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1278 bnc->metric, VTY_NEWLINE);
1279 else
1280 vty_out (vty, " %s invalid%s",
1281 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1282 VTY_NEWLINE);
1283 }
1284 }
1285#endif /* HAVE_IPV6 */
1286
1287 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001288 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1289 rn;
1290 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001291 if (rn->info != NULL)
1292 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1293 VTY_NEWLINE);
1294
1295#ifdef HAVE_IPV6
1296 {
1297 char buf[BUFSIZ];
1298
paul59320202004-11-09 01:54:03 +00001299 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1300 rn;
1301 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001302 if (rn->info != NULL)
1303 vty_out (vty, " %s/%d%s",
1304 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1305 rn->p.prefixlen,
1306 VTY_NEWLINE);
1307 }
1308#endif /* HAVE_IPV6 */
1309
1310 return CMD_SUCCESS;
1311}
1312
1313int
1314bgp_config_write_scan_time (struct vty *vty)
1315{
1316 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1317 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1318 return CMD_SUCCESS;
1319}
1320
1321void
1322bgp_scan_init ()
1323{
1324 zlookup = zclient_new ();
1325 zlookup->sock = -1;
1326 zlookup->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1327 zlookup->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1328 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1329
1330 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1331 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1332
Paul Jakma64e580a2006-02-21 01:09:01 +00001333 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1334 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001335 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001336
Paul Jakma64e580a2006-02-21 01:09:01 +00001337 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001338
1339#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001340 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1341 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001342 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001343 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001344#endif /* HAVE_IPV6 */
1345
1346 /* Make BGP scan thread. */
paul59320202004-11-09 01:54:03 +00001347 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1348 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001349 /* Make BGP import there. */
1350 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001351
1352 install_element (BGP_NODE, &bgp_scan_time_cmd);
1353 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1354 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1355 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
1356 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
1357}