]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_nexthop.c
zebra: zebra-nht-routemap.patch
[mirror_frr.git] / bgpd / bgp_nexthop.c
CommitLineData
718e3744 1/* 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"
10f9bf3f
JBD
31#include "hash.h"
32#include "jhash.h"
fb018d25 33#include "nexthop.h"
718e3744 34
35#include "bgpd/bgpd.h"
36#include "bgpd/bgp_table.h"
37#include "bgpd/bgp_route.h"
38#include "bgpd/bgp_attr.h"
39#include "bgpd/bgp_nexthop.h"
fb018d25 40#include "bgpd/bgp_nht.h"
718e3744 41#include "bgpd/bgp_debug.h"
42#include "bgpd/bgp_damp.h"
43#include "zebra/rib.h"
44#include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
45
fb018d25
DS
46extern struct zclient *zclient;
47
718e3744 48struct bgp_nexthop_cache *zlookup_query (struct in_addr);
49#ifdef HAVE_IPV6
50struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *);
51#endif /* HAVE_IPV6 */
6b0655a2 52
718e3744 53/* Only one BGP scan thread are activated at the same time. */
00d252cb 54static struct thread *bgp_scan_thread = NULL;
718e3744 55
56/* BGP import thread */
00d252cb 57static struct thread *bgp_import_thread = NULL;
718e3744 58
59/* BGP scan interval. */
00d252cb 60static int bgp_scan_interval;
718e3744 61
62/* BGP import interval. */
00d252cb 63static int bgp_import_interval;
718e3744 64
65/* Route table for next-hop lookup cache. */
fb018d25 66struct bgp_table *bgp_nexthop_cache_table[AFI_MAX];
00d252cb 67static struct bgp_table *cache1_table[AFI_MAX];
68static struct bgp_table *cache2_table[AFI_MAX];
718e3744 69
70/* Route table for connected route. */
00d252cb 71static struct bgp_table *bgp_connected_table[AFI_MAX];
718e3744 72
73/* BGP nexthop lookup query client. */
228da428 74struct zclient *zlookup = NULL;
6b0655a2 75
fb018d25
DS
76char *
77bnc_str (struct bgp_nexthop_cache *bnc, char *buf, int size)
78{
79 prefix2str(&(bnc->node->p), buf, size);
80 return buf;
81}
82
718e3744 83/* Add nexthop to the end of the list. */
94f2b392 84static void
718e3744 85bnc_nexthop_add (struct bgp_nexthop_cache *bnc, struct nexthop *nexthop)
86{
87 struct nexthop *last;
88
89 for (last = bnc->nexthop; last && last->next; last = last->next)
90 ;
91 if (last)
92 last->next = nexthop;
93 else
94 bnc->nexthop = nexthop;
95 nexthop->prev = last;
96}
97
fb018d25 98void
718e3744 99bnc_nexthop_free (struct bgp_nexthop_cache *bnc)
100{
101 struct nexthop *nexthop;
102 struct nexthop *next = NULL;
103
104 for (nexthop = bnc->nexthop; nexthop; nexthop = next)
105 {
106 next = nexthop->next;
107 XFREE (MTYPE_NEXTHOP, nexthop);
108 }
109}
110
fb018d25
DS
111struct bgp_nexthop_cache *
112bnc_new ()
718e3744 113{
fb018d25
DS
114 struct bgp_nexthop_cache *bnc;
115
116 bnc = XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
117 LIST_INIT(&(bnc->paths));
118 return bnc;
718e3744 119}
120
fb018d25 121void
718e3744 122bnc_free (struct bgp_nexthop_cache *bnc)
123{
124 bnc_nexthop_free (bnc);
125 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
126}
6b0655a2 127
94f2b392 128static int
8e80bdf2 129bgp_nexthop_cache_different (struct bgp_nexthop_cache *bnc1,
718e3744 130 struct bgp_nexthop_cache *bnc2)
131{
132 int i;
133 struct nexthop *next1, *next2;
134
135 if (bnc1->nexthop_num != bnc2->nexthop_num)
136 return 1;
137
138 next1 = bnc1->nexthop;
139 next2 = bnc2->nexthop;
140
141 for (i = 0; i < bnc1->nexthop_num; i++)
142 {
fb018d25 143 if (! nexthop_same_no_recurse (next1, next2))
718e3744 144 return 1;
145
146 next1 = next1->next;
147 next2 = next2->next;
148 }
149 return 0;
150}
151
152/* If nexthop exists on connected network return 1. */
153int
8e80bdf2 154bgp_nexthop_onlink (afi_t afi, struct attr *attr)
718e3744 155{
156 struct bgp_node *rn;
fc98d16e
PJ
157
158 /* If zebra is not enabled return */
159 if (zlookup->sock < 0)
160 return 1;
161
718e3744 162 /* Lookup the address is onlink or not. */
163 if (afi == AFI_IP)
164 {
5932020b 165 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
718e3744 166 if (rn)
167 {
168 bgp_unlock_node (rn);
169 return 1;
170 }
171 }
172#ifdef HAVE_IPV6
173 else if (afi == AFI_IP6)
174 {
fb982c25 175 if (attr->extra->mp_nexthop_len == 32)
718e3744 176 return 1;
fb982c25 177 else if (attr->extra->mp_nexthop_len == 16)
718e3744 178 {
fb982c25 179 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
718e3744 180 return 1;
181
5932020b 182 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
fb982c25 183 &attr->extra->mp_nexthop_global);
718e3744 184 if (rn)
185 {
186 bgp_unlock_node (rn);
187 return 1;
188 }
189 }
190 }
191#endif /* HAVE_IPV6 */
192 return 0;
193}
194
195#ifdef HAVE_IPV6
196/* Check specified next-hop is reachable or not. */
94f2b392 197static int
718e3744 198bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
199 int *metricchanged)
200{
201 struct bgp_node *rn;
202 struct prefix p;
203 struct bgp_nexthop_cache *bnc;
204 struct attr *attr;
fc98d16e
PJ
205
206 /* If lookup is not enabled, return valid. */
207 if (zlookup->sock < 0)
208 {
209 if (ri->extra)
210 ri->extra->igpmetric = 0;
211 return 1;
212 }
213
718e3744 214 /* Only check IPv6 global address only nexthop. */
215 attr = ri->attr;
216
fb982c25
PJ
217 if (attr->extra->mp_nexthop_len != 16
218 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
718e3744 219 return 1;
220
221 memset (&p, 0, sizeof (struct prefix));
222 p.family = AF_INET6;
223 p.prefixlen = IPV6_MAX_BITLEN;
fb982c25 224 p.u.prefix6 = attr->extra->mp_nexthop_global;
718e3744 225
226 /* IBGP or ebgp-multihop */
5932020b 227 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
718e3744 228
229 if (rn->info)
230 {
231 bnc = rn->info;
232 bgp_unlock_node (rn);
233 }
234 else
235 {
b64bfc1c
DO
236 if (NULL == (bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global)))
237 bnc = bnc_new ();
238 else
718e3744 239 {
718e3744 240 if (changed)
241 {
8e80bdf2
DO
242 struct bgp_table *old;
243 struct bgp_node *oldrn;
244
5932020b 245 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
246 old = cache2_table[AFI_IP6];
718e3744 247 else
5932020b 248 old = cache1_table[AFI_IP6];
718e3744 249
250 oldrn = bgp_node_lookup (old, &p);
251 if (oldrn)
252 {
8e80bdf2 253 struct bgp_nexthop_cache *oldbnc = oldrn->info;
718e3744 254
8e80bdf2 255 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
718e3744 256
257 if (bnc->metric != oldbnc->metric)
258 bnc->metricchanged = 1;
6c88b44d
CC
259
260 bgp_unlock_node (oldrn);
718e3744 261 }
262 }
263 }
718e3744 264 rn->info = bnc;
265 }
266
267 if (changed)
268 *changed = bnc->changed;
269
270 if (metricchanged)
271 *metricchanged = bnc->metricchanged;
272
fb982c25
PJ
273 if (bnc->valid && bnc->metric)
274 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
275 else if (ri->extra)
276 ri->extra->igpmetric = 0;
718e3744 277
278 return bnc->valid;
279}
280#endif /* HAVE_IPV6 */
281
282/* Check specified next-hop is reachable or not. */
283int
284bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
285 int *changed, int *metricchanged)
286{
287 struct bgp_node *rn;
288 struct prefix p;
289 struct bgp_nexthop_cache *bnc;
290 struct in_addr addr;
fc98d16e
PJ
291
292 /* If lookup is not enabled, return valid. */
293 if (zlookup->sock < 0)
294 {
295 if (ri->extra)
296 ri->extra->igpmetric = 0;
297 return 1;
298 }
299
718e3744 300#ifdef HAVE_IPV6
301 if (afi == AFI_IP6)
302 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
303#endif /* HAVE_IPV6 */
304
305 addr = ri->attr->nexthop;
306
307 memset (&p, 0, sizeof (struct prefix));
308 p.family = AF_INET;
309 p.prefixlen = IPV4_MAX_BITLEN;
310 p.u.prefix4 = addr;
311
312 /* IBGP or ebgp-multihop */
5932020b 313 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
718e3744 314
315 if (rn->info)
316 {
317 bnc = rn->info;
318 bgp_unlock_node (rn);
319 }
320 else
321 {
b64bfc1c
DO
322 if (NULL == (bnc = zlookup_query (addr)))
323 bnc = bnc_new ();
324 else
718e3744 325 {
718e3744 326 if (changed)
327 {
8e80bdf2
DO
328 struct bgp_table *old;
329 struct bgp_node *oldrn;
330
5932020b 331 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
332 old = cache2_table[AFI_IP];
718e3744 333 else
5932020b 334 old = cache1_table[AFI_IP];
718e3744 335
336 oldrn = bgp_node_lookup (old, &p);
337 if (oldrn)
338 {
8e80bdf2 339 struct bgp_nexthop_cache *oldbnc = oldrn->info;
718e3744 340
8e80bdf2 341 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
718e3744 342
343 if (bnc->metric != oldbnc->metric)
344 bnc->metricchanged = 1;
6c88b44d
CC
345
346 bgp_unlock_node (oldrn);
718e3744 347 }
348 }
349 }
718e3744 350 rn->info = bnc;
351 }
352
353 if (changed)
354 *changed = bnc->changed;
355
356 if (metricchanged)
357 *metricchanged = bnc->metricchanged;
358
fb982c25
PJ
359 if (bnc->valid && bnc->metric)
360 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
361 else if (ri->extra)
362 ri->extra->igpmetric = 0;
718e3744 363
364 return bnc->valid;
365}
366
367/* Reset and free all BGP nexthop cache. */
94f2b392 368static void
718e3744 369bgp_nexthop_cache_reset (struct bgp_table *table)
370{
371 struct bgp_node *rn;
372 struct bgp_nexthop_cache *bnc;
373
374 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
375 if ((bnc = rn->info) != NULL)
376 {
377 bnc_free (bnc);
378 rn->info = NULL;
379 bgp_unlock_node (rn);
380 }
381}
382
94f2b392 383static void
5932020b 384bgp_scan (afi_t afi, safi_t safi)
718e3744 385{
386 struct bgp_node *rn;
387 struct bgp *bgp;
388 struct bgp_info *bi;
389 struct bgp_info *next;
390 struct peer *peer;
1eb8ef25 391 struct listnode *node, *nnode;
fb018d25 392#if BGP_SCAN_NEXTHOP
718e3744 393 int valid;
394 int current;
395 int changed;
396 int metricchanged;
397
398 /* Change cache. */
5932020b 399 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
400 bgp_nexthop_cache_table[afi] = cache2_table[afi];
718e3744 401 else
5932020b 402 bgp_nexthop_cache_table[afi] = cache1_table[afi];
fb018d25 403#endif
718e3744 404
405 /* Get default bgp. */
406 bgp = bgp_get_default ();
407 if (bgp == NULL)
408 return;
409
410 /* Maximum prefix check */
1eb8ef25 411 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
718e3744 412 {
413 if (peer->status != Established)
414 continue;
415
5932020b 416 if (peer->afc[afi][SAFI_UNICAST])
417 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
418 if (peer->afc[afi][SAFI_MULTICAST])
419 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
420 if (peer->afc[afi][SAFI_MPLS_VPN])
421 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
718e3744 422 }
423
5932020b 424 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
718e3744 425 rn = bgp_route_next (rn))
426 {
427 for (bi = rn->info; bi; bi = next)
428 {
429 next = bi->next;
430
431 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
432 {
fb018d25 433#if BGP_SCAN_NEXTHOP
718e3744 434 changed = 0;
435 metricchanged = 0;
436
a0f6ce5b
CF
437 if (bi->peer->sort == BGP_PEER_EBGP && bi->peer->ttl == 1
438 && !CHECK_FLAG(bi->peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
8e80bdf2 439 valid = bgp_nexthop_onlink (afi, bi->attr);
718e3744 440 else
5932020b 441 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
718e3744 442 &changed, &metricchanged);
443
444 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
445
446 if (changed)
447 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
448 else
449 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
450
451 if (valid != current)
452 {
453 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
454 {
455 bgp_aggregate_decrement (bgp, &rn->p, bi,
5932020b 456 afi, SAFI_UNICAST);
1a392d46 457 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
718e3744 458 }
459 else
460 {
1a392d46 461 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
718e3744 462 bgp_aggregate_increment (bgp, &rn->p, bi,
5932020b 463 afi, SAFI_UNICAST);
718e3744 464 }
465 }
fb018d25 466#endif
718e3744 467
5932020b 468 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
718e3744 469 BGP_CONFIG_DAMPENING)
fb982c25 470 && bi->extra && bi->extra->damp_info )
5932020b 471 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
718e3744 472 bgp_aggregate_increment (bgp, &rn->p, bi,
5932020b 473 afi, SAFI_UNICAST);
718e3744 474 }
475 }
5932020b 476 bgp_process (bgp, rn, afi, SAFI_UNICAST);
718e3744 477 }
478
fb018d25 479#if BGP_SCAN_NEXTHOP
718e3744 480 /* Flash old cache. */
5932020b 481 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
482 bgp_nexthop_cache_reset (cache2_table[afi]);
718e3744 483 else
5932020b 484 bgp_nexthop_cache_reset (cache1_table[afi]);
fb018d25 485#endif
f418446b 486
487 if (BGP_DEBUG (events, EVENTS))
488 {
489 if (afi == AFI_IP)
490 zlog_debug ("scanning IPv4 Unicast routing tables");
491 else if (afi == AFI_IP6)
492 zlog_debug ("scanning IPv6 Unicast routing tables");
493 }
dcab1bb8
CF
494
495 /* Reevaluate default-originate route-maps and announce/withdraw
496 * default route if neccesary. */
497 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
498 {
499 if (peer->status == Established
500 && CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE)
501 && peer->default_rmap[afi][safi].name)
502 bgp_default_originate (peer, afi, safi, 0);
503 }
718e3744 504}
718e3744 505
506/* BGP scan thread. This thread check nexthop reachability. */
94f2b392 507static int
5932020b 508bgp_scan_timer (struct thread *t)
718e3744 509{
510 bgp_scan_thread =
5932020b 511 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
718e3744 512
f418446b 513 if (BGP_DEBUG (events, EVENTS))
478ba054 514 zlog_debug ("Performing BGP general scanning");
718e3744 515
5932020b 516 bgp_scan (AFI_IP, SAFI_UNICAST);
718e3744 517
518#ifdef HAVE_IPV6
5932020b 519 bgp_scan (AFI_IP6, SAFI_UNICAST);
718e3744 520#endif /* HAVE_IPV6 */
521
522 return 0;
523}
10f9bf3f
JBD
524
525/* BGP own address structure */
526struct bgp_addr
527{
528 struct in_addr addr;
529 int refcnt;
530};
531
532static struct hash *bgp_address_hash;
533
534static void *
535bgp_address_hash_alloc (void *p)
536{
537 struct in_addr *val = p;
538 struct bgp_addr *addr;
539
540 addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr));
541 addr->refcnt = 0;
542 addr->addr.s_addr = val->s_addr;
543
544 return addr;
545}
546
547static unsigned int
548bgp_address_hash_key_make (void *p)
549{
550 const struct bgp_addr *addr = p;
551
552 return jhash_1word(addr->addr.s_addr, 0);
553}
554
555static int
556bgp_address_hash_cmp (const void *p1, const void *p2)
557{
558 const struct bgp_addr *addr1 = p1;
559 const struct bgp_addr *addr2 = p2;
560
561 return addr1->addr.s_addr == addr2->addr.s_addr;
562}
563
564void
565bgp_address_init (void)
566{
567 bgp_address_hash = hash_create (bgp_address_hash_key_make,
568 bgp_address_hash_cmp);
569}
570
571static void
572bgp_address_add (struct prefix *p)
573{
574 struct bgp_addr tmp;
575 struct bgp_addr *addr;
576
577 tmp.addr = p->u.prefix4;
578
579 addr = hash_get (bgp_address_hash, &tmp, bgp_address_hash_alloc);
5ce10e92
DS
580 if (!addr)
581 return;
582
10f9bf3f
JBD
583 addr->refcnt++;
584}
585
586static void
587bgp_address_del (struct prefix *p)
588{
589 struct bgp_addr tmp;
590 struct bgp_addr *addr;
591
592 tmp.addr = p->u.prefix4;
593
594 addr = hash_lookup (bgp_address_hash, &tmp);
9e47abd8
RG
595 /* may have been deleted earlier by bgp_interface_down() */
596 if (addr == NULL)
597 return;
598
10f9bf3f
JBD
599 addr->refcnt--;
600
601 if (addr->refcnt == 0)
602 {
603 hash_release (bgp_address_hash, addr);
604 XFREE (MTYPE_BGP_ADDR, addr);
605 }
606}
607
6b0655a2 608
5932020b 609struct bgp_connected_ref
718e3744 610{
611 unsigned int refcnt;
612};
613
614void
615bgp_connected_add (struct connected *ifc)
616{
617 struct prefix p;
618 struct prefix *addr;
718e3744 619 struct interface *ifp;
620 struct bgp_node *rn;
5932020b 621 struct bgp_connected_ref *bc;
718e3744 622
623 ifp = ifc->ifp;
624
625 if (! ifp)
626 return;
627
628 if (if_is_loopback (ifp))
629 return;
630
631 addr = ifc->address;
718e3744 632
633 if (addr->family == AF_INET)
634 {
e4529636 635 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
718e3744 636 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
637
638 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
639 return;
640
10f9bf3f
JBD
641 bgp_address_add (addr);
642
5932020b 643 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
718e3744 644 if (rn->info)
645 {
646 bc = rn->info;
647 bc->refcnt++;
648 }
649 else
650 {
6c88b44d 651 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
718e3744 652 bc->refcnt = 1;
653 rn->info = bc;
654 }
655 }
656#ifdef HAVE_IPV6
e4529636 657 else if (addr->family == AF_INET6)
718e3744 658 {
e4529636 659 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
718e3744 660 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
661
662 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
663 return;
664
665 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
666 return;
667
5932020b 668 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
718e3744 669 if (rn->info)
670 {
671 bc = rn->info;
672 bc->refcnt++;
673 }
674 else
675 {
6c88b44d 676 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
718e3744 677 bc->refcnt = 1;
678 rn->info = bc;
679 }
680 }
681#endif /* HAVE_IPV6 */
682}
683
684void
685bgp_connected_delete (struct connected *ifc)
686{
687 struct prefix p;
688 struct prefix *addr;
718e3744 689 struct interface *ifp;
690 struct bgp_node *rn;
5932020b 691 struct bgp_connected_ref *bc;
718e3744 692
693 ifp = ifc->ifp;
694
695 if (if_is_loopback (ifp))
696 return;
697
698 addr = ifc->address;
718e3744 699
700 if (addr->family == AF_INET)
701 {
e4529636 702 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
718e3744 703 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
704
705 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
706 return;
707
10f9bf3f
JBD
708 bgp_address_del (addr);
709
5932020b 710 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
718e3744 711 if (! rn)
712 return;
713
714 bc = rn->info;
715 bc->refcnt--;
716 if (bc->refcnt == 0)
717 {
6c88b44d 718 XFREE (MTYPE_BGP_CONN, bc);
718e3744 719 rn->info = NULL;
720 }
721 bgp_unlock_node (rn);
722 bgp_unlock_node (rn);
723 }
724#ifdef HAVE_IPV6
725 else if (addr->family == AF_INET6)
726 {
e4529636 727 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
718e3744 728 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
729
730 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
731 return;
732
733 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
734 return;
735
5932020b 736 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
718e3744 737 if (! rn)
738 return;
739
740 bc = rn->info;
741 bc->refcnt--;
742 if (bc->refcnt == 0)
743 {
6c88b44d 744 XFREE (MTYPE_BGP_CONN, bc);
718e3744 745 rn->info = NULL;
746 }
747 bgp_unlock_node (rn);
748 bgp_unlock_node (rn);
749 }
750#endif /* HAVE_IPV6 */
751}
752
753int
10f9bf3f 754bgp_nexthop_self (struct attr *attr)
718e3744 755{
10f9bf3f 756 struct bgp_addr tmp, *addr;
718e3744 757
10f9bf3f
JBD
758 tmp.addr = attr->nexthop;
759
760 addr = hash_lookup (bgp_address_hash, &tmp);
761 if (addr)
762 return 1;
718e3744 763
718e3744 764 return 0;
765}
6b0655a2 766
94f2b392 767static struct bgp_nexthop_cache *
66e5cd87 768zlookup_read (void)
718e3744 769{
770 struct stream *s;
d3092e7f 771 uint16_t length;
772 u_char marker;
773 u_char version;
774 uint16_t command;
718e3744 775 int nbytes;
776 struct in_addr raddr;
d3092e7f 777 uint32_t metric;
718e3744 778 int i;
779 u_char nexthop_num;
780 struct nexthop *nexthop;
781 struct bgp_nexthop_cache *bnc;
782
783 s = zlookup->ibuf;
784 stream_reset (s);
785
786 nbytes = stream_read (s, zlookup->sock, 2);
787 length = stream_getw (s);
788
789 nbytes = stream_read (s, zlookup->sock, length - 2);
d3092e7f 790 marker = stream_getc (s);
791 version = stream_getc (s);
792
793 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
794 {
795 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
796 __func__, zlookup->sock, marker, version);
797 return NULL;
798 }
799
800 command = stream_getw (s);
801
718e3744 802 raddr.s_addr = stream_get_ipv4 (s);
803 metric = stream_getl (s);
804 nexthop_num = stream_getc (s);
805
806 if (nexthop_num)
807 {
808 bnc = bnc_new ();
809 bnc->valid = 1;
810 bnc->metric = metric;
811 bnc->nexthop_num = nexthop_num;
812
813 for (i = 0; i < nexthop_num; i++)
814 {
393deb9b 815 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
718e3744 816 nexthop->type = stream_getc (s);
817 switch (nexthop->type)
818 {
819 case ZEBRA_NEXTHOP_IPV4:
820 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
821 break;
bb97e462
CF
822 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
823 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
824 nexthop->ifindex = stream_getl (s);
825 break;
718e3744 826 case ZEBRA_NEXTHOP_IFINDEX:
827 case ZEBRA_NEXTHOP_IFNAME:
828 nexthop->ifindex = stream_getl (s);
829 break;
fa2b17e3 830 default:
831 /* do nothing */
832 break;
718e3744 833 }
834 bnc_nexthop_add (bnc, nexthop);
835 }
836 }
837 else
838 return NULL;
839
840 return bnc;
841}
842
843struct bgp_nexthop_cache *
844zlookup_query (struct in_addr addr)
845{
846 int ret;
847 struct stream *s;
848
849 /* Check socket. */
850 if (zlookup->sock < 0)
851 return NULL;
852
853 s = zlookup->obuf;
854 stream_reset (s);
d3092e7f 855 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
718e3744 856 stream_put_in_addr (s, &addr);
d3092e7f 857
858 stream_putw_at (s, 0, stream_get_endp (s));
859
860 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
718e3744 861 if (ret < 0)
862 {
863 zlog_err ("can't write to zlookup->sock");
864 close (zlookup->sock);
865 zlookup->sock = -1;
866 return NULL;
867 }
868 if (ret == 0)
869 {
870 zlog_err ("zlookup->sock connection closed");
871 close (zlookup->sock);
872 zlookup->sock = -1;
873 return NULL;
874 }
875
876 return zlookup_read ();
877}
878
879#ifdef HAVE_IPV6
94f2b392 880static struct bgp_nexthop_cache *
66e5cd87 881zlookup_read_ipv6 (void)
718e3744 882{
883 struct stream *s;
d3092e7f 884 uint16_t length;
885 u_char version, marker;
886 uint16_t command;
718e3744 887 int nbytes;
888 struct in6_addr raddr;
d3092e7f 889 uint32_t metric;
718e3744 890 int i;
891 u_char nexthop_num;
892 struct nexthop *nexthop;
893 struct bgp_nexthop_cache *bnc;
894
895 s = zlookup->ibuf;
896 stream_reset (s);
897
898 nbytes = stream_read (s, zlookup->sock, 2);
899 length = stream_getw (s);
900
901 nbytes = stream_read (s, zlookup->sock, length - 2);
d3092e7f 902 marker = stream_getc (s);
903 version = stream_getc (s);
904
905 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
906 {
907 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
908 __func__, zlookup->sock, marker, version);
909 return NULL;
910 }
911
912 command = stream_getw (s);
913
718e3744 914 stream_get (&raddr, s, 16);
915
916 metric = stream_getl (s);
917 nexthop_num = stream_getc (s);
918
919 if (nexthop_num)
920 {
921 bnc = bnc_new ();
922 bnc->valid = 1;
923 bnc->metric = metric;
924 bnc->nexthop_num = nexthop_num;
925
926 for (i = 0; i < nexthop_num; i++)
927 {
393deb9b 928 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
718e3744 929 nexthop->type = stream_getc (s);
930 switch (nexthop->type)
931 {
932 case ZEBRA_NEXTHOP_IPV6:
933 stream_get (&nexthop->gate.ipv6, s, 16);
934 break;
935 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
936 case ZEBRA_NEXTHOP_IPV6_IFNAME:
937 stream_get (&nexthop->gate.ipv6, s, 16);
938 nexthop->ifindex = stream_getl (s);
939 break;
940 case ZEBRA_NEXTHOP_IFINDEX:
941 case ZEBRA_NEXTHOP_IFNAME:
942 nexthop->ifindex = stream_getl (s);
943 break;
fa2b17e3 944 default:
945 /* do nothing */
946 break;
718e3744 947 }
948 bnc_nexthop_add (bnc, nexthop);
949 }
950 }
951 else
952 return NULL;
953
954 return bnc;
955}
956
957struct bgp_nexthop_cache *
958zlookup_query_ipv6 (struct in6_addr *addr)
959{
960 int ret;
961 struct stream *s;
962
963 /* Check socket. */
964 if (zlookup->sock < 0)
965 return NULL;
966
967 s = zlookup->obuf;
968 stream_reset (s);
d3092e7f 969 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
718e3744 970 stream_put (s, addr, 16);
d3092e7f 971 stream_putw_at (s, 0, stream_get_endp (s));
972
973 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
718e3744 974 if (ret < 0)
975 {
976 zlog_err ("can't write to zlookup->sock");
977 close (zlookup->sock);
978 zlookup->sock = -1;
979 return NULL;
980 }
981 if (ret == 0)
982 {
983 zlog_err ("zlookup->sock connection closed");
984 close (zlookup->sock);
985 zlookup->sock = -1;
986 return NULL;
987 }
988
989 return zlookup_read_ipv6 ();
990}
991#endif /* HAVE_IPV6 */
992
94f2b392 993static int
994bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
995 struct in_addr *igpnexthop)
718e3744 996{
997 struct stream *s;
998 int ret;
d3092e7f 999 u_int16_t length, command;
1000 u_char version, marker;
718e3744 1001 int nbytes;
1002 struct in_addr addr;
1003 struct in_addr nexthop;
1004 u_int32_t metric = 0;
1005 u_char nexthop_num;
1006 u_char nexthop_type;
1007
1008 /* If lookup connection is not available return valid. */
1009 if (zlookup->sock < 0)
1010 {
1011 if (igpmetric)
1012 *igpmetric = 0;
1013 return 1;
1014 }
1015
1016 /* Send query to the lookup connection */
1017 s = zlookup->obuf;
1018 stream_reset (s);
d3092e7f 1019 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
1020
718e3744 1021 stream_putc (s, p->prefixlen);
1022 stream_put_in_addr (s, &p->u.prefix4);
d3092e7f 1023
1024 stream_putw_at (s, 0, stream_get_endp (s));
1025
718e3744 1026 /* Write the packet. */
d3092e7f 1027 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
718e3744 1028
1029 if (ret < 0)
1030 {
1031 zlog_err ("can't write to zlookup->sock");
1032 close (zlookup->sock);
1033 zlookup->sock = -1;
1034 return 1;
1035 }
1036 if (ret == 0)
1037 {
1038 zlog_err ("zlookup->sock connection closed");
1039 close (zlookup->sock);
1040 zlookup->sock = -1;
1041 return 1;
1042 }
1043
1044 /* Get result. */
1045 stream_reset (s);
1046
1047 /* Fetch length. */
1048 nbytes = stream_read (s, zlookup->sock, 2);
1049 length = stream_getw (s);
1050
1051 /* Fetch whole data. */
1052 nbytes = stream_read (s, zlookup->sock, length - 2);
d3092e7f 1053 marker = stream_getc (s);
1054 version = stream_getc (s);
1055
1056 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
1057 {
1058 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1059 __func__, zlookup->sock, marker, version);
1060 return 0;
1061 }
1062
1063 command = stream_getw (s);
1064
718e3744 1065 addr.s_addr = stream_get_ipv4 (s);
1066 metric = stream_getl (s);
1067 nexthop_num = stream_getc (s);
1068
1069 /* Set IGP metric value. */
1070 if (igpmetric)
1071 *igpmetric = metric;
1072
1073 /* If there is nexthop then this is active route. */
1074 if (nexthop_num)
1075 {
1076 nexthop.s_addr = 0;
1077 nexthop_type = stream_getc (s);
a12afd5e 1078 switch (nexthop_type)
718e3744 1079 {
a12afd5e 1080 case ZEBRA_NEXTHOP_IPV4:
718e3744 1081 nexthop.s_addr = stream_get_ipv4 (s);
a12afd5e
CF
1082 break;
1083 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1084 nexthop.s_addr = stream_get_ipv4 (s);
1085 /* ifindex */ (void)stream_getl (s);
1086 break;
1087 default:
1088 /* do nothing */
1089 break;
718e3744 1090 }
a12afd5e 1091 *igpnexthop = nexthop;
718e3744 1092
1093 return 1;
1094 }
1095 else
1096 return 0;
1097}
1098
1099/* Scan all configured BGP route then check the route exists in IGP or
1100 not. */
94f2b392 1101static int
718e3744 1102bgp_import (struct thread *t)
1103{
1104 struct bgp *bgp;
1105 struct bgp_node *rn;
1106 struct bgp_static *bgp_static;
1eb8ef25 1107 struct listnode *node, *nnode;
718e3744 1108 int valid;
1109 u_int32_t metric;
1110 struct in_addr nexthop;
1111 afi_t afi;
1112 safi_t safi;
1113
1114 bgp_import_thread =
1115 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1116
f418446b 1117 if (BGP_DEBUG (events, EVENTS))
1118 zlog_debug ("Import timer expired.");
718e3744 1119
1eb8ef25 1120 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
6cbbc3cc 1121 {
1122 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1123 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1124 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1125 rn = bgp_route_next (rn))
1126 if ((bgp_static = rn->info) != NULL)
718e3744 1127 {
6cbbc3cc 1128 if (bgp_static->backdoor)
1129 continue;
718e3744 1130
6cbbc3cc 1131 valid = bgp_static->valid;
1132 metric = bgp_static->igpmetric;
1133 nexthop = bgp_static->igpnexthop;
1134
1135 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1136 && afi == AFI_IP && safi == SAFI_UNICAST)
1137 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1138 &bgp_static->igpnexthop);
718e3744 1139 else
6cbbc3cc 1140 {
1141 bgp_static->valid = 1;
1142 bgp_static->igpmetric = 0;
1143 bgp_static->igpnexthop.s_addr = 0;
1144 }
1145
1146 if (bgp_static->valid != valid)
1147 {
1148 if (bgp_static->valid)
1149 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1150 else
1151 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1152 }
1153 else if (bgp_static->valid)
1154 {
1155 if (bgp_static->igpmetric != metric
1156 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1157 || bgp_static->rmap.name)
1158 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1159 }
718e3744 1160 }
6cbbc3cc 1161 }
718e3744 1162 return 0;
1163}
1164
1165/* Connect to zebra for nexthop lookup. */
94f2b392 1166static int
718e3744 1167zlookup_connect (struct thread *t)
1168{
1169 struct zclient *zlookup;
1170
1171 zlookup = THREAD_ARG (t);
1172 zlookup->t_connect = NULL;
1173
1174 if (zlookup->sock != -1)
1175 return 0;
1176
b5114685 1177 if (zclient_socket_connect (zlookup) < 0)
718e3744 1178 return -1;
1179
718e3744 1180 return 0;
1181}
1182
1183/* Check specified multiaccess next-hop. */
1184int
1185bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1186{
1187 struct bgp_node *rn1;
1188 struct bgp_node *rn2;
1189 struct prefix p1;
1190 struct prefix p2;
1191 struct in_addr addr;
1192 int ret;
1193
1194 ret = inet_aton (peer, &addr);
1195 if (! ret)
1196 return 0;
1197
1198 memset (&p1, 0, sizeof (struct prefix));
1199 p1.family = AF_INET;
1200 p1.prefixlen = IPV4_MAX_BITLEN;
1201 p1.u.prefix4 = nexthop;
1202 memset (&p2, 0, sizeof (struct prefix));
1203 p2.family = AF_INET;
1204 p2.prefixlen = IPV4_MAX_BITLEN;
1205 p2.u.prefix4 = addr;
1206
1207 /* If bgp scan is not enabled, return invalid. */
1208 if (zlookup->sock < 0)
1209 return 0;
1210
5932020b 1211 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
718e3744 1212 if (! rn1)
1213 return 0;
6c88b44d 1214 bgp_unlock_node (rn1);
718e3744 1215
5932020b 1216 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
718e3744 1217 if (! rn2)
1218 return 0;
6c88b44d 1219 bgp_unlock_node (rn2);
718e3744 1220
6c88b44d
CC
1221 /* This is safe, even with above unlocks, since we are just
1222 comparing pointers to the objects, not the objects themselves. */
718e3744 1223 if (rn1 == rn2)
1224 return 1;
1225
1226 return 0;
1227}
6b0655a2 1228
718e3744 1229DEFUN (bgp_scan_time,
1230 bgp_scan_time_cmd,
1231 "bgp scan-time <5-60>",
1232 "BGP specific commands\n"
1233 "Configure background scanner interval\n"
1234 "Scanner interval (seconds)\n")
1235{
1236 bgp_scan_interval = atoi (argv[0]);
1237
1238 if (bgp_scan_thread)
1239 {
1240 thread_cancel (bgp_scan_thread);
1241 bgp_scan_thread =
5932020b 1242 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
718e3744 1243 }
1244
1245 return CMD_SUCCESS;
1246}
1247
1248DEFUN (no_bgp_scan_time,
1249 no_bgp_scan_time_cmd,
1250 "no bgp scan-time",
1251 NO_STR
1252 "BGP specific commands\n"
1253 "Configure background scanner interval\n")
1254{
1255 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1256
1257 if (bgp_scan_thread)
1258 {
1259 thread_cancel (bgp_scan_thread);
1260 bgp_scan_thread =
5932020b 1261 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
718e3744 1262 }
1263
1264 return CMD_SUCCESS;
1265}
1266
1267ALIAS (no_bgp_scan_time,
1268 no_bgp_scan_time_val_cmd,
1269 "no bgp scan-time <5-60>",
1270 NO_STR
1271 "BGP specific commands\n"
1272 "Configure background scanner interval\n"
1273 "Scanner interval (seconds)\n")
1274
318f0d8a
DO
1275static int
1276show_ip_bgp_scan_tables (struct vty *vty, const char detail)
718e3744 1277{
1278 struct bgp_node *rn;
318f0d8a 1279 char buf[INET6_ADDRSTRLEN];
77217fd4 1280 struct nexthop *nexthop;
718e3744 1281
1282 if (bgp_scan_thread)
1283 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1284 else
1285 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1286 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1287
fb018d25 1288#if BGP_SCAN_NEXTHOP
718e3744 1289 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
5932020b 1290 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
718e3744 1291 if ((bnc = rn->info) != NULL)
1292 {
1293 if (bnc->valid)
318f0d8a 1294 {
718e3744 1295 vty_out (vty, " %s valid [IGP metric %d]%s",
318f0d8a
DO
1296 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1297 if (detail)
77217fd4
DS
1298 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
1299 switch (nexthop->type)
0e8032d6
DO
1300 {
1301 case NEXTHOP_TYPE_IPV4:
77217fd4 1302 vty_out (vty, " gate %s%s", inet_ntop (AF_INET, &nexthop->gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
0e8032d6 1303 break;
bb97e462 1304 case NEXTHOP_TYPE_IPV4_IFINDEX:
77217fd4
DS
1305 vty_out (vty, " gate %s", inet_ntop (AF_INET, &nexthop->gate.ipv4, buf, INET6_ADDRSTRLEN));
1306 vty_out (vty, " ifidx %u%s", nexthop->ifindex, VTY_NEWLINE);
bb97e462 1307 break;
0e8032d6 1308 case NEXTHOP_TYPE_IFINDEX:
77217fd4 1309 vty_out (vty, " ifidx %u%s", nexthop->ifindex, VTY_NEWLINE);
0e8032d6
DO
1310 break;
1311 default:
77217fd4 1312 vty_out (vty, " invalid nexthop type %u%s", nexthop->type, VTY_NEWLINE);
0e8032d6 1313 }
318f0d8a 1314 }
718e3744 1315 else
1316 vty_out (vty, " %s invalid%s",
318f0d8a 1317 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
718e3744 1318 }
1319
1320#ifdef HAVE_IPV6
1321 {
5932020b 1322 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1323 rn;
1324 rn = bgp_route_next (rn))
718e3744 1325 if ((bnc = rn->info) != NULL)
1326 {
1327 if (bnc->valid)
318f0d8a 1328 {
718e3744 1329 vty_out (vty, " %s valid [IGP metric %d]%s",
318f0d8a 1330 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
718e3744 1331 bnc->metric, VTY_NEWLINE);
318f0d8a 1332 if (detail)
77217fd4
DS
1333 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
1334 switch (nexthop->type)
0e8032d6
DO
1335 {
1336 case NEXTHOP_TYPE_IPV6:
77217fd4 1337 vty_out (vty, " gate %s%s", inet_ntop (AF_INET6, &nexthop->gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
0e8032d6
DO
1338 break;
1339 case NEXTHOP_TYPE_IFINDEX:
77217fd4 1340 vty_out (vty, " ifidx %u%s", nexthop->ifindex, VTY_NEWLINE);
0e8032d6
DO
1341 break;
1342 default:
77217fd4 1343 vty_out (vty, " invalid nexthop type %u%s", nexthop->type, VTY_NEWLINE);
0e8032d6 1344 }
318f0d8a 1345 }
718e3744 1346 else
1347 vty_out (vty, " %s invalid%s",
318f0d8a 1348 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
718e3744 1349 VTY_NEWLINE);
1350 }
1351 }
1352#endif /* HAVE_IPV6 */
fb018d25
DS
1353#else
1354 vty_out (vty, "BGP next-hop tracking is on%s", VTY_NEWLINE);
1355#endif
718e3744 1356 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
5932020b 1357 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1358 rn;
1359 rn = bgp_route_next (rn))
718e3744 1360 if (rn->info != NULL)
1361 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1362 VTY_NEWLINE);
1363
1364#ifdef HAVE_IPV6
1365 {
5932020b 1366 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1367 rn;
1368 rn = bgp_route_next (rn))
718e3744 1369 if (rn->info != NULL)
1370 vty_out (vty, " %s/%d%s",
318f0d8a 1371 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
718e3744 1372 rn->p.prefixlen,
1373 VTY_NEWLINE);
1374 }
1375#endif /* HAVE_IPV6 */
1376
1377 return CMD_SUCCESS;
1378}
1379
fb018d25
DS
1380static int
1381show_ip_bgp_nexthop_table (struct vty *vty, int detail)
1382{
1383 struct bgp_node *rn;
1384 struct bgp_nexthop_cache *bnc;
1385 char buf[INET6_ADDRSTRLEN];
77217fd4 1386 struct nexthop *nexthop;
fb018d25
DS
1387 time_t tbuf;
1388 u_char i;
1389
1390 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
1391 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
1392 if ((bnc = rn->info) != NULL)
1393 {
1394 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID))
1395 {
1396 vty_out (vty, " %s valid [IGP metric %d], #paths %d%s",
1397 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN),
1398 bnc->metric, bnc->path_count, VTY_NEWLINE);
1399 if (detail)
77217fd4
DS
1400 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
1401 switch (nexthop->type)
1402 {
1403 case NEXTHOP_TYPE_IPV4:
1404 vty_out (vty, " gate %s%s",
1405 inet_ntop (AF_INET, &nexthop->gate.ipv4, buf,
1406 INET6_ADDRSTRLEN), VTY_NEWLINE);
1407 break;
1408 case NEXTHOP_TYPE_IFINDEX:
1409 vty_out (vty, " if %s%s",
1410 ifindex2ifname(nexthop->ifindex), VTY_NEWLINE);
1411 break;
1412 case NEXTHOP_TYPE_IPV4_IFINDEX:
1413 vty_out (vty, " gate %s, if %s%s",
1414 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
1415 INET6_ADDRSTRLEN),
1416 ifindex2ifname(nexthop->ifindex), VTY_NEWLINE);
1417 break;
1418 default:
1419 vty_out (vty, " invalid nexthop type %u%s",
1420 nexthop->type, VTY_NEWLINE);
1421 }
fb018d25
DS
1422 }
1423 else
1424 vty_out (vty, " %s invalid%s",
1425 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1426#ifdef HAVE_CLOCK_MONOTONIC
1427 tbuf = time(NULL) - (bgp_clock() - bnc->last_update);
1428 vty_out (vty, " Last update: %s", ctime(&tbuf));
1429#else
1430 vty_out (vty, " Last update: %s", ctime(&bnc->uptime));
1431#endif /* HAVE_CLOCK_MONOTONIC */
1432
1433 vty_out(vty, "%s", VTY_NEWLINE);
1434 }
1435
1436#ifdef HAVE_IPV6
1437 {
1438 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1439 rn;
1440 rn = bgp_route_next (rn))
1441 if ((bnc = rn->info) != NULL)
1442 {
1443 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID))
1444 {
1445 vty_out (vty, " %s valid [IGP metric %d]%s",
1446 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf,
1447 INET6_ADDRSTRLEN),
1448 bnc->metric, VTY_NEWLINE);
1449 if (detail)
77217fd4
DS
1450 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
1451 switch (nexthop->type)
1452 {
1453 case NEXTHOP_TYPE_IPV6:
1454 vty_out (vty, " gate %s%s",
1455 inet_ntop (AF_INET6, &nexthop->gate.ipv6,
1456 buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1457 break;
1458 case NEXTHOP_TYPE_IPV6_IFINDEX:
1459 vty_out(vty, " gate %s, if %s%s",
1460 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
1461 INET6_ADDRSTRLEN),
1462 ifindex2ifname(nexthop->ifindex),
1463 VTY_NEWLINE);
1464 break;
1465 case NEXTHOP_TYPE_IFINDEX:
1466 vty_out (vty, " ifidx %u%s", nexthop->ifindex,
1467 VTY_NEWLINE);
1468 break;
1469 default:
1470 vty_out (vty, " invalid nexthop type %u%s",
1471 nexthop->type, VTY_NEWLINE);
1472 }
fb018d25
DS
1473 }
1474 else
1475 vty_out (vty, " %s invalid%s",
1476 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
1477 VTY_NEWLINE);
1478#ifdef HAVE_CLOCK_MONOTONIC
1479 tbuf = time(NULL) - (bgp_clock() - bnc->last_update);
1480 vty_out (vty, " Last update: %s", ctime(&tbuf));
1481#else
1482 vty_out (vty, " Last update: %s", ctime(&bnc->uptime));
1483#endif /* HAVE_CLOCK_MONOTONIC */
1484
1485 vty_out(vty, "%s", VTY_NEWLINE);
1486 }
1487 }
1488#endif /* HAVE_IPV6 */
1489 return CMD_SUCCESS;
1490}
1491
318f0d8a
DO
1492DEFUN (show_ip_bgp_scan,
1493 show_ip_bgp_scan_cmd,
1494 "show ip bgp scan",
1495 SHOW_STR
1496 IP_STR
1497 BGP_STR
1498 "BGP scan status\n")
1499{
1500 return show_ip_bgp_scan_tables (vty, 0);
1501}
1502
1503DEFUN (show_ip_bgp_scan_detail,
1504 show_ip_bgp_scan_detail_cmd,
1505 "show ip bgp scan detail",
1506 SHOW_STR
1507 IP_STR
1508 BGP_STR
1509 "BGP scan status\n"
1510 "More detailed output\n")
1511{
1512 return show_ip_bgp_scan_tables (vty, 1);
1513}
1514
fb018d25
DS
1515DEFUN (show_ip_bgp_nexthop,
1516 show_ip_bgp_nexthop_cmd,
1517 "show ip bgp nexthop",
1518 SHOW_STR
1519 IP_STR
1520 BGP_STR
1521 "BGP nexthop table\n")
1522{
1523 return show_ip_bgp_nexthop_table (vty, 0);
1524}
1525
1526DEFUN (show_ip_bgp_nexthop_detail,
1527 show_ip_bgp_nexthop_detail_cmd,
1528 "show ip bgp nexthop detail",
1529 SHOW_STR
1530 IP_STR
1531 BGP_STR
1532 "BGP nexthop table\n")
1533{
1534 return show_ip_bgp_nexthop_table (vty, 1);
1535}
1536
718e3744 1537int
1538bgp_config_write_scan_time (struct vty *vty)
1539{
1540 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1541 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1542 return CMD_SUCCESS;
1543}
1544
1545void
66e5cd87 1546bgp_scan_init (void)
718e3744 1547{
1548 zlookup = zclient_new ();
1549 zlookup->sock = -1;
718e3744 1550 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1551
1552 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1553 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1554
64e580a7
PJ
1555 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1556 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
5932020b 1557 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
718e3744 1558
64e580a7 1559 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
718e3744 1560
1561#ifdef HAVE_IPV6
64e580a7
PJ
1562 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1563 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
5932020b 1564 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
64e580a7 1565 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
718e3744 1566#endif /* HAVE_IPV6 */
1567
1568 /* Make BGP scan thread. */
5932020b 1569 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1570 NULL, bgp_scan_interval);
6cbbc3cc 1571 /* Make BGP import there. */
1572 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
718e3744 1573
1574 install_element (BGP_NODE, &bgp_scan_time_cmd);
1575 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1576 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1577 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
318f0d8a 1578 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
fb018d25
DS
1579 install_element (VIEW_NODE, &show_ip_bgp_nexthop_cmd);
1580 install_element (VIEW_NODE, &show_ip_bgp_nexthop_detail_cmd);
62687ff1 1581 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
718e3744 1582 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
fb018d25
DS
1583 install_element (ENABLE_NODE, &show_ip_bgp_nexthop_cmd);
1584 install_element (ENABLE_NODE, &show_ip_bgp_nexthop_detail_cmd);
318f0d8a 1585 install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
718e3744 1586}
228da428
CC
1587
1588void
1589bgp_scan_finish (void)
1590{
6c88b44d
CC
1591 /* Only the current one needs to be reset. */
1592 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]);
1593
228da428
CC
1594 bgp_table_unlock (cache1_table[AFI_IP]);
1595 cache1_table[AFI_IP] = NULL;
1596
1597 bgp_table_unlock (cache2_table[AFI_IP]);
1598 cache2_table[AFI_IP] = NULL;
1599
1600 bgp_table_unlock (bgp_connected_table[AFI_IP]);
1601 bgp_connected_table[AFI_IP] = NULL;
1602
1603#ifdef HAVE_IPV6
6c88b44d
CC
1604 /* Only the current one needs to be reset. */
1605 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]);
1606
228da428
CC
1607 bgp_table_unlock (cache1_table[AFI_IP6]);
1608 cache1_table[AFI_IP6] = NULL;
1609
1610 bgp_table_unlock (cache2_table[AFI_IP6]);
1611 cache2_table[AFI_IP6] = NULL;
1612
1613 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
1614 bgp_connected_table[AFI_IP6] = NULL;
1615#endif /* HAVE_IPV6 */
1616}