blob: c06a28149e79b929dbdd180c97788e82b9ddbc02 [file] [log] [blame]
Willy Tarreau18b7df72020-08-28 12:07:22 +02001/*
2 * Generic code for native (BSD-compatible) sockets
3 *
4 * Copyright 2000-2020 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20
21#include <sys/param.h>
22#include <sys/socket.h>
23#include <sys/types.h>
24
Willy Tarreau42961742020-08-28 18:42:45 +020025#include <net/if.h>
26
Willy Tarreau18b7df72020-08-28 12:07:22 +020027#include <haproxy/api.h>
28#include <haproxy/connection.h>
Willy Tarreau063d47d2020-08-28 16:29:53 +020029#include <haproxy/listener-t.h>
Willy Tarreau18b7df72020-08-28 12:07:22 +020030#include <haproxy/namespace.h>
31#include <haproxy/sock.h>
Willy Tarreau2d34a712020-08-28 16:49:41 +020032#include <haproxy/sock_inet.h>
Willy Tarreau18b7df72020-08-28 12:07:22 +020033#include <haproxy/tools.h>
34
Willy Tarreau063d47d2020-08-28 16:29:53 +020035/* the list of remaining sockets transferred from an older process */
36struct xfer_sock_list *xfer_sock_list = NULL;
Willy Tarreau18b7df72020-08-28 12:07:22 +020037
38/* Create a socket to connect to the server in conn->dst (which MUST be valid),
39 * using the configured namespace if needed, or the one passed by the proxy
40 * protocol if required to do so. It ultimately calls socket() or socketat()
41 * and returns the FD or error code.
42 */
43int sock_create_server_socket(struct connection *conn)
44{
45 const struct netns_entry *ns = NULL;
46
47#ifdef USE_NS
48 if (objt_server(conn->target)) {
49 if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP)
50 ns = conn->proxy_netns;
51 else
52 ns = __objt_server(conn->target)->netns;
53 }
54#endif
55 return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, 0);
56}
57
Willy Tarreaue70c7972020-09-25 19:00:01 +020058/* Enables receiving on receiver <rx> once already bound. Does nothing in early
59 * boot (needs fd_updt).
60 */
61void sock_enable(struct receiver *rx)
62{
63 if (rx->flags & RX_F_BOUND && fd_updt)
64 fd_want_recv(rx->fd);
65}
66
67/* Disables receiving on receiver <rx> once already bound. Does nothing in early
68 * boot (needs fd_updt).
69 */
70void sock_disable(struct receiver *rx)
71{
72 if (rx->flags & RX_F_BOUND && fd_updt)
73 fd_stop_recv(rx->fd);
74}
75
Willy Tarreau18b7df72020-08-28 12:07:22 +020076/*
77 * Retrieves the source address for the socket <fd>, with <dir> indicating
78 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
79 * success, -1 in case of error. The socket's source address is stored in
80 * <sa> for <salen> bytes.
81 */
82int sock_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
83{
84 if (dir)
85 return getsockname(fd, sa, &salen);
86 else
87 return getpeername(fd, sa, &salen);
88}
89
90/*
91 * Retrieves the original destination address for the socket <fd>, with <dir>
92 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
93 * case of success, -1 in case of error. The socket's source address is stored
94 * in <sa> for <salen> bytes.
95 */
96int sock_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
97{
98 if (dir)
99 return getpeername(fd, sa, &salen);
100 else
101 return getsockname(fd, sa, &salen);
102}
103
Willy Tarreau42961742020-08-28 18:42:45 +0200104/* Try to retrieve exported sockets from worker at CLI <unixsocket>. These
105 * ones will be placed into the xfer_sock_list for later use by function
106 * sock_find_compatible_fd(). Returns 0 on success, -1 on failure.
107 */
108int sock_get_old_sockets(const char *unixsocket)
109{
110 char *cmsgbuf = NULL, *tmpbuf = NULL;
111 int *tmpfd = NULL;
112 struct sockaddr_un addr;
113 struct cmsghdr *cmsg;
114 struct msghdr msghdr;
115 struct iovec iov;
116 struct xfer_sock_list *xfer_sock = NULL;
117 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
118 int sock = -1;
119 int ret = -1;
120 int ret2 = -1;
121 int fd_nb;
122 int got_fd = 0;
123 int cur_fd = 0;
124 size_t maxoff = 0, curoff = 0;
125
126 memset(&msghdr, 0, sizeof(msghdr));
127 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
128 if (!cmsgbuf) {
129 ha_warning("Failed to allocate memory to send sockets\n");
130 goto out;
131 }
132
133 sock = socket(PF_UNIX, SOCK_STREAM, 0);
134 if (sock < 0) {
135 ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket);
136 goto out;
137 }
138
139 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1);
140 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
141 addr.sun_family = PF_UNIX;
142
143 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
144 if (ret < 0) {
145 ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket);
146 goto out;
147 }
148
149 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
150 iov.iov_base = &fd_nb;
151 iov.iov_len = sizeof(fd_nb);
152 msghdr.msg_iov = &iov;
153 msghdr.msg_iovlen = 1;
154
155 if (send(sock, "_getsocks\n", strlen("_getsocks\n"), 0) != strlen("_getsocks\n")) {
156 ha_warning("Failed to get the number of sockets to be transferred !\n");
157 goto out;
158 }
159
160 /* First, get the number of file descriptors to be received */
161 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
162 ha_warning("Failed to get the number of sockets to be transferred !\n");
163 goto out;
164 }
165
166 if (fd_nb == 0) {
167 ret2 = 0;
168 goto out;
169 }
170
171 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
172 if (tmpbuf == NULL) {
173 ha_warning("Failed to allocate memory while receiving sockets\n");
174 goto out;
175 }
176
177 tmpfd = malloc(fd_nb * sizeof(int));
178 if (tmpfd == NULL) {
179 ha_warning("Failed to allocate memory while receiving sockets\n");
180 goto out;
181 }
182
183 msghdr.msg_control = cmsgbuf;
184 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
185 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
186
187 do {
188 int ret3;
189
190 iov.iov_base = tmpbuf + curoff;
191
192 ret = recvmsg(sock, &msghdr, 0);
193
194 if (ret == -1 && errno == EINTR)
195 continue;
196
197 if (ret <= 0)
198 break;
199
200 /* Send an ack to let the sender know we got the sockets
201 * and it can send some more
202 */
203 do {
204 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
205 } while (ret3 == -1 && errno == EINTR);
206
207 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
208 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
209 size_t totlen = cmsg->cmsg_len - CMSG_LEN(0);
210
211 if (totlen / sizeof(int) + got_fd > fd_nb) {
212 ha_warning("Got to many sockets !\n");
213 goto out;
214 }
215
216 /*
217 * Be paranoid and use memcpy() to avoid any
218 * potential alignment issue.
219 */
220 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
221 got_fd += totlen / sizeof(int);
222 }
223 }
224 curoff += ret;
225 } while (got_fd < fd_nb);
226
227 if (got_fd != fd_nb) {
228 ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
229 fd_nb, got_fd);
230 goto out;
231 }
232
233 maxoff = curoff;
234 curoff = 0;
235
236 for (cur_fd = 0; cur_fd < got_fd; cur_fd++) {
237 int fd = tmpfd[cur_fd];
238 socklen_t socklen;
239 int val;
240 int len;
241
242 xfer_sock = calloc(1, sizeof(*xfer_sock));
243 if (!xfer_sock) {
244 ha_warning("Failed to allocate memory in get_old_sockets() !\n");
245 break;
246 }
247 xfer_sock->fd = -1;
248
249 socklen = sizeof(xfer_sock->addr);
250 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
251 ha_warning("Failed to get socket address\n");
252 free(xfer_sock);
253 xfer_sock = NULL;
254 continue;
255 }
256
257 if (curoff >= maxoff) {
258 ha_warning("Inconsistency while transferring sockets\n");
259 goto out;
260 }
261
262 len = tmpbuf[curoff++];
263 if (len > 0) {
264 /* We have a namespace */
265 if (curoff + len > maxoff) {
266 ha_warning("Inconsistency while transferring sockets\n");
267 goto out;
268 }
269 xfer_sock->namespace = malloc(len + 1);
270 if (!xfer_sock->namespace) {
271 ha_warning("Failed to allocate memory while transferring sockets\n");
272 goto out;
273 }
274 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
275 xfer_sock->namespace[len] = 0;
276 xfer_sock->ns_namelen = len;
277 curoff += len;
278 }
279
280 if (curoff >= maxoff) {
281 ha_warning("Inconsistency while transferring sockets\n");
282 goto out;
283 }
284
285 len = tmpbuf[curoff++];
286 if (len > 0) {
287 /* We have an interface */
288 if (curoff + len > maxoff) {
289 ha_warning("Inconsistency while transferring sockets\n");
290 goto out;
291 }
292 xfer_sock->iface = malloc(len + 1);
293 if (!xfer_sock->iface) {
294 ha_warning("Failed to allocate memory while transferring sockets\n");
295 goto out;
296 }
297 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
298 xfer_sock->iface[len] = 0;
299 xfer_sock->if_namelen = len;
300 curoff += len;
301 }
302
303 if (curoff + sizeof(int) > maxoff) {
304 ha_warning("Inconsistency while transferring sockets\n");
305 goto out;
306 }
307
308 /* we used to have 32 bits of listener options here but we don't
309 * use them anymore.
310 */
311 curoff += sizeof(int);
312
313 /* determine the foreign status directly from the socket itself */
314 if (sock_inet_is_foreign(fd, xfer_sock->addr.ss_family))
Willy Tarreaua2c17872020-08-28 19:09:19 +0200315 xfer_sock->options |= SOCK_XFER_OPT_FOREIGN;
Willy Tarreau42961742020-08-28 18:42:45 +0200316
Willy Tarreau9dbb6c42020-08-28 19:20:23 +0200317 socklen = sizeof(val);
318 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &socklen) == 0 && val == SOCK_DGRAM)
319 xfer_sock->options |= SOCK_XFER_OPT_DGRAM;
320
Willy Tarreau42961742020-08-28 18:42:45 +0200321#if defined(IPV6_V6ONLY)
322 /* keep only the v6only flag depending on what's currently
323 * active on the socket, and always drop the v4v6 one.
324 */
325 socklen = sizeof(val);
326 if (xfer_sock->addr.ss_family == AF_INET6 &&
327 getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &socklen) == 0 && val > 0)
Willy Tarreaua2c17872020-08-28 19:09:19 +0200328 xfer_sock->options |= SOCK_XFER_OPT_V6ONLY;
Willy Tarreau42961742020-08-28 18:42:45 +0200329#endif
330
331 xfer_sock->fd = fd;
332 if (xfer_sock_list)
333 xfer_sock_list->prev = xfer_sock;
334 xfer_sock->next = xfer_sock_list;
335 xfer_sock->prev = NULL;
336 xfer_sock_list = xfer_sock;
337 xfer_sock = NULL;
338 }
339
340 ret2 = 0;
341out:
342 /* If we failed midway make sure to close the remaining
343 * file descriptors
344 */
345 if (tmpfd != NULL && cur_fd < got_fd) {
346 for (; cur_fd < got_fd; cur_fd++) {
347 close(tmpfd[cur_fd]);
348 }
349 }
350
351 free(tmpbuf);
352 free(tmpfd);
353 free(cmsgbuf);
354
355 if (sock != -1)
356 close(sock);
357
358 if (xfer_sock) {
359 free(xfer_sock->namespace);
360 free(xfer_sock->iface);
361 if (xfer_sock->fd != -1)
362 close(xfer_sock->fd);
363 free(xfer_sock);
364 }
365 return (ret2);
366}
367
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200368/* When binding the receivers, check if a socket has been sent to us by the
Willy Tarreau2d34a712020-08-28 16:49:41 +0200369 * previous process that we could reuse, instead of creating a new one. Note
370 * that some address family-specific options are checked on the listener and
371 * on the socket. Typically for AF_INET and AF_INET6, we check for transparent
372 * mode, and for AF_INET6 we also check for "v4v6" or "v6only". The reused
373 * socket is automatically removed from the list so that it's not proposed
374 * anymore.
375 */
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200376int sock_find_compatible_fd(const struct receiver *rx)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200377{
378 struct xfer_sock_list *xfer_sock = xfer_sock_list;
Willy Tarreaua2c17872020-08-28 19:09:19 +0200379 int options = 0;
Willy Tarreau2d34a712020-08-28 16:49:41 +0200380 int if_namelen = 0;
381 int ns_namelen = 0;
382 int ret = -1;
383
Willy Tarreauf1f66092020-09-04 08:15:31 +0200384 if (!rx->proto->fam->addrcmp)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200385 return -1;
386
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200387 if (rx->proto->sock_type == SOCK_DGRAM)
Willy Tarreau9dbb6c42020-08-28 19:20:23 +0200388 options |= SOCK_XFER_OPT_DGRAM;
389
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200390 if (rx->settings->options & RX_O_FOREIGN)
Willy Tarreaua2c17872020-08-28 19:09:19 +0200391 options |= SOCK_XFER_OPT_FOREIGN;
392
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200393 if (rx->addr.ss_family == AF_INET6) {
Willy Tarreau2d34a712020-08-28 16:49:41 +0200394 /* Prepare to match the v6only option against what we really want. Note
395 * that sadly the two options are not exclusive to each other and that
396 * v6only is stronger than v4v6.
397 */
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200398 if ((rx->settings->options & RX_O_V6ONLY) ||
399 (sock_inet6_v6only_default && !(rx->settings->options & RX_O_V4V6)))
Willy Tarreaua2c17872020-08-28 19:09:19 +0200400 options |= SOCK_XFER_OPT_V6ONLY;
Willy Tarreau2d34a712020-08-28 16:49:41 +0200401 }
Willy Tarreau2d34a712020-08-28 16:49:41 +0200402
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200403 if (rx->settings->interface)
404 if_namelen = strlen(rx->settings->interface);
Willy Tarreau2d34a712020-08-28 16:49:41 +0200405#ifdef USE_NS
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200406 if (rx->settings->netns)
407 ns_namelen = rx->settings->netns->name_len;
Willy Tarreau2d34a712020-08-28 16:49:41 +0200408#endif
409
410 while (xfer_sock) {
Willy Tarreaua2c17872020-08-28 19:09:19 +0200411 if ((options == xfer_sock->options) &&
Willy Tarreau2d34a712020-08-28 16:49:41 +0200412 (if_namelen == xfer_sock->if_namelen) &&
413 (ns_namelen == xfer_sock->ns_namelen) &&
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200414 (!if_namelen || strcmp(rx->settings->interface, xfer_sock->iface) == 0) &&
Willy Tarreau2d34a712020-08-28 16:49:41 +0200415#ifdef USE_NS
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200416 (!ns_namelen || strcmp(rx->settings->netns->node.key, xfer_sock->namespace) == 0) &&
Willy Tarreau2d34a712020-08-28 16:49:41 +0200417#endif
Willy Tarreauf1f66092020-09-04 08:15:31 +0200418 rx->proto->fam->addrcmp(&xfer_sock->addr, &rx->addr) == 0)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200419 break;
420 xfer_sock = xfer_sock->next;
421 }
422
423 if (xfer_sock != NULL) {
424 ret = xfer_sock->fd;
425 if (xfer_sock == xfer_sock_list)
426 xfer_sock_list = xfer_sock->next;
427 if (xfer_sock->prev)
428 xfer_sock->prev->next = xfer_sock->next;
429 if (xfer_sock->next)
430 xfer_sock->next->prev = xfer_sock->prev;
431 free(xfer_sock->iface);
432 free(xfer_sock->namespace);
433 free(xfer_sock);
434 }
435 return ret;
436}
437
Willy Tarreau18b7df72020-08-28 12:07:22 +0200438/*
439 * Local variables:
440 * c-indent-level: 8
441 * c-basic-offset: 8
442 * End:
443 */