]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_nexthop.c
bgpd-fix-nexthop-show.patch
[mirror_frr.git] / bgpd / bgp_nexthop.c
1 /* BGP nexthop scan
2 Copyright (C) 2000 Kunihiro Ishiguro
3
4 This file is part of GNU Zebra.
5
6 GNU Zebra is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 GNU Zebra is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-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 #include "hash.h"
32 #include "jhash.h"
33 #include "nexthop.h"
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"
40 #include "bgpd/bgp_nht.h"
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
46 extern struct zclient *zclient;
47
48 struct bgp_nexthop_cache *zlookup_query (struct in_addr);
49 #ifdef HAVE_IPV6
50 struct bgp_nexthop_cache *zlookup_query_ipv6 (struct in6_addr *);
51 #endif /* HAVE_IPV6 */
52
53 /* Only one BGP scan thread are activated at the same time. */
54 static struct thread *bgp_scan_thread = NULL;
55
56 /* BGP import thread */
57 static struct thread *bgp_import_thread = NULL;
58
59 /* BGP scan interval. */
60 static int bgp_scan_interval;
61
62 /* BGP import interval. */
63 static int bgp_import_interval;
64
65 /* Route table for next-hop lookup cache. */
66 struct bgp_table *bgp_nexthop_cache_table[AFI_MAX];
67 static struct bgp_table *cache1_table[AFI_MAX];
68 static struct bgp_table *cache2_table[AFI_MAX];
69
70 /* Route table for connected route. */
71 static struct bgp_table *bgp_connected_table[AFI_MAX];
72
73 /* BGP nexthop lookup query client. */
74 struct zclient *zlookup = NULL;
75
76 char *
77 bnc_str (struct bgp_nexthop_cache *bnc, char *buf, int size)
78 {
79 prefix2str(&(bnc->node->p), buf, size);
80 return buf;
81 }
82
83 /* Add nexthop to the end of the list. */
84 static void
85 bnc_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
98 void
99 bnc_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
111 struct bgp_nexthop_cache *
112 bnc_new ()
113 {
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;
119 }
120
121 void
122 bnc_free (struct bgp_nexthop_cache *bnc)
123 {
124 bnc_nexthop_free (bnc);
125 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
126 }
127
128 static int
129 bgp_nexthop_cache_different (struct bgp_nexthop_cache *bnc1,
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 {
143 if (! nexthop_same_no_recurse (next1, next2))
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. */
153 int
154 bgp_nexthop_onlink (afi_t afi, struct attr *attr)
155 {
156 struct bgp_node *rn;
157
158 /* If zebra is not enabled return */
159 if (zlookup->sock < 0)
160 return 1;
161
162 /* Lookup the address is onlink or not. */
163 if (afi == AFI_IP)
164 {
165 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
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 {
175 if (attr->extra->mp_nexthop_len == 32)
176 return 1;
177 else if (attr->extra->mp_nexthop_len == 16)
178 {
179 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
180 return 1;
181
182 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
183 &attr->extra->mp_nexthop_global);
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. */
197 static int
198 bgp_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;
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
214 /* Only check IPv6 global address only nexthop. */
215 attr = ri->attr;
216
217 if (attr->extra->mp_nexthop_len != 16
218 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
219 return 1;
220
221 memset (&p, 0, sizeof (struct prefix));
222 p.family = AF_INET6;
223 p.prefixlen = IPV6_MAX_BITLEN;
224 p.u.prefix6 = attr->extra->mp_nexthop_global;
225
226 /* IBGP or ebgp-multihop */
227 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
228
229 if (rn->info)
230 {
231 bnc = rn->info;
232 bgp_unlock_node (rn);
233 }
234 else
235 {
236 if (NULL == (bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global)))
237 bnc = bnc_new ();
238 else
239 {
240 if (changed)
241 {
242 struct bgp_table *old;
243 struct bgp_node *oldrn;
244
245 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
246 old = cache2_table[AFI_IP6];
247 else
248 old = cache1_table[AFI_IP6];
249
250 oldrn = bgp_node_lookup (old, &p);
251 if (oldrn)
252 {
253 struct bgp_nexthop_cache *oldbnc = oldrn->info;
254
255 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
256
257 if (bnc->metric != oldbnc->metric)
258 bnc->metricchanged = 1;
259
260 bgp_unlock_node (oldrn);
261 }
262 }
263 }
264 rn->info = bnc;
265 }
266
267 if (changed)
268 *changed = bnc->changed;
269
270 if (metricchanged)
271 *metricchanged = bnc->metricchanged;
272
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;
277
278 return bnc->valid;
279 }
280 #endif /* HAVE_IPV6 */
281
282 /* Check specified next-hop is reachable or not. */
283 int
284 bgp_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;
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
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 */
313 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
314
315 if (rn->info)
316 {
317 bnc = rn->info;
318 bgp_unlock_node (rn);
319 }
320 else
321 {
322 if (NULL == (bnc = zlookup_query (addr)))
323 bnc = bnc_new ();
324 else
325 {
326 if (changed)
327 {
328 struct bgp_table *old;
329 struct bgp_node *oldrn;
330
331 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
332 old = cache2_table[AFI_IP];
333 else
334 old = cache1_table[AFI_IP];
335
336 oldrn = bgp_node_lookup (old, &p);
337 if (oldrn)
338 {
339 struct bgp_nexthop_cache *oldbnc = oldrn->info;
340
341 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
342
343 if (bnc->metric != oldbnc->metric)
344 bnc->metricchanged = 1;
345
346 bgp_unlock_node (oldrn);
347 }
348 }
349 }
350 rn->info = bnc;
351 }
352
353 if (changed)
354 *changed = bnc->changed;
355
356 if (metricchanged)
357 *metricchanged = bnc->metricchanged;
358
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;
363
364 return bnc->valid;
365 }
366
367 /* Reset and free all BGP nexthop cache. */
368 static void
369 bgp_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
383 static void
384 bgp_scan (afi_t afi, safi_t safi)
385 {
386 struct bgp_node *rn;
387 struct bgp *bgp;
388 struct bgp_info *bi;
389 struct bgp_info *next;
390 struct peer *peer;
391 struct listnode *node, *nnode;
392 #if BGP_SCAN_NEXTHOP
393 int valid;
394 int current;
395 int changed;
396 int metricchanged;
397
398 /* Change cache. */
399 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
400 bgp_nexthop_cache_table[afi] = cache2_table[afi];
401 else
402 bgp_nexthop_cache_table[afi] = cache1_table[afi];
403 #endif
404
405 /* Get default bgp. */
406 bgp = bgp_get_default ();
407 if (bgp == NULL)
408 return;
409
410 /* Maximum prefix check */
411 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
412 {
413 if (peer->status != Established)
414 continue;
415
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);
422 }
423
424 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
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 {
433 #if BGP_SCAN_NEXTHOP
434 changed = 0;
435 metricchanged = 0;
436
437 if (bi->peer->sort == BGP_PEER_EBGP && bi->peer->ttl == 1
438 && !CHECK_FLAG(bi->peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK))
439 valid = bgp_nexthop_onlink (afi, bi->attr);
440 else
441 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
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,
456 afi, SAFI_UNICAST);
457 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
458 }
459 else
460 {
461 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
462 bgp_aggregate_increment (bgp, &rn->p, bi,
463 afi, SAFI_UNICAST);
464 }
465 }
466 #endif
467
468 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
469 BGP_CONFIG_DAMPENING)
470 && bi->extra && bi->extra->damp_info )
471 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
472 bgp_aggregate_increment (bgp, &rn->p, bi,
473 afi, SAFI_UNICAST);
474 }
475 }
476 bgp_process (bgp, rn, afi, SAFI_UNICAST);
477 }
478
479 #if BGP_SCAN_NEXTHOP
480 /* Flash old cache. */
481 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
482 bgp_nexthop_cache_reset (cache2_table[afi]);
483 else
484 bgp_nexthop_cache_reset (cache1_table[afi]);
485 #endif
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 }
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 }
504 }
505
506 /* BGP scan thread. This thread check nexthop reachability. */
507 static int
508 bgp_scan_timer (struct thread *t)
509 {
510 bgp_scan_thread =
511 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
512
513 if (BGP_DEBUG (events, EVENTS))
514 zlog_debug ("Performing BGP general scanning");
515
516 bgp_scan (AFI_IP, SAFI_UNICAST);
517
518 #ifdef HAVE_IPV6
519 bgp_scan (AFI_IP6, SAFI_UNICAST);
520 #endif /* HAVE_IPV6 */
521
522 return 0;
523 }
524
525 /* BGP own address structure */
526 struct bgp_addr
527 {
528 struct in_addr addr;
529 int refcnt;
530 };
531
532 static struct hash *bgp_address_hash;
533
534 static void *
535 bgp_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
547 static unsigned int
548 bgp_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
555 static int
556 bgp_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
564 void
565 bgp_address_init (void)
566 {
567 bgp_address_hash = hash_create (bgp_address_hash_key_make,
568 bgp_address_hash_cmp);
569 }
570
571 static void
572 bgp_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);
580 if (!addr)
581 return;
582
583 addr->refcnt++;
584 }
585
586 static void
587 bgp_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);
595 /* may have been deleted earlier by bgp_interface_down() */
596 if (addr == NULL)
597 return;
598
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
608
609 struct bgp_connected_ref
610 {
611 unsigned int refcnt;
612 };
613
614 void
615 bgp_connected_add (struct connected *ifc)
616 {
617 struct prefix p;
618 struct prefix *addr;
619 struct interface *ifp;
620 struct bgp_node *rn;
621 struct bgp_connected_ref *bc;
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;
632
633 if (addr->family == AF_INET)
634 {
635 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
636 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
637
638 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
639 return;
640
641 bgp_address_add (addr);
642
643 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
644 if (rn->info)
645 {
646 bc = rn->info;
647 bc->refcnt++;
648 }
649 else
650 {
651 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
652 bc->refcnt = 1;
653 rn->info = bc;
654 }
655 }
656 #ifdef HAVE_IPV6
657 else if (addr->family == AF_INET6)
658 {
659 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
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
668 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
669 if (rn->info)
670 {
671 bc = rn->info;
672 bc->refcnt++;
673 }
674 else
675 {
676 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
677 bc->refcnt = 1;
678 rn->info = bc;
679 }
680 }
681 #endif /* HAVE_IPV6 */
682 }
683
684 void
685 bgp_connected_delete (struct connected *ifc)
686 {
687 struct prefix p;
688 struct prefix *addr;
689 struct interface *ifp;
690 struct bgp_node *rn;
691 struct bgp_connected_ref *bc;
692
693 ifp = ifc->ifp;
694
695 if (if_is_loopback (ifp))
696 return;
697
698 addr = ifc->address;
699
700 if (addr->family == AF_INET)
701 {
702 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
703 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
704
705 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
706 return;
707
708 bgp_address_del (addr);
709
710 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
711 if (! rn)
712 return;
713
714 bc = rn->info;
715 bc->refcnt--;
716 if (bc->refcnt == 0)
717 {
718 XFREE (MTYPE_BGP_CONN, bc);
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 {
727 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
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
736 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
737 if (! rn)
738 return;
739
740 bc = rn->info;
741 bc->refcnt--;
742 if (bc->refcnt == 0)
743 {
744 XFREE (MTYPE_BGP_CONN, bc);
745 rn->info = NULL;
746 }
747 bgp_unlock_node (rn);
748 bgp_unlock_node (rn);
749 }
750 #endif /* HAVE_IPV6 */
751 }
752
753 int
754 bgp_nexthop_self (struct attr *attr)
755 {
756 struct bgp_addr tmp, *addr;
757
758 tmp.addr = attr->nexthop;
759
760 addr = hash_lookup (bgp_address_hash, &tmp);
761 if (addr)
762 return 1;
763
764 return 0;
765 }
766
767 static struct bgp_nexthop_cache *
768 zlookup_read (void)
769 {
770 struct stream *s;
771 uint16_t length;
772 u_char marker;
773 u_char version;
774 uint16_t command;
775 int nbytes;
776 struct in_addr raddr;
777 uint32_t metric;
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);
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
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 {
815 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
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;
822 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
823 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
824 nexthop->ifindex = stream_getl (s);
825 break;
826 case ZEBRA_NEXTHOP_IFINDEX:
827 case ZEBRA_NEXTHOP_IFNAME:
828 nexthop->ifindex = stream_getl (s);
829 break;
830 default:
831 /* do nothing */
832 break;
833 }
834 bnc_nexthop_add (bnc, nexthop);
835 }
836 }
837 else
838 return NULL;
839
840 return bnc;
841 }
842
843 struct bgp_nexthop_cache *
844 zlookup_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);
855 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
856 stream_put_in_addr (s, &addr);
857
858 stream_putw_at (s, 0, stream_get_endp (s));
859
860 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
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
880 static struct bgp_nexthop_cache *
881 zlookup_read_ipv6 (void)
882 {
883 struct stream *s;
884 uint16_t length;
885 u_char version, marker;
886 uint16_t command;
887 int nbytes;
888 struct in6_addr raddr;
889 uint32_t metric;
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);
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
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 {
928 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
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;
944 default:
945 /* do nothing */
946 break;
947 }
948 bnc_nexthop_add (bnc, nexthop);
949 }
950 }
951 else
952 return NULL;
953
954 return bnc;
955 }
956
957 struct bgp_nexthop_cache *
958 zlookup_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);
969 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
970 stream_put (s, addr, 16);
971 stream_putw_at (s, 0, stream_get_endp (s));
972
973 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
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
993 static int
994 bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
995 struct in_addr *igpnexthop)
996 {
997 struct stream *s;
998 int ret;
999 u_int16_t length, command;
1000 u_char version, marker;
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);
1019 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
1020
1021 stream_putc (s, p->prefixlen);
1022 stream_put_in_addr (s, &p->u.prefix4);
1023
1024 stream_putw_at (s, 0, stream_get_endp (s));
1025
1026 /* Write the packet. */
1027 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
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);
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
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);
1078 switch (nexthop_type)
1079 {
1080 case ZEBRA_NEXTHOP_IPV4:
1081 nexthop.s_addr = stream_get_ipv4 (s);
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;
1090 }
1091 *igpnexthop = nexthop;
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. */
1101 static int
1102 bgp_import (struct thread *t)
1103 {
1104 struct bgp *bgp;
1105 struct bgp_node *rn;
1106 struct bgp_static *bgp_static;
1107 struct listnode *node, *nnode;
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
1117 if (BGP_DEBUG (events, EVENTS))
1118 zlog_debug ("Import timer expired.");
1119
1120 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
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)
1127 {
1128 if (bgp_static->backdoor)
1129 continue;
1130
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);
1139 else
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 }
1160 }
1161 }
1162 return 0;
1163 }
1164
1165 /* Connect to zebra for nexthop lookup. */
1166 static int
1167 zlookup_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
1177 if (zclient_socket_connect (zlookup) < 0)
1178 return -1;
1179
1180 return 0;
1181 }
1182
1183 /* Check specified multiaccess next-hop. */
1184 int
1185 bgp_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
1211 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
1212 if (! rn1)
1213 return 0;
1214 bgp_unlock_node (rn1);
1215
1216 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
1217 if (! rn2)
1218 return 0;
1219 bgp_unlock_node (rn2);
1220
1221 /* This is safe, even with above unlocks, since we are just
1222 comparing pointers to the objects, not the objects themselves. */
1223 if (rn1 == rn2)
1224 return 1;
1225
1226 return 0;
1227 }
1228
1229 DEFUN (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 =
1242 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
1243 }
1244
1245 return CMD_SUCCESS;
1246 }
1247
1248 DEFUN (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 =
1261 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
1262 }
1263
1264 return CMD_SUCCESS;
1265 }
1266
1267 ALIAS (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
1275 static int
1276 show_ip_bgp_scan_tables (struct vty *vty, const char detail)
1277 {
1278 struct bgp_node *rn;
1279 char buf[INET6_ADDRSTRLEN];
1280 struct nexthop *nexthop;
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
1288 #if BGP_SCAN_NEXTHOP
1289 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
1290 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
1291 if ((bnc = rn->info) != NULL)
1292 {
1293 if (bnc->valid)
1294 {
1295 vty_out (vty, " %s valid [IGP metric %d]%s",
1296 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1297 if (detail)
1298 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
1299 switch (nexthop->type)
1300 {
1301 case NEXTHOP_TYPE_IPV4:
1302 vty_out (vty, " gate %s%s", inet_ntop (AF_INET, &nexthop->gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1303 break;
1304 case NEXTHOP_TYPE_IPV4_IFINDEX:
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);
1307 break;
1308 case NEXTHOP_TYPE_IFINDEX:
1309 vty_out (vty, " ifidx %u%s", nexthop->ifindex, VTY_NEWLINE);
1310 break;
1311 default:
1312 vty_out (vty, " invalid nexthop type %u%s", nexthop->type, VTY_NEWLINE);
1313 }
1314 }
1315 else
1316 vty_out (vty, " %s invalid%s",
1317 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1318 }
1319
1320 #ifdef HAVE_IPV6
1321 {
1322 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1323 rn;
1324 rn = bgp_route_next (rn))
1325 if ((bnc = rn->info) != NULL)
1326 {
1327 if (bnc->valid)
1328 {
1329 vty_out (vty, " %s valid [IGP metric %d]%s",
1330 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
1331 bnc->metric, VTY_NEWLINE);
1332 if (detail)
1333 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
1334 switch (nexthop->type)
1335 {
1336 case NEXTHOP_TYPE_IPV6:
1337 vty_out (vty, " gate %s%s", inet_ntop (AF_INET6, &nexthop->gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1338 break;
1339 case NEXTHOP_TYPE_IFINDEX:
1340 vty_out (vty, " ifidx %u%s", nexthop->ifindex, VTY_NEWLINE);
1341 break;
1342 default:
1343 vty_out (vty, " invalid nexthop type %u%s", nexthop->type, VTY_NEWLINE);
1344 }
1345 }
1346 else
1347 vty_out (vty, " %s invalid%s",
1348 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
1349 VTY_NEWLINE);
1350 }
1351 }
1352 #endif /* HAVE_IPV6 */
1353 #else
1354 vty_out (vty, "BGP next-hop tracking is on%s", VTY_NEWLINE);
1355 #endif
1356 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
1357 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1358 rn;
1359 rn = bgp_route_next (rn))
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 {
1366 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1367 rn;
1368 rn = bgp_route_next (rn))
1369 if (rn->info != NULL)
1370 vty_out (vty, " %s/%d%s",
1371 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
1372 rn->p.prefixlen,
1373 VTY_NEWLINE);
1374 }
1375 #endif /* HAVE_IPV6 */
1376
1377 return CMD_SUCCESS;
1378 }
1379
1380 static int
1381 show_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];
1386 struct nexthop *nexthop;
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)
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 }
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)
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 }
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
1492 DEFUN (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
1503 DEFUN (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
1515 DEFUN (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
1526 DEFUN (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
1537 int
1538 bgp_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
1545 void
1546 bgp_scan_init (void)
1547 {
1548 zlookup = zclient_new ();
1549 zlookup->sock = -1;
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
1555 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1556 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1557 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
1558
1559 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1560
1561 #ifdef HAVE_IPV6
1562 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1563 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1564 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
1565 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1566 #endif /* HAVE_IPV6 */
1567
1568 /* Make BGP scan thread. */
1569 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1570 NULL, bgp_scan_interval);
1571 /* Make BGP import there. */
1572 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
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);
1578 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
1579 install_element (VIEW_NODE, &show_ip_bgp_nexthop_cmd);
1580 install_element (VIEW_NODE, &show_ip_bgp_nexthop_detail_cmd);
1581 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
1582 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
1583 install_element (ENABLE_NODE, &show_ip_bgp_nexthop_cmd);
1584 install_element (ENABLE_NODE, &show_ip_bgp_nexthop_detail_cmd);
1585 install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
1586 }
1587
1588 void
1589 bgp_scan_finish (void)
1590 {
1591 /* Only the current one needs to be reset. */
1592 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]);
1593
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
1604 /* Only the current one needs to be reset. */
1605 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]);
1606
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 }