blob: 67b212452a46e9783fcd12ee8991c9abb7451170 [file] [log] [blame]
Willy Tarreau59f98392012-07-06 14:13:49 +02001/*
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 Tarreaue1e4a612012-10-05 00:10:55 +020013#include <errno.h>
14
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020016#include <haproxy/cfgparse.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020017#include <haproxy/connection.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/fd.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020019#include <haproxy/frontend.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <haproxy/hash.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020021#include <haproxy/log-t.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020022#include <haproxy/namespace.h>
Willy Tarreau6131d6a2020-06-02 16:48:09 +020023#include <haproxy/net_helper.h>
Willy Tarreaufc774542020-06-04 17:31:04 +020024#include <haproxy/proto_tcp.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020025#include <haproxy/sample.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020026#include <haproxy/ssl_sock.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020027#include <haproxy/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020028
Alexander Liu2a54bb72019-05-22 19:44:48 +080029
Amaury Denoyelle8990b012021-02-19 15:29:16 +010030DECLARE_POOL(pool_head_connection, "connection", sizeof(struct connection));
31DECLARE_POOL(pool_head_connstream, "conn_stream", sizeof(struct conn_stream));
32DECLARE_POOL(pool_head_conn_hash_node, "conn_hash_node", sizeof(struct conn_hash_node));
33DECLARE_POOL(pool_head_sockaddr, "sockaddr", sizeof(struct sockaddr_storage));
34DECLARE_POOL(pool_head_authority, "authority", PP2_AUTHORITY_MAX);
Willy Tarreau8ceae722018-11-26 11:58:30 +010035
Willy Tarreau4d82bf52020-06-28 00:19:17 +020036struct idle_conns idle_conns[MAX_THREADS] = { };
Willy Tarreau13e14102016-12-22 20:25:26 +010037struct xprt_ops *registered_xprt[XPRT_ENTRIES] = { NULL, };
Willy Tarreauf2943dc2012-10-26 20:10:28 +020038
Christopher Faulet32f61c02018-04-10 14:33:41 +020039/* List head of all known muxes for PROTO */
40struct mux_proto_list mux_proto_list = {
41 .list = LIST_HEAD_INIT(mux_proto_list.list)
Willy Tarreau2386be62017-09-21 19:40:52 +020042};
43
Amaury Denoyelled3a88c12021-05-03 10:47:51 +020044struct mux_stopping_data mux_stopping_data[MAX_THREADS];
45
Willy Tarreau119e50e2020-05-22 13:53:29 +020046/* disables sending of proxy-protocol-v2's LOCAL command */
47static int pp2_never_send_local;
48
Olivier Houchard477902b2020-01-22 18:08:48 +010049int conn_create_mux(struct connection *conn)
50{
Olivier Houchard477902b2020-01-22 18:08:48 +010051 if (conn_is_back(conn)) {
52 struct server *srv;
53 struct conn_stream *cs = conn->ctx;
Christopher Faulet14cd3162020-04-16 14:50:06 +020054 struct session *sess = conn->owner;
Olivier Houchard477902b2020-01-22 18:08:48 +010055
56 if (conn->flags & CO_FL_ERROR)
57 goto fail;
Olivier Houcharda8a415d2020-01-23 13:15:14 +010058
Christopher Faulet14cd3162020-04-16 14:50:06 +020059 if (sess && obj_type(sess->origin) == OBJ_TYPE_CHECK) {
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010060 if (conn_install_mux_chk(conn, conn->ctx, sess) < 0)
Christopher Faulet14cd3162020-04-16 14:50:06 +020061 goto fail;
62 }
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010063 else if (conn_install_mux_be(conn, conn->ctx, sess) < 0)
Olivier Houchard477902b2020-01-22 18:08:48 +010064 goto fail;
65 srv = objt_server(conn->target);
Christopher Faulet08016ab2020-07-01 16:10:06 +020066
67 /* If we're doing http-reuse always, and the connection is not
68 * private with available streams (an http2 connection), add it
69 * to the available list, so that others can use it right
70 * away. If the connection is private, add it in the session
71 * server list.
72 */
Christopher Faulet2883fcf2020-07-01 14:59:43 +020073 if (srv && ((srv->proxy->options & PR_O_REUSE_MASK) == PR_O_REUSE_ALWS) &&
Christopher Fauletaa278532020-06-30 14:47:46 +020074 !(conn->flags & CO_FL_PRIVATE) && conn->mux->avail_streams(conn) > 0)
Willy Tarreau430bf4a2021-03-04 09:45:32 +010075 ebmb_insert(&srv->per_thr[tid].avail_conns, &conn->hash_node->node, sizeof(conn->hash_node->hash));
Christopher Faulet08016ab2020-07-01 16:10:06 +020076 else if (conn->flags & CO_FL_PRIVATE) {
Ilya Shipitsin6b79f382020-07-23 00:32:55 +050077 /* If it fail now, the same will be done in mux->detach() callback */
Willy Tarreau38b4d2e2020-11-20 17:08:15 +010078 session_add_conn(sess, conn, conn->target);
Christopher Faulet08016ab2020-07-01 16:10:06 +020079 }
Olivier Houchard477902b2020-01-22 18:08:48 +010080 return 0;
81fail:
82 /* let the upper layer know the connection failed */
83 cs->data_cb->wake(cs);
84 return -1;
85 } else
86 return conn_complete_session(conn);
87
88}
89
Willy Tarreauff3e6482015-03-12 23:56:52 +010090/* Send a message over an established connection. It makes use of send() and
91 * returns the same return code and errno. If the socket layer is not ready yet
92 * then -1 is returned and ENOTSOCK is set into errno. If the fd is not marked
93 * as ready, or if EAGAIN or ENOTCONN is returned, then we return 0. It returns
94 * EMSGSIZE if called with a zero length message. The purpose is to simplify
95 * some rare attempts to directly write on the socket from above the connection
96 * (typically send_proxy). In case of EAGAIN, the fd is marked as "cant_send".
97 * It automatically retries on EINTR. Other errors cause the connection to be
98 * marked as in error state. It takes similar arguments as send() except the
Willy Tarreau827fee72020-12-11 15:26:55 +010099 * first one which is the connection instead of the file descriptor. <flags>
100 * only support CO_SFL_MSG_MORE.
Willy Tarreauff3e6482015-03-12 23:56:52 +0100101 */
Willy Tarreau827fee72020-12-11 15:26:55 +0100102int conn_ctrl_send(struct connection *conn, const void *buf, int len, int flags)
Willy Tarreauff3e6482015-03-12 23:56:52 +0100103{
Willy Tarreau827fee72020-12-11 15:26:55 +0100104 const struct buffer buffer = b_make((char*)buf, len, 0, len);
105 const struct xprt_ops *xprt = xprt_get(XPRT_RAW);
Willy Tarreauff3e6482015-03-12 23:56:52 +0100106 int ret;
107
108 ret = -1;
109 errno = ENOTSOCK;
110
111 if (conn->flags & CO_FL_SOCK_WR_SH)
112 goto fail;
113
114 if (!conn_ctrl_ready(conn))
115 goto fail;
116
117 errno = EMSGSIZE;
118 if (!len)
119 goto fail;
120
Willy Tarreau827fee72020-12-11 15:26:55 +0100121 /* snd_buf() already takes care of updating conn->flags and handling
122 * the FD polling status.
123 */
124 ret = xprt->snd_buf(conn, NULL, &buffer, buffer.data, flags);
125 if (conn->flags & CO_FL_ERROR)
126 ret = -1;
127 return ret;
Willy Tarreauff3e6482015-03-12 23:56:52 +0100128 fail:
129 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH | CO_FL_ERROR;
130 return ret;
131}
132
Willy Tarreau746b0512020-12-11 17:06:11 +0100133/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
134 * The event subscriber <es> is not allowed to change from a previous call as
135 * long as at least one event is still subscribed. The <event_type> must only
136 * be a combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100137 */
138int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200139{
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100140 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100141 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100142
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100143 es->events &= ~event_type;
144 if (!es->events)
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100145 conn->subs = NULL;
146
Willy Tarreau746b0512020-12-11 17:06:11 +0100147 if (conn_ctrl_ready(conn) && conn->ctrl->ignore_events)
148 conn->ctrl->ignore_events(conn, event_type);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100149
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200150 return 0;
151}
152
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100153/* Called from the upper layer, to subscribe <es> to events <event_type>.
154 * The <es> struct is not allowed to differ from the one passed during a
Willy Tarreau746b0512020-12-11 17:06:11 +0100155 * previous call to subscribe(). If the connection's ctrl layer is ready,
156 * the wait_event is immediately woken up and the subcription is cancelled.
157 * It always returns zero.
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100158 */
159int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houchard6ff20392018-07-17 18:46:31 +0200160{
Willy Tarreau746b0512020-12-11 17:06:11 +0100161 int ret = 0;
162
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100163 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +0100164 BUG_ON(conn->subs && conn->subs != es);
Willy Tarreau7872d1f2020-01-10 07:06:05 +0100165
Willy Tarreau7e59c0a2020-02-28 14:24:49 +0100166 if (conn->subs && (conn->subs->events & event_type) == event_type)
167 return 0;
168
Willy Tarreau746b0512020-12-11 17:06:11 +0100169 if (conn_ctrl_ready(conn) && conn->ctrl->check_events) {
170 ret = conn->ctrl->check_events(conn, event_type);
171 if (ret)
172 tasklet_wakeup(es->tasklet);
Willy Tarreaud1d14c32020-02-21 10:34:19 +0100173 }
Willy Tarreau746b0512020-12-11 17:06:11 +0100174
175 es->events = (es->events | event_type) & ~ret;
176 conn->subs = es->events ? es : NULL;
Olivier Houchard83a0cd82018-09-28 17:57:58 +0200177 return 0;
Olivier Houchard6ff20392018-07-17 18:46:31 +0200178}
179
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100180/* Drains possibly pending incoming data on the connection and update the flags
181 * accordingly. This is used to know whether we need to disable lingering on
182 * close. Returns non-zero if it is safe to close without disabling lingering,
183 * otherwise zero. The CO_FL_SOCK_RD_SH flag may also be updated if the incoming
184 * shutdown was reported by the ->drain() function.
Willy Tarreaud85c4852015-03-13 00:40:28 +0100185 */
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100186int conn_ctrl_drain(struct connection *conn)
Willy Tarreaud85c4852015-03-13 00:40:28 +0100187{
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100188 int ret = 0;
Willy Tarreaue215bba2018-08-24 14:31:53 +0200189
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100190 if (!conn_ctrl_ready(conn) || conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))
191 ret = 1;
192 else if (conn->ctrl->drain) {
193 ret = conn->ctrl->drain(conn);
194 if (ret)
195 conn->flags |= CO_FL_SOCK_RD_SH;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100196 }
Willy Tarreau2ded48d2020-12-11 16:20:34 +0100197 return ret;
Willy Tarreaud85c4852015-03-13 00:40:28 +0100198}
199
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100200/*
201 * Get data length from tlv
202 */
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100203static inline size_t get_tlv_length(const struct tlv *src)
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100204{
205 return (src->length_hi << 8) | src->length_lo;
206}
207
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200208/* This handshake handler waits a PROXY protocol header at the beginning of the
209 * raw data stream. The header looks like this :
210 *
211 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
212 *
213 * There must be exactly one space between each field. Fields are :
214 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
215 * - SRC3 : layer 3 (eg: IP) source address in standard text form
216 * - DST3 : layer 3 (eg: IP) destination address in standard text form
217 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
218 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
219 *
220 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
221 *
222 * The header line is small and in all cases smaller than the smallest normal
223 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
224 * can safely use MSG_PEEK and avoid buffering.
225 *
226 * Once the data is fetched, the values are set in the connection's address
227 * fields, and data are removed from the socket's buffer. The function returns
228 * zero if it needs to wait for more data or if it fails, or 1 if it completed
229 * and removed itself.
230 */
231int conn_recv_proxy(struct connection *conn, int flag)
232{
233 char *line, *end;
Willy Tarreau77992672014-06-14 11:06:17 +0200234 struct proxy_hdr_v2 *hdr_v2;
235 const char v2sig[] = PP2_SIGNATURE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100236 size_t total_v2_len;
Tim Duesterhusba837ec2020-03-05 23:11:02 +0100237 size_t tlv_offset = 0;
Willy Tarreaub406b872018-08-22 05:20:32 +0200238 int ret;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200239
Willy Tarreau3c728722014-01-23 13:50:42 +0100240 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +0200241 goto fail;
242
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200243 if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0))
Willy Tarreauca79f592019-07-17 19:04:47 +0200244 goto fail;
245
Willy Tarreau585744b2017-08-24 14:31:19 +0200246 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200247 goto not_ready;
Willy Tarreaufd803bb2014-01-20 15:13:07 +0100248
Willy Tarreau157788c2020-02-11 10:08:05 +0100249 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200250 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
251 if (ret < 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200252 if (errno == EINTR)
253 continue;
254 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200255 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200256 goto not_ready;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200257 }
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100258 goto recv_abort;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200259 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200260 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100261 break;
262 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200263
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200264 if (!trash.data) {
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100265 /* client shutdown */
266 conn->err_code = CO_ER_PRX_EMPTY;
267 goto fail;
268 }
269
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100270 conn->flags &= ~CO_FL_WAIT_L4_CONN;
271
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200272 if (trash.data < 6)
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200273 goto missing;
274
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200275 line = trash.area;
276 end = trash.area + trash.data;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200277
278 /* Decode a possible proxy request, fail early if it does not match */
Willy Tarreau77992672014-06-14 11:06:17 +0200279 if (strncmp(line, "PROXY ", 6) != 0)
280 goto not_v1;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200281
282 line += 6;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200283 if (trash.data < 9) /* shortest possible line */
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200284 goto missing;
285
David CARLIER42ff05e2016-03-24 09:22:36 +0000286 if (memcmp(line, "TCP4 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200287 u32 src3, dst3, sport, dport;
288
289 line += 5;
290
291 src3 = inetaddr_host_lim_ret(line, end, &line);
292 if (line == end)
293 goto missing;
294 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100295 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200296
297 dst3 = inetaddr_host_lim_ret(line, end, &line);
298 if (line == end)
299 goto missing;
300 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100301 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200302
303 sport = read_uint((const char **)&line, end);
304 if (line == end)
305 goto missing;
306 if (*line++ != ' ')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100307 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200308
309 dport = read_uint((const char **)&line, end);
310 if (line > end - 2)
311 goto missing;
312 if (*line++ != '\r')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100313 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200314 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100315 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200316
317 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200318 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
319 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = htonl(src3);
320 ((struct sockaddr_in *)conn->src)->sin_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200321
Willy Tarreau226572f2019-07-17 14:46:00 +0200322 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
323 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = htonl(dst3);
324 ((struct sockaddr_in *)conn->dst)->sin_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200325 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
326 }
David CARLIER42ff05e2016-03-24 09:22:36 +0000327 else if (memcmp(line, "TCP6 ", 5) == 0) {
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200328 u32 sport, dport;
329 char *src_s;
330 char *dst_s, *sport_s, *dport_s;
331 struct in6_addr src3, dst3;
332
333 line += 5;
334
335 src_s = line;
336 dst_s = sport_s = dport_s = NULL;
337 while (1) {
338 if (line > end - 2) {
339 goto missing;
340 }
341 else if (*line == '\r') {
342 *line = 0;
343 line++;
344 if (*line++ != '\n')
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100345 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200346 break;
347 }
348
349 if (*line == ' ') {
350 *line = 0;
351 if (!dst_s)
352 dst_s = line + 1;
353 else if (!sport_s)
354 sport_s = line + 1;
355 else if (!dport_s)
356 dport_s = line + 1;
357 }
358 line++;
359 }
360
361 if (!dst_s || !sport_s || !dport_s)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100362 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200363
364 sport = read_uint((const char **)&sport_s,dport_s - 1);
365 if (*sport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100366 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200367
368 dport = read_uint((const char **)&dport_s,line - 2);
369 if (*dport_s != 0)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100370 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200371
372 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100373 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200374
375 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100376 goto bad_header;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200377
378 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200379 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
380 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, &src3, sizeof(struct in6_addr));
381 ((struct sockaddr_in6 *)conn->src)->sin6_port = htons(sport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200382
Willy Tarreau226572f2019-07-17 14:46:00 +0200383 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
384 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, &dst3, sizeof(struct in6_addr));
385 ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(dport);
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200386 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
387 }
Willy Tarreau4c20d292014-06-14 11:41:36 +0200388 else if (memcmp(line, "UNKNOWN\r\n", 9) == 0) {
389 /* This can be a UNIX socket forwarded by an haproxy upstream */
390 line += 9;
391 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200392 else {
Willy Tarreau4c20d292014-06-14 11:41:36 +0200393 /* The protocol does not match something known (TCP4/TCP6/UNKNOWN) */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100394 conn->err_code = CO_ER_PRX_BAD_PROTO;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200395 goto fail;
396 }
397
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200398 trash.data = line - trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200399 goto eat_header;
400
401 not_v1:
402 /* try PPv2 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200403 if (trash.data < PP2_HEADER_LEN)
Willy Tarreau77992672014-06-14 11:06:17 +0200404 goto missing;
405
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200406 hdr_v2 = (struct proxy_hdr_v2 *) trash.area;
Willy Tarreau77992672014-06-14 11:06:17 +0200407
408 if (memcmp(hdr_v2->sig, v2sig, PP2_SIGNATURE_LEN) != 0 ||
409 (hdr_v2->ver_cmd & PP2_VERSION_MASK) != PP2_VERSION) {
410 conn->err_code = CO_ER_PRX_NOT_HDR;
411 goto fail;
412 }
413
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100414 total_v2_len = PP2_HEADER_LEN + ntohs(hdr_v2->len);
415 if (trash.data < total_v2_len)
Willy Tarreau77992672014-06-14 11:06:17 +0200416 goto missing;
417
418 switch (hdr_v2->ver_cmd & PP2_CMD_MASK) {
419 case 0x01: /* PROXY command */
420 switch (hdr_v2->fam) {
421 case 0x11: /* TCPv4 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100422 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET)
423 goto bad_header;
424
Willy Tarreau226572f2019-07-17 14:46:00 +0200425 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
426 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_v2->addr.ip4.src_addr;
427 ((struct sockaddr_in *)conn->src)->sin_port = hdr_v2->addr.ip4.src_port;
428 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
429 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_v2->addr.ip4.dst_addr;
430 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_v2->addr.ip4.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200431 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200432 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET;
Willy Tarreau77992672014-06-14 11:06:17 +0200433 break;
434 case 0x21: /* TCPv6 */
KOVACS Krisztianefd3aa92014-11-19 10:53:20 +0100435 if (ntohs(hdr_v2->len) < PP2_ADDR_LEN_INET6)
436 goto bad_header;
437
Willy Tarreau226572f2019-07-17 14:46:00 +0200438 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
439 memcpy(&((struct sockaddr_in6 *)conn->src)->sin6_addr, hdr_v2->addr.ip6.src_addr, 16);
440 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_v2->addr.ip6.src_port;
441 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
442 memcpy(&((struct sockaddr_in6 *)conn->dst)->sin6_addr, hdr_v2->addr.ip6.dst_addr, 16);
443 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_v2->addr.ip6.dst_port;
Willy Tarreau77992672014-06-14 11:06:17 +0200444 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
KOVACS Krisztian7209c202015-07-03 14:09:10 +0200445 tlv_offset = PP2_HEADER_LEN + PP2_ADDR_LEN_INET6;
Willy Tarreau77992672014-06-14 11:06:17 +0200446 break;
447 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100448
449 /* TLV parsing */
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100450 while (tlv_offset < total_v2_len) {
451 struct tlv *tlv_packet;
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100452 struct ist tlv;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100453
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100454 /* Verify that we have at least TLV_HEADER_SIZE bytes left */
455 if (tlv_offset + TLV_HEADER_SIZE > total_v2_len)
456 goto bad_header;
457
458 tlv_packet = (struct tlv *) &trash.area[tlv_offset];
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100459 tlv = ist2((const char *)tlv_packet->value, get_tlv_length(tlv_packet));
460 tlv_offset += istlen(tlv) + TLV_HEADER_SIZE;
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100461
462 /* Verify that the TLV length does not exceed the total PROXYv2 length */
463 if (tlv_offset > total_v2_len)
464 goto bad_header;
465
466 switch (tlv_packet->type) {
467 case PP2_TYPE_CRC32C: {
468 uint32_t n_crc32c;
469
470 /* Verify that this TLV is exactly 4 bytes long */
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100471 if (istlen(tlv) != 4)
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100472 goto bad_header;
473
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100474 n_crc32c = read_n32(istptr(tlv));
475 write_n32(istptr(tlv), 0); // compute with CRC==0
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100476
477 if (hash_crc32c(trash.area, total_v2_len) != n_crc32c)
478 goto bad_header;
479 break;
480 }
Willy Tarreaue5733232019-05-22 19:24:06 +0200481#ifdef USE_NS
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100482 case PP2_TYPE_NETNS: {
483 const struct netns_entry *ns;
484
Tim Duesterhus56c176a2021-03-06 20:06:51 +0100485 ns = netns_store_lookup(istptr(tlv), istlen(tlv));
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100486 if (ns)
487 conn->proxy_netns = ns;
488 break;
489 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100490#endif
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100491 case PP2_TYPE_AUTHORITY: {
Tim Duesterhus615f81e2021-03-06 20:06:50 +0100492 if (istlen(tlv) > PP2_AUTHORITY_MAX)
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100493 goto bad_header;
Tim Duesterhus615f81e2021-03-06 20:06:50 +0100494 conn->proxy_authority = ist2(pool_alloc(pool_head_authority), 0);
495 if (!isttest(conn->proxy_authority))
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100496 goto fail;
Tim Duesterhus615f81e2021-03-06 20:06:50 +0100497 if (istcpy(&conn->proxy_authority, tlv, PP2_AUTHORITY_MAX) < 0) {
498 /* This is technically unreachable, because we verified above
499 * that the TLV value fits.
500 */
501 goto fail;
502 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100503 break;
504 }
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100505 case PP2_TYPE_UNIQUE_ID: {
Tim Duesterhus002bd772021-03-06 20:06:49 +0100506 if (istlen(tlv) > UNIQUEID_LEN)
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100507 goto bad_header;
Tim Duesterhus2b7f6c22020-03-14 13:07:05 +0100508 conn->proxy_unique_id = ist2(pool_alloc(pool_head_uniqueid), 0);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +0100509 if (!isttest(conn->proxy_unique_id))
510 goto fail;
511 if (istcpy(&conn->proxy_unique_id, tlv, UNIQUEID_LEN) < 0) {
512 /* This is technically unreachable, because we verified above
513 * that the TLV value fits.
514 */
515 goto fail;
516 }
517 break;
518 }
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100519 default:
520 break;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +0100521 }
522 }
523
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100524 /* Verify that the PROXYv2 header ends at a TLV boundary.
525 * This is technically unreachable, because the TLV parsing already
526 * verifies that a TLV does not exceed the total length and also
527 * that there is space for a TLV header.
528 */
529 if (tlv_offset != total_v2_len)
530 goto bad_header;
531
Willy Tarreau77992672014-06-14 11:06:17 +0200532 /* unsupported protocol, keep local connection address */
533 break;
534 case 0x00: /* LOCAL command */
535 /* keep local connection address for LOCAL */
536 break;
537 default:
538 goto bad_header; /* not a supported command */
539 }
540
Tim Duesterhus488ee7f2020-03-05 22:55:20 +0100541 trash.data = total_v2_len;
Willy Tarreau77992672014-06-14 11:06:17 +0200542 goto eat_header;
543
544 eat_header:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200545 /* remove the PROXY line from the request. For this we re-read the
546 * exact line at once. If we don't get the exact same result, we
547 * fail.
548 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100549 while (1) {
Tim Duesterhusa8692f32020-03-13 12:34:25 +0100550 ssize_t len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
551
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200552 if (len2 < 0 && errno == EINTR)
553 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200554 if (len2 != trash.data)
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100555 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100556 break;
557 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200558
559 conn->flags &= ~flag;
Emeric Brun4f603012017-01-05 15:11:44 +0100560 conn->flags |= CO_FL_RCVD_PROXY;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200561 return 1;
562
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200563 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200564 return 0;
565
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200566 missing:
567 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
568 * we have not read anything. Otherwise we need to fail because we won't
569 * be able to poll anymore.
570 */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100571 conn->err_code = CO_ER_PRX_TRUNCATED;
572 goto fail;
573
574 bad_header:
575 /* This is not a valid proxy protocol header */
576 conn->err_code = CO_ER_PRX_BAD_HDR;
577 goto fail;
578
579 recv_abort:
580 conn->err_code = CO_ER_PRX_ABORT;
Willy Tarreau26f4a042013-12-04 23:44:10 +0100581 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100582 goto fail;
583
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200584 fail:
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200585 conn->flags |= CO_FL_ERROR;
Willy Tarreaue1e4a612012-10-05 00:10:55 +0200586 return 0;
587}
588
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100589/* This handshake handler waits a NetScaler Client IP insertion header
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000590 * at the beginning of the raw data stream. The header format is
591 * described in doc/netscaler-client-ip-insertion-protocol.txt
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100592 *
593 * This line MUST be at the beginning of the buffer and MUST NOT be
594 * fragmented.
595 *
596 * The header line is small and in all cases smaller than the smallest normal
597 * TCP MSS. So it MUST always be delivered as one segment, which ensures we
598 * can safely use MSG_PEEK and avoid buffering.
599 *
600 * Once the data is fetched, the values are set in the connection's address
601 * fields, and data are removed from the socket's buffer. The function returns
602 * zero if it needs to wait for more data or if it fails, or 1 if it completed
603 * and removed itself.
604 */
605int conn_recv_netscaler_cip(struct connection *conn, int flag)
606{
607 char *line;
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000608 uint32_t hdr_len;
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100609 uint8_t ip_ver;
Willy Tarreaub406b872018-08-22 05:20:32 +0200610 int ret;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100611
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100612 if (!conn_ctrl_ready(conn))
613 goto fail;
614
Willy Tarreau9b7587a2020-10-15 07:32:10 +0200615 if (!sockaddr_alloc(&conn->src, NULL, 0) || !sockaddr_alloc(&conn->dst, NULL, 0))
Olivier Houchard1a9dbe52020-01-22 15:31:09 +0100616 goto fail;
617
Willy Tarreau585744b2017-08-24 14:31:19 +0200618 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200619 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100620
Willy Tarreau157788c2020-02-11 10:08:05 +0100621 while (1) {
Willy Tarreaub406b872018-08-22 05:20:32 +0200622 ret = recv(conn->handle.fd, trash.area, trash.size, MSG_PEEK);
623 if (ret < 0) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100624 if (errno == EINTR)
625 continue;
626 if (errno == EAGAIN) {
Willy Tarreau585744b2017-08-24 14:31:19 +0200627 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200628 goto not_ready;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100629 }
630 goto recv_abort;
631 }
Willy Tarreaub406b872018-08-22 05:20:32 +0200632 trash.data = ret;
Willy Tarreau157788c2020-02-11 10:08:05 +0100633 break;
634 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100635
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100636 conn->flags &= ~CO_FL_WAIT_L4_CONN;
637
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200638 if (!trash.data) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100639 /* client shutdown */
640 conn->err_code = CO_ER_CIP_EMPTY;
641 goto fail;
642 }
643
644 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000645 * CIP magic, header length or
646 * CIP magic, CIP length, CIP type, header length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200647 if (trash.data < 12)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100648 goto missing;
649
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200650 line = trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100651
652 /* Decode a possible NetScaler Client IP request, fail early if
653 * it does not match */
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100654 if (ntohl(read_u32(line)) != __objt_listener(conn->target)->bind_conf->ns_cip_magic)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100655 goto bad_magic;
656
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000657 /* Legacy CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200658 if ((trash.area[8] & 0xD0) == 0x40) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100659 hdr_len = ntohl(read_u32((line+4)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000660 line += 8;
661 }
662 /* Standard CIP protocol */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200663 else if (trash.area[8] == 0x00) {
Willy Tarreau1ac83af2020-02-25 10:06:49 +0100664 hdr_len = ntohs(read_u32((line+10)));
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000665 line += 12;
666 }
667 /* Unknown CIP protocol */
668 else {
669 conn->err_code = CO_ER_CIP_BAD_PROTO;
670 goto fail;
671 }
672
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100673 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin72fa1ec2017-12-12 01:17:23 +0000674 * a minimal IP header */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200675 if (trash.data < 20)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100676 goto missing;
677
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100678 /* Get IP version from the first four bits */
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100679 ip_ver = (*line & 0xf0) >> 4;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100680
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100681 if (ip_ver == 4) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100682 struct ip *hdr_ip4;
David Carlier3015a2e2016-07-04 22:51:33 +0100683 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100684
685 hdr_ip4 = (struct ip *)line;
686
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200687 if (trash.data < 40 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100688 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000689 * IPv4 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100690 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000691 }
692 else if (hdr_ip4->ip_p != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100693 /* The protocol does not include a TCP header */
694 conn->err_code = CO_ER_CIP_BAD_PROTO;
695 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000696 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100697
David Carlier3015a2e2016-07-04 22:51:33 +0100698 hdr_tcp = (struct my_tcphdr *)(line + (hdr_ip4->ip_hl * 4));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100699
700 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200701 ((struct sockaddr_in *)conn->src)->sin_family = AF_INET;
702 ((struct sockaddr_in *)conn->src)->sin_addr.s_addr = hdr_ip4->ip_src.s_addr;
703 ((struct sockaddr_in *)conn->src)->sin_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100704
Willy Tarreau226572f2019-07-17 14:46:00 +0200705 ((struct sockaddr_in *)conn->dst)->sin_family = AF_INET;
706 ((struct sockaddr_in *)conn->dst)->sin_addr.s_addr = hdr_ip4->ip_dst.s_addr;
707 ((struct sockaddr_in *)conn->dst)->sin_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100708
709 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
710 }
Willy Tarreau0ca24aa2019-03-29 17:35:32 +0100711 else if (ip_ver == 6) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100712 struct ip6_hdr *hdr_ip6;
David Carlier3015a2e2016-07-04 22:51:33 +0100713 struct my_tcphdr *hdr_tcp;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100714
715 hdr_ip6 = (struct ip6_hdr *)line;
716
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200717 if (trash.data < 60 || trash.data < hdr_len) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100718 /* Fail if buffer length is not large enough to contain
Bertrand Jacquin67de5a22017-12-13 01:15:05 +0000719 * IPv6 header, TCP header */
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100720 goto missing;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000721 }
722 else if (hdr_ip6->ip6_nxt != IPPROTO_TCP) {
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100723 /* The protocol does not include a TCP header */
724 conn->err_code = CO_ER_CIP_BAD_PROTO;
725 goto fail;
Bertrand Jacquinb3875912017-12-13 00:58:51 +0000726 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100727
David Carlier3015a2e2016-07-04 22:51:33 +0100728 hdr_tcp = (struct my_tcphdr *)(line + sizeof(struct ip6_hdr));
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100729
730 /* update the session's addresses and mark them set */
Willy Tarreau226572f2019-07-17 14:46:00 +0200731 ((struct sockaddr_in6 *)conn->src)->sin6_family = AF_INET6;
732 ((struct sockaddr_in6 *)conn->src)->sin6_addr = hdr_ip6->ip6_src;
733 ((struct sockaddr_in6 *)conn->src)->sin6_port = hdr_tcp->source;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100734
Willy Tarreau226572f2019-07-17 14:46:00 +0200735 ((struct sockaddr_in6 *)conn->dst)->sin6_family = AF_INET6;
736 ((struct sockaddr_in6 *)conn->dst)->sin6_addr = hdr_ip6->ip6_dst;
737 ((struct sockaddr_in6 *)conn->dst)->sin6_port = hdr_tcp->dest;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100738
739 conn->flags |= CO_FL_ADDR_FROM_SET | CO_FL_ADDR_TO_SET;
740 }
741 else {
742 /* The protocol does not match something known (IPv4/IPv6) */
743 conn->err_code = CO_ER_CIP_BAD_PROTO;
744 goto fail;
745 }
746
Bertrand Jacquin7d668f92017-12-13 01:23:39 +0000747 line += hdr_len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200748 trash.data = line - trash.area;
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100749
750 /* remove the NetScaler Client IP header from the request. For this
751 * we re-read the exact line at once. If we don't get the exact same
752 * result, we fail.
753 */
Willy Tarreau157788c2020-02-11 10:08:05 +0100754 while (1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200755 int len2 = recv(conn->handle.fd, trash.area, trash.data, 0);
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100756 if (len2 < 0 && errno == EINTR)
757 continue;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200758 if (len2 != trash.data)
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100759 goto recv_abort;
Willy Tarreau157788c2020-02-11 10:08:05 +0100760 break;
761 }
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100762
763 conn->flags &= ~flag;
764 return 1;
765
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200766 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200767 return 0;
768
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100769 missing:
770 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
771 * we have not read anything. Otherwise we need to fail because we won't
772 * be able to poll anymore.
773 */
774 conn->err_code = CO_ER_CIP_TRUNCATED;
775 goto fail;
776
777 bad_magic:
778 conn->err_code = CO_ER_CIP_BAD_MAGIC;
779 goto fail;
780
781 recv_abort:
782 conn->err_code = CO_ER_CIP_ABORT;
783 conn->flags |= CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH;
784 goto fail;
785
786 fail:
Bertrand Jacquin93b227d2016-06-04 15:11:10 +0100787 conn->flags |= CO_FL_ERROR;
788 return 0;
789}
790
Alexander Liu2a54bb72019-05-22 19:44:48 +0800791
792int conn_send_socks4_proxy_request(struct connection *conn)
793{
794 struct socks4_request req_line;
795
Alexander Liu2a54bb72019-05-22 19:44:48 +0800796 if (!conn_ctrl_ready(conn))
797 goto out_error;
798
Willy Tarreau226572f2019-07-17 14:46:00 +0200799 if (!conn_get_dst(conn))
800 goto out_error;
801
Alexander Liu2a54bb72019-05-22 19:44:48 +0800802 req_line.version = 0x04;
803 req_line.command = 0x01;
Willy Tarreau226572f2019-07-17 14:46:00 +0200804 req_line.port = get_net_port(conn->dst);
805 req_line.ip = is_inet_addr(conn->dst);
Alexander Liu2a54bb72019-05-22 19:44:48 +0800806 memcpy(req_line.user_id, "HAProxy\0", 8);
807
808 if (conn->send_proxy_ofs > 0) {
809 /*
810 * This is the first call to send the request
811 */
812 conn->send_proxy_ofs = -(int)sizeof(req_line);
813 }
814
815 if (conn->send_proxy_ofs < 0) {
816 int ret = 0;
817
818 /* we are sending the socks4_req_line here. If the data layer
819 * has a pending write, we'll also set MSG_MORE.
820 */
Willy Tarreau827fee72020-12-11 15:26:55 +0100821 ret = conn_ctrl_send(
Alexander Liu2a54bb72019-05-22 19:44:48 +0800822 conn,
823 ((char *)(&req_line)) + (sizeof(req_line)+conn->send_proxy_ofs),
824 -conn->send_proxy_ofs,
Willy Tarreau827fee72020-12-11 15:26:55 +0100825 (conn->subs && conn->subs->events & SUB_RETRY_SEND) ? CO_SFL_MSG_MORE : 0);
Alexander Liu2a54bb72019-05-22 19:44:48 +0800826
827 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Before send remain is [%d], sent [%d]\n",
828 conn->handle.fd, -conn->send_proxy_ofs, ret);
829
830 if (ret < 0) {
831 goto out_error;
832 }
833
834 conn->send_proxy_ofs += ret; /* becomes zero once complete */
835 if (conn->send_proxy_ofs != 0) {
836 goto out_wait;
837 }
838 }
839
840 /* OK we've the whole request sent */
841 conn->flags &= ~CO_FL_SOCKS4_SEND;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800842
843 /* The connection is ready now, simply return and let the connection
844 * handler notify upper layers if needed.
845 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100846 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800847
848 if (conn->flags & CO_FL_SEND_PROXY) {
849 /*
850 * Get the send_proxy_ofs ready for the send_proxy due to we are
851 * reusing the "send_proxy_ofs", and SOCKS4 handshake should be done
852 * before sending PROXY Protocol.
853 */
854 conn->send_proxy_ofs = 1;
855 }
856 return 1;
857
858 out_error:
859 /* Write error on the file descriptor */
860 conn->flags |= CO_FL_ERROR;
861 if (conn->err_code == CO_ER_NONE) {
862 conn->err_code = CO_ER_SOCKS4_SEND;
863 }
864 return 0;
865
866 out_wait:
Alexander Liu2a54bb72019-05-22 19:44:48 +0800867 return 0;
868}
869
870int conn_recv_socks4_proxy_response(struct connection *conn)
871{
872 char line[SOCKS4_HS_RSP_LEN];
873 int ret;
874
Alexander Liu2a54bb72019-05-22 19:44:48 +0800875 if (!conn_ctrl_ready(conn))
876 goto fail;
877
878 if (!fd_recv_ready(conn->handle.fd))
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200879 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800880
Willy Tarreau157788c2020-02-11 10:08:05 +0100881 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +0800882 /* SOCKS4 Proxy will response with 8 bytes, 0x00 | 0x5A | 0x00 0x00 | 0x00 0x00 0x00 0x00
883 * Try to peek into it, before all 8 bytes ready.
884 */
885 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, MSG_PEEK);
886
887 if (ret == 0) {
888 /* the socket has been closed or shutdown for send */
889 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d], looks like the socket has been closed or shutdown for send\n",
890 conn->handle.fd, ret, errno);
891 if (conn->err_code == CO_ER_NONE) {
892 conn->err_code = CO_ER_SOCKS4_RECV;
893 }
894 goto fail;
895 }
896
897 if (ret > 0) {
898 if (ret == SOCKS4_HS_RSP_LEN) {
899 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received 8 bytes, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
900 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
901 }else{
902 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]);
903 }
904 } else {
905 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: Received ret[%d], errno[%d]\n", conn->handle.fd, ret, errno);
906 }
907
908 if (ret < 0) {
909 if (errno == EINTR) {
910 continue;
911 }
912 if (errno == EAGAIN) {
913 fd_cant_recv(conn->handle.fd);
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200914 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800915 }
916 goto recv_abort;
917 }
Willy Tarreau157788c2020-02-11 10:08:05 +0100918 break;
919 }
Alexander Liu2a54bb72019-05-22 19:44:48 +0800920
Willy Tarreauc192b0a2020-01-23 09:11:58 +0100921 conn->flags &= ~CO_FL_WAIT_L4_CONN;
922
Alexander Liu2a54bb72019-05-22 19:44:48 +0800923 if (ret < SOCKS4_HS_RSP_LEN) {
924 /* Missing data. Since we're using MSG_PEEK, we can only poll again if
925 * we are not able to read enough data.
926 */
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200927 goto not_ready;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800928 }
929
930 /*
931 * Base on the SOCSK4 protocol:
932 *
933 * +----+----+----+----+----+----+----+----+
934 * | VN | CD | DSTPORT | DSTIP |
935 * +----+----+----+----+----+----+----+----+
936 * # of bytes: 1 1 2 4
937 * VN is the version of the reply code and should be 0. CD is the result
938 * code with one of the following values:
939 * 90: request granted
940 * 91: request rejected or failed
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500941 * 92: request rejected because SOCKS server cannot connect to identd on the client
Alexander Liu2a54bb72019-05-22 19:44:48 +0800942 * 93: request rejected because the client program and identd report different user-ids
943 * The remaining fields are ignored.
944 */
945 if (line[1] != 90) {
946 conn->flags &= ~CO_FL_SOCKS4_RECV;
947
948 DPRINTF(stderr, "SOCKS PROXY HS FD[%04X]: FAIL, the response is [%02X|%02X|%02X %02X|%02X %02X %02X %02X]\n",
949 conn->handle.fd, line[0], line[1], line[2], line[3], line[4], line[5], line[6], line[7]);
950 if (conn->err_code == CO_ER_NONE) {
951 conn->err_code = CO_ER_SOCKS4_DENY;
952 }
953 goto fail;
954 }
955
956 /* remove the 8 bytes response from the stream */
Willy Tarreau157788c2020-02-11 10:08:05 +0100957 while (1) {
Alexander Liu2a54bb72019-05-22 19:44:48 +0800958 ret = recv(conn->handle.fd, line, SOCKS4_HS_RSP_LEN, 0);
959 if (ret < 0 && errno == EINTR) {
960 continue;
961 }
962 if (ret != SOCKS4_HS_RSP_LEN) {
963 if (conn->err_code == CO_ER_NONE) {
964 conn->err_code = CO_ER_SOCKS4_RECV;
965 }
966 goto fail;
967 }
Willy Tarreau157788c2020-02-11 10:08:05 +0100968 break;
969 }
Alexander Liu2a54bb72019-05-22 19:44:48 +0800970
971 conn->flags &= ~CO_FL_SOCKS4_RECV;
972 return 1;
973
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200974 not_ready:
Willy Tarreau6499b9d2019-06-03 08:17:30 +0200975 return 0;
976
Alexander Liu2a54bb72019-05-22 19:44:48 +0800977 recv_abort:
978 if (conn->err_code == CO_ER_NONE) {
979 conn->err_code = CO_ER_SOCKS4_ABORT;
980 }
981 conn->flags |= (CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH);
982 goto fail;
983
984 fail:
Alexander Liu2a54bb72019-05-22 19:44:48 +0800985 conn->flags |= CO_FL_ERROR;
986 return 0;
987}
988
Ilya Shipitsinca56fce2018-09-15 00:50:05 +0500989/* Note: <remote> is explicitly allowed to be NULL */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100990int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
David Safb76832014-05-08 23:42:08 -0400991{
992 int ret = 0;
993
994 if (srv && (srv->pp_opts & SRV_PP_V2)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100995 ret = make_proxy_line_v2(buf, buf_len, srv, remote, strm);
David Safb76832014-05-08 23:42:08 -0400996 }
997 else {
Willy Tarreau226572f2019-07-17 14:46:00 +0200998 if (remote && conn_get_src(remote) && conn_get_dst(remote))
999 ret = make_proxy_line_v1(buf, buf_len, remote->src, remote->dst);
David Safb76832014-05-08 23:42:08 -04001000 else
1001 ret = make_proxy_line_v1(buf, buf_len, NULL, NULL);
1002 }
1003
1004 return ret;
1005}
1006
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001007/* Makes a PROXY protocol line from the two addresses. The output is sent to
1008 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
1009 * It returns the number of bytes composing this line (including the trailing
1010 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
Willy Tarreau2e1401a2013-10-01 11:41:55 +02001011 * TCP6 and "UNKNOWN" formats. If any of <src> or <dst> is null, UNKNOWN is
1012 * emitted as well.
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001013 */
David Safb76832014-05-08 23:42:08 -04001014int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001015{
1016 int ret = 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001017 char * protocol;
1018 char src_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1019 char dst_str[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
1020 in_port_t src_port;
1021 in_port_t dst_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001022
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001023 if ( !src
1024 || !dst
1025 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1026 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
1027 /* unknown family combination */
1028 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001029 if (ret >= buf_len)
1030 return 0;
1031
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001032 return ret;
1033 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001034
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001035 /* IPv4 for both src and dst */
1036 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1037 protocol = "TCP4";
1038 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)src)->sin_addr, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001039 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001040 src_port = ((struct sockaddr_in *)src)->sin_port;
1041 if (!inet_ntop(AF_INET, &((struct sockaddr_in *)dst)->sin_addr, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001042 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001043 dst_port = ((struct sockaddr_in *)dst)->sin_port;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001044 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001045 /* IPv6 for at least one of src and dst */
1046 else {
1047 struct in6_addr tmp;
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001048
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001049 protocol = "TCP6";
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001050
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001051 if (src->ss_family == AF_INET) {
1052 /* Convert src to IPv6 */
1053 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1054 src_port = ((struct sockaddr_in *)src)->sin_port;
1055 }
1056 else {
1057 tmp = ((struct sockaddr_in6 *)src)->sin6_addr;
1058 src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1059 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001060
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001061 if (!inet_ntop(AF_INET6, &tmp, src_str, sizeof(src_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001062 return 0;
1063
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001064 if (dst->ss_family == AF_INET) {
1065 /* Convert dst to IPv6 */
1066 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1067 dst_port = ((struct sockaddr_in *)dst)->sin_port;
1068 }
1069 else {
1070 tmp = ((struct sockaddr_in6 *)dst)->sin6_addr;
1071 dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1072 }
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001073
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001074 if (!inet_ntop(AF_INET6, &tmp, dst_str, sizeof(dst_str)))
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001075 return 0;
1076 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001077
1078 ret = snprintf(buf, buf_len, "PROXY %s %s %s %u %u\r\n", protocol, src_str, dst_str, ntohs(src_port), ntohs(dst_port));
1079 if (ret >= buf_len)
1080 return 0;
1081
Willy Tarreaue1e4a612012-10-05 00:10:55 +02001082 return ret;
1083}
David Safb76832014-05-08 23:42:08 -04001084
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001085static int make_tlv(char *dest, int dest_len, char type, uint16_t length, const char *value)
David Safb76832014-05-08 23:42:08 -04001086{
1087 struct tlv *tlv;
1088
1089 if (!dest || (length + sizeof(*tlv) > dest_len))
1090 return 0;
1091
1092 tlv = (struct tlv *)dest;
1093
1094 tlv->type = type;
1095 tlv->length_hi = length >> 8;
1096 tlv->length_lo = length & 0x00ff;
1097 memcpy(tlv->value, value, length);
1098 return length + sizeof(*tlv);
1099}
David Safb76832014-05-08 23:42:08 -04001100
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001101/* Note: <remote> is explicitly allowed to be NULL */
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001102int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm)
David Safb76832014-05-08 23:42:08 -04001103{
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001104 const char pp2_signature[] = PP2_SIGNATURE;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001105 void *tlv_crc32c_p = NULL;
David Safb76832014-05-08 23:42:08 -04001106 int ret = 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001107 struct proxy_hdr_v2 *hdr = (struct proxy_hdr_v2 *)buf;
Vincent Bernat6e615892016-05-18 16:17:44 +02001108 struct sockaddr_storage null_addr = { .ss_family = 0 };
David Safb76832014-05-08 23:42:08 -04001109 struct sockaddr_storage *src = &null_addr;
1110 struct sockaddr_storage *dst = &null_addr;
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001111 const char *value;
1112 int value_len;
David Safb76832014-05-08 23:42:08 -04001113
1114 if (buf_len < PP2_HEADER_LEN)
1115 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001116 memcpy(hdr->sig, pp2_signature, PP2_SIGNATURE_LEN);
David Safb76832014-05-08 23:42:08 -04001117
Willy Tarreau226572f2019-07-17 14:46:00 +02001118 if (remote && conn_get_src(remote) && conn_get_dst(remote)) {
1119 src = remote->src;
1120 dst = remote->dst;
David Safb76832014-05-08 23:42:08 -04001121 }
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001122
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001123 /* At least one of src or dst is not of AF_INET or AF_INET6 */
1124 if ( !src
1125 || !dst
Willy Tarreau119e50e2020-05-22 13:53:29 +02001126 || (!pp2_never_send_local && conn_is_back(remote)) // locally initiated connection
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001127 || (src->ss_family != AF_INET && src->ss_family != AF_INET6)
1128 || (dst->ss_family != AF_INET && dst->ss_family != AF_INET6)) {
David Safb76832014-05-08 23:42:08 -04001129 if (buf_len < PP2_HDR_LEN_UNSPEC)
1130 return 0;
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001131 hdr->ver_cmd = PP2_VERSION | PP2_CMD_LOCAL;
1132 hdr->fam = PP2_FAM_UNSPEC | PP2_TRANS_UNSPEC;
David Safb76832014-05-08 23:42:08 -04001133 ret = PP2_HDR_LEN_UNSPEC;
1134 }
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001135 else {
Willy Tarreau02c88032020-04-14 12:54:10 +02001136 hdr->ver_cmd = PP2_VERSION | PP2_CMD_PROXY;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001137 /* IPv4 for both src and dst */
1138 if (src->ss_family == AF_INET && dst->ss_family == AF_INET) {
1139 if (buf_len < PP2_HDR_LEN_INET)
1140 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001141 hdr->fam = PP2_FAM_INET | PP2_TRANS_STREAM;
1142 hdr->addr.ip4.src_addr = ((struct sockaddr_in *)src)->sin_addr.s_addr;
1143 hdr->addr.ip4.src_port = ((struct sockaddr_in *)src)->sin_port;
1144 hdr->addr.ip4.dst_addr = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
1145 hdr->addr.ip4.dst_port = ((struct sockaddr_in *)dst)->sin_port;
1146 ret = PP2_HDR_LEN_INET;
1147 }
1148 /* IPv6 for at least one of src and dst */
1149 else {
1150 struct in6_addr tmp;
1151
1152 if (buf_len < PP2_HDR_LEN_INET6)
1153 return 0;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001154 hdr->fam = PP2_FAM_INET6 | PP2_TRANS_STREAM;
1155 if (src->ss_family == AF_INET) {
1156 v4tov6(&tmp, &((struct sockaddr_in *)src)->sin_addr);
1157 memcpy(hdr->addr.ip6.src_addr, &tmp, 16);
1158 hdr->addr.ip6.src_port = ((struct sockaddr_in *)src)->sin_port;
1159 }
1160 else {
1161 memcpy(hdr->addr.ip6.src_addr, &((struct sockaddr_in6 *)src)->sin6_addr, 16);
1162 hdr->addr.ip6.src_port = ((struct sockaddr_in6 *)src)->sin6_port;
1163 }
1164 if (dst->ss_family == AF_INET) {
1165 v4tov6(&tmp, &((struct sockaddr_in *)dst)->sin_addr);
1166 memcpy(hdr->addr.ip6.dst_addr, &tmp, 16);
William Dauchybd8bf672020-01-26 19:06:39 +01001167 hdr->addr.ip6.dst_port = ((struct sockaddr_in *)dst)->sin_port;
Tim Duesterhus7fec0212018-07-27 18:46:13 +02001168 }
1169 else {
1170 memcpy(hdr->addr.ip6.dst_addr, &((struct sockaddr_in6 *)dst)->sin6_addr, 16);
1171 hdr->addr.ip6.dst_port = ((struct sockaddr_in6 *)dst)->sin6_port;
1172 }
1173
1174 ret = PP2_HDR_LEN_INET6;
1175 }
1176 }
David Safb76832014-05-08 23:42:08 -04001177
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001178 if (srv->pp_opts & SRV_PP_V2_CRC32C) {
1179 uint32_t zero_crc32c = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001180
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001181 if ((buf_len - ret) < sizeof(struct tlv))
1182 return 0;
1183 tlv_crc32c_p = (void *)((struct tlv *)&buf[ret])->value;
1184 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_CRC32C, sizeof(zero_crc32c), (const char *)&zero_crc32c);
1185 }
1186
Ilya Shipitsinca56fce2018-09-15 00:50:05 +05001187 if (remote && conn_get_alpn(remote, &value, &value_len)) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001188 if ((buf_len - ret) < sizeof(struct tlv))
1189 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001190 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_ALPN, value_len, value);
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001191 }
1192
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001193 if (srv->pp_opts & SRV_PP_V2_AUTHORITY) {
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001194 value = NULL;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001195 if (remote && isttest(remote->proxy_authority)) {
1196 value = istptr(remote->proxy_authority);
1197 value_len = istlen(remote->proxy_authority);
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001198 }
1199#ifdef USE_OPENSSL
1200 else {
Jerome Magnin78891c72019-09-02 09:53:41 +02001201 if ((value = ssl_sock_get_sni(remote)))
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001202 value_len = strlen(value);
1203 }
1204#endif
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001205 if (value) {
1206 if ((buf_len - ret) < sizeof(struct tlv))
1207 return 0;
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001208 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_AUTHORITY, value_len, value);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001209 }
1210 }
1211
Christopher Faulet3ab504f2020-05-26 15:16:01 +02001212 if (strm && (srv->pp_opts & SRV_PP_V2_UNIQUE_ID)) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001213 struct session* sess = strm_sess(strm);
1214 struct ist unique_id = stream_generate_unique_id(strm, &sess->fe->format_unique_id);
1215
1216 value = unique_id.ptr;
1217 value_len = unique_id.len;
1218
1219 if (value_len >= 0) {
1220 if ((buf_len - ret) < sizeof(struct tlv))
1221 return 0;
1222 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_UNIQUE_ID, value_len, value);
1223 }
1224 }
1225
Emmanuel Hocdet8a4ffa02019-08-29 11:54:51 +02001226#ifdef USE_OPENSSL
David Safb76832014-05-08 23:42:08 -04001227 if (srv->pp_opts & SRV_PP_V2_SSL) {
Emmanuel Hocdet404d9782017-10-24 10:55:14 +02001228 struct tlv_ssl *tlv;
1229 int ssl_tlv_len = 0;
Tim Duesterhusa8692f32020-03-13 12:34:25 +01001230
David Safb76832014-05-08 23:42:08 -04001231 if ((buf_len - ret) < sizeof(struct tlv_ssl))
1232 return 0;
1233 tlv = (struct tlv_ssl *)&buf[ret];
1234 memset(tlv, 0, sizeof(struct tlv_ssl));
1235 ssl_tlv_len += sizeof(struct tlv_ssl);
1236 tlv->tlv.type = PP2_TYPE_SSL;
1237 if (ssl_sock_is_ssl(remote)) {
1238 tlv->client |= PP2_CLIENT_SSL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02001239 value = ssl_sock_get_proto_version(remote);
David Safb76832014-05-08 23:42:08 -04001240 if (value) {
Emmanuel Hocdet8c0c34b2018-02-28 12:02:14 +01001241 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len-ret-ssl_tlv_len), PP2_SUBTYPE_SSL_VERSION, strlen(value), value);
David Safb76832014-05-08 23:42:08 -04001242 }
Dave McCowan328fb582014-07-30 10:39:13 -04001243 if (ssl_sock_get_cert_used_sess(remote)) {
1244 tlv->client |= PP2_CLIENT_CERT_SESS;
David Safb76832014-05-08 23:42:08 -04001245 tlv->verify = htonl(ssl_sock_get_verify_result(remote));
Dave McCowan328fb582014-07-30 10:39:13 -04001246 if (ssl_sock_get_cert_used_conn(remote))
1247 tlv->client |= PP2_CLIENT_CERT_CONN;
David Safb76832014-05-08 23:42:08 -04001248 }
1249 if (srv->pp_opts & SRV_PP_V2_SSL_CN) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001250 struct buffer *cn_trash = get_trash_chunk();
Willy Tarreau3b9a0c92014-07-19 06:37:33 +02001251 if (ssl_sock_get_remote_common_name(remote, cn_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001252 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CN,
1253 cn_trash->data,
1254 cn_trash->area);
David Safb76832014-05-08 23:42:08 -04001255 }
1256 }
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001257 if (srv->pp_opts & SRV_PP_V2_SSL_KEY_ALG) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001258 struct buffer *pkey_trash = get_trash_chunk();
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001259 if (ssl_sock_get_pkey_algo(remote, pkey_trash) > 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001260 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_KEY_ALG,
1261 pkey_trash->data,
1262 pkey_trash->area);
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001263 }
1264 }
1265 if (srv->pp_opts & SRV_PP_V2_SSL_SIG_ALG) {
1266 value = ssl_sock_get_cert_sig(remote);
1267 if (value) {
1268 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_SIG_ALG, strlen(value), value);
1269 }
1270 }
1271 if (srv->pp_opts & SRV_PP_V2_SSL_CIPHER) {
1272 value = ssl_sock_get_cipher_name(remote);
1273 if (value) {
1274 ssl_tlv_len += make_tlv(&buf[ret+ssl_tlv_len], (buf_len - ret - ssl_tlv_len), PP2_SUBTYPE_SSL_CIPHER, strlen(value), value);
1275 }
1276 }
David Safb76832014-05-08 23:42:08 -04001277 }
1278 tlv->tlv.length_hi = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) >> 8;
1279 tlv->tlv.length_lo = (uint16_t)(ssl_tlv_len - sizeof(struct tlv)) & 0x00ff;
1280 ret += ssl_tlv_len;
1281 }
1282#endif
1283
Willy Tarreaue5733232019-05-22 19:24:06 +02001284#ifdef USE_NS
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001285 if (remote && (remote->proxy_netns)) {
1286 if ((buf_len - ret) < sizeof(struct tlv))
1287 return 0;
Emmanuel Hocdet571c7ac2017-10-31 18:24:05 +01001288 ret += make_tlv(&buf[ret], (buf_len - ret), PP2_TYPE_NETNS, remote->proxy_netns->name_len, remote->proxy_netns->node.key);
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01001289 }
1290#endif
1291
Willy Tarreau8fccfa22014-06-14 08:28:06 +02001292 hdr->len = htons((uint16_t)(ret - PP2_HEADER_LEN));
David Safb76832014-05-08 23:42:08 -04001293
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001294 if (tlv_crc32c_p) {
1295 write_u32(tlv_crc32c_p, htonl(hash_crc32c(buf, ret)));
1296 }
1297
David Safb76832014-05-08 23:42:08 -04001298 return ret;
1299}
Emeric Brun4f603012017-01-05 15:11:44 +01001300
Willy Tarreau119e50e2020-05-22 13:53:29 +02001301/* returns 0 on success */
1302static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001303 const struct proxy *defpx, const char *file, int line,
Willy Tarreau119e50e2020-05-22 13:53:29 +02001304 char **err)
1305{
1306 if (too_many_args(0, args, err, NULL))
1307 return -1;
1308 pp2_never_send_local = 1;
1309 return 0;
1310}
1311
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001312/* return the major HTTP version as 1 or 2 depending on how the request arrived
1313 * before being processed.
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02001314 *
1315 * WARNING: Should be updated if a new major HTTP version is added.
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001316 */
1317static int
1318smp_fetch_fc_http_major(const struct arg *args, struct sample *smp, const char *kw, void *private)
1319{
Christopher Faulet242f8ce2021-04-14 15:46:49 +02001320 struct connection *conn = NULL;
1321
1322 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
1323 conn = (kw[0] == 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
1324 else
1325 conn = (kw[0] != 'b') ? objt_conn(smp->sess->origin) :
1326 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001327
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02001328 /* No connection or a connection with a RAW muxx */
1329 if (!conn || (conn->mux && !(conn->mux->flags & MX_FL_HTX)))
1330 return 0;
1331
1332 /* No mux install, this may change */
1333 if (!conn->mux) {
1334 smp->flags |= SMP_F_MAY_CHANGE;
1335 return 0;
1336 }
1337
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001338 smp->data.type = SMP_T_SINT;
Christopher Fauletf4dd9ae2021-04-14 15:40:30 +02001339 smp->data.u.sint = (strcmp(conn_get_mux_name(conn), "H2") == 0) ? 2 : 1;
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001340 return 1;
1341}
1342
Emeric Brun4f603012017-01-05 15:11:44 +01001343/* fetch if the received connection used a PROXY protocol header */
1344int smp_fetch_fc_rcvd_proxy(const struct arg *args, struct sample *smp, const char *kw, void *private)
1345{
1346 struct connection *conn;
1347
1348 conn = objt_conn(smp->sess->origin);
1349 if (!conn)
1350 return 0;
1351
Willy Tarreau911db9b2020-01-23 16:27:54 +01001352 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun4f603012017-01-05 15:11:44 +01001353 smp->flags |= SMP_F_MAY_CHANGE;
1354 return 0;
1355 }
1356
1357 smp->flags = 0;
1358 smp->data.type = SMP_T_BOOL;
1359 smp->data.u.sint = (conn->flags & CO_FL_RCVD_PROXY) ? 1 : 0;
1360
1361 return 1;
1362}
1363
Geoff Simmons7185b782019-08-27 18:31:16 +02001364/* fetch the authority TLV from a PROXY protocol header */
1365int smp_fetch_fc_pp_authority(const struct arg *args, struct sample *smp, const char *kw, void *private)
1366{
1367 struct connection *conn;
1368
1369 conn = objt_conn(smp->sess->origin);
1370 if (!conn)
1371 return 0;
1372
Willy Tarreau911db9b2020-01-23 16:27:54 +01001373 if (conn->flags & CO_FL_WAIT_XPRT) {
Geoff Simmons7185b782019-08-27 18:31:16 +02001374 smp->flags |= SMP_F_MAY_CHANGE;
1375 return 0;
1376 }
1377
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001378 if (!isttest(conn->proxy_authority))
Geoff Simmons7185b782019-08-27 18:31:16 +02001379 return 0;
1380
1381 smp->flags = 0;
1382 smp->data.type = SMP_T_STR;
Tim Duesterhus615f81e2021-03-06 20:06:50 +01001383 smp->data.u.str.area = istptr(conn->proxy_authority);
1384 smp->data.u.str.data = istlen(conn->proxy_authority);
Geoff Simmons7185b782019-08-27 18:31:16 +02001385
1386 return 1;
1387}
1388
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001389/* fetch the unique ID TLV from a PROXY protocol header */
1390int smp_fetch_fc_pp_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
1391{
1392 struct connection *conn;
1393
1394 conn = objt_conn(smp->sess->origin);
1395 if (!conn)
1396 return 0;
1397
1398 if (conn->flags & CO_FL_WAIT_XPRT) {
1399 smp->flags |= SMP_F_MAY_CHANGE;
1400 return 0;
1401 }
1402
1403 if (!isttest(conn->proxy_unique_id))
1404 return 0;
1405
1406 smp->flags = 0;
1407 smp->data.type = SMP_T_STR;
Tim Duesterhus002bd772021-03-06 20:06:49 +01001408 smp->data.u.str.area = istptr(conn->proxy_unique_id);
1409 smp->data.u.str.data = istlen(conn->proxy_unique_id);
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001410
1411 return 1;
1412}
1413
Emeric Brun4f603012017-01-05 15:11:44 +01001414/* Note: must not be declared <const> as its list will be overwritten.
1415 * Note: fetches that may return multiple types must be declared as the lowest
1416 * common denominator, the type that can be casted into all other ones. For
1417 * instance v4/v6 must be declared v4.
1418 */
1419static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Willy Tarreau60ca10a2017-08-18 15:26:54 +02001420 { "fc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Jérôme Magnin86577422018-12-07 09:03:11 +01001421 { "bc_http_major", smp_fetch_fc_http_major, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
Emeric Brun4f603012017-01-05 15:11:44 +01001422 { "fc_rcvd_proxy", smp_fetch_fc_rcvd_proxy, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
Geoff Simmons7185b782019-08-27 18:31:16 +02001423 { "fc_pp_authority", smp_fetch_fc_pp_authority, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Tim Duesterhusd1b15b62020-03-13 12:34:23 +01001424 { "fc_pp_unique_id", smp_fetch_fc_pp_unique_id, 0, NULL, SMP_T_STR, SMP_USE_L4CLI },
Emeric Brun4f603012017-01-05 15:11:44 +01001425 { /* END */ },
1426}};
1427
Willy Tarreau0108d902018-11-25 19:14:37 +01001428INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
Willy Tarreau119e50e2020-05-22 13:53:29 +02001429
1430static struct cfg_kw_list cfg_kws = {ILH, {
1431 { CFG_GLOBAL, "pp2-never-send-local", cfg_parse_pp2_never_send_local },
1432 { /* END */ },
1433}};
1434
1435INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01001436
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001437/* private function to handle sockaddr as input for connection hash */
1438static void conn_calculate_hash_sockaddr(const struct sockaddr_storage *ss,
1439 char *buf, size_t *idx,
1440 enum conn_hash_params_t *hash_flags,
1441 enum conn_hash_params_t param_type_addr,
1442 enum conn_hash_params_t param_type_port)
1443{
1444 struct sockaddr_in *addr;
1445 struct sockaddr_in6 *addr6;
1446
1447 switch (ss->ss_family) {
1448 case AF_INET:
1449 addr = (struct sockaddr_in *)ss;
1450
1451 conn_hash_update(buf, idx,
1452 &addr->sin_addr, sizeof(addr->sin_addr),
1453 hash_flags, param_type_addr);
1454
1455 if (addr->sin_port) {
1456 conn_hash_update(buf, idx,
1457 &addr->sin_port, sizeof(addr->sin_port),
1458 hash_flags, param_type_port);
1459 }
1460
1461 break;
1462
1463 case AF_INET6:
1464 addr6 = (struct sockaddr_in6 *)ss;
1465
1466 conn_hash_update(buf, idx,
1467 &addr6->sin6_addr, sizeof(addr6->sin6_addr),
1468 hash_flags, param_type_addr);
1469
1470 if (addr6->sin6_port) {
1471 conn_hash_update(buf, idx,
1472 &addr6->sin6_port, sizeof(addr6->sin6_port),
1473 hash_flags, param_type_port);
1474 }
1475
1476 break;
1477 }
1478}
1479
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01001480XXH64_hash_t conn_calculate_hash(const struct conn_hash_params *params)
1481{
1482 char *buf;
1483 size_t idx = 0;
1484 XXH64_hash_t hash = 0;
1485 enum conn_hash_params_t hash_flags = 0;
1486
1487 buf = trash.area;
1488
Amaury Denoyelle8ede3db2021-03-02 14:38:53 +01001489 conn_hash_update(buf, &idx, &params->target, sizeof(params->target), &hash_flags, 0);
Amaury Denoyelle1a58aca2021-01-22 16:47:46 +01001490
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001491 if (params->sni_prehash) {
1492 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01001493 &params->sni_prehash, sizeof(params->sni_prehash),
Amaury Denoyelle9b626e32021-01-06 17:03:27 +01001494 &hash_flags, CONN_HASH_PARAMS_TYPE_SNI);
1495 }
1496
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001497 if (params->dst_addr) {
1498 conn_calculate_hash_sockaddr(params->dst_addr,
1499 buf, &idx, &hash_flags,
1500 CONN_HASH_PARAMS_TYPE_DST_ADDR,
1501 CONN_HASH_PARAMS_TYPE_DST_PORT);
1502 }
1503
Amaury Denoyelled10a2002021-02-11 19:45:19 +01001504 if (params->src_addr) {
1505 conn_calculate_hash_sockaddr(params->src_addr,
1506 buf, &idx, &hash_flags,
1507 CONN_HASH_PARAMS_TYPE_SRC_ADDR,
1508 CONN_HASH_PARAMS_TYPE_SRC_PORT);
1509 }
1510
Amaury Denoyelle1921d202021-01-14 10:15:29 +01001511 if (params->proxy_prehash) {
1512 conn_hash_update(buf, &idx,
Amaury Denoyelle36441f42021-02-17 16:25:31 +01001513 &params->proxy_prehash, sizeof(params->proxy_prehash),
Amaury Denoyelle1921d202021-01-14 10:15:29 +01001514 &hash_flags, CONN_HASH_PARAMS_TYPE_PROXY);
1515 }
Amaury Denoyelle01a287f2021-02-11 16:46:53 +01001516
Amaury Denoyelle1921d202021-01-14 10:15:29 +01001517 hash = conn_hash_digest(buf, idx, hash_flags);
Amaury Denoyelle81c6f762021-01-18 14:57:50 +01001518 return hash;
1519}