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