1 /* BGP network related fucntions
2 * Copyright (C) 1999 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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "sockunion.h"
38 #include "lib_errors.h"
41 #include "bgpd/bgpd.h"
42 #include "bgpd/bgp_open.h"
43 #include "bgpd/bgp_fsm.h"
44 #include "bgpd/bgp_attr.h"
45 #include "bgpd/bgp_debug.h"
46 #include "bgpd/bgp_errors.h"
47 #include "bgpd/bgp_network.h"
48 #include "bgpd/bgp_zebra.h"
50 extern struct zebra_privs_t bgpd_privs
;
52 static char *bgp_get_bound_name(struct peer
*peer
);
54 /* BGP listening socket. */
58 struct thread
*thread
;
63 * Set MD5 key for the socket, for the given IPv4 peer address.
64 * If the password is NULL or zero-length, the option will be disabled.
66 static int bgp_md5_set_socket(int socket
, union sockunion
*su
,
71 #if HAVE_DECL_TCP_MD5SIG
73 #endif /* HAVE_TCP_MD5SIG */
77 #if HAVE_DECL_TCP_MD5SIG
78 /* Ensure there is no extraneous port information. */
79 memcpy(&su2
, su
, sizeof(union sockunion
));
80 if (su2
.sa
.sa_family
== AF_INET
)
83 su2
.sin6
.sin6_port
= 0;
84 ret
= sockopt_tcp_signature(socket
, &su2
, password
);
86 #endif /* HAVE_TCP_MD5SIG */
89 zlog_warn("can't set TCP_MD5SIG option on socket %d: %s",
90 socket
, safe_strerror(en
));
95 /* Helper for bgp_connect */
96 static int bgp_md5_set_connect(int socket
, union sockunion
*su
,
101 #if HAVE_DECL_TCP_MD5SIG
102 frr_elevate_privs(&bgpd_privs
) {
103 ret
= bgp_md5_set_socket(socket
, su
, password
);
105 #endif /* HAVE_TCP_MD5SIG */
110 static int bgp_md5_set_password(struct peer
*peer
, const char *password
)
112 struct listnode
*node
;
114 struct bgp_listener
*listener
;
116 frr_elevate_privs(&bgpd_privs
) {
117 /* Set or unset the password on the listen socket(s). Outbound
118 * connections are taken care of in bgp_connect() below.
120 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, listener
))
121 if (listener
->su
.sa
.sa_family
122 == peer
->su
.sa
.sa_family
) {
123 ret
= bgp_md5_set_socket(listener
->fd
,
124 &peer
->su
, password
);
131 int bgp_md5_set(struct peer
*peer
)
133 /* Set the password from listen socket. */
134 return bgp_md5_set_password(peer
, peer
->password
);
137 int bgp_md5_unset(struct peer
*peer
)
139 /* Unset the password from listen socket. */
140 return bgp_md5_set_password(peer
, NULL
);
143 int bgp_set_socket_ttl(struct peer
*peer
, int bgp_sock
)
145 char buf
[INET_ADDRSTRLEN
];
148 /* In case of peer is EBGP, we should set TTL for this connection. */
149 if (!peer
->gtsm_hops
&& (peer_sort(peer
) == BGP_PEER_EBGP
)) {
150 ret
= sockopt_ttl(peer
->su
.sa
.sa_family
, bgp_sock
, peer
->ttl
);
154 "%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
156 inet_ntop(AF_INET
, &peer
->remote_id
, buf
,
161 } else if (peer
->gtsm_hops
) {
162 /* On Linux, setting minttl without setting ttl seems to mess
164 outgoing ttl. Therefore setting both.
166 ret
= sockopt_ttl(peer
->su
.sa
.sa_family
, bgp_sock
, MAXTTL
);
170 "%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
172 inet_ntop(AF_INET
, &peer
->remote_id
, buf
,
177 ret
= sockopt_minttl(peer
->su
.sa
.sa_family
, bgp_sock
,
178 MAXTTL
+ 1 - peer
->gtsm_hops
);
182 "%s: Can't set MinTTL on peer (rtrid %s) socket, err = %d",
184 inet_ntop(AF_INET
, &peer
->remote_id
, buf
,
195 * Obtain the BGP instance that the incoming connection should be processed
196 * against. This is important because more than one VRF could be using the
197 * same IP address space. The instance is got by obtaining the device to
198 * which the incoming connection is bound to. This could either be a VRF
199 * or it could be an interface, which in turn determines the VRF.
201 static int bgp_get_instance_for_inc_conn(int sock
, struct bgp
**bgp_inst
)
203 #ifndef SO_BINDTODEVICE
204 /* only Linux has SO_BINDTODEVICE, but we're in Linux-specific code here
205 * anyway since the assumption is that the interface name returned by
206 * getsockopt() is useful in identifying the VRF, particularly with
208 * VRF l3master device. The whole mechanism is specific to Linux, so...
209 * when other platforms add VRF support, this will need handling here as
210 * well. (or, some restructuring) */
211 *bgp_inst
= bgp_get_default();
215 char name
[VRF_NAMSIZ
+ 1];
216 socklen_t name_len
= VRF_NAMSIZ
;
219 struct listnode
*node
, *nnode
;
223 rc
= getsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, &name_len
);
225 #if defined(HAVE_CUMULUS)
228 "[Error] BGP SO_BINDTODEVICE get failed (%s), sock %d",
229 safe_strerror(errno
), sock
);
235 *bgp_inst
= bgp_get_default();
236 return 0; /* default instance. */
239 /* First try match to instance; if that fails, check for interfaces. */
240 bgp
= bgp_lookup_by_name(name
);
242 if (!bgp
->vrf_id
) // unexpected
248 /* TODO - This will be optimized once interfaces move into the NS */
249 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
250 struct interface
*ifp
;
252 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
255 ifp
= if_lookup_by_name(name
, bgp
->vrf_id
);
262 /* We didn't match to either an instance or an interface. */
267 /* Accept bgp connection. */
268 static int bgp_accept(struct thread
*thread
)
273 struct bgp_listener
*listener
= THREAD_ARG(thread
);
276 char buf
[SU_ADDRSTRLEN
];
277 struct bgp
*bgp
= NULL
;
281 /* Register accept thread. */
282 accept_sock
= THREAD_FD(thread
);
283 if (accept_sock
< 0) {
284 flog_err_sys(LIB_ERR_SOCKET
, "accept_sock is nevative value %d",
288 listener
->thread
= NULL
;
290 thread_add_read(bm
->master
, bgp_accept
, listener
, accept_sock
,
293 /* Accept client connection. */
294 bgp_sock
= sockunion_accept(accept_sock
, &su
);
296 flog_err_sys(LIB_ERR_SOCKET
,
297 "[Error] BGP socket accept failed (%s)",
298 safe_strerror(errno
));
301 set_nonblocking(bgp_sock
);
303 /* Obtain BGP instance this connection is meant for.
304 * - if it is a VRF netns sock, then BGP is in listener structure
305 * - otherwise, the bgp instance need to be demultiplexed
309 else if (bgp_get_instance_for_inc_conn(bgp_sock
, &bgp
)) {
310 if (bgp_debug_neighbor_events(NULL
))
312 "[Event] Could not get instance for incoming conn from %s",
313 inet_sutop(&su
, buf
));
318 /* Set socket send buffer size */
319 setsockopt_so_sendbuf(bgp_sock
, BGP_SOCKET_SNDBUF_SIZE
);
321 /* Check remote IP address */
322 peer1
= peer_lookup(bgp
, &su
);
325 peer1
= peer_lookup_dynamic_neighbor(bgp
, &su
);
327 /* Dynamic neighbor has been created, let it proceed */
328 peer1
->fd
= bgp_sock
;
329 bgp_fsm_change_status(peer1
, Active
);
331 peer1
->t_start
); /* created in peer_create() */
333 if (peer_active(peer1
))
334 BGP_EVENT_ADD(peer1
, TCP_connection_open
);
341 if (bgp_debug_neighbor_events(NULL
)) {
343 "[Event] %s connection rejected - not configured"
344 " and not valid for dynamic",
345 inet_sutop(&su
, buf
));
351 if (CHECK_FLAG(peer1
->flags
, PEER_FLAG_SHUTDOWN
)) {
352 if (bgp_debug_neighbor_events(peer1
))
354 "[Event] connection from %s rejected due to admin shutdown",
355 inet_sutop(&su
, buf
));
361 * Do not accept incoming connections in Clearing state. This can result
362 * in incorect state transitions - e.g., the connection goes back to
363 * Established and then the Clearing_Completed event is generated. Also,
364 * block incoming connection in Deleted state.
366 if (peer1
->status
== Clearing
|| peer1
->status
== Deleted
) {
367 if (bgp_debug_neighbor_events(peer1
))
369 "[Event] Closing incoming conn for %s (%p) state %d",
370 peer1
->host
, peer1
, peer1
->status
);
375 /* Check that at least one AF is activated for the peer. */
376 if (!peer_active(peer1
)) {
377 if (bgp_debug_neighbor_events(peer1
))
379 "%s - incoming conn rejected - no AF activated for peer",
385 if (bgp_debug_neighbor_events(peer1
))
386 zlog_debug("[Event] BGP connection from host %s fd %d",
387 inet_sutop(&su
, buf
), bgp_sock
);
389 if (peer1
->doppelganger
) {
390 /* We have an existing connection. Kill the existing one and run
393 if (bgp_debug_neighbor_events(peer1
))
395 "[Event] New active connection from peer %s, Killing"
396 " previous active connection",
398 peer_delete(peer1
->doppelganger
);
401 if (bgp_set_socket_ttl(peer1
, bgp_sock
) < 0)
402 if (bgp_debug_neighbor_events(peer1
))
404 "[Event] Unable to set min/max TTL on peer %s, Continuing",
407 peer
= peer_create(&su
, peer1
->conf_if
, peer1
->bgp
, peer1
->local_as
,
408 peer1
->as
, peer1
->as_type
, 0, 0, NULL
);
410 hash_release(peer
->bgp
->peerhash
, peer
);
411 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
413 peer_xfer_config(peer
, peer1
);
414 UNSET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
416 peer
->doppelganger
= peer1
;
417 peer1
->doppelganger
= peer
;
419 vrf_bind(peer
->bgp
->vrf_id
, bgp_sock
, bgp_get_bound_name(peer
));
420 bgp_fsm_change_status(peer
, Active
);
421 BGP_TIMER_OFF(peer
->t_start
); /* created in peer_create() */
423 SET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
425 /* Make dummy peer until read Open packet. */
426 if (peer1
->status
== Established
427 && CHECK_FLAG(peer1
->sflags
, PEER_STATUS_NSF_MODE
)) {
428 /* If we have an existing established connection with graceful
430 * capability announced with one or more address families, then
432 * existing established connection and move state to connect.
434 peer1
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
435 SET_FLAG(peer1
->sflags
, PEER_STATUS_NSF_WAIT
);
436 bgp_event_update(peer1
, TCP_connection_closed
);
439 if (peer_active(peer
)) {
440 BGP_EVENT_ADD(peer
, TCP_connection_open
);
446 /* BGP socket bind. */
447 static char *bgp_get_bound_name(struct peer
*peer
)
454 if ((peer
->bgp
->vrf_id
== VRF_DEFAULT
) && !peer
->ifname
458 if (peer
->su
.sa
.sa_family
!= AF_INET
459 && peer
->su
.sa
.sa_family
!= AF_INET6
)
460 return NULL
; // unexpected
462 /* For IPv6 peering, interface (unnumbered or link-local with interface)
463 * takes precedence over VRF. For IPv4 peering, explicit interface or
464 * VRF are the situations to bind.
466 if (peer
->su
.sa
.sa_family
== AF_INET6
)
467 name
= (peer
->conf_if
? peer
->conf_if
468 : (peer
->ifname
? peer
->ifname
471 name
= peer
->ifname
? peer
->ifname
: peer
->bgp
->name
;
476 static int bgp_update_address(struct interface
*ifp
, const union sockunion
*dst
,
477 union sockunion
*addr
)
479 struct prefix
*p
, *sel
, d
;
480 struct connected
*connected
;
481 struct listnode
*node
;
484 sockunion2hostprefix(dst
, &d
);
488 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
489 p
= connected
->address
;
490 if (p
->family
!= d
.family
)
492 if (prefix_common_bits(p
, &d
) > common
) {
494 common
= prefix_common_bits(sel
, &d
);
501 prefix2sockunion(sel
, addr
);
505 /* Update source selection. */
506 static int bgp_update_source(struct peer
*peer
)
508 struct interface
*ifp
;
509 union sockunion addr
;
512 sockunion_init(&addr
);
514 /* Source is specified with interface name. */
515 if (peer
->update_if
) {
516 ifp
= if_lookup_by_name(peer
->update_if
, peer
->bgp
->vrf_id
);
520 if (bgp_update_address(ifp
, &peer
->su
, &addr
))
523 ret
= sockunion_bind(peer
->fd
, &addr
, 0, &addr
);
526 /* Source is specified with IP address. */
527 if (peer
->update_source
)
528 ret
= sockunion_bind(peer
->fd
, peer
->update_source
, 0,
529 peer
->update_source
);
534 #define DATAPLANE_MARK 254 /* main table ID */
536 /* BGP try to connect to the peer. */
537 int bgp_connect(struct peer
*peer
)
539 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
540 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
541 ifindex_t ifindex
= 0;
543 if (peer
->conf_if
&& BGP_PEER_SU_UNSPEC(peer
)) {
544 zlog_debug("Peer address not learnt: Returning from connect");
547 frr_elevate_privs(&bgpd_privs
) {
548 /* Make socket for the peer. */
549 peer
->fd
= vrf_sockunion_socket(&peer
->su
, peer
->bgp
->vrf_id
,
550 bgp_get_bound_name(peer
));
555 set_nonblocking(peer
->fd
);
557 /* Set socket send buffer size */
558 setsockopt_so_sendbuf(peer
->fd
, BGP_SOCKET_SNDBUF_SIZE
);
560 if (bgp_set_socket_ttl(peer
, peer
->fd
) < 0)
563 sockopt_reuseaddr(peer
->fd
);
564 sockopt_reuseport(peer
->fd
);
565 if (sockopt_mark_default(peer
->fd
, DATAPLANE_MARK
, &bgpd_privs
) < 0)
566 zlog_warn("Unable to set mark on FD for peer %s, err=%s",
567 peer
->host
, safe_strerror(errno
));
569 #ifdef IPTOS_PREC_INTERNETCONTROL
570 frr_elevate_privs(&bgpd_privs
) {
571 if (sockunion_family(&peer
->su
) == AF_INET
)
572 setsockopt_ipv4_tos(peer
->fd
,
573 IPTOS_PREC_INTERNETCONTROL
);
574 else if (sockunion_family(&peer
->su
) == AF_INET6
)
575 setsockopt_ipv6_tclass(peer
->fd
,
576 IPTOS_PREC_INTERNETCONTROL
);
581 bgp_md5_set_connect(peer
->fd
, &peer
->su
, peer
->password
);
583 /* Update source bind. */
584 if (bgp_update_source(peer
) < 0) {
585 return connect_error
;
588 if (peer
->conf_if
|| peer
->ifname
)
589 ifindex
= ifname2ifindex(peer
->conf_if
? peer
->conf_if
593 if (bgp_debug_neighbor_events(peer
))
594 zlog_debug("%s [Event] Connect start to %s fd %d", peer
->host
,
595 peer
->host
, peer
->fd
);
597 /* Connect to the remote peer. */
598 return sockunion_connect(peer
->fd
, &peer
->su
, htons(peer
->port
),
602 /* After TCP connection is established. Get local address and port. */
603 int bgp_getsockname(struct peer
*peer
)
605 if (peer
->su_local
) {
606 sockunion_free(peer
->su_local
);
607 peer
->su_local
= NULL
;
610 if (peer
->su_remote
) {
611 sockunion_free(peer
->su_remote
);
612 peer
->su_remote
= NULL
;
615 peer
->su_local
= sockunion_getsockname(peer
->fd
);
618 peer
->su_remote
= sockunion_getpeername(peer
->fd
);
619 if (!peer
->su_remote
)
622 if (!bgp_zebra_nexthop_set(peer
->su_local
, peer
->su_remote
,
623 &peer
->nexthop
, peer
)) {
624 flog_err(BGP_ERR_NH_UPD
,
625 "%s: nexthop_set failed, resetting connection - intf %p",
626 peer
->host
, peer
->nexthop
.ifp
);
633 static int bgp_listener(int sock
, struct sockaddr
*sa
, socklen_t salen
,
636 struct bgp_listener
*listener
;
639 sockopt_reuseaddr(sock
);
640 sockopt_reuseport(sock
);
642 frr_elevate_privs(&bgpd_privs
) {
644 #ifdef IPTOS_PREC_INTERNETCONTROL
645 if (sa
->sa_family
== AF_INET
)
646 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
647 else if (sa
->sa_family
== AF_INET6
)
648 setsockopt_ipv6_tclass(sock
,
649 IPTOS_PREC_INTERNETCONTROL
);
652 sockopt_v6only(sa
->sa_family
, sock
);
654 ret
= bind(sock
, sa
, salen
);
659 flog_err_sys(LIB_ERR_SOCKET
, "bind: %s", safe_strerror(en
));
663 ret
= listen(sock
, SOMAXCONN
);
665 flog_err_sys(LIB_ERR_SOCKET
, "listen: %s",
666 safe_strerror(errno
));
670 listener
= XCALLOC(MTYPE_BGP_LISTENER
, sizeof(*listener
));
673 /* this socket needs a change of ns. record bgp back pointer */
674 if (bgp
->vrf_id
!= VRF_DEFAULT
&& vrf_is_mapped_on_netns(
675 vrf_lookup_by_id(bgp
->vrf_id
)))
678 memcpy(&listener
->su
, sa
, salen
);
679 listener
->thread
= NULL
;
680 thread_add_read(bm
->master
, bgp_accept
, listener
, sock
,
682 listnode_add(bm
->listen_sockets
, listener
);
687 /* IPv6 supported version of BGP server socket setup. */
688 int bgp_socket(struct bgp
*bgp
, unsigned short port
, const char *address
)
690 struct addrinfo
*ainfo
;
691 struct addrinfo
*ainfo_save
;
692 static const struct addrinfo req
= {
693 .ai_family
= AF_UNSPEC
,
694 .ai_flags
= AI_PASSIVE
,
695 .ai_socktype
= SOCK_STREAM
,
698 char port_str
[BUFSIZ
];
700 snprintf(port_str
, sizeof(port_str
), "%d", port
);
701 port_str
[sizeof(port_str
) - 1] = '\0';
703 frr_elevate_privs(&bgpd_privs
) {
704 ret
= vrf_getaddrinfo(address
, port_str
, &req
, &ainfo_save
,
708 flog_err_sys(LIB_ERR_SOCKET
, "getaddrinfo: %s",
714 for (ainfo
= ainfo_save
; ainfo
; ainfo
= ainfo
->ai_next
) {
717 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
720 frr_elevate_privs(&bgpd_privs
) {
721 sock
= vrf_socket(ainfo
->ai_family
,
723 ainfo
->ai_protocol
, bgp
->vrf_id
,
725 == BGP_INSTANCE_TYPE_VRF
726 ? bgp
->name
: NULL
));
729 flog_err_sys(LIB_ERR_SOCKET
, "socket: %s",
730 safe_strerror(errno
));
734 /* if we intend to implement ttl-security, this socket needs
736 sockopt_ttl(ainfo
->ai_family
, sock
, MAXTTL
);
738 ret
= bgp_listener(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
,
745 freeaddrinfo(ainfo_save
);
746 if (count
== 0 && bgp
->inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
749 "%s: no usable addresses please check other programs usage of specified port %d",
751 flog_err_sys(LIB_ERR_SOCKET
, "%s: Program cannot continue",
759 /* this function closes vrf socket
760 * this should be called only for vrf socket with netns backend
762 void bgp_close_vrf_socket(struct bgp
*bgp
)
764 struct listnode
*node
, *next
;
765 struct bgp_listener
*listener
;
770 if (bm
->listen_sockets
== NULL
)
773 for (ALL_LIST_ELEMENTS(bm
->listen_sockets
, node
, next
, listener
)) {
774 if (listener
->bgp
== bgp
) {
775 thread_cancel(listener
->thread
);
777 listnode_delete(bm
->listen_sockets
, listener
);
778 XFREE(MTYPE_BGP_LISTENER
, listener
);
783 /* this function closes main socket
787 struct listnode
*node
, *next
;
788 struct bgp_listener
*listener
;
790 if (bm
->listen_sockets
== NULL
)
793 for (ALL_LIST_ELEMENTS(bm
->listen_sockets
, node
, next
, listener
)) {
796 thread_cancel(listener
->thread
);
798 listnode_delete(bm
->listen_sockets
, listener
);
799 XFREE(MTYPE_BGP_LISTENER
, listener
);