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> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 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 Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 25 | #include <net/if.h> |
| 26 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 27 | #include <haproxy/api.h> |
Willy Tarreau | 5d9ddc5 | 2021-10-06 19:54:09 +0200 | [diff] [blame] | 28 | #include <haproxy/activity.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 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> |
Amaury Denoyelle | 1125d05 | 2024-05-22 14:21:16 +0200 | [diff] [blame] | 33 | #include <haproxy/protocol-t.h> |
William Lallemand | 2be557f | 2021-11-24 18:45:37 +0100 | [diff] [blame] | 34 | #include <haproxy/proto_sockpair.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 35 | #include <haproxy/sock.h> |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 36 | #include <haproxy/sock_inet.h> |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 37 | #include <haproxy/tools.h> |
| 38 | |
Willy Tarreau | b510116 | 2022-01-28 18:28:18 +0100 | [diff] [blame] | 39 | #define SOCK_XFER_OPT_FOREIGN 0x000000001 |
| 40 | #define SOCK_XFER_OPT_V6ONLY 0x000000002 |
| 41 | #define SOCK_XFER_OPT_DGRAM 0x000000004 |
| 42 | |
Willy Tarreau | 063d47d | 2020-08-28 16:29:53 +0200 | [diff] [blame] | 43 | /* the list of remaining sockets transferred from an older process */ |
Willy Tarreau | b510116 | 2022-01-28 18:28:18 +0100 | [diff] [blame] | 44 | struct xfer_sock_list { |
| 45 | int fd; |
| 46 | int options; /* socket options as SOCK_XFER_OPT_* */ |
| 47 | char *iface; |
| 48 | char *namespace; |
| 49 | int if_namelen; |
| 50 | int ns_namelen; |
| 51 | struct xfer_sock_list *prev; |
| 52 | struct xfer_sock_list *next; |
| 53 | struct sockaddr_storage addr; |
| 54 | }; |
| 55 | |
| 56 | static struct xfer_sock_list *xfer_sock_list; |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 57 | |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 58 | |
| 59 | /* Accept an incoming connection from listener <l>, and return it, as well as |
| 60 | * a CO_AC_* status code into <status> if not null. Null is returned on error. |
| 61 | * <l> must be a valid listener with a valid frontend. |
| 62 | */ |
| 63 | struct connection *sock_accept_conn(struct listener *l, int *status) |
| 64 | { |
| 65 | #ifdef USE_ACCEPT4 |
| 66 | static int accept4_broken; |
| 67 | #endif |
| 68 | struct proxy *p = l->bind_conf->frontend; |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 69 | struct connection *conn = NULL; |
| 70 | struct sockaddr_storage *addr = NULL; |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 71 | socklen_t laddr; |
| 72 | int ret; |
| 73 | int cfd; |
| 74 | |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 75 | if (!sockaddr_alloc(&addr, NULL, 0)) |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 76 | goto fail_addr; |
| 77 | |
| 78 | /* accept() will mark all accepted FDs O_NONBLOCK and the ones accepted |
| 79 | * in the master process as FD_CLOEXEC. It's not done for workers |
| 80 | * because 1) workers are not supposed to execute anything so there's |
| 81 | * no reason for uselessly slowing down everything, and 2) that would |
| 82 | * prevent us from implementing fd passing in the future. |
| 83 | */ |
| 84 | #ifdef USE_ACCEPT4 |
| 85 | laddr = sizeof(*conn->src); |
| 86 | |
| 87 | /* only call accept4() if it's known to be safe, otherwise fallback to |
| 88 | * the legacy accept() + fcntl(). |
| 89 | */ |
| 90 | if (unlikely(accept4_broken) || |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 91 | (((cfd = accept4(l->rx.fd, (struct sockaddr*)addr, &laddr, |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 92 | SOCK_NONBLOCK | (master ? SOCK_CLOEXEC : 0))) == -1) && |
| 93 | (errno == ENOSYS || errno == EINVAL || errno == EBADF) && |
Tim Duesterhus | f897fc9 | 2021-11-20 14:39:47 +0100 | [diff] [blame] | 94 | ((accept4_broken = 1)))) |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 95 | #endif |
| 96 | { |
| 97 | laddr = sizeof(*conn->src); |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 98 | if ((cfd = accept(l->rx.fd, (struct sockaddr*)addr, &laddr)) != -1) { |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 99 | fd_set_nonblock(cfd); |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 100 | if (master) |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 101 | fd_set_cloexec(cfd); |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | if (likely(cfd != -1)) { |
Willy Tarreau | e4d09ce | 2022-04-11 15:01:37 +0200 | [diff] [blame] | 106 | if (unlikely(cfd >= global.maxsock)) { |
Willy Tarreau | e4d09ce | 2022-04-11 15:01:37 +0200 | [diff] [blame] | 107 | send_log(p, LOG_EMERG, |
| 108 | "Proxy %s reached the configured maximum connection limit. Please check the global 'maxconn' value.\n", |
| 109 | p->id); |
| 110 | goto fail_conn; |
| 111 | } |
| 112 | |
Amaury Denoyelle | 1125d05 | 2024-05-22 14:21:16 +0200 | [diff] [blame] | 113 | if (unlikely(port_is_restricted(addr, HA_PROTO_TCP))) |
| 114 | goto fail_conn; |
| 115 | |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 116 | /* Perfect, the connection was accepted */ |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 117 | conn = conn_new(&l->obj_type); |
| 118 | if (!conn) |
| 119 | goto fail_conn; |
| 120 | |
| 121 | conn->src = addr; |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 122 | conn->handle.fd = cfd; |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 123 | ret = CO_AC_DONE; |
| 124 | goto done; |
| 125 | } |
| 126 | |
| 127 | /* error conditions below */ |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 128 | sockaddr_free(&addr); |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 129 | |
| 130 | switch (errno) { |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 131 | #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN |
| 132 | case EWOULDBLOCK: |
| 133 | #endif |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 134 | case EAGAIN: |
| 135 | ret = CO_AC_DONE; /* nothing more to accept */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 136 | if (fdtab[l->rx.fd].state & (FD_POLL_HUP|FD_POLL_ERR)) { |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 137 | /* the listening socket might have been disabled in a shared |
| 138 | * process and we're a collateral victim. We'll just pause for |
| 139 | * a while in case it comes back. In the mean time, we need to |
| 140 | * clear this sticky flag. |
| 141 | */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 142 | _HA_ATOMIC_AND(&fdtab[l->rx.fd].state, ~(FD_POLL_HUP|FD_POLL_ERR)); |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 143 | ret = CO_AC_PAUSE; |
| 144 | } |
| 145 | fd_cant_recv(l->rx.fd); |
| 146 | break; |
| 147 | |
| 148 | case EINVAL: |
| 149 | /* might be trying to accept on a shut fd (eg: soft stop) */ |
| 150 | ret = CO_AC_PAUSE; |
| 151 | break; |
| 152 | |
| 153 | case EINTR: |
| 154 | case ECONNABORTED: |
| 155 | ret = CO_AC_RETRY; |
| 156 | break; |
| 157 | |
| 158 | case ENFILE: |
| 159 | if (p) |
| 160 | send_log(p, LOG_EMERG, |
| 161 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 162 | p->id, global.maxsock); |
| 163 | ret = CO_AC_PAUSE; |
| 164 | break; |
| 165 | |
| 166 | case EMFILE: |
| 167 | if (p) |
| 168 | send_log(p, LOG_EMERG, |
| 169 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 170 | p->id, global.maxsock); |
| 171 | ret = CO_AC_PAUSE; |
| 172 | break; |
| 173 | |
| 174 | case ENOBUFS: |
| 175 | case ENOMEM: |
| 176 | if (p) |
| 177 | send_log(p, LOG_EMERG, |
| 178 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 179 | p->id, global.maxsock); |
| 180 | ret = CO_AC_PAUSE; |
| 181 | break; |
| 182 | |
| 183 | default: |
| 184 | /* unexpected result, let's give up and let other tasks run */ |
| 185 | ret = CO_AC_YIELD; |
| 186 | } |
| 187 | done: |
| 188 | if (status) |
| 189 | *status = ret; |
| 190 | return conn; |
| 191 | |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 192 | fail_conn: |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 193 | sockaddr_free(&addr); |
Remi Tricot-Le Breton | 25dd0ad | 2021-01-14 15:26:24 +0100 | [diff] [blame] | 194 | /* The accept call already succeeded by the time we try to allocate the connection, |
| 195 | * we need to close it in case of failure. */ |
| 196 | close(cfd); |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 197 | fail_addr: |
Willy Tarreau | f1dc9f2 | 2020-10-15 09:21:31 +0200 | [diff] [blame] | 198 | ret = CO_AC_PAUSE; |
| 199 | goto done; |
| 200 | } |
| 201 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 202 | /* Create a socket to connect to the server in conn->dst (which MUST be valid), |
| 203 | * using the configured namespace if needed, or the one passed by the proxy |
| 204 | * protocol if required to do so. It ultimately calls socket() or socketat() |
| 205 | * and returns the FD or error code. |
| 206 | */ |
| 207 | int sock_create_server_socket(struct connection *conn) |
| 208 | { |
| 209 | const struct netns_entry *ns = NULL; |
| 210 | |
| 211 | #ifdef USE_NS |
| 212 | if (objt_server(conn->target)) { |
| 213 | if (__objt_server(conn->target)->flags & SRV_F_USE_NS_FROM_PP) |
| 214 | ns = conn->proxy_netns; |
| 215 | else |
| 216 | ns = __objt_server(conn->target)->netns; |
| 217 | } |
| 218 | #endif |
| 219 | return my_socketat(ns, conn->dst->ss_family, SOCK_STREAM, 0); |
| 220 | } |
| 221 | |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 222 | /* Enables receiving on receiver <rx> once already bound. */ |
Willy Tarreau | e70c797 | 2020-09-25 19:00:01 +0200 | [diff] [blame] | 223 | void sock_enable(struct receiver *rx) |
| 224 | { |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 225 | if (rx->flags & RX_F_BOUND) |
| 226 | fd_want_recv_safe(rx->fd); |
Willy Tarreau | e70c797 | 2020-09-25 19:00:01 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 229 | /* Disables receiving on receiver <rx> once already bound. */ |
Willy Tarreau | e70c797 | 2020-09-25 19:00:01 +0200 | [diff] [blame] | 230 | void sock_disable(struct receiver *rx) |
| 231 | { |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 232 | if (rx->flags & RX_F_BOUND) |
Willy Tarreau | e70c797 | 2020-09-25 19:00:01 +0200 | [diff] [blame] | 233 | fd_stop_recv(rx->fd); |
| 234 | } |
| 235 | |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 236 | /* stops, unbinds and possibly closes the FD associated with receiver rx */ |
| 237 | void sock_unbind(struct receiver *rx) |
| 238 | { |
| 239 | /* There are a number of situations where we prefer to keep the FD and |
| 240 | * not to close it (unless we're stopping, of course): |
Willy Tarreau | 3ecb341 | 2023-11-20 10:44:21 +0100 | [diff] [blame] | 241 | * - worker process unbinding from a worker's non-suspendable FD (ABNS) => close |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 242 | * - worker process unbinding from a worker's FD with socket transfer enabled => keep |
| 243 | * - master process unbinding from a master's inherited FD => keep |
| 244 | * - master process unbinding from a master's FD => close |
Willy Tarreau | 22ccd5e | 2020-11-03 18:38:05 +0100 | [diff] [blame] | 245 | * - master process unbinding from a worker's inherited FD => keep |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 246 | * - master process unbinding from a worker's FD => close |
| 247 | * - worker process unbinding from a master's FD => close |
| 248 | * - worker process unbinding from a worker's FD => close |
| 249 | */ |
| 250 | if (rx->flags & RX_F_BOUND) |
| 251 | rx->proto->rx_disable(rx); |
| 252 | |
| 253 | if (!stopping && !master && |
| 254 | !(rx->flags & RX_F_MWORKER) && |
Willy Tarreau | 3ecb341 | 2023-11-20 10:44:21 +0100 | [diff] [blame] | 255 | !(rx->flags & RX_F_NON_SUSPENDABLE) && |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 256 | (global.tune.options & GTUNE_SOCKET_TRANSFER)) |
| 257 | return; |
| 258 | |
| 259 | if (!stopping && master && |
Willy Tarreau | f58b8db | 2020-10-09 16:32:08 +0200 | [diff] [blame] | 260 | rx->flags & RX_F_INHERITED) |
| 261 | return; |
| 262 | |
| 263 | rx->flags &= ~RX_F_BOUND; |
| 264 | if (rx->fd != -1) |
| 265 | fd_delete(rx->fd); |
| 266 | rx->fd = -1; |
| 267 | } |
| 268 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 269 | /* |
| 270 | * Retrieves the source address for the socket <fd>, with <dir> indicating |
| 271 | * if we're a listener (=0) or an initiator (!=0). It returns 0 in case of |
| 272 | * success, -1 in case of error. The socket's source address is stored in |
| 273 | * <sa> for <salen> bytes. |
| 274 | */ |
| 275 | int sock_get_src(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 276 | { |
| 277 | if (dir) |
| 278 | return getsockname(fd, sa, &salen); |
| 279 | else |
| 280 | return getpeername(fd, sa, &salen); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Retrieves the original destination address for the socket <fd>, with <dir> |
| 285 | * indicating if we're a listener (=0) or an initiator (!=0). It returns 0 in |
| 286 | * case of success, -1 in case of error. The socket's source address is stored |
| 287 | * in <sa> for <salen> bytes. |
| 288 | */ |
| 289 | int sock_get_dst(int fd, struct sockaddr *sa, socklen_t salen, int dir) |
| 290 | { |
| 291 | if (dir) |
| 292 | return getpeername(fd, sa, &salen); |
| 293 | else |
| 294 | return getsockname(fd, sa, &salen); |
| 295 | } |
| 296 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 297 | /* Try to retrieve exported sockets from worker at CLI <unixsocket>. These |
| 298 | * ones will be placed into the xfer_sock_list for later use by function |
| 299 | * sock_find_compatible_fd(). Returns 0 on success, -1 on failure. |
| 300 | */ |
| 301 | int sock_get_old_sockets(const char *unixsocket) |
| 302 | { |
| 303 | char *cmsgbuf = NULL, *tmpbuf = NULL; |
| 304 | int *tmpfd = NULL; |
| 305 | struct sockaddr_un addr; |
| 306 | struct cmsghdr *cmsg; |
| 307 | struct msghdr msghdr; |
| 308 | struct iovec iov; |
| 309 | struct xfer_sock_list *xfer_sock = NULL; |
| 310 | struct timeval tv = { .tv_sec = 1, .tv_usec = 0 }; |
| 311 | int sock = -1; |
| 312 | int ret = -1; |
| 313 | int ret2 = -1; |
| 314 | int fd_nb; |
| 315 | int got_fd = 0; |
| 316 | int cur_fd = 0; |
| 317 | size_t maxoff = 0, curoff = 0; |
| 318 | |
William Lallemand | 2be557f | 2021-11-24 18:45:37 +0100 | [diff] [blame] | 319 | if (strncmp("sockpair@", unixsocket, strlen("sockpair@")) == 0) { |
| 320 | /* sockpair for master-worker usage */ |
| 321 | int sv[2]; |
| 322 | int dst_fd; |
| 323 | |
| 324 | dst_fd = strtoll(unixsocket + strlen("sockpair@"), NULL, 0); |
| 325 | |
| 326 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { |
| 327 | ha_warning("socketpair(): Cannot create socketpair. Giving up.\n"); |
| 328 | } |
| 329 | |
| 330 | if (send_fd_uxst(dst_fd, sv[0]) == -1) { |
William Lallemand | 708949d | 2022-07-25 16:04:38 +0200 | [diff] [blame] | 331 | ha_alert("socketpair: Cannot transfer the fd %d over sockpair@%d. Giving up.\n", sv[0], dst_fd); |
William Lallemand | 2be557f | 2021-11-24 18:45:37 +0100 | [diff] [blame] | 332 | close(sv[0]); |
| 333 | close(sv[1]); |
| 334 | goto out; |
| 335 | } |
| 336 | |
| 337 | close(sv[0]); /* we don't need this side anymore */ |
| 338 | sock = sv[1]; |
| 339 | |
| 340 | } else { |
| 341 | /* Unix socket */ |
| 342 | |
| 343 | sock = socket(PF_UNIX, SOCK_STREAM, 0); |
| 344 | if (sock < 0) { |
| 345 | ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket); |
| 346 | goto out; |
| 347 | } |
| 348 | |
| 349 | strncpy(addr.sun_path, unixsocket, sizeof(addr.sun_path) - 1); |
| 350 | addr.sun_path[sizeof(addr.sun_path) - 1] = 0; |
| 351 | addr.sun_family = PF_UNIX; |
| 352 | |
| 353 | ret = connect(sock, (struct sockaddr *)&addr, sizeof(addr)); |
| 354 | if (ret < 0) { |
| 355 | ha_warning("Failed to connect to the old process socket '%s'\n", unixsocket); |
| 356 | goto out; |
| 357 | } |
| 358 | |
| 359 | } |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 360 | memset(&msghdr, 0, sizeof(msghdr)); |
| 361 | cmsgbuf = malloc(CMSG_SPACE(sizeof(int)) * MAX_SEND_FD); |
| 362 | if (!cmsgbuf) { |
| 363 | ha_warning("Failed to allocate memory to send sockets\n"); |
| 364 | goto out; |
| 365 | } |
| 366 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 367 | setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&tv, sizeof(tv)); |
| 368 | iov.iov_base = &fd_nb; |
| 369 | iov.iov_len = sizeof(fd_nb); |
| 370 | msghdr.msg_iov = &iov; |
| 371 | msghdr.msg_iovlen = 1; |
| 372 | |
| 373 | if (send(sock, "_getsocks\n", strlen("_getsocks\n"), 0) != strlen("_getsocks\n")) { |
| 374 | ha_warning("Failed to get the number of sockets to be transferred !\n"); |
| 375 | goto out; |
| 376 | } |
| 377 | |
| 378 | /* First, get the number of file descriptors to be received */ |
| 379 | if (recvmsg(sock, &msghdr, MSG_WAITALL) != sizeof(fd_nb)) { |
| 380 | ha_warning("Failed to get the number of sockets to be transferred !\n"); |
| 381 | goto out; |
| 382 | } |
| 383 | |
| 384 | if (fd_nb == 0) { |
| 385 | ret2 = 0; |
| 386 | goto out; |
| 387 | } |
| 388 | |
| 389 | tmpbuf = malloc(fd_nb * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int))); |
| 390 | if (tmpbuf == NULL) { |
| 391 | ha_warning("Failed to allocate memory while receiving sockets\n"); |
| 392 | goto out; |
| 393 | } |
| 394 | |
| 395 | tmpfd = malloc(fd_nb * sizeof(int)); |
| 396 | if (tmpfd == NULL) { |
| 397 | ha_warning("Failed to allocate memory while receiving sockets\n"); |
| 398 | goto out; |
| 399 | } |
| 400 | |
| 401 | msghdr.msg_control = cmsgbuf; |
| 402 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)) * MAX_SEND_FD; |
| 403 | iov.iov_len = MAX_SEND_FD * (1 + MAXPATHLEN + 1 + IFNAMSIZ + sizeof(int)); |
| 404 | |
| 405 | do { |
| 406 | int ret3; |
| 407 | |
| 408 | iov.iov_base = tmpbuf + curoff; |
| 409 | |
| 410 | ret = recvmsg(sock, &msghdr, 0); |
| 411 | |
| 412 | if (ret == -1 && errno == EINTR) |
| 413 | continue; |
| 414 | |
| 415 | if (ret <= 0) |
| 416 | break; |
| 417 | |
| 418 | /* Send an ack to let the sender know we got the sockets |
| 419 | * and it can send some more |
| 420 | */ |
| 421 | do { |
| 422 | ret3 = send(sock, &got_fd, sizeof(got_fd), 0); |
| 423 | } while (ret3 == -1 && errno == EINTR); |
| 424 | |
| 425 | for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) { |
| 426 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
| 427 | size_t totlen = cmsg->cmsg_len - CMSG_LEN(0); |
| 428 | |
| 429 | if (totlen / sizeof(int) + got_fd > fd_nb) { |
| 430 | ha_warning("Got to many sockets !\n"); |
| 431 | goto out; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Be paranoid and use memcpy() to avoid any |
| 436 | * potential alignment issue. |
| 437 | */ |
| 438 | memcpy(&tmpfd[got_fd], CMSG_DATA(cmsg), totlen); |
| 439 | got_fd += totlen / sizeof(int); |
| 440 | } |
| 441 | } |
| 442 | curoff += ret; |
| 443 | } while (got_fd < fd_nb); |
| 444 | |
| 445 | if (got_fd != fd_nb) { |
| 446 | ha_warning("We didn't get the expected number of sockets (expecting %d got %d)\n", |
| 447 | fd_nb, got_fd); |
| 448 | goto out; |
| 449 | } |
| 450 | |
| 451 | maxoff = curoff; |
| 452 | curoff = 0; |
| 453 | |
| 454 | for (cur_fd = 0; cur_fd < got_fd; cur_fd++) { |
| 455 | int fd = tmpfd[cur_fd]; |
| 456 | socklen_t socklen; |
| 457 | int val; |
| 458 | int len; |
| 459 | |
| 460 | xfer_sock = calloc(1, sizeof(*xfer_sock)); |
| 461 | if (!xfer_sock) { |
| 462 | ha_warning("Failed to allocate memory in get_old_sockets() !\n"); |
| 463 | break; |
| 464 | } |
| 465 | xfer_sock->fd = -1; |
| 466 | |
| 467 | socklen = sizeof(xfer_sock->addr); |
| 468 | if (getsockname(fd, (struct sockaddr *)&xfer_sock->addr, &socklen) != 0) { |
| 469 | ha_warning("Failed to get socket address\n"); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 470 | ha_free(&xfer_sock); |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 471 | continue; |
| 472 | } |
| 473 | |
| 474 | if (curoff >= maxoff) { |
| 475 | ha_warning("Inconsistency while transferring sockets\n"); |
| 476 | goto out; |
| 477 | } |
| 478 | |
| 479 | len = tmpbuf[curoff++]; |
| 480 | if (len > 0) { |
| 481 | /* We have a namespace */ |
| 482 | if (curoff + len > maxoff) { |
| 483 | ha_warning("Inconsistency while transferring sockets\n"); |
| 484 | goto out; |
| 485 | } |
| 486 | xfer_sock->namespace = malloc(len + 1); |
| 487 | if (!xfer_sock->namespace) { |
| 488 | ha_warning("Failed to allocate memory while transferring sockets\n"); |
| 489 | goto out; |
| 490 | } |
| 491 | memcpy(xfer_sock->namespace, &tmpbuf[curoff], len); |
| 492 | xfer_sock->namespace[len] = 0; |
| 493 | xfer_sock->ns_namelen = len; |
| 494 | curoff += len; |
| 495 | } |
| 496 | |
| 497 | if (curoff >= maxoff) { |
| 498 | ha_warning("Inconsistency while transferring sockets\n"); |
| 499 | goto out; |
| 500 | } |
| 501 | |
| 502 | len = tmpbuf[curoff++]; |
| 503 | if (len > 0) { |
| 504 | /* We have an interface */ |
| 505 | if (curoff + len > maxoff) { |
| 506 | ha_warning("Inconsistency while transferring sockets\n"); |
| 507 | goto out; |
| 508 | } |
| 509 | xfer_sock->iface = malloc(len + 1); |
| 510 | if (!xfer_sock->iface) { |
| 511 | ha_warning("Failed to allocate memory while transferring sockets\n"); |
| 512 | goto out; |
| 513 | } |
| 514 | memcpy(xfer_sock->iface, &tmpbuf[curoff], len); |
| 515 | xfer_sock->iface[len] = 0; |
| 516 | xfer_sock->if_namelen = len; |
| 517 | curoff += len; |
| 518 | } |
| 519 | |
| 520 | if (curoff + sizeof(int) > maxoff) { |
| 521 | ha_warning("Inconsistency while transferring sockets\n"); |
| 522 | goto out; |
| 523 | } |
| 524 | |
| 525 | /* we used to have 32 bits of listener options here but we don't |
| 526 | * use them anymore. |
| 527 | */ |
| 528 | curoff += sizeof(int); |
| 529 | |
| 530 | /* determine the foreign status directly from the socket itself */ |
| 531 | if (sock_inet_is_foreign(fd, xfer_sock->addr.ss_family)) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 532 | xfer_sock->options |= SOCK_XFER_OPT_FOREIGN; |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 533 | |
Willy Tarreau | 9dbb6c4 | 2020-08-28 19:20:23 +0200 | [diff] [blame] | 534 | socklen = sizeof(val); |
| 535 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &socklen) == 0 && val == SOCK_DGRAM) |
| 536 | xfer_sock->options |= SOCK_XFER_OPT_DGRAM; |
| 537 | |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 538 | #if defined(IPV6_V6ONLY) |
| 539 | /* keep only the v6only flag depending on what's currently |
| 540 | * active on the socket, and always drop the v4v6 one. |
| 541 | */ |
| 542 | socklen = sizeof(val); |
| 543 | if (xfer_sock->addr.ss_family == AF_INET6 && |
| 544 | getsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, &socklen) == 0 && val > 0) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 545 | xfer_sock->options |= SOCK_XFER_OPT_V6ONLY; |
Willy Tarreau | 4296174 | 2020-08-28 18:42:45 +0200 | [diff] [blame] | 546 | #endif |
| 547 | |
| 548 | xfer_sock->fd = fd; |
| 549 | if (xfer_sock_list) |
| 550 | xfer_sock_list->prev = xfer_sock; |
| 551 | xfer_sock->next = xfer_sock_list; |
| 552 | xfer_sock->prev = NULL; |
| 553 | xfer_sock_list = xfer_sock; |
| 554 | xfer_sock = NULL; |
| 555 | } |
| 556 | |
| 557 | ret2 = 0; |
| 558 | out: |
| 559 | /* If we failed midway make sure to close the remaining |
| 560 | * file descriptors |
| 561 | */ |
| 562 | if (tmpfd != NULL && cur_fd < got_fd) { |
| 563 | for (; cur_fd < got_fd; cur_fd++) { |
| 564 | close(tmpfd[cur_fd]); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | free(tmpbuf); |
| 569 | free(tmpfd); |
| 570 | free(cmsgbuf); |
| 571 | |
| 572 | if (sock != -1) |
| 573 | close(sock); |
| 574 | |
| 575 | if (xfer_sock) { |
| 576 | free(xfer_sock->namespace); |
| 577 | free(xfer_sock->iface); |
| 578 | if (xfer_sock->fd != -1) |
| 579 | close(xfer_sock->fd); |
| 580 | free(xfer_sock); |
| 581 | } |
| 582 | return (ret2); |
| 583 | } |
| 584 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 585 | /* 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] | 586 | * previous process that we could reuse, instead of creating a new one. Note |
| 587 | * that some address family-specific options are checked on the listener and |
| 588 | * on the socket. Typically for AF_INET and AF_INET6, we check for transparent |
| 589 | * mode, and for AF_INET6 we also check for "v4v6" or "v6only". The reused |
| 590 | * socket is automatically removed from the list so that it's not proposed |
| 591 | * anymore. |
| 592 | */ |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 593 | int sock_find_compatible_fd(const struct receiver *rx) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 594 | { |
| 595 | struct xfer_sock_list *xfer_sock = xfer_sock_list; |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 596 | int options = 0; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 597 | int if_namelen = 0; |
| 598 | int ns_namelen = 0; |
| 599 | int ret = -1; |
| 600 | |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 601 | if (!rx->proto->fam->addrcmp) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 602 | return -1; |
| 603 | |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 604 | if (rx->proto->proto_type == PROTO_TYPE_DGRAM) |
Willy Tarreau | 9dbb6c4 | 2020-08-28 19:20:23 +0200 | [diff] [blame] | 605 | options |= SOCK_XFER_OPT_DGRAM; |
| 606 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 607 | if (rx->settings->options & RX_O_FOREIGN) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 608 | options |= SOCK_XFER_OPT_FOREIGN; |
| 609 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 610 | if (rx->addr.ss_family == AF_INET6) { |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 611 | /* Prepare to match the v6only option against what we really want. Note |
| 612 | * that sadly the two options are not exclusive to each other and that |
| 613 | * v6only is stronger than v4v6. |
| 614 | */ |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 615 | if ((rx->settings->options & RX_O_V6ONLY) || |
| 616 | (sock_inet6_v6only_default && !(rx->settings->options & RX_O_V4V6))) |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 617 | options |= SOCK_XFER_OPT_V6ONLY; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 618 | } |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 619 | |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 620 | if (rx->settings->interface) |
| 621 | if_namelen = strlen(rx->settings->interface); |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 622 | #ifdef USE_NS |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 623 | if (rx->settings->netns) |
| 624 | ns_namelen = rx->settings->netns->name_len; |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 625 | #endif |
| 626 | |
| 627 | while (xfer_sock) { |
Willy Tarreau | a2c1787 | 2020-08-28 19:09:19 +0200 | [diff] [blame] | 628 | if ((options == xfer_sock->options) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 629 | (if_namelen == xfer_sock->if_namelen) && |
| 630 | (ns_namelen == xfer_sock->ns_namelen) && |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 631 | (!if_namelen || strcmp(rx->settings->interface, xfer_sock->iface) == 0) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 632 | #ifdef USE_NS |
Willy Tarreau | c049c0d | 2020-09-01 15:20:52 +0200 | [diff] [blame] | 633 | (!ns_namelen || strcmp(rx->settings->netns->node.key, xfer_sock->namespace) == 0) && |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 634 | #endif |
Willy Tarreau | f1f6609 | 2020-09-04 08:15:31 +0200 | [diff] [blame] | 635 | rx->proto->fam->addrcmp(&xfer_sock->addr, &rx->addr) == 0) |
Willy Tarreau | 2d34a71 | 2020-08-28 16:49:41 +0200 | [diff] [blame] | 636 | break; |
| 637 | xfer_sock = xfer_sock->next; |
| 638 | } |
| 639 | |
| 640 | if (xfer_sock != NULL) { |
| 641 | ret = xfer_sock->fd; |
| 642 | if (xfer_sock == xfer_sock_list) |
| 643 | xfer_sock_list = xfer_sock->next; |
| 644 | if (xfer_sock->prev) |
| 645 | xfer_sock->prev->next = xfer_sock->next; |
| 646 | if (xfer_sock->next) |
| 647 | xfer_sock->next->prev = xfer_sock->prev; |
| 648 | free(xfer_sock->iface); |
| 649 | free(xfer_sock->namespace); |
| 650 | free(xfer_sock); |
| 651 | } |
| 652 | return ret; |
| 653 | } |
| 654 | |
Willy Tarreau | b510116 | 2022-01-28 18:28:18 +0100 | [diff] [blame] | 655 | /* After all protocols are bound, there may remain some old sockets that have |
| 656 | * been removed between the previous config and the new one. These ones must |
| 657 | * be dropped, otherwise they will remain open and may prevent a service from |
| 658 | * restarting. |
| 659 | */ |
| 660 | void sock_drop_unused_old_sockets() |
| 661 | { |
| 662 | while (xfer_sock_list != NULL) { |
| 663 | struct xfer_sock_list *tmpxfer = xfer_sock_list->next; |
| 664 | |
| 665 | close(xfer_sock_list->fd); |
| 666 | free(xfer_sock_list->iface); |
| 667 | free(xfer_sock_list->namespace); |
| 668 | free(xfer_sock_list); |
| 669 | xfer_sock_list = tmpxfer; |
| 670 | } |
| 671 | } |
| 672 | |
Willy Tarreau | 5ced3e8 | 2020-10-13 17:06:12 +0200 | [diff] [blame] | 673 | /* Tests if the receiver supports accepting connections. Returns positive on |
| 674 | * success, 0 if not possible, negative if the socket is non-recoverable. The |
| 675 | * rationale behind this is that inherited FDs may be broken and that shared |
| 676 | * FDs might have been paused by another process. |
| 677 | */ |
Willy Tarreau | 7d053e4 | 2020-10-15 09:19:43 +0200 | [diff] [blame] | 678 | int sock_accepting_conn(const struct receiver *rx) |
Willy Tarreau | 5ced3e8 | 2020-10-13 17:06:12 +0200 | [diff] [blame] | 679 | { |
| 680 | int opt_val = 0; |
| 681 | socklen_t opt_len = sizeof(opt_val); |
| 682 | |
| 683 | if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &opt_val, &opt_len) == -1) |
| 684 | return -1; |
| 685 | |
| 686 | return opt_val; |
| 687 | } |
| 688 | |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 689 | /* This is the FD handler IO callback for stream sockets configured for |
| 690 | * accepting incoming connections. It's a pass-through to listener_accept() |
| 691 | * which will iterate over the listener protocol's accept_conn() function. |
| 692 | * The FD's owner must be a listener. |
| 693 | */ |
| 694 | void sock_accept_iocb(int fd) |
| 695 | { |
| 696 | struct listener *l = fdtab[fd].owner; |
| 697 | |
| 698 | if (!l) |
| 699 | return; |
| 700 | |
Willy Tarreau | b4daeeb | 2020-11-04 14:58:36 +0100 | [diff] [blame] | 701 | BUG_ON(!!master != !!(l->rx.flags & RX_F_MWORKER)); |
Willy Tarreau | a74cb38 | 2020-10-15 21:29:49 +0200 | [diff] [blame] | 702 | listener_accept(l); |
| 703 | } |
| 704 | |
Willy Tarreau | de471c4 | 2020-12-08 15:50:56 +0100 | [diff] [blame] | 705 | /* This completes the initialization of connection <conn> by inserting its FD |
| 706 | * into the fdtab, associating it with the regular connection handler. It will |
| 707 | * be bound to the current thread only. This call cannot fail. |
| 708 | */ |
| 709 | void sock_conn_ctrl_init(struct connection *conn) |
| 710 | { |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 711 | BUG_ON(conn->flags & CO_FL_FDLESS); |
Willy Tarreau | 27a3245 | 2022-07-07 08:29:00 +0200 | [diff] [blame] | 712 | fd_insert(conn->handle.fd, conn, sock_conn_iocb, tgid, ti->ltid_bit); |
Willy Tarreau | de471c4 | 2020-12-08 15:50:56 +0100 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* This completes the release of connection <conn> by removing its FD from the |
| 716 | * fdtab and deleting it. The connection must not use the FD anymore past this |
| 717 | * point. The FD may be modified in the connection. |
| 718 | */ |
| 719 | void sock_conn_ctrl_close(struct connection *conn) |
| 720 | { |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 721 | BUG_ON(conn->flags & CO_FL_FDLESS); |
Willy Tarreau | de471c4 | 2020-12-08 15:50:56 +0100 | [diff] [blame] | 722 | fd_delete(conn->handle.fd); |
| 723 | conn->handle.fd = DEAD_FD_MAGIC; |
| 724 | } |
| 725 | |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 726 | /* This is the callback which is set when a connection establishment is pending |
| 727 | * and we have nothing to send. It may update the FD polling status to indicate |
| 728 | * !READY. It returns 0 if it fails in a fatal way or needs to poll to go |
| 729 | * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN |
| 730 | * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and |
| 731 | * leaves the error code in errno. |
| 732 | */ |
| 733 | int sock_conn_check(struct connection *conn) |
| 734 | { |
| 735 | struct sockaddr_storage *addr; |
| 736 | int fd = conn->handle.fd; |
| 737 | |
| 738 | if (conn->flags & CO_FL_ERROR) |
| 739 | return 0; |
| 740 | |
| 741 | if (!conn_ctrl_ready(conn)) |
| 742 | return 0; |
| 743 | |
| 744 | if (!(conn->flags & CO_FL_WAIT_L4_CONN)) |
| 745 | return 1; /* strange we were called while ready */ |
| 746 | |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 747 | BUG_ON(conn->flags & CO_FL_FDLESS); |
| 748 | |
Willy Tarreau | 5a9c637 | 2021-07-06 08:29:20 +0200 | [diff] [blame] | 749 | if (!fd_send_ready(fd) && !(fdtab[fd].state & (FD_POLL_ERR|FD_POLL_HUP))) |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 750 | return 0; |
| 751 | |
| 752 | /* Here we have 2 cases : |
| 753 | * - modern pollers, able to report ERR/HUP. If these ones return any |
| 754 | * of these flags then it's likely a failure, otherwise it possibly |
| 755 | * is a success (i.e. there may have been data received just before |
| 756 | * the error was reported). |
| 757 | * - select, which doesn't report these and with which it's always |
| 758 | * necessary either to try connect() again or to check for SO_ERROR. |
| 759 | * In order to simplify everything, we double-check using connect() as |
| 760 | * soon as we meet either of these delicate situations. Note that |
| 761 | * SO_ERROR would clear the error after reporting it! |
| 762 | */ |
| 763 | if (cur_poller.flags & HAP_POLL_F_ERRHUP) { |
| 764 | /* modern poller, able to report ERR/HUP */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 765 | if ((fdtab[fd].state & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN) |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 766 | goto done; |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 767 | if ((fdtab[fd].state & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT) |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 768 | goto done; |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 769 | if (!(fdtab[fd].state & (FD_POLL_ERR|FD_POLL_HUP))) |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 770 | goto wait; |
| 771 | /* error present, fall through common error check path */ |
| 772 | } |
| 773 | |
| 774 | /* Use connect() to check the state of the socket. This has the double |
| 775 | * advantage of *not* clearing the error (so that health checks can |
| 776 | * still use getsockopt(SO_ERROR)) and giving us the following info : |
| 777 | * - error |
| 778 | * - connecting (EALREADY, EINPROGRESS) |
| 779 | * - connected (EISCONN, 0) |
| 780 | */ |
| 781 | addr = conn->dst; |
| 782 | if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER) |
| 783 | addr = &objt_server(conn->target)->socks4_addr; |
| 784 | |
| 785 | if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) { |
| 786 | if (errno == EALREADY || errno == EINPROGRESS) |
| 787 | goto wait; |
| 788 | |
| 789 | if (errno && errno != EISCONN) |
| 790 | goto out_error; |
| 791 | } |
| 792 | |
| 793 | done: |
| 794 | /* The FD is ready now, we'll mark the connection as complete and |
| 795 | * forward the event to the transport layer which will notify the |
| 796 | * data layer. |
| 797 | */ |
| 798 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 799 | fd_may_send(fd); |
| 800 | fd_cond_recv(fd); |
| 801 | errno = 0; // make health checks happy |
| 802 | return 1; |
| 803 | |
| 804 | out_error: |
| 805 | /* Write error on the file descriptor. Report it to the connection |
| 806 | * and disable polling on this FD. |
| 807 | */ |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 808 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
Willy Tarreau | b41a6e9 | 2021-04-06 17:49:19 +0200 | [diff] [blame] | 809 | HA_ATOMIC_AND(&fdtab[fd].state, ~FD_LINGER_RISK); |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 810 | fd_stop_both(fd); |
| 811 | return 0; |
| 812 | |
| 813 | wait: |
Willy Tarreau | 46dabcc | 2024-04-09 08:03:10 +0200 | [diff] [blame] | 814 | /* we may arrive here due to connect() misleadingly reporting EALREADY |
| 815 | * in some corner cases while the system disagrees and reports an error |
| 816 | * on the FD. |
| 817 | */ |
| 818 | if (fdtab[fd].state & FD_POLL_ERR) |
| 819 | goto out_error; |
| 820 | |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 821 | fd_cant_send(fd); |
| 822 | fd_want_send(fd); |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | /* I/O callback for fd-based connections. It calls the read/write handlers |
| 827 | * provided by the connection's sock_ops, which must be valid. |
| 828 | */ |
| 829 | void sock_conn_iocb(int fd) |
| 830 | { |
| 831 | struct connection *conn = fdtab[fd].owner; |
| 832 | unsigned int flags; |
| 833 | int need_wake = 0; |
Willy Tarreau | 9b773ec | 2023-03-02 15:07:51 +0100 | [diff] [blame] | 834 | struct tasklet *t; |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 835 | |
| 836 | if (unlikely(!conn)) { |
| 837 | activity[tid].conn_dead++; |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */ |
| 842 | |
| 843 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && |
| 844 | ((fd_send_ready(fd) && fd_send_active(fd)) || |
| 845 | (fd_recv_ready(fd) && fd_recv_active(fd)))) { |
| 846 | /* Still waiting for a connection to establish and nothing was |
| 847 | * attempted yet to probe the connection. this will clear the |
| 848 | * CO_FL_WAIT_L4_CONN flag on success. |
| 849 | */ |
| 850 | if (!sock_conn_check(conn)) |
| 851 | goto leave; |
| 852 | need_wake = 1; |
| 853 | } |
| 854 | |
| 855 | if (fd_send_ready(fd) && fd_send_active(fd)) { |
| 856 | /* force reporting of activity by clearing the previous flags : |
| 857 | * we'll have at least ERROR or CONNECTED at the end of an I/O, |
| 858 | * both of which will be detected below. |
| 859 | */ |
| 860 | flags = 0; |
| 861 | if (conn->subs && conn->subs->events & SUB_RETRY_SEND) { |
Willy Tarreau | 9b773ec | 2023-03-02 15:07:51 +0100 | [diff] [blame] | 862 | t = conn->subs->tasklet; |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 863 | need_wake = 0; // wake will be called after this I/O |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 864 | conn->subs->events &= ~SUB_RETRY_SEND; |
| 865 | if (!conn->subs->events) |
| 866 | conn->subs = NULL; |
Willy Tarreau | 9b773ec | 2023-03-02 15:07:51 +0100 | [diff] [blame] | 867 | tasklet_wakeup(t); |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 868 | } |
| 869 | fd_stop_send(fd); |
| 870 | } |
| 871 | |
| 872 | /* The data transfer starts here and stops on error and handshakes. Note |
| 873 | * that we must absolutely test conn->xprt at each step in case it suddenly |
| 874 | * changes due to a quick unexpected close(). |
| 875 | */ |
| 876 | if (fd_recv_ready(fd) && fd_recv_active(fd)) { |
| 877 | /* force reporting of activity by clearing the previous flags : |
| 878 | * we'll have at least ERROR or CONNECTED at the end of an I/O, |
| 879 | * both of which will be detected below. |
| 880 | */ |
| 881 | flags = 0; |
| 882 | if (conn->subs && conn->subs->events & SUB_RETRY_RECV) { |
Willy Tarreau | 9b773ec | 2023-03-02 15:07:51 +0100 | [diff] [blame] | 883 | t = conn->subs->tasklet; |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 884 | need_wake = 0; // wake will be called after this I/O |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 885 | conn->subs->events &= ~SUB_RETRY_RECV; |
| 886 | if (!conn->subs->events) |
| 887 | conn->subs = NULL; |
Willy Tarreau | 9b773ec | 2023-03-02 15:07:51 +0100 | [diff] [blame] | 888 | tasklet_wakeup(t); |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 889 | } |
| 890 | fd_stop_recv(fd); |
| 891 | } |
| 892 | |
| 893 | leave: |
| 894 | /* we may have to finish to install a mux or to wake it up based on |
| 895 | * what was just done above. It may kill the connection so we have to |
| 896 | * be prpared not to use it anymore. |
| 897 | */ |
| 898 | if (conn_notify_mux(conn, flags, need_wake) < 0) |
| 899 | return; |
| 900 | |
| 901 | /* commit polling changes in case of error. |
| 902 | * WT: it seems that the last case where this could still be relevant |
| 903 | * is if a mux wake function above report a connection error but does |
| 904 | * not stop polling. Shouldn't we enforce this into the mux instead of |
| 905 | * having to deal with this ? |
| 906 | */ |
| 907 | if (unlikely(conn->flags & CO_FL_ERROR)) { |
| 908 | if (conn_ctrl_ready(conn)) |
| 909 | fd_stop_both(fd); |
Willy Tarreau | 482038c | 2023-11-14 07:55:04 +0100 | [diff] [blame] | 910 | |
| 911 | if (conn->subs) { |
| 912 | t = conn->subs->tasklet; |
| 913 | conn->subs->events = 0; |
| 914 | if (!conn->subs->events) |
| 915 | conn->subs = NULL; |
| 916 | tasklet_wakeup(t); |
| 917 | } |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
Willy Tarreau | 427c846 | 2020-12-11 16:19:12 +0100 | [diff] [blame] | 921 | /* Drains possibly pending incoming data on the file descriptor attached to the |
| 922 | * connection. This is used to know whether we need to disable lingering on |
| 923 | * close. Returns non-zero if it is safe to close without disabling lingering, |
| 924 | * otherwise zero. |
| 925 | */ |
| 926 | int sock_drain(struct connection *conn) |
| 927 | { |
| 928 | int turns = 2; |
| 929 | int fd = conn->handle.fd; |
| 930 | int len; |
| 931 | |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 932 | BUG_ON(conn->flags & CO_FL_FDLESS); |
| 933 | |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 934 | if (fdtab[fd].state & (FD_POLL_ERR|FD_POLL_HUP)) |
Willy Tarreau | 427c846 | 2020-12-11 16:19:12 +0100 | [diff] [blame] | 935 | goto shut; |
| 936 | |
Willy Tarreau | 20b622e | 2021-10-21 21:31:42 +0200 | [diff] [blame] | 937 | if (!(conn->flags & CO_FL_WANT_DRAIN) && !fd_recv_ready(fd)) |
Willy Tarreau | 427c846 | 2020-12-11 16:19:12 +0100 | [diff] [blame] | 938 | return 0; |
| 939 | |
| 940 | /* no drain function defined, use the generic one */ |
| 941 | |
| 942 | while (turns) { |
| 943 | #ifdef MSG_TRUNC_CLEARS_INPUT |
| 944 | len = recv(fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC); |
| 945 | if (len == -1 && errno == EFAULT) |
| 946 | #endif |
| 947 | len = recv(fd, trash.area, trash.size, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 948 | |
| 949 | if (len == 0) |
| 950 | goto shut; |
| 951 | |
| 952 | if (len < 0) { |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 953 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
Willy Tarreau | 427c846 | 2020-12-11 16:19:12 +0100 | [diff] [blame] | 954 | /* connection not closed yet */ |
| 955 | fd_cant_recv(fd); |
| 956 | break; |
| 957 | } |
| 958 | if (errno == EINTR) /* oops, try again */ |
| 959 | continue; |
| 960 | /* other errors indicate a dead connection, fine. */ |
| 961 | goto shut; |
| 962 | } |
| 963 | /* OK we read some data, let's try again once */ |
| 964 | turns--; |
| 965 | } |
| 966 | |
| 967 | /* some data are still present, give up */ |
| 968 | return 0; |
| 969 | |
| 970 | shut: |
| 971 | /* we're certain the connection was shut down */ |
Willy Tarreau | b41a6e9 | 2021-04-06 17:49:19 +0200 | [diff] [blame] | 972 | HA_ATOMIC_AND(&fdtab[fd].state, ~FD_LINGER_RISK); |
Willy Tarreau | 427c846 | 2020-12-11 16:19:12 +0100 | [diff] [blame] | 973 | return 1; |
| 974 | } |
| 975 | |
Willy Tarreau | 472125b | 2020-12-11 17:02:50 +0100 | [diff] [blame] | 976 | /* Checks the connection's FD for readiness of events <event_type>, which may |
| 977 | * only be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. Those which are |
| 978 | * ready are returned. The ones that are not ready are enabled. The caller is |
| 979 | * expected to do what is needed to handle ready events and to deal with |
| 980 | * subsequent wakeups caused by the requested events' readiness. |
| 981 | */ |
| 982 | int sock_check_events(struct connection *conn, int event_type) |
| 983 | { |
| 984 | int ret = 0; |
| 985 | |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 986 | BUG_ON(conn->flags & CO_FL_FDLESS); |
| 987 | |
Willy Tarreau | 472125b | 2020-12-11 17:02:50 +0100 | [diff] [blame] | 988 | if (event_type & SUB_RETRY_RECV) { |
| 989 | if (fd_recv_ready(conn->handle.fd)) |
| 990 | ret |= SUB_RETRY_RECV; |
| 991 | else |
| 992 | fd_want_recv(conn->handle.fd); |
| 993 | } |
| 994 | |
| 995 | if (event_type & SUB_RETRY_SEND) { |
| 996 | if (fd_send_ready(conn->handle.fd)) |
| 997 | ret |= SUB_RETRY_SEND; |
| 998 | else |
| 999 | fd_want_send(conn->handle.fd); |
| 1000 | } |
| 1001 | |
| 1002 | return ret; |
| 1003 | } |
| 1004 | |
| 1005 | /* Ignore readiness events from connection's FD for events of types <event_type> |
| 1006 | * which may only be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. |
| 1007 | */ |
| 1008 | void sock_ignore_events(struct connection *conn, int event_type) |
| 1009 | { |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 1010 | BUG_ON(conn->flags & CO_FL_FDLESS); |
| 1011 | |
Willy Tarreau | 472125b | 2020-12-11 17:02:50 +0100 | [diff] [blame] | 1012 | if (event_type & SUB_RETRY_RECV) |
| 1013 | fd_stop_recv(conn->handle.fd); |
| 1014 | |
| 1015 | if (event_type & SUB_RETRY_SEND) |
| 1016 | fd_stop_send(conn->handle.fd); |
| 1017 | } |
| 1018 | |
Willy Tarreau | b073573 | 2023-04-22 18:25:09 +0200 | [diff] [blame] | 1019 | /* Live check to see if a socket type supports SO_REUSEPORT for the specified |
| 1020 | * family and socket() settings. Returns non-zero on success, 0 on failure. Use |
| 1021 | * protocol_supports_flag() instead, which checks cached flags. |
| 1022 | */ |
| 1023 | int _sock_supports_reuseport(const struct proto_fam *fam, int type, int protocol) |
| 1024 | { |
| 1025 | int ret = 0; |
| 1026 | #ifdef SO_REUSEPORT |
| 1027 | struct sockaddr_storage ss; |
| 1028 | socklen_t sl = sizeof(ss); |
| 1029 | int fd1, fd2; |
| 1030 | |
| 1031 | /* for the check, we'll need two sockets */ |
| 1032 | fd1 = fd2 = -1; |
| 1033 | |
| 1034 | /* ignore custom sockets */ |
| 1035 | if (!fam || fam->sock_domain >= AF_MAX) |
| 1036 | goto leave; |
| 1037 | |
| 1038 | fd1 = socket(fam->sock_domain, type, protocol); |
| 1039 | if (fd1 < 0) |
| 1040 | goto leave; |
| 1041 | |
| 1042 | if (setsockopt(fd1, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0) |
| 1043 | goto leave; |
| 1044 | |
| 1045 | /* bind to any address assigned by the kernel, we'll then try to do it twice */ |
| 1046 | memset(&ss, 0, sizeof(ss)); |
| 1047 | ss.ss_family = fam->sock_family; |
| 1048 | if (bind(fd1, (struct sockaddr *)&ss, fam->sock_addrlen) < 0) |
| 1049 | goto leave; |
| 1050 | |
| 1051 | if (getsockname(fd1, (struct sockaddr *)&ss, &sl) < 0) |
| 1052 | goto leave; |
| 1053 | |
| 1054 | fd2 = socket(fam->sock_domain, type, protocol); |
| 1055 | if (fd2 < 0) |
| 1056 | goto leave; |
| 1057 | |
| 1058 | if (setsockopt(fd2, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0) |
| 1059 | goto leave; |
| 1060 | |
| 1061 | if (bind(fd2, (struct sockaddr *)&ss, sl) < 0) |
| 1062 | goto leave; |
| 1063 | |
| 1064 | /* OK we could bind twice to the same address:port, REUSEPORT |
| 1065 | * is supported for this protocol. |
| 1066 | */ |
| 1067 | ret = 1; |
| 1068 | |
| 1069 | leave: |
| 1070 | if (fd2 >= 0) |
| 1071 | close(fd2); |
| 1072 | if (fd1 >= 0) |
| 1073 | close(fd1); |
| 1074 | #endif |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
Willy Tarreau | 18b7df7 | 2020-08-28 12:07:22 +0200 | [diff] [blame] | 1078 | /* |
| 1079 | * Local variables: |
| 1080 | * c-indent-level: 8 |
| 1081 | * c-basic-offset: 8 |
| 1082 | * End: |
| 1083 | */ |