William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Socket Pair protocol layer (sockpair) |
| 3 | * |
| 4 | * Copyright HAProxy Technologies - William Lallemand <wlallemand@haproxy.com> |
| 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> |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 15 | #include <pwd.h> |
| 16 | #include <grp.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <syslog.h> |
| 21 | #include <time.h> |
| 22 | |
| 23 | #include <sys/socket.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/un.h> |
| 27 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 29 | #include <haproxy/connection.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 30 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 31 | #include <haproxy/fd.h> |
| 32 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 33 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 34 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 35 | #include <haproxy/listener.h> |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 36 | #include <haproxy/log.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/protocol.h> |
Willy Tarreau | 62292b2 | 2020-09-02 17:52:23 +0200 | [diff] [blame] | 38 | #include <haproxy/proto_sockpair.h> |
Willy Tarreau | 686fa3d | 2020-09-25 19:09:53 +0200 | [diff] [blame] | 39 | #include <haproxy/sock.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 40 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 41 | #include <haproxy/version.h> |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 42 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 43 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 44 | static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen); |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 45 | static void sockpair_enable_listener(struct listener *listener); |
| 46 | static void sockpair_disable_listener(struct listener *listener); |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 47 | static int sockpair_connect_server(struct connection *conn, int flags); |
Willy Tarreau | 7d053e4 | 2020-10-15 09:19:43 +0200 | [diff] [blame] | 48 | static int sockpair_accepting_conn(const struct receiver *rx); |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 49 | struct connection *sockpair_accept_conn(struct listener *l, int *status); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 50 | |
Willy Tarreau | b0254cb | 2020-09-04 08:07:11 +0200 | [diff] [blame] | 51 | struct proto_fam proto_fam_sockpair = { |
| 52 | .name = "sockpair", |
| 53 | .sock_domain = AF_CUST_SOCKPAIR, |
| 54 | .sock_family = AF_UNIX, |
| 55 | .sock_addrlen = sizeof(struct sockaddr_un), |
| 56 | .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path), |
| 57 | .addrcmp = NULL, |
| 58 | .bind = sockpair_bind_receiver, |
| 59 | .get_src = NULL, |
| 60 | .get_dst = NULL, |
| 61 | }; |
| 62 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 63 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | b9b2fd7 | 2020-12-08 14:13:11 +0100 | [diff] [blame] | 64 | struct protocol proto_sockpair = { |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 65 | .name = "sockpair", |
| 66 | |
| 67 | /* connection layer */ |
Willy Tarreau | 91b4726 | 2022-05-20 16:36:46 +0200 | [diff] [blame] | 68 | .xprt_type = PROTO_TYPE_STREAM, |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 69 | .listen = sockpair_bind_listener, |
| 70 | .enable = sockpair_enable_listener, |
| 71 | .disable = sockpair_disable_listener, |
| 72 | .add = default_add_listener, |
| 73 | .unbind = default_unbind_listener, |
| 74 | .accept_conn = sockpair_accept_conn, |
Willy Tarreau | de471c4 | 2020-12-08 15:50:56 +0100 | [diff] [blame] | 75 | .ctrl_init = sock_conn_ctrl_init, |
| 76 | .ctrl_close = sock_conn_ctrl_close, |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 77 | .connect = sockpair_connect_server, |
Willy Tarreau | 427c846 | 2020-12-11 16:19:12 +0100 | [diff] [blame] | 78 | .drain = sock_drain, |
Willy Tarreau | 472125b | 2020-12-11 17:02:50 +0100 | [diff] [blame] | 79 | .check_events = sock_check_events, |
| 80 | .ignore_events = sock_ignore_events, |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 81 | |
| 82 | /* binding layer */ |
| 83 | /* Note: suspend/resume not supported */ |
| 84 | |
| 85 | /* address family */ |
| 86 | .fam = &proto_fam_sockpair, |
| 87 | |
| 88 | /* socket layer */ |
Willy Tarreau | 337edfd | 2021-10-27 17:05:36 +0200 | [diff] [blame] | 89 | .proto_type = PROTO_TYPE_STREAM, |
Willy Tarreau | b366c9a | 2020-12-08 14:54:20 +0100 | [diff] [blame] | 90 | .sock_type = SOCK_STREAM, |
| 91 | .sock_prot = 0, |
| 92 | .rx_enable = sock_enable, |
| 93 | .rx_disable = sock_disable, |
| 94 | .rx_unbind = sock_unbind, |
| 95 | .rx_listening = sockpair_accepting_conn, |
| 96 | .default_iocb = sock_accept_iocb, |
| 97 | .receivers = LIST_HEAD_INIT(proto_sockpair.receivers), |
| 98 | .nb_receivers = 0, |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 101 | INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair); |
| 102 | |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 103 | /* Enable receipt of incoming connections for listener <l>. The receiver must |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 104 | * still be valid. |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 105 | */ |
| 106 | static void sockpair_enable_listener(struct listener *l) |
| 107 | { |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 108 | fd_want_recv_safe(l->rx.fd); |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* Disable receipt of incoming connections for listener <l>. The receiver must |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 112 | * still be valid. |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 113 | */ |
| 114 | static void sockpair_disable_listener(struct listener *l) |
| 115 | { |
Willy Tarreau | a4380b2 | 2020-11-04 13:59:04 +0100 | [diff] [blame] | 116 | fd_stop_recv(l->rx.fd); |
Willy Tarreau | 5ddf1ce | 2020-09-25 19:27:39 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Willy Tarreau | 233ad28 | 2020-10-15 21:45:15 +0200 | [diff] [blame] | 119 | /* Binds receiver <rx>, and assigns rx->iocb and rx->owner as the callback |
| 120 | * and context, respectively, with ->bind_thread as the thread mask. Returns an |
| 121 | * error code made of ERR_* bits on failure or ERR_NONE on success. On failure, |
| 122 | * an error message may be passed into <errmsg>. Note that the binding address |
| 123 | * is only an FD to receive the incoming FDs on. Thus by definition there is no |
| 124 | * real "bind" operation, this only completes the receiver. Such FDs are not |
Willy Tarreau | 62292b2 | 2020-09-02 17:52:23 +0200 | [diff] [blame] | 125 | * inherited upon reload. |
| 126 | */ |
Willy Tarreau | 233ad28 | 2020-10-15 21:45:15 +0200 | [diff] [blame] | 127 | int sockpair_bind_receiver(struct receiver *rx, char **errmsg) |
Willy Tarreau | 62292b2 | 2020-09-02 17:52:23 +0200 | [diff] [blame] | 128 | { |
| 129 | int err; |
| 130 | |
| 131 | /* ensure we never return garbage */ |
| 132 | if (errmsg) |
| 133 | *errmsg = 0; |
| 134 | |
| 135 | err = ERR_NONE; |
| 136 | |
| 137 | if (rx->flags & RX_F_BOUND) |
| 138 | return ERR_NONE; |
| 139 | |
Willy Tarreau | 0e1aaf4 | 2023-02-27 16:39:32 +0100 | [diff] [blame] | 140 | if (rx->flags & RX_F_MUST_DUP) { |
| 141 | /* this is a secondary receiver that is an exact copy of a |
| 142 | * reference which must already be bound (or has failed). |
| 143 | * We'll try to dup() the other one's FD and take it. We |
| 144 | * try hard not to reconfigure the socket since it's shared. |
| 145 | */ |
| 146 | BUG_ON(!rx->shard_info); |
| 147 | if (!(rx->shard_info->ref->flags & RX_F_BOUND)) { |
| 148 | /* it's assumed that the first one has already reported |
| 149 | * the error, let's not spam with another one, and do |
| 150 | * not set ERR_ALERT. |
| 151 | */ |
| 152 | err |= ERR_RETRYABLE; |
| 153 | goto bind_ret_err; |
| 154 | } |
| 155 | /* taking the other one's FD will result in it being marked |
| 156 | * extern and being dup()ed. Let's mark the receiver as |
| 157 | * inherited so that it properly bypasses all second-stage |
| 158 | * setup and avoids being passed to new processes. |
| 159 | */ |
| 160 | rx->flags |= RX_F_INHERITED; |
| 161 | rx->fd = rx->shard_info->ref->fd; |
| 162 | } |
| 163 | |
Willy Tarreau | 62292b2 | 2020-09-02 17:52:23 +0200 | [diff] [blame] | 164 | if (rx->fd == -1) { |
| 165 | err |= ERR_FATAL | ERR_ALERT; |
| 166 | memprintf(errmsg, "sockpair may be only used with inherited FDs"); |
| 167 | goto bind_return; |
| 168 | } |
| 169 | |
| 170 | if (rx->fd >= global.maxsock) { |
| 171 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 172 | memprintf(errmsg, "not enough free sockets (raise '-n' parameter)"); |
| 173 | goto bind_close_return; |
| 174 | } |
| 175 | |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 176 | if (fd_set_nonblock(rx->fd) == -1) { |
Willy Tarreau | 62292b2 | 2020-09-02 17:52:23 +0200 | [diff] [blame] | 177 | err |= ERR_FATAL | ERR_ALERT; |
| 178 | memprintf(errmsg, "cannot make socket non-blocking"); |
| 179 | goto bind_close_return; |
| 180 | } |
| 181 | |
| 182 | rx->flags |= RX_F_BOUND; |
| 183 | |
Willy Tarreau | 9464bb1 | 2022-07-05 05:16:13 +0200 | [diff] [blame] | 184 | fd_insert(rx->fd, rx->owner, rx->iocb, rx->bind_tgroup, rx->bind_thread); |
Willy Tarreau | 62292b2 | 2020-09-02 17:52:23 +0200 | [diff] [blame] | 185 | return err; |
| 186 | |
| 187 | bind_return: |
| 188 | if (errmsg && *errmsg) |
Willy Tarreau | 6823a3a | 2021-10-14 11:59:15 +0200 | [diff] [blame] | 189 | memprintf(errmsg, "%s for [fd %d]", *errmsg, rx->fd); |
Willy Tarreau | 62292b2 | 2020-09-02 17:52:23 +0200 | [diff] [blame] | 190 | |
Willy Tarreau | 0e1aaf4 | 2023-02-27 16:39:32 +0100 | [diff] [blame] | 191 | bind_ret_err: |
Willy Tarreau | 62292b2 | 2020-09-02 17:52:23 +0200 | [diff] [blame] | 192 | return err; |
| 193 | |
| 194 | bind_close_return: |
| 195 | close(rx->fd); |
| 196 | goto bind_return; |
| 197 | } |
| 198 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 199 | /* This function changes the state from ASSIGNED to LISTEN. The socket is NOT |
| 200 | * enabled for polling. The return value is composed from ERR_NONE, |
| 201 | * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in |
| 202 | * <errmsg> if the message is at most <errlen> bytes long (including '\0'). |
| 203 | * Note that <errmsg> may be NULL if <errlen> is also zero. |
| 204 | */ |
| 205 | static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 206 | { |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 207 | int err; |
Willy Tarreau | 9eda7a6 | 2020-09-02 18:02:00 +0200 | [diff] [blame] | 208 | char *msg = NULL; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 209 | |
| 210 | err = ERR_NONE; |
| 211 | |
| 212 | /* ensure we never return garbage */ |
| 213 | if (errlen) |
| 214 | *errmsg = 0; |
| 215 | |
| 216 | if (listener->state != LI_ASSIGNED) |
| 217 | return ERR_NONE; /* already bound */ |
| 218 | |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 219 | if (!(listener->rx.flags & RX_F_BOUND)) { |
| 220 | msg = "receiving socket not bound"; |
| 221 | goto err_return; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 222 | } |
Willy Tarreau | ad33acf | 2020-09-02 18:40:02 +0200 | [diff] [blame] | 223 | |
Willy Tarreau | a37b244 | 2020-09-24 07:23:45 +0200 | [diff] [blame] | 224 | listener_set_state(listener, LI_LISTEN); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 225 | return err; |
| 226 | |
| 227 | err_return: |
| 228 | if (msg && errlen) |
Willy Tarreau | 9eda7a6 | 2020-09-02 18:02:00 +0200 | [diff] [blame] | 229 | snprintf(errmsg, errlen, "%s [fd %d]", msg, listener->rx.fd); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 230 | return err; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Send FD over a unix socket |
| 235 | * |
| 236 | * <send_fd> is the FD to send |
| 237 | * <fd> is the fd of the unix socket to use for the transfer |
| 238 | * |
| 239 | * The iobuf variable could be use in the future to enhance the protocol. |
| 240 | */ |
| 241 | int send_fd_uxst(int fd, int send_fd) |
| 242 | { |
| 243 | char iobuf[2]; |
| 244 | struct iovec iov; |
| 245 | struct msghdr msghdr; |
| 246 | |
| 247 | char cmsgbuf[CMSG_SPACE(sizeof(int))]; |
| 248 | char buf[CMSG_SPACE(sizeof(int))]; |
| 249 | struct cmsghdr *cmsg = (void *)buf; |
| 250 | |
| 251 | int *fdptr; |
| 252 | |
| 253 | iov.iov_base = iobuf; |
| 254 | iov.iov_len = sizeof(iobuf); |
| 255 | |
| 256 | memset(&msghdr, 0, sizeof(msghdr)); |
| 257 | msghdr.msg_iov = &iov; |
| 258 | msghdr.msg_iovlen = 1; |
| 259 | |
| 260 | /* Now send the fds */ |
| 261 | msghdr.msg_control = cmsgbuf; |
| 262 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); |
| 263 | |
| 264 | cmsg = CMSG_FIRSTHDR(&msghdr); |
| 265 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 266 | cmsg->cmsg_level = SOL_SOCKET; |
| 267 | cmsg->cmsg_type = SCM_RIGHTS; |
| 268 | |
| 269 | fdptr = (int *)CMSG_DATA(cmsg); |
| 270 | memcpy(fdptr, &send_fd, sizeof(send_fd)); |
| 271 | |
| 272 | if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) { |
William Lallemand | f67e8fb | 2022-07-25 15:51:30 +0200 | [diff] [blame] | 273 | return -1; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * |
Joseph Herlant | 8bb32ae | 2018-11-25 11:43:27 -0800 | [diff] [blame] | 281 | * This function works like uxst_connect_server but instead of creating a |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 282 | * socket and establishing a connection, it creates a pair of connected |
| 283 | * sockets, and send one of them through the destination FD. The destination FD |
Willy Tarreau | 3f4fa09 | 2019-07-17 16:42:04 +0200 | [diff] [blame] | 284 | * is stored in conn->dst->sin_addr.s_addr during configuration parsing. |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 285 | * |
| 286 | * conn->target may point either to a valid server or to a backend, depending |
| 287 | * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The |
| 288 | * <data> parameter is a boolean indicating whether there are data waiting for |
| 289 | * being sent or not, in order to adjust data write polling and on some |
| 290 | * platforms. The <delack> argument is ignored. |
| 291 | * |
| 292 | * Note that a pending send_proxy message accounts for data. |
| 293 | * |
| 294 | * It can return one of : |
| 295 | * - SF_ERR_NONE if everything's OK |
| 296 | * - SF_ERR_SRVTO if there are no more servers |
| 297 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 298 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 299 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 300 | * - SF_ERR_INTERNAL for any other purely internal errors |
| 301 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 302 | * |
| 303 | * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise |
| 304 | * it's invalid and the caller has nothing to do. |
| 305 | */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 306 | static int sockpair_connect_server(struct connection *conn, int flags) |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 307 | { |
| 308 | int sv[2], fd, dst_fd = -1; |
| 309 | |
Willy Tarreau | 158b6cf | 2022-05-02 17:45:12 +0200 | [diff] [blame] | 310 | BUG_ON(!conn->dst); |
| 311 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 312 | /* the FD is stored in the sockaddr struct */ |
Willy Tarreau | 3f4fa09 | 2019-07-17 16:42:04 +0200 | [diff] [blame] | 313 | dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 314 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 315 | if (obj_type(conn->target) != OBJ_TYPE_PROXY && |
| 316 | obj_type(conn->target) != OBJ_TYPE_SERVER) { |
| 317 | conn->flags |= CO_FL_ERROR; |
| 318 | return SF_ERR_INTERNAL; |
| 319 | } |
| 320 | |
| 321 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { |
| 322 | ha_alert("socketpair(): Cannot create socketpair. Giving up.\n"); |
| 323 | conn->flags |= CO_FL_ERROR; |
| 324 | return SF_ERR_RESOURCE; |
| 325 | } |
| 326 | |
| 327 | fd = conn->handle.fd = sv[1]; |
| 328 | |
| 329 | if (fd >= global.maxsock) { |
| 330 | /* do not log anything there, it's a normal condition when this option |
| 331 | * is used to serialize connections to a server ! |
| 332 | */ |
| 333 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 334 | close(sv[0]); |
| 335 | close(sv[1]); |
| 336 | conn->err_code = CO_ER_CONF_FDLIM; |
| 337 | conn->flags |= CO_FL_ERROR; |
| 338 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
| 339 | } |
| 340 | |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 341 | if (fd_set_nonblock(fd) == -1) { |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 342 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 343 | close(sv[0]); |
| 344 | close(sv[1]); |
| 345 | conn->err_code = CO_ER_SOCK_ERR; |
| 346 | conn->flags |= CO_FL_ERROR; |
| 347 | return SF_ERR_INTERNAL; |
| 348 | } |
| 349 | |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 350 | if (master == 1 && fd_set_cloexec(fd) == -1) { |
William Lallemand | c03eb01 | 2018-11-27 12:02:37 +0100 | [diff] [blame] | 351 | ha_alert("Cannot set CLOEXEC on client socket.\n"); |
| 352 | close(sv[0]); |
| 353 | close(sv[1]); |
| 354 | conn->err_code = CO_ER_SOCK_ERR; |
| 355 | conn->flags |= CO_FL_ERROR; |
| 356 | return SF_ERR_INTERNAL; |
| 357 | } |
| 358 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 359 | if (global.tune.server_sndbuf) |
| 360 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 361 | |
| 362 | if (global.tune.server_rcvbuf) |
| 363 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 364 | |
| 365 | /* The new socket is sent on the other side, it should be retrieved and |
| 366 | * considered as an 'accept' socket on the server side */ |
| 367 | if (send_fd_uxst(dst_fd, sv[0]) == -1) { |
William Lallemand | 708949d | 2022-07-25 16:04:38 +0200 | [diff] [blame] | 368 | ha_alert("socketpair: Cannot transfer the fd %d over sockpair@%d. Giving up.\n", sv[0], dst_fd); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 369 | close(sv[0]); |
| 370 | close(sv[1]); |
| 371 | conn->err_code = CO_ER_SOCK_ERR; |
| 372 | conn->flags |= CO_FL_ERROR; |
| 373 | return SF_ERR_INTERNAL; |
| 374 | } |
| 375 | |
| 376 | close(sv[0]); /* we don't need this side anymore */ |
| 377 | |
| 378 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 379 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 380 | /* Prepare to send a few handshakes related to the on-wire protocol. */ |
| 381 | if (conn->send_proxy_ofs) |
| 382 | conn->flags |= CO_FL_SEND_PROXY; |
| 383 | |
| 384 | conn_ctrl_init(conn); /* registers the FD */ |
Willy Tarreau | b41a6e9 | 2021-04-06 17:49:19 +0200 | [diff] [blame] | 385 | HA_ATOMIC_AND(&fdtab[fd].state, ~FD_LINGER_RISK); /* no need to disable lingering */ |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 386 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 387 | return SF_ERR_NONE; /* connection is OK */ |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /* |
Joseph Herlant | 8bb32ae | 2018-11-25 11:43:27 -0800 | [diff] [blame] | 392 | * Receives a file descriptor transferred from a unix socket. |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 393 | * |
| 394 | * Return -1 or a socket fd; |
| 395 | * |
Joseph Herlant | 8bb32ae | 2018-11-25 11:43:27 -0800 | [diff] [blame] | 396 | * The iobuf variable could be used in the future to enhance the protocol. |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 397 | */ |
| 398 | int recv_fd_uxst(int sock) |
| 399 | { |
| 400 | struct msghdr msghdr; |
| 401 | struct iovec iov; |
| 402 | char iobuf[2]; |
| 403 | |
| 404 | char cmsgbuf[CMSG_SPACE(sizeof(int))]; |
| 405 | char buf[CMSG_SPACE(sizeof(int))]; |
| 406 | struct cmsghdr *cmsg = (void *)buf; |
| 407 | |
| 408 | |
| 409 | int recv_fd = -1; |
| 410 | int ret = -1; |
| 411 | |
| 412 | memset(&msghdr, 0, sizeof(msghdr)); |
| 413 | |
| 414 | iov.iov_base = iobuf; |
| 415 | iov.iov_len = sizeof(iobuf); |
| 416 | |
| 417 | msghdr.msg_iov = &iov; |
| 418 | msghdr.msg_iovlen = 1; |
| 419 | |
| 420 | msghdr.msg_control = cmsgbuf; |
| 421 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); |
| 422 | |
| 423 | iov.iov_len = sizeof(iobuf); |
| 424 | iov.iov_base = iobuf; |
| 425 | |
| 426 | while (1) { |
| 427 | ret = recvmsg(sock, &msghdr, 0); |
| 428 | if (ret == -1 && errno == EINTR) |
| 429 | continue; |
| 430 | else |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | if (ret == -1) |
| 435 | return ret; |
| 436 | |
| 437 | cmsg = CMSG_FIRSTHDR(&msghdr); |
Willy Tarreau | 7d7ab43 | 2018-09-20 11:39:39 +0200 | [diff] [blame] | 438 | if (cmsg && cmsg->cmsg_level == SOL_SOCKET && |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 439 | cmsg->cmsg_type == SCM_RIGHTS) { |
| 440 | size_t totlen = cmsg->cmsg_len - |
| 441 | CMSG_LEN(0); |
| 442 | memcpy(&recv_fd, CMSG_DATA(cmsg), totlen); |
| 443 | } |
| 444 | return recv_fd; |
| 445 | } |
| 446 | |
Willy Tarreau | cc8b653 | 2020-10-13 17:27:34 +0200 | [diff] [blame] | 447 | /* Tests if the receiver supports accepting connections. Returns positive on |
| 448 | * success, 0 if not possible, negative if the socket is non-recoverable. In |
| 449 | * practice zero is never returned since we don't support suspending sockets. |
| 450 | * The real test consists in verifying we have a connected SOCK_STREAM of |
| 451 | * family AF_UNIX. |
| 452 | */ |
Willy Tarreau | 7d053e4 | 2020-10-15 09:19:43 +0200 | [diff] [blame] | 453 | static int sockpair_accepting_conn(const struct receiver *rx) |
Willy Tarreau | cc8b653 | 2020-10-13 17:27:34 +0200 | [diff] [blame] | 454 | { |
| 455 | struct sockaddr sa; |
| 456 | socklen_t len; |
| 457 | int val; |
| 458 | |
| 459 | len = sizeof(val); |
| 460 | if (getsockopt(rx->fd, SOL_SOCKET, SO_TYPE, &val, &len) == -1) |
| 461 | return -1; |
| 462 | |
| 463 | if (val != SOCK_STREAM) |
| 464 | return -1; |
| 465 | |
| 466 | len = sizeof(sa); |
| 467 | if (getsockname(rx->fd, &sa, &len) != 0) |
| 468 | return -1; |
| 469 | |
| 470 | if (sa.sa_family != AF_UNIX) |
| 471 | return -1; |
| 472 | |
| 473 | len = sizeof(val); |
| 474 | if (getsockopt(rx->fd, SOL_SOCKET, SO_ACCEPTCONN, &val, &len) == -1) |
| 475 | return -1; |
| 476 | |
| 477 | /* Note: cannot be a listening socket, must be established */ |
| 478 | if (val) |
| 479 | return -1; |
| 480 | |
| 481 | return 1; |
| 482 | } |
| 483 | |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 484 | /* Accept an incoming connection from listener <l>, and return it, as well as |
| 485 | * a CO_AC_* status code into <status> if not null. Null is returned on error. |
| 486 | * <l> must be a valid listener with a valid frontend. |
| 487 | */ |
| 488 | struct connection *sockpair_accept_conn(struct listener *l, int *status) |
| 489 | { |
| 490 | struct proxy *p = l->bind_conf->frontend; |
| 491 | struct connection *conn = NULL; |
| 492 | int ret; |
| 493 | int cfd; |
| 494 | |
| 495 | if ((cfd = recv_fd_uxst(l->rx.fd)) != -1) |
Willy Tarreau | 3824743 | 2022-04-26 10:24:14 +0200 | [diff] [blame] | 496 | fd_set_nonblock(cfd); |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 497 | |
| 498 | if (likely(cfd != -1)) { |
| 499 | /* Perfect, the connection was accepted */ |
| 500 | conn = conn_new(&l->obj_type); |
| 501 | if (!conn) |
| 502 | goto fail_conn; |
| 503 | |
| 504 | if (!sockaddr_alloc(&conn->src, NULL, 0)) |
| 505 | goto fail_addr; |
| 506 | |
| 507 | /* just like with UNIX sockets, only the family is filled */ |
| 508 | conn->src->ss_family = AF_UNIX; |
| 509 | conn->handle.fd = cfd; |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 510 | ret = CO_AC_DONE; |
| 511 | goto done; |
| 512 | } |
| 513 | |
| 514 | switch (errno) { |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 515 | #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN |
| 516 | case EWOULDBLOCK: |
| 517 | #endif |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 518 | case EAGAIN: |
| 519 | ret = CO_AC_DONE; /* nothing more to accept */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 520 | if (fdtab[l->rx.fd].state & (FD_POLL_HUP|FD_POLL_ERR)) { |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 521 | /* the listening socket might have been disabled in a shared |
| 522 | * process and we're a collateral victim. We'll just pause for |
| 523 | * a while in case it comes back. In the mean time, we need to |
| 524 | * clear this sticky flag. |
| 525 | */ |
Willy Tarreau | f509065 | 2021-04-06 17:23:40 +0200 | [diff] [blame] | 526 | _HA_ATOMIC_AND(&fdtab[l->rx.fd].state, ~(FD_POLL_HUP|FD_POLL_ERR)); |
Willy Tarreau | 344b8fc | 2020-10-15 09:43:31 +0200 | [diff] [blame] | 527 | ret = CO_AC_PAUSE; |
| 528 | } |
| 529 | fd_cant_recv(l->rx.fd); |
| 530 | break; |
| 531 | |
| 532 | case EINVAL: |
| 533 | /* might be trying to accept on a shut fd (eg: soft stop) */ |
| 534 | ret = CO_AC_PAUSE; |
| 535 | break; |
| 536 | |
| 537 | case EINTR: |
| 538 | case ECONNABORTED: |
| 539 | ret = CO_AC_RETRY; |
| 540 | break; |
| 541 | |
| 542 | case ENFILE: |
| 543 | if (p) |
| 544 | send_log(p, LOG_EMERG, |
| 545 | "Proxy %s reached system FD limit (maxsock=%d). Please check system tunables.\n", |
| 546 | p->id, global.maxsock); |
| 547 | ret = CO_AC_PAUSE; |
| 548 | break; |
| 549 | |
| 550 | case EMFILE: |
| 551 | if (p) |
| 552 | send_log(p, LOG_EMERG, |
| 553 | "Proxy %s reached process FD limit (maxsock=%d). Please check 'ulimit-n' and restart.\n", |
| 554 | p->id, global.maxsock); |
| 555 | ret = CO_AC_PAUSE; |
| 556 | break; |
| 557 | |
| 558 | case ENOBUFS: |
| 559 | case ENOMEM: |
| 560 | if (p) |
| 561 | send_log(p, LOG_EMERG, |
| 562 | "Proxy %s reached system memory limit (maxsock=%d). Please check system tunables.\n", |
| 563 | p->id, global.maxsock); |
| 564 | ret = CO_AC_PAUSE; |
| 565 | break; |
| 566 | |
| 567 | default: |
| 568 | /* unexpected result, let's give up and let other tasks run */ |
| 569 | ret = CO_AC_YIELD; |
| 570 | } |
| 571 | done: |
| 572 | if (status) |
| 573 | *status = ret; |
| 574 | return conn; |
| 575 | |
| 576 | fail_addr: |
| 577 | conn_free(conn); |
| 578 | conn = NULL; |
| 579 | fail_conn: |
| 580 | ret = CO_AC_PAUSE; |
| 581 | goto done; |
| 582 | } |
| 583 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 584 | /* |
| 585 | * Local variables: |
| 586 | * c-indent-level: 8 |
| 587 | * c-basic-offset: 8 |
| 588 | * End: |
| 589 | */ |