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