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 void bgp_dump_listener_info(struct vty
*vty
)
57 struct listnode
*node
;
58 struct bgp_listener
*listener
;
60 vty_out(vty
, "Name fd Address\n");
61 vty_out(vty
, "---------------------------\n");
62 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, listener
)) {
63 char buf
[SU_ADDRSTRLEN
];
65 vty_out(vty
, "%-16s %d %s\n",
66 listener
->name
? listener
->name
: VRF_DEFAULT_NAME
,
68 sockunion2str(&listener
->su
, buf
, sizeof(buf
)));
73 * Set MD5 key for the socket, for the given IPv4 peer address.
74 * If the password is NULL or zero-length, the option will be disabled.
76 static int bgp_md5_set_socket(int socket
, union sockunion
*su
,
77 uint16_t prefixlen
, const char *password
)
81 #if HAVE_DECL_TCP_MD5SIG
83 #endif /* HAVE_TCP_MD5SIG */
87 #if HAVE_DECL_TCP_MD5SIG
88 /* Ensure there is no extraneous port information. */
89 memcpy(&su2
, su
, sizeof(union sockunion
));
90 if (su2
.sa
.sa_family
== AF_INET
)
93 su2
.sin6
.sin6_port
= 0;
95 /* For addresses, use the non-extended signature functionality */
96 if ((su2
.sa
.sa_family
== AF_INET
&& prefixlen
== IPV4_MAX_BITLEN
)
97 || (su2
.sa
.sa_family
== AF_INET6
&& prefixlen
== IPV6_MAX_BITLEN
))
98 ret
= sockopt_tcp_signature(socket
, &su2
, password
);
100 ret
= sockopt_tcp_signature_ext(socket
, &su2
, prefixlen
,
103 #endif /* HAVE_TCP_MD5SIG */
106 char sabuf
[SU_ADDRSTRLEN
];
107 sockunion2str(su
, sabuf
, sizeof(sabuf
));
113 "Unable to set TCP MD5 option on socket for peer %s (sock=%d): This platform does not support MD5 auth for prefixes",
119 "Unable to set TCP MD5 option on socket for peer %s (sock=%d): %s",
120 sabuf
, socket
, safe_strerror(en
));
127 /* Helper for bgp_connect */
128 static int bgp_md5_set_connect(int socket
, union sockunion
*su
,
129 uint16_t prefixlen
, const char *password
)
133 #if HAVE_DECL_TCP_MD5SIG
134 frr_with_privs(&bgpd_privs
) {
135 ret
= bgp_md5_set_socket(socket
, su
, prefixlen
, password
);
137 #endif /* HAVE_TCP_MD5SIG */
142 static int bgp_md5_set_password(struct peer
*peer
, const char *password
)
144 struct listnode
*node
;
146 struct bgp_listener
*listener
;
149 * Set or unset the password on the listen socket(s). Outbound
150 * connections are taken care of in bgp_connect() below.
152 frr_with_privs(&bgpd_privs
) {
153 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, listener
))
154 if (listener
->su
.sa
.sa_family
==
155 peer
->su
.sa
.sa_family
) {
157 peer
->su
.sa
.sa_family
== AF_INET
162 * if we have stored a BGP vrf instance in the
163 * listener it must match the bgp instance in
164 * the peer otherwise the peer bgp instance
165 * must be the default vrf or a view instance
167 if (!listener
->bgp
) {
168 if (peer
->bgp
->vrf_id
!= VRF_DEFAULT
)
170 } else if (listener
->bgp
!= peer
->bgp
)
173 ret
= bgp_md5_set_socket(listener
->fd
,
174 &peer
->su
, prefixlen
,
182 int bgp_md5_set_prefix(struct bgp
*bgp
, struct prefix
*p
, const char *password
)
186 struct listnode
*node
;
187 struct bgp_listener
*listener
;
189 /* Set or unset the password on the listen socket(s). */
190 frr_with_privs(&bgpd_privs
) {
191 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, listener
))
192 if (listener
->su
.sa
.sa_family
== p
->family
193 && ((bgp
->vrf_id
== VRF_DEFAULT
)
194 || (listener
->bgp
== bgp
))) {
195 prefix2sockunion(p
, &su
);
196 ret
= bgp_md5_set_socket(listener
->fd
, &su
,
206 int bgp_md5_unset_prefix(struct bgp
*bgp
, struct prefix
*p
)
208 return bgp_md5_set_prefix(bgp
, p
, NULL
);
211 int bgp_md5_set(struct peer
*peer
)
213 /* Set the password from listen socket. */
214 return bgp_md5_set_password(peer
, peer
->password
);
217 int bgp_md5_unset(struct peer
*peer
)
219 /* Unset the password from listen socket. */
220 return bgp_md5_set_password(peer
, NULL
);
223 int bgp_set_socket_ttl(struct peer
*peer
, int bgp_sock
)
227 /* In case of peer is EBGP, we should set TTL for this connection. */
228 if (!peer
->gtsm_hops
&& (peer_sort_lookup(peer
) == BGP_PEER_EBGP
)) {
229 ret
= sockopt_ttl(peer
->su
.sa
.sa_family
, bgp_sock
, peer
->ttl
);
233 "%s: Can't set TxTTL on peer (rtrid %pI4) socket, err = %d",
234 __func__
, &peer
->remote_id
, errno
);
237 } else if (peer
->gtsm_hops
) {
238 /* On Linux, setting minttl without setting ttl seems to mess
240 outgoing ttl. Therefore setting both.
242 ret
= sockopt_ttl(peer
->su
.sa
.sa_family
, bgp_sock
, MAXTTL
);
246 "%s: Can't set TxTTL on peer (rtrid %pI4) socket, err = %d",
247 __func__
, &peer
->remote_id
, errno
);
250 ret
= sockopt_minttl(peer
->su
.sa
.sa_family
, bgp_sock
,
251 MAXTTL
+ 1 - peer
->gtsm_hops
);
255 "%s: Can't set MinTTL on peer (rtrid %pI4) socket, err = %d",
256 __func__
, &peer
->remote_id
, errno
);
265 * Obtain the BGP instance that the incoming connection should be processed
266 * against. This is important because more than one VRF could be using the
267 * same IP address space. The instance is got by obtaining the device to
268 * which the incoming connection is bound to. This could either be a VRF
269 * or it could be an interface, which in turn determines the VRF.
271 static int bgp_get_instance_for_inc_conn(int sock
, struct bgp
**bgp_inst
)
273 #ifndef SO_BINDTODEVICE
274 /* only Linux has SO_BINDTODEVICE, but we're in Linux-specific code here
275 * anyway since the assumption is that the interface name returned by
276 * getsockopt() is useful in identifying the VRF, particularly with
278 * VRF l3master device. The whole mechanism is specific to Linux, so...
279 * when other platforms add VRF support, this will need handling here as
280 * well. (or, some restructuring) */
281 *bgp_inst
= bgp_get_default();
285 char name
[VRF_NAMSIZ
+ 1];
286 socklen_t name_len
= VRF_NAMSIZ
;
289 struct listnode
*node
, *nnode
;
293 rc
= getsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, &name_len
);
295 #if defined(HAVE_CUMULUS)
296 flog_err(EC_LIB_SOCKET
,
297 "[Error] BGP SO_BINDTODEVICE get failed (%s), sock %d",
298 safe_strerror(errno
), sock
);
304 *bgp_inst
= bgp_get_default();
305 return 0; /* default instance. */
308 /* First try match to instance; if that fails, check for interfaces. */
309 bgp
= bgp_lookup_by_name(name
);
311 if (!bgp
->vrf_id
) // unexpected
317 /* TODO - This will be optimized once interfaces move into the NS */
318 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
319 struct interface
*ifp
;
321 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
324 ifp
= if_lookup_by_name(name
, bgp
->vrf_id
);
331 /* We didn't match to either an instance or an interface. */
336 static void bgp_socket_set_buffer_size(const int fd
)
338 if (getsockopt_so_sendbuf(fd
) < (int)bm
->socket_buffer
)
339 setsockopt_so_sendbuf(fd
, bm
->socket_buffer
);
340 if (getsockopt_so_recvbuf(fd
) < (int)bm
->socket_buffer
)
341 setsockopt_so_recvbuf(fd
, bm
->socket_buffer
);
344 /* Accept bgp connection. */
345 static void bgp_accept(struct thread
*thread
)
350 struct bgp_listener
*listener
= THREAD_ARG(thread
);
353 char buf
[SU_ADDRSTRLEN
];
354 struct bgp
*bgp
= NULL
;
358 bgp
= bgp_lookup_by_name(listener
->name
);
360 /* Register accept thread. */
361 accept_sock
= THREAD_FD(thread
);
362 if (accept_sock
< 0) {
363 flog_err_sys(EC_LIB_SOCKET
,
364 "[Error] BGP accept socket fd is negative: %d",
369 thread_add_read(bm
->master
, bgp_accept
, listener
, accept_sock
,
372 /* Accept client connection. */
373 bgp_sock
= sockunion_accept(accept_sock
, &su
);
374 int save_errno
= errno
;
376 if (save_errno
== EINVAL
) {
378 bgp
? vrf_lookup_by_id(bgp
->vrf_id
) : NULL
;
381 * It appears that sometimes, when VRFs are deleted on
382 * the system, it takes a little while for us to get
383 * notified about that. In the meantime we endlessly
384 * loop on accept(), because the socket, having been
385 * bound to a now-deleted VRF device, is in some weird
386 * state which causes accept() to fail.
388 * To avoid this, if we see accept() fail with EINVAL,
389 * we cancel ourselves and trust that when the VRF
390 * deletion notification comes in the event handler for
391 * that will take care of cleaning us up.
395 "[Error] accept() failed with error \"%s\" on BGP listener socket %d for BGP instance in VRF \"%s\"; refreshing socket",
396 safe_strerror(save_errno
), accept_sock
,
398 THREAD_OFF(listener
->thread
);
402 "[Error] BGP socket accept failed (%s); retrying",
403 safe_strerror(save_errno
));
407 set_nonblocking(bgp_sock
);
409 /* Obtain BGP instance this connection is meant for.
410 * - if it is a VRF netns sock, then BGP is in listener structure
411 * - otherwise, the bgp instance need to be demultiplexed
415 else if (bgp_get_instance_for_inc_conn(bgp_sock
, &bgp
)) {
416 if (bgp_debug_neighbor_events(NULL
))
418 "[Event] Could not get instance for incoming conn from %s",
419 inet_sutop(&su
, buf
));
424 bgp_socket_set_buffer_size(bgp_sock
);
426 /* Check remote IP address */
427 peer1
= peer_lookup(bgp
, &su
);
430 peer1
= peer_lookup_dynamic_neighbor(bgp
, &su
);
432 /* Dynamic neighbor has been created, let it proceed */
433 peer1
->fd
= bgp_sock
;
435 /* Set the user configured MSS to TCP socket */
436 if (CHECK_FLAG(peer1
->flags
, PEER_FLAG_TCP_MSS
))
437 sockopt_tcp_mss_set(bgp_sock
, peer1
->tcp_mss
);
439 bgp_fsm_change_status(peer1
, Active
);
441 peer1
->t_start
); /* created in peer_create() */
443 if (peer_active(peer1
)) {
444 if (CHECK_FLAG(peer1
->flags
,
445 PEER_FLAG_TIMER_DELAYOPEN
))
448 TCP_connection_open_w_delay
);
451 TCP_connection_open
);
459 if (bgp_debug_neighbor_events(NULL
)) {
461 "[Event] %s connection rejected(%s:%u:%s) - not configured and not valid for dynamic",
462 inet_sutop(&su
, buf
), bgp
->name_pretty
, bgp
->as
,
463 VRF_LOGNAME(vrf_lookup_by_id(bgp
->vrf_id
)));
469 if (CHECK_FLAG(peer1
->flags
, PEER_FLAG_SHUTDOWN
)
470 || CHECK_FLAG(peer1
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
471 if (bgp_debug_neighbor_events(peer1
))
473 "[Event] connection from %s rejected(%s:%u:%s) due to admin shutdown",
474 inet_sutop(&su
, buf
), bgp
->name_pretty
, bgp
->as
,
475 VRF_LOGNAME(vrf_lookup_by_id(bgp
->vrf_id
)));
481 * Do not accept incoming connections in Clearing state. This can result
482 * in incorect state transitions - e.g., the connection goes back to
483 * Established and then the Clearing_Completed event is generated. Also,
484 * block incoming connection in Deleted state.
486 if (peer1
->status
== Clearing
|| peer1
->status
== Deleted
) {
487 if (bgp_debug_neighbor_events(peer1
))
489 "[Event] Closing incoming conn for %s (%p) state %d",
490 peer1
->host
, peer1
, peer1
->status
);
495 /* Check that at least one AF is activated for the peer. */
496 if (!peer_active(peer1
)) {
497 if (bgp_debug_neighbor_events(peer1
))
499 "%s - incoming conn rejected - no AF activated for peer",
505 /* Do not try to reconnect if the peer reached maximum
506 * prefixes, restart timer is still running or the peer
509 if (BGP_PEER_START_SUPPRESSED(peer1
)) {
510 if (bgp_debug_neighbor_events(peer1
))
512 "[Event] Incoming BGP connection rejected from %s due to maximum-prefix or shutdown",
518 if (bgp_debug_neighbor_events(peer1
))
519 zlog_debug("[Event] BGP connection from host %s fd %d",
520 inet_sutop(&su
, buf
), bgp_sock
);
522 if (peer1
->doppelganger
) {
523 /* We have an existing connection. Kill the existing one and run
526 if (bgp_debug_neighbor_events(peer1
))
528 "[Event] New active connection from peer %s, Killing previous active connection",
530 peer_delete(peer1
->doppelganger
);
533 if (bgp_set_socket_ttl(peer1
, bgp_sock
) < 0)
534 if (bgp_debug_neighbor_events(peer1
))
536 "[Event] Unable to set min/max TTL on peer %s, Continuing",
539 peer
= peer_create(&su
, peer1
->conf_if
, peer1
->bgp
, peer1
->local_as
,
540 peer1
->as
, peer1
->as_type
, NULL
);
541 hash_release(peer
->bgp
->peerhash
, peer
);
542 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
544 peer_xfer_config(peer
, peer1
);
545 bgp_peer_gr_flags_update(peer
);
547 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
550 if (bgp_peer_gr_mode_get(peer
) == PEER_DISABLE
) {
552 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
554 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
559 UNSET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
561 peer
->doppelganger
= peer1
;
562 peer1
->doppelganger
= peer
;
564 frr_with_privs(&bgpd_privs
) {
565 vrf_bind(peer
->bgp
->vrf_id
, bgp_sock
, bgp_get_bound_name(peer
));
567 bgp_peer_reg_with_nht(peer
);
568 bgp_fsm_change_status(peer
, Active
);
569 BGP_TIMER_OFF(peer
->t_start
); /* created in peer_create() */
571 SET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
572 /* Make dummy peer until read Open packet. */
573 if (peer_established(peer1
)
574 && CHECK_FLAG(peer1
->sflags
, PEER_STATUS_NSF_MODE
)) {
575 /* If we have an existing established connection with graceful
577 * capability announced with one or more address families, then
579 * existing established connection and move state to connect.
581 peer1
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
583 if (CHECK_FLAG(peer1
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
584 || CHECK_FLAG(peer1
->flags
,
585 PEER_FLAG_GRACEFUL_RESTART_HELPER
))
586 SET_FLAG(peer1
->sflags
, PEER_STATUS_NSF_WAIT
);
588 bgp_event_update(peer1
, TCP_connection_closed
);
591 if (peer_active(peer
)) {
592 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
593 BGP_EVENT_ADD(peer
, TCP_connection_open_w_delay
);
595 BGP_EVENT_ADD(peer
, TCP_connection_open
);
599 * If we are doing nht for a peer that is v6 LL based
600 * massage the event system to make things happy
602 bgp_nht_interface_events(peer
);
605 /* BGP socket bind. */
606 static char *bgp_get_bound_name(struct peer
*peer
)
611 if ((peer
->bgp
->vrf_id
== VRF_DEFAULT
) && !peer
->ifname
615 if (peer
->su
.sa
.sa_family
!= AF_INET
616 && peer
->su
.sa
.sa_family
!= AF_INET6
)
617 return NULL
; // unexpected
619 /* For IPv6 peering, interface (unnumbered or link-local with interface)
620 * takes precedence over VRF. For IPv4 peering, explicit interface or
621 * VRF are the situations to bind.
623 if (peer
->su
.sa
.sa_family
== AF_INET6
&& peer
->conf_if
)
624 return peer
->conf_if
;
629 if (peer
->bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
632 return peer
->bgp
->name
;
635 static int bgp_update_address(struct interface
*ifp
, const union sockunion
*dst
,
636 union sockunion
*addr
)
638 struct prefix
*p
, *sel
, d
;
639 struct connected
*connected
;
640 struct listnode
*node
;
643 if (!sockunion2hostprefix(dst
, &d
))
649 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
650 p
= connected
->address
;
651 if (p
->family
!= d
.family
)
653 if (prefix_common_bits(p
, &d
) > common
) {
655 common
= prefix_common_bits(sel
, &d
);
662 prefix2sockunion(sel
, addr
);
666 /* Update source selection. */
667 static int bgp_update_source(struct peer
*peer
)
669 struct interface
*ifp
;
670 union sockunion addr
;
673 sockunion_init(&addr
);
675 /* Source is specified with interface name. */
676 if (peer
->update_if
) {
677 ifp
= if_lookup_by_name(peer
->update_if
, peer
->bgp
->vrf_id
);
681 if (bgp_update_address(ifp
, &peer
->su
, &addr
))
684 ret
= sockunion_bind(peer
->fd
, &addr
, 0, &addr
);
687 /* Source is specified with IP address. */
688 if (peer
->update_source
)
689 ret
= sockunion_bind(peer
->fd
, peer
->update_source
, 0,
690 peer
->update_source
);
695 /* BGP try to connect to the peer. */
696 int bgp_connect(struct peer
*peer
)
698 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
699 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
700 ifindex_t ifindex
= 0;
702 if (peer
->conf_if
&& BGP_PEER_SU_UNSPEC(peer
)) {
703 if (bgp_debug_neighbor_events(peer
))
704 zlog_debug("Peer address not learnt: Returning from connect");
707 frr_with_privs(&bgpd_privs
) {
708 /* Make socket for the peer. */
709 peer
->fd
= vrf_sockunion_socket(&peer
->su
, peer
->bgp
->vrf_id
,
710 bgp_get_bound_name(peer
));
713 peer
->last_reset
= PEER_DOWN_SOCKET_ERROR
;
714 if (bgp_debug_neighbor_events(peer
))
715 zlog_debug("%s: Failure to create socket for connection to %s, error received: %s(%d)",
716 __func__
, peer
->host
, safe_strerror(errno
),
721 set_nonblocking(peer
->fd
);
723 /* Set the user configured MSS to TCP socket */
724 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TCP_MSS
))
725 sockopt_tcp_mss_set(peer
->fd
, peer
->tcp_mss
);
727 bgp_socket_set_buffer_size(peer
->fd
);
729 if (bgp_set_socket_ttl(peer
, peer
->fd
) < 0) {
730 peer
->last_reset
= PEER_DOWN_SOCKET_ERROR
;
731 if (bgp_debug_neighbor_events(peer
))
732 zlog_debug("%s: Failure to set socket ttl for connection to %s, error received: %s(%d)",
733 __func__
, peer
->host
, safe_strerror(errno
),
738 sockopt_reuseaddr(peer
->fd
);
739 sockopt_reuseport(peer
->fd
);
741 #ifdef IPTOS_PREC_INTERNETCONTROL
742 frr_with_privs(&bgpd_privs
) {
743 if (sockunion_family(&peer
->su
) == AF_INET
)
744 setsockopt_ipv4_tos(peer
->fd
,
745 IPTOS_PREC_INTERNETCONTROL
);
746 else if (sockunion_family(&peer
->su
) == AF_INET6
)
747 setsockopt_ipv6_tclass(peer
->fd
,
748 IPTOS_PREC_INTERNETCONTROL
);
752 if (peer
->password
) {
753 uint16_t prefixlen
= peer
->su
.sa
.sa_family
== AF_INET
757 bgp_md5_set_connect(peer
->fd
, &peer
->su
, prefixlen
,
761 /* Update source bind. */
762 if (bgp_update_source(peer
) < 0) {
763 peer
->last_reset
= PEER_DOWN_SOCKET_ERROR
;
764 return connect_error
;
767 if (peer
->conf_if
|| peer
->ifname
)
768 ifindex
= ifname2ifindex(peer
->conf_if
? peer
->conf_if
772 if (bgp_debug_neighbor_events(peer
))
773 zlog_debug("%s [Event] Connect start to %s fd %d", peer
->host
,
774 peer
->host
, peer
->fd
);
776 /* Connect to the remote peer. */
777 return sockunion_connect(peer
->fd
, &peer
->su
, htons(peer
->port
),
781 /* After TCP connection is established. Get local address and port. */
782 int bgp_getsockname(struct peer
*peer
)
784 if (peer
->su_local
) {
785 sockunion_free(peer
->su_local
);
786 peer
->su_local
= NULL
;
789 if (peer
->su_remote
) {
790 sockunion_free(peer
->su_remote
);
791 peer
->su_remote
= NULL
;
794 peer
->su_local
= sockunion_getsockname(peer
->fd
);
797 peer
->su_remote
= sockunion_getpeername(peer
->fd
);
798 if (!peer
->su_remote
)
801 if (!bgp_zebra_nexthop_set(peer
->su_local
, peer
->su_remote
,
802 &peer
->nexthop
, peer
)) {
803 flog_err(EC_BGP_NH_UPD
,
804 "%s: nexthop_set failed, resetting connection - intf %p",
805 peer
->host
, peer
->nexthop
.ifp
);
812 static int bgp_listener(int sock
, struct sockaddr
*sa
, socklen_t salen
,
815 struct bgp_listener
*listener
;
818 sockopt_reuseaddr(sock
);
819 sockopt_reuseport(sock
);
821 frr_with_privs(&bgpd_privs
) {
823 #ifdef IPTOS_PREC_INTERNETCONTROL
824 if (sa
->sa_family
== AF_INET
)
825 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
826 else if (sa
->sa_family
== AF_INET6
)
827 setsockopt_ipv6_tclass(sock
,
828 IPTOS_PREC_INTERNETCONTROL
);
831 sockopt_v6only(sa
->sa_family
, sock
);
833 ret
= bind(sock
, sa
, salen
);
838 flog_err_sys(EC_LIB_SOCKET
, "bind: %s", safe_strerror(en
));
842 ret
= listen(sock
, SOMAXCONN
);
844 flog_err_sys(EC_LIB_SOCKET
, "listen: %s", safe_strerror(errno
));
848 listener
= XCALLOC(MTYPE_BGP_LISTENER
, sizeof(*listener
));
850 listener
->name
= XSTRDUP(MTYPE_BGP_LISTENER
, bgp
->name
);
852 /* this socket is in a vrf record bgp back pointer */
853 if (bgp
->vrf_id
!= VRF_DEFAULT
)
856 memcpy(&listener
->su
, sa
, salen
);
857 thread_add_read(bm
->master
, bgp_accept
, listener
, sock
,
859 listnode_add(bm
->listen_sockets
, listener
);
864 /* IPv6 supported version of BGP server socket setup. */
865 int bgp_socket(struct bgp
*bgp
, unsigned short port
, const char *address
)
867 struct addrinfo
*ainfo
;
868 struct addrinfo
*ainfo_save
;
869 static const struct addrinfo req
= {
870 .ai_family
= AF_UNSPEC
,
871 .ai_flags
= AI_PASSIVE
,
872 .ai_socktype
= SOCK_STREAM
,
875 char port_str
[BUFSIZ
];
877 snprintf(port_str
, sizeof(port_str
), "%d", port
);
878 port_str
[sizeof(port_str
) - 1] = '\0';
880 frr_with_privs(&bgpd_privs
) {
881 ret
= vrf_getaddrinfo(address
, port_str
, &req
, &ainfo_save
,
885 flog_err_sys(EC_LIB_SOCKET
, "getaddrinfo: %s",
889 if (bgp_option_check(BGP_OPT_NO_ZEBRA
) &&
890 bgp
->vrf_id
!= VRF_DEFAULT
) {
891 freeaddrinfo(ainfo_save
);
895 for (ainfo
= ainfo_save
; ainfo
; ainfo
= ainfo
->ai_next
) {
898 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
901 frr_with_privs(&bgpd_privs
) {
902 sock
= vrf_socket(ainfo
->ai_family
,
907 == BGP_INSTANCE_TYPE_VRF
908 ? bgp
->name
: NULL
));
911 flog_err_sys(EC_LIB_SOCKET
, "socket: %s",
912 safe_strerror(errno
));
916 /* if we intend to implement ttl-security, this socket needs
918 sockopt_ttl(ainfo
->ai_family
, sock
, MAXTTL
);
920 ret
= bgp_listener(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
,
927 freeaddrinfo(ainfo_save
);
928 if (count
== 0 && bgp
->inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
931 "%s: no usable addresses please check other programs usage of specified port %d",
933 flog_err_sys(EC_LIB_SOCKET
, "%s: Program cannot continue",
941 /* this function closes vrf socket
942 * this should be called only for vrf socket with netns backend
944 void bgp_close_vrf_socket(struct bgp
*bgp
)
946 struct listnode
*node
, *next
;
947 struct bgp_listener
*listener
;
952 if (bm
->listen_sockets
== NULL
)
955 for (ALL_LIST_ELEMENTS(bm
->listen_sockets
, node
, next
, listener
)) {
956 if (listener
->bgp
== bgp
) {
957 THREAD_OFF(listener
->thread
);
959 listnode_delete(bm
->listen_sockets
, listener
);
960 XFREE(MTYPE_BGP_LISTENER
, listener
->name
);
961 XFREE(MTYPE_BGP_LISTENER
, listener
);
966 /* this function closes main socket
970 struct listnode
*node
, *next
;
971 struct bgp_listener
*listener
;
973 if (bm
->listen_sockets
== NULL
)
976 for (ALL_LIST_ELEMENTS(bm
->listen_sockets
, node
, next
, listener
)) {
979 THREAD_OFF(listener
->thread
);
981 listnode_delete(bm
->listen_sockets
, listener
);
982 XFREE(MTYPE_BGP_LISTENER
, listener
->name
);
983 XFREE(MTYPE_BGP_LISTENER
, listener
);