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