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