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 void bgp_dump_listener_info(struct vty
*vty
)
65 struct listnode
*node
;
66 struct bgp_listener
*listener
;
68 vty_out(vty
, "Name fd Address\n");
69 vty_out(vty
, "---------------------------\n");
70 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, listener
)) {
71 char buf
[SU_ADDRSTRLEN
];
73 vty_out(vty
, "%-16s %d %s\n",
74 listener
->name
? listener
->name
: VRF_DEFAULT_NAME
,
76 sockunion2str(&listener
->su
, buf
, sizeof(buf
)));
81 * Set MD5 key for the socket, for the given IPv4 peer address.
82 * If the password is NULL or zero-length, the option will be disabled.
84 static int bgp_md5_set_socket(int socket
, union sockunion
*su
,
85 uint16_t prefixlen
, const char *password
)
89 #if HAVE_DECL_TCP_MD5SIG
91 #endif /* HAVE_TCP_MD5SIG */
95 #if HAVE_DECL_TCP_MD5SIG
96 /* Ensure there is no extraneous port information. */
97 memcpy(&su2
, su
, sizeof(union sockunion
));
98 if (su2
.sa
.sa_family
== AF_INET
)
101 su2
.sin6
.sin6_port
= 0;
103 /* For addresses, use the non-extended signature functionality */
104 if ((su2
.sa
.sa_family
== AF_INET
&& prefixlen
== IPV4_MAX_PREFIXLEN
)
105 || (su2
.sa
.sa_family
== AF_INET6
106 && prefixlen
== IPV6_MAX_PREFIXLEN
))
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
168 : IPV6_MAX_PREFIXLEN
;
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
178 && peer
->bgp
->inst_type
179 != BGP_INSTANCE_TYPE_VIEW
)
181 } else if (listener
->bgp
!= peer
->bgp
)
184 ret
= bgp_md5_set_socket(listener
->fd
,
185 &peer
->su
, prefixlen
,
193 int bgp_md5_set_prefix(struct bgp
*bgp
, struct prefix
*p
, const char *password
)
197 struct listnode
*node
;
198 struct bgp_listener
*listener
;
200 /* Set or unset the password on the listen socket(s). */
201 frr_with_privs(&bgpd_privs
) {
202 for (ALL_LIST_ELEMENTS_RO(bm
->listen_sockets
, node
, listener
))
203 if (listener
->su
.sa
.sa_family
== p
->family
204 && ((bgp
->vrf_id
== VRF_DEFAULT
)
205 || (listener
->bgp
== bgp
))) {
206 prefix2sockunion(p
, &su
);
207 ret
= bgp_md5_set_socket(listener
->fd
, &su
,
217 int bgp_md5_unset_prefix(struct bgp
*bgp
, struct prefix
*p
)
219 return bgp_md5_set_prefix(bgp
, p
, NULL
);
222 int bgp_md5_set(struct peer
*peer
)
224 /* Set the password from listen socket. */
225 return bgp_md5_set_password(peer
, peer
->password
);
228 int bgp_md5_unset(struct peer
*peer
)
230 /* Unset the password from listen socket. */
231 return bgp_md5_set_password(peer
, NULL
);
234 int bgp_set_socket_ttl(struct peer
*peer
, int bgp_sock
)
236 char buf
[INET_ADDRSTRLEN
];
239 /* In case of peer is EBGP, we should set TTL for this connection. */
240 if (!peer
->gtsm_hops
&& (peer_sort_lookup(peer
) == BGP_PEER_EBGP
)) {
241 ret
= sockopt_ttl(peer
->su
.sa
.sa_family
, bgp_sock
, peer
->ttl
);
245 "%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
247 inet_ntop(AF_INET
, &peer
->remote_id
, buf
,
252 } else if (peer
->gtsm_hops
) {
253 /* On Linux, setting minttl without setting ttl seems to mess
255 outgoing ttl. Therefore setting both.
257 ret
= sockopt_ttl(peer
->su
.sa
.sa_family
, bgp_sock
, MAXTTL
);
261 "%s: Can't set TxTTL on peer (rtrid %s) socket, err = %d",
263 inet_ntop(AF_INET
, &peer
->remote_id
, buf
,
268 ret
= sockopt_minttl(peer
->su
.sa
.sa_family
, bgp_sock
,
269 MAXTTL
+ 1 - peer
->gtsm_hops
);
273 "%s: Can't set MinTTL on peer (rtrid %s) socket, err = %d",
275 inet_ntop(AF_INET
, &peer
->remote_id
, buf
,
286 * Obtain the BGP instance that the incoming connection should be processed
287 * against. This is important because more than one VRF could be using the
288 * same IP address space. The instance is got by obtaining the device to
289 * which the incoming connection is bound to. This could either be a VRF
290 * or it could be an interface, which in turn determines the VRF.
292 static int bgp_get_instance_for_inc_conn(int sock
, struct bgp
**bgp_inst
)
294 #ifndef SO_BINDTODEVICE
295 /* only Linux has SO_BINDTODEVICE, but we're in Linux-specific code here
296 * anyway since the assumption is that the interface name returned by
297 * getsockopt() is useful in identifying the VRF, particularly with
299 * VRF l3master device. The whole mechanism is specific to Linux, so...
300 * when other platforms add VRF support, this will need handling here as
301 * well. (or, some restructuring) */
302 *bgp_inst
= bgp_get_default();
306 char name
[VRF_NAMSIZ
+ 1];
307 socklen_t name_len
= VRF_NAMSIZ
;
310 struct listnode
*node
, *nnode
;
314 rc
= getsockopt(sock
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, &name_len
);
316 #if defined(HAVE_CUMULUS)
317 flog_err(EC_LIB_SOCKET
,
318 "[Error] BGP SO_BINDTODEVICE get failed (%s), sock %d",
319 safe_strerror(errno
), sock
);
325 *bgp_inst
= bgp_get_default();
326 return 0; /* default instance. */
329 /* First try match to instance; if that fails, check for interfaces. */
330 bgp
= bgp_lookup_by_name(name
);
332 if (!bgp
->vrf_id
) // unexpected
338 /* TODO - This will be optimized once interfaces move into the NS */
339 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
340 struct interface
*ifp
;
342 if (bgp
->inst_type
== BGP_INSTANCE_TYPE_VIEW
)
345 ifp
= if_lookup_by_name(name
, bgp
->vrf_id
);
352 /* We didn't match to either an instance or an interface. */
357 static void bgp_socket_set_buffer_size(const int fd
)
359 if (getsockopt_so_sendbuf(fd
) < (int)bm
->socket_buffer
)
360 setsockopt_so_sendbuf(fd
, bm
->socket_buffer
);
361 if (getsockopt_so_recvbuf(fd
) < (int)bm
->socket_buffer
)
362 setsockopt_so_recvbuf(fd
, bm
->socket_buffer
);
365 /* Accept bgp connection. */
366 static int bgp_accept(struct thread
*thread
)
371 struct bgp_listener
*listener
= THREAD_ARG(thread
);
374 char buf
[SU_ADDRSTRLEN
];
375 struct bgp
*bgp
= NULL
;
379 bgp
= bgp_lookup_by_name(listener
->name
);
381 /* Register accept thread. */
382 accept_sock
= THREAD_FD(thread
);
383 if (accept_sock
< 0) {
384 flog_err_sys(EC_LIB_SOCKET
,
385 "[Error] BGP accept socket fd is negative: %d",
389 listener
->thread
= NULL
;
391 thread_add_read(bm
->master
, bgp_accept
, listener
, accept_sock
,
394 /* Accept client connection. */
395 bgp_sock
= sockunion_accept(accept_sock
, &su
);
396 int save_errno
= errno
;
398 if (save_errno
== EINVAL
) {
400 bgp
? vrf_lookup_by_id(bgp
->vrf_id
) : NULL
;
403 * It appears that sometimes, when VRFs are deleted on
404 * the system, it takes a little while for us to get
405 * notified about that. In the meantime we endlessly
406 * loop on accept(), because the socket, having been
407 * bound to a now-deleted VRF device, is in some weird
408 * state which causes accept() to fail.
410 * To avoid this, if we see accept() fail with EINVAL,
411 * we cancel ourselves and trust that when the VRF
412 * deletion notification comes in the event handler for
413 * that will take care of cleaning us up.
417 "[Error] accept() failed with error \"%s\" on BGP listener socket %d for BGP instance in VRF \"%s\"; refreshing socket",
418 safe_strerror(save_errno
), accept_sock
,
420 THREAD_OFF(listener
->thread
);
424 "[Error] BGP socket accept failed (%s); retrying",
425 safe_strerror(save_errno
));
429 set_nonblocking(bgp_sock
);
431 /* Obtain BGP instance this connection is meant for.
432 * - if it is a VRF netns sock, then BGP is in listener structure
433 * - otherwise, the bgp instance need to be demultiplexed
437 else if (bgp_get_instance_for_inc_conn(bgp_sock
, &bgp
)) {
438 if (bgp_debug_neighbor_events(NULL
))
440 "[Event] Could not get instance for incoming conn from %s",
441 inet_sutop(&su
, buf
));
446 bgp_socket_set_buffer_size(bgp_sock
);
448 /* Check remote IP address */
449 peer1
= peer_lookup(bgp
, &su
);
452 peer1
= peer_lookup_dynamic_neighbor(bgp
, &su
);
454 /* Dynamic neighbor has been created, let it proceed */
455 peer1
->fd
= bgp_sock
;
456 bgp_fsm_change_status(peer1
, Active
);
458 peer1
->t_start
); /* created in peer_create() */
460 if (peer_active(peer1
)) {
461 if (CHECK_FLAG(peer1
->flags
,
462 PEER_FLAG_TIMER_DELAYOPEN
))
465 TCP_connection_open_w_delay
);
468 TCP_connection_open
);
476 if (bgp_debug_neighbor_events(NULL
)) {
478 "[Event] %s connection rejected(%s:%u:%s) - not configured and not valid for dynamic",
479 inet_sutop(&su
, buf
), bgp
->name_pretty
, bgp
->as
,
480 VRF_LOGNAME(vrf_lookup_by_id(bgp
->vrf_id
)));
486 if (CHECK_FLAG(peer1
->flags
, PEER_FLAG_SHUTDOWN
)
487 || CHECK_FLAG(peer1
->bgp
->flags
, BGP_FLAG_SHUTDOWN
)) {
488 if (bgp_debug_neighbor_events(peer1
))
490 "[Event] connection from %s rejected(%s:%u:%s) due to admin shutdown",
491 inet_sutop(&su
, buf
), bgp
->name_pretty
, bgp
->as
,
492 VRF_LOGNAME(vrf_lookup_by_id(bgp
->vrf_id
)));
498 * Do not accept incoming connections in Clearing state. This can result
499 * in incorect state transitions - e.g., the connection goes back to
500 * Established and then the Clearing_Completed event is generated. Also,
501 * block incoming connection in Deleted state.
503 if (peer1
->status
== Clearing
|| peer1
->status
== Deleted
) {
504 if (bgp_debug_neighbor_events(peer1
))
506 "[Event] Closing incoming conn for %s (%p) state %d",
507 peer1
->host
, peer1
, peer1
->status
);
512 /* Check that at least one AF is activated for the peer. */
513 if (!peer_active(peer1
)) {
514 if (bgp_debug_neighbor_events(peer1
))
516 "%s - incoming conn rejected - no AF activated for peer",
522 /* Do not try to reconnect if the peer reached maximum
523 * prefixes, restart timer is still running or the peer
526 if (BGP_PEER_START_SUPPRESSED(peer1
)) {
527 if (bgp_debug_neighbor_events(peer1
))
529 "[Event] Incoming BGP connection rejected from %s due to maximum-prefix or shutdown",
535 if (bgp_debug_neighbor_events(peer1
))
536 zlog_debug("[Event] BGP connection from host %s fd %d",
537 inet_sutop(&su
, buf
), bgp_sock
);
539 if (peer1
->doppelganger
) {
540 /* We have an existing connection. Kill the existing one and run
543 if (bgp_debug_neighbor_events(peer1
))
545 "[Event] New active connection from peer %s, Killing previous active connection",
547 peer_delete(peer1
->doppelganger
);
550 if (bgp_set_socket_ttl(peer1
, bgp_sock
) < 0)
551 if (bgp_debug_neighbor_events(peer1
))
553 "[Event] Unable to set min/max TTL on peer %s, Continuing",
556 peer
= peer_create(&su
, peer1
->conf_if
, peer1
->bgp
, peer1
->local_as
,
557 peer1
->as
, peer1
->as_type
, 0, 0, NULL
);
558 hash_release(peer
->bgp
->peerhash
, peer
);
559 hash_get(peer
->bgp
->peerhash
, peer
, hash_alloc_intern
);
561 peer_xfer_config(peer
, peer1
);
562 bgp_peer_gr_flags_update(peer
);
564 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer
->bgp
,
567 if (bgp_peer_gr_mode_get(peer
) == PEER_DISABLE
) {
569 UNSET_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
);
571 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
)) {
576 UNSET_FLAG(peer
->flags
, PEER_FLAG_CONFIG_NODE
);
578 peer
->doppelganger
= peer1
;
579 peer1
->doppelganger
= peer
;
581 vrf_bind(peer
->bgp
->vrf_id
, bgp_sock
, bgp_get_bound_name(peer
));
582 bgp_fsm_change_status(peer
, Active
);
583 BGP_TIMER_OFF(peer
->t_start
); /* created in peer_create() */
585 SET_FLAG(peer
->sflags
, PEER_STATUS_ACCEPT_PEER
);
586 /* Make dummy peer until read Open packet. */
587 if (peer1
->status
== Established
588 && CHECK_FLAG(peer1
->sflags
, PEER_STATUS_NSF_MODE
)) {
589 /* If we have an existing established connection with graceful
591 * capability announced with one or more address families, then
593 * existing established connection and move state to connect.
595 peer1
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
597 if (CHECK_FLAG(peer1
->flags
, PEER_FLAG_GRACEFUL_RESTART
)
598 || CHECK_FLAG(peer1
->flags
,
599 PEER_FLAG_GRACEFUL_RESTART_HELPER
))
600 SET_FLAG(peer1
->sflags
, PEER_STATUS_NSF_WAIT
);
602 bgp_event_update(peer1
, TCP_connection_closed
);
605 if (peer_active(peer
)) {
606 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_TIMER_DELAYOPEN
))
607 BGP_EVENT_ADD(peer
, TCP_connection_open_w_delay
);
609 BGP_EVENT_ADD(peer
, TCP_connection_open
);
615 /* BGP socket bind. */
616 static char *bgp_get_bound_name(struct peer
*peer
)
623 if ((peer
->bgp
->vrf_id
== VRF_DEFAULT
) && !peer
->ifname
627 if (peer
->su
.sa
.sa_family
!= AF_INET
628 && peer
->su
.sa
.sa_family
!= AF_INET6
)
629 return NULL
; // unexpected
631 /* For IPv6 peering, interface (unnumbered or link-local with interface)
632 * takes precedence over VRF. For IPv4 peering, explicit interface or
633 * VRF are the situations to bind.
635 if (peer
->su
.sa
.sa_family
== AF_INET6
)
636 name
= (peer
->conf_if
? peer
->conf_if
637 : (peer
->ifname
? peer
->ifname
640 name
= peer
->ifname
? peer
->ifname
: peer
->bgp
->name
;
645 static int bgp_update_address(struct interface
*ifp
, const union sockunion
*dst
,
646 union sockunion
*addr
)
648 struct prefix
*p
, *sel
, d
;
649 struct connected
*connected
;
650 struct listnode
*node
;
653 if (!sockunion2hostprefix(dst
, &d
))
659 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
660 p
= connected
->address
;
661 if (p
->family
!= d
.family
)
663 if (prefix_common_bits(p
, &d
) > common
) {
665 common
= prefix_common_bits(sel
, &d
);
672 prefix2sockunion(sel
, addr
);
676 /* Update source selection. */
677 static int bgp_update_source(struct peer
*peer
)
679 struct interface
*ifp
;
680 union sockunion addr
;
683 sockunion_init(&addr
);
685 /* Source is specified with interface name. */
686 if (peer
->update_if
) {
687 ifp
= if_lookup_by_name(peer
->update_if
, peer
->bgp
->vrf_id
);
691 if (bgp_update_address(ifp
, &peer
->su
, &addr
))
694 ret
= sockunion_bind(peer
->fd
, &addr
, 0, &addr
);
697 /* Source is specified with IP address. */
698 if (peer
->update_source
)
699 ret
= sockunion_bind(peer
->fd
, peer
->update_source
, 0,
700 peer
->update_source
);
705 /* BGP try to connect to the peer. */
706 int bgp_connect(struct peer
*peer
)
708 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
));
709 assert(!CHECK_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
));
710 ifindex_t ifindex
= 0;
712 if (peer
->conf_if
&& BGP_PEER_SU_UNSPEC(peer
)) {
713 zlog_debug("Peer address not learnt: Returning from connect");
716 frr_with_privs(&bgpd_privs
) {
717 /* Make socket for the peer. */
718 peer
->fd
= vrf_sockunion_socket(&peer
->su
, peer
->bgp
->vrf_id
,
719 bgp_get_bound_name(peer
));
724 set_nonblocking(peer
->fd
);
726 bgp_socket_set_buffer_size(peer
->fd
);
728 if (bgp_set_socket_ttl(peer
, peer
->fd
) < 0)
731 sockopt_reuseaddr(peer
->fd
);
732 sockopt_reuseport(peer
->fd
);
734 #ifdef IPTOS_PREC_INTERNETCONTROL
735 frr_with_privs(&bgpd_privs
) {
736 if (sockunion_family(&peer
->su
) == AF_INET
)
737 setsockopt_ipv4_tos(peer
->fd
,
738 IPTOS_PREC_INTERNETCONTROL
);
739 else if (sockunion_family(&peer
->su
) == AF_INET6
)
740 setsockopt_ipv6_tclass(peer
->fd
,
741 IPTOS_PREC_INTERNETCONTROL
);
745 if (peer
->password
) {
746 uint16_t prefixlen
= peer
->su
.sa
.sa_family
== AF_INET
748 : IPV6_MAX_PREFIXLEN
;
750 bgp_md5_set_connect(peer
->fd
, &peer
->su
, prefixlen
,
754 /* Update source bind. */
755 if (bgp_update_source(peer
) < 0) {
756 return connect_error
;
759 if (peer
->conf_if
|| peer
->ifname
)
760 ifindex
= ifname2ifindex(peer
->conf_if
? peer
->conf_if
764 if (bgp_debug_neighbor_events(peer
))
765 zlog_debug("%s [Event] Connect start to %s fd %d", peer
->host
,
766 peer
->host
, peer
->fd
);
768 /* Connect to the remote peer. */
769 return sockunion_connect(peer
->fd
, &peer
->su
, htons(peer
->port
),
773 /* After TCP connection is established. Get local address and port. */
774 int bgp_getsockname(struct peer
*peer
)
776 if (peer
->su_local
) {
777 sockunion_free(peer
->su_local
);
778 peer
->su_local
= NULL
;
781 if (peer
->su_remote
) {
782 sockunion_free(peer
->su_remote
);
783 peer
->su_remote
= NULL
;
786 peer
->su_local
= sockunion_getsockname(peer
->fd
);
789 peer
->su_remote
= sockunion_getpeername(peer
->fd
);
790 if (!peer
->su_remote
)
793 if (!bgp_zebra_nexthop_set(peer
->su_local
, peer
->su_remote
,
794 &peer
->nexthop
, peer
)) {
795 flog_err(EC_BGP_NH_UPD
,
796 "%s: nexthop_set failed, resetting connection - intf %p",
797 peer
->host
, peer
->nexthop
.ifp
);
804 static int bgp_listener(int sock
, struct sockaddr
*sa
, socklen_t salen
,
807 struct bgp_listener
*listener
;
810 sockopt_reuseaddr(sock
);
811 sockopt_reuseport(sock
);
813 frr_with_privs(&bgpd_privs
) {
815 #ifdef IPTOS_PREC_INTERNETCONTROL
816 if (sa
->sa_family
== AF_INET
)
817 setsockopt_ipv4_tos(sock
, IPTOS_PREC_INTERNETCONTROL
);
818 else if (sa
->sa_family
== AF_INET6
)
819 setsockopt_ipv6_tclass(sock
,
820 IPTOS_PREC_INTERNETCONTROL
);
823 sockopt_v6only(sa
->sa_family
, sock
);
825 ret
= bind(sock
, sa
, salen
);
830 flog_err_sys(EC_LIB_SOCKET
, "bind: %s", safe_strerror(en
));
834 ret
= listen(sock
, SOMAXCONN
);
836 flog_err_sys(EC_LIB_SOCKET
, "listen: %s", safe_strerror(errno
));
840 listener
= XCALLOC(MTYPE_BGP_LISTENER
, sizeof(*listener
));
842 listener
->name
= XSTRDUP(MTYPE_BGP_LISTENER
, bgp
->name
);
844 /* this socket is in a vrf record bgp back pointer */
845 if (bgp
->vrf_id
!= VRF_DEFAULT
846 && bgp
->inst_type
!= BGP_INSTANCE_TYPE_VIEW
)
849 memcpy(&listener
->su
, sa
, salen
);
850 listener
->thread
= NULL
;
851 thread_add_read(bm
->master
, bgp_accept
, listener
, sock
,
853 listnode_add(bm
->listen_sockets
, listener
);
858 /* IPv6 supported version of BGP server socket setup. */
859 int bgp_socket(struct bgp
*bgp
, unsigned short port
, const char *address
)
861 struct addrinfo
*ainfo
;
862 struct addrinfo
*ainfo_save
;
863 static const struct addrinfo req
= {
864 .ai_family
= AF_UNSPEC
,
865 .ai_flags
= AI_PASSIVE
,
866 .ai_socktype
= SOCK_STREAM
,
869 char port_str
[BUFSIZ
];
871 snprintf(port_str
, sizeof(port_str
), "%d", port
);
872 port_str
[sizeof(port_str
) - 1] = '\0';
874 frr_with_privs(&bgpd_privs
) {
875 ret
= vrf_getaddrinfo(address
, port_str
, &req
, &ainfo_save
,
879 flog_err_sys(EC_LIB_SOCKET
, "getaddrinfo: %s",
883 if (bgp_option_check(BGP_OPT_NO_ZEBRA
) &&
884 bgp
->vrf_id
!= VRF_DEFAULT
) {
885 freeaddrinfo(ainfo_save
);
889 for (ainfo
= ainfo_save
; ainfo
; ainfo
= ainfo
->ai_next
) {
892 if (ainfo
->ai_family
!= AF_INET
&& ainfo
->ai_family
!= AF_INET6
)
895 frr_with_privs(&bgpd_privs
) {
896 sock
= vrf_socket(ainfo
->ai_family
,
898 ainfo
->ai_protocol
, bgp
->vrf_id
,
900 == BGP_INSTANCE_TYPE_VRF
901 ? bgp
->name
: NULL
));
904 flog_err_sys(EC_LIB_SOCKET
, "socket: %s",
905 safe_strerror(errno
));
909 /* if we intend to implement ttl-security, this socket needs
911 sockopt_ttl(ainfo
->ai_family
, sock
, MAXTTL
);
913 ret
= bgp_listener(sock
, ainfo
->ai_addr
, ainfo
->ai_addrlen
,
920 freeaddrinfo(ainfo_save
);
921 if (count
== 0 && bgp
->inst_type
!= BGP_INSTANCE_TYPE_VRF
) {
924 "%s: no usable addresses please check other programs usage of specified port %d",
926 flog_err_sys(EC_LIB_SOCKET
, "%s: Program cannot continue",
934 /* this function closes vrf socket
935 * this should be called only for vrf socket with netns backend
937 void bgp_close_vrf_socket(struct bgp
*bgp
)
939 struct listnode
*node
, *next
;
940 struct bgp_listener
*listener
;
945 if (bm
->listen_sockets
== NULL
)
948 for (ALL_LIST_ELEMENTS(bm
->listen_sockets
, node
, next
, listener
)) {
949 if (listener
->bgp
== bgp
) {
950 THREAD_OFF(listener
->thread
);
952 listnode_delete(bm
->listen_sockets
, listener
);
953 XFREE(MTYPE_BGP_LISTENER
, listener
->name
);
954 XFREE(MTYPE_BGP_LISTENER
, listener
);
959 /* this function closes main socket
963 struct listnode
*node
, *next
;
964 struct bgp_listener
*listener
;
966 if (bm
->listen_sockets
== NULL
)
969 for (ALL_LIST_ELEMENTS(bm
->listen_sockets
, node
, next
, listener
)) {
972 THREAD_OFF(listener
->thread
);
974 listnode_delete(bm
->listen_sockets
, listener
);
975 XFREE(MTYPE_BGP_LISTENER
, listener
->name
);
976 XFREE(MTYPE_BGP_LISTENER
, listener
);