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