blob: 50ae74e45582d263efda1e441eeff6ee9e3f46cc [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;
paul718e3742002-12-13 20:15:29 +0000546 struct interface *ifp;
547 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000548 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000549
550 ifp = ifc->ifp;
551
552 if (! ifp)
553 return;
554
555 if (if_is_loopback (ifp))
556 return;
557
558 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000559
560 if (addr->family == AF_INET)
561 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000562 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000563 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
564
565 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
566 return;
567
paul59320202004-11-09 01:54:03 +0000568 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000569 if (rn->info)
570 {
571 bc = rn->info;
572 bc->refcnt++;
573 }
574 else
575 {
paul59320202004-11-09 01:54:03 +0000576 bc = XMALLOC (0, sizeof (struct bgp_connected_ref));
577 memset (bc, 0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000578 bc->refcnt = 1;
579 rn->info = bc;
580 }
581 }
582#ifdef HAVE_IPV6
Andrew J. Schorre4529632006-12-12 19:18:21 +0000583 else if (addr->family == AF_INET6)
paul718e3742002-12-13 20:15:29 +0000584 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000585 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000586 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
587
588 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
589 return;
590
591 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
592 return;
593
paul59320202004-11-09 01:54:03 +0000594 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000595 if (rn->info)
596 {
597 bc = rn->info;
598 bc->refcnt++;
599 }
600 else
601 {
paul59320202004-11-09 01:54:03 +0000602 bc = XMALLOC (0, sizeof (struct bgp_connected_ref));
603 memset (bc, 0, sizeof (struct bgp_connected_ref));
paul718e3742002-12-13 20:15:29 +0000604 bc->refcnt = 1;
605 rn->info = bc;
606 }
607 }
608#endif /* HAVE_IPV6 */
609}
610
611void
612bgp_connected_delete (struct connected *ifc)
613{
614 struct prefix p;
615 struct prefix *addr;
paul718e3742002-12-13 20:15:29 +0000616 struct interface *ifp;
617 struct bgp_node *rn;
paul59320202004-11-09 01:54:03 +0000618 struct bgp_connected_ref *bc;
paul718e3742002-12-13 20:15:29 +0000619
620 ifp = ifc->ifp;
621
622 if (if_is_loopback (ifp))
623 return;
624
625 addr = ifc->address;
paul718e3742002-12-13 20:15:29 +0000626
627 if (addr->family == AF_INET)
628 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000629 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000630 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
631
632 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
633 return;
634
paul59320202004-11-09 01:54:03 +0000635 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
paul718e3742002-12-13 20:15:29 +0000636 if (! rn)
637 return;
638
639 bc = rn->info;
640 bc->refcnt--;
641 if (bc->refcnt == 0)
642 {
643 XFREE (0, bc);
644 rn->info = NULL;
645 }
646 bgp_unlock_node (rn);
647 bgp_unlock_node (rn);
648 }
649#ifdef HAVE_IPV6
650 else if (addr->family == AF_INET6)
651 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000652 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000653 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
654
655 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
656 return;
657
658 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
659 return;
660
paul59320202004-11-09 01:54:03 +0000661 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000662 if (! rn)
663 return;
664
665 bc = rn->info;
666 bc->refcnt--;
667 if (bc->refcnt == 0)
668 {
669 XFREE (0, bc);
670 rn->info = NULL;
671 }
672 bgp_unlock_node (rn);
673 bgp_unlock_node (rn);
674 }
675#endif /* HAVE_IPV6 */
676}
677
678int
679bgp_nexthop_self (afi_t afi, struct attr *attr)
680{
hasso52dc7ee2004-09-23 19:18:23 +0000681 struct listnode *node;
682 struct listnode *node2;
paul718e3742002-12-13 20:15:29 +0000683 struct interface *ifp;
684 struct connected *ifc;
685 struct prefix *p;
686
paul1eb8ef22005-04-07 07:30:20 +0000687 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
paul718e3742002-12-13 20:15:29 +0000688 {
paul1eb8ef22005-04-07 07:30:20 +0000689 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
paul718e3742002-12-13 20:15:29 +0000690 {
paul718e3742002-12-13 20:15:29 +0000691 p = ifc->address;
692
693 if (p && p->family == AF_INET
694 && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
695 return 1;
696 }
697 }
698 return 0;
699}
700
paul94f2b392005-06-28 12:44:16 +0000701static struct bgp_nexthop_cache *
paul718e3742002-12-13 20:15:29 +0000702zlookup_read ()
703{
704 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000705 uint16_t length;
706 u_char marker;
707 u_char version;
708 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000709 int nbytes;
710 struct in_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000711 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000712 int i;
713 u_char nexthop_num;
714 struct nexthop *nexthop;
715 struct bgp_nexthop_cache *bnc;
716
717 s = zlookup->ibuf;
718 stream_reset (s);
719
720 nbytes = stream_read (s, zlookup->sock, 2);
721 length = stream_getw (s);
722
723 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000724 marker = stream_getc (s);
725 version = stream_getc (s);
726
727 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
728 {
729 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
730 __func__, zlookup->sock, marker, version);
731 return NULL;
732 }
733
734 command = stream_getw (s);
735
paul718e3742002-12-13 20:15:29 +0000736 raddr.s_addr = stream_get_ipv4 (s);
737 metric = stream_getl (s);
738 nexthop_num = stream_getc (s);
739
740 if (nexthop_num)
741 {
742 bnc = bnc_new ();
743 bnc->valid = 1;
744 bnc->metric = metric;
745 bnc->nexthop_num = nexthop_num;
746
747 for (i = 0; i < nexthop_num; i++)
748 {
749 nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
750 memset (nexthop, 0, sizeof (struct nexthop));
751 nexthop->type = stream_getc (s);
752 switch (nexthop->type)
753 {
754 case ZEBRA_NEXTHOP_IPV4:
755 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
756 break;
757 case ZEBRA_NEXTHOP_IFINDEX:
758 case ZEBRA_NEXTHOP_IFNAME:
759 nexthop->ifindex = stream_getl (s);
760 break;
hassofa2b17e2004-03-04 17:45:00 +0000761 default:
762 /* do nothing */
763 break;
paul718e3742002-12-13 20:15:29 +0000764 }
765 bnc_nexthop_add (bnc, nexthop);
766 }
767 }
768 else
769 return NULL;
770
771 return bnc;
772}
773
774struct bgp_nexthop_cache *
775zlookup_query (struct in_addr addr)
776{
777 int ret;
778 struct stream *s;
779
780 /* Check socket. */
781 if (zlookup->sock < 0)
782 return NULL;
783
784 s = zlookup->obuf;
785 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000786 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000787 stream_put_in_addr (s, &addr);
pauld3092e72006-01-17 17:33:46 +0000788
789 stream_putw_at (s, 0, stream_get_endp (s));
790
791 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000792 if (ret < 0)
793 {
794 zlog_err ("can't write to zlookup->sock");
795 close (zlookup->sock);
796 zlookup->sock = -1;
797 return NULL;
798 }
799 if (ret == 0)
800 {
801 zlog_err ("zlookup->sock connection closed");
802 close (zlookup->sock);
803 zlookup->sock = -1;
804 return NULL;
805 }
806
807 return zlookup_read ();
808}
809
810#ifdef HAVE_IPV6
paul94f2b392005-06-28 12:44:16 +0000811static struct bgp_nexthop_cache *
paul718e3742002-12-13 20:15:29 +0000812zlookup_read_ipv6 ()
813{
814 struct stream *s;
pauld3092e72006-01-17 17:33:46 +0000815 uint16_t length;
816 u_char version, marker;
817 uint16_t command;
paul718e3742002-12-13 20:15:29 +0000818 int nbytes;
819 struct in6_addr raddr;
pauld3092e72006-01-17 17:33:46 +0000820 uint32_t metric;
paul718e3742002-12-13 20:15:29 +0000821 int i;
822 u_char nexthop_num;
823 struct nexthop *nexthop;
824 struct bgp_nexthop_cache *bnc;
825
826 s = zlookup->ibuf;
827 stream_reset (s);
828
829 nbytes = stream_read (s, zlookup->sock, 2);
830 length = stream_getw (s);
831
832 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000833 marker = stream_getc (s);
834 version = stream_getc (s);
835
836 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
837 {
838 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
839 __func__, zlookup->sock, marker, version);
840 return NULL;
841 }
842
843 command = stream_getw (s);
844
paul718e3742002-12-13 20:15:29 +0000845 stream_get (&raddr, s, 16);
846
847 metric = stream_getl (s);
848 nexthop_num = stream_getc (s);
849
850 if (nexthop_num)
851 {
852 bnc = bnc_new ();
853 bnc->valid = 1;
854 bnc->metric = metric;
855 bnc->nexthop_num = nexthop_num;
856
857 for (i = 0; i < nexthop_num; i++)
858 {
859 nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
860 memset (nexthop, 0, sizeof (struct nexthop));
861 nexthop->type = stream_getc (s);
862 switch (nexthop->type)
863 {
864 case ZEBRA_NEXTHOP_IPV6:
865 stream_get (&nexthop->gate.ipv6, s, 16);
866 break;
867 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
868 case ZEBRA_NEXTHOP_IPV6_IFNAME:
869 stream_get (&nexthop->gate.ipv6, s, 16);
870 nexthop->ifindex = stream_getl (s);
871 break;
872 case ZEBRA_NEXTHOP_IFINDEX:
873 case ZEBRA_NEXTHOP_IFNAME:
874 nexthop->ifindex = stream_getl (s);
875 break;
hassofa2b17e2004-03-04 17:45:00 +0000876 default:
877 /* do nothing */
878 break;
paul718e3742002-12-13 20:15:29 +0000879 }
880 bnc_nexthop_add (bnc, nexthop);
881 }
882 }
883 else
884 return NULL;
885
886 return bnc;
887}
888
889struct bgp_nexthop_cache *
890zlookup_query_ipv6 (struct in6_addr *addr)
891{
892 int ret;
893 struct stream *s;
894
895 /* Check socket. */
896 if (zlookup->sock < 0)
897 return NULL;
898
899 s = zlookup->obuf;
900 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000901 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000902 stream_put (s, addr, 16);
pauld3092e72006-01-17 17:33:46 +0000903 stream_putw_at (s, 0, stream_get_endp (s));
904
905 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000906 if (ret < 0)
907 {
908 zlog_err ("can't write to zlookup->sock");
909 close (zlookup->sock);
910 zlookup->sock = -1;
911 return NULL;
912 }
913 if (ret == 0)
914 {
915 zlog_err ("zlookup->sock connection closed");
916 close (zlookup->sock);
917 zlookup->sock = -1;
918 return NULL;
919 }
920
921 return zlookup_read_ipv6 ();
922}
923#endif /* HAVE_IPV6 */
924
paul94f2b392005-06-28 12:44:16 +0000925static int
926bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
927 struct in_addr *igpnexthop)
paul718e3742002-12-13 20:15:29 +0000928{
929 struct stream *s;
930 int ret;
pauld3092e72006-01-17 17:33:46 +0000931 u_int16_t length, command;
932 u_char version, marker;
paul718e3742002-12-13 20:15:29 +0000933 int nbytes;
934 struct in_addr addr;
935 struct in_addr nexthop;
936 u_int32_t metric = 0;
937 u_char nexthop_num;
938 u_char nexthop_type;
939
940 /* If lookup connection is not available return valid. */
941 if (zlookup->sock < 0)
942 {
943 if (igpmetric)
944 *igpmetric = 0;
945 return 1;
946 }
947
948 /* Send query to the lookup connection */
949 s = zlookup->obuf;
950 stream_reset (s);
pauld3092e72006-01-17 17:33:46 +0000951 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
952
paul718e3742002-12-13 20:15:29 +0000953 stream_putc (s, p->prefixlen);
954 stream_put_in_addr (s, &p->u.prefix4);
pauld3092e72006-01-17 17:33:46 +0000955
956 stream_putw_at (s, 0, stream_get_endp (s));
957
paul718e3742002-12-13 20:15:29 +0000958 /* Write the packet. */
pauld3092e72006-01-17 17:33:46 +0000959 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +0000960
961 if (ret < 0)
962 {
963 zlog_err ("can't write to zlookup->sock");
964 close (zlookup->sock);
965 zlookup->sock = -1;
966 return 1;
967 }
968 if (ret == 0)
969 {
970 zlog_err ("zlookup->sock connection closed");
971 close (zlookup->sock);
972 zlookup->sock = -1;
973 return 1;
974 }
975
976 /* Get result. */
977 stream_reset (s);
978
979 /* Fetch length. */
980 nbytes = stream_read (s, zlookup->sock, 2);
981 length = stream_getw (s);
982
983 /* Fetch whole data. */
984 nbytes = stream_read (s, zlookup->sock, length - 2);
pauld3092e72006-01-17 17:33:46 +0000985 marker = stream_getc (s);
986 version = stream_getc (s);
987
988 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
989 {
990 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
991 __func__, zlookup->sock, marker, version);
992 return 0;
993 }
994
995 command = stream_getw (s);
996
paul718e3742002-12-13 20:15:29 +0000997 addr.s_addr = stream_get_ipv4 (s);
998 metric = stream_getl (s);
999 nexthop_num = stream_getc (s);
1000
1001 /* Set IGP metric value. */
1002 if (igpmetric)
1003 *igpmetric = metric;
1004
1005 /* If there is nexthop then this is active route. */
1006 if (nexthop_num)
1007 {
1008 nexthop.s_addr = 0;
1009 nexthop_type = stream_getc (s);
1010 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
1011 {
1012 nexthop.s_addr = stream_get_ipv4 (s);
1013 if (igpnexthop)
1014 *igpnexthop = nexthop;
1015 }
1016 else
1017 *igpnexthop = nexthop;
1018
1019 return 1;
1020 }
1021 else
1022 return 0;
1023}
1024
1025/* Scan all configured BGP route then check the route exists in IGP or
1026 not. */
paul94f2b392005-06-28 12:44:16 +00001027static int
paul718e3742002-12-13 20:15:29 +00001028bgp_import (struct thread *t)
1029{
1030 struct bgp *bgp;
1031 struct bgp_node *rn;
1032 struct bgp_static *bgp_static;
paul1eb8ef22005-04-07 07:30:20 +00001033 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001034 int valid;
1035 u_int32_t metric;
1036 struct in_addr nexthop;
1037 afi_t afi;
1038 safi_t safi;
1039
1040 bgp_import_thread =
1041 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1042
hassof4184462005-02-01 20:13:16 +00001043 if (BGP_DEBUG (events, EVENTS))
1044 zlog_debug ("Import timer expired.");
paul718e3742002-12-13 20:15:29 +00001045
paul1eb8ef22005-04-07 07:30:20 +00001046 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
paul6cbbc3c2003-04-28 17:11:02 +00001047 {
1048 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1049 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1050 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1051 rn = bgp_route_next (rn))
1052 if ((bgp_static = rn->info) != NULL)
paul718e3742002-12-13 20:15:29 +00001053 {
paul6cbbc3c2003-04-28 17:11:02 +00001054 if (bgp_static->backdoor)
1055 continue;
paul718e3742002-12-13 20:15:29 +00001056
paul6cbbc3c2003-04-28 17:11:02 +00001057 valid = bgp_static->valid;
1058 metric = bgp_static->igpmetric;
1059 nexthop = bgp_static->igpnexthop;
1060
1061 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1062 && afi == AFI_IP && safi == SAFI_UNICAST)
1063 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1064 &bgp_static->igpnexthop);
paul718e3742002-12-13 20:15:29 +00001065 else
paul6cbbc3c2003-04-28 17:11:02 +00001066 {
1067 bgp_static->valid = 1;
1068 bgp_static->igpmetric = 0;
1069 bgp_static->igpnexthop.s_addr = 0;
1070 }
1071
1072 if (bgp_static->valid != valid)
1073 {
1074 if (bgp_static->valid)
1075 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1076 else
1077 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1078 }
1079 else if (bgp_static->valid)
1080 {
1081 if (bgp_static->igpmetric != metric
1082 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1083 || bgp_static->rmap.name)
1084 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1085 }
paul718e3742002-12-13 20:15:29 +00001086 }
paul6cbbc3c2003-04-28 17:11:02 +00001087 }
paul718e3742002-12-13 20:15:29 +00001088 return 0;
1089}
1090
1091/* Connect to zebra for nexthop lookup. */
paul94f2b392005-06-28 12:44:16 +00001092static int
paul718e3742002-12-13 20:15:29 +00001093zlookup_connect (struct thread *t)
1094{
1095 struct zclient *zlookup;
1096
1097 zlookup = THREAD_ARG (t);
1098 zlookup->t_connect = NULL;
1099
1100 if (zlookup->sock != -1)
1101 return 0;
1102
1103#ifdef HAVE_TCP_ZEBRA
1104 zlookup->sock = zclient_socket ();
1105#else
1106 zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH);
1107#endif /* HAVE_TCP_ZEBRA */
1108 if (zlookup->sock < 0)
1109 return -1;
1110
paul718e3742002-12-13 20:15:29 +00001111 return 0;
1112}
1113
1114/* Check specified multiaccess next-hop. */
1115int
1116bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1117{
1118 struct bgp_node *rn1;
1119 struct bgp_node *rn2;
1120 struct prefix p1;
1121 struct prefix p2;
1122 struct in_addr addr;
1123 int ret;
1124
1125 ret = inet_aton (peer, &addr);
1126 if (! ret)
1127 return 0;
1128
1129 memset (&p1, 0, sizeof (struct prefix));
1130 p1.family = AF_INET;
1131 p1.prefixlen = IPV4_MAX_BITLEN;
1132 p1.u.prefix4 = nexthop;
1133 memset (&p2, 0, sizeof (struct prefix));
1134 p2.family = AF_INET;
1135 p2.prefixlen = IPV4_MAX_BITLEN;
1136 p2.u.prefix4 = addr;
1137
1138 /* If bgp scan is not enabled, return invalid. */
1139 if (zlookup->sock < 0)
1140 return 0;
1141
paul59320202004-11-09 01:54:03 +00001142 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
paul718e3742002-12-13 20:15:29 +00001143 if (! rn1)
1144 return 0;
1145
paul59320202004-11-09 01:54:03 +00001146 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
paul718e3742002-12-13 20:15:29 +00001147 if (! rn2)
1148 return 0;
1149
1150 if (rn1 == rn2)
1151 return 1;
1152
1153 return 0;
1154}
1155
1156DEFUN (bgp_scan_time,
1157 bgp_scan_time_cmd,
1158 "bgp scan-time <5-60>",
1159 "BGP specific commands\n"
1160 "Configure background scanner interval\n"
1161 "Scanner interval (seconds)\n")
1162{
1163 bgp_scan_interval = atoi (argv[0]);
1164
1165 if (bgp_scan_thread)
1166 {
1167 thread_cancel (bgp_scan_thread);
1168 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001169 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001170 }
1171
1172 return CMD_SUCCESS;
1173}
1174
1175DEFUN (no_bgp_scan_time,
1176 no_bgp_scan_time_cmd,
1177 "no bgp scan-time",
1178 NO_STR
1179 "BGP specific commands\n"
1180 "Configure background scanner interval\n")
1181{
1182 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1183
1184 if (bgp_scan_thread)
1185 {
1186 thread_cancel (bgp_scan_thread);
1187 bgp_scan_thread =
paul59320202004-11-09 01:54:03 +00001188 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
paul718e3742002-12-13 20:15:29 +00001189 }
1190
1191 return CMD_SUCCESS;
1192}
1193
1194ALIAS (no_bgp_scan_time,
1195 no_bgp_scan_time_val_cmd,
1196 "no bgp scan-time <5-60>",
1197 NO_STR
1198 "BGP specific commands\n"
1199 "Configure background scanner interval\n"
1200 "Scanner interval (seconds)\n")
1201
1202DEFUN (show_ip_bgp_scan,
1203 show_ip_bgp_scan_cmd,
1204 "show ip bgp scan",
1205 SHOW_STR
1206 IP_STR
1207 BGP_STR
1208 "BGP scan status\n")
1209{
1210 struct bgp_node *rn;
1211 struct bgp_nexthop_cache *bnc;
1212
1213 if (bgp_scan_thread)
1214 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1215 else
1216 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1217 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1218
1219 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001220 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001221 if ((bnc = rn->info) != NULL)
1222 {
1223 if (bnc->valid)
1224 vty_out (vty, " %s valid [IGP metric %d]%s",
1225 inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE);
1226 else
1227 vty_out (vty, " %s invalid%s",
1228 inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE);
1229 }
1230
1231#ifdef HAVE_IPV6
1232 {
1233 char buf[BUFSIZ];
paul59320202004-11-09 01:54:03 +00001234 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1235 rn;
1236 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001237 if ((bnc = rn->info) != NULL)
1238 {
1239 if (bnc->valid)
1240 vty_out (vty, " %s valid [IGP metric %d]%s",
1241 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1242 bnc->metric, VTY_NEWLINE);
1243 else
1244 vty_out (vty, " %s invalid%s",
1245 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1246 VTY_NEWLINE);
1247 }
1248 }
1249#endif /* HAVE_IPV6 */
1250
1251 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
paul59320202004-11-09 01:54:03 +00001252 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1253 rn;
1254 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001255 if (rn->info != NULL)
1256 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1257 VTY_NEWLINE);
1258
1259#ifdef HAVE_IPV6
1260 {
1261 char buf[BUFSIZ];
1262
paul59320202004-11-09 01:54:03 +00001263 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1264 rn;
1265 rn = bgp_route_next (rn))
paul718e3742002-12-13 20:15:29 +00001266 if (rn->info != NULL)
1267 vty_out (vty, " %s/%d%s",
1268 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),
1269 rn->p.prefixlen,
1270 VTY_NEWLINE);
1271 }
1272#endif /* HAVE_IPV6 */
1273
1274 return CMD_SUCCESS;
1275}
1276
1277int
1278bgp_config_write_scan_time (struct vty *vty)
1279{
1280 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1281 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1282 return CMD_SUCCESS;
1283}
1284
1285void
1286bgp_scan_init ()
1287{
1288 zlookup = zclient_new ();
1289 zlookup->sock = -1;
1290 zlookup->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1291 zlookup->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1292 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1293
1294 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1295 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1296
Paul Jakma64e580a2006-02-21 01:09:01 +00001297 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1298 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001299 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
paul718e3742002-12-13 20:15:29 +00001300
Paul Jakma64e580a2006-02-21 01:09:01 +00001301 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001302
1303#ifdef HAVE_IPV6
Paul Jakma64e580a2006-02-21 01:09:01 +00001304 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1305 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul59320202004-11-09 01:54:03 +00001306 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
Paul Jakma64e580a2006-02-21 01:09:01 +00001307 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
paul718e3742002-12-13 20:15:29 +00001308#endif /* HAVE_IPV6 */
1309
1310 /* Make BGP scan thread. */
paul59320202004-11-09 01:54:03 +00001311 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1312 NULL, bgp_scan_interval);
paul6cbbc3c2003-04-28 17:11:02 +00001313 /* Make BGP import there. */
1314 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
paul718e3742002-12-13 20:15:29 +00001315
1316 install_element (BGP_NODE, &bgp_scan_time_cmd);
1317 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1318 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1319 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
1320 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
1321}