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
->mp_nexthop_len
== 32)
201 else if (attr
->mp_nexthop_len
== 16)
203 if (IN6_IS_ADDR_LINKLOCAL (&attr
->mp_nexthop_global
))
206 rn
= bgp_node_match_ipv6 (bgp_connected_table
[AFI_IP6
],
207 &attr
->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)
237 /* Only check IPv6 global address only nexthop. */
240 if (attr
->mp_nexthop_len
!= 16
241 || IN6_IS_ADDR_LINKLOCAL (&attr
->mp_nexthop_global
))
244 memset (&p
, 0, sizeof (struct prefix
));
246 p
.prefixlen
= IPV6_MAX_BITLEN
;
247 p
.u
.prefix6
= attr
->mp_nexthop_global
;
249 /* IBGP or ebgp-multihop */
250 rn
= bgp_node_get (bgp_nexthop_cache_table
[AFI_IP6
], &p
);
255 bgp_unlock_node (rn
);
259 bnc
= zlookup_query_ipv6 (&attr
->mp_nexthop_global
);
262 struct bgp_table
*old
;
263 struct bgp_node
*oldrn
;
264 struct bgp_nexthop_cache
*oldbnc
;
268 if (bgp_nexthop_cache_table
[AFI_IP6
] == cache1_table
[AFI_IP6
])
269 old
= cache2_table
[AFI_IP6
];
271 old
= cache1_table
[AFI_IP6
];
273 oldrn
= bgp_node_lookup (old
, &p
);
276 oldbnc
= oldrn
->info
;
278 bnc
->changed
= bgp_nexthop_cache_changed (bnc
, oldbnc
);
280 if (bnc
->metric
!= oldbnc
->metric
)
281 bnc
->metricchanged
= 1;
294 *changed
= bnc
->changed
;
297 *metricchanged
= bnc
->metricchanged
;
300 ri
->igpmetric
= bnc
->metric
;
306 #endif /* HAVE_IPV6 */
308 /* Check specified next-hop is reachable or not. */
310 bgp_nexthop_lookup (afi_t afi
, struct peer
*peer
, struct bgp_info
*ri
,
311 int *changed
, int *metricchanged
)
315 struct bgp_nexthop_cache
*bnc
;
318 /* If lookup is not enabled, return valid. */
319 if (zlookup
->sock
< 0)
327 return bgp_nexthop_lookup_ipv6 (peer
, ri
, changed
, metricchanged
);
328 #endif /* HAVE_IPV6 */
330 addr
= ri
->attr
->nexthop
;
332 memset (&p
, 0, sizeof (struct prefix
));
334 p
.prefixlen
= IPV4_MAX_BITLEN
;
337 /* IBGP or ebgp-multihop */
338 rn
= bgp_node_get (bgp_nexthop_cache_table
[AFI_IP
], &p
);
343 bgp_unlock_node (rn
);
347 bnc
= zlookup_query (addr
);
350 struct bgp_table
*old
;
351 struct bgp_node
*oldrn
;
352 struct bgp_nexthop_cache
*oldbnc
;
356 if (bgp_nexthop_cache_table
[AFI_IP
] == cache1_table
[AFI_IP
])
357 old
= cache2_table
[AFI_IP
];
359 old
= cache1_table
[AFI_IP
];
361 oldrn
= bgp_node_lookup (old
, &p
);
364 oldbnc
= oldrn
->info
;
366 bnc
->changed
= bgp_nexthop_cache_changed (bnc
, oldbnc
);
368 if (bnc
->metric
!= oldbnc
->metric
)
369 bnc
->metricchanged
= 1;
382 *changed
= bnc
->changed
;
385 *metricchanged
= bnc
->metricchanged
;
388 ri
->igpmetric
= bnc
->metric
;
395 /* Reset and free all BGP nexthop cache. */
397 bgp_nexthop_cache_reset (struct bgp_table
*table
)
400 struct bgp_nexthop_cache
*bnc
;
402 for (rn
= bgp_table_top (table
); rn
; rn
= bgp_route_next (rn
))
403 if ((bnc
= rn
->info
) != NULL
)
407 bgp_unlock_node (rn
);
412 bgp_scan (afi_t afi
, safi_t safi
)
417 struct bgp_info
*next
;
419 struct listnode
*node
, *nnode
;
426 if (bgp_nexthop_cache_table
[afi
] == cache1_table
[afi
])
427 bgp_nexthop_cache_table
[afi
] = cache2_table
[afi
];
429 bgp_nexthop_cache_table
[afi
] = cache1_table
[afi
];
431 /* Get default bgp. */
432 bgp
= bgp_get_default ();
436 /* Maximum prefix check */
437 for (ALL_LIST_ELEMENTS (bgp
->peer
, node
, nnode
, peer
))
439 if (peer
->status
!= Established
)
442 if (peer
->afc
[afi
][SAFI_UNICAST
])
443 bgp_maximum_prefix_overflow (peer
, afi
, SAFI_UNICAST
, 1);
444 if (peer
->afc
[afi
][SAFI_MULTICAST
])
445 bgp_maximum_prefix_overflow (peer
, afi
, SAFI_MULTICAST
, 1);
446 if (peer
->afc
[afi
][SAFI_MPLS_VPN
])
447 bgp_maximum_prefix_overflow (peer
, afi
, SAFI_MPLS_VPN
, 1);
450 for (rn
= bgp_table_top (bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
451 rn
= bgp_route_next (rn
))
453 for (bi
= rn
->info
; bi
; bi
= next
)
457 if (bi
->type
== ZEBRA_ROUTE_BGP
&& bi
->sub_type
== BGP_ROUTE_NORMAL
)
462 if (peer_sort (bi
->peer
) == BGP_PEER_EBGP
&& bi
->peer
->ttl
== 1)
463 valid
= bgp_nexthop_check_ebgp (afi
, bi
->attr
);
465 valid
= bgp_nexthop_lookup (afi
, bi
->peer
, bi
,
466 &changed
, &metricchanged
);
468 current
= CHECK_FLAG (bi
->flags
, BGP_INFO_VALID
) ? 1 : 0;
471 SET_FLAG (bi
->flags
, BGP_INFO_IGP_CHANGED
);
473 UNSET_FLAG (bi
->flags
, BGP_INFO_IGP_CHANGED
);
475 if (valid
!= current
)
477 if (CHECK_FLAG (bi
->flags
, BGP_INFO_VALID
))
479 bgp_aggregate_decrement (bgp
, &rn
->p
, bi
,
481 bgp_info_unset_flag (rn
, bi
, BGP_INFO_VALID
);
485 bgp_info_set_flag (rn
, bi
, BGP_INFO_VALID
);
486 bgp_aggregate_increment (bgp
, &rn
->p
, bi
,
491 if (CHECK_FLAG (bgp
->af_flags
[afi
][SAFI_UNICAST
],
492 BGP_CONFIG_DAMPENING
)
494 if (bgp_damp_scan (bi
, afi
, SAFI_UNICAST
))
495 bgp_aggregate_increment (bgp
, &rn
->p
, bi
,
499 bgp_process (bgp
, rn
, afi
, SAFI_UNICAST
);
502 /* Flash old cache. */
503 if (bgp_nexthop_cache_table
[afi
] == cache1_table
[afi
])
504 bgp_nexthop_cache_reset (cache2_table
[afi
]);
506 bgp_nexthop_cache_reset (cache1_table
[afi
]);
508 if (BGP_DEBUG (events
, EVENTS
))
511 zlog_debug ("scanning IPv4 Unicast routing tables");
512 else if (afi
== AFI_IP6
)
513 zlog_debug ("scanning IPv6 Unicast routing tables");
517 /* BGP scan thread. This thread check nexthop reachability. */
519 bgp_scan_timer (struct thread
*t
)
522 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
524 if (BGP_DEBUG (events
, EVENTS
))
525 zlog_debug ("Performing BGP general scanning");
527 bgp_scan (AFI_IP
, SAFI_UNICAST
);
530 bgp_scan (AFI_IP6
, SAFI_UNICAST
);
531 #endif /* HAVE_IPV6 */
536 struct bgp_connected_ref
542 bgp_connected_add (struct connected
*ifc
)
546 struct interface
*ifp
;
548 struct bgp_connected_ref
*bc
;
555 if (if_is_loopback (ifp
))
560 if (addr
->family
== AF_INET
)
562 PREFIX_COPY_IPV4(&p
, CONNECTED_PREFIX(ifc
));
563 apply_mask_ipv4 ((struct prefix_ipv4
*) &p
);
565 if (prefix_ipv4_any ((struct prefix_ipv4
*) &p
))
568 rn
= bgp_node_get (bgp_connected_table
[AFI_IP
], (struct prefix
*) &p
);
576 bc
= XMALLOC (0, sizeof (struct bgp_connected_ref
));
577 memset (bc
, 0, sizeof (struct bgp_connected_ref
));
583 else if (addr
->family
== AF_INET6
)
585 PREFIX_COPY_IPV6(&p
, CONNECTED_PREFIX(ifc
));
586 apply_mask_ipv6 ((struct prefix_ipv6
*) &p
);
588 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
591 if (IN6_IS_ADDR_LINKLOCAL (&p
.u
.prefix6
))
594 rn
= bgp_node_get (bgp_connected_table
[AFI_IP6
], (struct prefix
*) &p
);
602 bc
= XMALLOC (0, sizeof (struct bgp_connected_ref
));
603 memset (bc
, 0, sizeof (struct bgp_connected_ref
));
608 #endif /* HAVE_IPV6 */
612 bgp_connected_delete (struct connected
*ifc
)
616 struct interface
*ifp
;
618 struct bgp_connected_ref
*bc
;
622 if (if_is_loopback (ifp
))
627 if (addr
->family
== AF_INET
)
629 PREFIX_COPY_IPV4(&p
, CONNECTED_PREFIX(ifc
));
630 apply_mask_ipv4 ((struct prefix_ipv4
*) &p
);
632 if (prefix_ipv4_any ((struct prefix_ipv4
*) &p
))
635 rn
= bgp_node_lookup (bgp_connected_table
[AFI_IP
], &p
);
646 bgp_unlock_node (rn
);
647 bgp_unlock_node (rn
);
650 else if (addr
->family
== AF_INET6
)
652 PREFIX_COPY_IPV6(&p
, CONNECTED_PREFIX(ifc
));
653 apply_mask_ipv6 ((struct prefix_ipv6
*) &p
);
655 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
658 if (IN6_IS_ADDR_LINKLOCAL (&p
.u
.prefix6
))
661 rn
= bgp_node_lookup (bgp_connected_table
[AFI_IP6
], (struct prefix
*) &p
);
672 bgp_unlock_node (rn
);
673 bgp_unlock_node (rn
);
675 #endif /* HAVE_IPV6 */
679 bgp_nexthop_self (afi_t afi
, struct attr
*attr
)
681 struct listnode
*node
;
682 struct listnode
*node2
;
683 struct interface
*ifp
;
684 struct connected
*ifc
;
687 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
689 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
693 if (p
&& p
->family
== AF_INET
694 && IPV4_ADDR_SAME (&p
->u
.prefix4
, &attr
->nexthop
))
701 static struct bgp_nexthop_cache
*
710 struct in_addr raddr
;
714 struct nexthop
*nexthop
;
715 struct bgp_nexthop_cache
*bnc
;
720 nbytes
= stream_read (s
, zlookup
->sock
, 2);
721 length
= stream_getw (s
);
723 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
724 marker
= stream_getc (s
);
725 version
= stream_getc (s
);
727 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
729 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
730 __func__
, zlookup
->sock
, marker
, version
);
734 command
= stream_getw (s
);
736 raddr
.s_addr
= stream_get_ipv4 (s
);
737 metric
= stream_getl (s
);
738 nexthop_num
= stream_getc (s
);
744 bnc
->metric
= metric
;
745 bnc
->nexthop_num
= nexthop_num
;
747 for (i
= 0; i
< nexthop_num
; i
++)
749 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
750 memset (nexthop
, 0, sizeof (struct nexthop
));
751 nexthop
->type
= stream_getc (s
);
752 switch (nexthop
->type
)
754 case ZEBRA_NEXTHOP_IPV4
:
755 nexthop
->gate
.ipv4
.s_addr
= stream_get_ipv4 (s
);
757 case ZEBRA_NEXTHOP_IFINDEX
:
758 case ZEBRA_NEXTHOP_IFNAME
:
759 nexthop
->ifindex
= stream_getl (s
);
765 bnc_nexthop_add (bnc
, nexthop
);
774 struct bgp_nexthop_cache
*
775 zlookup_query (struct in_addr addr
)
781 if (zlookup
->sock
< 0)
786 zclient_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP
);
787 stream_put_in_addr (s
, &addr
);
789 stream_putw_at (s
, 0, stream_get_endp (s
));
791 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
794 zlog_err ("can't write to zlookup->sock");
795 close (zlookup
->sock
);
801 zlog_err ("zlookup->sock connection closed");
802 close (zlookup
->sock
);
807 return zlookup_read ();
811 static struct bgp_nexthop_cache
*
816 u_char version
, marker
;
819 struct in6_addr raddr
;
823 struct nexthop
*nexthop
;
824 struct bgp_nexthop_cache
*bnc
;
829 nbytes
= stream_read (s
, zlookup
->sock
, 2);
830 length
= stream_getw (s
);
832 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
833 marker
= stream_getc (s
);
834 version
= stream_getc (s
);
836 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
838 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
839 __func__
, zlookup
->sock
, marker
, version
);
843 command
= stream_getw (s
);
845 stream_get (&raddr
, s
, 16);
847 metric
= stream_getl (s
);
848 nexthop_num
= stream_getc (s
);
854 bnc
->metric
= metric
;
855 bnc
->nexthop_num
= nexthop_num
;
857 for (i
= 0; i
< nexthop_num
; i
++)
859 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
860 memset (nexthop
, 0, sizeof (struct nexthop
));
861 nexthop
->type
= stream_getc (s
);
862 switch (nexthop
->type
)
864 case ZEBRA_NEXTHOP_IPV6
:
865 stream_get (&nexthop
->gate
.ipv6
, s
, 16);
867 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
868 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
869 stream_get (&nexthop
->gate
.ipv6
, s
, 16);
870 nexthop
->ifindex
= stream_getl (s
);
872 case ZEBRA_NEXTHOP_IFINDEX
:
873 case ZEBRA_NEXTHOP_IFNAME
:
874 nexthop
->ifindex
= stream_getl (s
);
880 bnc_nexthop_add (bnc
, nexthop
);
889 struct bgp_nexthop_cache
*
890 zlookup_query_ipv6 (struct in6_addr
*addr
)
896 if (zlookup
->sock
< 0)
901 zclient_create_header (s
, ZEBRA_IPV6_NEXTHOP_LOOKUP
);
902 stream_put (s
, addr
, 16);
903 stream_putw_at (s
, 0, stream_get_endp (s
));
905 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
908 zlog_err ("can't write to zlookup->sock");
909 close (zlookup
->sock
);
915 zlog_err ("zlookup->sock connection closed");
916 close (zlookup
->sock
);
921 return zlookup_read_ipv6 ();
923 #endif /* HAVE_IPV6 */
926 bgp_import_check (struct prefix
*p
, u_int32_t
*igpmetric
,
927 struct in_addr
*igpnexthop
)
931 u_int16_t length
, command
;
932 u_char version
, marker
;
935 struct in_addr nexthop
;
936 u_int32_t metric
= 0;
940 /* If lookup connection is not available return valid. */
941 if (zlookup
->sock
< 0)
948 /* Send query to the lookup connection */
951 zclient_create_header (s
, ZEBRA_IPV4_IMPORT_LOOKUP
);
953 stream_putc (s
, p
->prefixlen
);
954 stream_put_in_addr (s
, &p
->u
.prefix4
);
956 stream_putw_at (s
, 0, stream_get_endp (s
));
958 /* Write the packet. */
959 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
963 zlog_err ("can't write to zlookup->sock");
964 close (zlookup
->sock
);
970 zlog_err ("zlookup->sock connection closed");
971 close (zlookup
->sock
);
980 nbytes
= stream_read (s
, zlookup
->sock
, 2);
981 length
= stream_getw (s
);
983 /* Fetch whole data. */
984 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
985 marker
= stream_getc (s
);
986 version
= stream_getc (s
);
988 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
990 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
991 __func__
, zlookup
->sock
, marker
, version
);
995 command
= stream_getw (s
);
997 addr
.s_addr
= stream_get_ipv4 (s
);
998 metric
= stream_getl (s
);
999 nexthop_num
= stream_getc (s
);
1001 /* Set IGP metric value. */
1003 *igpmetric
= metric
;
1005 /* If there is nexthop then this is active route. */
1009 nexthop_type
= stream_getc (s
);
1010 if (nexthop_type
== ZEBRA_NEXTHOP_IPV4
)
1012 nexthop
.s_addr
= stream_get_ipv4 (s
);
1014 *igpnexthop
= nexthop
;
1017 *igpnexthop
= nexthop
;
1025 /* Scan all configured BGP route then check the route exists in IGP or
1028 bgp_import (struct thread
*t
)
1031 struct bgp_node
*rn
;
1032 struct bgp_static
*bgp_static
;
1033 struct listnode
*node
, *nnode
;
1036 struct in_addr nexthop
;
1041 thread_add_timer (master
, bgp_import
, NULL
, bgp_import_interval
);
1043 if (BGP_DEBUG (events
, EVENTS
))
1044 zlog_debug ("Import timer expired.");
1046 for (ALL_LIST_ELEMENTS (bm
->bgp
, node
, nnode
, bgp
))
1048 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1049 for (safi
= SAFI_UNICAST
; safi
< SAFI_MPLS_VPN
; safi
++)
1050 for (rn
= bgp_table_top (bgp
->route
[afi
][safi
]); rn
;
1051 rn
= bgp_route_next (rn
))
1052 if ((bgp_static
= rn
->info
) != NULL
)
1054 if (bgp_static
->backdoor
)
1057 valid
= bgp_static
->valid
;
1058 metric
= bgp_static
->igpmetric
;
1059 nexthop
= bgp_static
->igpnexthop
;
1061 if (bgp_flag_check (bgp
, BGP_FLAG_IMPORT_CHECK
)
1062 && afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
1063 bgp_static
->valid
= bgp_import_check (&rn
->p
, &bgp_static
->igpmetric
,
1064 &bgp_static
->igpnexthop
);
1067 bgp_static
->valid
= 1;
1068 bgp_static
->igpmetric
= 0;
1069 bgp_static
->igpnexthop
.s_addr
= 0;
1072 if (bgp_static
->valid
!= valid
)
1074 if (bgp_static
->valid
)
1075 bgp_static_update (bgp
, &rn
->p
, bgp_static
, afi
, safi
);
1077 bgp_static_withdraw (bgp
, &rn
->p
, afi
, safi
);
1079 else if (bgp_static
->valid
)
1081 if (bgp_static
->igpmetric
!= metric
1082 || bgp_static
->igpnexthop
.s_addr
!= nexthop
.s_addr
1083 || bgp_static
->rmap
.name
)
1084 bgp_static_update (bgp
, &rn
->p
, bgp_static
, afi
, safi
);
1091 /* Connect to zebra for nexthop lookup. */
1093 zlookup_connect (struct thread
*t
)
1095 struct zclient
*zlookup
;
1097 zlookup
= THREAD_ARG (t
);
1098 zlookup
->t_connect
= NULL
;
1100 if (zlookup
->sock
!= -1)
1103 #ifdef HAVE_TCP_ZEBRA
1104 zlookup
->sock
= zclient_socket ();
1106 zlookup
->sock
= zclient_socket_un (ZEBRA_SERV_PATH
);
1107 #endif /* HAVE_TCP_ZEBRA */
1108 if (zlookup
->sock
< 0)
1114 /* Check specified multiaccess next-hop. */
1116 bgp_multiaccess_check_v4 (struct in_addr nexthop
, char *peer
)
1118 struct bgp_node
*rn1
;
1119 struct bgp_node
*rn2
;
1122 struct in_addr addr
;
1125 ret
= inet_aton (peer
, &addr
);
1129 memset (&p1
, 0, sizeof (struct prefix
));
1130 p1
.family
= AF_INET
;
1131 p1
.prefixlen
= IPV4_MAX_BITLEN
;
1132 p1
.u
.prefix4
= nexthop
;
1133 memset (&p2
, 0, sizeof (struct prefix
));
1134 p2
.family
= AF_INET
;
1135 p2
.prefixlen
= IPV4_MAX_BITLEN
;
1136 p2
.u
.prefix4
= addr
;
1138 /* If bgp scan is not enabled, return invalid. */
1139 if (zlookup
->sock
< 0)
1142 rn1
= bgp_node_match (bgp_connected_table
[AFI_IP
], &p1
);
1146 rn2
= bgp_node_match (bgp_connected_table
[AFI_IP
], &p2
);
1156 DEFUN (bgp_scan_time
,
1158 "bgp scan-time <5-60>",
1159 "BGP specific commands\n"
1160 "Configure background scanner interval\n"
1161 "Scanner interval (seconds)\n")
1163 bgp_scan_interval
= atoi (argv
[0]);
1165 if (bgp_scan_thread
)
1167 thread_cancel (bgp_scan_thread
);
1169 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
1175 DEFUN (no_bgp_scan_time
,
1176 no_bgp_scan_time_cmd
,
1179 "BGP specific commands\n"
1180 "Configure background scanner interval\n")
1182 bgp_scan_interval
= BGP_SCAN_INTERVAL_DEFAULT
;
1184 if (bgp_scan_thread
)
1186 thread_cancel (bgp_scan_thread
);
1188 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
1194 ALIAS (no_bgp_scan_time
,
1195 no_bgp_scan_time_val_cmd
,
1196 "no bgp scan-time <5-60>",
1198 "BGP specific commands\n"
1199 "Configure background scanner interval\n"
1200 "Scanner interval (seconds)\n")
1202 DEFUN (show_ip_bgp_scan
,
1203 show_ip_bgp_scan_cmd
,
1208 "BGP scan status\n")
1210 struct bgp_node
*rn
;
1211 struct bgp_nexthop_cache
*bnc
;
1213 if (bgp_scan_thread
)
1214 vty_out (vty
, "BGP scan is running%s", VTY_NEWLINE
);
1216 vty_out (vty
, "BGP scan is not running%s", VTY_NEWLINE
);
1217 vty_out (vty
, "BGP scan interval is %d%s", bgp_scan_interval
, VTY_NEWLINE
);
1219 vty_out (vty
, "Current BGP nexthop cache:%s", VTY_NEWLINE
);
1220 for (rn
= bgp_table_top (bgp_nexthop_cache_table
[AFI_IP
]); rn
; rn
= bgp_route_next (rn
))
1221 if ((bnc
= rn
->info
) != NULL
)
1224 vty_out (vty
, " %s valid [IGP metric %d]%s",
1225 inet_ntoa (rn
->p
.u
.prefix4
), bnc
->metric
, VTY_NEWLINE
);
1227 vty_out (vty
, " %s invalid%s",
1228 inet_ntoa (rn
->p
.u
.prefix4
), VTY_NEWLINE
);
1234 for (rn
= bgp_table_top (bgp_nexthop_cache_table
[AFI_IP6
]);
1236 rn
= bgp_route_next (rn
))
1237 if ((bnc
= rn
->info
) != NULL
)
1240 vty_out (vty
, " %s valid [IGP metric %d]%s",
1241 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1242 bnc
->metric
, VTY_NEWLINE
);
1244 vty_out (vty
, " %s invalid%s",
1245 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1249 #endif /* HAVE_IPV6 */
1251 vty_out (vty
, "BGP connected route:%s", VTY_NEWLINE
);
1252 for (rn
= bgp_table_top (bgp_connected_table
[AFI_IP
]);
1254 rn
= bgp_route_next (rn
))
1255 if (rn
->info
!= NULL
)
1256 vty_out (vty
, " %s/%d%s", inet_ntoa (rn
->p
.u
.prefix4
), rn
->p
.prefixlen
,
1263 for (rn
= bgp_table_top (bgp_connected_table
[AFI_IP6
]);
1265 rn
= bgp_route_next (rn
))
1266 if (rn
->info
!= NULL
)
1267 vty_out (vty
, " %s/%d%s",
1268 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1272 #endif /* HAVE_IPV6 */
1278 bgp_config_write_scan_time (struct vty
*vty
)
1280 if (bgp_scan_interval
!= BGP_SCAN_INTERVAL_DEFAULT
)
1281 vty_out (vty
, " bgp scan-time %d%s", bgp_scan_interval
, VTY_NEWLINE
);
1288 zlookup
= zclient_new ();
1290 zlookup
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1291 zlookup
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1292 zlookup
->t_connect
= thread_add_event (master
, zlookup_connect
, zlookup
, 0);
1294 bgp_scan_interval
= BGP_SCAN_INTERVAL_DEFAULT
;
1295 bgp_import_interval
= BGP_IMPORT_INTERVAL_DEFAULT
;
1297 cache1_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1298 cache2_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1299 bgp_nexthop_cache_table
[AFI_IP
] = cache1_table
[AFI_IP
];
1301 bgp_connected_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1304 cache1_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1305 cache2_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1306 bgp_nexthop_cache_table
[AFI_IP6
] = cache1_table
[AFI_IP6
];
1307 bgp_connected_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1308 #endif /* HAVE_IPV6 */
1310 /* Make BGP scan thread. */
1311 bgp_scan_thread
= thread_add_timer (master
, bgp_scan_timer
,
1312 NULL
, bgp_scan_interval
);
1313 /* Make BGP import there. */
1314 bgp_import_thread
= thread_add_timer (master
, bgp_import
, NULL
, 0);
1316 install_element (BGP_NODE
, &bgp_scan_time_cmd
);
1317 install_element (BGP_NODE
, &no_bgp_scan_time_cmd
);
1318 install_element (BGP_NODE
, &no_bgp_scan_time_val_cmd
);
1319 install_element (VIEW_NODE
, &show_ip_bgp_scan_cmd
);
1320 install_element (ENABLE_NODE
, &show_ip_bgp_scan_cmd
);