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