2 * IP MSDP socket management
3 * Copyright (C) 2016 Cumulus Networks, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <lib/network.h>
24 #include <lib/sockunion.h>
25 #include <lib/thread.h>
29 #include <lib/lib_errors.h>
33 #include "pim_errors.h"
36 #include "pim_msdp_socket.h"
38 /* increase socket send buffer size */
39 static void pim_msdp_update_sock_send_buffer_size(int fd
)
41 int size
= PIM_MSDP_SOCKET_SNDBUF_SIZE
;
43 socklen_t optlen
= sizeof(optval
);
45 if (getsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &optval
, &optlen
) < 0) {
46 flog_err_sys(LIB_ERR_SOCKET
,
47 "getsockopt of SO_SNDBUF failed %s\n",
48 safe_strerror(errno
));
53 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &size
, sizeof(size
))
55 flog_err_sys(LIB_ERR_SOCKET
,
56 "Couldn't increase send buffer: %s\n",
57 safe_strerror(errno
));
62 /* passive peer socket accept */
63 static int pim_msdp_sock_accept(struct thread
*thread
)
66 struct pim_instance
*pim
= THREAD_ARG(thread
);
69 struct pim_msdp_peer
*mp
;
70 char buf
[SU_ADDRSTRLEN
];
74 /* re-register accept thread */
75 accept_sock
= THREAD_FD(thread
);
76 if (accept_sock
< 0) {
77 flog_err(LIB_ERR_DEVELOPMENT
,
78 "accept_sock is negative value %d", accept_sock
);
81 pim
->msdp
.listener
.thread
= NULL
;
82 thread_add_read(master
, pim_msdp_sock_accept
, pim
, accept_sock
,
83 &pim
->msdp
.listener
.thread
);
85 /* accept client connection. */
86 msdp_sock
= sockunion_accept(accept_sock
, &su
);
88 flog_err_sys(LIB_ERR_SOCKET
, "pim_msdp_sock_accept failed (%s)",
89 safe_strerror(errno
));
93 /* see if have peer config for this */
94 mp
= pim_msdp_peer_find(pim
, su
.sin
.sin_addr
);
95 if (!mp
|| !PIM_MSDP_PEER_IS_LISTENER(mp
)) {
96 ++pim
->msdp
.rejected_accepts
;
97 if (PIM_DEBUG_MSDP_EVENTS
) {
98 flog_err(PIM_ERR_MSDP_PACKET
,
99 "msdp peer connection refused from %s",
100 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
106 if (PIM_DEBUG_MSDP_INTERNAL
) {
107 zlog_debug("MSDP peer %s accept success%s", mp
->key_str
,
108 mp
->fd
>= 0 ? "(dup)" : "");
111 /* if we have an existing connection we need to kill that one
114 if (PIM_DEBUG_MSDP_EVENTS
) {
116 "msdp peer new connection from %s stop old connection",
117 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
119 pim_msdp_peer_stop_tcp_conn(mp
, true /* chg_state */);
122 set_nonblocking(mp
->fd
);
123 pim_msdp_update_sock_send_buffer_size(mp
->fd
);
124 pim_msdp_peer_established(mp
);
128 /* global listener for the MSDP well know TCP port */
129 int pim_msdp_sock_listen(struct pim_instance
*pim
)
133 struct sockaddr_in sin
;
135 struct pim_msdp_listener
*listener
= &pim
->msdp
.listener
;
137 if (pim
->msdp
.flags
& PIM_MSDPF_LISTENER
) {
138 /* listener already setup */
142 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
144 flog_err_sys(LIB_ERR_SOCKET
, "socket: %s",
145 safe_strerror(errno
));
149 memset(&sin
, 0, sizeof(struct sockaddr_in
));
150 sin
.sin_family
= AF_INET
;
151 sin
.sin_port
= htons(PIM_MSDP_TCP_PORT
);
152 socklen
= sizeof(struct sockaddr_in
);
153 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
154 sin
.sin_len
= socklen
;
155 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
157 sockopt_reuseaddr(sock
);
158 sockopt_reuseport(sock
);
160 if (pim
->vrf_id
!= VRF_DEFAULT
) {
161 struct interface
*ifp
=
162 if_lookup_by_name(pim
->vrf
->name
, pim
->vrf_id
);
164 flog_err(LIB_ERR_INTERFACE
,
165 "%s: Unable to lookup vrf interface: %s",
166 __PRETTY_FUNCTION__
, pim
->vrf
->name
);
170 if (pim_socket_bind(sock
, ifp
)) {
171 flog_err_sys(LIB_ERR_SOCKET
,
172 "%s: Unable to bind to socket: %s",
173 __PRETTY_FUNCTION__
, safe_strerror(errno
));
179 frr_elevate_privs(&pimd_privs
) {
180 /* bind to well known TCP port */
181 rc
= bind(sock
, (struct sockaddr
*)&sin
, socklen
);
185 flog_err_sys(LIB_ERR_SOCKET
,
186 "pim_msdp_socket bind to port %d: %s",
187 ntohs(sin
.sin_port
), safe_strerror(errno
));
192 rc
= listen(sock
, 3 /* backlog */);
194 flog_err_sys(LIB_ERR_SOCKET
, "pim_msdp_socket listen: %s",
195 safe_strerror(errno
));
200 /* add accept thread */
202 memcpy(&listener
->su
, &sin
, socklen
);
203 listener
->thread
= NULL
;
204 thread_add_read(pim
->msdp
.master
, pim_msdp_sock_accept
, pim
, sock
,
207 pim
->msdp
.flags
|= PIM_MSDPF_LISTENER
;
211 /* active peer socket setup */
212 int pim_msdp_sock_connect(struct pim_msdp_peer
*mp
)
216 if (PIM_DEBUG_MSDP_INTERNAL
) {
217 zlog_debug("MSDP peer %s attempt connect%s", mp
->key_str
,
218 mp
->fd
< 0 ? "" : "(dup)");
221 /* if we have an existing connection we need to kill that one
224 if (PIM_DEBUG_MSDP_EVENTS
) {
226 "msdp duplicate connect to %s nuke old connection",
229 pim_msdp_peer_stop_tcp_conn(mp
, false /* chg_state */);
232 /* Make socket for the peer. */
233 mp
->fd
= sockunion_socket(&mp
->su_peer
);
235 flog_err_sys(LIB_ERR_SOCKET
,
236 "pim_msdp_socket socket failure: %s",
237 safe_strerror(errno
));
241 if (mp
->pim
->vrf_id
!= VRF_DEFAULT
) {
242 struct interface
*ifp
=
243 if_lookup_by_name(mp
->pim
->vrf
->name
, mp
->pim
->vrf_id
);
245 flog_err(LIB_ERR_INTERFACE
,
246 "%s: Unable to lookup vrf interface: %s",
247 __PRETTY_FUNCTION__
, mp
->pim
->vrf
->name
);
250 if (pim_socket_bind(mp
->fd
, ifp
)) {
251 flog_err_sys(LIB_ERR_SOCKET
,
252 "%s: Unable to bind to socket: %s",
253 __PRETTY_FUNCTION__
, safe_strerror(errno
));
260 set_nonblocking(mp
->fd
);
262 /* Set socket send buffer size */
263 pim_msdp_update_sock_send_buffer_size(mp
->fd
);
264 sockopt_reuseaddr(mp
->fd
);
265 sockopt_reuseport(mp
->fd
);
268 rc
= sockunion_bind(mp
->fd
, &mp
->su_local
, 0, &mp
->su_local
);
270 flog_err_sys(LIB_ERR_SOCKET
,
271 "pim_msdp_socket connect bind failure: %s",
272 safe_strerror(errno
));
278 /* Connect to the remote mp. */
279 return (sockunion_connect(mp
->fd
, &mp
->su_peer
,
280 htons(PIM_MSDP_TCP_PORT
), 0));