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>
34 #include "pim_msdp_socket.h"
36 /* increase socket send buffer size */
37 static void pim_msdp_update_sock_send_buffer_size(int fd
)
39 int size
= PIM_MSDP_SOCKET_SNDBUF_SIZE
;
41 socklen_t optlen
= sizeof(optval
);
43 if (getsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &optval
, &optlen
) < 0) {
44 zlog_err("getsockopt of SO_SNDBUF failed %s\n",
45 safe_strerror(errno
));
50 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &size
, sizeof(size
))
52 zlog_err("Couldn't increase send buffer: %s\n",
53 safe_strerror(errno
));
58 /* passive peer socket accept */
59 static int pim_msdp_sock_accept(struct thread
*thread
)
62 struct pim_instance
*pim
= THREAD_ARG(thread
);
65 struct pim_msdp_peer
*mp
;
66 char buf
[SU_ADDRSTRLEN
];
70 /* re-register accept thread */
71 accept_sock
= THREAD_FD(thread
);
72 if (accept_sock
< 0) {
73 zlog_err("accept_sock is negative value %d", accept_sock
);
76 pim
->msdp
.listener
.thread
= NULL
;
77 thread_add_read(master
, pim_msdp_sock_accept
, pim
, accept_sock
,
78 &pim
->msdp
.listener
.thread
);
80 /* accept client connection. */
81 msdp_sock
= sockunion_accept(accept_sock
, &su
);
83 zlog_err("pim_msdp_sock_accept failed (%s)",
84 safe_strerror(errno
));
88 /* see if have peer config for this */
89 mp
= pim_msdp_peer_find(pim
, su
.sin
.sin_addr
);
90 if (!mp
|| !PIM_MSDP_PEER_IS_LISTENER(mp
)) {
91 ++pim
->msdp
.rejected_accepts
;
92 if (PIM_DEBUG_MSDP_EVENTS
) {
93 zlog_err("msdp peer connection refused from %s",
94 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
100 if (PIM_DEBUG_MSDP_INTERNAL
) {
101 zlog_debug("MSDP peer %s accept success%s", mp
->key_str
,
102 mp
->fd
>= 0 ? "(dup)" : "");
105 /* if we have an existing connection we need to kill that one
108 if (PIM_DEBUG_MSDP_EVENTS
) {
110 "msdp peer new connection from %s stop old connection",
111 sockunion2str(&su
, buf
, SU_ADDRSTRLEN
));
113 pim_msdp_peer_stop_tcp_conn(mp
, true /* chg_state */);
116 set_nonblocking(mp
->fd
);
117 pim_msdp_update_sock_send_buffer_size(mp
->fd
);
118 pim_msdp_peer_established(mp
);
122 /* global listener for the MSDP well know TCP port */
123 int pim_msdp_sock_listen(struct pim_instance
*pim
)
127 struct sockaddr_in sin
;
129 struct pim_msdp_listener
*listener
= &pim
->msdp
.listener
;
131 if (pim
->msdp
.flags
& PIM_MSDPF_LISTENER
) {
132 /* listener already setup */
136 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
138 zlog_err("socket: %s", safe_strerror(errno
));
142 memset(&sin
, 0, sizeof(struct sockaddr_in
));
143 sin
.sin_family
= AF_INET
;
144 sin
.sin_port
= htons(PIM_MSDP_TCP_PORT
);
145 socklen
= sizeof(struct sockaddr_in
);
146 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
147 sin
.sin_len
= socklen
;
148 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
150 sockopt_reuseaddr(sock
);
151 sockopt_reuseport(sock
);
153 if (pim
->vrf_id
!= VRF_DEFAULT
) {
154 struct interface
*ifp
=
155 if_lookup_by_name(pim
->vrf
->name
, pim
->vrf_id
);
157 zlog_err("%s: Unable to lookup vrf interface: %s",
158 __PRETTY_FUNCTION__
, pim
->vrf
->name
);
162 if (pim_socket_bind(sock
, ifp
)) {
163 zlog_err("%s: Unable to bind to socket: %s",
164 __PRETTY_FUNCTION__
, safe_strerror(errno
));
170 if (pimd_privs
.change(ZPRIVS_RAISE
)) {
171 zlog_err("pim_msdp_socket: could not raise privs, %s",
172 safe_strerror(errno
));
175 /* bind to well known TCP port */
176 rc
= bind(sock
, (struct sockaddr
*)&sin
, socklen
);
178 if (pimd_privs
.change(ZPRIVS_LOWER
)) {
179 zlog_err("pim_msdp_socket: could not lower privs, %s",
180 safe_strerror(errno
));
184 zlog_err("pim_msdp_socket bind to port %d: %s",
185 ntohs(sin
.sin_port
), safe_strerror(errno
));
190 rc
= listen(sock
, 3 /* backlog */);
192 zlog_err("pim_msdp_socket listen: %s", safe_strerror(errno
));
197 /* add accept thread */
199 memcpy(&listener
->su
, &sin
, socklen
);
200 listener
->thread
= NULL
;
201 thread_add_read(pim
->msdp
.master
, pim_msdp_sock_accept
, pim
, sock
,
204 pim
->msdp
.flags
|= PIM_MSDPF_LISTENER
;
208 /* active peer socket setup */
209 int pim_msdp_sock_connect(struct pim_msdp_peer
*mp
)
213 if (PIM_DEBUG_MSDP_INTERNAL
) {
214 zlog_debug("MSDP peer %s attempt connect%s", mp
->key_str
,
215 mp
->fd
< 0 ? "" : "(dup)");
218 /* if we have an existing connection we need to kill that one
221 if (PIM_DEBUG_MSDP_EVENTS
) {
223 "msdp duplicate connect to %s nuke old connection",
226 pim_msdp_peer_stop_tcp_conn(mp
, false /* chg_state */);
229 /* Make socket for the peer. */
230 mp
->fd
= sockunion_socket(&mp
->su_peer
);
232 zlog_err("pim_msdp_socket socket failure: %s",
233 safe_strerror(errno
));
237 if (mp
->pim
->vrf_id
!= VRF_DEFAULT
) {
238 struct interface
*ifp
=
239 if_lookup_by_name(mp
->pim
->vrf
->name
, mp
->pim
->vrf_id
);
241 zlog_err("%s: Unable to lookup vrf interface: %s",
242 __PRETTY_FUNCTION__
, mp
->pim
->vrf
->name
);
245 if (pim_socket_bind(mp
->fd
, ifp
)) {
246 zlog_err("%s: Unable to bind to socket: %s",
247 __PRETTY_FUNCTION__
, safe_strerror(errno
));
254 set_nonblocking(mp
->fd
);
256 /* Set socket send buffer size */
257 pim_msdp_update_sock_send_buffer_size(mp
->fd
);
258 sockopt_reuseaddr(mp
->fd
);
259 sockopt_reuseport(mp
->fd
);
262 rc
= sockunion_bind(mp
->fd
, &mp
->su_local
, 0, &mp
->su_local
);
264 zlog_err("pim_msdp_socket connect bind failure: %s",
265 safe_strerror(errno
));
271 /* Connect to the remote mp. */
272 return (sockunion_connect(mp
->fd
, &mp
->su_peer
,
273 htons(PIM_MSDP_TCP_PORT
), 0));