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"
49 #include "bgpd/bgp_nht.h"
51 extern struct zebra_privs_t bgpd_privs
;
53 static char *bgp_get_bound_name(struct peer
*peer
);
55 /* BGP listening socket. */
59 struct thread
*thread
;
64 void bgp_dump_listener_info(struct vty
*vty
)
66 struct listnode
*node
;
67 struct bgp_listener
*listener
;
69 vty_out(vty
, "Name fd Address\n");
70 vty_out(vty
, "---------------------------\n");
71 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, listener
)) {
72 char buf
[SU_ADDRSTRLEN
];
74 vty_out(vty
, "%-16s %d %s\n",
75 listener
->name
? listener
->name
: VRF_DEFAULT_NAME
,
77 sockunion2str(&listener
->su
, buf
, sizeof(buf
)));
82 * Set MD5 key for the socket, for the given IPv4 peer address.
83 * If the password is NULL or zero-length, the option will be disabled.
85 static int bgp_md5_set_socket(int socket
, union sockunion
*su
,
86 uint16_t prefixlen
, const char *password
)
90 #if HAVE_DECL_TCP_MD5SIG
92 #endif /* HAVE_TCP_MD5SIG */
96 #if HAVE_DECL_TCP_MD5SIG
97 /* Ensure there is no extraneous port information. */
98 memcpy(&su2
, su
, sizeof(union sockunion
));
99 if (su2
.sa
.sa_family
== AF_INET
)
100 su2
.sin
.sin_port
= 0;
102 su2
.sin6
.sin6_port
= 0;
104 /* For addresses, use the non-extended signature functionality */
105 if ((su2
.sa
.sa_family
== AF_INET
&& prefixlen
== IPV4_MAX_BITLEN
)
106 || (su2
.sa
.sa_family
== AF_INET6
&& prefixlen
== IPV6_MAX_BITLEN
))
107 ret
= sockopt_tcp_signature(socket
, &su2
, password
);
109 ret
= sockopt_tcp_signature_ext(socket
, &su2
, prefixlen
,
112 #endif /* HAVE_TCP_MD5SIG */
115 char sabuf
[SU_ADDRSTRLEN
];
116 sockunion2str(su
, sabuf
, sizeof(sabuf
));
122 "Unable to set TCP MD5 option on socket for peer %s (sock=%d): This platform does not support MD5 auth for prefixes",
128 "Unable to set TCP MD5 option on socket for peer %s (sock=%d): %s",
129 sabuf
, socket
, safe_strerror(en
));
136 /* Helper for bgp_connect */
137 static int bgp_md5_set_connect(int socket
, union sockunion
*su
,
138 uint16_t prefixlen
, const char *password
)
142 #if HAVE_DECL_TCP_MD5SIG
143 frr_with_privs(&bgpd_privs
) {
144 ret
= bgp_md5_set_socket(socket
, su
, prefixlen
, password
);
146 #endif /* HAVE_TCP_MD5SIG */
151 static int bgp_md5_set_password(struct peer
*peer
, const char *password
)
153 struct listnode
*node
;
155 struct bgp_listener
*listener
;
158 * Set or unset the password on the listen socket(s). Outbound
159 * connections are taken care of in bgp_connect() below.
161 frr_with_privs(&bgpd_privs
) {
162 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, listener
))
163 if (listener
->su
.sa
.sa_family
==
164 peer
->su
.sa
.sa_family
) {
166 peer
->su
.sa
.sa_family
== AF_INET
171 * if we have stored a BGP vrf instance in the
172 * listener it must match the bgp instance in
173 * the peer otherwise the peer bgp instance
174 * must be the default vrf or a view instance
176 if (!listener
->bgp
) {
177 if (peer
->bgp
->vrf_id
!= VRF_DEFAULT
)
179 } else if (listener
->bgp
!= peer
->bgp
)
182 ret
= bgp_md5_set_socket(listener
->fd
,
183 &peer
->su
, prefixlen
,
191 int bgp_md5_set_prefix(struct bgp
*bgp
, struct prefix
*p
, const char *password
)
195 struct listnode
*node
;
196 struct bgp_listener
*listener
;
198 /* Set or unset the password on the listen socket(s). */
199 frr_with_privs(&bgpd_privs
) {
200 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, listener
))
201 if (listener
->su
.sa
.sa_family
== p
->family
202 && ((bgp
->vrf_id
== VRF_DEFAULT
)
203 || (listener
->bgp
== bgp
))) {
204 prefix2sockunion(p
, &su
);
205 ret
= bgp_md5_set_socket(listener
->fd
, &su
,
215 int bgp_md5_unset_prefix(struct bgp
*bgp
, struct prefix
*p
)
217 return bgp_md5_set_prefix(bgp
, p
, NULL
);
220 int bgp_md5_set(struct peer
*peer
)
222 /* Set the password from listen socket. */
223 return bgp_md5_set_password(peer
, peer
->password
);
226 int bgp_md5_unset(struct peer
*peer
)
228 /* Unset the password from listen socket. */
229 return bgp_md5_set_password(peer
, NULL
);
232 int bgp_set_socket_ttl(struct peer
*peer
, int bgp_sock
)
236 /* In case of peer is EBGP, we should set TTL for this connection. */
237 if (!peer
->gtsm_hops
&& (peer_sort_lookup(peer
) == BGP_PEER_EBGP
)) {
238 ret
= sockopt_ttl(peer
->su
.sa
.sa_family
, bgp_sock
, peer
->ttl
);
242 "%s: Can't set TxTTL on peer (rtrid %pI4) socket, err = %d",
243 __func__
, &peer
->remote_id
, errno
);
246 } else if (peer
->gtsm_hops
) {
247 /* On Linux, setting minttl without setting ttl seems to mess
249 outgoing ttl. Therefore setting both.
251 ret
= sockopt_ttl(peer
->su
.sa
.sa_family
, bgp_sock
, MAXTTL
);
255 "%s: Can't set TxTTL on peer (rtrid %pI4) socket, err = %d",
256 __func__
, &peer
->remote_id
, errno
);
259 ret
= sockopt_minttl(peer
->su
.sa
.sa_family
, bgp_sock
,
260 MAXTTL
+ 1 - peer
->gtsm_hops
);
264 "%s: Can't set MinTTL on peer (rtrid %pI4) socket, err = %d",
265 __func__
, &peer
->remote_id
, errno
);
274 * Obtain the BGP instance that the incoming connection should be processed
275 * against. This is important because more than one VRF could be using the
276 * same IP address space. The instance is got by obtaining the device to
277 * which the incoming connection is bound to. This could either be a VRF
278 * or it could be an interface, which in turn determines the VRF.
280 static int bgp_get_instance_for_inc_conn(int sock
, struct bgp
**bgp_inst
)
282 #ifndef SO_BINDTODEVICE
283 /* only Linux has SO_BINDTODEVICE, but we're in Linux-specific code here
284 * anyway since the assumption is that the interface name returned by
285 * getsockopt() is useful in identifying the VRF, particularly with
287 * VRF l3master device. The whole mechanism is specific to Linux, so...
288 * when other platforms add VRF support, this will need handling here as
289 * well. (or, some restructuring) */
290 *bgp_inst
= bgp_get_default();
294 char name
[VRF_NAMSIZ
+ 1];
295 socklen_t name_len
= VRF_NAMSIZ
;
298 struct listnode
*node
, *nnode
;
302 rc
= getsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, &name_len
);
304 #if defined(HAVE_CUMULUS)
305 flog_err(EC_LIB_SOCKET
,
306 "[Error] BGP SO_BINDTODEVICE get failed (%s), sock %d",
307 safe_strerror(errno
), sock
);
313 *bgp_inst
= bgp_get_default();
314 return 0; /* default instance. */
317 /* First try match to instance; if that fails, check for interfaces. */
318 bgp
= bgp_lookup_by_name(name
);
320 if (!bgp
->vrf_id
) // unexpected
326 /* TODO - This will be optimized once interfaces move into the NS */
327 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
328 struct interface
*ifp
;
330 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
333 ifp
= if_lookup_by_name(name
, bgp
->vrf_id
);
340 /* We didn't match to either an instance or an interface. */
345 static void bgp_socket_set_buffer_size(const int fd
)
347 if (getsockopt_so_sendbuf(fd
) < (int)bm
->socket_buffer
)
348 setsockopt_so_sendbuf(fd
, bm
->socket_buffer
);
349 if (getsockopt_so_recvbuf(fd
) < (int)bm
->socket_buffer
)
350 setsockopt_so_recvbuf(fd
, bm
->socket_buffer
);
353 /* Accept bgp connection. */
354 static int bgp_accept(struct thread
*thread
)
359 struct bgp_listener
*listener
= THREAD_ARG(thread
);
362 char buf
[SU_ADDRSTRLEN
];
363 struct bgp
*bgp
= NULL
;
367 bgp
= bgp_lookup_by_name(listener
->name
);
369 /* Register accept thread. */
370 accept_sock
= THREAD_FD(thread
);
371 if (accept_sock
< 0) {
372 flog_err_sys(EC_LIB_SOCKET
,
373 "[Error] BGP accept socket fd is negative: %d",
378 thread_add_read(bm
->master
, bgp_accept
, listener
, accept_sock
,
381 /* Accept client connection. */
382 bgp_sock
= sockunion_accept(accept_sock
, &su
);
383 int save_errno
= errno
;
385 if (save_errno
== EINVAL
) {
387 bgp
? vrf_lookup_by_id(bgp
->vrf_id
) : NULL
;
390 * It appears that sometimes, when VRFs are deleted on
391 * the system, it takes a little while for us to get
392 * notified about that. In the meantime we endlessly
393 * loop on accept(), because the socket, having been
394 * bound to a now-deleted VRF device, is in some weird
395 * state which causes accept() to fail.
397 * To avoid this, if we see accept() fail with EINVAL,
398 * we cancel ourselves and trust that when the VRF
399 * deletion notification comes in the event handler for
400 * that will take care of cleaning us up.
404 "[Error] accept() failed with error \"%s\" on BGP listener socket %d for BGP instance in VRF \"%s\"; refreshing socket",
405 safe_strerror(save_errno
), accept_sock
,
407 THREAD_OFF(listener
->thread
);
411 "[Error] BGP socket accept failed (%s); retrying",
412 safe_strerror(save_errno
));
416 set_nonblocking(bgp_sock
);
418 /* Obtain BGP instance this connection is meant for.
419 * - if it is a VRF netns sock, then BGP is in listener structure
420 * - otherwise, the bgp instance need to be demultiplexed
424 else if (bgp_get_instance_for_inc_conn(bgp_sock
, &bgp
)) {
425 if (bgp_debug_neighbor_events(NULL
))
427 "[Event] Could not get instance for incoming conn from %s",
428 inet_sutop(&su
, buf
));
433 bgp_socket_set_buffer_size(bgp_sock
);
435 /* Check remote IP address */
436 peer1
= peer_lookup(bgp
, &su
);
439 peer1
= peer_lookup_dynamic_neighbor(bgp
, &su
);
441 /* Dynamic neighbor has been created, let it proceed */
442 peer1
->fd
= bgp_sock
;
444 /* Set the user configured MSS to TCP socket */
445 if (CHECK_FLAG(peer1
->flags
, PEER_FLAG_TCP_MSS
))
446 sockopt_tcp_mss_set(bgp_sock
, peer1
->tcp_mss
);
448 bgp_fsm_change_status(peer1
, Active
);
450 peer1
->t_start
); /* created in peer_create() */
452 if (peer_active(peer1
)) {
453 if (CHECK_FLAG(peer1
->flags
,
454 PEER_FLAG_TIMER_DELAYOPEN
))
457 TCP_connection_open_w_delay
);
460 TCP_connection_open
);
468 if (bgp_debug_neighbor_events(NULL
)) {
470 "[Event] %s connection rejected(%s:%u:%s) - not configured and not valid for dynamic",
471 inet_sutop(&su
, buf
), bgp
->name_pretty
, bgp
->as
,
472 VRF_LOGNAME(vrf_lookup_by_id(bgp
->vrf_id
)));
478 if (CHECK_FLAG(peer1
->flags
, PEER_FLAG_SHUTDOWN
)
479 || CHECK_FLAG(peer1
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
480 if (bgp_debug_neighbor_events(peer1
))
482 "[Event] connection from %s rejected(%s:%u:%s) due to admin shutdown",
483 inet_sutop(&su
, buf
), bgp
->name_pretty
, bgp
->as
,
484 VRF_LOGNAME(vrf_lookup_by_id(bgp
->vrf_id
)));
490 * Do not accept incoming connections in Clearing state. This can result
491 * in incorect state transitions - e.g., the connection goes back to
492 * Established and then the Clearing_Completed event is generated. Also,
493 * block incoming connection in Deleted state.
495 if (peer1
->status
== Clearing
|| peer1
->status
== Deleted
) {
496 if (bgp_debug_neighbor_events(peer1
))
498 "[Event] Closing incoming conn for %s (%p) state %d",
499 peer1
->host
, peer1
, peer1
->status
);
504 /* Check that at least one AF is activated for the peer. */
505 if (!peer_active(peer1
)) {
506 if (bgp_debug_neighbor_events(peer1
))
508 "%s - incoming conn rejected - no AF activated for peer",
514 /* Do not try to reconnect if the peer reached maximum
515 * prefixes, restart timer is still running or the peer
518 if (BGP_PEER_START_SUPPRESSED(peer1
)) {
519 if (bgp_debug_neighbor_events(peer1
))
521 "[Event] Incoming BGP connection rejected from %s due to maximum-prefix or shutdown",
527 if (bgp_debug_neighbor_events(peer1
))
528 zlog_debug("[Event] BGP connection from host %s fd %d",
529 inet_sutop(&su
, buf
), bgp_sock
);
531 if (peer1
->doppelganger
) {
532 /* We have an existing connection. Kill the existing one and run
535 if (bgp_debug_neighbor_events(peer1
))
537 "[Event] New active connection from peer %s, Killing previous active connection",
539 peer_delete(peer1
->doppelganger
);
542 if (bgp_set_socket_ttl(peer1
, bgp_sock
) < 0)
543 if (bgp_debug_neighbor_events(peer1
))
545 "[Event] Unable to set min/max TTL on peer %s, Continuing",
548 peer
= peer_create(&su
, peer1
->conf_if
, peer1
->bgp
, peer1
->local_as
,
549 peer1
->as
, peer1
->as_type
, NULL
);
550 hash_release(peer
->bgp
->peerhash
, peer
);
551 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
553 peer_xfer_config(peer
, peer1
);
554 bgp_peer_gr_flags_update(peer
);
556 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
559 if (bgp_peer_gr_mode_get(peer
) == PEER_DISABLE
) {
561 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
563 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
568 UNSET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
570 peer
->doppelganger
= peer1
;
571 peer1
->doppelganger
= peer
;
573 frr_with_privs(&bgpd_privs
) {
574 vrf_bind(peer
->bgp
->vrf_id
, bgp_sock
, bgp_get_bound_name(peer
));
576 bgp_peer_reg_with_nht(peer
);
577 bgp_fsm_change_status(peer
, Active
);
578 BGP_TIMER_OFF(peer
->t_start
); /* created in peer_create() */
580 SET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
581 /* Make dummy peer until read Open packet. */
582 if (peer_established(peer1
)
583 && CHECK_FLAG(peer1
->sflags
, PEER_STATUS_NSF_MODE
)) {
584 /* If we have an existing established connection with graceful
586 * capability announced with one or more address families, then
588 * existing established connection and move state to connect.
590 peer1
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
592 if (CHECK_FLAG(peer1
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
593 || CHECK_FLAG(peer1
->flags
,
594 PEER_FLAG_GRACEFUL_RESTART_HELPER
))
595 SET_FLAG(peer1
->sflags
, PEER_STATUS_NSF_WAIT
);
597 bgp_event_update(peer1
, TCP_connection_closed
);
600 if (peer_active(peer
)) {
601 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
602 BGP_EVENT_ADD(peer
, TCP_connection_open_w_delay
);
604 BGP_EVENT_ADD(peer
, TCP_connection_open
);
608 * If we are doing nht for a peer that is v6 LL based
609 * massage the event system to make things happy
611 bgp_nht_interface_events(peer
);
616 /* BGP socket bind. */
617 static char *bgp_get_bound_name(struct peer
*peer
)
622 if ((peer
->bgp
->vrf_id
== VRF_DEFAULT
) && !peer
->ifname
626 if (peer
->su
.sa
.sa_family
!= AF_INET
627 && peer
->su
.sa
.sa_family
!= AF_INET6
)
628 return NULL
; // unexpected
630 /* For IPv6 peering, interface (unnumbered or link-local with interface)
631 * takes precedence over VRF. For IPv4 peering, explicit interface or
632 * VRF are the situations to bind.
634 if (peer
->su
.sa
.sa_family
== AF_INET6
&& peer
->conf_if
)
635 return peer
->conf_if
;
640 if (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
643 return peer
->bgp
->name
;
646 static int bgp_update_address(struct interface
*ifp
, const union sockunion
*dst
,
647 union sockunion
*addr
)
649 struct prefix
*p
, *sel
, d
;
650 struct connected
*connected
;
651 struct listnode
*node
;
654 if (!sockunion2hostprefix(dst
, &d
))
660 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
661 p
= connected
->address
;
662 if (p
->family
!= d
.family
)
664 if (prefix_common_bits(p
, &d
) > common
) {
666 common
= prefix_common_bits(sel
, &d
);
673 prefix2sockunion(sel
, addr
);
677 /* Update source selection. */
678 static int bgp_update_source(struct peer
*peer
)
680 struct interface
*ifp
;
681 union sockunion addr
;
684 sockunion_init(&addr
);
686 /* Source is specified with interface name. */
687 if (peer
->update_if
) {
688 ifp
= if_lookup_by_name(peer
->update_if
, peer
->bgp
->vrf_id
);
692 if (bgp_update_address(ifp
, &peer
->su
, &addr
))
695 ret
= sockunion_bind(peer
->fd
, &addr
, 0, &addr
);
698 /* Source is specified with IP address. */
699 if (peer
->update_source
)
700 ret
= sockunion_bind(peer
->fd
, peer
->update_source
, 0,
701 peer
->update_source
);
706 /* BGP try to connect to the peer. */
707 int bgp_connect(struct peer
*peer
)
709 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
710 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
711 ifindex_t ifindex
= 0;
713 if (peer
->conf_if
&& BGP_PEER_SU_UNSPEC(peer
)) {
714 if (bgp_debug_neighbor_events(peer
))
715 zlog_debug("Peer address not learnt: Returning from connect");
718 frr_with_privs(&bgpd_privs
) {
719 /* Make socket for the peer. */
720 peer
->fd
= vrf_sockunion_socket(&peer
->su
, peer
->bgp
->vrf_id
,
721 bgp_get_bound_name(peer
));
724 peer
->last_reset
= PEER_DOWN_SOCKET_ERROR
;
725 if (bgp_debug_neighbor_events(peer
))
726 zlog_debug("%s: Failure to create socket for connection to %s, error received: %s(%d)",
727 __func__
, peer
->host
, safe_strerror(errno
),
732 set_nonblocking(peer
->fd
);
734 /* Set the user configured MSS to TCP socket */
735 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
))
736 sockopt_tcp_mss_set(peer
->fd
, peer
->tcp_mss
);
738 bgp_socket_set_buffer_size(peer
->fd
);
740 if (bgp_set_socket_ttl(peer
, peer
->fd
) < 0) {
741 peer
->last_reset
= PEER_DOWN_SOCKET_ERROR
;
742 if (bgp_debug_neighbor_events(peer
))
743 zlog_debug("%s: Failure to set socket ttl for connection to %s, error received: %s(%d)",
744 __func__
, peer
->host
, safe_strerror(errno
),
749 sockopt_reuseaddr(peer
->fd
);
750 sockopt_reuseport(peer
->fd
);
752 #ifdef IPTOS_PREC_INTERNETCONTROL
753 frr_with_privs(&bgpd_privs
) {
754 if (sockunion_family(&peer
->su
) == AF_INET
)
755 setsockopt_ipv4_tos(peer
->fd
,
756 IPTOS_PREC_INTERNETCONTROL
);
757 else if (sockunion_family(&peer
->su
) == AF_INET6
)
758 setsockopt_ipv6_tclass(peer
->fd
,
759 IPTOS_PREC_INTERNETCONTROL
);
763 if (peer
->password
) {
764 uint16_t prefixlen
= peer
->su
.sa
.sa_family
== AF_INET
768 bgp_md5_set_connect(peer
->fd
, &peer
->su
, prefixlen
,
772 /* Update source bind. */
773 if (bgp_update_source(peer
) < 0) {
774 peer
->last_reset
= PEER_DOWN_SOCKET_ERROR
;
775 return connect_error
;
778 if (peer
->conf_if
|| peer
->ifname
)
779 ifindex
= ifname2ifindex(peer
->conf_if
? peer
->conf_if
783 if (bgp_debug_neighbor_events(peer
))
784 zlog_debug("%s [Event] Connect start to %s fd %d", peer
->host
,
785 peer
->host
, peer
->fd
);
787 /* Connect to the remote peer. */
788 return sockunion_connect(peer
->fd
, &peer
->su
, htons(peer
->port
),
792 /* After TCP connection is established. Get local address and port. */
793 int bgp_getsockname(struct peer
*peer
)
795 if (peer
->su_local
) {
796 sockunion_free(peer
->su_local
);
797 peer
->su_local
= NULL
;
800 if (peer
->su_remote
) {
801 sockunion_free(peer
->su_remote
);
802 peer
->su_remote
= NULL
;
805 peer
->su_local
= sockunion_getsockname(peer
->fd
);
808 peer
->su_remote
= sockunion_getpeername(peer
->fd
);
809 if (!peer
->su_remote
)
812 if (!bgp_zebra_nexthop_set(peer
->su_local
, peer
->su_remote
,
813 &peer
->nexthop
, peer
)) {
814 flog_err(EC_BGP_NH_UPD
,
815 "%s: nexthop_set failed, resetting connection - intf %p",
816 peer
->host
, peer
->nexthop
.ifp
);
823 static int bgp_listener(int sock
, struct sockaddr
*sa
, socklen_t salen
,
826 struct bgp_listener
*listener
;
829 sockopt_reuseaddr(sock
);
830 sockopt_reuseport(sock
);
832 frr_with_privs(&bgpd_privs
) {
834 #ifdef IPTOS_PREC_INTERNETCONTROL
835 if (sa
->sa_family
== AF_INET
)
836 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
837 else if (sa
->sa_family
== AF_INET6
)
838 setsockopt_ipv6_tclass(sock
,
839 IPTOS_PREC_INTERNETCONTROL
);
842 sockopt_v6only(sa
->sa_family
, sock
);
844 ret
= bind(sock
, sa
, salen
);
849 flog_err_sys(EC_LIB_SOCKET
, "bind: %s", safe_strerror(en
));
853 ret
= listen(sock
, SOMAXCONN
);
855 flog_err_sys(EC_LIB_SOCKET
, "listen: %s", safe_strerror(errno
));
859 listener
= XCALLOC(MTYPE_BGP_LISTENER
, sizeof(*listener
));
861 listener
->name
= XSTRDUP(MTYPE_BGP_LISTENER
, bgp
->name
);
863 /* this socket is in a vrf record bgp back pointer */
864 if (bgp
->vrf_id
!= VRF_DEFAULT
)
867 memcpy(&listener
->su
, sa
, salen
);
868 thread_add_read(bm
->master
, bgp_accept
, listener
, sock
,
870 listnode_add(bm
->listen_sockets
, listener
);
875 /* IPv6 supported version of BGP server socket setup. */
876 int bgp_socket(struct bgp
*bgp
, unsigned short port
, const char *address
)
878 struct addrinfo
*ainfo
;
879 struct addrinfo
*ainfo_save
;
880 static const struct addrinfo req
= {
881 .ai_family
= AF_UNSPEC
,
882 .ai_flags
= AI_PASSIVE
,
883 .ai_socktype
= SOCK_STREAM
,
886 char port_str
[BUFSIZ
];
888 snprintf(port_str
, sizeof(port_str
), "%d", port
);
889 port_str
[sizeof(port_str
) - 1] = '\0';
891 frr_with_privs(&bgpd_privs
) {
892 ret
= vrf_getaddrinfo(address
, port_str
, &req
, &ainfo_save
,
896 flog_err_sys(EC_LIB_SOCKET
, "getaddrinfo: %s",
900 if (bgp_option_check(BGP_OPT_NO_ZEBRA
) &&
901 bgp
->vrf_id
!= VRF_DEFAULT
) {
902 freeaddrinfo(ainfo_save
);
906 for (ainfo
= ainfo_save
; ainfo
; ainfo
= ainfo
->ai_next
) {
909 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
912 frr_with_privs(&bgpd_privs
) {
913 sock
= vrf_socket(ainfo
->ai_family
,
918 == BGP_INSTANCE_TYPE_VRF
919 ? bgp
->name
: NULL
));
922 flog_err_sys(EC_LIB_SOCKET
, "socket: %s",
923 safe_strerror(errno
));
927 /* if we intend to implement ttl-security, this socket needs
929 sockopt_ttl(ainfo
->ai_family
, sock
, MAXTTL
);
931 ret
= bgp_listener(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
,
938 freeaddrinfo(ainfo_save
);
939 if (count
== 0 && bgp
->inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
942 "%s: no usable addresses please check other programs usage of specified port %d",
944 flog_err_sys(EC_LIB_SOCKET
, "%s: Program cannot continue",
952 /* this function closes vrf socket
953 * this should be called only for vrf socket with netns backend
955 void bgp_close_vrf_socket(struct bgp
*bgp
)
957 struct listnode
*node
, *next
;
958 struct bgp_listener
*listener
;
963 if (bm
->listen_sockets
== NULL
)
966 for (ALL_LIST_ELEMENTS(bm
->listen_sockets
, node
, next
, listener
)) {
967 if (listener
->bgp
== bgp
) {
968 THREAD_OFF(listener
->thread
);
970 listnode_delete(bm
->listen_sockets
, listener
);
971 XFREE(MTYPE_BGP_LISTENER
, listener
->name
);
972 XFREE(MTYPE_BGP_LISTENER
, listener
);
977 /* this function closes main socket
981 struct listnode
*node
, *next
;
982 struct bgp_listener
*listener
;
984 if (bm
->listen_sockets
== NULL
)
987 for (ALL_LIST_ELEMENTS(bm
->listen_sockets
, node
, next
, listener
)) {
990 THREAD_OFF(listener
->thread
);
992 listnode_delete(bm
->listen_sockets
, listener
);
993 XFREE(MTYPE_BGP_LISTENER
, listener
->name
);
994 XFREE(MTYPE_BGP_LISTENER
, listener
);