Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 13 | #define _GNU_SOURCE |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
| 15 | #include <errno.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <time.h> |
| 21 | |
| 22 | #include <sys/param.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 26 | #include <net/if.h> |
| 27 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/connection.h> |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 30 | #include <haproxy/listener.h> |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 31 | #include <haproxy/log.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 32 | #include <haproxy/namespace.h> |
| 33 | #include <haproxy/sock.h> |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 34 | #include <haproxy/sock_inet.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 35 | #include <haproxy/tools.h> |
| 36 | |
Willy Tarreau | 063d47d | 2020-08-28 16:29:53 +0200 | [diff] [blame] | 37 | /* the list of remaining sockets transferred from an older process */ |
| 38 | struct xfer_sock_list *xfer_sock_list = NULL; |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 40 | |
| 41 | /* Accept an incoming connection from listener <l>, and return it, as well as |
| 42 | * a CO_AC_* status code into <status> if not null. Null is returned on error. |
| 43 | * <l> must be a valid listener with a valid frontend. |
| 44 | */ |
| 45 | struct connection *sock_accept_conn(struct listener *l, int *status) |
| 46 | { |
| 47 | #ifdef USE_ACCEPT4 |
| 48 | static int accept4_broken; |
| 49 | #endif |
| 50 | struct proxy *p = l->bind_conf->frontend; |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 51 | struct connection *conn = NULL; |
| 52 | struct sockaddr_storage *addr = NULL; |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 53 | socklen_t laddr; |
| 54 | int ret; |
| 55 | int cfd; |
| 56 | |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 57 | if (!sockaddr_alloc(&addr, NULL, 0)) |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 58 | goto fail_addr; |
| 59 | |
| 60 | /* accept() will mark all accepted FDs O_NONBLOCK and the ones accepted |
| 61 | * in the master process as FD_CLOEXEC. It's not done for workers |
| 62 | * because 1) workers are not supposed to execute anything so there's |
| 63 | * no reason for uselessly slowing down everything, and 2) that would |
| 64 | * prevent us from implementing fd passing in the future. |
| 65 | */ |
| 66 | #ifdef USE_ACCEPT4 |
| 67 | laddr = sizeof(*conn->src); |
| 68 | |
| 69 | /* only call accept4() if it's known to be safe, otherwise fallback to |
| 70 | * the legacy accept() + fcntl(). |
| 71 | */ |
| 72 | if (unlikely(accept4_broken) || |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 73 | (((cfd = accept4(l->rx.fd, (struct sockaddr*)addr, &laddr, |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 74 | SOCK_NONBLOCK | (master ? SOCK_CLOEXEC : 0))) == -1) && |
| 75 | (errno == ENOSYS || errno == EINVAL || errno == EBADF) && |
| 76 | (accept4_broken = 1))) |
| 77 | #endif |
| 78 | { |
| 79 | laddr = sizeof(*conn->src); |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 80 | if ((cfd = accept(l->rx.fd, (struct sockaddr*)addr, &laddr)) != -1) { |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 81 | fcntl(cfd, F_SETFL, O_NONBLOCK); |
| 82 | if (master) |
| 83 | fcntl(cfd, F_SETFD, FD_CLOEXEC); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (likely(cfd != -1)) { |
| 88 | /* Perfect, the connection was accepted */ |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 89 | conn = conn_new(&l->obj_type); |
| 90 | if (!conn) |
| 91 | goto fail_conn; |
| 92 | |
| 93 | conn->src = addr; |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 94 | conn->handle.fd = cfd; |
| 95 | conn->flags |= CO_FL_ADDR_FROM_SET; |
| 96 | ret = CO_AC_DONE; |
| 97 | goto done; |
| 98 | } |
| 99 | |
| 100 | /* error conditions below */ |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 101 | sockaddr_free(&addr); |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 102 | |
| 103 | switch (errno) { |
| 104 | case EAGAIN: |
| 105 | ret = CO_AC_DONE; /* nothing more to accept */ |
| 106 | if (fdtab[l->rx.fd].ev & (FD_POLL_HUP|FD_POLL_ERR)) { |
| 107 | /* the listening socket might have been disabled in a shared |
| 108 | * process and we're a collateral victim. We'll just pause for |
| 109 | * a while in case it comes back. In the mean time, we need to |
| 110 | * clear this sticky flag. |
| 111 | */ |
| 112 | _HA_ATOMIC_AND(&fdtab[l->rx.fd].ev, ~(FD_POLL_HUP|FD_POLL_ERR)); |
| 113 | ret = CO_AC_PAUSE; |
| 114 | } |
| 115 | fd_cant_recv(l->rx.fd); |
| 116 | break; |
| 117 | |
| 118 | case EINVAL: |
| 119 | /* might be trying to accept on a shut fd (eg: soft stop) */ |
| 120 | ret = CO_AC_PAUSE; |
| 121 | break; |
| 122 | |
| 123 | case EINTR: |
| 124 | case ECONNABORTED: |
| 125 | ret = CO_AC_RETRY; |
| 126 | break; |
| 127 | |
| 128 | case ENFILE: |
| 129 | if (p) |
| 130 | send_log(p, LOG_EMERG, |
| 131 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 132 | p->id, global.maxsock); |
| 133 | ret = CO_AC_PAUSE; |
| 134 | break; |
| 135 | |
| 136 | case EMFILE: |
| 137 | if (p) |
| 138 | send_log(p, LOG_EMERG, |
| 139 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 140 | p->id, global.maxsock); |
| 141 | ret = CO_AC_PAUSE; |
| 142 | break; |
| 143 | |
| 144 | case ENOBUFS: |
| 145 | case ENOMEM: |
| 146 | if (p) |
| 147 | send_log(p, LOG_EMERG, |
| 148 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 149 | p->id, global.maxsock); |
| 150 | ret = CO_AC_PAUSE; |
| 151 | break; |
| 152 | |
| 153 | default: |
| 154 | /* unexpected result, let's give up and let other tasks run */ |
| 155 | ret = CO_AC_YIELD; |
| 156 | } |
| 157 | done: |
| 158 | if (status) |
| 159 | *status = ret; |
| 160 | return conn; |
| 161 | |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 162 | fail_conn: |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 163 | sockaddr_free(&addr); |
| 164 | fail_addr: |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 165 | ret = CO_AC_PAUSE; |
| 166 | goto done; |
| 167 | } |
| 168 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 169 | /* Create a socket to connect to the server in conn->dst (which MUST be valid), |
| 170 | * using the configured namespace if needed, or the one passed by the proxy |
| 171 | * protocol if required to do so. It ultimately calls socket() or socketat() |
| 172 | * and returns the FD or error code. |
| 173 | */ |
| 174 | int sock_create_server_socket(struct connection *conn) |
| 175 | { |
| 176 | const struct netns_entry *ns = NULL; |
| 177 | |
| 178 | #ifdef USE_NS |
| 179 | if (objt_server(conn->target)) { |
| 180 | if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP) |
| 181 | ns = conn->proxy_netns; |
| 182 | else |
| 183 | ns = __objt_server(conn->target)->netns; |
| 184 | } |
| 185 | #endif |
| 186 | return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, 0); |
| 187 | } |
| 188 | |
Willy Tarreau | e70c797 | 2020-09-25 19:00:01 +0200 | [diff] [blame] | 189 | /* Enables receiving on receiver <rx> once already bound. Does nothing in early |
| 190 | * boot (needs fd_updt). |
| 191 | */ |
| 192 | void sock_enable(struct receiver *rx) |
| 193 | { |
| 194 | if (rx->flags & RX_F_BOUND && fd_updt) |
| 195 | fd_want_recv(rx->fd); |
| 196 | } |
| 197 | |
| 198 | /* Disables receiving on receiver <rx> once already bound. Does nothing in early |
| 199 | * boot (needs fd_updt). |
| 200 | */ |
| 201 | void sock_disable(struct receiver *rx) |
| 202 | { |
| 203 | if (rx->flags & RX_F_BOUND && fd_updt) |
| 204 | fd_stop_recv(rx->fd); |
| 205 | } |
| 206 | |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 207 | /* stops, unbinds and possibly closes the FD associated with receiver rx */ |
| 208 | void sock_unbind(struct receiver *rx) |
| 209 | { |
| 210 | /* There are a number of situations where we prefer to keep the FD and |
| 211 | * not to close it (unless we're stopping, of course): |
| 212 | * - worker process unbinding from a worker's FD with socket transfer enabled => keep |
| 213 | * - master process unbinding from a master's inherited FD => keep |
| 214 | * - master process unbinding from a master's FD => close |
Willy Tarreau | 22ccd5e | 2020-11-03 18:38:05 +0100 | [diff] [blame] | 215 | * - master process unbinding from a worker's inherited FD => keep |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 216 | * - master process unbinding from a worker's FD => close |
| 217 | * - worker process unbinding from a master's FD => close |
| 218 | * - worker process unbinding from a worker's FD => close |
| 219 | */ |
| 220 | if (rx->flags & RX_F_BOUND) |
| 221 | rx->proto->rx_disable(rx); |
| 222 | |
| 223 | if (!stopping && !master && |
| 224 | !(rx->flags & RX_F_MWORKER) && |
| 225 | (global.tune.options & GTUNE_SOCKET_TRANSFER)) |
| 226 | return; |
| 227 | |
| 228 | if (!stopping && master && |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 229 | rx->flags & RX_F_INHERITED) |
| 230 | return; |
| 231 | |
| 232 | rx->flags &= ~RX_F_BOUND; |
| 233 | if (rx->fd != -1) |
| 234 | fd_delete(rx->fd); |
| 235 | rx->fd = -1; |
| 236 | } |
| 237 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 238 | /* |
| 239 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 240 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 241 | * success, -1 in case of error. The socket's source address is stored in |
| 242 | * <sa> for <salen> bytes. |
| 243 | */ |
| 244 | int sock_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 245 | { |
| 246 | if (dir) |
| 247 | return getsockname(fd, sa, &salen); |
| 248 | else |
| 249 | return getpeername(fd, sa, &salen); |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 254 | * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in |
| 255 | * case of success, -1 in case of error. The socket's source address is stored |
| 256 | * in <sa> for <salen> bytes. |
| 257 | */ |
| 258 | int sock_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 259 | { |
| 260 | if (dir) |
| 261 | return getpeername(fd, sa, &salen); |
| 262 | else |
| 263 | return getsockname(fd, sa, &salen); |
| 264 | } |
| 265 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 266 | /* Try to retrieve exported sockets from worker at CLI <unixsocket>. These |
| 267 | * ones will be placed into the xfer_sock_list for later use by function |
| 268 | * sock_find_compatible_fd(). Returns 0 on success, -1 on failure. |
| 269 | */ |
| 270 | int sock_get_old_sockets(const char *unixsocket) |
| 271 | { |
| 272 | char *cmsgbuf = NULL, *tmpbuf = NULL; |
| 273 | int *tmpfd = NULL; |
| 274 | struct sockaddr_un addr; |
| 275 | struct cmsghdr *cmsg; |
| 276 | struct msghdr msghdr; |
| 277 | struct iovec iov; |
| 278 | struct xfer_sock_list *xfer_sock = NULL; |
| 279 | struct timeval tv = { .tv_sec = 1, .tv_usec = 0 }; |
| 280 | int sock = -1; |
| 281 | int ret = -1; |
| 282 | int ret2 = -1; |
| 283 | int fd_nb; |
| 284 | int got_fd = 0; |
| 285 | int cur_fd = 0; |
| 286 | size_t maxoff = 0, curoff = 0; |
| 287 | |
| 288 | memset(&msghdr, 0, sizeof(msghdr)); |
| 289 | cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD); |
| 290 | if (!cmsgbuf) { |
| 291 | ha_warning("Failed to allocate memory to send sockets\n"); |
| 292 | goto out; |
| 293 | } |
| 294 | |
| 295 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 296 | if (sock < 0) { |
| 297 | ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket); |
| 298 | goto out; |
| 299 | } |
| 300 | |
| 301 | strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1); |
| 302 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 303 | addr.sun_family = PF_UNIX; |
| 304 | |
| 305 | ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr)); |
| 306 | if (ret < 0) { |
| 307 | ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket); |
| 308 | goto out; |
| 309 | } |
| 310 | |
| 311 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)); |
| 312 | iov.iov_base = &fd_nb; |
| 313 | iov.iov_len = sizeof(fd_nb); |
| 314 | msghdr.msg_iov = &iov; |
| 315 | msghdr.msg_iovlen = 1; |
| 316 | |
| 317 | if (send(sock, "_getsocks\n", strlen("_getsocks\n"), 0) != strlen("_getsocks\n")) { |
| 318 | ha_warning("Failed to get the number of sockets to be transferred !\n"); |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | /* First, get the number of file descriptors to be received */ |
| 323 | if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) { |
| 324 | ha_warning("Failed to get the number of sockets to be transferred !\n"); |
| 325 | goto out; |
| 326 | } |
| 327 | |
| 328 | if (fd_nb == 0) { |
| 329 | ret2 = 0; |
| 330 | goto out; |
| 331 | } |
| 332 | |
| 333 | tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int))); |
| 334 | if (tmpbuf == NULL) { |
| 335 | ha_warning("Failed to allocate memory while receiving sockets\n"); |
| 336 | goto out; |
| 337 | } |
| 338 | |
| 339 | tmpfd = malloc(fd_nb * sizeof(int)); |
| 340 | if (tmpfd == NULL) { |
| 341 | ha_warning("Failed to allocate memory while receiving sockets\n"); |
| 342 | goto out; |
| 343 | } |
| 344 | |
| 345 | msghdr.msg_control = cmsgbuf; |
| 346 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD; |
| 347 | iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)); |
| 348 | |
| 349 | do { |
| 350 | int ret3; |
| 351 | |
| 352 | iov.iov_base = tmpbuf + curoff; |
| 353 | |
| 354 | ret = recvmsg(sock, &msghdr, 0); |
| 355 | |
| 356 | if (ret == -1 && errno == EINTR) |
| 357 | continue; |
| 358 | |
| 359 | if (ret <= 0) |
| 360 | break; |
| 361 | |
| 362 | /* Send an ack to let the sender know we got the sockets |
| 363 | * and it can send some more |
| 364 | */ |
| 365 | do { |
| 366 | ret3 = send(sock, &got_fd, sizeof(got_fd), 0); |
| 367 | } while (ret3 == -1 && errno == EINTR); |
| 368 | |
| 369 | for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) { |
| 370 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
| 371 | size_t totlen = cmsg->cmsg_len - CMSG_LEN(0); |
| 372 | |
| 373 | if (totlen / sizeof(int) + got_fd > fd_nb) { |
| 374 | ha_warning("Got to many sockets !\n"); |
| 375 | goto out; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Be paranoid and use memcpy() to avoid any |
| 380 | * potential alignment issue. |
| 381 | */ |
| 382 | memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen); |
| 383 | got_fd += totlen / sizeof(int); |
| 384 | } |
| 385 | } |
| 386 | curoff += ret; |
| 387 | } while (got_fd < fd_nb); |
| 388 | |
| 389 | if (got_fd != fd_nb) { |
| 390 | ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n", |
| 391 | fd_nb, got_fd); |
| 392 | goto out; |
| 393 | } |
| 394 | |
| 395 | maxoff = curoff; |
| 396 | curoff = 0; |
| 397 | |
| 398 | for (cur_fd = 0; cur_fd < got_fd; cur_fd++) { |
| 399 | int fd = tmpfd[cur_fd]; |
| 400 | socklen_t socklen; |
| 401 | int val; |
| 402 | int len; |
| 403 | |
| 404 | xfer_sock = calloc(1, sizeof(*xfer_sock)); |
| 405 | if (!xfer_sock) { |
| 406 | ha_warning("Failed to allocate memory in get_old_sockets() !\n"); |
| 407 | break; |
| 408 | } |
| 409 | xfer_sock->fd = -1; |
| 410 | |
| 411 | socklen = sizeof(xfer_sock->addr); |
| 412 | if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) { |
| 413 | ha_warning("Failed to get socket address\n"); |
| 414 | free(xfer_sock); |
| 415 | xfer_sock = NULL; |
| 416 | continue; |
| 417 | } |
| 418 | |
| 419 | if (curoff >= maxoff) { |
| 420 | ha_warning("Inconsistency while transferring sockets\n"); |
| 421 | goto out; |
| 422 | } |
| 423 | |
| 424 | len = tmpbuf[curoff++]; |
| 425 | if (len > 0) { |
| 426 | /* We have a namespace */ |
| 427 | if (curoff + len > maxoff) { |
| 428 | ha_warning("Inconsistency while transferring sockets\n"); |
| 429 | goto out; |
| 430 | } |
| 431 | xfer_sock->namespace = malloc(len + 1); |
| 432 | if (!xfer_sock->namespace) { |
| 433 | ha_warning("Failed to allocate memory while transferring sockets\n"); |
| 434 | goto out; |
| 435 | } |
| 436 | memcpy(xfer_sock->namespace, &tmpbuf[curoff], len); |
| 437 | xfer_sock->namespace[len] = 0; |
| 438 | xfer_sock->ns_namelen = len; |
| 439 | curoff += len; |
| 440 | } |
| 441 | |
| 442 | if (curoff >= maxoff) { |
| 443 | ha_warning("Inconsistency while transferring sockets\n"); |
| 444 | goto out; |
| 445 | } |
| 446 | |
| 447 | len = tmpbuf[curoff++]; |
| 448 | if (len > 0) { |
| 449 | /* We have an interface */ |
| 450 | if (curoff + len > maxoff) { |
| 451 | ha_warning("Inconsistency while transferring sockets\n"); |
| 452 | goto out; |
| 453 | } |
| 454 | xfer_sock->iface = malloc(len + 1); |
| 455 | if (!xfer_sock->iface) { |
| 456 | ha_warning("Failed to allocate memory while transferring sockets\n"); |
| 457 | goto out; |
| 458 | } |
| 459 | memcpy(xfer_sock->iface, &tmpbuf[curoff], len); |
| 460 | xfer_sock->iface[len] = 0; |
| 461 | xfer_sock->if_namelen = len; |
| 462 | curoff += len; |
| 463 | } |
| 464 | |
| 465 | if (curoff + sizeof(int) > maxoff) { |
| 466 | ha_warning("Inconsistency while transferring sockets\n"); |
| 467 | goto out; |
| 468 | } |
| 469 | |
| 470 | /* we used to have 32 bits of listener options here but we don't |
| 471 | * use them anymore. |
| 472 | */ |
| 473 | curoff += sizeof(int); |
| 474 | |
| 475 | /* determine the foreign status directly from the socket itself */ |
| 476 | if (sock_inet_is_foreign(fd, xfer_sock->addr.ss_family)) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 477 | xfer_sock->options |= SOCK_XFER_OPT_FOREIGN; |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 478 | |
Willy Tarreau | 9dbb6c4 | 2020-08-28 19:20:23 +0200 | [diff] [blame] | 479 | socklen = sizeof(val); |
| 480 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &socklen) == 0 && val == SOCK_DGRAM) |
| 481 | xfer_sock->options |= SOCK_XFER_OPT_DGRAM; |
| 482 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 483 | #if defined(IPV6_V6ONLY) |
| 484 | /* keep only the v6only flag depending on what's currently |
| 485 | * active on the socket, and always drop the v4v6 one. |
| 486 | */ |
| 487 | socklen = sizeof(val); |
| 488 | if (xfer_sock->addr.ss_family == AF_INET6 && |
| 489 | getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &socklen) == 0 && val > 0) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 490 | xfer_sock->options |= SOCK_XFER_OPT_V6ONLY; |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 491 | #endif |
| 492 | |
| 493 | xfer_sock->fd = fd; |
| 494 | if (xfer_sock_list) |
| 495 | xfer_sock_list->prev = xfer_sock; |
| 496 | xfer_sock->next = xfer_sock_list; |
| 497 | xfer_sock->prev = NULL; |
| 498 | xfer_sock_list = xfer_sock; |
| 499 | xfer_sock = NULL; |
| 500 | } |
| 501 | |
| 502 | ret2 = 0; |
| 503 | out: |
| 504 | /* If we failed midway make sure to close the remaining |
| 505 | * file descriptors |
| 506 | */ |
| 507 | if (tmpfd != NULL && cur_fd < got_fd) { |
| 508 | for (; cur_fd < got_fd; cur_fd++) { |
| 509 | close(tmpfd[cur_fd]); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | free(tmpbuf); |
| 514 | free(tmpfd); |
| 515 | free(cmsgbuf); |
| 516 | |
| 517 | if (sock != -1) |
| 518 | close(sock); |
| 519 | |
| 520 | if (xfer_sock) { |
| 521 | free(xfer_sock->namespace); |
| 522 | free(xfer_sock->iface); |
| 523 | if (xfer_sock->fd != -1) |
| 524 | close(xfer_sock->fd); |
| 525 | free(xfer_sock); |
| 526 | } |
| 527 | return (ret2); |
| 528 | } |
| 529 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 530 | /* When binding the receivers, check if a socket has been sent to us by the |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 531 | * previous process that we could reuse, instead of creating a new one. Note |
| 532 | * that some address family-specific options are checked on the listener and |
| 533 | * on the socket. Typically for AF_INET and AF_INET6, we check for transparent |
| 534 | * mode, and for AF_INET6 we also check for "v4v6" or "v6only". The reused |
| 535 | * socket is automatically removed from the list so that it's not proposed |
| 536 | * anymore. |
| 537 | */ |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 538 | int sock_find_compatible_fd(const struct receiver *rx) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 539 | { |
| 540 | struct xfer_sock_list *xfer_sock = xfer_sock_list; |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 541 | int options = 0; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 542 | int if_namelen = 0; |
| 543 | int ns_namelen = 0; |
| 544 | int ret = -1; |
| 545 | |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 546 | if (!rx->proto->fam->addrcmp) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 547 | return -1; |
| 548 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 549 | if (rx->proto->sock_type == SOCK_DGRAM) |
Willy Tarreau | 9dbb6c4 | 2020-08-28 19:20:23 +0200 | [diff] [blame] | 550 | options |= SOCK_XFER_OPT_DGRAM; |
| 551 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 552 | if (rx->settings->options & RX_O_FOREIGN) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 553 | options |= SOCK_XFER_OPT_FOREIGN; |
| 554 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 555 | if (rx->addr.ss_family == AF_INET6) { |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 556 | /* Prepare to match the v6only option against what we really want. Note |
| 557 | * that sadly the two options are not exclusive to each other and that |
| 558 | * v6only is stronger than v4v6. |
| 559 | */ |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 560 | if ((rx->settings->options & RX_O_V6ONLY) || |
| 561 | (sock_inet6_v6only_default && !(rx->settings->options & RX_O_V4V6))) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 562 | options |= SOCK_XFER_OPT_V6ONLY; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 563 | } |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 564 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 565 | if (rx->settings->interface) |
| 566 | if_namelen = strlen(rx->settings->interface); |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 567 | #ifdef USE_NS |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 568 | if (rx->settings->netns) |
| 569 | ns_namelen = rx->settings->netns->name_len; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 570 | #endif |
| 571 | |
| 572 | while (xfer_sock) { |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 573 | if ((options == xfer_sock->options) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 574 | (if_namelen == xfer_sock->if_namelen) && |
| 575 | (ns_namelen == xfer_sock->ns_namelen) && |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 576 | (!if_namelen || strcmp(rx->settings->interface, xfer_sock->iface) == 0) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 577 | #ifdef USE_NS |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 578 | (!ns_namelen || strcmp(rx->settings->netns->node.key, xfer_sock->namespace) == 0) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 579 | #endif |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 580 | rx->proto->fam->addrcmp(&xfer_sock->addr, &rx->addr) == 0) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 581 | break; |
| 582 | xfer_sock = xfer_sock->next; |
| 583 | } |
| 584 | |
| 585 | if (xfer_sock != NULL) { |
| 586 | ret = xfer_sock->fd; |
| 587 | if (xfer_sock == xfer_sock_list) |
| 588 | xfer_sock_list = xfer_sock->next; |
| 589 | if (xfer_sock->prev) |
| 590 | xfer_sock->prev->next = xfer_sock->next; |
| 591 | if (xfer_sock->next) |
| 592 | xfer_sock->next->prev = xfer_sock->prev; |
| 593 | free(xfer_sock->iface); |
| 594 | free(xfer_sock->namespace); |
| 595 | free(xfer_sock); |
| 596 | } |
| 597 | return ret; |
| 598 | } |
| 599 | |
Willy Tarreau | 5ced3e8 | 2020-10-13 17:06:12 +0200 | [diff] [blame] | 600 | /* Tests if the receiver supports accepting connections. Returns positive on |
| 601 | * success, 0 if not possible, negative if the socket is non-recoverable. The |
| 602 | * rationale behind this is that inherited FDs may be broken and that shared |
| 603 | * FDs might have been paused by another process. |
| 604 | */ |
Willy Tarreau | 7d053e4 | 2020-10-15 09:19:43 +0200 | [diff] [blame] | 605 | int sock_accepting_conn(const struct receiver *rx) |
Willy Tarreau | 5ced3e8 | 2020-10-13 17:06:12 +0200 | [diff] [blame] | 606 | { |
| 607 | int opt_val = 0; |
| 608 | socklen_t opt_len = sizeof(opt_val); |
| 609 | |
| 610 | if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &opt_val, &opt_len) == -1) |
| 611 | return -1; |
| 612 | |
| 613 | return opt_val; |
| 614 | } |
| 615 | |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 616 | /* This is the FD handler IO callback for stream sockets configured for |
| 617 | * accepting incoming connections. It's a pass-through to listener_accept() |
| 618 | * which will iterate over the listener protocol's accept_conn() function. |
| 619 | * The FD's owner must be a listener. |
| 620 | */ |
| 621 | void sock_accept_iocb(int fd) |
| 622 | { |
| 623 | struct listener *l = fdtab[fd].owner; |
| 624 | |
| 625 | if (!l) |
| 626 | return; |
| 627 | |
| 628 | listener_accept(l); |
| 629 | } |
| 630 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 631 | /* |
| 632 | * Local variables: |
| 633 | * c-indent-level: 8 |
| 634 | * c-basic-offset: 8 |
| 635 | * End: |
| 636 | */ |