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