blob: ea6e3667248dbb7d381fd9cc71b5f470cb509755 [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 Tarreauf58b8db2020-10-09 16:32:08 +020076/* stops, unbinds and possibly closes the FD associated with receiver rx */
77void sock_unbind(struct receiver *rx)
78{
79 /* There are a number of situations where we prefer to keep the FD and
80 * not to close it (unless we're stopping, of course):
81 * - worker process unbinding from a worker's FD with socket transfer enabled => keep
82 * - master process unbinding from a master's inherited FD => keep
83 * - master process unbinding from a master's FD => close
84 * - master process unbinding from a worker's FD => close
85 * - worker process unbinding from a master's FD => close
86 * - worker process unbinding from a worker's FD => close
87 */
88 if (rx->flags & RX_F_BOUND)
89 rx->proto->rx_disable(rx);
90
91 if (!stopping && !master &&
92 !(rx->flags & RX_F_MWORKER) &&
93 (global.tune.options & GTUNE_SOCKET_TRANSFER))
94 return;
95
96 if (!stopping && master &&
97 rx->flags & RX_F_MWORKER &&
98 rx->flags & RX_F_INHERITED)
99 return;
100
101 rx->flags &= ~RX_F_BOUND;
102 if (rx->fd != -1)
103 fd_delete(rx->fd);
104 rx->fd = -1;
105}
106
Willy Tarreau18b7df72020-08-28 12:07:22 +0200107/*
108 * Retrieves the source address for the socket <fd>, with <dir> indicating
109 * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of
110 * success, -1 in case of error. The socket's source address is stored in
111 * <sa> for <salen> bytes.
112 */
113int sock_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir)
114{
115 if (dir)
116 return getsockname(fd, sa, &salen);
117 else
118 return getpeername(fd, sa, &salen);
119}
120
121/*
122 * Retrieves the original destination address for the socket <fd>, with <dir>
123 * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in
124 * case of success, -1 in case of error. The socket's source address is stored
125 * in <sa> for <salen> bytes.
126 */
127int sock_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir)
128{
129 if (dir)
130 return getpeername(fd, sa, &salen);
131 else
132 return getsockname(fd, sa, &salen);
133}
134
Willy Tarreau42961742020-08-28 18:42:45 +0200135/* Try to retrieve exported sockets from worker at CLI <unixsocket>. These
136 * ones will be placed into the xfer_sock_list for later use by function
137 * sock_find_compatible_fd(). Returns 0 on success, -1 on failure.
138 */
139int sock_get_old_sockets(const char *unixsocket)
140{
141 char *cmsgbuf = NULL, *tmpbuf = NULL;
142 int *tmpfd = NULL;
143 struct sockaddr_un addr;
144 struct cmsghdr *cmsg;
145 struct msghdr msghdr;
146 struct iovec iov;
147 struct xfer_sock_list *xfer_sock = NULL;
148 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
149 int sock = -1;
150 int ret = -1;
151 int ret2 = -1;
152 int fd_nb;
153 int got_fd = 0;
154 int cur_fd = 0;
155 size_t maxoff = 0, curoff = 0;
156
157 memset(&msghdr, 0, sizeof(msghdr));
158 cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD);
159 if (!cmsgbuf) {
160 ha_warning("Failed to allocate memory to send sockets\n");
161 goto out;
162 }
163
164 sock = socket(PF_UNIX, SOCK_STREAM, 0);
165 if (sock < 0) {
166 ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket);
167 goto out;
168 }
169
170 strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1);
171 addr.sun_path[sizeof(addr.sun_path) - 1] = 0;
172 addr.sun_family = PF_UNIX;
173
174 ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr));
175 if (ret < 0) {
176 ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket);
177 goto out;
178 }
179
180 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv));
181 iov.iov_base = &fd_nb;
182 iov.iov_len = sizeof(fd_nb);
183 msghdr.msg_iov = &iov;
184 msghdr.msg_iovlen = 1;
185
186 if (send(sock, "_getsocks\n", strlen("_getsocks\n"), 0) != strlen("_getsocks\n")) {
187 ha_warning("Failed to get the number of sockets to be transferred !\n");
188 goto out;
189 }
190
191 /* First, get the number of file descriptors to be received */
192 if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) {
193 ha_warning("Failed to get the number of sockets to be transferred !\n");
194 goto out;
195 }
196
197 if (fd_nb == 0) {
198 ret2 = 0;
199 goto out;
200 }
201
202 tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)));
203 if (tmpbuf == NULL) {
204 ha_warning("Failed to allocate memory while receiving sockets\n");
205 goto out;
206 }
207
208 tmpfd = malloc(fd_nb * sizeof(int));
209 if (tmpfd == NULL) {
210 ha_warning("Failed to allocate memory while receiving sockets\n");
211 goto out;
212 }
213
214 msghdr.msg_control = cmsgbuf;
215 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD;
216 iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int));
217
218 do {
219 int ret3;
220
221 iov.iov_base = tmpbuf + curoff;
222
223 ret = recvmsg(sock, &msghdr, 0);
224
225 if (ret == -1 && errno == EINTR)
226 continue;
227
228 if (ret <= 0)
229 break;
230
231 /* Send an ack to let the sender know we got the sockets
232 * and it can send some more
233 */
234 do {
235 ret3 = send(sock, &got_fd, sizeof(got_fd), 0);
236 } while (ret3 == -1 && errno == EINTR);
237
238 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
239 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
240 size_t totlen = cmsg->cmsg_len - CMSG_LEN(0);
241
242 if (totlen / sizeof(int) + got_fd > fd_nb) {
243 ha_warning("Got to many sockets !\n");
244 goto out;
245 }
246
247 /*
248 * Be paranoid and use memcpy() to avoid any
249 * potential alignment issue.
250 */
251 memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen);
252 got_fd += totlen / sizeof(int);
253 }
254 }
255 curoff += ret;
256 } while (got_fd < fd_nb);
257
258 if (got_fd != fd_nb) {
259 ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n",
260 fd_nb, got_fd);
261 goto out;
262 }
263
264 maxoff = curoff;
265 curoff = 0;
266
267 for (cur_fd = 0; cur_fd < got_fd; cur_fd++) {
268 int fd = tmpfd[cur_fd];
269 socklen_t socklen;
270 int val;
271 int len;
272
273 xfer_sock = calloc(1, sizeof(*xfer_sock));
274 if (!xfer_sock) {
275 ha_warning("Failed to allocate memory in get_old_sockets() !\n");
276 break;
277 }
278 xfer_sock->fd = -1;
279
280 socklen = sizeof(xfer_sock->addr);
281 if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) {
282 ha_warning("Failed to get socket address\n");
283 free(xfer_sock);
284 xfer_sock = NULL;
285 continue;
286 }
287
288 if (curoff >= maxoff) {
289 ha_warning("Inconsistency while transferring sockets\n");
290 goto out;
291 }
292
293 len = tmpbuf[curoff++];
294 if (len > 0) {
295 /* We have a namespace */
296 if (curoff + len > maxoff) {
297 ha_warning("Inconsistency while transferring sockets\n");
298 goto out;
299 }
300 xfer_sock->namespace = malloc(len + 1);
301 if (!xfer_sock->namespace) {
302 ha_warning("Failed to allocate memory while transferring sockets\n");
303 goto out;
304 }
305 memcpy(xfer_sock->namespace, &tmpbuf[curoff], len);
306 xfer_sock->namespace[len] = 0;
307 xfer_sock->ns_namelen = len;
308 curoff += len;
309 }
310
311 if (curoff >= maxoff) {
312 ha_warning("Inconsistency while transferring sockets\n");
313 goto out;
314 }
315
316 len = tmpbuf[curoff++];
317 if (len > 0) {
318 /* We have an interface */
319 if (curoff + len > maxoff) {
320 ha_warning("Inconsistency while transferring sockets\n");
321 goto out;
322 }
323 xfer_sock->iface = malloc(len + 1);
324 if (!xfer_sock->iface) {
325 ha_warning("Failed to allocate memory while transferring sockets\n");
326 goto out;
327 }
328 memcpy(xfer_sock->iface, &tmpbuf[curoff], len);
329 xfer_sock->iface[len] = 0;
330 xfer_sock->if_namelen = len;
331 curoff += len;
332 }
333
334 if (curoff + sizeof(int) > maxoff) {
335 ha_warning("Inconsistency while transferring sockets\n");
336 goto out;
337 }
338
339 /* we used to have 32 bits of listener options here but we don't
340 * use them anymore.
341 */
342 curoff += sizeof(int);
343
344 /* determine the foreign status directly from the socket itself */
345 if (sock_inet_is_foreign(fd, xfer_sock->addr.ss_family))
Willy Tarreaua2c17872020-08-28 19:09:19 +0200346 xfer_sock->options |= SOCK_XFER_OPT_FOREIGN;
Willy Tarreau42961742020-08-28 18:42:45 +0200347
Willy Tarreau9dbb6c42020-08-28 19:20:23 +0200348 socklen = sizeof(val);
349 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &socklen) == 0 && val == SOCK_DGRAM)
350 xfer_sock->options |= SOCK_XFER_OPT_DGRAM;
351
Willy Tarreau42961742020-08-28 18:42:45 +0200352#if defined(IPV6_V6ONLY)
353 /* keep only the v6only flag depending on what's currently
354 * active on the socket, and always drop the v4v6 one.
355 */
356 socklen = sizeof(val);
357 if (xfer_sock->addr.ss_family == AF_INET6 &&
358 getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &socklen) == 0 && val > 0)
Willy Tarreaua2c17872020-08-28 19:09:19 +0200359 xfer_sock->options |= SOCK_XFER_OPT_V6ONLY;
Willy Tarreau42961742020-08-28 18:42:45 +0200360#endif
361
362 xfer_sock->fd = fd;
363 if (xfer_sock_list)
364 xfer_sock_list->prev = xfer_sock;
365 xfer_sock->next = xfer_sock_list;
366 xfer_sock->prev = NULL;
367 xfer_sock_list = xfer_sock;
368 xfer_sock = NULL;
369 }
370
371 ret2 = 0;
372out:
373 /* If we failed midway make sure to close the remaining
374 * file descriptors
375 */
376 if (tmpfd != NULL && cur_fd < got_fd) {
377 for (; cur_fd < got_fd; cur_fd++) {
378 close(tmpfd[cur_fd]);
379 }
380 }
381
382 free(tmpbuf);
383 free(tmpfd);
384 free(cmsgbuf);
385
386 if (sock != -1)
387 close(sock);
388
389 if (xfer_sock) {
390 free(xfer_sock->namespace);
391 free(xfer_sock->iface);
392 if (xfer_sock->fd != -1)
393 close(xfer_sock->fd);
394 free(xfer_sock);
395 }
396 return (ret2);
397}
398
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200399/* When binding the receivers, check if a socket has been sent to us by the
Willy Tarreau2d34a712020-08-28 16:49:41 +0200400 * previous process that we could reuse, instead of creating a new one. Note
401 * that some address family-specific options are checked on the listener and
402 * on the socket. Typically for AF_INET and AF_INET6, we check for transparent
403 * mode, and for AF_INET6 we also check for "v4v6" or "v6only". The reused
404 * socket is automatically removed from the list so that it's not proposed
405 * anymore.
406 */
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200407int sock_find_compatible_fd(const struct receiver *rx)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200408{
409 struct xfer_sock_list *xfer_sock = xfer_sock_list;
Willy Tarreaua2c17872020-08-28 19:09:19 +0200410 int options = 0;
Willy Tarreau2d34a712020-08-28 16:49:41 +0200411 int if_namelen = 0;
412 int ns_namelen = 0;
413 int ret = -1;
414
Willy Tarreauf1f66092020-09-04 08:15:31 +0200415 if (!rx->proto->fam->addrcmp)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200416 return -1;
417
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200418 if (rx->proto->sock_type == SOCK_DGRAM)
Willy Tarreau9dbb6c42020-08-28 19:20:23 +0200419 options |= SOCK_XFER_OPT_DGRAM;
420
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200421 if (rx->settings->options & RX_O_FOREIGN)
Willy Tarreaua2c17872020-08-28 19:09:19 +0200422 options |= SOCK_XFER_OPT_FOREIGN;
423
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200424 if (rx->addr.ss_family == AF_INET6) {
Willy Tarreau2d34a712020-08-28 16:49:41 +0200425 /* Prepare to match the v6only option against what we really want. Note
426 * that sadly the two options are not exclusive to each other and that
427 * v6only is stronger than v4v6.
428 */
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200429 if ((rx->settings->options & RX_O_V6ONLY) ||
430 (sock_inet6_v6only_default && !(rx->settings->options & RX_O_V4V6)))
Willy Tarreaua2c17872020-08-28 19:09:19 +0200431 options |= SOCK_XFER_OPT_V6ONLY;
Willy Tarreau2d34a712020-08-28 16:49:41 +0200432 }
Willy Tarreau2d34a712020-08-28 16:49:41 +0200433
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200434 if (rx->settings->interface)
435 if_namelen = strlen(rx->settings->interface);
Willy Tarreau2d34a712020-08-28 16:49:41 +0200436#ifdef USE_NS
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200437 if (rx->settings->netns)
438 ns_namelen = rx->settings->netns->name_len;
Willy Tarreau2d34a712020-08-28 16:49:41 +0200439#endif
440
441 while (xfer_sock) {
Willy Tarreaua2c17872020-08-28 19:09:19 +0200442 if ((options == xfer_sock->options) &&
Willy Tarreau2d34a712020-08-28 16:49:41 +0200443 (if_namelen == xfer_sock->if_namelen) &&
444 (ns_namelen == xfer_sock->ns_namelen) &&
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200445 (!if_namelen || strcmp(rx->settings->interface, xfer_sock->iface) == 0) &&
Willy Tarreau2d34a712020-08-28 16:49:41 +0200446#ifdef USE_NS
Willy Tarreauc049c0d2020-09-01 15:20:52 +0200447 (!ns_namelen || strcmp(rx->settings->netns->node.key, xfer_sock->namespace) == 0) &&
Willy Tarreau2d34a712020-08-28 16:49:41 +0200448#endif
Willy Tarreauf1f66092020-09-04 08:15:31 +0200449 rx->proto->fam->addrcmp(&xfer_sock->addr, &rx->addr) == 0)
Willy Tarreau2d34a712020-08-28 16:49:41 +0200450 break;
451 xfer_sock = xfer_sock->next;
452 }
453
454 if (xfer_sock != NULL) {
455 ret = xfer_sock->fd;
456 if (xfer_sock == xfer_sock_list)
457 xfer_sock_list = xfer_sock->next;
458 if (xfer_sock->prev)
459 xfer_sock->prev->next = xfer_sock->next;
460 if (xfer_sock->next)
461 xfer_sock->next->prev = xfer_sock->prev;
462 free(xfer_sock->iface);
463 free(xfer_sock->namespace);
464 free(xfer_sock);
465 }
466 return ret;
467}
468
Willy Tarreau5ced3e82020-10-13 17:06:12 +0200469/* Tests if the receiver supports accepting connections. Returns positive on
470 * success, 0 if not possible, negative if the socket is non-recoverable. The
471 * rationale behind this is that inherited FDs may be broken and that shared
472 * FDs might have been paused by another process.
473 */
474int sock_accept_conn(const struct receiver *rx)
475{
476 int opt_val = 0;
477 socklen_t opt_len = sizeof(opt_val);
478
479 if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &opt_val, &opt_len) == -1)
480 return -1;
481
482 return opt_val;
483}
484
Willy Tarreau18b7df72020-08-28 12:07:22 +0200485/*
486 * Local variables:
487 * c-indent-level: 8
488 * c-basic-offset: 8
489 * End:
490 */