]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_msdp_socket.c
pimd: Use builtin printf functionality
[mirror_frr.git] / pimd / pim_msdp_socket.c
CommitLineData
2a333e0f 1/*
2 * IP MSDP socket management
3 * Copyright (C) 2016 Cumulus Networks, Inc.
4 *
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.
9 *
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.
14 *
896014f4
DL
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
2a333e0f 18 */
19
20#include <zebra.h>
21
22#include <lib/log.h>
23#include <lib/network.h>
2a333e0f 24#include <lib/sockunion.h>
3c72d654 25#include <lib/thread.h>
26#include <lib/vty.h>
62fde409
DS
27#include <lib/if.h>
28#include <lib/vrf.h>
3613d898 29#include <lib/lib_errors.h>
2a333e0f 30
31#include "pimd.h"
62fde409 32#include "pim_sock.h"
d9ff4302 33#include "pim_errors.h"
2a333e0f 34
35#include "pim_msdp.h"
36#include "pim_msdp_socket.h"
37
2a333e0f 38/* increase socket send buffer size */
d62a17ae 39static void pim_msdp_update_sock_send_buffer_size(int fd)
2a333e0f 40{
d62a17ae 41 int size = PIM_MSDP_SOCKET_SNDBUF_SIZE;
42 int optval;
43 socklen_t optlen = sizeof(optval);
44
45 if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optval, &optlen) < 0) {
450971aa 46 flog_err_sys(EC_LIB_SOCKET,
1d5453d6 47 "getsockopt of SO_SNDBUF failed %s",
09c866e3 48 safe_strerror(errno));
d62a17ae 49 return;
50 }
51
52 if (optval < size) {
53 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size))
54 < 0) {
450971aa 55 flog_err_sys(EC_LIB_SOCKET,
1d5453d6 56 "Couldn't increase send buffer: %s",
09c866e3 57 safe_strerror(errno));
d62a17ae 58 }
59 }
2a333e0f 60}
61
62/* passive peer socket accept */
d62a17ae 63static int pim_msdp_sock_accept(struct thread *thread)
2a333e0f 64{
d62a17ae 65 union sockunion su;
472ad383 66 struct pim_instance *pim = THREAD_ARG(thread);
d62a17ae 67 int accept_sock;
68 int msdp_sock;
69 struct pim_msdp_peer *mp;
d62a17ae 70
71 sockunion_init(&su);
72
73 /* re-register accept thread */
74 accept_sock = THREAD_FD(thread);
75 if (accept_sock < 0) {
1c50c1c0
QY
76 flog_err(EC_LIB_DEVELOPMENT, "accept_sock is negative value %d",
77 accept_sock);
d62a17ae 78 return -1;
79 }
472ad383 80 pim->msdp.listener.thread = NULL;
36417fcc 81 thread_add_read(router->master, pim_msdp_sock_accept, pim, accept_sock,
472ad383 82 &pim->msdp.listener.thread);
d62a17ae 83
84 /* accept client connection. */
85 msdp_sock = sockunion_accept(accept_sock, &su);
86 if (msdp_sock < 0) {
450971aa 87 flog_err_sys(EC_LIB_SOCKET, "pim_msdp_sock_accept failed (%s)",
09c866e3 88 safe_strerror(errno));
d62a17ae 89 return -1;
90 }
91
92 /* see if have peer config for this */
472ad383 93 mp = pim_msdp_peer_find(pim, su.sin.sin_addr);
d62a17ae 94 if (!mp || !PIM_MSDP_PEER_IS_LISTENER(mp)) {
472ad383 95 ++pim->msdp.rejected_accepts;
d62a17ae 96 if (PIM_DEBUG_MSDP_EVENTS) {
298004a1 97 flog_err(EC_PIM_MSDP_PACKET,
6b73800b 98 "msdp peer connection refused from %pSU", &su);
d62a17ae 99 }
100 close(msdp_sock);
101 return -1;
102 }
103
104 if (PIM_DEBUG_MSDP_INTERNAL) {
105 zlog_debug("MSDP peer %s accept success%s", mp->key_str,
106 mp->fd >= 0 ? "(dup)" : "");
107 }
108
109 /* if we have an existing connection we need to kill that one
110 * with this one */
111 if (mp->fd >= 0) {
112 if (PIM_DEBUG_MSDP_EVENTS) {
3613d898 113 zlog_notice(
6b73800b
DS
114 "msdp peer new connection from %pSU stop old connection",
115 &su);
d62a17ae 116 }
117 pim_msdp_peer_stop_tcp_conn(mp, true /* chg_state */);
118 }
119 mp->fd = msdp_sock;
120 set_nonblocking(mp->fd);
121 pim_msdp_update_sock_send_buffer_size(mp->fd);
122 pim_msdp_peer_established(mp);
123 return 0;
2a333e0f 124}
125
126/* global listener for the MSDP well know TCP port */
472ad383 127int pim_msdp_sock_listen(struct pim_instance *pim)
2a333e0f 128{
d62a17ae 129 int sock;
130 int socklen;
131 struct sockaddr_in sin;
132 int rc;
472ad383 133 struct pim_msdp_listener *listener = &pim->msdp.listener;
d62a17ae 134
472ad383 135 if (pim->msdp.flags & PIM_MSDPF_LISTENER) {
d62a17ae 136 /* listener already setup */
137 return 0;
138 }
139
140 sock = socket(AF_INET, SOCK_STREAM, 0);
141 if (sock < 0) {
1c50c1c0 142 flog_err_sys(EC_LIB_SOCKET, "socket: %s", safe_strerror(errno));
d62a17ae 143 return sock;
144 }
145
146 memset(&sin, 0, sizeof(struct sockaddr_in));
147 sin.sin_family = AF_INET;
148 sin.sin_port = htons(PIM_MSDP_TCP_PORT);
149 socklen = sizeof(struct sockaddr_in);
2a333e0f 150#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 151 sin.sin_len = socklen;
2a333e0f 152#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
153
d62a17ae 154 sockopt_reuseaddr(sock);
155 sockopt_reuseport(sock);
156
2267994c
DS
157 if (pim->vrf_id != VRF_DEFAULT) {
158 struct interface *ifp =
a36898e7 159 if_lookup_by_name(pim->vrf->name, pim->vrf_id);
449a3f5b 160 if (!ifp) {
450971aa 161 flog_err(EC_LIB_INTERFACE,
1c50c1c0 162 "%s: Unable to lookup vrf interface: %s",
5e81f5dd 163 __func__, pim->vrf->name);
e691f179
DS
164 close(sock);
165 return -1;
166 }
167 if (pim_socket_bind(sock, ifp)) {
450971aa 168 flog_err_sys(EC_LIB_SOCKET,
09c866e3 169 "%s: Unable to bind to socket: %s",
5e81f5dd 170 __func__, safe_strerror(errno));
e691f179 171 close(sock);
449a3f5b
DS
172 return -1;
173 }
2267994c
DS
174 }
175
0cf6db21 176 frr_with_privs(&pimd_privs) {
6bb30c2c
DL
177 /* bind to well known TCP port */
178 rc = bind(sock, (struct sockaddr *)&sin, socklen);
d62a17ae 179 }
180
181 if (rc < 0) {
450971aa 182 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
183 "pim_msdp_socket bind to port %d: %s",
184 ntohs(sin.sin_port), safe_strerror(errno));
d62a17ae 185 close(sock);
186 return rc;
187 }
188
189 rc = listen(sock, 3 /* backlog */);
190 if (rc < 0) {
450971aa 191 flog_err_sys(EC_LIB_SOCKET, "pim_msdp_socket listen: %s",
09c866e3 192 safe_strerror(errno));
d62a17ae 193 close(sock);
194 return rc;
195 }
196
197 /* add accept thread */
198 listener->fd = sock;
199 memcpy(&listener->su, &sin, socklen);
200 listener->thread = NULL;
472ad383 201 thread_add_read(pim->msdp.master, pim_msdp_sock_accept, pim, sock,
d62a17ae 202 &listener->thread);
203
472ad383 204 pim->msdp.flags |= PIM_MSDPF_LISTENER;
d62a17ae 205 return 0;
2a333e0f 206}
207
208/* active peer socket setup */
d62a17ae 209int pim_msdp_sock_connect(struct pim_msdp_peer *mp)
2a333e0f 210{
d62a17ae 211 int rc;
212
213 if (PIM_DEBUG_MSDP_INTERNAL) {
214 zlog_debug("MSDP peer %s attempt connect%s", mp->key_str,
215 mp->fd < 0 ? "" : "(dup)");
216 }
217
218 /* if we have an existing connection we need to kill that one
219 * with this one */
220 if (mp->fd >= 0) {
221 if (PIM_DEBUG_MSDP_EVENTS) {
3613d898 222 zlog_notice(
d62a17ae 223 "msdp duplicate connect to %s nuke old connection",
224 mp->key_str);
225 }
226 pim_msdp_peer_stop_tcp_conn(mp, false /* chg_state */);
227 }
228
229 /* Make socket for the peer. */
230 mp->fd = sockunion_socket(&mp->su_peer);
231 if (mp->fd < 0) {
450971aa 232 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
233 "pim_msdp_socket socket failure: %s",
234 safe_strerror(errno));
d62a17ae 235 return -1;
236 }
237
62fde409
DS
238 if (mp->pim->vrf_id != VRF_DEFAULT) {
239 struct interface *ifp =
a36898e7 240 if_lookup_by_name(mp->pim->vrf->name, mp->pim->vrf_id);
449a3f5b 241 if (!ifp) {
450971aa 242 flog_err(EC_LIB_INTERFACE,
1c50c1c0 243 "%s: Unable to lookup vrf interface: %s",
15569c58 244 __func__, mp->pim->vrf->name);
449a3f5b
DS
245 return -1;
246 }
e691f179 247 if (pim_socket_bind(mp->fd, ifp)) {
450971aa 248 flog_err_sys(EC_LIB_SOCKET,
09c866e3 249 "%s: Unable to bind to socket: %s",
15569c58 250 __func__, safe_strerror(errno));
e691f179
DS
251 close(mp->fd);
252 mp->fd = -1;
253 return -1;
254 }
62fde409
DS
255 }
256
d62a17ae 257 set_nonblocking(mp->fd);
258
259 /* Set socket send buffer size */
260 pim_msdp_update_sock_send_buffer_size(mp->fd);
261 sockopt_reuseaddr(mp->fd);
262 sockopt_reuseport(mp->fd);
263
264 /* source bind */
265 rc = sockunion_bind(mp->fd, &mp->su_local, 0, &mp->su_local);
266 if (rc < 0) {
450971aa 267 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
268 "pim_msdp_socket connect bind failure: %s",
269 safe_strerror(errno));
d62a17ae 270 close(mp->fd);
271 mp->fd = -1;
272 return rc;
273 }
274
275 /* Connect to the remote mp. */
276 return (sockunion_connect(mp->fd, &mp->su_peer,
277 htons(PIM_MSDP_TCP_PORT), 0));
2a333e0f 278}