2 Copyright (C) 2000 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
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
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.
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
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. */
42 struct bgp_nexthop_cache
*zlookup_query (struct in_addr
);
44 struct bgp_nexthop_cache
*zlookup_query_ipv6 (struct in6_addr
*);
45 #endif /* HAVE_IPV6 */
47 /* Only one BGP scan thread are activated at the same time. */
48 static struct thread
*bgp_scan_thread
= NULL
;
50 /* BGP import thread */
51 static struct thread
*bgp_import_thread
= NULL
;
53 /* BGP scan interval. */
54 static int bgp_scan_interval
;
56 /* BGP import interval. */
57 static int bgp_import_interval
;
59 /* Route table for next-hop lookup cache. */
60 static struct bgp_table
*bgp_nexthop_cache_table
[AFI_MAX
];
61 static struct bgp_table
*cache1_table
[AFI_MAX
];
62 static struct bgp_table
*cache2_table
[AFI_MAX
];
64 /* Route table for connected route. */
65 static struct bgp_table
*bgp_connected_table
[AFI_MAX
];
67 /* BGP nexthop lookup query client. */
68 static struct zclient
*zlookup
= NULL
;
70 /* Add nexthop to the end of the list. */
72 bnc_nexthop_add (struct bgp_nexthop_cache
*bnc
, struct nexthop
*nexthop
)
76 for (last
= bnc
->nexthop
; last
&& last
->next
; last
= last
->next
)
81 bnc
->nexthop
= nexthop
;
86 bnc_nexthop_free (struct bgp_nexthop_cache
*bnc
)
88 struct nexthop
*nexthop
;
89 struct nexthop
*next
= NULL
;
91 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= next
)
94 XFREE (MTYPE_NEXTHOP
, nexthop
);
98 static struct bgp_nexthop_cache
*
101 struct bgp_nexthop_cache
*new;
103 new = XMALLOC (MTYPE_BGP_NEXTHOP_CACHE
, sizeof (struct bgp_nexthop_cache
));
104 memset (new, 0, sizeof (struct bgp_nexthop_cache
));
109 bnc_free (struct bgp_nexthop_cache
*bnc
)
111 bnc_nexthop_free (bnc
);
112 XFREE (MTYPE_BGP_NEXTHOP_CACHE
, bnc
);
116 bgp_nexthop_same (struct nexthop
*next1
, struct nexthop
*next2
)
118 if (next1
->type
!= next2
->type
)
123 case ZEBRA_NEXTHOP_IPV4
:
124 if (! IPV4_ADDR_SAME (&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
127 case ZEBRA_NEXTHOP_IFINDEX
:
128 case ZEBRA_NEXTHOP_IFNAME
:
129 if (next1
->ifindex
!= next2
->ifindex
)
133 case ZEBRA_NEXTHOP_IPV6
:
134 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
137 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
138 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
139 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
141 if (next1
->ifindex
!= next2
->ifindex
)
144 #endif /* HAVE_IPV6 */
153 bgp_nexthop_cache_changed (struct bgp_nexthop_cache
*bnc1
,
154 struct bgp_nexthop_cache
*bnc2
)
157 struct nexthop
*next1
, *next2
;
159 if (bnc1
->nexthop_num
!= bnc2
->nexthop_num
)
162 next1
= bnc1
->nexthop
;
163 next2
= bnc2
->nexthop
;
165 for (i
= 0; i
< bnc1
->nexthop_num
; i
++)
167 if (! bgp_nexthop_same (next1
, next2
))
176 /* If nexthop exists on connected network return 1. */
178 bgp_nexthop_check_ebgp (afi_t afi
, struct attr
*attr
)
182 /* If zebra is not enabled return */
183 if (zlookup
->sock
< 0)
186 /* Lookup the address is onlink or not. */
189 rn
= bgp_node_match_ipv4 (bgp_connected_table
[AFI_IP
], &attr
->nexthop
);
192 bgp_unlock_node (rn
);
197 else if (afi
== AFI_IP6
)
199 if (attr
->extra
->mp_nexthop_len
== 32)
201 else if (attr
->extra
->mp_nexthop_len
== 16)
203 if (IN6_IS_ADDR_LINKLOCAL (&attr
->extra
->mp_nexthop_global
))
206 rn
= bgp_node_match_ipv6 (bgp_connected_table
[AFI_IP6
],
207 &attr
->extra
->mp_nexthop_global
);
210 bgp_unlock_node (rn
);
215 #endif /* HAVE_IPV6 */
220 /* Check specified next-hop is reachable or not. */
222 bgp_nexthop_lookup_ipv6 (struct peer
*peer
, struct bgp_info
*ri
, int *changed
,
227 struct bgp_nexthop_cache
*bnc
;
230 /* If lookup is not enabled, return valid. */
231 if (zlookup
->sock
< 0)
234 ri
->extra
->igpmetric
= 0;
238 /* Only check IPv6 global address only nexthop. */
241 if (attr
->extra
->mp_nexthop_len
!= 16
242 || IN6_IS_ADDR_LINKLOCAL (&attr
->extra
->mp_nexthop_global
))
245 memset (&p
, 0, sizeof (struct prefix
));
247 p
.prefixlen
= IPV6_MAX_BITLEN
;
248 p
.u
.prefix6
= attr
->extra
->mp_nexthop_global
;
250 /* IBGP or ebgp-multihop */
251 rn
= bgp_node_get (bgp_nexthop_cache_table
[AFI_IP6
], &p
);
256 bgp_unlock_node (rn
);
260 bnc
= zlookup_query_ipv6 (&attr
->extra
->mp_nexthop_global
);
263 struct bgp_table
*old
;
264 struct bgp_node
*oldrn
;
265 struct bgp_nexthop_cache
*oldbnc
;
269 if (bgp_nexthop_cache_table
[AFI_IP6
] == cache1_table
[AFI_IP6
])
270 old
= cache2_table
[AFI_IP6
];
272 old
= cache1_table
[AFI_IP6
];
274 oldrn
= bgp_node_lookup (old
, &p
);
277 oldbnc
= oldrn
->info
;
279 bnc
->changed
= bgp_nexthop_cache_changed (bnc
, oldbnc
);
281 if (bnc
->metric
!= oldbnc
->metric
)
282 bnc
->metricchanged
= 1;
295 *changed
= bnc
->changed
;
298 *metricchanged
= bnc
->metricchanged
;
300 if (bnc
->valid
&& bnc
->metric
)
301 (bgp_info_extra_get (ri
))->igpmetric
= bnc
->metric
;
303 ri
->extra
->igpmetric
= 0;
307 #endif /* HAVE_IPV6 */
309 /* Check specified next-hop is reachable or not. */
311 bgp_nexthop_lookup (afi_t afi
, struct peer
*peer
, struct bgp_info
*ri
,
312 int *changed
, int *metricchanged
)
316 struct bgp_nexthop_cache
*bnc
;
319 /* If lookup is not enabled, return valid. */
320 if (zlookup
->sock
< 0)
323 ri
->extra
->igpmetric
= 0;
329 return bgp_nexthop_lookup_ipv6 (peer
, ri
, changed
, metricchanged
);
330 #endif /* HAVE_IPV6 */
332 addr
= ri
->attr
->nexthop
;
334 memset (&p
, 0, sizeof (struct prefix
));
336 p
.prefixlen
= IPV4_MAX_BITLEN
;
339 /* IBGP or ebgp-multihop */
340 rn
= bgp_node_get (bgp_nexthop_cache_table
[AFI_IP
], &p
);
345 bgp_unlock_node (rn
);
349 bnc
= zlookup_query (addr
);
352 struct bgp_table
*old
;
353 struct bgp_node
*oldrn
;
354 struct bgp_nexthop_cache
*oldbnc
;
358 if (bgp_nexthop_cache_table
[AFI_IP
] == cache1_table
[AFI_IP
])
359 old
= cache2_table
[AFI_IP
];
361 old
= cache1_table
[AFI_IP
];
363 oldrn
= bgp_node_lookup (old
, &p
);
366 oldbnc
= oldrn
->info
;
368 bnc
->changed
= bgp_nexthop_cache_changed (bnc
, oldbnc
);
370 if (bnc
->metric
!= oldbnc
->metric
)
371 bnc
->metricchanged
= 1;
384 *changed
= bnc
->changed
;
387 *metricchanged
= bnc
->metricchanged
;
389 if (bnc
->valid
&& bnc
->metric
)
390 (bgp_info_extra_get(ri
))->igpmetric
= bnc
->metric
;
392 ri
->extra
->igpmetric
= 0;
397 /* Reset and free all BGP nexthop cache. */
399 bgp_nexthop_cache_reset (struct bgp_table
*table
)
402 struct bgp_nexthop_cache
*bnc
;
404 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
405 if ((bnc
= rn
->info
) != NULL
)
409 bgp_unlock_node (rn
);
414 bgp_scan (afi_t afi
, safi_t safi
)
419 struct bgp_info
*next
;
421 struct listnode
*node
, *nnode
;
428 if (bgp_nexthop_cache_table
[afi
] == cache1_table
[afi
])
429 bgp_nexthop_cache_table
[afi
] = cache2_table
[afi
];
431 bgp_nexthop_cache_table
[afi
] = cache1_table
[afi
];
433 /* Get default bgp. */
434 bgp
= bgp_get_default ();
438 /* Maximum prefix check */
439 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
441 if (peer
->status
!= Established
)
444 if (peer
->afc
[afi
][SAFI_UNICAST
])
445 bgp_maximum_prefix_overflow (peer
, afi
, SAFI_UNICAST
, 1);
446 if (peer
->afc
[afi
][SAFI_MULTICAST
])
447 bgp_maximum_prefix_overflow (peer
, afi
, SAFI_MULTICAST
, 1);
448 if (peer
->afc
[afi
][SAFI_MPLS_VPN
])
449 bgp_maximum_prefix_overflow (peer
, afi
, SAFI_MPLS_VPN
, 1);
452 for (rn
= bgp_table_top (bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
453 rn
= bgp_route_next (rn
))
455 for (bi
= rn
->info
; bi
; bi
= next
)
459 if (bi
->type
== ZEBRA_ROUTE_BGP
&& bi
->sub_type
== BGP_ROUTE_NORMAL
)
464 if (peer_sort (bi
->peer
) == BGP_PEER_EBGP
&& bi
->peer
->ttl
== 1)
465 valid
= bgp_nexthop_check_ebgp (afi
, bi
->attr
);
467 valid
= bgp_nexthop_lookup (afi
, bi
->peer
, bi
,
468 &changed
, &metricchanged
);
470 current
= CHECK_FLAG (bi
->flags
, BGP_INFO_VALID
) ? 1 : 0;
473 SET_FLAG (bi
->flags
, BGP_INFO_IGP_CHANGED
);
475 UNSET_FLAG (bi
->flags
, BGP_INFO_IGP_CHANGED
);
477 if (valid
!= current
)
479 if (CHECK_FLAG (bi
->flags
, BGP_INFO_VALID
))
481 bgp_aggregate_decrement (bgp
, &rn
->p
, bi
,
483 bgp_info_unset_flag (rn
, bi
, BGP_INFO_VALID
);
487 bgp_info_set_flag (rn
, bi
, BGP_INFO_VALID
);
488 bgp_aggregate_increment (bgp
, &rn
->p
, bi
,
493 if (CHECK_FLAG (bgp
->af_flags
[afi
][SAFI_UNICAST
],
494 BGP_CONFIG_DAMPENING
)
495 && bi
->extra
&& bi
->extra
->damp_info
)
496 if (bgp_damp_scan (bi
, afi
, SAFI_UNICAST
))
497 bgp_aggregate_increment (bgp
, &rn
->p
, bi
,
501 bgp_process (bgp
, rn
, afi
, SAFI_UNICAST
);
504 /* Flash old cache. */
505 if (bgp_nexthop_cache_table
[afi
] == cache1_table
[afi
])
506 bgp_nexthop_cache_reset (cache2_table
[afi
]);
508 bgp_nexthop_cache_reset (cache1_table
[afi
]);
510 if (BGP_DEBUG (events
, EVENTS
))
513 zlog_debug ("scanning IPv4 Unicast routing tables");
514 else if (afi
== AFI_IP6
)
515 zlog_debug ("scanning IPv6 Unicast routing tables");
519 /* BGP scan thread. This thread check nexthop reachability. */
521 bgp_scan_timer (struct thread
*t
)
524 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
526 if (BGP_DEBUG (events
, EVENTS
))
527 zlog_debug ("Performing BGP general scanning");
529 bgp_scan (AFI_IP
, SAFI_UNICAST
);
532 bgp_scan (AFI_IP6
, SAFI_UNICAST
);
533 #endif /* HAVE_IPV6 */
538 struct bgp_connected_ref
544 bgp_connected_add (struct connected
*ifc
)
548 struct interface
*ifp
;
550 struct bgp_connected_ref
*bc
;
557 if (if_is_loopback (ifp
))
562 if (addr
->family
== AF_INET
)
564 PREFIX_COPY_IPV4(&p
, CONNECTED_PREFIX(ifc
));
565 apply_mask_ipv4 ((struct prefix_ipv4
*) &p
);
567 if (prefix_ipv4_any ((struct prefix_ipv4
*) &p
))
570 rn
= bgp_node_get (bgp_connected_table
[AFI_IP
], (struct prefix
*) &p
);
578 bc
= XMALLOC (0, sizeof (struct bgp_connected_ref
));
579 memset (bc
, 0, sizeof (struct bgp_connected_ref
));
585 else if (addr
->family
== AF_INET6
)
587 PREFIX_COPY_IPV6(&p
, CONNECTED_PREFIX(ifc
));
588 apply_mask_ipv6 ((struct prefix_ipv6
*) &p
);
590 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
593 if (IN6_IS_ADDR_LINKLOCAL (&p
.u
.prefix6
))
596 rn
= bgp_node_get (bgp_connected_table
[AFI_IP6
], (struct prefix
*) &p
);
604 bc
= XMALLOC (0, sizeof (struct bgp_connected_ref
));
605 memset (bc
, 0, sizeof (struct bgp_connected_ref
));
610 #endif /* HAVE_IPV6 */
614 bgp_connected_delete (struct connected
*ifc
)
618 struct interface
*ifp
;
620 struct bgp_connected_ref
*bc
;
624 if (if_is_loopback (ifp
))
629 if (addr
->family
== AF_INET
)
631 PREFIX_COPY_IPV4(&p
, CONNECTED_PREFIX(ifc
));
632 apply_mask_ipv4 ((struct prefix_ipv4
*) &p
);
634 if (prefix_ipv4_any ((struct prefix_ipv4
*) &p
))
637 rn
= bgp_node_lookup (bgp_connected_table
[AFI_IP
], &p
);
648 bgp_unlock_node (rn
);
649 bgp_unlock_node (rn
);
652 else if (addr
->family
== AF_INET6
)
654 PREFIX_COPY_IPV6(&p
, CONNECTED_PREFIX(ifc
));
655 apply_mask_ipv6 ((struct prefix_ipv6
*) &p
);
657 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
660 if (IN6_IS_ADDR_LINKLOCAL (&p
.u
.prefix6
))
663 rn
= bgp_node_lookup (bgp_connected_table
[AFI_IP6
], (struct prefix
*) &p
);
674 bgp_unlock_node (rn
);
675 bgp_unlock_node (rn
);
677 #endif /* HAVE_IPV6 */
681 bgp_nexthop_self (afi_t afi
, struct attr
*attr
)
683 struct listnode
*node
;
684 struct listnode
*node2
;
685 struct interface
*ifp
;
686 struct connected
*ifc
;
689 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
691 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
695 if (p
&& p
->family
== AF_INET
696 && IPV4_ADDR_SAME (&p
->u
.prefix4
, &attr
->nexthop
))
703 static struct bgp_nexthop_cache
*
712 struct in_addr raddr
;
716 struct nexthop
*nexthop
;
717 struct bgp_nexthop_cache
*bnc
;
722 nbytes
= stream_read (s
, zlookup
->sock
, 2);
723 length
= stream_getw (s
);
725 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
726 marker
= stream_getc (s
);
727 version
= stream_getc (s
);
729 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
731 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
732 __func__
, zlookup
->sock
, marker
, version
);
736 command
= stream_getw (s
);
738 raddr
.s_addr
= stream_get_ipv4 (s
);
739 metric
= stream_getl (s
);
740 nexthop_num
= stream_getc (s
);
746 bnc
->metric
= metric
;
747 bnc
->nexthop_num
= nexthop_num
;
749 for (i
= 0; i
< nexthop_num
; i
++)
751 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
752 memset (nexthop
, 0, sizeof (struct nexthop
));
753 nexthop
->type
= stream_getc (s
);
754 switch (nexthop
->type
)
756 case ZEBRA_NEXTHOP_IPV4
:
757 nexthop
->gate
.ipv4
.s_addr
= stream_get_ipv4 (s
);
759 case ZEBRA_NEXTHOP_IFINDEX
:
760 case ZEBRA_NEXTHOP_IFNAME
:
761 nexthop
->ifindex
= stream_getl (s
);
767 bnc_nexthop_add (bnc
, nexthop
);
776 struct bgp_nexthop_cache
*
777 zlookup_query (struct in_addr addr
)
783 if (zlookup
->sock
< 0)
788 zclient_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP
);
789 stream_put_in_addr (s
, &addr
);
791 stream_putw_at (s
, 0, stream_get_endp (s
));
793 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
796 zlog_err ("can't write to zlookup->sock");
797 close (zlookup
->sock
);
803 zlog_err ("zlookup->sock connection closed");
804 close (zlookup
->sock
);
809 return zlookup_read ();
813 static struct bgp_nexthop_cache
*
818 u_char version
, marker
;
821 struct in6_addr raddr
;
825 struct nexthop
*nexthop
;
826 struct bgp_nexthop_cache
*bnc
;
831 nbytes
= stream_read (s
, zlookup
->sock
, 2);
832 length
= stream_getw (s
);
834 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
835 marker
= stream_getc (s
);
836 version
= stream_getc (s
);
838 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
840 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
841 __func__
, zlookup
->sock
, marker
, version
);
845 command
= stream_getw (s
);
847 stream_get (&raddr
, s
, 16);
849 metric
= stream_getl (s
);
850 nexthop_num
= stream_getc (s
);
856 bnc
->metric
= metric
;
857 bnc
->nexthop_num
= nexthop_num
;
859 for (i
= 0; i
< nexthop_num
; i
++)
861 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
862 memset (nexthop
, 0, sizeof (struct nexthop
));
863 nexthop
->type
= stream_getc (s
);
864 switch (nexthop
->type
)
866 case ZEBRA_NEXTHOP_IPV6
:
867 stream_get (&nexthop
->gate
.ipv6
, s
, 16);
869 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
870 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
871 stream_get (&nexthop
->gate
.ipv6
, s
, 16);
872 nexthop
->ifindex
= stream_getl (s
);
874 case ZEBRA_NEXTHOP_IFINDEX
:
875 case ZEBRA_NEXTHOP_IFNAME
:
876 nexthop
->ifindex
= stream_getl (s
);
882 bnc_nexthop_add (bnc
, nexthop
);
891 struct bgp_nexthop_cache
*
892 zlookup_query_ipv6 (struct in6_addr
*addr
)
898 if (zlookup
->sock
< 0)
903 zclient_create_header (s
, ZEBRA_IPV6_NEXTHOP_LOOKUP
);
904 stream_put (s
, addr
, 16);
905 stream_putw_at (s
, 0, stream_get_endp (s
));
907 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
910 zlog_err ("can't write to zlookup->sock");
911 close (zlookup
->sock
);
917 zlog_err ("zlookup->sock connection closed");
918 close (zlookup
->sock
);
923 return zlookup_read_ipv6 ();
925 #endif /* HAVE_IPV6 */
928 bgp_import_check (struct prefix
*p
, u_int32_t
*igpmetric
,
929 struct in_addr
*igpnexthop
)
933 u_int16_t length
, command
;
934 u_char version
, marker
;
937 struct in_addr nexthop
;
938 u_int32_t metric
= 0;
942 /* If lookup connection is not available return valid. */
943 if (zlookup
->sock
< 0)
950 /* Send query to the lookup connection */
953 zclient_create_header (s
, ZEBRA_IPV4_IMPORT_LOOKUP
);
955 stream_putc (s
, p
->prefixlen
);
956 stream_put_in_addr (s
, &p
->u
.prefix4
);
958 stream_putw_at (s
, 0, stream_get_endp (s
));
960 /* Write the packet. */
961 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
965 zlog_err ("can't write to zlookup->sock");
966 close (zlookup
->sock
);
972 zlog_err ("zlookup->sock connection closed");
973 close (zlookup
->sock
);
982 nbytes
= stream_read (s
, zlookup
->sock
, 2);
983 length
= stream_getw (s
);
985 /* Fetch whole data. */
986 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
987 marker
= stream_getc (s
);
988 version
= stream_getc (s
);
990 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
992 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
993 __func__
, zlookup
->sock
, marker
, version
);
997 command
= stream_getw (s
);
999 addr
.s_addr
= stream_get_ipv4 (s
);
1000 metric
= stream_getl (s
);
1001 nexthop_num
= stream_getc (s
);
1003 /* Set IGP metric value. */
1005 *igpmetric
= metric
;
1007 /* If there is nexthop then this is active route. */
1011 nexthop_type
= stream_getc (s
);
1012 if (nexthop_type
== ZEBRA_NEXTHOP_IPV4
)
1014 nexthop
.s_addr
= stream_get_ipv4 (s
);
1016 *igpnexthop
= nexthop
;
1019 *igpnexthop
= nexthop
;
1027 /* Scan all configured BGP route then check the route exists in IGP or
1030 bgp_import (struct thread
*t
)
1033 struct bgp_node
*rn
;
1034 struct bgp_static
*bgp_static
;
1035 struct listnode
*node
, *nnode
;
1038 struct in_addr nexthop
;
1043 thread_add_timer (master
, bgp_import
, NULL
, bgp_import_interval
);
1045 if (BGP_DEBUG (events
, EVENTS
))
1046 zlog_debug ("Import timer expired.");
1048 for (ALL_LIST_ELEMENTS (bm
->bgp
, node
, nnode
, bgp
))
1050 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1051 for (safi
= SAFI_UNICAST
; safi
< SAFI_MPLS_VPN
; safi
++)
1052 for (rn
= bgp_table_top (bgp
->route
[afi
][safi
]); rn
;
1053 rn
= bgp_route_next (rn
))
1054 if ((bgp_static
= rn
->info
) != NULL
)
1056 if (bgp_static
->backdoor
)
1059 valid
= bgp_static
->valid
;
1060 metric
= bgp_static
->igpmetric
;
1061 nexthop
= bgp_static
->igpnexthop
;
1063 if (bgp_flag_check (bgp
, BGP_FLAG_IMPORT_CHECK
)
1064 && afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
1065 bgp_static
->valid
= bgp_import_check (&rn
->p
, &bgp_static
->igpmetric
,
1066 &bgp_static
->igpnexthop
);
1069 bgp_static
->valid
= 1;
1070 bgp_static
->igpmetric
= 0;
1071 bgp_static
->igpnexthop
.s_addr
= 0;
1074 if (bgp_static
->valid
!= valid
)
1076 if (bgp_static
->valid
)
1077 bgp_static_update (bgp
, &rn
->p
, bgp_static
, afi
, safi
);
1079 bgp_static_withdraw (bgp
, &rn
->p
, afi
, safi
);
1081 else if (bgp_static
->valid
)
1083 if (bgp_static
->igpmetric
!= metric
1084 || bgp_static
->igpnexthop
.s_addr
!= nexthop
.s_addr
1085 || bgp_static
->rmap
.name
)
1086 bgp_static_update (bgp
, &rn
->p
, bgp_static
, afi
, safi
);
1093 /* Connect to zebra for nexthop lookup. */
1095 zlookup_connect (struct thread
*t
)
1097 struct zclient
*zlookup
;
1099 zlookup
= THREAD_ARG (t
);
1100 zlookup
->t_connect
= NULL
;
1102 if (zlookup
->sock
!= -1)
1105 #ifdef HAVE_TCP_ZEBRA
1106 zlookup
->sock
= zclient_socket ();
1108 zlookup
->sock
= zclient_socket_un (ZEBRA_SERV_PATH
);
1109 #endif /* HAVE_TCP_ZEBRA */
1110 if (zlookup
->sock
< 0)
1116 /* Check specified multiaccess next-hop. */
1118 bgp_multiaccess_check_v4 (struct in_addr nexthop
, char *peer
)
1120 struct bgp_node
*rn1
;
1121 struct bgp_node
*rn2
;
1124 struct in_addr addr
;
1127 ret
= inet_aton (peer
, &addr
);
1131 memset (&p1
, 0, sizeof (struct prefix
));
1132 p1
.family
= AF_INET
;
1133 p1
.prefixlen
= IPV4_MAX_BITLEN
;
1134 p1
.u
.prefix4
= nexthop
;
1135 memset (&p2
, 0, sizeof (struct prefix
));
1136 p2
.family
= AF_INET
;
1137 p2
.prefixlen
= IPV4_MAX_BITLEN
;
1138 p2
.u
.prefix4
= addr
;
1140 /* If bgp scan is not enabled, return invalid. */
1141 if (zlookup
->sock
< 0)
1144 rn1
= bgp_node_match (bgp_connected_table
[AFI_IP
], &p1
);
1148 rn2
= bgp_node_match (bgp_connected_table
[AFI_IP
], &p2
);
1158 DEFUN (bgp_scan_time
,
1160 "bgp scan-time <5-60>",
1161 "BGP specific commands\n"
1162 "Configure background scanner interval\n"
1163 "Scanner interval (seconds)\n")
1165 bgp_scan_interval
= atoi (argv
[0]);
1167 if (bgp_scan_thread
)
1169 thread_cancel (bgp_scan_thread
);
1171 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
1177 DEFUN (no_bgp_scan_time
,
1178 no_bgp_scan_time_cmd
,
1181 "BGP specific commands\n"
1182 "Configure background scanner interval\n")
1184 bgp_scan_interval
= BGP_SCAN_INTERVAL_DEFAULT
;
1186 if (bgp_scan_thread
)
1188 thread_cancel (bgp_scan_thread
);
1190 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
1196 ALIAS (no_bgp_scan_time
,
1197 no_bgp_scan_time_val_cmd
,
1198 "no bgp scan-time <5-60>",
1200 "BGP specific commands\n"
1201 "Configure background scanner interval\n"
1202 "Scanner interval (seconds)\n")
1204 DEFUN (show_ip_bgp_scan
,
1205 show_ip_bgp_scan_cmd
,
1210 "BGP scan status\n")
1212 struct bgp_node
*rn
;
1213 struct bgp_nexthop_cache
*bnc
;
1215 if (bgp_scan_thread
)
1216 vty_out (vty
, "BGP scan is running%s", VTY_NEWLINE
);
1218 vty_out (vty
, "BGP scan is not running%s", VTY_NEWLINE
);
1219 vty_out (vty
, "BGP scan interval is %d%s", bgp_scan_interval
, VTY_NEWLINE
);
1221 vty_out (vty
, "Current BGP nexthop cache:%s", VTY_NEWLINE
);
1222 for (rn
= bgp_table_top (bgp_nexthop_cache_table
[AFI_IP
]); rn
; rn
= bgp_route_next (rn
))
1223 if ((bnc
= rn
->info
) != NULL
)
1226 vty_out (vty
, " %s valid [IGP metric %d]%s",
1227 inet_ntoa (rn
->p
.u
.prefix4
), bnc
->metric
, VTY_NEWLINE
);
1229 vty_out (vty
, " %s invalid%s",
1230 inet_ntoa (rn
->p
.u
.prefix4
), VTY_NEWLINE
);
1236 for (rn
= bgp_table_top (bgp_nexthop_cache_table
[AFI_IP6
]);
1238 rn
= bgp_route_next (rn
))
1239 if ((bnc
= rn
->info
) != NULL
)
1242 vty_out (vty
, " %s valid [IGP metric %d]%s",
1243 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1244 bnc
->metric
, VTY_NEWLINE
);
1246 vty_out (vty
, " %s invalid%s",
1247 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1251 #endif /* HAVE_IPV6 */
1253 vty_out (vty
, "BGP connected route:%s", VTY_NEWLINE
);
1254 for (rn
= bgp_table_top (bgp_connected_table
[AFI_IP
]);
1256 rn
= bgp_route_next (rn
))
1257 if (rn
->info
!= NULL
)
1258 vty_out (vty
, " %s/%d%s", inet_ntoa (rn
->p
.u
.prefix4
), rn
->p
.prefixlen
,
1265 for (rn
= bgp_table_top (bgp_connected_table
[AFI_IP6
]);
1267 rn
= bgp_route_next (rn
))
1268 if (rn
->info
!= NULL
)
1269 vty_out (vty
, " %s/%d%s",
1270 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1274 #endif /* HAVE_IPV6 */
1280 bgp_config_write_scan_time (struct vty
*vty
)
1282 if (bgp_scan_interval
!= BGP_SCAN_INTERVAL_DEFAULT
)
1283 vty_out (vty
, " bgp scan-time %d%s", bgp_scan_interval
, VTY_NEWLINE
);
1290 zlookup
= zclient_new ();
1292 zlookup
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1293 zlookup
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1294 zlookup
->t_connect
= thread_add_event (master
, zlookup_connect
, zlookup
, 0);
1296 bgp_scan_interval
= BGP_SCAN_INTERVAL_DEFAULT
;
1297 bgp_import_interval
= BGP_IMPORT_INTERVAL_DEFAULT
;
1299 cache1_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1300 cache2_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1301 bgp_nexthop_cache_table
[AFI_IP
] = cache1_table
[AFI_IP
];
1303 bgp_connected_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1306 cache1_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1307 cache2_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1308 bgp_nexthop_cache_table
[AFI_IP6
] = cache1_table
[AFI_IP6
];
1309 bgp_connected_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1310 #endif /* HAVE_IPV6 */
1312 /* Make BGP scan thread. */
1313 bgp_scan_thread
= thread_add_timer (master
, bgp_scan_timer
,
1314 NULL
, bgp_scan_interval
);
1315 /* Make BGP import there. */
1316 bgp_import_thread
= thread_add_timer (master
, bgp_import
, NULL
, 0);
1318 install_element (BGP_NODE
, &bgp_scan_time_cmd
);
1319 install_element (BGP_NODE
, &no_bgp_scan_time_cmd
);
1320 install_element (BGP_NODE
, &no_bgp_scan_time_val_cmd
);
1321 install_element (VIEW_NODE
, &show_ip_bgp_scan_cmd
);
1322 install_element (RESTRICTED_NODE
, &show_ip_bgp_scan_cmd
);
1323 install_element (ENABLE_NODE
, &show_ip_bgp_scan_cmd
);