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 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 return XCALLOC (MTYPE_BGP_NEXTHOP_CACHE
, sizeof (struct bgp_nexthop_cache
));
105 bnc_free (struct bgp_nexthop_cache
*bnc
)
107 bnc_nexthop_free (bnc
);
108 XFREE (MTYPE_BGP_NEXTHOP_CACHE
, bnc
);
112 bgp_nexthop_same (struct nexthop
*next1
, struct nexthop
*next2
)
114 if (next1
->type
!= next2
->type
)
119 case ZEBRA_NEXTHOP_IPV4
:
120 if (! IPV4_ADDR_SAME (&next1
->gate
.ipv4
, &next2
->gate
.ipv4
))
123 case ZEBRA_NEXTHOP_IFINDEX
:
124 case ZEBRA_NEXTHOP_IFNAME
:
125 if (next1
->ifindex
!= next2
->ifindex
)
129 case ZEBRA_NEXTHOP_IPV6
:
130 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
133 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
134 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
135 if (! IPV6_ADDR_SAME (&next1
->gate
.ipv6
, &next2
->gate
.ipv6
))
137 if (next1
->ifindex
!= next2
->ifindex
)
140 #endif /* HAVE_IPV6 */
149 bgp_nexthop_cache_changed (struct bgp_nexthop_cache
*bnc1
,
150 struct bgp_nexthop_cache
*bnc2
)
153 struct nexthop
*next1
, *next2
;
155 if (bnc1
->nexthop_num
!= bnc2
->nexthop_num
)
158 next1
= bnc1
->nexthop
;
159 next2
= bnc2
->nexthop
;
161 for (i
= 0; i
< bnc1
->nexthop_num
; i
++)
163 if (! bgp_nexthop_same (next1
, next2
))
172 /* If nexthop exists on connected network return 1. */
174 bgp_nexthop_check_ebgp (afi_t afi
, struct attr
*attr
)
178 /* If zebra is not enabled return */
179 if (zlookup
->sock
< 0)
182 /* Lookup the address is onlink or not. */
185 rn
= bgp_node_match_ipv4 (bgp_connected_table
[AFI_IP
], &attr
->nexthop
);
188 bgp_unlock_node (rn
);
193 else if (afi
== AFI_IP6
)
195 if (attr
->extra
->mp_nexthop_len
== 32)
197 else if (attr
->extra
->mp_nexthop_len
== 16)
199 if (IN6_IS_ADDR_LINKLOCAL (&attr
->extra
->mp_nexthop_global
))
202 rn
= bgp_node_match_ipv6 (bgp_connected_table
[AFI_IP6
],
203 &attr
->extra
->mp_nexthop_global
);
206 bgp_unlock_node (rn
);
211 #endif /* HAVE_IPV6 */
216 /* Check specified next-hop is reachable or not. */
218 bgp_nexthop_lookup_ipv6 (struct peer
*peer
, struct bgp_info
*ri
, int *changed
,
223 struct bgp_nexthop_cache
*bnc
;
226 /* If lookup is not enabled, return valid. */
227 if (zlookup
->sock
< 0)
230 ri
->extra
->igpmetric
= 0;
234 /* Only check IPv6 global address only nexthop. */
237 if (attr
->extra
->mp_nexthop_len
!= 16
238 || IN6_IS_ADDR_LINKLOCAL (&attr
->extra
->mp_nexthop_global
))
241 memset (&p
, 0, sizeof (struct prefix
));
243 p
.prefixlen
= IPV6_MAX_BITLEN
;
244 p
.u
.prefix6
= attr
->extra
->mp_nexthop_global
;
246 /* IBGP or ebgp-multihop */
247 rn
= bgp_node_get (bgp_nexthop_cache_table
[AFI_IP6
], &p
);
252 bgp_unlock_node (rn
);
256 bnc
= zlookup_query_ipv6 (&attr
->extra
->mp_nexthop_global
);
259 struct bgp_table
*old
;
260 struct bgp_node
*oldrn
;
261 struct bgp_nexthop_cache
*oldbnc
;
265 if (bgp_nexthop_cache_table
[AFI_IP6
] == cache1_table
[AFI_IP6
])
266 old
= cache2_table
[AFI_IP6
];
268 old
= cache1_table
[AFI_IP6
];
270 oldrn
= bgp_node_lookup (old
, &p
);
273 oldbnc
= oldrn
->info
;
275 bnc
->changed
= bgp_nexthop_cache_changed (bnc
, oldbnc
);
277 if (bnc
->metric
!= oldbnc
->metric
)
278 bnc
->metricchanged
= 1;
291 *changed
= bnc
->changed
;
294 *metricchanged
= bnc
->metricchanged
;
296 if (bnc
->valid
&& bnc
->metric
)
297 (bgp_info_extra_get (ri
))->igpmetric
= bnc
->metric
;
299 ri
->extra
->igpmetric
= 0;
303 #endif /* HAVE_IPV6 */
305 /* Check specified next-hop is reachable or not. */
307 bgp_nexthop_lookup (afi_t afi
, struct peer
*peer
, struct bgp_info
*ri
,
308 int *changed
, int *metricchanged
)
312 struct bgp_nexthop_cache
*bnc
;
315 /* If lookup is not enabled, return valid. */
316 if (zlookup
->sock
< 0)
319 ri
->extra
->igpmetric
= 0;
325 return bgp_nexthop_lookup_ipv6 (peer
, ri
, changed
, metricchanged
);
326 #endif /* HAVE_IPV6 */
328 addr
= ri
->attr
->nexthop
;
330 memset (&p
, 0, sizeof (struct prefix
));
332 p
.prefixlen
= IPV4_MAX_BITLEN
;
335 /* IBGP or ebgp-multihop */
336 rn
= bgp_node_get (bgp_nexthop_cache_table
[AFI_IP
], &p
);
341 bgp_unlock_node (rn
);
345 bnc
= zlookup_query (addr
);
348 struct bgp_table
*old
;
349 struct bgp_node
*oldrn
;
350 struct bgp_nexthop_cache
*oldbnc
;
354 if (bgp_nexthop_cache_table
[AFI_IP
] == cache1_table
[AFI_IP
])
355 old
= cache2_table
[AFI_IP
];
357 old
= cache1_table
[AFI_IP
];
359 oldrn
= bgp_node_lookup (old
, &p
);
362 oldbnc
= oldrn
->info
;
364 bnc
->changed
= bgp_nexthop_cache_changed (bnc
, oldbnc
);
366 if (bnc
->metric
!= oldbnc
->metric
)
367 bnc
->metricchanged
= 1;
380 *changed
= bnc
->changed
;
383 *metricchanged
= bnc
->metricchanged
;
385 if (bnc
->valid
&& bnc
->metric
)
386 (bgp_info_extra_get(ri
))->igpmetric
= bnc
->metric
;
388 ri
->extra
->igpmetric
= 0;
393 /* Reset and free all BGP nexthop cache. */
395 bgp_nexthop_cache_reset (struct bgp_table
*table
)
398 struct bgp_nexthop_cache
*bnc
;
400 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
401 if ((bnc
= rn
->info
) != NULL
)
405 bgp_unlock_node (rn
);
410 bgp_scan (afi_t afi
, safi_t safi
)
415 struct bgp_info
*next
;
417 struct listnode
*node
, *nnode
;
424 if (bgp_nexthop_cache_table
[afi
] == cache1_table
[afi
])
425 bgp_nexthop_cache_table
[afi
] = cache2_table
[afi
];
427 bgp_nexthop_cache_table
[afi
] = cache1_table
[afi
];
429 /* Get default bgp. */
430 bgp
= bgp_get_default ();
434 /* Maximum prefix check */
435 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
437 if (peer
->status
!= Established
)
440 if (peer
->afc
[afi
][SAFI_UNICAST
])
441 bgp_maximum_prefix_overflow (peer
, afi
, SAFI_UNICAST
, 1);
442 if (peer
->afc
[afi
][SAFI_MULTICAST
])
443 bgp_maximum_prefix_overflow (peer
, afi
, SAFI_MULTICAST
, 1);
444 if (peer
->afc
[afi
][SAFI_MPLS_VPN
])
445 bgp_maximum_prefix_overflow (peer
, afi
, SAFI_MPLS_VPN
, 1);
448 for (rn
= bgp_table_top (bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
449 rn
= bgp_route_next (rn
))
451 for (bi
= rn
->info
; bi
; bi
= next
)
455 if (bi
->type
== ZEBRA_ROUTE_BGP
&& bi
->sub_type
== BGP_ROUTE_NORMAL
)
460 if (peer_sort (bi
->peer
) == BGP_PEER_EBGP
&& bi
->peer
->ttl
== 1)
461 valid
= bgp_nexthop_check_ebgp (afi
, bi
->attr
);
463 valid
= bgp_nexthop_lookup (afi
, bi
->peer
, bi
,
464 &changed
, &metricchanged
);
466 current
= CHECK_FLAG (bi
->flags
, BGP_INFO_VALID
) ? 1 : 0;
469 SET_FLAG (bi
->flags
, BGP_INFO_IGP_CHANGED
);
471 UNSET_FLAG (bi
->flags
, BGP_INFO_IGP_CHANGED
);
473 if (valid
!= current
)
475 if (CHECK_FLAG (bi
->flags
, BGP_INFO_VALID
))
477 bgp_aggregate_decrement (bgp
, &rn
->p
, bi
,
479 bgp_info_unset_flag (rn
, bi
, BGP_INFO_VALID
);
483 bgp_info_set_flag (rn
, bi
, BGP_INFO_VALID
);
484 bgp_aggregate_increment (bgp
, &rn
->p
, bi
,
489 if (CHECK_FLAG (bgp
->af_flags
[afi
][SAFI_UNICAST
],
490 BGP_CONFIG_DAMPENING
)
491 && bi
->extra
&& bi
->extra
->damp_info
)
492 if (bgp_damp_scan (bi
, afi
, SAFI_UNICAST
))
493 bgp_aggregate_increment (bgp
, &rn
->p
, bi
,
497 bgp_process (bgp
, rn
, afi
, SAFI_UNICAST
);
500 /* Flash old cache. */
501 if (bgp_nexthop_cache_table
[afi
] == cache1_table
[afi
])
502 bgp_nexthop_cache_reset (cache2_table
[afi
]);
504 bgp_nexthop_cache_reset (cache1_table
[afi
]);
506 if (BGP_DEBUG (events
, EVENTS
))
509 zlog_debug ("scanning IPv4 Unicast routing tables");
510 else if (afi
== AFI_IP6
)
511 zlog_debug ("scanning IPv6 Unicast routing tables");
515 /* BGP scan thread. This thread check nexthop reachability. */
517 bgp_scan_timer (struct thread
*t
)
520 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
522 if (BGP_DEBUG (events
, EVENTS
))
523 zlog_debug ("Performing BGP general scanning");
525 bgp_scan (AFI_IP
, SAFI_UNICAST
);
528 bgp_scan (AFI_IP6
, SAFI_UNICAST
);
529 #endif /* HAVE_IPV6 */
534 struct bgp_connected_ref
540 bgp_connected_add (struct connected
*ifc
)
544 struct interface
*ifp
;
546 struct bgp_connected_ref
*bc
;
553 if (if_is_loopback (ifp
))
558 if (addr
->family
== AF_INET
)
560 PREFIX_COPY_IPV4(&p
, CONNECTED_PREFIX(ifc
));
561 apply_mask_ipv4 ((struct prefix_ipv4
*) &p
);
563 if (prefix_ipv4_any ((struct prefix_ipv4
*) &p
))
566 rn
= bgp_node_get (bgp_connected_table
[AFI_IP
], (struct prefix
*) &p
);
574 bc
= XCALLOC (0, sizeof (struct bgp_connected_ref
));
580 else if (addr
->family
== AF_INET6
)
582 PREFIX_COPY_IPV6(&p
, CONNECTED_PREFIX(ifc
));
583 apply_mask_ipv6 ((struct prefix_ipv6
*) &p
);
585 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
588 if (IN6_IS_ADDR_LINKLOCAL (&p
.u
.prefix6
))
591 rn
= bgp_node_get (bgp_connected_table
[AFI_IP6
], (struct prefix
*) &p
);
599 bc
= XCALLOC (0, sizeof (struct bgp_connected_ref
));
604 #endif /* HAVE_IPV6 */
608 bgp_connected_delete (struct connected
*ifc
)
612 struct interface
*ifp
;
614 struct bgp_connected_ref
*bc
;
618 if (if_is_loopback (ifp
))
623 if (addr
->family
== AF_INET
)
625 PREFIX_COPY_IPV4(&p
, CONNECTED_PREFIX(ifc
));
626 apply_mask_ipv4 ((struct prefix_ipv4
*) &p
);
628 if (prefix_ipv4_any ((struct prefix_ipv4
*) &p
))
631 rn
= bgp_node_lookup (bgp_connected_table
[AFI_IP
], &p
);
642 bgp_unlock_node (rn
);
643 bgp_unlock_node (rn
);
646 else if (addr
->family
== AF_INET6
)
648 PREFIX_COPY_IPV6(&p
, CONNECTED_PREFIX(ifc
));
649 apply_mask_ipv6 ((struct prefix_ipv6
*) &p
);
651 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
654 if (IN6_IS_ADDR_LINKLOCAL (&p
.u
.prefix6
))
657 rn
= bgp_node_lookup (bgp_connected_table
[AFI_IP6
], (struct prefix
*) &p
);
668 bgp_unlock_node (rn
);
669 bgp_unlock_node (rn
);
671 #endif /* HAVE_IPV6 */
675 bgp_nexthop_self (afi_t afi
, struct attr
*attr
)
677 struct listnode
*node
;
678 struct listnode
*node2
;
679 struct interface
*ifp
;
680 struct connected
*ifc
;
683 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
685 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
689 if (p
&& p
->family
== AF_INET
690 && IPV4_ADDR_SAME (&p
->u
.prefix4
, &attr
->nexthop
))
697 static struct bgp_nexthop_cache
*
706 struct in_addr raddr
;
710 struct nexthop
*nexthop
;
711 struct bgp_nexthop_cache
*bnc
;
716 nbytes
= stream_read (s
, zlookup
->sock
, 2);
717 length
= stream_getw (s
);
719 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
720 marker
= stream_getc (s
);
721 version
= stream_getc (s
);
723 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
725 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
726 __func__
, zlookup
->sock
, marker
, version
);
730 command
= stream_getw (s
);
732 raddr
.s_addr
= stream_get_ipv4 (s
);
733 metric
= stream_getl (s
);
734 nexthop_num
= stream_getc (s
);
740 bnc
->metric
= metric
;
741 bnc
->nexthop_num
= nexthop_num
;
743 for (i
= 0; i
< nexthop_num
; i
++)
745 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
746 nexthop
->type
= stream_getc (s
);
747 switch (nexthop
->type
)
749 case ZEBRA_NEXTHOP_IPV4
:
750 nexthop
->gate
.ipv4
.s_addr
= stream_get_ipv4 (s
);
752 case ZEBRA_NEXTHOP_IFINDEX
:
753 case ZEBRA_NEXTHOP_IFNAME
:
754 nexthop
->ifindex
= stream_getl (s
);
760 bnc_nexthop_add (bnc
, nexthop
);
769 struct bgp_nexthop_cache
*
770 zlookup_query (struct in_addr addr
)
776 if (zlookup
->sock
< 0)
781 zclient_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP
);
782 stream_put_in_addr (s
, &addr
);
784 stream_putw_at (s
, 0, stream_get_endp (s
));
786 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
789 zlog_err ("can't write to zlookup->sock");
790 close (zlookup
->sock
);
796 zlog_err ("zlookup->sock connection closed");
797 close (zlookup
->sock
);
802 return zlookup_read ();
806 static struct bgp_nexthop_cache
*
807 zlookup_read_ipv6 (void)
811 u_char version
, marker
;
814 struct in6_addr raddr
;
818 struct nexthop
*nexthop
;
819 struct bgp_nexthop_cache
*bnc
;
824 nbytes
= stream_read (s
, zlookup
->sock
, 2);
825 length
= stream_getw (s
);
827 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
828 marker
= stream_getc (s
);
829 version
= stream_getc (s
);
831 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
833 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
834 __func__
, zlookup
->sock
, marker
, version
);
838 command
= stream_getw (s
);
840 stream_get (&raddr
, s
, 16);
842 metric
= stream_getl (s
);
843 nexthop_num
= stream_getc (s
);
849 bnc
->metric
= metric
;
850 bnc
->nexthop_num
= nexthop_num
;
852 for (i
= 0; i
< nexthop_num
; i
++)
854 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
855 nexthop
->type
= stream_getc (s
);
856 switch (nexthop
->type
)
858 case ZEBRA_NEXTHOP_IPV6
:
859 stream_get (&nexthop
->gate
.ipv6
, s
, 16);
861 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
862 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
863 stream_get (&nexthop
->gate
.ipv6
, s
, 16);
864 nexthop
->ifindex
= stream_getl (s
);
866 case ZEBRA_NEXTHOP_IFINDEX
:
867 case ZEBRA_NEXTHOP_IFNAME
:
868 nexthop
->ifindex
= stream_getl (s
);
874 bnc_nexthop_add (bnc
, nexthop
);
883 struct bgp_nexthop_cache
*
884 zlookup_query_ipv6 (struct in6_addr
*addr
)
890 if (zlookup
->sock
< 0)
895 zclient_create_header (s
, ZEBRA_IPV6_NEXTHOP_LOOKUP
);
896 stream_put (s
, addr
, 16);
897 stream_putw_at (s
, 0, stream_get_endp (s
));
899 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
902 zlog_err ("can't write to zlookup->sock");
903 close (zlookup
->sock
);
909 zlog_err ("zlookup->sock connection closed");
910 close (zlookup
->sock
);
915 return zlookup_read_ipv6 ();
917 #endif /* HAVE_IPV6 */
920 bgp_import_check (struct prefix
*p
, u_int32_t
*igpmetric
,
921 struct in_addr
*igpnexthop
)
925 u_int16_t length
, command
;
926 u_char version
, marker
;
929 struct in_addr nexthop
;
930 u_int32_t metric
= 0;
934 /* If lookup connection is not available return valid. */
935 if (zlookup
->sock
< 0)
942 /* Send query to the lookup connection */
945 zclient_create_header (s
, ZEBRA_IPV4_IMPORT_LOOKUP
);
947 stream_putc (s
, p
->prefixlen
);
948 stream_put_in_addr (s
, &p
->u
.prefix4
);
950 stream_putw_at (s
, 0, stream_get_endp (s
));
952 /* Write the packet. */
953 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
957 zlog_err ("can't write to zlookup->sock");
958 close (zlookup
->sock
);
964 zlog_err ("zlookup->sock connection closed");
965 close (zlookup
->sock
);
974 nbytes
= stream_read (s
, zlookup
->sock
, 2);
975 length
= stream_getw (s
);
977 /* Fetch whole data. */
978 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
979 marker
= stream_getc (s
);
980 version
= stream_getc (s
);
982 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
984 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
985 __func__
, zlookup
->sock
, marker
, version
);
989 command
= stream_getw (s
);
991 addr
.s_addr
= stream_get_ipv4 (s
);
992 metric
= stream_getl (s
);
993 nexthop_num
= stream_getc (s
);
995 /* Set IGP metric value. */
999 /* If there is nexthop then this is active route. */
1003 nexthop_type
= stream_getc (s
);
1004 if (nexthop_type
== ZEBRA_NEXTHOP_IPV4
)
1006 nexthop
.s_addr
= stream_get_ipv4 (s
);
1008 *igpnexthop
= nexthop
;
1011 *igpnexthop
= nexthop
;
1019 /* Scan all configured BGP route then check the route exists in IGP or
1022 bgp_import (struct thread
*t
)
1025 struct bgp_node
*rn
;
1026 struct bgp_static
*bgp_static
;
1027 struct listnode
*node
, *nnode
;
1030 struct in_addr nexthop
;
1035 thread_add_timer (master
, bgp_import
, NULL
, bgp_import_interval
);
1037 if (BGP_DEBUG (events
, EVENTS
))
1038 zlog_debug ("Import timer expired.");
1040 for (ALL_LIST_ELEMENTS (bm
->bgp
, node
, nnode
, bgp
))
1042 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1043 for (safi
= SAFI_UNICAST
; safi
< SAFI_MPLS_VPN
; safi
++)
1044 for (rn
= bgp_table_top (bgp
->route
[afi
][safi
]); rn
;
1045 rn
= bgp_route_next (rn
))
1046 if ((bgp_static
= rn
->info
) != NULL
)
1048 if (bgp_static
->backdoor
)
1051 valid
= bgp_static
->valid
;
1052 metric
= bgp_static
->igpmetric
;
1053 nexthop
= bgp_static
->igpnexthop
;
1055 if (bgp_flag_check (bgp
, BGP_FLAG_IMPORT_CHECK
)
1056 && afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
1057 bgp_static
->valid
= bgp_import_check (&rn
->p
, &bgp_static
->igpmetric
,
1058 &bgp_static
->igpnexthop
);
1061 bgp_static
->valid
= 1;
1062 bgp_static
->igpmetric
= 0;
1063 bgp_static
->igpnexthop
.s_addr
= 0;
1066 if (bgp_static
->valid
!= valid
)
1068 if (bgp_static
->valid
)
1069 bgp_static_update (bgp
, &rn
->p
, bgp_static
, afi
, safi
);
1071 bgp_static_withdraw (bgp
, &rn
->p
, afi
, safi
);
1073 else if (bgp_static
->valid
)
1075 if (bgp_static
->igpmetric
!= metric
1076 || bgp_static
->igpnexthop
.s_addr
!= nexthop
.s_addr
1077 || bgp_static
->rmap
.name
)
1078 bgp_static_update (bgp
, &rn
->p
, bgp_static
, afi
, safi
);
1085 /* Connect to zebra for nexthop lookup. */
1087 zlookup_connect (struct thread
*t
)
1089 struct zclient
*zlookup
;
1091 zlookup
= THREAD_ARG (t
);
1092 zlookup
->t_connect
= NULL
;
1094 if (zlookup
->sock
!= -1)
1097 #ifdef HAVE_TCP_ZEBRA
1098 zlookup
->sock
= zclient_socket ();
1100 zlookup
->sock
= zclient_socket_un (ZEBRA_SERV_PATH
);
1101 #endif /* HAVE_TCP_ZEBRA */
1102 if (zlookup
->sock
< 0)
1108 /* Check specified multiaccess next-hop. */
1110 bgp_multiaccess_check_v4 (struct in_addr nexthop
, char *peer
)
1112 struct bgp_node
*rn1
;
1113 struct bgp_node
*rn2
;
1116 struct in_addr addr
;
1119 ret
= inet_aton (peer
, &addr
);
1123 memset (&p1
, 0, sizeof (struct prefix
));
1124 p1
.family
= AF_INET
;
1125 p1
.prefixlen
= IPV4_MAX_BITLEN
;
1126 p1
.u
.prefix4
= nexthop
;
1127 memset (&p2
, 0, sizeof (struct prefix
));
1128 p2
.family
= AF_INET
;
1129 p2
.prefixlen
= IPV4_MAX_BITLEN
;
1130 p2
.u
.prefix4
= addr
;
1132 /* If bgp scan is not enabled, return invalid. */
1133 if (zlookup
->sock
< 0)
1136 rn1
= bgp_node_match (bgp_connected_table
[AFI_IP
], &p1
);
1140 rn2
= bgp_node_match (bgp_connected_table
[AFI_IP
], &p2
);
1150 DEFUN (bgp_scan_time
,
1152 "bgp scan-time <5-60>",
1153 "BGP specific commands\n"
1154 "Configure background scanner interval\n"
1155 "Scanner interval (seconds)\n")
1157 bgp_scan_interval
= atoi (argv
[0]);
1159 if (bgp_scan_thread
)
1161 thread_cancel (bgp_scan_thread
);
1163 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
1169 DEFUN (no_bgp_scan_time
,
1170 no_bgp_scan_time_cmd
,
1173 "BGP specific commands\n"
1174 "Configure background scanner interval\n")
1176 bgp_scan_interval
= BGP_SCAN_INTERVAL_DEFAULT
;
1178 if (bgp_scan_thread
)
1180 thread_cancel (bgp_scan_thread
);
1182 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
1188 ALIAS (no_bgp_scan_time
,
1189 no_bgp_scan_time_val_cmd
,
1190 "no bgp scan-time <5-60>",
1192 "BGP specific commands\n"
1193 "Configure background scanner interval\n"
1194 "Scanner interval (seconds)\n")
1196 DEFUN (show_ip_bgp_scan
,
1197 show_ip_bgp_scan_cmd
,
1202 "BGP scan status\n")
1204 struct bgp_node
*rn
;
1205 struct bgp_nexthop_cache
*bnc
;
1207 if (bgp_scan_thread
)
1208 vty_out (vty
, "BGP scan is running%s", VTY_NEWLINE
);
1210 vty_out (vty
, "BGP scan is not running%s", VTY_NEWLINE
);
1211 vty_out (vty
, "BGP scan interval is %d%s", bgp_scan_interval
, VTY_NEWLINE
);
1213 vty_out (vty
, "Current BGP nexthop cache:%s", VTY_NEWLINE
);
1214 for (rn
= bgp_table_top (bgp_nexthop_cache_table
[AFI_IP
]); rn
; rn
= bgp_route_next (rn
))
1215 if ((bnc
= rn
->info
) != NULL
)
1218 vty_out (vty
, " %s valid [IGP metric %d]%s",
1219 inet_ntoa (rn
->p
.u
.prefix4
), bnc
->metric
, VTY_NEWLINE
);
1221 vty_out (vty
, " %s invalid%s",
1222 inet_ntoa (rn
->p
.u
.prefix4
), VTY_NEWLINE
);
1228 for (rn
= bgp_table_top (bgp_nexthop_cache_table
[AFI_IP6
]);
1230 rn
= bgp_route_next (rn
))
1231 if ((bnc
= rn
->info
) != NULL
)
1234 vty_out (vty
, " %s valid [IGP metric %d]%s",
1235 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1236 bnc
->metric
, VTY_NEWLINE
);
1238 vty_out (vty
, " %s invalid%s",
1239 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1243 #endif /* HAVE_IPV6 */
1245 vty_out (vty
, "BGP connected route:%s", VTY_NEWLINE
);
1246 for (rn
= bgp_table_top (bgp_connected_table
[AFI_IP
]);
1248 rn
= bgp_route_next (rn
))
1249 if (rn
->info
!= NULL
)
1250 vty_out (vty
, " %s/%d%s", inet_ntoa (rn
->p
.u
.prefix4
), rn
->p
.prefixlen
,
1257 for (rn
= bgp_table_top (bgp_connected_table
[AFI_IP6
]);
1259 rn
= bgp_route_next (rn
))
1260 if (rn
->info
!= NULL
)
1261 vty_out (vty
, " %s/%d%s",
1262 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1266 #endif /* HAVE_IPV6 */
1272 bgp_config_write_scan_time (struct vty
*vty
)
1274 if (bgp_scan_interval
!= BGP_SCAN_INTERVAL_DEFAULT
)
1275 vty_out (vty
, " bgp scan-time %d%s", bgp_scan_interval
, VTY_NEWLINE
);
1280 bgp_scan_init (void)
1282 zlookup
= zclient_new ();
1284 zlookup
->t_connect
= thread_add_event (master
, zlookup_connect
, zlookup
, 0);
1286 bgp_scan_interval
= BGP_SCAN_INTERVAL_DEFAULT
;
1287 bgp_import_interval
= BGP_IMPORT_INTERVAL_DEFAULT
;
1289 cache1_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1290 cache2_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1291 bgp_nexthop_cache_table
[AFI_IP
] = cache1_table
[AFI_IP
];
1293 bgp_connected_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1296 cache1_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1297 cache2_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1298 bgp_nexthop_cache_table
[AFI_IP6
] = cache1_table
[AFI_IP6
];
1299 bgp_connected_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1300 #endif /* HAVE_IPV6 */
1302 /* Make BGP scan thread. */
1303 bgp_scan_thread
= thread_add_timer (master
, bgp_scan_timer
,
1304 NULL
, bgp_scan_interval
);
1305 /* Make BGP import there. */
1306 bgp_import_thread
= thread_add_timer (master
, bgp_import
, NULL
, 0);
1308 install_element (BGP_NODE
, &bgp_scan_time_cmd
);
1309 install_element (BGP_NODE
, &no_bgp_scan_time_cmd
);
1310 install_element (BGP_NODE
, &no_bgp_scan_time_val_cmd
);
1311 install_element (VIEW_NODE
, &show_ip_bgp_scan_cmd
);
1312 install_element (RESTRICTED_NODE
, &show_ip_bgp_scan_cmd
);
1313 install_element (ENABLE_NODE
, &show_ip_bgp_scan_cmd
);
1317 bgp_scan_finish (void)
1319 bgp_table_unlock (cache1_table
[AFI_IP
]);
1320 cache1_table
[AFI_IP
] = NULL
;
1322 bgp_table_unlock (cache2_table
[AFI_IP
]);
1323 cache2_table
[AFI_IP
] = NULL
;
1325 bgp_table_unlock (bgp_connected_table
[AFI_IP
]);
1326 bgp_connected_table
[AFI_IP
] = NULL
;
1329 bgp_table_unlock (cache1_table
[AFI_IP6
]);
1330 cache1_table
[AFI_IP6
] = NULL
;
1332 bgp_table_unlock (cache2_table
[AFI_IP6
]);
1333 cache2_table
[AFI_IP6
] = NULL
;
1335 bgp_table_unlock (bgp_connected_table
[AFI_IP6
]);
1336 bgp_connected_table
[AFI_IP6
] = NULL
;
1337 #endif /* HAVE_IPV6 */