Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Connection management functions |
| 3 | * |
| 4 | * Copyright 2000-2012 Willy Tarreau <w@1wt.eu> |
| 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 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 13 | #include <errno.h> |
| 14 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 15 | #include <haproxy/api.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 16 | #include <haproxy/cfgparse.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 17 | #include <haproxy/connection.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/fd.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 19 | #include <haproxy/frontend.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <haproxy/hash.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 21 | #include <haproxy/log-t.h> |
Willy Tarreau | 7a00efb | 2020-06-02 17:02:59 +0200 | [diff] [blame] | 22 | #include <haproxy/namespace.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 23 | #include <haproxy/net_helper.h> |
Willy Tarreau | fc77454 | 2020-06-04 17:31:04 +0200 | [diff] [blame] | 24 | #include <haproxy/proto_tcp.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 25 | #include <haproxy/sample.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 26 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 27 | #include <haproxy/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 28 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 29 | |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 30 | DECLARE_POOL(pool_head_connection, "connection", sizeof(struct connection)); |
| 31 | DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream)); |
Willy Tarreau | ff5d57b | 2019-07-17 18:37:02 +0200 | [diff] [blame] | 32 | DECLARE_POOL(pool_head_sockaddr, "sockaddr", sizeof(struct sockaddr_storage)); |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 33 | DECLARE_POOL(pool_head_authority, "authority", PP2_AUTHORITY_MAX); |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 34 | |
Willy Tarreau | 4d82bf5 | 2020-06-28 00:19:17 +0200 | [diff] [blame] | 35 | struct idle_conns idle_conns[MAX_THREADS] = { }; |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 36 | struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, }; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 37 | |
Christopher Faulet | 32f61c0 | 2018-04-10 14:33:41 +0200 | [diff] [blame] | 38 | /* List head of all known muxes for PROTO */ |
| 39 | struct mux_proto_list mux_proto_list = { |
| 40 | .list = LIST_HEAD_INIT(mux_proto_list.list) |
Willy Tarreau | 2386be6 | 2017-09-21 19:40:52 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 43 | /* disables sending of proxy-protocol-v2's LOCAL command */ |
| 44 | static int pp2_never_send_local; |
| 45 | |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 46 | int conn_create_mux(struct connection *conn) |
| 47 | { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 48 | if (conn_is_back(conn)) { |
| 49 | struct server *srv; |
| 50 | struct conn_stream *cs = conn->ctx; |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 51 | struct session *sess = conn->owner; |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 52 | |
| 53 | if (conn->flags & CO_FL_ERROR) |
| 54 | goto fail; |
Olivier Houchard | a8a415d | 2020-01-23 13:15:14 +0100 | [diff] [blame] | 55 | |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 56 | if (sess && obj_type(sess->origin) == OBJ_TYPE_CHECK) { |
Willy Tarreau | 38b4d2e | 2020-11-20 17:08:15 +0100 | [diff] [blame] | 57 | if (conn_install_mux_chk(conn, conn->ctx, sess) < 0) |
Christopher Faulet | 14cd316 | 2020-04-16 14:50:06 +0200 | [diff] [blame] | 58 | goto fail; |
| 59 | } |
Willy Tarreau | 38b4d2e | 2020-11-20 17:08:15 +0100 | [diff] [blame] | 60 | else if (conn_install_mux_be(conn, conn->ctx, sess) < 0) |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 61 | goto fail; |
| 62 | srv = objt_server(conn->target); |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 63 | |
| 64 | /* If we're doing http-reuse always, and the connection is not |
| 65 | * private with available streams (an http2 connection), add it |
| 66 | * to the available list, so that others can use it right |
| 67 | * away. If the connection is private, add it in the session |
| 68 | * server list. |
| 69 | */ |
Christopher Faulet | 2883fcf | 2020-07-01 14:59:43 +0200 | [diff] [blame] | 70 | if (srv && ((srv->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) && |
Christopher Faulet | aa27853 | 2020-06-30 14:47:46 +0200 | [diff] [blame] | 71 | !(conn->flags & CO_FL_PRIVATE) && conn->mux->avail_streams(conn) > 0) |
Olivier Houchard | f0d4dff | 2020-03-06 18:12:03 +0100 | [diff] [blame] | 72 | LIST_ADDQ(&srv->available_conns[tid], mt_list_to_list(&conn->list)); |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 73 | else if (conn->flags & CO_FL_PRIVATE) { |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 74 | /* If it fail now, the same will be done in mux->detach() callback */ |
Willy Tarreau | 38b4d2e | 2020-11-20 17:08:15 +0100 | [diff] [blame] | 75 | session_add_conn(sess, conn, conn->target); |
Christopher Faulet | 08016ab | 2020-07-01 16:10:06 +0200 | [diff] [blame] | 76 | } |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 77 | return 0; |
| 78 | fail: |
| 79 | /* let the upper layer know the connection failed */ |
| 80 | cs->data_cb->wake(cs); |
| 81 | return -1; |
| 82 | } else |
| 83 | return conn_complete_session(conn); |
| 84 | |
| 85 | } |
| 86 | |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 87 | /* I/O callback for fd-based connections. It calls the read/write handlers |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 88 | * provided by the connection's sock_ops, which must be valid. |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 89 | */ |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 90 | void conn_fd_handler(int fd) |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 91 | { |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 92 | struct connection *conn = fdtab[fd].owner; |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 93 | unsigned int flags; |
Willy Tarreau | 8de5c4f | 2020-03-04 17:45:21 +0100 | [diff] [blame] | 94 | int need_wake = 0; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 95 | |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 96 | if (unlikely(!conn)) { |
| 97 | activity[tid].conn_dead++; |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 98 | return; |
Willy Tarreau | d80cb4e | 2018-01-20 19:30:13 +0100 | [diff] [blame] | 99 | } |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 100 | |
Willy Tarreau | 7d28149 | 2012-12-16 19:19:13 +0100 | [diff] [blame] | 101 | flags = conn->flags & ~CO_FL_ERROR; /* ensure to call the wake handler upon error */ |
Willy Tarreau | d29a066 | 2012-12-10 16:33:38 +0100 | [diff] [blame] | 102 | |
Willy Tarreau | b2a7ab0 | 2019-12-27 10:54:22 +0100 | [diff] [blame] | 103 | if (unlikely(conn->flags & CO_FL_WAIT_L4_CONN) && |
| 104 | ((fd_send_ready(fd) && fd_send_active(fd)) || |
| 105 | (fd_recv_ready(fd) && fd_recv_active(fd)))) { |
| 106 | /* Still waiting for a connection to establish and nothing was |
| 107 | * attempted yet to probe the connection. this will clear the |
| 108 | * CO_FL_WAIT_L4_CONN flag on success. |
| 109 | */ |
| 110 | if (!conn_fd_check(conn)) |
| 111 | goto leave; |
Willy Tarreau | 8de5c4f | 2020-03-04 17:45:21 +0100 | [diff] [blame] | 112 | need_wake = 1; |
Willy Tarreau | b2a7ab0 | 2019-12-27 10:54:22 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Willy Tarreau | 8081abe | 2019-11-28 18:08:49 +0100 | [diff] [blame] | 115 | if (fd_send_ready(fd) && fd_send_active(fd)) { |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 116 | /* force reporting of activity by clearing the previous flags : |
| 117 | * we'll have at least ERROR or CONNECTED at the end of an I/O, |
| 118 | * both of which will be detected below. |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 119 | */ |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 120 | flags = 0; |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 121 | if (conn->subs && conn->subs->events & SUB_RETRY_SEND) { |
Willy Tarreau | 8de5c4f | 2020-03-04 17:45:21 +0100 | [diff] [blame] | 122 | need_wake = 0; // wake will be called after this I/O |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 123 | tasklet_wakeup(conn->subs->tasklet); |
| 124 | conn->subs->events &= ~SUB_RETRY_SEND; |
| 125 | if (!conn->subs->events) |
| 126 | conn->subs = NULL; |
Willy Tarreau | 8de5c4f | 2020-03-04 17:45:21 +0100 | [diff] [blame] | 127 | } |
Willy Tarreau | 667fefd | 2020-03-04 17:22:10 +0100 | [diff] [blame] | 128 | fd_stop_send(fd); |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 129 | } |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 130 | |
Willy Tarreau | 57ec32f | 2017-04-11 19:59:33 +0200 | [diff] [blame] | 131 | /* The data transfer starts here and stops on error and handshakes. Note |
| 132 | * that we must absolutely test conn->xprt at each step in case it suddenly |
| 133 | * changes due to a quick unexpected close(). |
| 134 | */ |
Willy Tarreau | 8081abe | 2019-11-28 18:08:49 +0100 | [diff] [blame] | 135 | if (fd_recv_ready(fd) && fd_recv_active(fd)) { |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 136 | /* force reporting of activity by clearing the previous flags : |
| 137 | * we'll have at least ERROR or CONNECTED at the end of an I/O, |
| 138 | * both of which will be detected below. |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 139 | */ |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 140 | flags = 0; |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 141 | if (conn->subs && conn->subs->events & SUB_RETRY_RECV) { |
Willy Tarreau | 8de5c4f | 2020-03-04 17:45:21 +0100 | [diff] [blame] | 142 | need_wake = 0; // wake will be called after this I/O |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 143 | tasklet_wakeup(conn->subs->tasklet); |
| 144 | conn->subs->events &= ~SUB_RETRY_RECV; |
| 145 | if (!conn->subs->events) |
| 146 | conn->subs = NULL; |
Willy Tarreau | 8de5c4f | 2020-03-04 17:45:21 +0100 | [diff] [blame] | 147 | } |
Willy Tarreau | 4cabfc1 | 2020-06-17 16:26:22 +0200 | [diff] [blame] | 148 | fd_stop_recv(fd); |
Willy Tarreau | 9e272bf | 2012-10-03 21:04:48 +0200 | [diff] [blame] | 149 | } |
Willy Tarreau | 2da156f | 2012-07-23 15:07:23 +0200 | [diff] [blame] | 150 | |
Willy Tarreau | 2c6be84 | 2012-07-06 17:12:34 +0200 | [diff] [blame] | 151 | leave: |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 152 | /* If we don't yet have a mux, that means we were waiting for |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 153 | * information to create one, typically from the ALPN. If we're |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 154 | * done with the handshake, attempt to create one. |
Willy Tarreau | 8e3c6ce | 2017-08-28 15:46:01 +0200 | [diff] [blame] | 155 | */ |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 156 | if (unlikely(!conn->mux) && !(conn->flags & CO_FL_WAIT_XPRT)) |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 157 | if (conn_create_mux(conn) < 0) |
| 158 | return; |
Willy Tarreau | 8e3c6ce | 2017-08-28 15:46:01 +0200 | [diff] [blame] | 159 | |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 160 | /* The wake callback is normally used to notify the data layer about |
| 161 | * data layer activity (successful send/recv), connection establishment, |
| 162 | * shutdown and fatal errors. We need to consider the following |
| 163 | * situations to wake up the data layer : |
Willy Tarreau | 0fbc318 | 2019-12-27 14:57:45 +0100 | [diff] [blame] | 164 | * - change among the CO_FL_NOTIFY_DONE flags : |
| 165 | * SOCK_{RD,WR}_SH, ERROR, |
Willy Tarreau | 3c0cc49 | 2017-03-19 07:54:28 +0100 | [diff] [blame] | 166 | * - absence of any of {L4,L6}_CONN and CONNECTED, indicating the |
| 167 | * end of handshake and transition to CONNECTED |
| 168 | * - raise of CONNECTED with HANDSHAKE down |
| 169 | * - end of HANDSHAKE with CONNECTED set |
| 170 | * - regular data layer activity |
| 171 | * |
| 172 | * Note that the wake callback is allowed to release the connection and |
| 173 | * the fd (and return < 0 in this case). |
Willy Tarreau | 2396c1c | 2012-10-03 21:12:16 +0200 | [diff] [blame] | 174 | */ |
Willy Tarreau | 8de5c4f | 2020-03-04 17:45:21 +0100 | [diff] [blame] | 175 | if ((need_wake || ((conn->flags ^ flags) & CO_FL_NOTIFY_DONE) || |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 176 | ((flags & CO_FL_WAIT_XPRT) && !(conn->flags & CO_FL_WAIT_XPRT))) && |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 177 | conn->mux && conn->mux->wake && conn->mux->wake(conn) < 0) |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 178 | return; |
Willy Tarreau | fd31e53 | 2012-07-23 18:24:25 +0200 | [diff] [blame] | 179 | |
Willy Tarreau | f9dabec | 2012-08-17 17:33:53 +0200 | [diff] [blame] | 180 | /* commit polling changes */ |
| 181 | conn_cond_update_polling(conn); |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 182 | return; |
Willy Tarreau | 59f9839 | 2012-07-06 14:13:49 +0200 | [diff] [blame] | 183 | } |
Willy Tarreau | b5e2cbd | 2012-08-17 11:55:04 +0200 | [diff] [blame] | 184 | |
Willy Tarreau | 4970e5a | 2019-12-27 10:40:21 +0100 | [diff] [blame] | 185 | /* This is the callback which is set when a connection establishment is pending |
| 186 | * and we have nothing to send. It may update the FD polling status to indicate |
| 187 | * !READY. It returns 0 if it fails in a fatal way or needs to poll to go |
| 188 | * further, otherwise it returns non-zero and removes the CO_FL_WAIT_L4_CONN |
| 189 | * flag from the connection's flags. In case of error, it sets CO_FL_ERROR and |
| 190 | * leaves the error code in errno. |
| 191 | */ |
| 192 | int conn_fd_check(struct connection *conn) |
| 193 | { |
| 194 | struct sockaddr_storage *addr; |
| 195 | int fd = conn->handle.fd; |
| 196 | |
| 197 | if (conn->flags & CO_FL_ERROR) |
| 198 | return 0; |
| 199 | |
| 200 | if (!conn_ctrl_ready(conn)) |
| 201 | return 0; |
| 202 | |
| 203 | if (!(conn->flags & CO_FL_WAIT_L4_CONN)) |
| 204 | return 1; /* strange we were called while ready */ |
| 205 | |
| 206 | if (!fd_send_ready(fd)) |
| 207 | return 0; |
| 208 | |
| 209 | /* Here we have 2 cases : |
| 210 | * - modern pollers, able to report ERR/HUP. If these ones return any |
| 211 | * of these flags then it's likely a failure, otherwise it possibly |
| 212 | * is a success (i.e. there may have been data received just before |
| 213 | * the error was reported). |
| 214 | * - select, which doesn't report these and with which it's always |
| 215 | * necessary either to try connect() again or to check for SO_ERROR. |
| 216 | * In order to simplify everything, we double-check using connect() as |
| 217 | * soon as we meet either of these delicate situations. Note that |
| 218 | * SO_ERROR would clear the error after reporting it! |
| 219 | */ |
| 220 | if (cur_poller.flags & HAP_POLL_F_ERRHUP) { |
| 221 | /* modern poller, able to report ERR/HUP */ |
| 222 | if ((fdtab[fd].ev & (FD_POLL_IN|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_IN) |
| 223 | goto done; |
| 224 | if ((fdtab[fd].ev & (FD_POLL_OUT|FD_POLL_ERR|FD_POLL_HUP)) == FD_POLL_OUT) |
| 225 | goto done; |
| 226 | if (!(fdtab[fd].ev & (FD_POLL_ERR|FD_POLL_HUP))) |
| 227 | goto wait; |
| 228 | /* error present, fall through common error check path */ |
| 229 | } |
| 230 | |
| 231 | /* Use connect() to check the state of the socket. This has the double |
| 232 | * advantage of *not* clearing the error (so that health checks can |
| 233 | * still use getsockopt(SO_ERROR)) and giving us the following info : |
| 234 | * - error |
| 235 | * - connecting (EALREADY, EINPROGRESS) |
| 236 | * - connected (EISCONN, 0) |
| 237 | */ |
| 238 | addr = conn->dst; |
| 239 | if ((conn->flags & CO_FL_SOCKS4) && obj_type(conn->target) == OBJ_TYPE_SERVER) |
| 240 | addr = &objt_server(conn->target)->socks4_addr; |
| 241 | |
| 242 | if (connect(fd, (const struct sockaddr *)addr, get_addr_len(addr)) == -1) { |
| 243 | if (errno == EALREADY || errno == EINPROGRESS) |
| 244 | goto wait; |
| 245 | |
| 246 | if (errno && errno != EISCONN) |
| 247 | goto out_error; |
| 248 | } |
| 249 | |
| 250 | done: |
| 251 | /* The FD is ready now, we'll mark the connection as complete and |
| 252 | * forward the event to the transport layer which will notify the |
| 253 | * data layer. |
| 254 | */ |
| 255 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 256 | fd_may_send(fd); |
| 257 | fd_cond_recv(fd); |
| 258 | errno = 0; // make health checks happy |
| 259 | return 1; |
| 260 | |
| 261 | out_error: |
| 262 | /* Write error on the file descriptor. Report it to the connection |
| 263 | * and disable polling on this FD. |
| 264 | */ |
| 265 | fdtab[fd].linger_risk = 0; |
| 266 | conn->flags |= CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
Willy Tarreau | 5d4d180 | 2020-02-21 09:58:29 +0100 | [diff] [blame] | 267 | conn_stop_polling(conn); |
Willy Tarreau | 4970e5a | 2019-12-27 10:40:21 +0100 | [diff] [blame] | 268 | return 0; |
| 269 | |
| 270 | wait: |
Willy Tarreau | 4970e5a | 2019-12-27 10:40:21 +0100 | [diff] [blame] | 271 | fd_cant_send(fd); |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 272 | fd_want_send(fd); |
Willy Tarreau | 4970e5a | 2019-12-27 10:40:21 +0100 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 276 | /* Send a message over an established connection. It makes use of send() and |
| 277 | * returns the same return code and errno. If the socket layer is not ready yet |
| 278 | * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked |
| 279 | * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns |
| 280 | * EMSGSIZE if called with a zero length message. The purpose is to simplify |
| 281 | * some rare attempts to directly write on the socket from above the connection |
| 282 | * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send". |
| 283 | * It automatically retries on EINTR. Other errors cause the connection to be |
| 284 | * marked as in error state. It takes similar arguments as send() except the |
| 285 | * first one which is the connection instead of the file descriptor. Note, |
| 286 | * MSG_DONTWAIT and MSG_NOSIGNAL are forced on the flags. |
| 287 | */ |
| 288 | int conn_sock_send(struct connection *conn, const void *buf, int len, int flags) |
| 289 | { |
| 290 | int ret; |
| 291 | |
| 292 | ret = -1; |
| 293 | errno = ENOTSOCK; |
| 294 | |
| 295 | if (conn->flags & CO_FL_SOCK_WR_SH) |
| 296 | goto fail; |
| 297 | |
| 298 | if (!conn_ctrl_ready(conn)) |
| 299 | goto fail; |
| 300 | |
| 301 | errno = EMSGSIZE; |
| 302 | if (!len) |
| 303 | goto fail; |
| 304 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 305 | if (!fd_send_ready(conn->handle.fd)) |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 306 | goto wait; |
| 307 | |
| 308 | do { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 309 | ret = send(conn->handle.fd, buf, len, flags | MSG_DONTWAIT | MSG_NOSIGNAL); |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 310 | } while (ret < 0 && errno == EINTR); |
| 311 | |
| 312 | |
Willy Tarreau | ccf3f6d | 2019-09-05 17:05:05 +0200 | [diff] [blame] | 313 | if (ret > 0) { |
| 314 | if (conn->flags & CO_FL_WAIT_L4_CONN) { |
| 315 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 316 | fd_may_send(conn->handle.fd); |
| 317 | fd_cond_recv(conn->handle.fd); |
| 318 | } |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 319 | return ret; |
Willy Tarreau | ccf3f6d | 2019-09-05 17:05:05 +0200 | [diff] [blame] | 320 | } |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 321 | |
| 322 | if (ret == 0 || errno == EAGAIN || errno == ENOTCONN) { |
| 323 | wait: |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 324 | fd_cant_send(conn->handle.fd); |
Willy Tarreau | ff3e648 | 2015-03-12 23:56:52 +0100 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | fail: |
| 328 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR; |
| 329 | return ret; |
| 330 | } |
| 331 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 332 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 333 | * event subscriber <es> is not allowed to change from a previous call as long |
| 334 | * as at least one event is still subscribed. The <event_type> must only be a |
| 335 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0. |
| 336 | */ |
| 337 | int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 338 | { |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 339 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 340 | BUG_ON(conn->subs && conn->subs != es); |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 341 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 342 | es->events &= ~event_type; |
| 343 | if (!es->events) |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 344 | conn->subs = NULL; |
| 345 | |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 346 | if (conn_ctrl_ready(conn)) { |
| 347 | if (event_type & SUB_RETRY_RECV) |
| 348 | fd_stop_recv(conn->handle.fd); |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 349 | |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 350 | if (event_type & SUB_RETRY_SEND) |
| 351 | fd_stop_send(conn->handle.fd); |
| 352 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
Willy Tarreau | 7e59c0a | 2020-02-28 14:24:49 +0100 | [diff] [blame] | 356 | /* Called from the upper layer, to subscribe <es> to events <event_type>. |
| 357 | * The <es> struct is not allowed to differ from the one passed during a |
| 358 | * previous call to subscribe(). If the FD is ready, the wait_event is |
| 359 | * immediately woken up and the subcription is cancelled. It always |
| 360 | * returns zero. |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 361 | */ |
| 362 | int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 363 | { |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 364 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 365 | BUG_ON(conn->subs && conn->subs != es); |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 366 | |
Willy Tarreau | 7e59c0a | 2020-02-28 14:24:49 +0100 | [diff] [blame] | 367 | if (conn->subs && (conn->subs->events & event_type) == event_type) |
| 368 | return 0; |
| 369 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 370 | conn->subs = es; |
| 371 | es->events |= event_type; |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 372 | |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 373 | if (conn_ctrl_ready(conn)) { |
Willy Tarreau | 7e59c0a | 2020-02-28 14:24:49 +0100 | [diff] [blame] | 374 | if (event_type & SUB_RETRY_RECV) { |
| 375 | if (fd_recv_ready(conn->handle.fd)) { |
| 376 | tasklet_wakeup(es->tasklet); |
| 377 | es->events &= ~SUB_RETRY_RECV; |
| 378 | if (!es->events) |
| 379 | conn->subs = NULL; |
| 380 | } |
| 381 | else |
| 382 | fd_want_recv(conn->handle.fd); |
| 383 | } |
Willy Tarreau | 7872d1f | 2020-01-10 07:06:05 +0100 | [diff] [blame] | 384 | |
Willy Tarreau | 7e59c0a | 2020-02-28 14:24:49 +0100 | [diff] [blame] | 385 | if (event_type & SUB_RETRY_SEND) { |
| 386 | if (fd_send_ready(conn->handle.fd)) { |
| 387 | tasklet_wakeup(es->tasklet); |
| 388 | es->events &= ~SUB_RETRY_SEND; |
| 389 | if (!es->events) |
| 390 | conn->subs = NULL; |
| 391 | } |
| 392 | else |
| 393 | fd_want_send(conn->handle.fd); |
| 394 | } |
Willy Tarreau | d1d14c3 | 2020-02-21 10:34:19 +0100 | [diff] [blame] | 395 | } |
Olivier Houchard | 83a0cd8 | 2018-09-28 17:57:58 +0200 | [diff] [blame] | 396 | return 0; |
Olivier Houchard | 6ff2039 | 2018-07-17 18:46:31 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 399 | /* Drains possibly pending incoming data on the file descriptor attached to the |
| 400 | * connection and update the connection's flags accordingly. This is used to |
| 401 | * know whether we need to disable lingering on close. Returns non-zero if it |
| 402 | * is safe to close without disabling lingering, otherwise zero. The SOCK_RD_SH |
| 403 | * flag may also be updated if the incoming shutdown was reported by the drain() |
| 404 | * function. |
| 405 | */ |
| 406 | int conn_sock_drain(struct connection *conn) |
| 407 | { |
Willy Tarreau | e215bba | 2018-08-24 14:31:53 +0200 | [diff] [blame] | 408 | int turns = 2; |
| 409 | int len; |
| 410 | |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 411 | if (!conn_ctrl_ready(conn)) |
| 412 | return 1; |
| 413 | |
| 414 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH)) |
| 415 | return 1; |
| 416 | |
Willy Tarreau | e215bba | 2018-08-24 14:31:53 +0200 | [diff] [blame] | 417 | if (fdtab[conn->handle.fd].ev & (FD_POLL_ERR|FD_POLL_HUP)) |
| 418 | goto shut; |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 419 | |
Willy Tarreau | e215bba | 2018-08-24 14:31:53 +0200 | [diff] [blame] | 420 | if (!fd_recv_ready(conn->handle.fd)) |
| 421 | return 0; |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 422 | |
Willy Tarreau | e215bba | 2018-08-24 14:31:53 +0200 | [diff] [blame] | 423 | /* no drain function defined, use the generic one */ |
| 424 | |
| 425 | while (turns) { |
| 426 | #ifdef MSG_TRUNC_CLEARS_INPUT |
| 427 | len = recv(conn->handle.fd, NULL, INT_MAX, MSG_DONTWAIT | MSG_NOSIGNAL | MSG_TRUNC); |
| 428 | if (len == -1 && errno == EFAULT) |
| 429 | #endif |
| 430 | len = recv(conn->handle.fd, trash.area, trash.size, |
| 431 | MSG_DONTWAIT | MSG_NOSIGNAL); |
| 432 | |
| 433 | if (len == 0) |
| 434 | goto shut; |
| 435 | |
| 436 | if (len < 0) { |
| 437 | if (errno == EAGAIN) { |
| 438 | /* connection not closed yet */ |
| 439 | fd_cant_recv(conn->handle.fd); |
| 440 | break; |
| 441 | } |
| 442 | if (errno == EINTR) /* oops, try again */ |
| 443 | continue; |
| 444 | /* other errors indicate a dead connection, fine. */ |
| 445 | goto shut; |
| 446 | } |
| 447 | /* OK we read some data, let's try again once */ |
| 448 | turns--; |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 449 | } |
| 450 | |
Willy Tarreau | e215bba | 2018-08-24 14:31:53 +0200 | [diff] [blame] | 451 | /* some data are still present, give up */ |
| 452 | return 0; |
| 453 | |
| 454 | shut: |
| 455 | /* we're certain the connection was shut down */ |
| 456 | fdtab[conn->handle.fd].linger_risk = 0; |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 457 | conn->flags |= CO_FL_SOCK_RD_SH; |
| 458 | return 1; |
| 459 | } |
| 460 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 461 | /* |
| 462 | * Get data length from tlv |
| 463 | */ |
Tim Duesterhus | ba837ec | 2020-03-05 23:11:02 +0100 | [diff] [blame] | 464 | static inline size_t get_tlv_length(const struct tlv *src) |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 465 | { |
| 466 | return (src->length_hi << 8) | src->length_lo; |
| 467 | } |
| 468 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 469 | /* This handshake handler waits a PROXY protocol header at the beginning of the |
| 470 | * raw data stream. The header looks like this : |
| 471 | * |
| 472 | * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n" |
| 473 | * |
| 474 | * There must be exactly one space between each field. Fields are : |
| 475 | * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6". |
| 476 | * - SRC3 : layer 3 (eg: IP) source address in standard text form |
| 477 | * - DST3 : layer 3 (eg: IP) destination address in standard text form |
| 478 | * - SRC4 : layer 4 (eg: TCP port) source address in standard text form |
| 479 | * - DST4 : layer 4 (eg: TCP port) destination address in standard text form |
| 480 | * |
| 481 | * This line MUST be at the beginning of the buffer and MUST NOT wrap. |
| 482 | * |
| 483 | * The header line is small and in all cases smaller than the smallest normal |
| 484 | * TCP MSS. So it MUST always be delivered as one segment, which ensures we |
| 485 | * can safely use MSG_PEEK and avoid buffering. |
| 486 | * |
| 487 | * Once the data is fetched, the values are set in the connection's address |
| 488 | * fields, and data are removed from the socket's buffer. The function returns |
| 489 | * zero if it needs to wait for more data or if it fails, or 1 if it completed |
| 490 | * and removed itself. |
| 491 | */ |
| 492 | int conn_recv_proxy(struct connection *conn, int flag) |
| 493 | { |
| 494 | char *line, *end; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 495 | struct proxy_hdr_v2 *hdr_v2; |
| 496 | const char v2sig[] = PP2_SIGNATURE; |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 497 | size_t total_v2_len; |
Tim Duesterhus | ba837ec | 2020-03-05 23:11:02 +0100 | [diff] [blame] | 498 | size_t tlv_offset = 0; |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 499 | int ret; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 500 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 501 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 502 | goto fail; |
| 503 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 504 | if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0)) |
Willy Tarreau | ca79f59 | 2019-07-17 19:04:47 +0200 | [diff] [blame] | 505 | goto fail; |
| 506 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 507 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 508 | goto not_ready; |
Willy Tarreau | fd803bb | 2014-01-20 15:13:07 +0100 | [diff] [blame] | 509 | |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 510 | while (1) { |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 511 | ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK); |
| 512 | if (ret < 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 513 | if (errno == EINTR) |
| 514 | continue; |
| 515 | if (errno == EAGAIN) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 516 | fd_cant_recv(conn->handle.fd); |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 517 | goto not_ready; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 518 | } |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 519 | goto recv_abort; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 520 | } |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 521 | trash.data = ret; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 522 | break; |
| 523 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 524 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 525 | if (!trash.data) { |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 526 | /* client shutdown */ |
| 527 | conn->err_code = CO_ER_PRX_EMPTY; |
| 528 | goto fail; |
| 529 | } |
| 530 | |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 531 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 532 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 533 | if (trash.data < 6) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 534 | goto missing; |
| 535 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 536 | line = trash.area; |
| 537 | end = trash.area + trash.data; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 538 | |
| 539 | /* Decode a possible proxy request, fail early if it does not match */ |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 540 | if (strncmp(line, "PROXY ", 6) != 0) |
| 541 | goto not_v1; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 542 | |
| 543 | line += 6; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 544 | if (trash.data < 9) /* shortest possible line */ |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 545 | goto missing; |
| 546 | |
David CARLIER | 42ff05e | 2016-03-24 09:22:36 +0000 | [diff] [blame] | 547 | if (memcmp(line, "TCP4 ", 5) == 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 548 | u32 src3, dst3, sport, dport; |
| 549 | |
| 550 | line += 5; |
| 551 | |
| 552 | src3 = inetaddr_host_lim_ret(line, end, &line); |
| 553 | if (line == end) |
| 554 | goto missing; |
| 555 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 556 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 557 | |
| 558 | dst3 = inetaddr_host_lim_ret(line, end, &line); |
| 559 | if (line == end) |
| 560 | goto missing; |
| 561 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 562 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 563 | |
| 564 | sport = read_uint((const char **)&line, end); |
| 565 | if (line == end) |
| 566 | goto missing; |
| 567 | if (*line++ != ' ') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 568 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 569 | |
| 570 | dport = read_uint((const char **)&line, end); |
| 571 | if (line > end - 2) |
| 572 | goto missing; |
| 573 | if (*line++ != '\r') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 574 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 575 | if (*line++ != '\n') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 576 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 577 | |
| 578 | /* update the session's addresses and mark them set */ |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 579 | ((struct sockaddr_in *)conn->src)->sin_family = AF_INET; |
| 580 | ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3); |
| 581 | ((struct sockaddr_in *)conn->src)->sin_port = htons(sport); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 582 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 583 | ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET; |
| 584 | ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3); |
| 585 | ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 586 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 587 | } |
David CARLIER | 42ff05e | 2016-03-24 09:22:36 +0000 | [diff] [blame] | 588 | else if (memcmp(line, "TCP6 ", 5) == 0) { |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 589 | u32 sport, dport; |
| 590 | char *src_s; |
| 591 | char *dst_s, *sport_s, *dport_s; |
| 592 | struct in6_addr src3, dst3; |
| 593 | |
| 594 | line += 5; |
| 595 | |
| 596 | src_s = line; |
| 597 | dst_s = sport_s = dport_s = NULL; |
| 598 | while (1) { |
| 599 | if (line > end - 2) { |
| 600 | goto missing; |
| 601 | } |
| 602 | else if (*line == '\r') { |
| 603 | *line = 0; |
| 604 | line++; |
| 605 | if (*line++ != '\n') |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 606 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 607 | break; |
| 608 | } |
| 609 | |
| 610 | if (*line == ' ') { |
| 611 | *line = 0; |
| 612 | if (!dst_s) |
| 613 | dst_s = line + 1; |
| 614 | else if (!sport_s) |
| 615 | sport_s = line + 1; |
| 616 | else if (!dport_s) |
| 617 | dport_s = line + 1; |
| 618 | } |
| 619 | line++; |
| 620 | } |
| 621 | |
| 622 | if (!dst_s || !sport_s || !dport_s) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 623 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 624 | |
| 625 | sport = read_uint((const char **)&sport_s,dport_s - 1); |
| 626 | if (*sport_s != 0) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 627 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 628 | |
| 629 | dport = read_uint((const char **)&dport_s,line - 2); |
| 630 | if (*dport_s != 0) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 631 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 632 | |
| 633 | if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 634 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 635 | |
| 636 | if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 637 | goto bad_header; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 638 | |
| 639 | /* update the session's addresses and mark them set */ |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 640 | ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6; |
| 641 | memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr)); |
| 642 | ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 643 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 644 | ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6; |
| 645 | memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr)); |
| 646 | ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 647 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 648 | } |
Willy Tarreau | 4c20d29 | 2014-06-14 11:41:36 +0200 | [diff] [blame] | 649 | else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) { |
| 650 | /* This can be a UNIX socket forwarded by an haproxy upstream */ |
| 651 | line += 9; |
| 652 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 653 | else { |
Willy Tarreau | 4c20d29 | 2014-06-14 11:41:36 +0200 | [diff] [blame] | 654 | /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */ |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 655 | conn->err_code = CO_ER_PRX_BAD_PROTO; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 656 | goto fail; |
| 657 | } |
| 658 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 659 | trash.data = line - trash.area; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 660 | goto eat_header; |
| 661 | |
| 662 | not_v1: |
| 663 | /* try PPv2 */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 664 | if (trash.data < PP2_HEADER_LEN) |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 665 | goto missing; |
| 666 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 667 | hdr_v2 = (struct proxy_hdr_v2 *) trash.area; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 668 | |
| 669 | if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 || |
| 670 | (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) { |
| 671 | conn->err_code = CO_ER_PRX_NOT_HDR; |
| 672 | goto fail; |
| 673 | } |
| 674 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 675 | total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len); |
| 676 | if (trash.data < total_v2_len) |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 677 | goto missing; |
| 678 | |
| 679 | switch (hdr_v2->ver_cmd & PP2_CMD_MASK) { |
| 680 | case 0x01: /* PROXY command */ |
| 681 | switch (hdr_v2->fam) { |
| 682 | case 0x11: /* TCPv4 */ |
KOVACS Krisztian | efd3aa9 | 2014-11-19 10:53:20 +0100 | [diff] [blame] | 683 | if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET) |
| 684 | goto bad_header; |
| 685 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 686 | ((struct sockaddr_in *)conn->src)->sin_family = AF_INET; |
| 687 | ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr; |
| 688 | ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port; |
| 689 | ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET; |
| 690 | ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr; |
| 691 | ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 692 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
KOVACS Krisztian | 7209c20 | 2015-07-03 14:09:10 +0200 | [diff] [blame] | 693 | tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 694 | break; |
| 695 | case 0x21: /* TCPv6 */ |
KOVACS Krisztian | efd3aa9 | 2014-11-19 10:53:20 +0100 | [diff] [blame] | 696 | if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6) |
| 697 | goto bad_header; |
| 698 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 699 | ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6; |
| 700 | memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16); |
| 701 | ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port; |
| 702 | ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6; |
| 703 | memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16); |
| 704 | ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 705 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
KOVACS Krisztian | 7209c20 | 2015-07-03 14:09:10 +0200 | [diff] [blame] | 706 | tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 707 | break; |
| 708 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 709 | |
| 710 | /* TLV parsing */ |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 711 | while (tlv_offset < total_v2_len) { |
| 712 | struct tlv *tlv_packet; |
Tim Duesterhus | ba837ec | 2020-03-05 23:11:02 +0100 | [diff] [blame] | 713 | size_t tlv_len; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 714 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 715 | /* Verify that we have at least TLV_HEADER_SIZE bytes left */ |
| 716 | if (tlv_offset + TLV_HEADER_SIZE > total_v2_len) |
| 717 | goto bad_header; |
| 718 | |
| 719 | tlv_packet = (struct tlv *) &trash.area[tlv_offset]; |
| 720 | tlv_len = get_tlv_length(tlv_packet); |
| 721 | tlv_offset += tlv_len + TLV_HEADER_SIZE; |
| 722 | |
| 723 | /* Verify that the TLV length does not exceed the total PROXYv2 length */ |
| 724 | if (tlv_offset > total_v2_len) |
| 725 | goto bad_header; |
| 726 | |
| 727 | switch (tlv_packet->type) { |
| 728 | case PP2_TYPE_CRC32C: { |
| 729 | uint32_t n_crc32c; |
| 730 | |
| 731 | /* Verify that this TLV is exactly 4 bytes long */ |
| 732 | if (tlv_len != 4) |
| 733 | goto bad_header; |
| 734 | |
| 735 | n_crc32c = read_n32(tlv_packet->value); |
| 736 | write_n32(tlv_packet->value, 0); // compute with CRC==0 |
| 737 | |
| 738 | if (hash_crc32c(trash.area, total_v2_len) != n_crc32c) |
| 739 | goto bad_header; |
| 740 | break; |
| 741 | } |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 742 | #ifdef USE_NS |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 743 | case PP2_TYPE_NETNS: { |
| 744 | const struct netns_entry *ns; |
| 745 | |
| 746 | ns = netns_store_lookup((char*)tlv_packet->value, tlv_len); |
| 747 | if (ns) |
| 748 | conn->proxy_netns = ns; |
| 749 | break; |
| 750 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 751 | #endif |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 752 | case PP2_TYPE_AUTHORITY: { |
| 753 | if (tlv_len > PP2_AUTHORITY_MAX) |
| 754 | goto bad_header; |
| 755 | conn->proxy_authority = pool_alloc(pool_head_authority); |
| 756 | if (conn->proxy_authority == NULL) |
| 757 | goto fail; |
| 758 | memcpy(conn->proxy_authority, (const char *)tlv_packet->value, tlv_len); |
| 759 | conn->proxy_authority_len = tlv_len; |
| 760 | break; |
| 761 | } |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 762 | case PP2_TYPE_UNIQUE_ID: { |
| 763 | const struct ist tlv = ist2((const char *)tlv_packet->value, tlv_len); |
| 764 | |
| 765 | if (tlv.len > UNIQUEID_LEN) |
| 766 | goto bad_header; |
Tim Duesterhus | 2b7f6c2 | 2020-03-14 13:07:05 +0100 | [diff] [blame] | 767 | conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0); |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 768 | if (!isttest(conn->proxy_unique_id)) |
| 769 | goto fail; |
| 770 | if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) { |
| 771 | /* This is technically unreachable, because we verified above |
| 772 | * that the TLV value fits. |
| 773 | */ |
| 774 | goto fail; |
| 775 | } |
| 776 | break; |
| 777 | } |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 778 | default: |
| 779 | break; |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 783 | /* Verify that the PROXYv2 header ends at a TLV boundary. |
| 784 | * This is technically unreachable, because the TLV parsing already |
| 785 | * verifies that a TLV does not exceed the total length and also |
| 786 | * that there is space for a TLV header. |
| 787 | */ |
| 788 | if (tlv_offset != total_v2_len) |
| 789 | goto bad_header; |
| 790 | |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 791 | /* unsupported protocol, keep local connection address */ |
| 792 | break; |
| 793 | case 0x00: /* LOCAL command */ |
| 794 | /* keep local connection address for LOCAL */ |
| 795 | break; |
| 796 | default: |
| 797 | goto bad_header; /* not a supported command */ |
| 798 | } |
| 799 | |
Tim Duesterhus | 488ee7f | 2020-03-05 22:55:20 +0100 | [diff] [blame] | 800 | trash.data = total_v2_len; |
Willy Tarreau | 7799267 | 2014-06-14 11:06:17 +0200 | [diff] [blame] | 801 | goto eat_header; |
| 802 | |
| 803 | eat_header: |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 804 | /* remove the PROXY line from the request. For this we re-read the |
| 805 | * exact line at once. If we don't get the exact same result, we |
| 806 | * fail. |
| 807 | */ |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 808 | while (1) { |
Tim Duesterhus | a8692f3 | 2020-03-13 12:34:25 +0100 | [diff] [blame] | 809 | ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0); |
| 810 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 811 | if (len2 < 0 && errno == EINTR) |
| 812 | continue; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 813 | if (len2 != trash.data) |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 814 | goto recv_abort; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 815 | break; |
| 816 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 817 | |
| 818 | conn->flags &= ~flag; |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 819 | conn->flags |= CO_FL_RCVD_PROXY; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 820 | return 1; |
| 821 | |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 822 | not_ready: |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 823 | return 0; |
| 824 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 825 | missing: |
| 826 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 827 | * we have not read anything. Otherwise we need to fail because we won't |
| 828 | * be able to poll anymore. |
| 829 | */ |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 830 | conn->err_code = CO_ER_PRX_TRUNCATED; |
| 831 | goto fail; |
| 832 | |
| 833 | bad_header: |
| 834 | /* This is not a valid proxy protocol header */ |
| 835 | conn->err_code = CO_ER_PRX_BAD_HDR; |
| 836 | goto fail; |
| 837 | |
| 838 | recv_abort: |
| 839 | conn->err_code = CO_ER_PRX_ABORT; |
Willy Tarreau | 26f4a04 | 2013-12-04 23:44:10 +0100 | [diff] [blame] | 840 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
Willy Tarreau | 8e3bf69 | 2012-12-03 15:41:18 +0100 | [diff] [blame] | 841 | goto fail; |
| 842 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 843 | fail: |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 844 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 845 | return 0; |
| 846 | } |
| 847 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 848 | /* This handshake handler waits a NetScaler Client IP insertion header |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 849 | * at the beginning of the raw data stream. The header format is |
| 850 | * described in doc/netscaler-client-ip-insertion-protocol.txt |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 851 | * |
| 852 | * This line MUST be at the beginning of the buffer and MUST NOT be |
| 853 | * fragmented. |
| 854 | * |
| 855 | * The header line is small and in all cases smaller than the smallest normal |
| 856 | * TCP MSS. So it MUST always be delivered as one segment, which ensures we |
| 857 | * can safely use MSG_PEEK and avoid buffering. |
| 858 | * |
| 859 | * Once the data is fetched, the values are set in the connection's address |
| 860 | * fields, and data are removed from the socket's buffer. The function returns |
| 861 | * zero if it needs to wait for more data or if it fails, or 1 if it completed |
| 862 | * and removed itself. |
| 863 | */ |
| 864 | int conn_recv_netscaler_cip(struct connection *conn, int flag) |
| 865 | { |
| 866 | char *line; |
Bertrand Jacquin | 7d668f9 | 2017-12-13 01:23:39 +0000 | [diff] [blame] | 867 | uint32_t hdr_len; |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 868 | uint8_t ip_ver; |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 869 | int ret; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 870 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 871 | if (!conn_ctrl_ready(conn)) |
| 872 | goto fail; |
| 873 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 874 | if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0)) |
Olivier Houchard | 1a9dbe5 | 2020-01-22 15:31:09 +0100 | [diff] [blame] | 875 | goto fail; |
| 876 | |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 877 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 878 | goto not_ready; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 879 | |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 880 | while (1) { |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 881 | ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK); |
| 882 | if (ret < 0) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 883 | if (errno == EINTR) |
| 884 | continue; |
| 885 | if (errno == EAGAIN) { |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 886 | fd_cant_recv(conn->handle.fd); |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 887 | goto not_ready; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 888 | } |
| 889 | goto recv_abort; |
| 890 | } |
Willy Tarreau | b406b87 | 2018-08-22 05:20:32 +0200 | [diff] [blame] | 891 | trash.data = ret; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 892 | break; |
| 893 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 894 | |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 895 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 896 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 897 | if (!trash.data) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 898 | /* client shutdown */ |
| 899 | conn->err_code = CO_ER_CIP_EMPTY; |
| 900 | goto fail; |
| 901 | } |
| 902 | |
| 903 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 904 | * CIP magic, header length or |
| 905 | * CIP magic, CIP length, CIP type, header length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 906 | if (trash.data < 12) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 907 | goto missing; |
| 908 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 909 | line = trash.area; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 910 | |
| 911 | /* Decode a possible NetScaler Client IP request, fail early if |
| 912 | * it does not match */ |
Willy Tarreau | 1ac83af | 2020-02-25 10:06:49 +0100 | [diff] [blame] | 913 | if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 914 | goto bad_magic; |
| 915 | |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 916 | /* Legacy CIP protocol */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 917 | if ((trash.area[8] & 0xD0) == 0x40) { |
Willy Tarreau | 1ac83af | 2020-02-25 10:06:49 +0100 | [diff] [blame] | 918 | hdr_len = ntohl(read_u32((line+4))); |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 919 | line += 8; |
| 920 | } |
| 921 | /* Standard CIP protocol */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 922 | else if (trash.area[8] == 0x00) { |
Willy Tarreau | 1ac83af | 2020-02-25 10:06:49 +0100 | [diff] [blame] | 923 | hdr_len = ntohs(read_u32((line+10))); |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 924 | line += 12; |
| 925 | } |
| 926 | /* Unknown CIP protocol */ |
| 927 | else { |
| 928 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 929 | goto fail; |
| 930 | } |
| 931 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 932 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 72fa1ec | 2017-12-12 01:17:23 +0000 | [diff] [blame] | 933 | * a minimal IP header */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 934 | if (trash.data < 20) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 935 | goto missing; |
| 936 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 937 | /* Get IP version from the first four bits */ |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 938 | ip_ver = (*line & 0xf0) >> 4; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 939 | |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 940 | if (ip_ver == 4) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 941 | struct ip *hdr_ip4; |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 942 | struct my_tcphdr *hdr_tcp; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 943 | |
| 944 | hdr_ip4 = (struct ip *)line; |
| 945 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 946 | if (trash.data < 40 || trash.data < hdr_len) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 947 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 67de5a2 | 2017-12-13 01:15:05 +0000 | [diff] [blame] | 948 | * IPv4 header, TCP header */ |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 949 | goto missing; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 950 | } |
| 951 | else if (hdr_ip4->ip_p != IPPROTO_TCP) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 952 | /* The protocol does not include a TCP header */ |
| 953 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 954 | goto fail; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 955 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 956 | |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 957 | hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4)); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 958 | |
| 959 | /* update the session's addresses and mark them set */ |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 960 | ((struct sockaddr_in *)conn->src)->sin_family = AF_INET; |
| 961 | ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr; |
| 962 | ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 963 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 964 | ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET; |
| 965 | ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr; |
| 966 | ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 967 | |
| 968 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 969 | } |
Willy Tarreau | 0ca24aa | 2019-03-29 17:35:32 +0100 | [diff] [blame] | 970 | else if (ip_ver == 6) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 971 | struct ip6_hdr *hdr_ip6; |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 972 | struct my_tcphdr *hdr_tcp; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 973 | |
| 974 | hdr_ip6 = (struct ip6_hdr *)line; |
| 975 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 976 | if (trash.data < 60 || trash.data < hdr_len) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 977 | /* Fail if buffer length is not large enough to contain |
Bertrand Jacquin | 67de5a2 | 2017-12-13 01:15:05 +0000 | [diff] [blame] | 978 | * IPv6 header, TCP header */ |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 979 | goto missing; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 980 | } |
| 981 | else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) { |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 982 | /* The protocol does not include a TCP header */ |
| 983 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 984 | goto fail; |
Bertrand Jacquin | b387591 | 2017-12-13 00:58:51 +0000 | [diff] [blame] | 985 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 986 | |
David Carlier | 3015a2e | 2016-07-04 22:51:33 +0100 | [diff] [blame] | 987 | hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr)); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 988 | |
| 989 | /* update the session's addresses and mark them set */ |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 990 | ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6; |
| 991 | ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src; |
| 992 | ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 993 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 994 | ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6; |
| 995 | ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst; |
| 996 | ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 997 | |
| 998 | conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET; |
| 999 | } |
| 1000 | else { |
| 1001 | /* The protocol does not match something known (IPv4/IPv6) */ |
| 1002 | conn->err_code = CO_ER_CIP_BAD_PROTO; |
| 1003 | goto fail; |
| 1004 | } |
| 1005 | |
Bertrand Jacquin | 7d668f9 | 2017-12-13 01:23:39 +0000 | [diff] [blame] | 1006 | line += hdr_len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1007 | trash.data = line - trash.area; |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1008 | |
| 1009 | /* remove the NetScaler Client IP header from the request. For this |
| 1010 | * we re-read the exact line at once. If we don't get the exact same |
| 1011 | * result, we fail. |
| 1012 | */ |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1013 | while (1) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1014 | int len2 = recv(conn->handle.fd, trash.area, trash.data, 0); |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1015 | if (len2 < 0 && errno == EINTR) |
| 1016 | continue; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1017 | if (len2 != trash.data) |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1018 | goto recv_abort; |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1019 | break; |
| 1020 | } |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1021 | |
| 1022 | conn->flags &= ~flag; |
| 1023 | return 1; |
| 1024 | |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1025 | not_ready: |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1026 | return 0; |
| 1027 | |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1028 | missing: |
| 1029 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 1030 | * we have not read anything. Otherwise we need to fail because we won't |
| 1031 | * be able to poll anymore. |
| 1032 | */ |
| 1033 | conn->err_code = CO_ER_CIP_TRUNCATED; |
| 1034 | goto fail; |
| 1035 | |
| 1036 | bad_magic: |
| 1037 | conn->err_code = CO_ER_CIP_BAD_MAGIC; |
| 1038 | goto fail; |
| 1039 | |
| 1040 | recv_abort: |
| 1041 | conn->err_code = CO_ER_CIP_ABORT; |
| 1042 | conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH; |
| 1043 | goto fail; |
| 1044 | |
| 1045 | fail: |
Bertrand Jacquin | 93b227d | 2016-06-04 15:11:10 +0100 | [diff] [blame] | 1046 | conn->flags |= CO_FL_ERROR; |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1050 | |
| 1051 | int conn_send_socks4_proxy_request(struct connection *conn) |
| 1052 | { |
| 1053 | struct socks4_request req_line; |
| 1054 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1055 | if (!conn_ctrl_ready(conn)) |
| 1056 | goto out_error; |
| 1057 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 1058 | if (!conn_get_dst(conn)) |
| 1059 | goto out_error; |
| 1060 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1061 | req_line.version = 0x04; |
| 1062 | req_line.command = 0x01; |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 1063 | req_line.port = get_net_port(conn->dst); |
| 1064 | req_line.ip = is_inet_addr(conn->dst); |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1065 | memcpy(req_line.user_id, "HAProxy\0", 8); |
| 1066 | |
| 1067 | if (conn->send_proxy_ofs > 0) { |
| 1068 | /* |
| 1069 | * This is the first call to send the request |
| 1070 | */ |
| 1071 | conn->send_proxy_ofs = -(int)sizeof(req_line); |
| 1072 | } |
| 1073 | |
| 1074 | if (conn->send_proxy_ofs < 0) { |
| 1075 | int ret = 0; |
| 1076 | |
| 1077 | /* we are sending the socks4_req_line here. If the data layer |
| 1078 | * has a pending write, we'll also set MSG_MORE. |
| 1079 | */ |
| 1080 | ret = conn_sock_send( |
| 1081 | conn, |
| 1082 | ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs), |
| 1083 | -conn->send_proxy_ofs, |
Willy Tarreau | 19bc201 | 2020-02-21 08:46:19 +0100 | [diff] [blame] | 1084 | (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? MSG_MORE : 0); |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1085 | |
| 1086 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n", |
| 1087 | conn->handle.fd, -conn->send_proxy_ofs, ret); |
| 1088 | |
| 1089 | if (ret < 0) { |
| 1090 | goto out_error; |
| 1091 | } |
| 1092 | |
| 1093 | conn->send_proxy_ofs += ret; /* becomes zero once complete */ |
| 1094 | if (conn->send_proxy_ofs != 0) { |
| 1095 | goto out_wait; |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | /* OK we've the whole request sent */ |
| 1100 | conn->flags &= ~CO_FL_SOCKS4_SEND; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1101 | |
| 1102 | /* The connection is ready now, simply return and let the connection |
| 1103 | * handler notify upper layers if needed. |
| 1104 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1105 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1106 | |
| 1107 | if (conn->flags & CO_FL_SEND_PROXY) { |
| 1108 | /* |
| 1109 | * Get the send_proxy_ofs ready for the send_proxy due to we are |
| 1110 | * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done |
| 1111 | * before sending PROXY Protocol. |
| 1112 | */ |
| 1113 | conn->send_proxy_ofs = 1; |
| 1114 | } |
| 1115 | return 1; |
| 1116 | |
| 1117 | out_error: |
| 1118 | /* Write error on the file descriptor */ |
| 1119 | conn->flags |= CO_FL_ERROR; |
| 1120 | if (conn->err_code == CO_ER_NONE) { |
| 1121 | conn->err_code = CO_ER_SOCKS4_SEND; |
| 1122 | } |
| 1123 | return 0; |
| 1124 | |
| 1125 | out_wait: |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1126 | return 0; |
| 1127 | } |
| 1128 | |
| 1129 | int conn_recv_socks4_proxy_response(struct connection *conn) |
| 1130 | { |
| 1131 | char line[SOCKS4_HS_RSP_LEN]; |
| 1132 | int ret; |
| 1133 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1134 | if (!conn_ctrl_ready(conn)) |
| 1135 | goto fail; |
| 1136 | |
| 1137 | if (!fd_recv_ready(conn->handle.fd)) |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1138 | goto not_ready; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1139 | |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1140 | while (1) { |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1141 | /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00 |
| 1142 | * Try to peek into it, before all 8 bytes ready. |
| 1143 | */ |
| 1144 | ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK); |
| 1145 | |
| 1146 | if (ret == 0) { |
| 1147 | /* the socket has been closed or shutdown for send */ |
| 1148 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n", |
| 1149 | conn->handle.fd, ret, errno); |
| 1150 | if (conn->err_code == CO_ER_NONE) { |
| 1151 | conn->err_code = CO_ER_SOCKS4_RECV; |
| 1152 | } |
| 1153 | goto fail; |
| 1154 | } |
| 1155 | |
| 1156 | if (ret > 0) { |
| 1157 | if (ret == SOCKS4_HS_RSP_LEN) { |
| 1158 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n", |
| 1159 | conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]); |
| 1160 | }else{ |
| 1161 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], first byte is [%02X], last bye is [%02X]\n", conn->handle.fd, ret, line[0], line[ret-1]); |
| 1162 | } |
| 1163 | } else { |
| 1164 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno); |
| 1165 | } |
| 1166 | |
| 1167 | if (ret < 0) { |
| 1168 | if (errno == EINTR) { |
| 1169 | continue; |
| 1170 | } |
| 1171 | if (errno == EAGAIN) { |
| 1172 | fd_cant_recv(conn->handle.fd); |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1173 | goto not_ready; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1174 | } |
| 1175 | goto recv_abort; |
| 1176 | } |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1177 | break; |
| 1178 | } |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1179 | |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1180 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 1181 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1182 | if (ret < SOCKS4_HS_RSP_LEN) { |
| 1183 | /* Missing data. Since we're using MSG_PEEK, we can only poll again if |
| 1184 | * we are not able to read enough data. |
| 1185 | */ |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1186 | goto not_ready; |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /* |
| 1190 | * Base on the SOCSK4 protocol: |
| 1191 | * |
| 1192 | * +----+----+----+----+----+----+----+----+ |
| 1193 | * | VN | CD | DSTPORT | DSTIP | |
| 1194 | * +----+----+----+----+----+----+----+----+ |
| 1195 | * # of bytes: 1 1 2 4 |
| 1196 | * VN is the version of the reply code and should be 0. CD is the result |
| 1197 | * code with one of the following values: |
| 1198 | * 90: request granted |
| 1199 | * 91: request rejected or failed |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 1200 | * 92: request rejected because SOCKS server cannot connect to identd on the client |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1201 | * 93: request rejected because the client program and identd report different user-ids |
| 1202 | * The remaining fields are ignored. |
| 1203 | */ |
| 1204 | if (line[1] != 90) { |
| 1205 | conn->flags &= ~CO_FL_SOCKS4_RECV; |
| 1206 | |
| 1207 | DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n", |
| 1208 | conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]); |
| 1209 | if (conn->err_code == CO_ER_NONE) { |
| 1210 | conn->err_code = CO_ER_SOCKS4_DENY; |
| 1211 | } |
| 1212 | goto fail; |
| 1213 | } |
| 1214 | |
| 1215 | /* remove the 8 bytes response from the stream */ |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1216 | while (1) { |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1217 | ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0); |
| 1218 | if (ret < 0 && errno == EINTR) { |
| 1219 | continue; |
| 1220 | } |
| 1221 | if (ret != SOCKS4_HS_RSP_LEN) { |
| 1222 | if (conn->err_code == CO_ER_NONE) { |
| 1223 | conn->err_code = CO_ER_SOCKS4_RECV; |
| 1224 | } |
| 1225 | goto fail; |
| 1226 | } |
Willy Tarreau | 157788c | 2020-02-11 10:08:05 +0100 | [diff] [blame] | 1227 | break; |
| 1228 | } |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1229 | |
| 1230 | conn->flags &= ~CO_FL_SOCKS4_RECV; |
| 1231 | return 1; |
| 1232 | |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1233 | not_ready: |
Willy Tarreau | 6499b9d | 2019-06-03 08:17:30 +0200 | [diff] [blame] | 1234 | return 0; |
| 1235 | |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1236 | recv_abort: |
| 1237 | if (conn->err_code == CO_ER_NONE) { |
| 1238 | conn->err_code = CO_ER_SOCKS4_ABORT; |
| 1239 | } |
| 1240 | conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH); |
| 1241 | goto fail; |
| 1242 | |
| 1243 | fail: |
Alexander Liu | 2a54bb7 | 2019-05-22 19:44:48 +0800 | [diff] [blame] | 1244 | conn->flags |= CO_FL_ERROR; |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Ilya Shipitsin | ca56fce | 2018-09-15 00:50:05 +0500 | [diff] [blame] | 1248 | /* Note: <remote> is explicitly allowed to be NULL */ |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 1249 | int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1250 | { |
| 1251 | int ret = 0; |
| 1252 | |
| 1253 | if (srv && (srv->pp_opts & SRV_PP_V2)) { |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 1254 | ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1255 | } |
| 1256 | else { |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 1257 | if (remote && conn_get_src(remote) && conn_get_dst(remote)) |
| 1258 | ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1259 | else |
| 1260 | ret = make_proxy_line_v1(buf, buf_len, NULL, NULL); |
| 1261 | } |
| 1262 | |
| 1263 | return ret; |
| 1264 | } |
| 1265 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1266 | /* Makes a PROXY protocol line from the two addresses. The output is sent to |
| 1267 | * buffer <buf> for a maximum size of <buf_len> (including the trailing zero). |
| 1268 | * It returns the number of bytes composing this line (including the trailing |
| 1269 | * LF), or zero in case of failure (eg: not enough space). It supports TCP4, |
Willy Tarreau | 2e1401a | 2013-10-01 11:41:55 +0200 | [diff] [blame] | 1270 | * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is |
| 1271 | * emitted as well. |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1272 | */ |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1273 | int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1274 | { |
| 1275 | int ret = 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1276 | char * protocol; |
| 1277 | char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)]; |
| 1278 | char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)]; |
| 1279 | in_port_t src_port; |
| 1280 | in_port_t dst_port; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1281 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1282 | if ( !src |
| 1283 | || !dst |
| 1284 | || (src->ss_family != AF_INET && src->ss_family != AF_INET6) |
| 1285 | || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) { |
| 1286 | /* unknown family combination */ |
| 1287 | ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n"); |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1288 | if (ret >= buf_len) |
| 1289 | return 0; |
| 1290 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1291 | return ret; |
| 1292 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1293 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1294 | /* IPv4 for both src and dst */ |
| 1295 | if (src->ss_family == AF_INET && dst->ss_family == AF_INET) { |
| 1296 | protocol = "TCP4"; |
| 1297 | if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str))) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1298 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1299 | src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1300 | if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str))) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1301 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1302 | dst_port = ((struct sockaddr_in *)dst)->sin_port; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1303 | } |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1304 | /* IPv6 for at least one of src and dst */ |
| 1305 | else { |
| 1306 | struct in6_addr tmp; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1307 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1308 | protocol = "TCP6"; |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1309 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1310 | if (src->ss_family == AF_INET) { |
| 1311 | /* Convert src to IPv6 */ |
| 1312 | v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr); |
| 1313 | src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1314 | } |
| 1315 | else { |
| 1316 | tmp = ((struct sockaddr_in6 *)src)->sin6_addr; |
| 1317 | src_port = ((struct sockaddr_in6 *)src)->sin6_port; |
| 1318 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1319 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1320 | if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str))) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1321 | return 0; |
| 1322 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1323 | if (dst->ss_family == AF_INET) { |
| 1324 | /* Convert dst to IPv6 */ |
| 1325 | v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr); |
| 1326 | dst_port = ((struct sockaddr_in *)dst)->sin_port; |
| 1327 | } |
| 1328 | else { |
| 1329 | tmp = ((struct sockaddr_in6 *)dst)->sin6_addr; |
| 1330 | dst_port = ((struct sockaddr_in6 *)dst)->sin6_port; |
| 1331 | } |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1332 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1333 | if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str))) |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1334 | return 0; |
| 1335 | } |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1336 | |
| 1337 | ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port)); |
| 1338 | if (ret >= buf_len) |
| 1339 | return 0; |
| 1340 | |
Willy Tarreau | e1e4a61 | 2012-10-05 00:10:55 +0200 | [diff] [blame] | 1341 | return ret; |
| 1342 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1343 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1344 | static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1345 | { |
| 1346 | struct tlv *tlv; |
| 1347 | |
| 1348 | if (!dest || (length + sizeof(*tlv) > dest_len)) |
| 1349 | return 0; |
| 1350 | |
| 1351 | tlv = (struct tlv *)dest; |
| 1352 | |
| 1353 | tlv->type = type; |
| 1354 | tlv->length_hi = length >> 8; |
| 1355 | tlv->length_lo = length & 0x00ff; |
| 1356 | memcpy(tlv->value, value, length); |
| 1357 | return length + sizeof(*tlv); |
| 1358 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1359 | |
Ilya Shipitsin | ca56fce | 2018-09-15 00:50:05 +0500 | [diff] [blame] | 1360 | /* Note: <remote> is explicitly allowed to be NULL */ |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 1361 | int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1362 | { |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1363 | const char pp2_signature[] = PP2_SIGNATURE; |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1364 | void *tlv_crc32c_p = NULL; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1365 | int ret = 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1366 | struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf; |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 1367 | struct sockaddr_storage null_addr = { .ss_family = 0 }; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1368 | struct sockaddr_storage *src = &null_addr; |
| 1369 | struct sockaddr_storage *dst = &null_addr; |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1370 | const char *value; |
| 1371 | int value_len; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1372 | |
| 1373 | if (buf_len < PP2_HEADER_LEN) |
| 1374 | return 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1375 | memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1376 | |
Willy Tarreau | 226572f | 2019-07-17 14:46:00 +0200 | [diff] [blame] | 1377 | if (remote && conn_get_src(remote) && conn_get_dst(remote)) { |
| 1378 | src = remote->src; |
| 1379 | dst = remote->dst; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1380 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1381 | |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1382 | /* At least one of src or dst is not of AF_INET or AF_INET6 */ |
| 1383 | if ( !src |
| 1384 | || !dst |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 1385 | || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1386 | || (src->ss_family != AF_INET && src->ss_family != AF_INET6) |
| 1387 | || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) { |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1388 | if (buf_len < PP2_HDR_LEN_UNSPEC) |
| 1389 | return 0; |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1390 | hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL; |
| 1391 | hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1392 | ret = PP2_HDR_LEN_UNSPEC; |
| 1393 | } |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1394 | else { |
Willy Tarreau | 02c8803 | 2020-04-14 12:54:10 +0200 | [diff] [blame] | 1395 | hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1396 | /* IPv4 for both src and dst */ |
| 1397 | if (src->ss_family == AF_INET && dst->ss_family == AF_INET) { |
| 1398 | if (buf_len < PP2_HDR_LEN_INET) |
| 1399 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1400 | hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM; |
| 1401 | hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr; |
| 1402 | hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1403 | hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr; |
| 1404 | hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port; |
| 1405 | ret = PP2_HDR_LEN_INET; |
| 1406 | } |
| 1407 | /* IPv6 for at least one of src and dst */ |
| 1408 | else { |
| 1409 | struct in6_addr tmp; |
| 1410 | |
| 1411 | if (buf_len < PP2_HDR_LEN_INET6) |
| 1412 | return 0; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1413 | hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM; |
| 1414 | if (src->ss_family == AF_INET) { |
| 1415 | v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr); |
| 1416 | memcpy(hdr->addr.ip6.src_addr, &tmp, 16); |
| 1417 | hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port; |
| 1418 | } |
| 1419 | else { |
| 1420 | memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16); |
| 1421 | hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port; |
| 1422 | } |
| 1423 | if (dst->ss_family == AF_INET) { |
| 1424 | v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr); |
| 1425 | memcpy(hdr->addr.ip6.dst_addr, &tmp, 16); |
William Dauchy | bd8bf67 | 2020-01-26 19:06:39 +0100 | [diff] [blame] | 1426 | hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port; |
Tim Duesterhus | 7fec021 | 2018-07-27 18:46:13 +0200 | [diff] [blame] | 1427 | } |
| 1428 | else { |
| 1429 | memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16); |
| 1430 | hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port; |
| 1431 | } |
| 1432 | |
| 1433 | ret = PP2_HDR_LEN_INET6; |
| 1434 | } |
| 1435 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1436 | |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1437 | if (srv->pp_opts & SRV_PP_V2_CRC32C) { |
| 1438 | uint32_t zero_crc32c = 0; |
Tim Duesterhus | a8692f3 | 2020-03-13 12:34:25 +0100 | [diff] [blame] | 1439 | |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1440 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1441 | return 0; |
| 1442 | tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value; |
| 1443 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c); |
| 1444 | } |
| 1445 | |
Ilya Shipitsin | ca56fce | 2018-09-15 00:50:05 +0500 | [diff] [blame] | 1446 | if (remote && conn_get_alpn(remote, &value, &value_len)) { |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1447 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1448 | return 0; |
Emmanuel Hocdet | 571c7ac | 2017-10-31 18:24:05 +0100 | [diff] [blame] | 1449 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value); |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1450 | } |
| 1451 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 1452 | if (srv->pp_opts & SRV_PP_V2_AUTHORITY) { |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1453 | value = NULL; |
| 1454 | if (remote && remote->proxy_authority) { |
| 1455 | value = remote->proxy_authority; |
| 1456 | value_len = remote->proxy_authority_len; |
| 1457 | } |
| 1458 | #ifdef USE_OPENSSL |
| 1459 | else { |
Jerome Magnin | 78891c7 | 2019-09-02 09:53:41 +0200 | [diff] [blame] | 1460 | if ((value = ssl_sock_get_sni(remote))) |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1461 | value_len = strlen(value); |
| 1462 | } |
| 1463 | #endif |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 1464 | if (value) { |
| 1465 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1466 | return 0; |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1467 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 1468 | } |
| 1469 | } |
| 1470 | |
Christopher Faulet | 3ab504f | 2020-05-26 15:16:01 +0200 | [diff] [blame] | 1471 | if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) { |
Tim Duesterhus | cf6e0c8 | 2020-03-13 12:34:24 +0100 | [diff] [blame] | 1472 | struct session* sess = strm_sess(strm); |
| 1473 | struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id); |
| 1474 | |
| 1475 | value = unique_id.ptr; |
| 1476 | value_len = unique_id.len; |
| 1477 | |
| 1478 | if (value_len >= 0) { |
| 1479 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1480 | return 0; |
| 1481 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value); |
| 1482 | } |
| 1483 | } |
| 1484 | |
Emmanuel Hocdet | 8a4ffa0 | 2019-08-29 11:54:51 +0200 | [diff] [blame] | 1485 | #ifdef USE_OPENSSL |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1486 | if (srv->pp_opts & SRV_PP_V2_SSL) { |
Emmanuel Hocdet | 404d978 | 2017-10-24 10:55:14 +0200 | [diff] [blame] | 1487 | struct tlv_ssl *tlv; |
| 1488 | int ssl_tlv_len = 0; |
Tim Duesterhus | a8692f3 | 2020-03-13 12:34:25 +0100 | [diff] [blame] | 1489 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1490 | if ((buf_len - ret) < sizeof(struct tlv_ssl)) |
| 1491 | return 0; |
| 1492 | tlv = (struct tlv_ssl *)&buf[ret]; |
| 1493 | memset(tlv, 0, sizeof(struct tlv_ssl)); |
| 1494 | ssl_tlv_len += sizeof(struct tlv_ssl); |
| 1495 | tlv->tlv.type = PP2_TYPE_SSL; |
| 1496 | if (ssl_sock_is_ssl(remote)) { |
| 1497 | tlv->client |= PP2_CLIENT_SSL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 1498 | value = ssl_sock_get_proto_version(remote); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1499 | if (value) { |
Emmanuel Hocdet | 8c0c34b | 2018-02-28 12:02:14 +0100 | [diff] [blame] | 1500 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len-ret-ssl_tlv_len), PP2_SUBTYPE_SSL_VERSION, strlen(value), value); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1501 | } |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 1502 | if (ssl_sock_get_cert_used_sess(remote)) { |
| 1503 | tlv->client |= PP2_CLIENT_CERT_SESS; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1504 | tlv->verify = htonl(ssl_sock_get_verify_result(remote)); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 1505 | if (ssl_sock_get_cert_used_conn(remote)) |
| 1506 | tlv->client |= PP2_CLIENT_CERT_CONN; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1507 | } |
| 1508 | if (srv->pp_opts & SRV_PP_V2_SSL_CN) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1509 | struct buffer *cn_trash = get_trash_chunk(); |
Willy Tarreau | 3b9a0c9 | 2014-07-19 06:37:33 +0200 | [diff] [blame] | 1510 | if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1511 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN, |
| 1512 | cn_trash->data, |
| 1513 | cn_trash->area); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1514 | } |
| 1515 | } |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1516 | if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1517 | struct buffer *pkey_trash = get_trash_chunk(); |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1518 | if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1519 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG, |
| 1520 | pkey_trash->data, |
| 1521 | pkey_trash->area); |
Emmanuel Hocdet | fa8d0f1 | 2018-02-01 15:53:52 +0100 | [diff] [blame] | 1522 | } |
| 1523 | } |
| 1524 | if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) { |
| 1525 | value = ssl_sock_get_cert_sig(remote); |
| 1526 | if (value) { |
| 1527 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value); |
| 1528 | } |
| 1529 | } |
| 1530 | if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) { |
| 1531 | value = ssl_sock_get_cipher_name(remote); |
| 1532 | if (value) { |
| 1533 | ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value); |
| 1534 | } |
| 1535 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1536 | } |
| 1537 | tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8; |
| 1538 | tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff; |
| 1539 | ret += ssl_tlv_len; |
| 1540 | } |
| 1541 | #endif |
| 1542 | |
Willy Tarreau | e573323 | 2019-05-22 19:24:06 +0200 | [diff] [blame] | 1543 | #ifdef USE_NS |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1544 | if (remote && (remote->proxy_netns)) { |
| 1545 | if ((buf_len - ret) < sizeof(struct tlv)) |
| 1546 | return 0; |
Emmanuel Hocdet | 571c7ac | 2017-10-31 18:24:05 +0100 | [diff] [blame] | 1547 | ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1548 | } |
| 1549 | #endif |
| 1550 | |
Willy Tarreau | 8fccfa2 | 2014-06-14 08:28:06 +0200 | [diff] [blame] | 1551 | hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN)); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1552 | |
Emmanuel Hocdet | 4399c75 | 2018-02-05 15:26:43 +0100 | [diff] [blame] | 1553 | if (tlv_crc32c_p) { |
| 1554 | write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret))); |
| 1555 | } |
| 1556 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 1557 | return ret; |
| 1558 | } |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1559 | |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 1560 | /* returns 0 on success */ |
| 1561 | static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx, |
| 1562 | struct proxy *defpx, const char *file, int line, |
| 1563 | char **err) |
| 1564 | { |
| 1565 | if (too_many_args(0, args, err, NULL)) |
| 1566 | return -1; |
| 1567 | pp2_never_send_local = 1; |
| 1568 | return 0; |
| 1569 | } |
| 1570 | |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 1571 | /* return the major HTTP version as 1 or 2 depending on how the request arrived |
| 1572 | * before being processed. |
| 1573 | */ |
| 1574 | static int |
| 1575 | smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1576 | { |
Jérôme Magnin | 8657742 | 2018-12-07 09:03:11 +0100 | [diff] [blame] | 1577 | struct connection *conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) : |
| 1578 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 1579 | |
| 1580 | smp->data.type = SMP_T_SINT; |
| 1581 | smp->data.u.sint = (conn && strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1; |
| 1582 | return 1; |
| 1583 | } |
| 1584 | |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1585 | /* fetch if the received connection used a PROXY protocol header */ |
| 1586 | int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1587 | { |
| 1588 | struct connection *conn; |
| 1589 | |
| 1590 | conn = objt_conn(smp->sess->origin); |
| 1591 | if (!conn) |
| 1592 | return 0; |
| 1593 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 1594 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1595 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
| 1599 | smp->flags = 0; |
| 1600 | smp->data.type = SMP_T_BOOL; |
| 1601 | smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0; |
| 1602 | |
| 1603 | return 1; |
| 1604 | } |
| 1605 | |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 1606 | /* fetch the authority TLV from a PROXY protocol header */ |
| 1607 | int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1608 | { |
| 1609 | struct connection *conn; |
| 1610 | |
| 1611 | conn = objt_conn(smp->sess->origin); |
| 1612 | if (!conn) |
| 1613 | return 0; |
| 1614 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 1615 | if (conn->flags & CO_FL_WAIT_XPRT) { |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 1616 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
| 1620 | if (conn->proxy_authority == NULL) |
| 1621 | return 0; |
| 1622 | |
| 1623 | smp->flags = 0; |
| 1624 | smp->data.type = SMP_T_STR; |
| 1625 | smp->data.u.str.area = conn->proxy_authority; |
| 1626 | smp->data.u.str.data = conn->proxy_authority_len; |
| 1627 | |
| 1628 | return 1; |
| 1629 | } |
| 1630 | |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 1631 | /* fetch the unique ID TLV from a PROXY protocol header */ |
| 1632 | int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1633 | { |
| 1634 | struct connection *conn; |
| 1635 | |
| 1636 | conn = objt_conn(smp->sess->origin); |
| 1637 | if (!conn) |
| 1638 | return 0; |
| 1639 | |
| 1640 | if (conn->flags & CO_FL_WAIT_XPRT) { |
| 1641 | smp->flags |= SMP_F_MAY_CHANGE; |
| 1642 | return 0; |
| 1643 | } |
| 1644 | |
| 1645 | if (!isttest(conn->proxy_unique_id)) |
| 1646 | return 0; |
| 1647 | |
| 1648 | smp->flags = 0; |
| 1649 | smp->data.type = SMP_T_STR; |
| 1650 | smp->data.u.str.area = conn->proxy_unique_id.ptr; |
| 1651 | smp->data.u.str.data = conn->proxy_unique_id.len; |
| 1652 | |
| 1653 | return 1; |
| 1654 | } |
| 1655 | |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1656 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1657 | * Note: fetches that may return multiple types must be declared as the lowest |
| 1658 | * common denominator, the type that can be casted into all other ones. For |
| 1659 | * instance v4/v6 must be declared v4. |
| 1660 | */ |
| 1661 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Willy Tarreau | 60ca10a | 2017-08-18 15:26:54 +0200 | [diff] [blame] | 1662 | { "fc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI }, |
Jérôme Magnin | 8657742 | 2018-12-07 09:03:11 +0100 | [diff] [blame] | 1663 | { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV }, |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1664 | { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI }, |
Geoff Simmons | 7185b78 | 2019-08-27 18:31:16 +0200 | [diff] [blame] | 1665 | { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI }, |
Tim Duesterhus | d1b15b6 | 2020-03-13 12:34:23 +0100 | [diff] [blame] | 1666 | { "fc_pp_unique_id", smp_fetch_fc_pp_unique_id, 0, NULL, SMP_T_STR, SMP_USE_L4CLI }, |
Emeric Brun | 4f60301 | 2017-01-05 15:11:44 +0100 | [diff] [blame] | 1667 | { /* END */ }, |
| 1668 | }}; |
| 1669 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1670 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
Willy Tarreau | 119e50e | 2020-05-22 13:53:29 +0200 | [diff] [blame] | 1671 | |
| 1672 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1673 | { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local }, |
| 1674 | { /* END */ }, |
| 1675 | }}; |
| 1676 | |
| 1677 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |