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 | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 37 | #include <haproxy/protocol.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 38 | #include <haproxy/time.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 39 | #include <haproxy/tools.h> |
Willy Tarreau | d678805 | 2020-05-27 15:59:00 +0200 | [diff] [blame] | 40 | #include <haproxy/version.h> |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 41 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 42 | |
| 43 | static void sockpair_add_listener(struct listener *listener, int port); |
| 44 | static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen); |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 45 | static int sockpair_connect_server(struct connection *conn, int flags); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 46 | |
| 47 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 48 | static struct protocol proto_sockpair = { |
| 49 | .name = "sockpair", |
| 50 | .sock_domain = AF_CUST_SOCKPAIR, |
| 51 | .sock_type = SOCK_STREAM, |
| 52 | .sock_prot = 0, |
| 53 | .sock_family = AF_UNIX, |
| 54 | .sock_addrlen = sizeof(struct sockaddr_un), |
| 55 | .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),/* path len */ |
| 56 | .accept = &listener_accept, |
| 57 | .connect = &sockpair_connect_server, |
Willy Tarreau | b3580b1 | 2020-09-01 10:26:22 +0200 | [diff] [blame] | 58 | .listen = sockpair_bind_listener, |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 59 | .enable_all = enable_all_listeners, |
| 60 | .disable_all = disable_all_listeners, |
| 61 | .get_src = NULL, |
| 62 | .get_dst = NULL, |
| 63 | .pause = NULL, |
| 64 | .add = sockpair_add_listener, |
| 65 | .listeners = LIST_HEAD_INIT(proto_sockpair.listeners), |
| 66 | .nb_listeners = 0, |
| 67 | }; |
| 68 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 69 | INITCALL1(STG_REGISTER, protocol_register, &proto_sockpair); |
| 70 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 71 | /* Add <listener> to the list of sockpair listeners (port is ignored). The |
| 72 | * listener's state is automatically updated from LI_INIT to LI_ASSIGNED. |
| 73 | * The number of listeners for the protocol is updated. |
Willy Tarreau | daacf36 | 2019-07-24 16:45:02 +0200 | [diff] [blame] | 74 | * |
| 75 | * Must be called with proto_lock held. |
| 76 | * |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 77 | */ |
| 78 | static void sockpair_add_listener(struct listener *listener, int port) |
| 79 | { |
| 80 | if (listener->state != LI_INIT) |
| 81 | return; |
| 82 | listener->state = LI_ASSIGNED; |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 83 | listener->rx.proto = &proto_sockpair; |
| 84 | LIST_ADDQ(&proto_sockpair.listeners, &listener->rx.proto_list); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 85 | proto_sockpair.nb_listeners++; |
| 86 | } |
| 87 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 88 | /* This function changes the state from ASSIGNED to LISTEN. The socket is NOT |
| 89 | * enabled for polling. The return value is composed from ERR_NONE, |
| 90 | * ERR_RETRYABLE and ERR_FATAL. It may return a warning or an error message in |
| 91 | * <errmsg> if the message is at most <errlen> bytes long (including '\0'). |
| 92 | * Note that <errmsg> may be NULL if <errlen> is also zero. |
| 93 | */ |
| 94 | static int sockpair_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 95 | { |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame] | 96 | int fd = listener->rx.fd; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 97 | int err; |
| 98 | const char *msg = NULL; |
| 99 | |
| 100 | err = ERR_NONE; |
| 101 | |
| 102 | /* ensure we never return garbage */ |
| 103 | if (errlen) |
| 104 | *errmsg = 0; |
| 105 | |
| 106 | if (listener->state != LI_ASSIGNED) |
| 107 | return ERR_NONE; /* already bound */ |
| 108 | |
Willy Tarreau | 0b91501 | 2020-09-01 10:47:07 +0200 | [diff] [blame] | 109 | if (listener->rx.flags & RX_F_BOUND) |
| 110 | goto bound; |
| 111 | |
Willy Tarreau | 38ba647 | 2020-08-27 08:16:52 +0200 | [diff] [blame] | 112 | if (listener->rx.fd == -1) { |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 113 | err |= ERR_FATAL | ERR_ALERT; |
| 114 | msg = "sockpair can be only used with inherited FDs"; |
| 115 | goto err_return; |
| 116 | } |
| 117 | |
| 118 | if (fd >= global.maxsock) { |
| 119 | err |= ERR_FATAL | ERR_ALERT; |
| 120 | msg = "socket(): not enough free sockets, raise -n argument"; |
| 121 | goto err_return; |
| 122 | } |
| 123 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| 124 | err |= ERR_FATAL | ERR_ALERT; |
| 125 | msg = "cannot make sockpair non-blocking"; |
| 126 | goto err_return; |
| 127 | } |
Willy Tarreau | 0b91501 | 2020-09-01 10:47:07 +0200 | [diff] [blame] | 128 | listener->rx.flags |= RX_F_BOUND; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 129 | |
Willy Tarreau | 0b91501 | 2020-09-01 10:47:07 +0200 | [diff] [blame] | 130 | bound: |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 131 | listener->state = LI_LISTEN; |
| 132 | |
Willy Tarreau | b743661 | 2020-08-28 19:51:44 +0200 | [diff] [blame] | 133 | fd_insert(fd, listener, listener->rx.proto->accept, |
Willy Tarreau | 818a92e | 2020-09-03 07:50:19 +0200 | [diff] [blame] | 134 | thread_mask(listener->rx.settings->bind_thread) & all_threads_mask); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 135 | |
| 136 | return err; |
| 137 | |
| 138 | err_return: |
| 139 | if (msg && errlen) |
| 140 | snprintf(errmsg, errlen, "%s [fd %d]", msg, fd); |
| 141 | return err; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Send FD over a unix socket |
| 146 | * |
| 147 | * <send_fd> is the FD to send |
| 148 | * <fd> is the fd of the unix socket to use for the transfer |
| 149 | * |
| 150 | * The iobuf variable could be use in the future to enhance the protocol. |
| 151 | */ |
| 152 | int send_fd_uxst(int fd, int send_fd) |
| 153 | { |
| 154 | char iobuf[2]; |
| 155 | struct iovec iov; |
| 156 | struct msghdr msghdr; |
| 157 | |
| 158 | char cmsgbuf[CMSG_SPACE(sizeof(int))]; |
| 159 | char buf[CMSG_SPACE(sizeof(int))]; |
| 160 | struct cmsghdr *cmsg = (void *)buf; |
| 161 | |
| 162 | int *fdptr; |
| 163 | |
| 164 | iov.iov_base = iobuf; |
| 165 | iov.iov_len = sizeof(iobuf); |
| 166 | |
| 167 | memset(&msghdr, 0, sizeof(msghdr)); |
| 168 | msghdr.msg_iov = &iov; |
| 169 | msghdr.msg_iovlen = 1; |
| 170 | |
| 171 | /* Now send the fds */ |
| 172 | msghdr.msg_control = cmsgbuf; |
| 173 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); |
| 174 | |
| 175 | cmsg = CMSG_FIRSTHDR(&msghdr); |
| 176 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
| 177 | cmsg->cmsg_level = SOL_SOCKET; |
| 178 | cmsg->cmsg_type = SCM_RIGHTS; |
| 179 | |
| 180 | fdptr = (int *)CMSG_DATA(cmsg); |
| 181 | memcpy(fdptr, &send_fd, sizeof(send_fd)); |
| 182 | |
| 183 | if (sendmsg(fd, &msghdr, 0) != sizeof(iobuf)) { |
| 184 | ha_warning("Failed to transfer socket\n"); |
| 185 | return 1; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * |
Joseph Herlant | 8bb32ae | 2018-11-25 11:43:27 -0800 | [diff] [blame] | 193 | * This function works like uxst_connect_server but instead of creating a |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 194 | * socket and establishing a connection, it creates a pair of connected |
| 195 | * 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] | 196 | * is stored in conn->dst->sin_addr.s_addr during configuration parsing. |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 197 | * |
| 198 | * conn->target may point either to a valid server or to a backend, depending |
| 199 | * on conn->target. Only OBJ_TYPE_PROXY and OBJ_TYPE_SERVER are supported. The |
| 200 | * <data> parameter is a boolean indicating whether there are data waiting for |
| 201 | * being sent or not, in order to adjust data write polling and on some |
| 202 | * platforms. The <delack> argument is ignored. |
| 203 | * |
| 204 | * Note that a pending send_proxy message accounts for data. |
| 205 | * |
| 206 | * It can return one of : |
| 207 | * - SF_ERR_NONE if everything's OK |
| 208 | * - SF_ERR_SRVTO if there are no more servers |
| 209 | * - SF_ERR_SRVCL if the connection was refused by the server |
| 210 | * - SF_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 211 | * - SF_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 212 | * - SF_ERR_INTERNAL for any other purely internal errors |
| 213 | * Additionally, in the case of SF_ERR_RESOURCE, an emergency log will be emitted. |
| 214 | * |
| 215 | * The connection's fd is inserted only when SF_ERR_NONE is returned, otherwise |
| 216 | * it's invalid and the caller has nothing to do. |
| 217 | */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 218 | static int sockpair_connect_server(struct connection *conn, int flags) |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 219 | { |
| 220 | int sv[2], fd, dst_fd = -1; |
| 221 | |
| 222 | /* the FD is stored in the sockaddr struct */ |
Willy Tarreau | 3f4fa09 | 2019-07-17 16:42:04 +0200 | [diff] [blame] | 223 | dst_fd = ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 224 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 225 | if (obj_type(conn->target) != OBJ_TYPE_PROXY && |
| 226 | obj_type(conn->target) != OBJ_TYPE_SERVER) { |
| 227 | conn->flags |= CO_FL_ERROR; |
| 228 | return SF_ERR_INTERNAL; |
| 229 | } |
| 230 | |
| 231 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) { |
| 232 | ha_alert("socketpair(): Cannot create socketpair. Giving up.\n"); |
| 233 | conn->flags |= CO_FL_ERROR; |
| 234 | return SF_ERR_RESOURCE; |
| 235 | } |
| 236 | |
| 237 | fd = conn->handle.fd = sv[1]; |
| 238 | |
| 239 | if (fd >= global.maxsock) { |
| 240 | /* do not log anything there, it's a normal condition when this option |
| 241 | * is used to serialize connections to a server ! |
| 242 | */ |
| 243 | ha_alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 244 | close(sv[0]); |
| 245 | close(sv[1]); |
| 246 | conn->err_code = CO_ER_CONF_FDLIM; |
| 247 | conn->flags |= CO_FL_ERROR; |
| 248 | return SF_ERR_PRXCOND; /* it is a configuration limit */ |
| 249 | } |
| 250 | |
| 251 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
| 252 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 253 | close(sv[0]); |
| 254 | close(sv[1]); |
| 255 | conn->err_code = CO_ER_SOCK_ERR; |
| 256 | conn->flags |= CO_FL_ERROR; |
| 257 | return SF_ERR_INTERNAL; |
| 258 | } |
| 259 | |
William Lallemand | c03eb01 | 2018-11-27 12:02:37 +0100 | [diff] [blame] | 260 | if (master == 1 && (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)) { |
| 261 | ha_alert("Cannot set CLOEXEC on client socket.\n"); |
| 262 | close(sv[0]); |
| 263 | close(sv[1]); |
| 264 | conn->err_code = CO_ER_SOCK_ERR; |
| 265 | conn->flags |= CO_FL_ERROR; |
| 266 | return SF_ERR_INTERNAL; |
| 267 | } |
| 268 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 269 | /* if a send_proxy is there, there are data */ |
Olivier Houchard | fdcb007 | 2019-05-06 18:32:29 +0200 | [diff] [blame] | 270 | if (conn->send_proxy_ofs) |
| 271 | flags |= CONNECT_HAS_DATA; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 272 | |
| 273 | if (global.tune.server_sndbuf) |
| 274 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 275 | |
| 276 | if (global.tune.server_rcvbuf) |
| 277 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 278 | |
| 279 | /* The new socket is sent on the other side, it should be retrieved and |
| 280 | * considered as an 'accept' socket on the server side */ |
| 281 | if (send_fd_uxst(dst_fd, sv[0]) == -1) { |
| 282 | close(sv[0]); |
| 283 | close(sv[1]); |
| 284 | conn->err_code = CO_ER_SOCK_ERR; |
| 285 | conn->flags |= CO_FL_ERROR; |
| 286 | return SF_ERR_INTERNAL; |
| 287 | } |
| 288 | |
| 289 | close(sv[0]); /* we don't need this side anymore */ |
| 290 | |
| 291 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 292 | |
| 293 | conn->flags |= CO_FL_ADDR_TO_SET; |
| 294 | |
| 295 | /* Prepare to send a few handshakes related to the on-wire protocol. */ |
| 296 | if (conn->send_proxy_ofs) |
| 297 | conn->flags |= CO_FL_SEND_PROXY; |
| 298 | |
| 299 | conn_ctrl_init(conn); /* registers the FD */ |
| 300 | fdtab[fd].linger_risk = 0; /* no need to disable lingering */ |
| 301 | |
| 302 | if (conn_xprt_init(conn) < 0) { |
| 303 | conn_full_close(conn); |
| 304 | conn->flags |= CO_FL_ERROR; |
| 305 | return SF_ERR_RESOURCE; |
| 306 | } |
| 307 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 308 | return SF_ERR_NONE; /* connection is OK */ |
| 309 | } |
| 310 | |
| 311 | |
| 312 | /* |
Joseph Herlant | 8bb32ae | 2018-11-25 11:43:27 -0800 | [diff] [blame] | 313 | * Receives a file descriptor transferred from a unix socket. |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 314 | * |
| 315 | * Return -1 or a socket fd; |
| 316 | * |
Joseph Herlant | 8bb32ae | 2018-11-25 11:43:27 -0800 | [diff] [blame] | 317 | * 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] | 318 | */ |
| 319 | int recv_fd_uxst(int sock) |
| 320 | { |
| 321 | struct msghdr msghdr; |
| 322 | struct iovec iov; |
| 323 | char iobuf[2]; |
| 324 | |
| 325 | char cmsgbuf[CMSG_SPACE(sizeof(int))]; |
| 326 | char buf[CMSG_SPACE(sizeof(int))]; |
| 327 | struct cmsghdr *cmsg = (void *)buf; |
| 328 | |
| 329 | |
| 330 | int recv_fd = -1; |
| 331 | int ret = -1; |
| 332 | |
| 333 | memset(&msghdr, 0, sizeof(msghdr)); |
| 334 | |
| 335 | iov.iov_base = iobuf; |
| 336 | iov.iov_len = sizeof(iobuf); |
| 337 | |
| 338 | msghdr.msg_iov = &iov; |
| 339 | msghdr.msg_iovlen = 1; |
| 340 | |
| 341 | msghdr.msg_control = cmsgbuf; |
| 342 | msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); |
| 343 | |
| 344 | iov.iov_len = sizeof(iobuf); |
| 345 | iov.iov_base = iobuf; |
| 346 | |
| 347 | while (1) { |
| 348 | ret = recvmsg(sock, &msghdr, 0); |
| 349 | if (ret == -1 && errno == EINTR) |
| 350 | continue; |
| 351 | else |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | if (ret == -1) |
| 356 | return ret; |
| 357 | |
| 358 | cmsg = CMSG_FIRSTHDR(&msghdr); |
Willy Tarreau | 7d7ab43 | 2018-09-20 11:39:39 +0200 | [diff] [blame] | 359 | if (cmsg && cmsg->cmsg_level == SOL_SOCKET && |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 360 | cmsg->cmsg_type == SCM_RIGHTS) { |
| 361 | size_t totlen = cmsg->cmsg_len - |
| 362 | CMSG_LEN(0); |
| 363 | memcpy(&recv_fd, CMSG_DATA(cmsg), totlen); |
| 364 | } |
| 365 | return recv_fd; |
| 366 | } |
| 367 | |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 368 | /* |
| 369 | * Local variables: |
| 370 | * c-indent-level: 8 |
| 371 | * c-basic-offset: 8 |
| 372 | * End: |
| 373 | */ |