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