2000-03-20 21:50:17 +00:00
/*
* BIRD - - BGP Packet Processing
*
* ( c ) 2000 Martin Mares < mj @ ucw . cz >
*
* Can be freely distributed and used under the terms of the GNU GPL .
*/
2000-05-02 16:07:41 +00:00
# undef LOCAL_DEBUG
2000-03-21 15:53:50 +00:00
2000-03-20 21:50:17 +00:00
# include "nest/bird.h"
# include "nest/iface.h"
# include "nest/protocol.h"
# include "nest/route.h"
2008-10-26 21:36:08 +00:00
# include "nest/attrs.h"
2018-09-18 15:29:00 +00:00
# include "proto/mrt/mrt.h"
2000-03-20 21:50:17 +00:00
# include "conf/conf.h"
2000-03-21 15:53:50 +00:00
# include "lib/unaligned.h"
# include "lib/socket.h"
2000-03-20 21:50:17 +00:00
2008-12-19 00:34:39 +00:00
# include "nest/cli.h"
2000-03-20 21:50:17 +00:00
# include "bgp.h"
2000-03-21 15:53:50 +00:00
2015-03-29 16:27:13 +00:00
# define BGP_RR_REQUEST 0
# define BGP_RR_BEGIN 1
# define BGP_RR_END 2
2014-10-02 09:41:34 +00:00
static struct tbf rl_rcv_update = TBF_DEFAULT_LOG_LIMITS ;
static struct tbf rl_snd_update = TBF_DEFAULT_LOG_LIMITS ;
2009-02-26 13:23:54 +00:00
2013-07-25 11:55:24 +00:00
/* Table for state -> RFC 6608 FSM error subcodes */
static byte fsm_err_subcode [ BS_MAX ] = {
[ BS_OPENSENT ] = 1 ,
[ BS_OPENCONFIRM ] = 2 ,
[ BS_ESTABLISHED ] = 3
} ;
2018-09-18 15:29:00 +00:00
static void
init_mrt_bgp_data ( struct bgp_conn * conn , struct mrt_bgp_data * d )
2010-01-03 11:17:52 +00:00
{
struct bgp_proto * p = conn - > bgp ;
2018-09-18 15:29:00 +00:00
int p_ok = conn - > state > = BS_OPENCONFIRM ;
memset ( d , 0 , sizeof ( struct mrt_bgp_data ) ) ;
d - > peer_as = p - > remote_as ;
d - > local_as = p - > local_as ;
d - > index = ( p - > neigh & & p - > neigh - > iface ) ? p - > neigh - > iface - > index : 0 ;
d - > af = BGP_AF ;
d - > peer_ip = conn - > sk ? conn - > sk - > daddr : IPA_NONE ;
d - > local_ip = conn - > sk ? conn - > sk - > saddr : IPA_NONE ;
d - > as4 = p_ok ? p - > as4_session : 0 ;
d - > add_path = p_ok ? p - > add_path_rx : 0 ;
2010-01-03 11:17:52 +00:00
}
static void
2018-09-18 15:29:00 +00:00
bgp_dump_message ( struct bgp_conn * conn , byte * pkt , uint len )
2010-01-03 11:17:52 +00:00
{
2018-09-18 15:29:00 +00:00
struct mrt_bgp_data d ;
init_mrt_bgp_data ( conn , & d ) ;
2010-01-03 11:17:52 +00:00
2018-09-18 15:29:00 +00:00
d . message = pkt ;
d . msg_len = len ;
mrt_dump_bgp_message ( & d ) ;
2010-01-03 11:17:52 +00:00
}
void
2018-09-18 15:29:00 +00:00
bgp_dump_state_change ( struct bgp_conn * conn , uint old , uint new )
2010-01-03 11:17:52 +00:00
{
2018-09-18 15:29:00 +00:00
struct mrt_bgp_data d ;
init_mrt_bgp_data ( conn , & d ) ;
d . old_state = old ;
d . new_state = new ;
mrt_dump_bgp_state_change ( & d ) ;
2010-01-03 11:17:52 +00:00
}
2000-03-21 15:53:50 +00:00
static byte *
bgp_create_notification ( struct bgp_conn * conn , byte * buf )
{
2000-05-02 16:07:41 +00:00
struct bgp_proto * p = conn - > bgp ;
BGP_TRACE ( D_PACKETS , " Sending NOTIFICATION(code=%d.%d) " , conn - > notify_code , conn - > notify_subcode ) ;
2000-03-21 15:53:50 +00:00
buf [ 0 ] = conn - > notify_code ;
buf [ 1 ] = conn - > notify_subcode ;
2000-04-25 21:13:25 +00:00
memcpy ( buf + 2 , conn - > notify_data , conn - > notify_size ) ;
return buf + 2 + conn - > notify_size ;
2000-03-21 15:53:50 +00:00
}
2008-10-26 21:36:08 +00:00
# ifdef IPV6
static byte *
2014-03-20 13:07:12 +00:00
bgp_put_cap_ipv6 ( struct bgp_proto * p UNUSED , byte * buf )
2008-10-26 21:36:08 +00:00
{
* buf + + = 1 ; /* Capability 1: Multiprotocol extensions */
* buf + + = 4 ; /* Capability data length */
* buf + + = 0 ; /* We support AF IPv6 */
* buf + + = BGP_AF_IPV6 ;
* buf + + = 0 ; /* RFU */
* buf + + = 1 ; /* and SAFI 1 */
return buf ;
}
2009-03-05 10:52:47 +00:00
# else
static byte *
2014-03-20 13:07:12 +00:00
bgp_put_cap_ipv4 ( struct bgp_proto * p UNUSED , byte * buf )
2009-03-05 10:52:47 +00:00
{
* buf + + = 1 ; /* Capability 1: Multiprotocol extensions */
* buf + + = 4 ; /* Capability data length */
* buf + + = 0 ; /* We support AF IPv4 */
* buf + + = BGP_AF_IPV4 ;
* buf + + = 0 ; /* RFU */
* buf + + = 1 ; /* and SAFI 1 */
return buf ;
}
2008-10-26 21:36:08 +00:00
# endif
2009-11-26 19:47:59 +00:00
static byte *
2014-03-20 13:07:12 +00:00
bgp_put_cap_rr ( struct bgp_proto * p UNUSED , byte * buf )
2009-11-26 19:47:59 +00:00
{
* buf + + = 2 ; /* Capability 2: Support for route refresh */
* buf + + = 0 ; /* Capability data length */
return buf ;
}
2015-11-25 14:52:58 +00:00
static byte *
bgp_put_cap_ext_msg ( struct bgp_proto * p UNUSED , byte * buf )
{
* buf + + = 6 ; /* Capability 6: Support for extended messages */
* buf + + = 0 ; /* Capability data length */
return buf ;
}
2008-10-26 21:36:08 +00:00
static byte *
2014-03-20 13:07:12 +00:00
bgp_put_cap_gr1 ( struct bgp_proto * p , byte * buf )
{
* buf + + = 64 ; /* Capability 64: Support for graceful restart */
* buf + + = 6 ; /* Capability data length */
put_u16 ( buf , p - > cf - > gr_time ) ;
if ( p - > p . gr_recovery )
buf [ 0 ] | = BGP_GRF_RESTART ;
buf + = 2 ;
* buf + + = 0 ; /* Appropriate AF */
* buf + + = BGP_AF ;
* buf + + = 1 ; /* and SAFI 1 */
* buf + + = p - > p . gr_recovery ? BGP_GRF_FORWARDING : 0 ;
return buf ;
}
static byte *
2016-10-14 13:37:04 +00:00
bgp_put_cap_gr2 ( struct bgp_proto * p UNUSED , byte * buf )
2014-03-20 13:07:12 +00:00
{
* buf + + = 64 ; /* Capability 64: Support for graceful restart */
* buf + + = 2 ; /* Capability data length */
put_u16 ( buf , 0 ) ;
return buf + 2 ;
}
static byte *
bgp_put_cap_as4 ( struct bgp_proto * p , byte * buf )
2008-10-26 21:36:08 +00:00
{
* buf + + = 65 ; /* Capability 65: Support for 4-octet AS number */
* buf + + = 4 ; /* Capability data length */
2014-03-20 13:07:12 +00:00
put_u32 ( buf , p - > local_as ) ;
2008-10-26 21:36:08 +00:00
return buf + 4 ;
}
2012-08-14 14:25:22 +00:00
static byte *
2014-03-20 13:07:12 +00:00
bgp_put_cap_add_path ( struct bgp_proto * p , byte * buf )
2012-08-14 14:25:22 +00:00
{
* buf + + = 69 ; /* Capability 69: Support for ADD-PATH */
* buf + + = 4 ; /* Capability data length */
* buf + + = 0 ; /* Appropriate AF */
* buf + + = BGP_AF ;
* buf + + = 1 ; /* SAFI 1 */
2014-03-20 13:07:12 +00:00
* buf + + = p - > cf - > add_path ;
2012-08-14 14:25:22 +00:00
return buf ;
}
2015-03-29 16:27:13 +00:00
static byte *
bgp_put_cap_err ( struct bgp_proto * p UNUSED , byte * buf )
{
* buf + + = 70 ; /* Capability 70: Support for enhanced route refresh */
* buf + + = 0 ; /* Capability data length */
return buf ;
}
2018-06-04 16:56:05 +00:00
static byte *
bgp_put_cap_llgr1 ( struct bgp_proto * p , byte * buf )
{
* buf + + = 71 ; /* Capability 71: Support for long-lived graceful restart */
* buf + + = 7 ; /* Capability data length */
* buf + + = 0 ; /* Appropriate AF */
* buf + + = BGP_AF ;
* buf + + = 1 ; /* and SAFI 1 */
/* Next is 8bit flags and 24bit time */
put_u32 ( buf , p - > cf - > llgr_time ) ;
buf [ 0 ] = p - > p . gr_recovery ? BGP_LLGRF_FORWARDING : 0 ;
buf + = 4 ;
return buf ;
}
static byte *
bgp_put_cap_llgr2 ( struct bgp_proto * p UNUSED , byte * buf )
{
* buf + + = 71 ; /* Capability 71: Support for long-lived graceful restart */
* buf + + = 0 ; /* Capability data length */
return buf ;
}
2015-03-29 16:27:13 +00:00
2000-03-21 15:53:50 +00:00
static byte *
bgp_create_open ( struct bgp_conn * conn , byte * buf )
{
2000-05-02 16:07:41 +00:00
struct bgp_proto * p = conn - > bgp ;
2008-10-26 21:36:08 +00:00
byte * cap ;
int cap_len ;
2000-05-02 16:07:41 +00:00
BGP_TRACE ( D_PACKETS , " Sending OPEN(ver=%d,as=%d,hold=%d,id=%08x) " ,
BGP_VERSION , p - > local_as , p - > cf - > hold_time , p - > local_id ) ;
2000-03-21 15:53:50 +00:00
buf [ 0 ] = BGP_VERSION ;
2008-10-26 21:36:08 +00:00
put_u16 ( buf + 1 , ( p - > local_as < 0xFFFF ) ? p - > local_as : AS_TRANS ) ;
2000-05-02 16:07:41 +00:00
put_u16 ( buf + 3 , p - > cf - > hold_time ) ;
put_u32 ( buf + 5 , p - > local_id ) ;
2008-12-24 16:24:41 +00:00
if ( conn - > start_state = = BSS_CONNECT_NOCAP )
{
BGP_TRACE ( D_PACKETS , " Skipping capabilities " ) ;
buf [ 9 ] = 0 ;
return buf + 10 ;
}
2008-10-26 21:36:08 +00:00
/* Skipped 3 B for length field and Capabilities parameter header */
cap = buf + 12 ;
2009-03-05 10:52:47 +00:00
# ifndef IPV6
if ( p - > cf - > advertise_ipv4 )
2014-03-20 13:07:12 +00:00
cap = bgp_put_cap_ipv4 ( p , cap ) ;
2009-03-05 10:52:47 +00:00
# endif
2008-10-26 21:36:08 +00:00
# ifdef IPV6
2014-03-20 13:07:12 +00:00
cap = bgp_put_cap_ipv6 ( p , cap ) ;
2003-02-22 10:40:35 +00:00
# endif
2009-03-13 11:49:44 +00:00
2009-11-26 19:47:59 +00:00
if ( p - > cf - > enable_refresh )
2014-03-20 13:07:12 +00:00
cap = bgp_put_cap_rr ( p , cap ) ;
if ( p - > cf - > gr_mode = = BGP_GR_ABLE )
cap = bgp_put_cap_gr1 ( p , cap ) ;
else if ( p - > cf - > gr_mode = = BGP_GR_AWARE )
cap = bgp_put_cap_gr2 ( p , cap ) ;
2009-11-26 19:47:59 +00:00
2012-08-14 14:25:22 +00:00
if ( p - > cf - > enable_as4 )
2014-03-20 13:07:12 +00:00
cap = bgp_put_cap_as4 ( p , cap ) ;
2008-10-26 21:36:08 +00:00
2012-08-14 14:25:22 +00:00
if ( p - > cf - > add_path )
2014-03-20 13:07:12 +00:00
cap = bgp_put_cap_add_path ( p , cap ) ;
2012-08-14 14:25:22 +00:00
2015-03-29 16:27:13 +00:00
if ( p - > cf - > enable_refresh )
cap = bgp_put_cap_err ( p , cap ) ;
2015-07-18 11:38:21 +00:00
if ( p - > cf - > enable_extended_messages )
cap = bgp_put_cap_ext_msg ( p , cap ) ;
2018-06-04 16:56:05 +00:00
if ( p - > cf - > llgr_mode = = BGP_LLGR_ABLE )
cap = bgp_put_cap_llgr1 ( p , cap ) ;
else if ( p - > cf - > llgr_mode = = BGP_LLGR_AWARE )
cap = bgp_put_cap_llgr2 ( p , cap ) ;
2008-10-26 21:36:08 +00:00
cap_len = cap - buf - 12 ;
if ( cap_len > 0 )
{
buf [ 9 ] = cap_len + 2 ; /* Optional params len */
buf [ 10 ] = 2 ; /* Option: Capability list */
buf [ 11 ] = cap_len ; /* Option length */
return cap ;
}
else
{
buf [ 9 ] = 0 ; /* No optional parameters */
return buf + 10 ;
}
2000-03-21 15:53:50 +00:00
}
2015-05-19 06:53:34 +00:00
static uint
bgp_encode_prefixes ( struct bgp_proto * p , byte * w , struct bgp_bucket * buck , uint remains )
2000-04-17 09:37:31 +00:00
{
byte * start = w ;
ip_addr a ;
int bytes ;
2013-11-26 21:37:24 +00:00
while ( ! EMPTY_LIST ( buck - > prefixes ) & & ( remains > = ( 5 + sizeof ( ip_addr ) ) ) )
2000-04-17 09:37:31 +00:00
{
struct bgp_prefix * px = SKIP_BACK ( struct bgp_prefix , bucket_node , HEAD ( buck - > prefixes ) ) ;
DBG ( " \t Dequeued route %I/%d \n " , px - > n . prefix , px - > n . pxlen ) ;
2012-08-14 14:25:22 +00:00
if ( p - > add_path_tx )
{
put_u32 ( w , px - > path_id ) ;
w + = 4 ;
2013-11-26 21:37:24 +00:00
remains - = 4 ;
2012-08-14 14:25:22 +00:00
}
2000-04-17 09:37:31 +00:00
* w + + = px - > n . pxlen ;
bytes = ( px - > n . pxlen + 7 ) / 8 ;
a = px - > n . prefix ;
ipa_hton ( a ) ;
memcpy ( w , & a , bytes ) ;
w + = bytes ;
2000-05-08 14:53:22 +00:00
remains - = bytes + 1 ;
2000-04-17 09:37:31 +00:00
rem_node ( & px - > bucket_node ) ;
2012-08-14 14:25:22 +00:00
bgp_free_prefix ( p , px ) ;
// fib_delete(&p->prefix_fib, px);
2000-04-17 09:37:31 +00:00
}
return w - start ;
}
2009-02-27 14:24:46 +00:00
static void
bgp_flush_prefixes ( struct bgp_proto * p , struct bgp_bucket * buck )
{
while ( ! EMPTY_LIST ( buck - > prefixes ) )
{
struct bgp_prefix * px = SKIP_BACK ( struct bgp_prefix , bucket_node , HEAD ( buck - > prefixes ) ) ;
log ( L_ERR " %s: - route %I/%d skipped " , p - > p . name , px - > n . prefix , px - > n . pxlen ) ;
rem_node ( & px - > bucket_node ) ;
2012-08-14 14:25:22 +00:00
bgp_free_prefix ( p , px ) ;
// fib_delete(&p->prefix_fib, px);
2009-02-27 14:24:46 +00:00
}
}
2000-05-04 09:03:31 +00:00
# ifndef IPV6 /* IPv4 version */
2000-03-21 15:53:50 +00:00
static byte *
bgp_create_update ( struct bgp_conn * conn , byte * buf )
{
2000-05-02 16:07:41 +00:00
struct bgp_proto * p = conn - > bgp ;
2000-04-17 09:37:31 +00:00
struct bgp_bucket * buck ;
2015-07-18 11:38:21 +00:00
int remains = bgp_max_packet_length ( p ) - BGP_HEADER_LENGTH - 4 ;
2000-05-04 20:02:56 +00:00
byte * w ;
2000-04-17 09:37:31 +00:00
int wd_size = 0 ;
int r_size = 0 ;
2000-05-04 20:02:56 +00:00
int a_size = 0 ;
2000-04-17 09:37:31 +00:00
w = buf + 2 ;
2000-05-02 16:07:41 +00:00
if ( ( buck = p - > withdraw_bucket ) & & ! EMPTY_LIST ( buck - > prefixes ) )
2000-04-17 09:37:31 +00:00
{
DBG ( " Withdrawn routes: \n " ) ;
2000-05-02 16:07:41 +00:00
wd_size = bgp_encode_prefixes ( p , w , buck , remains ) ;
2000-04-17 09:37:31 +00:00
w + = wd_size ;
remains - = wd_size ;
}
put_u16 ( buf , wd_size ) ;
2000-03-30 10:44:20 +00:00
2016-07-11 18:41:32 +00:00
if ( ! wd_size )
2000-04-17 09:37:31 +00:00
{
2000-05-02 16:07:41 +00:00
while ( ( buck = ( struct bgp_bucket * ) HEAD ( p - > bucket_queue ) ) - > send_node . next )
2000-04-17 09:37:31 +00:00
{
if ( EMPTY_LIST ( buck - > prefixes ) )
{
DBG ( " Deleting empty bucket %p \n " , buck ) ;
rem_node ( & buck - > send_node ) ;
2000-05-02 16:07:41 +00:00
bgp_free_bucket ( p , buck ) ;
2000-04-17 09:37:31 +00:00
continue ;
}
2009-02-27 14:24:46 +00:00
2000-04-17 09:37:31 +00:00
DBG ( " Processing bucket %p \n " , buck ) ;
2016-07-11 18:41:32 +00:00
a_size = bgp_encode_attrs ( p , w + 2 , buck - > eattrs , remains - 1024 ) ;
2009-02-27 14:24:46 +00:00
if ( a_size < 0 )
{
2009-11-18 19:32:36 +00:00
log ( L_ERR " %s: Attribute list too long, skipping corresponding routes " , p - > p . name ) ;
2009-02-27 14:24:46 +00:00
bgp_flush_prefixes ( p , buck ) ;
rem_node ( & buck - > send_node ) ;
bgp_free_bucket ( p , buck ) ;
continue ;
}
2000-05-04 20:02:56 +00:00
put_u16 ( w , a_size ) ;
w + = a_size + 2 ;
r_size = bgp_encode_prefixes ( p , w , buck , remains - a_size ) ;
2000-04-17 09:37:31 +00:00
w + = r_size ;
break ;
}
}
2000-05-08 12:37:24 +00:00
if ( ! a_size ) /* Attributes not already encoded */
2000-04-17 09:37:31 +00:00
{
put_u16 ( w , 0 ) ;
w + = 2 ;
}
2000-05-19 18:05:19 +00:00
if ( wd_size | | r_size )
{
2009-02-26 13:23:54 +00:00
BGP_TRACE_RL ( & rl_snd_update , D_PACKETS , " Sending UPDATE " ) ;
2000-05-19 18:05:19 +00:00
return w ;
}
else
return NULL ;
2000-03-21 15:53:50 +00:00
}
2014-03-20 13:07:12 +00:00
static byte *
bgp_create_end_mark ( struct bgp_conn * conn , byte * buf )
{
struct bgp_proto * p = conn - > bgp ;
2015-03-29 16:27:13 +00:00
BGP_TRACE ( D_PACKETS , " Sending END-OF-RIB " ) ;
2014-03-20 13:07:12 +00:00
put_u32 ( buf , 0 ) ;
return buf + 4 ;
}
2000-05-04 09:03:31 +00:00
# else /* IPv6 version */
2012-01-08 14:28:27 +00:00
static inline int
same_iface ( struct bgp_proto * p , ip_addr * ip )
{
neighbor * n = neigh_find ( & p - > p , ip , 0 ) ;
return n & & p - > neigh & & n - > iface = = p - > neigh - > iface ;
}
2000-05-04 09:03:31 +00:00
static byte *
bgp_create_update ( struct bgp_conn * conn , byte * buf )
{
2000-05-04 20:02:56 +00:00
struct bgp_proto * p = conn - > bgp ;
struct bgp_bucket * buck ;
2009-11-18 19:32:36 +00:00
int size , second , rem_stored ;
2015-07-18 11:38:21 +00:00
int remains = bgp_max_packet_length ( p ) - BGP_HEADER_LENGTH - 4 ;
2009-11-18 19:32:36 +00:00
byte * w , * w_stored , * tmp , * tstart ;
2009-04-29 16:58:24 +00:00
ip_addr * ipp , ip , ip_ll ;
2000-05-04 20:02:56 +00:00
ea_list * ea ;
eattr * nh ;
put_u16 ( buf , 0 ) ;
w = buf + 4 ;
if ( ( buck = p - > withdraw_bucket ) & & ! EMPTY_LIST ( buck - > prefixes ) )
{
DBG ( " Withdrawn routes: \n " ) ;
2008-10-26 21:45:09 +00:00
tmp = bgp_attach_attr_wa ( & ea , bgp_linpool , BA_MP_UNREACH_NLRI , remains - 8 ) ;
2000-05-04 20:02:56 +00:00
* tmp + + = 0 ;
* tmp + + = BGP_AF_IPV6 ;
* tmp + + = 1 ;
2009-04-23 12:16:05 +00:00
ea - > attrs [ 0 ] . u . ptr - > length = 3 + bgp_encode_prefixes ( p , tmp , buck , remains - 11 ) ;
2008-10-26 21:36:08 +00:00
size = bgp_encode_attrs ( p , w , ea , remains ) ;
2009-02-27 14:24:46 +00:00
ASSERT ( size > = 0 ) ;
2000-05-04 20:02:56 +00:00
w + = size ;
remains - = size ;
}
2016-07-11 18:41:32 +00:00
else
2000-05-04 20:02:56 +00:00
{
while ( ( buck = ( struct bgp_bucket * ) HEAD ( p - > bucket_queue ) ) - > send_node . next )
{
if ( EMPTY_LIST ( buck - > prefixes ) )
{
DBG ( " Deleting empty bucket %p \n " , buck ) ;
rem_node ( & buck - > send_node ) ;
bgp_free_bucket ( p , buck ) ;
continue ;
}
2009-02-27 14:24:46 +00:00
2000-05-04 20:02:56 +00:00
DBG ( " Processing bucket %p \n " , buck ) ;
2009-11-18 19:32:36 +00:00
rem_stored = remains ;
w_stored = w ;
2009-02-27 14:24:46 +00:00
2016-07-11 18:41:32 +00:00
size = bgp_encode_attrs ( p , w , buck - > eattrs , remains - 1024 ) ;
2009-02-27 14:24:46 +00:00
if ( size < 0 )
{
2009-11-18 19:32:36 +00:00
log ( L_ERR " %s: Attribute list too long, skipping corresponding routes " , p - > p . name ) ;
2009-02-27 14:24:46 +00:00
bgp_flush_prefixes ( p , buck ) ;
rem_node ( & buck - > send_node ) ;
bgp_free_bucket ( p , buck ) ;
continue ;
}
2000-05-04 20:02:56 +00:00
w + = size ;
remains - = size ;
2009-04-29 16:58:24 +00:00
2009-11-18 19:32:36 +00:00
/* We have two addresses here in NEXT_HOP eattr. Really.
2009-09-18 11:59:04 +00:00
Unless NEXT_HOP was modified by filter */
2009-11-18 19:32:36 +00:00
nh = ea_find ( buck - > eattrs , EA_CODE ( EAP_BGP , BA_NEXT_HOP ) ) ;
ASSERT ( nh ) ;
2009-09-18 11:59:04 +00:00
second = ( nh - > u . ptr - > length = = NEXT_HOP_LENGTH ) ;
2009-04-29 16:58:24 +00:00
ipp = ( ip_addr * ) nh - > u . ptr - > data ;
ip = ipp [ 0 ] ;
ip_ll = IPA_NONE ;
2009-04-28 16:11:56 +00:00
if ( ipa_equal ( ip , p - > source_addr ) )
2009-04-29 16:58:24 +00:00
ip_ll = p - > local_link ;
2000-05-04 20:02:56 +00:00
else
{
2009-04-29 16:58:24 +00:00
/* If we send a route with 'third party' next hop destinated
* in the same interface , we should also send a link local
* next hop address . We use the received one ( stored in the
* other part of BA_NEXT_HOP eattr ) . If we didn ' t received
2009-05-06 13:18:52 +00:00
* it ( for example it is a static route ) , we can ' t use
* ' third party ' next hop and we have to use local IP address
* as next hop . Sending original next hop address without
* link local address seems to be a natural way to solve that
* problem , but it is contrary to RFC 2545 and Quagga does not
* accept such routes .
2012-01-08 14:28:27 +00:00
*
* There are two cases , either we have global IP , or
* IPA_NONE if the neighbor is link - local . For IPA_NONE ,
* we suppose it is on the same iface , see bgp_update_attrs ( ) .
2009-04-29 16:58:24 +00:00
*/
2012-01-08 14:28:27 +00:00
if ( ipa_zero ( ip ) | | same_iface ( p , & ip ) )
2009-05-06 13:18:52 +00:00
{
2009-09-18 11:59:04 +00:00
if ( second & & ipa_nonzero ( ipp [ 1 ] ) )
2009-05-06 13:18:52 +00:00
ip_ll = ipp [ 1 ] ;
else
{
2009-11-18 19:32:36 +00:00
switch ( p - > cf - > missing_lladdr )
{
case MLL_SELF :
ip = p - > source_addr ;
ip_ll = p - > local_link ;
break ;
case MLL_DROP :
log ( L_ERR " %s: Missing link-local next hop address, skipping corresponding routes " , p - > p . name ) ;
w = w_stored ;
remains = rem_stored ;
bgp_flush_prefixes ( p , buck ) ;
rem_node ( & buck - > send_node ) ;
bgp_free_bucket ( p , buck ) ;
continue ;
case MLL_IGNORE :
break ;
}
2009-05-06 13:18:52 +00:00
}
}
2000-05-04 20:02:56 +00:00
}
2009-04-29 16:58:24 +00:00
2009-11-18 19:32:36 +00:00
tstart = tmp = bgp_attach_attr_wa ( & ea , bgp_linpool , BA_MP_REACH_NLRI , remains - 8 ) ;
* tmp + + = 0 ;
* tmp + + = BGP_AF_IPV6 ;
* tmp + + = 1 ;
2014-10-24 09:11:43 +00:00
if ( ipa_is_link_local ( ip ) )
2012-01-08 14:28:27 +00:00
ip = IPA_NONE ;
2009-04-29 16:58:24 +00:00
if ( ipa_nonzero ( ip_ll ) )
2000-05-04 20:02:56 +00:00
{
* tmp + + = 32 ;
ipa_hton ( ip ) ;
memcpy ( tmp , & ip , 16 ) ;
ipa_hton ( ip_ll ) ;
memcpy ( tmp + 16 , & ip_ll , 16 ) ;
tmp + = 32 ;
}
else
{
* tmp + + = 16 ;
ipa_hton ( ip ) ;
memcpy ( tmp , & ip , 16 ) ;
tmp + = 16 ;
}
2009-04-29 16:58:24 +00:00
2000-05-04 20:02:56 +00:00
* tmp + + = 0 ; /* No SNPA information */
tmp + = bgp_encode_prefixes ( p , tmp , buck , remains - ( 8 + 3 + 32 + 1 ) ) ;
ea - > attrs [ 0 ] . u . ptr - > length = tmp - tstart ;
2009-02-27 14:24:46 +00:00
size = bgp_encode_attrs ( p , w , ea , remains ) ;
ASSERT ( size > = 0 ) ;
w + = size ;
2000-05-04 20:02:56 +00:00
break ;
}
}
size = w - ( buf + 4 ) ;
put_u16 ( buf + 2 , size ) ;
lp_flush ( bgp_linpool ) ;
2000-05-19 18:05:19 +00:00
if ( size )
{
2009-02-26 13:23:54 +00:00
BGP_TRACE_RL ( & rl_snd_update , D_PACKETS , " Sending UPDATE " ) ;
2000-05-19 18:05:19 +00:00
return w ;
}
else
return NULL ;
2000-05-04 09:03:31 +00:00
}
2014-03-20 13:07:12 +00:00
static byte *
bgp_create_end_mark ( struct bgp_conn * conn , byte * buf )
{
struct bgp_proto * p = conn - > bgp ;
2015-03-29 16:27:13 +00:00
BGP_TRACE ( D_PACKETS , " Sending END-OF-RIB " ) ;
2014-03-20 13:07:12 +00:00
put_u16 ( buf + 0 , 0 ) ;
put_u16 ( buf + 2 , 6 ) ; /* length 4-9 */
buf + = 4 ;
/* Empty MP_UNREACH_NLRI atribute */
* buf + + = BAF_OPTIONAL ;
* buf + + = BA_MP_UNREACH_NLRI ;
* buf + + = 3 ; /* Length 7-9 */
* buf + + = 0 ; /* AFI */
* buf + + = BGP_AF_IPV6 ;
* buf + + = 1 ; /* SAFI */
return buf ;
}
2000-05-04 09:03:31 +00:00
# endif
2015-03-29 16:27:13 +00:00
static inline byte *
2009-11-26 19:47:59 +00:00
bgp_create_route_refresh ( struct bgp_conn * conn , byte * buf )
{
struct bgp_proto * p = conn - > bgp ;
BGP_TRACE ( D_PACKETS , " Sending ROUTE-REFRESH " ) ;
2015-03-29 16:27:13 +00:00
/* Original original route refresh request, RFC 2918 */
2010-01-03 11:17:52 +00:00
* buf + + = 0 ;
* buf + + = BGP_AF ;
2015-03-29 16:27:13 +00:00
* buf + + = BGP_RR_REQUEST ;
* buf + + = 1 ; /* SAFI */
2009-11-26 19:47:59 +00:00
return buf ;
}
2015-03-29 16:27:13 +00:00
static inline byte *
bgp_create_begin_refresh ( struct bgp_conn * conn , byte * buf )
{
struct bgp_proto * p = conn - > bgp ;
BGP_TRACE ( D_PACKETS , " Sending BEGIN-OF-RR " ) ;
/* Demarcation of beginning of route refresh (BoRR), RFC 7313 */
* buf + + = 0 ;
* buf + + = BGP_AF ;
* buf + + = BGP_RR_BEGIN ;
* buf + + = 1 ; /* SAFI */
return buf ;
}
static inline byte *
bgp_create_end_refresh ( struct bgp_conn * conn , byte * buf )
{
struct bgp_proto * p = conn - > bgp ;
BGP_TRACE ( D_PACKETS , " Sending END-OF-RR " ) ;
/* Demarcation of ending of route refresh (EoRR), RFC 7313 */
* buf + + = 0 ;
* buf + + = BGP_AF ;
* buf + + = BGP_RR_END ;
* buf + + = 1 ; /* SAFI */
return buf ;
}
2000-03-21 15:53:50 +00:00
static void
2015-05-19 06:53:34 +00:00
bgp_create_header ( byte * buf , uint len , uint type )
2000-03-21 15:53:50 +00:00
{
memset ( buf , 0xff , 16 ) ; /* Marker */
put_u16 ( buf + 16 , len ) ;
buf [ 18 ] = type ;
}
2000-06-04 17:06:18 +00:00
/**
* bgp_fire_tx - transmit packets
* @ conn : connection
*
* Whenever the transmit buffers of the underlying TCP connection
* are free and we have any packets queued for sending , the socket functions
* call bgp_fire_tx ( ) which takes care of selecting the highest priority packet
* queued ( Notification > Keepalive > Open > Update ) , assembling its header
* and body and sending it to the connection .
*/
2000-03-30 10:44:20 +00:00
static int
2000-03-21 15:53:50 +00:00
bgp_fire_tx ( struct bgp_conn * conn )
{
2000-05-02 16:07:41 +00:00
struct bgp_proto * p = conn - > bgp ;
2015-05-19 06:53:34 +00:00
uint s = conn - > packets_to_send ;
2000-03-21 15:53:50 +00:00
sock * sk = conn - > sk ;
2000-05-08 14:53:22 +00:00
byte * buf , * pkt , * end ;
2000-03-21 15:53:50 +00:00
int type ;
2000-05-08 14:53:22 +00:00
if ( ! sk )
{
conn - > packets_to_send = 0 ;
return 0 ;
}
buf = sk - > tbuf ;
pkt = buf + BGP_HEADER_LENGTH ;
2000-03-21 15:53:50 +00:00
if ( s & ( 1 < < PKT_SCHEDULE_CLOSE ) )
{
2008-12-19 00:34:39 +00:00
/* We can finally close connection and enter idle state */
bgp_conn_enter_idle_state ( conn ) ;
2000-03-30 10:44:20 +00:00
return 0 ;
2000-03-21 15:53:50 +00:00
}
if ( s & ( 1 < < PKT_NOTIFICATION ) )
{
s = 1 < < PKT_SCHEDULE_CLOSE ;
type = PKT_NOTIFICATION ;
end = bgp_create_notification ( conn , pkt ) ;
}
else if ( s & ( 1 < < PKT_KEEPALIVE ) )
{
s & = ~ ( 1 < < PKT_KEEPALIVE ) ;
type = PKT_KEEPALIVE ;
end = pkt ; /* Keepalives carry no data */
2000-05-02 16:07:41 +00:00
BGP_TRACE ( D_PACKETS , " Sending KEEPALIVE " ) ;
2000-03-30 10:44:20 +00:00
bgp_start_timer ( conn - > keepalive_timer , conn - > keepalive_time ) ;
2000-03-21 15:53:50 +00:00
}
else if ( s & ( 1 < < PKT_OPEN ) )
{
s & = ~ ( 1 < < PKT_OPEN ) ;
type = PKT_OPEN ;
end = bgp_create_open ( conn , pkt ) ;
}
2009-11-26 19:47:59 +00:00
else if ( s & ( 1 < < PKT_ROUTE_REFRESH ) )
{
s & = ~ ( 1 < < PKT_ROUTE_REFRESH ) ;
type = PKT_ROUTE_REFRESH ;
end = bgp_create_route_refresh ( conn , pkt ) ;
}
2015-03-29 16:27:13 +00:00
else if ( s & ( 1 < < PKT_BEGIN_REFRESH ) )
{
s & = ~ ( 1 < < PKT_BEGIN_REFRESH ) ;
type = PKT_ROUTE_REFRESH ; /* BoRR is a subtype of RR */
end = bgp_create_begin_refresh ( conn , pkt ) ;
}
2000-03-21 15:53:50 +00:00
else if ( s & ( 1 < < PKT_UPDATE ) )
{
type = PKT_UPDATE ;
2015-03-29 16:27:13 +00:00
end = bgp_create_update ( conn , pkt ) ;
2014-03-20 13:07:12 +00:00
2000-03-21 15:53:50 +00:00
if ( ! end )
2015-03-29 16:27:13 +00:00
{
/* No update to send, perhaps we need to send End-of-RIB or EoRR */
2000-03-21 15:53:50 +00:00
conn - > packets_to_send = 0 ;
2014-03-20 13:07:12 +00:00
2015-03-29 16:27:13 +00:00
if ( p - > feed_state = = BFS_LOADED )
{
type = PKT_UPDATE ;
end = bgp_create_end_mark ( conn , pkt ) ;
}
else if ( p - > feed_state = = BFS_REFRESHED )
{
type = PKT_ROUTE_REFRESH ;
end = bgp_create_end_refresh ( conn , pkt ) ;
}
else /* Really nothing to send */
2014-03-20 13:07:12 +00:00
return 0 ;
2015-03-29 16:27:13 +00:00
p - > feed_state = BFS_NONE ;
2000-03-21 15:53:50 +00:00
}
}
else
2000-03-30 10:44:20 +00:00
return 0 ;
2015-03-29 16:27:13 +00:00
2000-03-21 15:53:50 +00:00
conn - > packets_to_send = s ;
bgp_create_header ( buf , end - buf , type ) ;
2000-03-30 10:44:20 +00:00
return sk_send ( sk , end - buf ) ;
2000-03-21 15:53:50 +00:00
}
2000-06-04 17:06:18 +00:00
/**
* bgp_schedule_packet - schedule a packet for transmission
* @ conn : connection
* @ type : packet type
*
* Schedule a packet of type @ type to be sent as soon as possible .
*/
2000-03-21 15:53:50 +00:00
void
bgp_schedule_packet ( struct bgp_conn * conn , int type )
{
DBG ( " BGP: Scheduling packet type %d \n " , type ) ;
conn - > packets_to_send | = 1 < < type ;
2015-03-31 22:01:35 +00:00
if ( conn - > sk & & conn - > sk - > tpos = = conn - > sk - > tbuf & & ! ev_active ( conn - > tx_ev ) )
2008-12-19 00:34:39 +00:00
ev_schedule ( conn - > tx_ev ) ;
}
void
bgp_kick_tx ( void * vconn )
{
struct bgp_conn * conn = vconn ;
DBG ( " BGP: kicking TX \n " ) ;
2019-06-11 09:35:25 +00:00
uint max = 1024 ;
while ( - - max & & ( bgp_fire_tx ( conn ) > 0 ) )
2008-12-19 00:34:39 +00:00
;
2019-06-11 09:35:25 +00:00
if ( ! max & & ! ev_active ( conn - > tx_ev ) )
ev_schedule ( conn - > tx_ev ) ;
2000-03-21 15:53:50 +00:00
}
void
bgp_tx ( sock * sk )
{
struct bgp_conn * conn = sk - > data ;
DBG ( " BGP: TX hook \n " ) ;
2019-06-11 09:35:25 +00:00
uint max = 1024 ;
while ( - - max & & ( bgp_fire_tx ( conn ) > 0 ) )
2000-03-30 10:44:20 +00:00
;
2019-06-11 09:35:25 +00:00
if ( ! max & & ! ev_active ( conn - > tx_ev ) )
ev_schedule ( conn - > tx_ev ) ;
2000-03-30 10:44:20 +00:00
}
2008-10-26 21:36:08 +00:00
/* Capatibility negotiation as per RFC 2842 */
void
bgp_parse_capabilities ( struct bgp_conn * conn , byte * opt , int len )
{
2010-02-21 13:34:53 +00:00
// struct bgp_proto *p = conn->bgp;
2012-08-14 14:25:22 +00:00
int i , cl ;
2008-10-26 21:36:08 +00:00
while ( len > 0 )
{
if ( len < 2 | | len < 2 + opt [ 1 ] )
goto err ;
2015-03-29 16:27:13 +00:00
2008-10-26 21:36:08 +00:00
cl = opt [ 1 ] ;
switch ( opt [ 0 ] )
{
2010-01-03 11:17:52 +00:00
case 2 : /* Route refresh capability, RFC 2918 */
2009-11-26 19:47:59 +00:00
if ( cl ! = 0 )
goto err ;
conn - > peer_refresh_support = 1 ;
break ;
2010-01-03 11:17:52 +00:00
2015-11-25 14:52:58 +00:00
case 6 : /* Extended message length capability, draft */
if ( cl ! = 0 )
goto err ;
conn - > peer_ext_messages_support = 1 ;
break ;
2014-03-20 13:07:12 +00:00
case 64 : /* Graceful restart capability, RFC 4724 */
if ( cl % 4 ! = 2 )
goto err ;
conn - > peer_gr_aware = 1 ;
conn - > peer_gr_able = 0 ;
conn - > peer_gr_time = get_u16 ( opt + 2 ) & 0x0fff ;
conn - > peer_gr_flags = opt [ 2 ] & 0xf0 ;
conn - > peer_gr_aflags = 0 ;
for ( i = 2 ; i < cl ; i + = 4 )
if ( opt [ 2 + i + 0 ] = = 0 & & opt [ 2 + i + 1 ] = = BGP_AF & & opt [ 2 + i + 2 ] = = 1 ) /* Match AFI/SAFI */
{
conn - > peer_gr_able = 1 ;
conn - > peer_gr_aflags = opt [ 2 + i + 3 ] ;
}
break ;
2012-08-14 14:25:22 +00:00
case 65 : /* AS4 capability, RFC 4893 */
2008-10-26 21:36:08 +00:00
if ( cl ! = 4 )
goto err ;
2008-12-24 16:24:41 +00:00
conn - > peer_as4_support = 1 ;
2012-08-14 14:25:22 +00:00
if ( conn - > bgp - > cf - > enable_as4 )
2008-10-26 21:36:08 +00:00
conn - > advertised_as = get_u32 ( opt + 2 ) ;
break ;
2017-02-19 11:02:39 +00:00
case 69 : /* ADD-PATH capability, RFC 7911 */
2012-08-14 14:25:22 +00:00
if ( cl % 4 )
goto err ;
for ( i = 0 ; i < cl ; i + = 4 )
if ( opt [ 2 + i + 0 ] = = 0 & & opt [ 2 + i + 1 ] = = BGP_AF & & opt [ 2 + i + 2 ] = = 1 ) /* Match AFI/SAFI */
conn - > peer_add_path = opt [ 2 + i + 3 ] ;
if ( conn - > peer_add_path > ADD_PATH_FULL )
goto err ;
2015-03-29 16:27:13 +00:00
break ;
2012-08-14 14:25:22 +00:00
2015-03-29 16:27:13 +00:00
case 70 : /* Enhanced route refresh capability, RFC 7313 */
if ( cl ! = 0 )
goto err ;
conn - > peer_enhanced_refresh_support = 1 ;
2012-08-14 14:25:22 +00:00
break ;
2018-06-04 16:56:05 +00:00
case 71 : /* Long-lived graceful restart capability, RFC draft */
if ( cl % 7 )
goto err ;
conn - > peer_llgr_aware = 1 ;
conn - > peer_llgr_able = 0 ;
conn - > peer_llgr_time = 0 ;
conn - > peer_llgr_aflags = 0 ;
2018-08-01 15:35:39 +00:00
for ( i = 0 ; i < cl ; i + = 7 )
2018-06-04 16:56:05 +00:00
if ( opt [ 2 + i + 0 ] = = 0 & & opt [ 2 + i + 1 ] = = BGP_AF & & opt [ 2 + i + 2 ] = = 1 ) /* Match AFI/SAFI */
{
conn - > peer_llgr_able = 1 ;
conn - > peer_llgr_time = get_u32 ( opt + 2 + i + 3 ) & 0xffffff ;
conn - > peer_llgr_aflags = opt [ 2 + i + 3 ] ;
}
break ;
2008-10-26 21:36:08 +00:00
/* We can safely ignore all other capabilities */
}
len - = 2 + cl ;
opt + = 2 + cl ;
}
2018-06-04 16:56:05 +00:00
/* The LLGR capability must be advertised together with the GR capability,
otherwise it must be disregarded */
if ( ! conn - > peer_gr_aware & & conn - > peer_llgr_aware )
{
conn - > peer_llgr_aware = 0 ;
conn - > peer_llgr_able = 0 ;
conn - > peer_llgr_time = 0 ;
conn - > peer_llgr_aflags = 0 ;
}
2008-10-26 21:36:08 +00:00
return ;
2014-03-20 13:07:12 +00:00
err :
2008-10-26 21:36:08 +00:00
bgp_error ( conn , 2 , 0 , NULL , 0 ) ;
return ;
}
2000-04-25 13:32:17 +00:00
static int
bgp_parse_options ( struct bgp_conn * conn , byte * opt , int len )
{
2009-03-13 11:49:44 +00:00
struct bgp_proto * p = conn - > bgp ;
2008-10-26 21:36:08 +00:00
int ol ;
2000-04-25 13:32:17 +00:00
while ( len > 0 )
{
if ( len < 2 | | len < 2 + opt [ 1 ] )
2000-04-25 21:13:25 +00:00
{ bgp_error ( conn , 2 , 0 , NULL , 0 ) ; return 0 ; }
2000-04-25 13:32:17 +00:00
# ifdef LOCAL_DEBUG
{
int i ;
DBG ( " \t Option %02x: " , opt [ 0 ] ) ;
for ( i = 0 ; i < opt [ 1 ] ; i + + )
DBG ( " %02x " , opt [ 2 + i ] ) ;
DBG ( " \n " ) ;
}
# endif
2008-10-26 21:36:08 +00:00
ol = opt [ 1 ] ;
2000-04-25 13:32:17 +00:00
switch ( opt [ 0 ] )
{
case 2 :
2009-03-13 11:49:44 +00:00
if ( conn - > start_state = = BSS_CONNECT_NOCAP )
BGP_TRACE ( D_PACKETS , " Ignoring received capabilities " ) ;
else
bgp_parse_capabilities ( conn , opt + 2 , ol ) ;
2000-04-25 13:32:17 +00:00
break ;
2008-10-26 21:36:08 +00:00
2000-04-25 13:32:17 +00:00
default :
/*
* BGP specs don ' t tell us to send which option
* we didn ' t recognize , but it ' s common practice
* to do so . Also , capability negotiation with
* Cisco routers doesn ' t work without that .
*/
2008-10-26 21:36:08 +00:00
bgp_error ( conn , 2 , 4 , opt , ol ) ;
2000-04-25 13:32:17 +00:00
return 0 ;
}
2008-10-26 21:36:08 +00:00
len - = 2 + ol ;
opt + = 2 + ol ;
2000-04-25 13:32:17 +00:00
}
return 0 ;
}
2000-03-30 10:44:20 +00:00
static void
2016-10-14 13:37:04 +00:00
bgp_rx_open ( struct bgp_conn * conn , byte * pkt , uint len )
2000-03-30 10:44:20 +00:00
{
2000-03-30 17:39:48 +00:00
struct bgp_conn * other ;
2000-03-30 10:44:20 +00:00
struct bgp_proto * p = conn - > bgp ;
2008-10-26 21:36:08 +00:00
unsigned hold ;
2009-03-13 11:49:44 +00:00
u16 base_as ;
2000-03-30 10:44:20 +00:00
u32 id ;
/* Check state */
if ( conn - > state ! = BS_OPENSENT )
2013-07-25 11:55:24 +00:00
{ bgp_error ( conn , 5 , fsm_err_subcode [ conn - > state ] , NULL , 0 ) ; return ; }
2000-03-30 10:44:20 +00:00
/* Check message contents */
2016-10-14 13:37:04 +00:00
if ( len < 29 | | len ! = 29U + pkt [ 28 ] )
2000-04-25 21:13:25 +00:00
{ bgp_error ( conn , 1 , 2 , pkt + 16 , 2 ) ; return ; }
2000-03-30 10:44:20 +00:00
if ( pkt [ 19 ] ! = BGP_VERSION )
2000-04-25 21:13:25 +00:00
{ bgp_error ( conn , 2 , 1 , pkt + 19 , 1 ) ; return ; } /* RFC 1771 says 16 bits, draft-09 tells to use 8 */
2009-03-13 11:49:44 +00:00
conn - > advertised_as = base_as = get_u16 ( pkt + 20 ) ;
2000-03-30 10:44:20 +00:00
hold = get_u16 ( pkt + 22 ) ;
id = get_u32 ( pkt + 24 ) ;
2008-10-26 21:36:08 +00:00
BGP_TRACE ( D_PACKETS , " Got OPEN(as=%d,hold=%d,id=%08x) " , conn - > advertised_as , hold , id ) ;
2000-04-25 13:32:17 +00:00
if ( bgp_parse_options ( conn , pkt + 29 , pkt [ 28 ] ) )
return ;
2008-10-26 21:36:08 +00:00
if ( hold > 0 & & hold < 3 )
{ bgp_error ( conn , 2 , 6 , pkt + 22 , 2 ) ; return ; }
2015-03-29 19:24:47 +00:00
/* RFC 6286 2.2 - router ID is nonzero and AS-wide unique */
if ( ! id | | ( p - > is_internal & & id = = p - > local_id ) )
2000-04-25 21:13:25 +00:00
{ bgp_error ( conn , 2 , 3 , pkt + 24 , - 4 ) ; return ; }
2000-03-30 10:44:20 +00:00
2009-03-13 11:49:44 +00:00
if ( ( conn - > advertised_as ! = base_as ) & & ( base_as ! = AS_TRANS ) )
log ( L_WARN " %s: Peer advertised inconsistent AS numbers " , p - > p . name ) ;
2008-10-26 21:36:08 +00:00
if ( conn - > advertised_as ! = p - > remote_as )
2009-09-24 21:14:44 +00:00
{
if ( conn - > peer_as4_support )
{
u32 val = htonl ( conn - > advertised_as ) ;
bgp_error ( conn , 2 , 2 , ( byte * ) & val , 4 ) ;
}
else
bgp_error ( conn , 2 , 2 , pkt + 20 , 2 ) ;
return ;
}
2008-10-26 21:36:08 +00:00
2000-03-30 17:39:48 +00:00
/* Check the other connection */
other = ( conn = = & p - > outgoing_conn ) ? & p - > incoming_conn : & p - > outgoing_conn ;
switch ( other - > state )
{
case BS_CONNECT :
case BS_ACTIVE :
2014-03-20 13:07:12 +00:00
/* Stop outgoing connection attempts */
bgp_conn_enter_idle_state ( other ) ;
break ;
case BS_IDLE :
2000-03-30 17:39:48 +00:00
case BS_OPENSENT :
2008-12-19 00:34:39 +00:00
case BS_CLOSE :
2000-03-30 17:39:48 +00:00
break ;
2014-03-20 13:07:12 +00:00
2000-03-30 17:39:48 +00:00
case BS_OPENCONFIRM :
2015-03-29 19:24:47 +00:00
/*
* Description of collision detection rules in RFC 4271 is confusing and
* contradictory , but it is essentially :
*
* 1. Router with higher ID is dominant
* 2. If both have the same ID , router with higher ASN is dominant [ RFC6286 ]
* 3. When both connections are in OpenConfirm state , one initiated by
* the dominant router is kept .
*
* The first line in the expression below evaluates whether the neighbor
* is dominant , the second line whether the new connection was initiated
* by the neighbor . If both are true ( or both are false ) , we keep the new
* connection , otherwise we keep the old one .
*/
if ( ( ( p - > local_id < id ) | | ( ( p - > local_id = = id ) & & ( p - > local_as < p - > remote_as ) ) )
= = ( conn = = & p - > incoming_conn ) )
{
2000-03-30 17:39:48 +00:00
/* Should close the other connection */
2000-05-02 16:07:41 +00:00
BGP_TRACE ( D_EVENTS , " Connection collision, giving up the other connection " ) ;
2009-06-06 22:38:38 +00:00
bgp_error ( other , 6 , 7 , NULL , 0 ) ;
2000-03-30 17:39:48 +00:00
break ;
}
/* Fall thru */
case BS_ESTABLISHED :
/* Should close this connection */
2000-05-02 16:07:41 +00:00
BGP_TRACE ( D_EVENTS , " Connection collision, giving up this connection " ) ;
2009-06-06 22:38:38 +00:00
bgp_error ( conn , 6 , 7 , NULL , 0 ) ;
2000-03-30 17:39:48 +00:00
return ;
default :
bug ( " bgp_rx_open: Unknown state " ) ;
}
2000-03-30 10:44:20 +00:00
/* Update our local variables */
2008-12-19 00:34:39 +00:00
conn - > hold_time = MIN ( hold , p - > cf - > hold_time ) ;
2000-03-30 10:44:20 +00:00
conn - > keepalive_time = p - > cf - > keepalive_time ? : conn - > hold_time / 3 ;
p - > remote_id = id ;
2012-08-14 14:25:22 +00:00
p - > as4_session = p - > cf - > enable_as4 & & conn - > peer_as4_support ;
p - > add_path_rx = ( p - > cf - > add_path & ADD_PATH_RX ) & & ( conn - > peer_add_path & ADD_PATH_TX ) ;
p - > add_path_tx = ( p - > cf - > add_path & ADD_PATH_TX ) & & ( conn - > peer_add_path & ADD_PATH_RX ) ;
2018-06-04 16:56:05 +00:00
p - > gr_ready = ( p - > cf - > gr_mode & & conn - > peer_gr_able ) | |
( p - > cf - > llgr_mode & & conn - > peer_llgr_able ) ;
2015-07-18 11:38:21 +00:00
p - > ext_messages = p - > cf - > enable_extended_messages & & conn - > peer_ext_messages_support ;
2012-08-14 14:25:22 +00:00
2017-03-11 15:21:28 +00:00
/* Update RA mode */
2012-08-14 14:25:22 +00:00
if ( p - > add_path_tx )
p - > p . accept_ra_types = RA_ANY ;
2017-03-11 15:21:28 +00:00
else if ( p - > cf - > secondary )
p - > p . accept_ra_types = RA_ACCEPTED ;
else
p - > p . accept_ra_types = RA_OPTIMAL ;
2008-12-24 16:24:41 +00:00
2008-12-19 00:34:39 +00:00
DBG ( " BGP: Hold timer set to %d, keepalive to %d, AS to %d, ID to %x, AS4 session to %d \n " , conn - > hold_time , conn - > keepalive_time , p - > remote_as , p - > remote_id , p - > as4_session ) ;
2000-03-30 10:44:20 +00:00
bgp_schedule_packet ( conn , PKT_KEEPALIVE ) ;
bgp_start_timer ( conn - > hold_timer , conn - > hold_time ) ;
2010-01-03 11:17:52 +00:00
bgp_conn_enter_openconfirm_state ( conn ) ;
2000-03-30 10:44:20 +00:00
}
2014-03-20 13:07:12 +00:00
static inline void
bgp_rx_end_mark ( struct bgp_proto * p )
{
2015-03-29 16:27:13 +00:00
BGP_TRACE ( D_PACKETS , " Got END-OF-RIB " ) ;
if ( p - > load_state = = BFS_LOADING )
p - > load_state = BFS_NONE ;
2014-03-20 13:07:12 +00:00
if ( p - > p . gr_recovery )
proto_graceful_restart_unlock ( & p - > p ) ;
if ( p - > gr_active )
bgp_graceful_restart_done ( p ) ;
}
2000-03-30 18:44:23 +00:00
# define DECODE_PREFIX(pp, ll) do { \
2012-08-14 14:25:22 +00:00
if ( p - > add_path_rx ) \
{ \
if ( ll < 5 ) { err = 1 ; goto done ; } \
path_id = get_u32 ( pp ) ; \
pp + = 4 ; \
ll - = 4 ; \
} \
2000-03-30 18:44:23 +00:00
int b = * pp + + ; \
int q ; \
ll - - ; \
2011-03-29 23:09:18 +00:00
if ( b > BITS_PER_IP_ADDRESS ) { err = 10 ; goto done ; } \
2000-03-30 18:44:23 +00:00
q = ( b + 7 ) / 8 ; \
2011-03-29 23:09:18 +00:00
if ( ll < q ) { err = 1 ; goto done ; } \
2000-03-31 23:21:37 +00:00
memcpy ( & prefix , pp , q ) ; \
2000-03-30 18:44:23 +00:00
pp + = q ; \
ll - = q ; \
2000-04-17 09:37:31 +00:00
ipa_ntoh ( prefix ) ; \
prefix = ipa_and ( prefix , ipa_mkmask ( b ) ) ; \
2000-03-31 23:21:37 +00:00
pxlen = b ; \
2000-03-30 18:44:23 +00:00
} while ( 0 )
2012-08-14 14:25:22 +00:00
static inline void
bgp_rte_update ( struct bgp_proto * p , ip_addr prefix , int pxlen ,
u32 path_id , u32 * last_id , struct rte_src * * src ,
rta * a0 , rta * * a )
{
if ( path_id ! = * last_id )
{
* src = rt_get_source ( & p - > p , path_id ) ;
* last_id = path_id ;
if ( * a )
{
rta_free ( * a ) ;
* a = NULL ;
}
}
/* Prepare cached route attributes */
if ( ! * a )
{
a0 - > src = * src ;
2015-03-02 09:58:20 +00:00
/* Workaround for rta_lookup() breaking eattrs */
ea_list * ea = a0 - > eattrs ;
2012-08-14 14:25:22 +00:00
* a = rta_lookup ( a0 ) ;
2015-03-02 09:58:20 +00:00
a0 - > eattrs = ea ;
2012-08-14 14:25:22 +00:00
}
net * n = net_get ( p - > p . table , prefix , pxlen ) ;
rte * e = rte_get_temp ( rta_clone ( * a ) ) ;
e - > net = n ;
e - > pflags = 0 ;
e - > u . bgp . suppressed = 0 ;
2018-06-04 16:56:05 +00:00
e - > u . bgp . stale = - 1 ;
2012-08-14 14:25:22 +00:00
rte_update2 ( p - > p . main_ahook , n , e , * src ) ;
}
static inline void
bgp_rte_withdraw ( struct bgp_proto * p , ip_addr prefix , int pxlen ,
u32 path_id , u32 * last_id , struct rte_src * * src )
{
if ( path_id ! = * last_id )
{
* src = rt_find_source ( & p - > p , path_id ) ;
* last_id = path_id ;
}
net * n = net_find ( p - > p . table , prefix , pxlen ) ;
rte_update2 ( p - > p . main_ahook , n , NULL , * src ) ;
}
2000-05-04 09:03:31 +00:00
static inline int
2010-07-05 15:50:19 +00:00
bgp_set_next_hop ( struct bgp_proto * p , rta * a )
2000-05-04 09:03:31 +00:00
{
struct eattr * nh = ea_find ( a - > eattrs , EA_CODE ( EAP_BGP , BA_NEXT_HOP ) ) ;
2010-07-28 09:45:35 +00:00
ip_addr * nexthop = ( ip_addr * ) nh - > u . ptr - > data ;
# ifdef IPV6
2013-04-16 15:40:44 +00:00
int second = ( nh - > u . ptr - > length = = NEXT_HOP_LENGTH ) & & ipa_nonzero ( nexthop [ 1 ] ) ;
2012-01-08 14:28:27 +00:00
/* First address should not be link-local, but may be zero in direct mode */
2014-10-24 09:11:43 +00:00
if ( ipa_is_link_local ( * nexthop ) )
2012-01-08 14:28:27 +00:00
* nexthop = IPA_NONE ;
2010-07-28 09:45:35 +00:00
# else
int second = 0 ;
# endif
2010-07-05 15:50:19 +00:00
2010-07-13 10:48:23 +00:00
if ( p - > cf - > gw_mode = = GW_DIRECT )
2000-05-04 09:03:31 +00:00
{
2012-01-09 01:40:57 +00:00
neighbor * ng = NULL ;
2012-01-08 14:28:27 +00:00
if ( ipa_nonzero ( * nexthop ) )
ng = neigh_find ( & p - > p , nexthop , 0 ) ;
else if ( second ) /* GW_DIRECT -> single_hop -> p->neigh != NULL */
ng = neigh_find2 ( & p - > p , nexthop + 1 , p - > neigh - > iface , 0 ) ;
/* Fallback */
if ( ! ng )
ng = p - > neigh ;
2010-07-05 15:50:19 +00:00
if ( ng - > scope = = SCOPE_HOST )
return 0 ;
a - > dest = RTD_ROUTER ;
a - > gw = ng - > addr ;
a - > iface = ng - > iface ;
a - > hostentry = NULL ;
2010-07-30 23:04:32 +00:00
a - > igp_metric = 0 ;
2000-05-04 09:03:31 +00:00
}
2010-07-13 10:48:23 +00:00
else /* GW_RECURSIVE */
2012-01-08 14:28:27 +00:00
{
if ( ipa_zero ( * nexthop ) )
return 0 ;
rta_set_recursive_next_hop ( p - > p . table , a , p - > igp_table , nexthop , nexthop + second ) ;
}
2010-07-05 15:50:19 +00:00
2000-05-04 09:03:31 +00:00
return 1 ;
}
# ifndef IPV6 /* IPv4 version */
2000-03-30 10:44:20 +00:00
static void
2000-05-04 09:03:31 +00:00
bgp_do_rx_update ( struct bgp_conn * conn ,
byte * withdrawn , int withdrawn_len ,
byte * nlri , int nlri_len ,
byte * attrs , int attr_len )
2000-03-30 10:44:20 +00:00
{
2000-05-02 16:07:41 +00:00
struct bgp_proto * p = conn - > bgp ;
2012-08-14 14:25:22 +00:00
struct rte_src * src = p - > p . main_source ;
2014-04-14 10:50:03 +00:00
rta * a0 , * a = NULL ;
2011-03-29 23:09:18 +00:00
ip_addr prefix ;
int pxlen , err = 0 ;
2012-08-14 14:25:22 +00:00
u32 path_id = 0 ;
u32 last_id = 0 ;
2000-03-30 18:44:23 +00:00
2014-03-20 13:07:12 +00:00
/* Check for End-of-RIB marker */
if ( ! withdrawn_len & & ! attr_len & & ! nlri_len )
{
bgp_rx_end_mark ( p ) ;
return ;
}
2000-03-30 18:44:23 +00:00
/* Withdraw routes */
while ( withdrawn_len )
{
DECODE_PREFIX ( withdrawn , withdrawn_len ) ;
2000-03-31 23:21:37 +00:00
DBG ( " Withdraw %I/%d \n " , prefix , pxlen ) ;
2012-08-14 14:25:22 +00:00
bgp_rte_withdraw ( p , prefix , pxlen , path_id , & last_id , & src ) ;
2000-03-30 18:44:23 +00:00
}
2000-04-27 22:40:19 +00:00
if ( ! attr_len & & ! nlri_len ) /* shortcut */
return ;
2000-04-21 12:25:35 +00:00
a0 = bgp_decode_attrs ( conn , attrs , attr_len , bgp_linpool , nlri_len ) ;
2011-03-29 23:09:18 +00:00
if ( conn - > state ! = BS_ESTABLISHED ) /* fatal error during decoding */
return ;
2014-05-03 22:01:06 +00:00
if ( a0 & & nlri_len & & ! bgp_set_next_hop ( p , a0 ) )
2012-08-14 14:25:22 +00:00
a0 = NULL ;
last_id = 0 ;
src = p - > p . main_source ;
2011-03-29 23:09:18 +00:00
while ( nlri_len )
2000-03-30 18:44:23 +00:00
{
2011-03-29 23:09:18 +00:00
DECODE_PREFIX ( nlri , nlri_len ) ;
DBG ( " Add %I/%d \n " , prefix , pxlen ) ;
2012-08-14 14:25:22 +00:00
if ( a0 )
bgp_rte_update ( p , prefix , pxlen , path_id , & last_id , & src , a0 , & a ) ;
else /* Forced withdraw as a result of soft error */
bgp_rte_withdraw ( p , prefix , pxlen , path_id , & last_id , & src ) ;
2011-03-29 23:09:18 +00:00
}
2009-06-04 11:31:09 +00:00
2011-03-29 23:09:18 +00:00
done :
2000-05-04 09:03:31 +00:00
if ( a )
rta_free ( a ) ;
2011-03-29 23:09:18 +00:00
if ( err )
bgp_error ( conn , 3 , err , NULL , 0 ) ;
2000-04-01 09:17:33 +00:00
return ;
2000-05-04 09:03:31 +00:00
}
2000-04-01 09:17:33 +00:00
2000-05-04 09:03:31 +00:00
# else /* IPv6 version */
# define DO_NLRI(name) \
2016-10-14 13:37:04 +00:00
x = p - > name # # _start ; \
2000-05-04 09:03:31 +00:00
len = len0 = p - > name # # _len ; \
if ( len ) \
{ \
2011-03-29 23:09:18 +00:00
if ( len < 3 ) { err = 9 ; goto done ; } \
2000-05-04 09:03:31 +00:00
af = get_u16 ( x ) ; \
x + = 3 ; \
len - = 3 ; \
2016-10-14 13:37:04 +00:00
DBG ( " \t NLRI AF=%d sub=%d len=%d \n " , af , x [ - 1 ] , len ) ; \
2000-05-04 09:03:31 +00:00
} \
else \
af = 0 ; \
if ( af = = BGP_AF_IPV6 )
2011-03-29 23:09:18 +00:00
static void
bgp_attach_next_hop ( rta * a0 , byte * x )
{
ip_addr * nh = ( ip_addr * ) bgp_attach_attr_wa ( & a0 - > eattrs , bgp_linpool , BA_NEXT_HOP , NEXT_HOP_LENGTH ) ;
memcpy ( nh , x + 1 , 16 ) ;
ipa_ntoh ( nh [ 0 ] ) ;
/* We store received link local address in the other part of BA_NEXT_HOP eattr. */
if ( * x = = 32 )
{
memcpy ( nh + 1 , x + 17 , 16 ) ;
ipa_ntoh ( nh [ 1 ] ) ;
}
else
nh [ 1 ] = IPA_NONE ;
}
2000-05-04 09:03:31 +00:00
static void
bgp_do_rx_update ( struct bgp_conn * conn ,
2016-10-14 13:37:04 +00:00
byte * withdrawn UNUSED , int withdrawn_len ,
byte * nlri UNUSED , int nlri_len ,
2000-05-04 09:03:31 +00:00
byte * attrs , int attr_len )
{
struct bgp_proto * p = conn - > bgp ;
2012-08-14 14:25:22 +00:00
struct rte_src * src = p - > p . main_source ;
2016-10-14 13:37:04 +00:00
byte * x ;
2000-05-04 09:03:31 +00:00
int len , len0 ;
2016-10-14 13:37:04 +00:00
unsigned af ;
2014-04-14 10:50:03 +00:00
rta * a0 , * a = NULL ;
2011-03-29 23:09:18 +00:00
ip_addr prefix ;
int pxlen , err = 0 ;
2012-08-14 14:25:22 +00:00
u32 path_id = 0 ;
u32 last_id = 0 ;
2000-05-04 09:03:31 +00:00
p - > mp_reach_len = 0 ;
p - > mp_unreach_len = 0 ;
a0 = bgp_decode_attrs ( conn , attrs , attr_len , bgp_linpool , 0 ) ;
2011-03-29 23:09:18 +00:00
if ( conn - > state ! = BS_ESTABLISHED ) /* fatal error during decoding */
2000-05-04 09:03:31 +00:00
return ;
2014-03-20 13:07:12 +00:00
/* Check for End-of-RIB marker */
2014-03-24 18:22:19 +00:00
if ( ( attr_len < 8 ) & & ! withdrawn_len & & ! nlri_len & & ! p - > mp_reach_len & &
2014-03-20 13:07:12 +00:00
( p - > mp_unreach_len = = 3 ) & & ( get_u16 ( p - > mp_unreach_start ) = = BGP_AF_IPV6 ) )
{
bgp_rx_end_mark ( p ) ;
return ;
}
2000-05-04 09:03:31 +00:00
DO_NLRI ( mp_unreach )
{
while ( len )
{
DECODE_PREFIX ( x , len ) ;
DBG ( " Withdraw %I/%d \n " , prefix , pxlen ) ;
2012-08-14 14:25:22 +00:00
bgp_rte_withdraw ( p , prefix , pxlen , path_id , & last_id , & src ) ;
2000-05-04 09:03:31 +00:00
}
}
DO_NLRI ( mp_reach )
{
/* Create fake NEXT_HOP attribute */
if ( len < 1 | | ( * x ! = 16 & & * x ! = 32 ) | | len < * x + 2 )
2011-03-29 23:09:18 +00:00
{ err = 9 ; goto done ; }
2000-05-04 09:03:31 +00:00
2011-03-29 23:09:18 +00:00
if ( a0 )
bgp_attach_next_hop ( a0 , x ) ;
2009-04-23 21:15:07 +00:00
/* Also ignore one reserved byte */
len - = * x + 2 ;
x + = * x + 2 ;
2000-05-04 09:03:31 +00:00
2012-08-14 14:25:22 +00:00
if ( a0 & & ! bgp_set_next_hop ( p , a0 ) )
a0 = NULL ;
last_id = 0 ;
src = p - > p . main_source ;
2011-03-29 23:09:18 +00:00
while ( len )
2000-05-04 09:03:31 +00:00
{
2011-03-29 23:09:18 +00:00
DECODE_PREFIX ( x , len ) ;
DBG ( " Add %I/%d \n " , prefix , pxlen ) ;
2012-08-14 14:25:22 +00:00
if ( a0 )
bgp_rte_update ( p , prefix , pxlen , path_id , & last_id , & src , a0 , & a ) ;
else /* Forced withdraw as a result of soft error */
bgp_rte_withdraw ( p , prefix , pxlen , path_id , & last_id , & src ) ;
2000-05-04 09:03:31 +00:00
}
}
2011-03-29 23:09:18 +00:00
done :
2000-04-01 09:17:33 +00:00
if ( a )
rta_free ( a ) ;
2011-03-29 23:09:18 +00:00
if ( err ) /* Use subcode 9, not err */
bgp_error ( conn , 3 , 9 , NULL , 0 ) ;
2000-05-04 09:03:31 +00:00
return ;
}
# endif
static void
2016-10-14 13:37:04 +00:00
bgp_rx_update ( struct bgp_conn * conn , byte * pkt , uint len )
2000-05-04 09:03:31 +00:00
{
struct bgp_proto * p = conn - > bgp ;
byte * withdrawn , * attrs , * nlri ;
2016-10-14 13:37:04 +00:00
uint withdrawn_len , attr_len , nlri_len ;
2000-05-04 09:03:31 +00:00
2009-02-26 13:23:54 +00:00
BGP_TRACE_RL ( & rl_rcv_update , D_PACKETS , " Got UPDATE " ) ;
2010-02-02 09:14:21 +00:00
/* Workaround for some BGP implementations that skip initial KEEPALIVE */
if ( conn - > state = = BS_OPENCONFIRM )
bgp_conn_enter_established_state ( conn ) ;
2000-05-04 09:03:31 +00:00
if ( conn - > state ! = BS_ESTABLISHED )
2013-07-25 11:55:24 +00:00
{ bgp_error ( conn , 5 , fsm_err_subcode [ conn - > state ] , NULL , 0 ) ; return ; }
2000-05-04 09:03:31 +00:00
bgp_start_timer ( conn - > hold_timer , conn - > hold_time ) ;
/* Find parts of the packet and check sizes */
if ( len < 23 )
{
bgp_error ( conn , 1 , 2 , pkt + 16 , 2 ) ;
return ;
}
withdrawn = pkt + 21 ;
withdrawn_len = get_u16 ( pkt + 19 ) ;
if ( withdrawn_len + 23 > len )
goto malformed ;
attrs = withdrawn + withdrawn_len + 2 ;
attr_len = get_u16 ( attrs - 2 ) ;
if ( withdrawn_len + attr_len + 23 > len )
goto malformed ;
nlri = attrs + attr_len ;
nlri_len = len - withdrawn_len - attr_len - 23 ;
if ( ! attr_len & & nlri_len )
goto malformed ;
DBG ( " Sizes: withdrawn=%d, attrs=%d, NLRI=%d \n " , withdrawn_len , attr_len , nlri_len ) ;
lp_flush ( bgp_linpool ) ;
bgp_do_rx_update ( conn , withdrawn , withdrawn_len , nlri , nlri_len , attrs , attr_len ) ;
return ;
malformed :
2000-04-25 21:13:25 +00:00
bgp_error ( conn , 3 , 1 , NULL , 0 ) ;
}
static struct {
byte major , minor ;
byte * msg ;
} bgp_msg_table [ ] = {
{ 1 , 0 , " Invalid message header " } ,
{ 1 , 1 , " Connection not synchronized " } ,
{ 1 , 2 , " Bad message length " } ,
{ 1 , 3 , " Bad message type " } ,
{ 2 , 0 , " Invalid OPEN message " } ,
{ 2 , 1 , " Unsupported version number " } ,
{ 2 , 2 , " Bad peer AS " } ,
{ 2 , 3 , " Bad BGP identifier " } ,
{ 2 , 4 , " Unsupported optional parameter " } ,
{ 2 , 5 , " Authentication failure " } ,
{ 2 , 6 , " Unacceptable hold time " } ,
2015-07-18 11:38:21 +00:00
{ 2 , 7 , " Required capability missing " } , /* [RFC5492] */
2010-05-02 20:41:40 +00:00
{ 2 , 8 , " No supported AFI/SAFI " } , /* This error msg is nonstandard */
2000-04-25 21:13:25 +00:00
{ 3 , 0 , " Invalid UPDATE message " } ,
{ 3 , 1 , " Malformed attribute list " } ,
{ 3 , 2 , " Unrecognized well-known attribute " } ,
{ 3 , 3 , " Missing mandatory attribute " } ,
{ 3 , 4 , " Invalid attribute flags " } ,
{ 3 , 5 , " Invalid attribute length " } ,
{ 3 , 6 , " Invalid ORIGIN attribute " } ,
{ 3 , 7 , " AS routing loop " } , /* Deprecated */
{ 3 , 8 , " Invalid NEXT_HOP attribute " } ,
{ 3 , 9 , " Optional attribute error " } ,
{ 3 , 10 , " Invalid network field " } ,
{ 3 , 11 , " Malformed AS_PATH " } ,
{ 4 , 0 , " Hold timer expired " } ,
2013-07-25 11:55:24 +00:00
{ 5 , 0 , " Finite state machine error " } , /* Subcodes are according to [RFC6608] */
{ 5 , 1 , " Unexpected message in OpenSent state " } ,
{ 5 , 2 , " Unexpected message in OpenConfirm state " } ,
{ 5 , 3 , " Unexpected message in Established state " } ,
2008-12-24 16:24:41 +00:00
{ 6 , 0 , " Cease " } , /* Subcodes are according to [RFC4486] */
{ 6 , 1 , " Maximum number of prefixes reached " } ,
{ 6 , 2 , " Administrative shutdown " } ,
{ 6 , 3 , " Peer de-configured " } ,
{ 6 , 4 , " Administrative reset " } ,
{ 6 , 5 , " Connection rejected " } ,
{ 6 , 6 , " Other configuration change " } ,
{ 6 , 7 , " Connection collision resolution " } ,
2015-03-29 16:27:13 +00:00
{ 6 , 8 , " Out of Resources " } ,
{ 7 , 0 , " Invalid ROUTE-REFRESH message " } , /* [RFC7313] */
{ 7 , 1 , " Invalid ROUTE-REFRESH message length " } /* [RFC7313] */
2000-04-25 21:13:25 +00:00
} ;
2008-12-19 00:34:39 +00:00
/**
* bgp_error_dsc - return BGP error description
* @ code : BGP error code
* @ subcode : BGP error subcode
*
* bgp_error_dsc ( ) returns error description for BGP errors
* which might be static string or given temporary buffer .
*/
2010-04-06 22:19:23 +00:00
const char *
bgp_error_dsc ( unsigned code , unsigned subcode )
2008-12-19 00:34:39 +00:00
{
2010-04-06 22:19:23 +00:00
static char buff [ 32 ] ;
2008-12-19 00:34:39 +00:00
unsigned i ;
for ( i = 0 ; i < ARRAY_SIZE ( bgp_msg_table ) ; i + + )
if ( bgp_msg_table [ i ] . major = = code & & bgp_msg_table [ i ] . minor = = subcode )
{
return bgp_msg_table [ i ] . msg ;
}
bsprintf ( buff , " Unknown error %d.%d " , code , subcode ) ;
return buff ;
}
2017-09-19 17:55:37 +00:00
/* RFC 8203 - shutdown communication message */
static int
bgp_handle_message ( struct bgp_proto * p , byte * data , uint len , byte * * bp )
{
byte * msg = data + 1 ;
uint msg_len = data [ 0 ] ;
uint i ;
/* Handle zero length message */
if ( msg_len = = 0 )
return 1 ;
/* Handle proper message */
2019-07-11 13:44:52 +00:00
if ( ( msg_len > 255 ) & & ( msg_len + 1 > len ) )
2017-09-19 17:55:37 +00:00
return 0 ;
/* Some elementary cleanup */
for ( i = 0 ; i < msg_len ; i + + )
if ( msg [ i ] < ' ' )
msg [ i ] = ' ' ;
proto_set_message ( & p - > p , msg , msg_len ) ;
* bp + = bsprintf ( * bp , " : \" %s \" " , p - > p . message ) ;
return 1 ;
}
2000-04-25 21:13:25 +00:00
void
2009-06-06 22:38:38 +00:00
bgp_log_error ( struct bgp_proto * p , u8 class , char * msg , unsigned code , unsigned subcode , byte * data , unsigned len )
2000-04-25 21:13:25 +00:00
{
2017-09-19 17:55:37 +00:00
byte argbuf [ 256 ] , * t = argbuf ;
2000-04-25 21:13:25 +00:00
unsigned i ;
2009-06-06 22:38:38 +00:00
/* Don't report Cease messages generated by myself */
if ( code = = 6 & & class = = BE_BGP_TX )
2000-04-25 23:08:03 +00:00
return ;
2017-09-19 17:55:37 +00:00
/* Reset shutdown message */
if ( ( code = = 6 ) & & ( ( subcode = = 2 ) | | ( subcode = = 4 ) ) )
proto_set_message ( & p - > p , NULL , 0 ) ;
2000-04-25 21:13:25 +00:00
if ( len )
{
2017-09-19 17:55:37 +00:00
/* Bad peer AS - we would like to print the AS */
2009-09-24 21:14:44 +00:00
if ( ( code = = 2 ) & & ( subcode = = 2 ) & & ( ( len = = 2 ) | | ( len = = 4 ) ) )
{
2017-09-19 17:55:37 +00:00
t + = bsprintf ( t , " : %u " , ( len = = 2 ) ? get_u16 ( data ) : get_u32 ( data ) ) ;
2009-09-24 21:14:44 +00:00
goto done ;
}
2017-09-19 17:55:37 +00:00
/* RFC 8203 - shutdown communication */
if ( ( ( code = = 6 ) & & ( ( subcode = = 2 ) | | ( subcode = = 4 ) ) ) )
if ( bgp_handle_message ( p , data , len , & t ) )
goto done ;
* t + + = ' : ' ;
* t + + = ' ' ;
2000-04-25 21:13:25 +00:00
if ( len > 16 )
len = 16 ;
for ( i = 0 ; i < len ; i + + )
t + = bsprintf ( t , " %02x " , data [ i ] ) ;
}
2017-09-19 17:55:37 +00:00
done :
2000-04-25 21:13:25 +00:00
* t = 0 ;
2017-09-19 17:55:37 +00:00
const byte * dsc = bgp_error_dsc ( code , subcode ) ;
log ( L_REMOTE " %s: %s: %s%s " , p - > p . name , msg , dsc , argbuf ) ;
2000-03-30 10:44:20 +00:00
}
static void
2016-10-14 13:37:04 +00:00
bgp_rx_notification ( struct bgp_conn * conn , byte * pkt , uint len )
2000-03-30 10:44:20 +00:00
{
2008-12-24 16:24:41 +00:00
struct bgp_proto * p = conn - > bgp ;
2000-03-30 10:44:20 +00:00
if ( len < 21 )
{
2000-04-25 21:13:25 +00:00
bgp_error ( conn , 1 , 2 , pkt + 16 , 2 ) ;
2000-03-30 10:44:20 +00:00
return ;
}
2008-12-19 00:34:39 +00:00
unsigned code = pkt [ 19 ] ;
unsigned subcode = pkt [ 20 ] ;
2009-06-06 22:38:38 +00:00
int err = ( code ! = 6 ) ;
2008-12-24 16:24:41 +00:00
2009-06-06 22:38:38 +00:00
bgp_log_error ( p , BE_BGP_RX , " Received " , code , subcode , pkt + 21 , len - 21 ) ;
bgp_store_error ( p , conn , BE_BGP_RX , ( code < < 16 ) | subcode ) ;
2009-03-05 10:52:47 +00:00
2008-12-24 16:24:41 +00:00
# ifndef IPV6
2009-03-13 11:49:44 +00:00
if ( ( code = = 2 ) & & ( ( subcode = = 4 ) | | ( subcode = = 7 ) )
2008-12-24 16:24:41 +00:00
/* Error related to capability:
* 4 - Peer does not support capabilities at all .
* 7 - Peer request some capability . Strange unless it is IPv6 only peer .
*/
2009-03-13 11:49:44 +00:00
& & ( p - > cf - > capabilities = = 2 )
/* Capabilities are not explicitly enabled or disabled, therefore heuristic is used */
& & ( conn - > start_state = = BSS_CONNECT )
/* Failed connection attempt have used capabilities */
& & ( p - > cf - > remote_as < = 0xFFFF ) )
/* Not possible with disabled capabilities */
{
/* We try connect without capabilities */
log ( L_WARN " %s: Capability related error received, retry with capabilities disabled " , p - > p . name ) ;
2009-06-06 22:38:38 +00:00
p - > start_state = BSS_CONNECT_NOCAP ;
err = 0 ;
2008-12-24 16:24:41 +00:00
}
# endif
2008-12-19 00:34:39 +00:00
bgp_conn_enter_close_state ( conn ) ;
2000-03-30 10:44:20 +00:00
bgp_schedule_packet ( conn , PKT_SCHEDULE_CLOSE ) ;
2009-06-06 22:38:38 +00:00
if ( err )
{
bgp_update_startup_delay ( p ) ;
2017-09-19 17:55:37 +00:00
bgp_stop ( p , 0 , NULL , 0 ) ;
2009-06-06 22:38:38 +00:00
}
2018-01-16 18:17:04 +00:00
else
{
uint subcode_bit = 1 < < ( ( subcode < = 8 ) ? subcode : 0 ) ;
if ( p - > cf - > disable_after_cease & subcode_bit )
{
log ( L_INFO " %s: Disabled after Cease notification " , p - > p . name ) ;
p - > startup_delay = 0 ;
p - > p . disabled = 1 ;
}
}
2000-03-30 10:44:20 +00:00
}
static void
2004-06-05 09:27:17 +00:00
bgp_rx_keepalive ( struct bgp_conn * conn )
2000-03-30 10:44:20 +00:00
{
2000-05-02 16:07:41 +00:00
struct bgp_proto * p = conn - > bgp ;
BGP_TRACE ( D_PACKETS , " Got KEEPALIVE " ) ;
2000-03-30 10:44:20 +00:00
bgp_start_timer ( conn - > hold_timer , conn - > hold_time ) ;
switch ( conn - > state )
{
case BS_OPENCONFIRM :
2008-12-19 00:34:39 +00:00
bgp_conn_enter_established_state ( conn ) ;
2000-03-30 10:44:20 +00:00
break ;
case BS_ESTABLISHED :
break ;
default :
2013-07-25 11:55:24 +00:00
bgp_error ( conn , 5 , fsm_err_subcode [ conn - > state ] , NULL , 0 ) ;
2000-03-30 10:44:20 +00:00
}
}
2009-11-26 19:47:59 +00:00
static void
2016-10-14 13:37:04 +00:00
bgp_rx_route_refresh ( struct bgp_conn * conn , byte * pkt , uint len )
2009-11-26 19:47:59 +00:00
{
struct bgp_proto * p = conn - > bgp ;
if ( conn - > state ! = BS_ESTABLISHED )
2013-07-25 11:55:24 +00:00
{ bgp_error ( conn , 5 , fsm_err_subcode [ conn - > state ] , NULL , 0 ) ; return ; }
2009-11-26 19:47:59 +00:00
if ( ! p - > cf - > enable_refresh )
{ bgp_error ( conn , 1 , 3 , pkt + 18 , 1 ) ; return ; }
2015-03-29 16:27:13 +00:00
if ( len < ( BGP_HEADER_LENGTH + 4 ) )
2009-11-26 19:47:59 +00:00
{ bgp_error ( conn , 1 , 2 , pkt + 16 , 2 ) ; return ; }
2015-03-29 16:27:13 +00:00
if ( len > ( BGP_HEADER_LENGTH + 4 ) )
{ bgp_error ( conn , 7 , 1 , pkt , MIN ( len , 2048 ) ) ; return ; }
2009-11-26 19:47:59 +00:00
/* FIXME - we ignore AFI/SAFI values, as we support
just one value and even an error code for an invalid
request is not defined */
2015-03-29 16:27:13 +00:00
/* RFC 7313 redefined reserved field as RR message subtype */
uint subtype = conn - > peer_enhanced_refresh_support ? pkt [ 21 ] : BGP_RR_REQUEST ;
switch ( subtype )
{
case BGP_RR_REQUEST :
BGP_TRACE ( D_PACKETS , " Got ROUTE-REFRESH " ) ;
proto_request_feeding ( & p - > p ) ;
break ;
case BGP_RR_BEGIN :
BGP_TRACE ( D_PACKETS , " Got BEGIN-OF-RR " ) ;
bgp_refresh_begin ( p ) ;
break ;
case BGP_RR_END :
BGP_TRACE ( D_PACKETS , " Got END-OF-RR " ) ;
bgp_refresh_end ( p ) ;
break ;
default :
log ( L_WARN " %s: Got ROUTE-REFRESH message with unknown subtype %u, ignoring " ,
p - > p . name , subtype ) ;
break ;
}
2009-11-26 19:47:59 +00:00
}
2000-06-04 17:06:18 +00:00
/**
* bgp_rx_packet - handle a received packet
* @ conn : BGP connection
* @ pkt : start of the packet
* @ len : packet size
*
* bgp_rx_packet ( ) takes a newly received packet and calls the corresponding
* packet handler according to the packet type .
*/
2000-03-30 10:44:20 +00:00
static void
bgp_rx_packet ( struct bgp_conn * conn , byte * pkt , unsigned len )
{
2010-01-03 11:17:52 +00:00
byte type = pkt [ 18 ] ;
DBG ( " BGP: Got packet %02x (%d bytes) \n " , type , len ) ;
if ( conn - > bgp - > p . mrtdump & MD_MESSAGES )
2018-09-18 15:29:00 +00:00
bgp_dump_message ( conn , pkt , len ) ;
2010-01-03 11:17:52 +00:00
switch ( type )
2000-03-30 10:44:20 +00:00
{
case PKT_OPEN : return bgp_rx_open ( conn , pkt , len ) ;
case PKT_UPDATE : return bgp_rx_update ( conn , pkt , len ) ;
case PKT_NOTIFICATION : return bgp_rx_notification ( conn , pkt , len ) ;
2004-06-05 09:27:17 +00:00
case PKT_KEEPALIVE : return bgp_rx_keepalive ( conn ) ;
2009-11-26 19:47:59 +00:00
case PKT_ROUTE_REFRESH : return bgp_rx_route_refresh ( conn , pkt , len ) ;
2000-04-25 21:13:25 +00:00
default : bgp_error ( conn , 1 , 3 , pkt + 18 , 1 ) ;
2000-03-30 10:44:20 +00:00
}
2000-03-21 15:53:50 +00:00
}
2000-06-04 17:06:18 +00:00
/**
* bgp_rx - handle received data
* @ sk : socket
* @ size : amount of data received
*
* bgp_rx ( ) is called by the socket layer whenever new data arrive from
* the underlying TCP connection . It assembles the data fragments to packets ,
* checks their headers and framing and passes complete packets to
* bgp_rx_packet ( ) .
*/
2000-03-21 15:53:50 +00:00
int
2016-10-14 13:37:04 +00:00
bgp_rx ( sock * sk , uint size )
2000-03-21 15:53:50 +00:00
{
struct bgp_conn * conn = sk - > data ;
2015-07-18 11:38:21 +00:00
struct bgp_proto * p = conn - > bgp ;
2000-03-21 15:53:50 +00:00
byte * pkt_start = sk - > rbuf ;
byte * end = pkt_start + size ;
2000-03-30 10:44:20 +00:00
unsigned i , len ;
2000-03-21 15:53:50 +00:00
DBG ( " BGP: RX hook: Got %d bytes \n " , size ) ;
while ( end > = pkt_start + BGP_HEADER_LENGTH )
{
2008-12-19 00:34:39 +00:00
if ( ( conn - > state = = BS_CLOSE ) | | ( conn - > sk ! = sk ) )
return 0 ;
2000-03-30 10:44:20 +00:00
for ( i = 0 ; i < 16 ; i + + )
if ( pkt_start [ i ] ! = 0xff )
{
2000-04-25 21:13:25 +00:00
bgp_error ( conn , 1 , 1 , NULL , 0 ) ;
2000-03-30 10:44:20 +00:00
break ;
}
len = get_u16 ( pkt_start + 16 ) ;
2015-07-18 11:38:21 +00:00
if ( len < BGP_HEADER_LENGTH | | len > bgp_max_packet_length ( p ) )
2000-03-30 10:44:20 +00:00
{
2000-04-25 21:13:25 +00:00
bgp_error ( conn , 1 , 2 , pkt_start + 16 , 2 ) ;
2000-03-30 10:44:20 +00:00
break ;
}
2000-04-19 13:54:35 +00:00
if ( end < pkt_start + len )
break ;
bgp_rx_packet ( conn , pkt_start , len ) ;
pkt_start + = len ;
2000-03-21 15:53:50 +00:00
}
if ( pkt_start ! = sk - > rbuf )
{
memmove ( sk - > rbuf , pkt_start , end - pkt_start ) ;
sk - > rpos = sk - > rbuf + ( end - pkt_start ) ;
}
return 0 ;
}