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 UNSET_FLAG (bi
->flags
, BGP_INFO_VALID
);
485 SET_FLAG (bi
->flags
, 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
)
547 struct interface
*ifp
;
549 struct bgp_connected_ref
*bc
;
556 if (if_is_loopback (ifp
))
560 dest
= ifc
->destination
;
562 if (addr
->family
== AF_INET
)
564 memset (&p
, 0, sizeof (struct prefix
));
566 p
.prefixlen
= addr
->prefixlen
;
568 if (CONNECTED_POINTOPOINT_HOST(ifc
))
569 p
.u
.prefix4
= dest
->u
.prefix4
;
571 p
.u
.prefix4
= addr
->u
.prefix4
;
573 apply_mask_ipv4 ((struct prefix_ipv4
*) &p
);
575 if (prefix_ipv4_any ((struct prefix_ipv4
*) &p
))
578 rn
= bgp_node_get (bgp_connected_table
[AFI_IP
], (struct prefix
*) &p
);
586 bc
= XMALLOC (0, sizeof (struct bgp_connected_ref
));
587 memset (bc
, 0, sizeof (struct bgp_connected_ref
));
593 if (addr
->family
== AF_INET6
)
595 memset (&p
, 0, sizeof (struct prefix
));
597 p
.prefixlen
= addr
->prefixlen
;
599 if (if_is_pointopoint (ifp
) && dest
)
600 p
.u
.prefix6
= dest
->u
.prefix6
;
602 p
.u
.prefix6
= addr
->u
.prefix6
;
604 apply_mask_ipv6 ((struct prefix_ipv6
*) &p
);
606 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
609 if (IN6_IS_ADDR_LINKLOCAL (&p
.u
.prefix6
))
612 rn
= bgp_node_get (bgp_connected_table
[AFI_IP6
], (struct prefix
*) &p
);
620 bc
= XMALLOC (0, sizeof (struct bgp_connected_ref
));
621 memset (bc
, 0, sizeof (struct bgp_connected_ref
));
626 #endif /* HAVE_IPV6 */
630 bgp_connected_delete (struct connected
*ifc
)
635 struct interface
*ifp
;
637 struct bgp_connected_ref
*bc
;
641 if (if_is_loopback (ifp
))
645 dest
= ifc
->destination
;
647 if (addr
->family
== AF_INET
)
649 memset (&p
, 0, sizeof (struct prefix
));
651 p
.prefixlen
= addr
->prefixlen
;
653 if (CONNECTED_POINTOPOINT_HOST(ifc
))
654 p
.u
.prefix4
= dest
->u
.prefix4
;
656 p
.u
.prefix4
= addr
->u
.prefix4
;
658 apply_mask_ipv4 ((struct prefix_ipv4
*) &p
);
660 if (prefix_ipv4_any ((struct prefix_ipv4
*) &p
))
663 rn
= bgp_node_lookup (bgp_connected_table
[AFI_IP
], &p
);
674 bgp_unlock_node (rn
);
675 bgp_unlock_node (rn
);
678 else if (addr
->family
== AF_INET6
)
680 memset (&p
, 0, sizeof (struct prefix
));
682 p
.prefixlen
= addr
->prefixlen
;
684 if (if_is_pointopoint (ifp
) && dest
)
685 p
.u
.prefix6
= dest
->u
.prefix6
;
687 p
.u
.prefix6
= addr
->u
.prefix6
;
689 apply_mask_ipv6 ((struct prefix_ipv6
*) &p
);
691 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
694 if (IN6_IS_ADDR_LINKLOCAL (&p
.u
.prefix6
))
697 rn
= bgp_node_lookup (bgp_connected_table
[AFI_IP6
], (struct prefix
*) &p
);
708 bgp_unlock_node (rn
);
709 bgp_unlock_node (rn
);
711 #endif /* HAVE_IPV6 */
715 bgp_nexthop_self (afi_t afi
, struct attr
*attr
)
717 struct listnode
*node
;
718 struct listnode
*node2
;
719 struct interface
*ifp
;
720 struct connected
*ifc
;
723 for (ALL_LIST_ELEMENTS_RO (iflist
, node
, ifp
))
725 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node2
, ifc
))
729 if (p
&& p
->family
== AF_INET
730 && IPV4_ADDR_SAME (&p
->u
.prefix4
, &attr
->nexthop
))
737 static struct bgp_nexthop_cache
*
746 struct in_addr raddr
;
750 struct nexthop
*nexthop
;
751 struct bgp_nexthop_cache
*bnc
;
756 nbytes
= stream_read (s
, zlookup
->sock
, 2);
757 length
= stream_getw (s
);
759 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
760 marker
= stream_getc (s
);
761 version
= stream_getc (s
);
763 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
765 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
766 __func__
, zlookup
->sock
, marker
, version
);
770 command
= stream_getw (s
);
772 raddr
.s_addr
= stream_get_ipv4 (s
);
773 metric
= stream_getl (s
);
774 nexthop_num
= stream_getc (s
);
780 bnc
->metric
= metric
;
781 bnc
->nexthop_num
= nexthop_num
;
783 for (i
= 0; i
< nexthop_num
; i
++)
785 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
786 memset (nexthop
, 0, sizeof (struct nexthop
));
787 nexthop
->type
= stream_getc (s
);
788 switch (nexthop
->type
)
790 case ZEBRA_NEXTHOP_IPV4
:
791 nexthop
->gate
.ipv4
.s_addr
= stream_get_ipv4 (s
);
793 case ZEBRA_NEXTHOP_IFINDEX
:
794 case ZEBRA_NEXTHOP_IFNAME
:
795 nexthop
->ifindex
= stream_getl (s
);
801 bnc_nexthop_add (bnc
, nexthop
);
810 struct bgp_nexthop_cache
*
811 zlookup_query (struct in_addr addr
)
817 if (zlookup
->sock
< 0)
822 zclient_create_header (s
, ZEBRA_IPV4_NEXTHOP_LOOKUP
);
823 stream_put_in_addr (s
, &addr
);
825 stream_putw_at (s
, 0, stream_get_endp (s
));
827 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
830 zlog_err ("can't write to zlookup->sock");
831 close (zlookup
->sock
);
837 zlog_err ("zlookup->sock connection closed");
838 close (zlookup
->sock
);
843 return zlookup_read ();
847 static struct bgp_nexthop_cache
*
852 u_char version
, marker
;
855 struct in6_addr raddr
;
859 struct nexthop
*nexthop
;
860 struct bgp_nexthop_cache
*bnc
;
865 nbytes
= stream_read (s
, zlookup
->sock
, 2);
866 length
= stream_getw (s
);
868 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
869 marker
= stream_getc (s
);
870 version
= stream_getc (s
);
872 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
874 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
875 __func__
, zlookup
->sock
, marker
, version
);
879 command
= stream_getw (s
);
881 stream_get (&raddr
, s
, 16);
883 metric
= stream_getl (s
);
884 nexthop_num
= stream_getc (s
);
890 bnc
->metric
= metric
;
891 bnc
->nexthop_num
= nexthop_num
;
893 for (i
= 0; i
< nexthop_num
; i
++)
895 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
896 memset (nexthop
, 0, sizeof (struct nexthop
));
897 nexthop
->type
= stream_getc (s
);
898 switch (nexthop
->type
)
900 case ZEBRA_NEXTHOP_IPV6
:
901 stream_get (&nexthop
->gate
.ipv6
, s
, 16);
903 case ZEBRA_NEXTHOP_IPV6_IFINDEX
:
904 case ZEBRA_NEXTHOP_IPV6_IFNAME
:
905 stream_get (&nexthop
->gate
.ipv6
, s
, 16);
906 nexthop
->ifindex
= stream_getl (s
);
908 case ZEBRA_NEXTHOP_IFINDEX
:
909 case ZEBRA_NEXTHOP_IFNAME
:
910 nexthop
->ifindex
= stream_getl (s
);
916 bnc_nexthop_add (bnc
, nexthop
);
925 struct bgp_nexthop_cache
*
926 zlookup_query_ipv6 (struct in6_addr
*addr
)
932 if (zlookup
->sock
< 0)
937 zclient_create_header (s
, ZEBRA_IPV6_NEXTHOP_LOOKUP
);
938 stream_put (s
, addr
, 16);
939 stream_putw_at (s
, 0, stream_get_endp (s
));
941 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
944 zlog_err ("can't write to zlookup->sock");
945 close (zlookup
->sock
);
951 zlog_err ("zlookup->sock connection closed");
952 close (zlookup
->sock
);
957 return zlookup_read_ipv6 ();
959 #endif /* HAVE_IPV6 */
962 bgp_import_check (struct prefix
*p
, u_int32_t
*igpmetric
,
963 struct in_addr
*igpnexthop
)
967 u_int16_t length
, command
;
968 u_char version
, marker
;
971 struct in_addr nexthop
;
972 u_int32_t metric
= 0;
976 /* If lookup connection is not available return valid. */
977 if (zlookup
->sock
< 0)
984 /* Send query to the lookup connection */
987 zclient_create_header (s
, ZEBRA_IPV4_IMPORT_LOOKUP
);
989 stream_putc (s
, p
->prefixlen
);
990 stream_put_in_addr (s
, &p
->u
.prefix4
);
992 stream_putw_at (s
, 0, stream_get_endp (s
));
994 /* Write the packet. */
995 ret
= writen (zlookup
->sock
, s
->data
, stream_get_endp (s
));
999 zlog_err ("can't write to zlookup->sock");
1000 close (zlookup
->sock
);
1006 zlog_err ("zlookup->sock connection closed");
1007 close (zlookup
->sock
);
1016 nbytes
= stream_read (s
, zlookup
->sock
, 2);
1017 length
= stream_getw (s
);
1019 /* Fetch whole data. */
1020 nbytes
= stream_read (s
, zlookup
->sock
, length
- 2);
1021 marker
= stream_getc (s
);
1022 version
= stream_getc (s
);
1024 if (version
!= ZSERV_VERSION
|| marker
!= ZEBRA_HEADER_MARKER
)
1026 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1027 __func__
, zlookup
->sock
, marker
, version
);
1031 command
= stream_getw (s
);
1033 addr
.s_addr
= stream_get_ipv4 (s
);
1034 metric
= stream_getl (s
);
1035 nexthop_num
= stream_getc (s
);
1037 /* Set IGP metric value. */
1039 *igpmetric
= metric
;
1041 /* If there is nexthop then this is active route. */
1045 nexthop_type
= stream_getc (s
);
1046 if (nexthop_type
== ZEBRA_NEXTHOP_IPV4
)
1048 nexthop
.s_addr
= stream_get_ipv4 (s
);
1050 *igpnexthop
= nexthop
;
1053 *igpnexthop
= nexthop
;
1061 /* Scan all configured BGP route then check the route exists in IGP or
1064 bgp_import (struct thread
*t
)
1067 struct bgp_node
*rn
;
1068 struct bgp_static
*bgp_static
;
1069 struct listnode
*node
, *nnode
;
1072 struct in_addr nexthop
;
1077 thread_add_timer (master
, bgp_import
, NULL
, bgp_import_interval
);
1079 if (BGP_DEBUG (events
, EVENTS
))
1080 zlog_debug ("Import timer expired.");
1082 for (ALL_LIST_ELEMENTS (bm
->bgp
, node
, nnode
, bgp
))
1084 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1085 for (safi
= SAFI_UNICAST
; safi
< SAFI_MPLS_VPN
; safi
++)
1086 for (rn
= bgp_table_top (bgp
->route
[afi
][safi
]); rn
;
1087 rn
= bgp_route_next (rn
))
1088 if ((bgp_static
= rn
->info
) != NULL
)
1090 if (bgp_static
->backdoor
)
1093 valid
= bgp_static
->valid
;
1094 metric
= bgp_static
->igpmetric
;
1095 nexthop
= bgp_static
->igpnexthop
;
1097 if (bgp_flag_check (bgp
, BGP_FLAG_IMPORT_CHECK
)
1098 && afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
1099 bgp_static
->valid
= bgp_import_check (&rn
->p
, &bgp_static
->igpmetric
,
1100 &bgp_static
->igpnexthop
);
1103 bgp_static
->valid
= 1;
1104 bgp_static
->igpmetric
= 0;
1105 bgp_static
->igpnexthop
.s_addr
= 0;
1108 if (bgp_static
->valid
!= valid
)
1110 if (bgp_static
->valid
)
1111 bgp_static_update (bgp
, &rn
->p
, bgp_static
, afi
, safi
);
1113 bgp_static_withdraw (bgp
, &rn
->p
, afi
, safi
);
1115 else if (bgp_static
->valid
)
1117 if (bgp_static
->igpmetric
!= metric
1118 || bgp_static
->igpnexthop
.s_addr
!= nexthop
.s_addr
1119 || bgp_static
->rmap
.name
)
1120 bgp_static_update (bgp
, &rn
->p
, bgp_static
, afi
, safi
);
1127 /* Connect to zebra for nexthop lookup. */
1129 zlookup_connect (struct thread
*t
)
1131 struct zclient
*zlookup
;
1133 zlookup
= THREAD_ARG (t
);
1134 zlookup
->t_connect
= NULL
;
1136 if (zlookup
->sock
!= -1)
1139 #ifdef HAVE_TCP_ZEBRA
1140 zlookup
->sock
= zclient_socket ();
1142 zlookup
->sock
= zclient_socket_un (ZEBRA_SERV_PATH
);
1143 #endif /* HAVE_TCP_ZEBRA */
1144 if (zlookup
->sock
< 0)
1150 /* Check specified multiaccess next-hop. */
1152 bgp_multiaccess_check_v4 (struct in_addr nexthop
, char *peer
)
1154 struct bgp_node
*rn1
;
1155 struct bgp_node
*rn2
;
1158 struct in_addr addr
;
1161 ret
= inet_aton (peer
, &addr
);
1165 memset (&p1
, 0, sizeof (struct prefix
));
1166 p1
.family
= AF_INET
;
1167 p1
.prefixlen
= IPV4_MAX_BITLEN
;
1168 p1
.u
.prefix4
= nexthop
;
1169 memset (&p2
, 0, sizeof (struct prefix
));
1170 p2
.family
= AF_INET
;
1171 p2
.prefixlen
= IPV4_MAX_BITLEN
;
1172 p2
.u
.prefix4
= addr
;
1174 /* If bgp scan is not enabled, return invalid. */
1175 if (zlookup
->sock
< 0)
1178 rn1
= bgp_node_match (bgp_connected_table
[AFI_IP
], &p1
);
1182 rn2
= bgp_node_match (bgp_connected_table
[AFI_IP
], &p2
);
1192 DEFUN (bgp_scan_time
,
1194 "bgp scan-time <5-60>",
1195 "BGP specific commands\n"
1196 "Configure background scanner interval\n"
1197 "Scanner interval (seconds)\n")
1199 bgp_scan_interval
= atoi (argv
[0]);
1201 if (bgp_scan_thread
)
1203 thread_cancel (bgp_scan_thread
);
1205 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
1211 DEFUN (no_bgp_scan_time
,
1212 no_bgp_scan_time_cmd
,
1215 "BGP specific commands\n"
1216 "Configure background scanner interval\n")
1218 bgp_scan_interval
= BGP_SCAN_INTERVAL_DEFAULT
;
1220 if (bgp_scan_thread
)
1222 thread_cancel (bgp_scan_thread
);
1224 thread_add_timer (master
, bgp_scan_timer
, NULL
, bgp_scan_interval
);
1230 ALIAS (no_bgp_scan_time
,
1231 no_bgp_scan_time_val_cmd
,
1232 "no bgp scan-time <5-60>",
1234 "BGP specific commands\n"
1235 "Configure background scanner interval\n"
1236 "Scanner interval (seconds)\n")
1238 DEFUN (show_ip_bgp_scan
,
1239 show_ip_bgp_scan_cmd
,
1244 "BGP scan status\n")
1246 struct bgp_node
*rn
;
1247 struct bgp_nexthop_cache
*bnc
;
1249 if (bgp_scan_thread
)
1250 vty_out (vty
, "BGP scan is running%s", VTY_NEWLINE
);
1252 vty_out (vty
, "BGP scan is not running%s", VTY_NEWLINE
);
1253 vty_out (vty
, "BGP scan interval is %d%s", bgp_scan_interval
, VTY_NEWLINE
);
1255 vty_out (vty
, "Current BGP nexthop cache:%s", VTY_NEWLINE
);
1256 for (rn
= bgp_table_top (bgp_nexthop_cache_table
[AFI_IP
]); rn
; rn
= bgp_route_next (rn
))
1257 if ((bnc
= rn
->info
) != NULL
)
1260 vty_out (vty
, " %s valid [IGP metric %d]%s",
1261 inet_ntoa (rn
->p
.u
.prefix4
), bnc
->metric
, VTY_NEWLINE
);
1263 vty_out (vty
, " %s invalid%s",
1264 inet_ntoa (rn
->p
.u
.prefix4
), VTY_NEWLINE
);
1270 for (rn
= bgp_table_top (bgp_nexthop_cache_table
[AFI_IP6
]);
1272 rn
= bgp_route_next (rn
))
1273 if ((bnc
= rn
->info
) != NULL
)
1276 vty_out (vty
, " %s valid [IGP metric %d]%s",
1277 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1278 bnc
->metric
, VTY_NEWLINE
);
1280 vty_out (vty
, " %s invalid%s",
1281 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1285 #endif /* HAVE_IPV6 */
1287 vty_out (vty
, "BGP connected route:%s", VTY_NEWLINE
);
1288 for (rn
= bgp_table_top (bgp_connected_table
[AFI_IP
]);
1290 rn
= bgp_route_next (rn
))
1291 if (rn
->info
!= NULL
)
1292 vty_out (vty
, " %s/%d%s", inet_ntoa (rn
->p
.u
.prefix4
), rn
->p
.prefixlen
,
1299 for (rn
= bgp_table_top (bgp_connected_table
[AFI_IP6
]);
1301 rn
= bgp_route_next (rn
))
1302 if (rn
->info
!= NULL
)
1303 vty_out (vty
, " %s/%d%s",
1304 inet_ntop (AF_INET6
, &rn
->p
.u
.prefix6
, buf
, BUFSIZ
),
1308 #endif /* HAVE_IPV6 */
1314 bgp_config_write_scan_time (struct vty
*vty
)
1316 if (bgp_scan_interval
!= BGP_SCAN_INTERVAL_DEFAULT
)
1317 vty_out (vty
, " bgp scan-time %d%s", bgp_scan_interval
, VTY_NEWLINE
);
1324 zlookup
= zclient_new ();
1326 zlookup
->ibuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1327 zlookup
->obuf
= stream_new (ZEBRA_MAX_PACKET_SIZ
);
1328 zlookup
->t_connect
= thread_add_event (master
, zlookup_connect
, zlookup
, 0);
1330 bgp_scan_interval
= BGP_SCAN_INTERVAL_DEFAULT
;
1331 bgp_import_interval
= BGP_IMPORT_INTERVAL_DEFAULT
;
1333 cache1_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1334 cache2_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1335 bgp_nexthop_cache_table
[AFI_IP
] = cache1_table
[AFI_IP
];
1337 bgp_connected_table
[AFI_IP
] = bgp_table_init (AFI_IP
, SAFI_UNICAST
);
1340 cache1_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1341 cache2_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1342 bgp_nexthop_cache_table
[AFI_IP6
] = cache1_table
[AFI_IP6
];
1343 bgp_connected_table
[AFI_IP6
] = bgp_table_init (AFI_IP6
, SAFI_UNICAST
);
1344 #endif /* HAVE_IPV6 */
1346 /* Make BGP scan thread. */
1347 bgp_scan_thread
= thread_add_timer (master
, bgp_scan_timer
,
1348 NULL
, bgp_scan_interval
);
1349 /* Make BGP import there. */
1350 bgp_import_thread
= thread_add_timer (master
, bgp_import
, NULL
, 0);
1352 install_element (BGP_NODE
, &bgp_scan_time_cmd
);
1353 install_element (BGP_NODE
, &no_bgp_scan_time_cmd
);
1354 install_element (BGP_NODE
, &no_bgp_scan_time_val_cmd
);
1355 install_element (VIEW_NODE
, &show_ip_bgp_scan_cmd
);
1356 install_element (ENABLE_NODE
, &show_ip_bgp_scan_cmd
);