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"
39 #include "bgpd/bgpd.h"
40 #include "bgpd/bgp_open.h"
41 #include "bgpd/bgp_fsm.h"
42 #include "bgpd/bgp_attr.h"
43 #include "bgpd/bgp_debug.h"
44 #include "bgpd/bgp_network.h"
46 extern struct zebra_privs_t bgpd_privs
;
48 static char *bgp_get_bound_name(struct peer
*peer
);
50 /* BGP listening socket. */
54 struct thread
*thread
;
59 * Set MD5 key for the socket, for the given IPv4 peer address.
60 * If the password is NULL or zero-length, the option will be disabled.
62 static int bgp_md5_set_socket(int socket
, union sockunion
*su
,
67 #if HAVE_DECL_TCP_MD5SIG
69 #endif /* HAVE_TCP_MD5SIG */
73 #if HAVE_DECL_TCP_MD5SIG
74 /* Ensure there is no extraneous port information. */
75 memcpy(&su2
, su
, sizeof(union sockunion
));
76 if (su2
.sa
.sa_family
== AF_INET
)
79 su2
.sin6
.sin6_port
= 0;
80 ret
= sockopt_tcp_signature(socket
, &su2
, password
);
82 #endif /* HAVE_TCP_MD5SIG */
85 zlog_warn("can't set TCP_MD5SIG option on socket %d: %s",
86 socket
, safe_strerror(en
));
91 /* Helper for bgp_connect */
92 static int bgp_md5_set_connect(int socket
, union sockunion
*su
,
97 #if HAVE_DECL_TCP_MD5SIG
98 if (bgpd_privs
.change(ZPRIVS_RAISE
)) {
99 zlog_err("%s: could not raise privs", __func__
);
103 ret
= bgp_md5_set_socket(socket
, su
, password
);
105 if (bgpd_privs
.change(ZPRIVS_LOWER
))
106 zlog_err("%s: could not lower privs", __func__
);
107 #endif /* HAVE_TCP_MD5SIG */
112 static int bgp_md5_set_password(struct peer
*peer
, const char *password
)
114 struct listnode
*node
;
116 struct bgp_listener
*listener
;
118 if (bgpd_privs
.change(ZPRIVS_RAISE
)) {
119 zlog_err("%s: could not raise privs", __func__
);
123 /* Set or unset the password on the listen socket(s). Outbound
125 * are taken care of in bgp_connect() below.
127 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, listener
))
128 if (listener
->su
.sa
.sa_family
== peer
->su
.sa
.sa_family
) {
129 ret
= bgp_md5_set_socket(listener
->fd
, &peer
->su
,
134 if (bgpd_privs
.change(ZPRIVS_LOWER
))
135 zlog_err("%s: could not lower privs", __func__
);
140 int bgp_md5_set(struct peer
*peer
)
142 /* Set the password from listen socket. */
143 return bgp_md5_set_password(peer
, peer
->password
);
146 int bgp_md5_unset(struct peer
*peer
)
148 /* Unset the password from listen socket. */
149 return bgp_md5_set_password(peer
, NULL
);
152 int bgp_set_socket_ttl(struct peer
*peer
, int bgp_sock
)
154 char buf
[INET_ADDRSTRLEN
];
157 /* In case of peer is EBGP, we should set TTL for this connection. */
158 if (!peer
->gtsm_hops
&& (peer_sort(peer
) == BGP_PEER_EBGP
)) {
159 ret
= sockopt_ttl(peer
->su
.sa
.sa_family
, bgp_sock
, peer
->ttl
);
162 "%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
163 __func__
, inet_ntop(AF_INET
, &peer
->remote_id
,
168 } else if (peer
->gtsm_hops
) {
169 /* On Linux, setting minttl without setting ttl seems to mess
171 outgoing ttl. Therefore setting both.
173 ret
= sockopt_ttl(peer
->su
.sa
.sa_family
, bgp_sock
, MAXTTL
);
176 "%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
177 __func__
, inet_ntop(AF_INET
, &peer
->remote_id
,
182 ret
= sockopt_minttl(peer
->su
.sa
.sa_family
, bgp_sock
,
183 MAXTTL
+ 1 - peer
->gtsm_hops
);
186 "%s: Can't set MinTTL on peer (rtrid %s) socket, err = %d",
187 __func__
, inet_ntop(AF_INET
, &peer
->remote_id
,
198 * Obtain the BGP instance that the incoming connection should be processed
199 * against. This is important because more than one VRF could be using the
200 * same IP address space. The instance is got by obtaining the device to
201 * which the incoming connection is bound to. This could either be a VRF
202 * or it could be an interface, which in turn determines the VRF.
204 static int bgp_get_instance_for_inc_conn(int sock
, struct bgp
**bgp_inst
)
206 #ifndef SO_BINDTODEVICE
207 /* only Linux has SO_BINDTODEVICE, but we're in Linux-specific code here
208 * anyway since the assumption is that the interface name returned by
209 * getsockopt() is useful in identifying the VRF, particularly with
211 * VRF l3master device. The whole mechanism is specific to Linux, so...
212 * when other platforms add VRF support, this will need handling here as
213 * well. (or, some restructuring) */
214 *bgp_inst
= bgp_get_default();
218 char name
[VRF_NAMSIZ
+ 1];
219 socklen_t name_len
= VRF_NAMSIZ
;
222 struct listnode
*node
, *nnode
;
226 rc
= getsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, &name_len
);
228 #if defined(HAVE_CUMULUS)
229 zlog_err("[Error] BGP SO_BINDTODEVICE get failed (%s), sock %d",
230 safe_strerror(errno
), sock
);
236 *bgp_inst
= bgp_get_default();
237 return 0; /* default instance. */
240 /* First try match to instance; if that fails, check for interfaces. */
241 bgp
= bgp_lookup_by_name(name
);
243 if (!bgp
->vrf_id
) // unexpected
249 /* TODO - This will be optimized once interfaces move into the NS */
250 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
251 struct interface
*ifp
;
253 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
256 ifp
= if_lookup_by_name(name
, bgp
->vrf_id
);
263 /* We didn't match to either an instance or an interface. */
268 /* Accept bgp connection. */
269 static int bgp_accept(struct thread
*thread
)
274 struct bgp_listener
*listener
= THREAD_ARG(thread
);
277 char buf
[SU_ADDRSTRLEN
];
278 struct bgp
*bgp
= NULL
;
282 /* Register accept thread. */
283 accept_sock
= THREAD_FD(thread
);
284 if (accept_sock
< 0) {
285 zlog_err("accept_sock is nevative value %d", accept_sock
);
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 zlog_err("[Error] BGP socket accept failed (%s)",
297 safe_strerror(errno
));
300 set_nonblocking(bgp_sock
);
302 /* Obtain BGP instance this connection is meant for.
303 * - if it is a VRF netns sock, then BGP is in listener structure
304 * - otherwise, the bgp instance need to be demultiplexed
308 else if (bgp_get_instance_for_inc_conn(bgp_sock
, &bgp
)) {
309 if (bgp_debug_neighbor_events(NULL
))
311 "[Event] Could not get instance for incoming conn from %s",
312 inet_sutop(&su
, buf
));
317 /* Set socket send buffer size */
318 setsockopt_so_sendbuf(bgp_sock
, BGP_SOCKET_SNDBUF_SIZE
);
320 /* Check remote IP address */
321 peer1
= peer_lookup(bgp
, &su
);
324 peer1
= peer_lookup_dynamic_neighbor(bgp
, &su
);
326 /* Dynamic neighbor has been created, let it proceed */
327 peer1
->fd
= bgp_sock
;
328 bgp_fsm_change_status(peer1
, Active
);
330 peer1
->t_start
); /* created in peer_create() */
332 if (peer_active(peer1
))
333 BGP_EVENT_ADD(peer1
, TCP_connection_open
);
340 if (bgp_debug_neighbor_events(NULL
)) {
342 "[Event] %s connection rejected - not configured"
343 " and not valid for dynamic",
344 inet_sutop(&su
, buf
));
350 if (CHECK_FLAG(peer1
->flags
, PEER_FLAG_SHUTDOWN
)) {
351 if (bgp_debug_neighbor_events(peer1
))
353 "[Event] connection from %s rejected due to admin shutdown",
354 inet_sutop(&su
, buf
));
360 * Do not accept incoming connections in Clearing state. This can result
361 * in incorect state transitions - e.g., the connection goes back to
362 * Established and then the Clearing_Completed event is generated. Also,
363 * block incoming connection in Deleted state.
365 if (peer1
->status
== Clearing
|| peer1
->status
== Deleted
) {
366 if (bgp_debug_neighbor_events(peer1
))
368 "[Event] Closing incoming conn for %s (%p) state %d",
369 peer1
->host
, peer1
, peer1
->status
);
374 /* Check that at least one AF is activated for the peer. */
375 if (!peer_active(peer1
)) {
376 if (bgp_debug_neighbor_events(peer1
))
378 "%s - incoming conn rejected - no AF activated for peer",
384 if (bgp_debug_neighbor_events(peer1
))
385 zlog_debug("[Event] BGP connection from host %s fd %d",
386 inet_sutop(&su
, buf
), bgp_sock
);
388 if (peer1
->doppelganger
) {
389 /* We have an existing connection. Kill the existing one and run
392 if (bgp_debug_neighbor_events(peer1
))
394 "[Event] New active connection from peer %s, Killing"
395 " previous active connection",
397 peer_delete(peer1
->doppelganger
);
400 if (bgp_set_socket_ttl(peer1
, bgp_sock
) < 0)
401 if (bgp_debug_neighbor_events(peer1
))
403 "[Event] Unable to set min/max TTL on peer %s, Continuing",
406 peer
= peer_create(&su
, peer1
->conf_if
, peer1
->bgp
, peer1
->local_as
,
407 peer1
->as
, peer1
->as_type
, 0, 0, NULL
);
409 hash_release(peer
->bgp
->peerhash
, peer
);
410 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
412 peer_xfer_config(peer
, peer1
);
413 UNSET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
415 peer
->doppelganger
= peer1
;
416 peer1
->doppelganger
= peer
;
418 vrf_bind(peer
->bgp
->vrf_id
, bgp_sock
, bgp_get_bound_name(peer
));
419 bgp_fsm_change_status(peer
, Active
);
420 BGP_TIMER_OFF(peer
->t_start
); /* created in peer_create() */
422 SET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
424 /* Make dummy peer until read Open packet. */
425 if (peer1
->status
== Established
426 && CHECK_FLAG(peer1
->sflags
, PEER_STATUS_NSF_MODE
)) {
427 /* If we have an existing established connection with graceful
429 * capability announced with one or more address families, then
431 * existing established connection and move state to connect.
433 peer1
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
434 SET_FLAG(peer1
->sflags
, PEER_STATUS_NSF_WAIT
);
435 bgp_event_update(peer1
, TCP_connection_closed
);
438 if (peer_active(peer
)) {
439 BGP_EVENT_ADD(peer
, TCP_connection_open
);
445 /* BGP socket bind. */
446 static char *bgp_get_bound_name(struct peer
*peer
)
453 if ((peer
->bgp
->vrf_id
== VRF_DEFAULT
) && !peer
->ifname
457 if (peer
->su
.sa
.sa_family
!= AF_INET
458 && peer
->su
.sa
.sa_family
!= AF_INET6
)
459 return NULL
; // unexpected
461 /* For IPv6 peering, interface (unnumbered or link-local with interface)
462 * takes precedence over VRF. For IPv4 peering, explicit interface or
463 * VRF are the situations to bind.
465 if (peer
->su
.sa
.sa_family
== AF_INET6
)
466 name
= (peer
->conf_if
? peer
->conf_if
467 : (peer
->ifname
? peer
->ifname
470 name
= peer
->ifname
? peer
->ifname
: peer
->bgp
->name
;
475 static int bgp_update_address(struct interface
*ifp
, const union sockunion
*dst
,
476 union sockunion
*addr
)
478 struct prefix
*p
, *sel
, d
;
479 struct connected
*connected
;
480 struct listnode
*node
;
483 sockunion2hostprefix(dst
, &d
);
487 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
488 p
= connected
->address
;
489 if (p
->family
!= d
.family
)
491 if (prefix_common_bits(p
, &d
) > common
) {
493 common
= prefix_common_bits(sel
, &d
);
500 prefix2sockunion(sel
, addr
);
504 /* Update source selection. */
505 static int bgp_update_source(struct peer
*peer
)
507 struct interface
*ifp
;
508 union sockunion addr
;
511 sockunion_init(&addr
);
513 /* Source is specified with interface name. */
514 if (peer
->update_if
) {
515 ifp
= if_lookup_by_name(peer
->update_if
, peer
->bgp
->vrf_id
);
519 if (bgp_update_address(ifp
, &peer
->su
, &addr
))
522 ret
= sockunion_bind(peer
->fd
, &addr
, 0, &addr
);
525 /* Source is specified with IP address. */
526 if (peer
->update_source
)
527 ret
= sockunion_bind(peer
->fd
, peer
->update_source
, 0,
528 peer
->update_source
);
533 #define DATAPLANE_MARK 254 /* main table ID */
535 /* BGP try to connect to the peer. */
536 int bgp_connect(struct peer
*peer
)
538 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
539 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
540 ifindex_t ifindex
= 0;
542 if (peer
->conf_if
&& BGP_PEER_SU_UNSPEC(peer
)) {
543 zlog_debug("Peer address not learnt: Returning from connect");
546 if (bgpd_privs
.change(ZPRIVS_RAISE
))
547 zlog_err("Can't raise privileges");
548 /* Make socket for the peer. */
549 peer
->fd
= vrf_sockunion_socket(&peer
->su
, peer
->bgp
->vrf_id
,
550 bgp_get_bound_name(peer
));
551 if (bgpd_privs
.change(ZPRIVS_LOWER
))
552 zlog_err("Can't lower privileges");
556 set_nonblocking(peer
->fd
);
558 /* Set socket send buffer size */
559 setsockopt_so_sendbuf(peer
->fd
, BGP_SOCKET_SNDBUF_SIZE
);
561 if (bgp_set_socket_ttl(peer
, peer
->fd
) < 0)
564 sockopt_reuseaddr(peer
->fd
);
565 sockopt_reuseport(peer
->fd
);
566 if (sockopt_mark_default(peer
->fd
, DATAPLANE_MARK
, &bgpd_privs
) < 0)
567 zlog_warn("Unable to set mark on FD for peer %s, err=%s",
568 peer
->host
, safe_strerror(errno
));
570 #ifdef IPTOS_PREC_INTERNETCONTROL
571 if (bgpd_privs
.change(ZPRIVS_RAISE
))
572 zlog_err("%s: could not raise privs", __func__
);
573 if (sockunion_family(&peer
->su
) == AF_INET
)
574 setsockopt_ipv4_tos(peer
->fd
, IPTOS_PREC_INTERNETCONTROL
);
575 else if (sockunion_family(&peer
->su
) == AF_INET6
)
576 setsockopt_ipv6_tclass(peer
->fd
, IPTOS_PREC_INTERNETCONTROL
);
577 if (bgpd_privs
.change(ZPRIVS_LOWER
))
578 zlog_err("%s: could not lower privs", __func__
);
582 bgp_md5_set_connect(peer
->fd
, &peer
->su
, peer
->password
);
584 /* Update source bind. */
585 if (bgp_update_source(peer
) < 0) {
586 return connect_error
;
589 if (peer
->conf_if
|| peer
->ifname
)
590 ifindex
= ifname2ifindex(peer
->conf_if
? peer
->conf_if
594 if (bgp_debug_neighbor_events(peer
))
595 zlog_debug("%s [Event] Connect start to %s fd %d", peer
->host
,
596 peer
->host
, peer
->fd
);
598 /* Connect to the remote peer. */
599 return sockunion_connect(peer
->fd
, &peer
->su
, htons(peer
->port
),
603 /* After TCP connection is established. Get local address and port. */
604 int bgp_getsockname(struct peer
*peer
)
606 if (peer
->su_local
) {
607 sockunion_free(peer
->su_local
);
608 peer
->su_local
= NULL
;
611 if (peer
->su_remote
) {
612 sockunion_free(peer
->su_remote
);
613 peer
->su_remote
= NULL
;
616 peer
->su_local
= sockunion_getsockname(peer
->fd
);
619 peer
->su_remote
= sockunion_getpeername(peer
->fd
);
620 if (!peer
->su_remote
)
623 if (bgp_nexthop_set(peer
->su_local
, peer
->su_remote
, &peer
->nexthop
,
625 #if defined(HAVE_CUMULUS)
627 "%s: nexthop_set failed, resetting connection - intf %p",
628 peer
->host
, peer
->nexthop
.ifp
);
636 static int bgp_listener(int sock
, struct sockaddr
*sa
, socklen_t salen
,
639 struct bgp_listener
*listener
;
642 sockopt_reuseaddr(sock
);
643 sockopt_reuseport(sock
);
645 if (bgpd_privs
.change(ZPRIVS_RAISE
))
646 zlog_err("%s: could not raise privs", __func__
);
648 #ifdef IPTOS_PREC_INTERNETCONTROL
649 if (sa
->sa_family
== AF_INET
)
650 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
651 else if (sa
->sa_family
== AF_INET6
)
652 setsockopt_ipv6_tclass(sock
, IPTOS_PREC_INTERNETCONTROL
);
655 sockopt_v6only(sa
->sa_family
, sock
);
657 ret
= bind(sock
, sa
, salen
);
659 if (bgpd_privs
.change(ZPRIVS_LOWER
))
660 zlog_err("%s: could not lower privs", __func__
);
663 zlog_err("bind: %s", safe_strerror(en
));
667 ret
= listen(sock
, SOMAXCONN
);
669 zlog_err("listen: %s", safe_strerror(errno
));
673 listener
= XCALLOC(MTYPE_BGP_LISTENER
, sizeof(*listener
));
676 /* this socket needs a change of ns. record bgp back pointer */
677 if (bgp
->vrf_id
!= VRF_DEFAULT
&& vrf_is_mapped_on_netns(bgp
->vrf_id
))
680 memcpy(&listener
->su
, sa
, salen
);
681 listener
->thread
= NULL
;
682 thread_add_read(bm
->master
, bgp_accept
, listener
, sock
,
684 listnode_add(bm
->listen_sockets
, listener
);
689 /* IPv6 supported version of BGP server socket setup. */
690 int bgp_socket(struct bgp
*bgp
, unsigned short port
, const char *address
)
692 struct addrinfo
*ainfo
;
693 struct addrinfo
*ainfo_save
;
694 static const struct addrinfo req
= {
695 .ai_family
= AF_UNSPEC
,
696 .ai_flags
= AI_PASSIVE
,
697 .ai_socktype
= SOCK_STREAM
,
700 char port_str
[BUFSIZ
];
702 snprintf(port_str
, sizeof(port_str
), "%d", port
);
703 port_str
[sizeof(port_str
) - 1] = '\0';
705 if (bgpd_privs
.change(ZPRIVS_RAISE
))
706 zlog_err("Can't raise privileges");
707 ret
= vrf_getaddrinfo(address
, port_str
, &req
, &ainfo_save
,
709 if (bgpd_privs
.change(ZPRIVS_LOWER
))
710 zlog_err("Can't lower privileges");
712 zlog_err("getaddrinfo: %s", gai_strerror(ret
));
717 for (ainfo
= ainfo_save
; ainfo
; ainfo
= ainfo
->ai_next
) {
720 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
723 if (bgpd_privs
.change(ZPRIVS_RAISE
))
724 zlog_err("Can't raise privileges");
725 sock
= vrf_socket(ainfo
->ai_family
, ainfo
->ai_socktype
,
726 ainfo
->ai_protocol
, bgp
->vrf_id
, NULL
);
727 if (bgpd_privs
.change(ZPRIVS_LOWER
))
728 zlog_err("Can't lower privileges");
730 zlog_err("socket: %s", 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
);
748 "%s: no usable addresses please check other programs usage of specified port %d",
750 zlog_err("%s: Program cannot continue", __func__
);
757 /* this function closes vrf socket
758 * this should be called only for vrf socket with netns backend
760 void bgp_close_vrf_socket(struct bgp
*bgp
)
762 struct listnode
*node
, *next
;
763 struct bgp_listener
*listener
;
768 if (bm
->listen_sockets
== NULL
)
771 for (ALL_LIST_ELEMENTS(bm
->listen_sockets
, node
, next
, listener
)) {
772 if (listener
->bgp
== bgp
) {
773 thread_cancel(listener
->thread
);
775 listnode_delete(bm
->listen_sockets
, listener
);
776 XFREE(MTYPE_BGP_LISTENER
, listener
);
781 /* this function closes main socket
785 struct listnode
*node
, *next
;
786 struct bgp_listener
*listener
;
788 if (bm
->listen_sockets
== NULL
)
791 for (ALL_LIST_ELEMENTS(bm
->listen_sockets
, node
, next
, listener
)) {
794 thread_cancel(listener
->thread
);
796 listnode_delete(bm
->listen_sockets
, listener
);
797 XFREE(MTYPE_BGP_LISTENER
, listener
);