blob: b3efea6f948ff1a329377e0cf1120168c9348c3c [file] [log] [blame]
Christopher Faulete6c3b692015-09-02 17:15:16 +02001/*
2 * Stream filters related variables and functions.
3 *
4 * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Christopher Fauletfcd99f82016-10-31 11:27:21 +010013#include <ctype.h>
14
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020016#include <haproxy/channel-t.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020017#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020018#include <haproxy/filters.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020019#include <haproxy/global.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020020#include <haproxy/http_ana-t.h>
Willy Tarreau87735332020-06-04 09:08:41 +020021#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020022#include <haproxy/htx.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020023#include <haproxy/proxy-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020024#include <haproxy/stream.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020025#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020026#include <haproxy/tools.h>
Christopher Faulete6c3b692015-09-02 17:15:16 +020027
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010028const char *trace_flt_id = "trace filter";
29
Christopher Faulete6c3b692015-09-02 17:15:16 +020030struct flt_ops trace_ops;
31
Christopher Faulet47d9a4e2020-11-17 11:33:36 +010032#define TRACE_F_QUIET 0x00000001
33#define TRACE_F_RAND_FWD 0x00000002
34#define TRACE_F_HEXDUMP 0x00000004
35
Christopher Faulete6c3b692015-09-02 17:15:16 +020036struct trace_config {
37 struct proxy *proxy;
38 char *name;
Christopher Faulet47d9a4e2020-11-17 11:33:36 +010039 unsigned int flags;
Christopher Faulete6c3b692015-09-02 17:15:16 +020040};
41
Christopher Fauleta3ed2712019-11-04 11:35:42 +010042#define FLT_TRACE(conf, fmt, ...) \
Christopher Faulet96a577a2020-11-17 10:45:05 +010043 do { \
Christopher Faulet47d9a4e2020-11-17 11:33:36 +010044 if (!(conf->flags & TRACE_F_QUIET)) \
Christopher Faulet96a577a2020-11-17 10:45:05 +010045 fprintf(stderr, "%d.%06d [%-20s] " fmt "\n", \
46 (int)now.tv_sec, (int)now.tv_usec, (conf)->name,\
47 ##__VA_ARGS__); \
48 } while (0)
Christopher Faulete6c3b692015-09-02 17:15:16 +020049
Christopher Faulet96a577a2020-11-17 10:45:05 +010050#define FLT_STRM_TRACE(conf, strm, fmt, ...) \
51 do { \
Christopher Faulet47d9a4e2020-11-17 11:33:36 +010052 if (!(conf->flags & TRACE_F_QUIET)) \
Christopher Faulet96a577a2020-11-17 10:45:05 +010053 fprintf(stderr, "%d.%06d [%-20s] [strm %p(%x) 0x%08x 0x%08x] " fmt "\n", \
54 (int)now.tv_sec, (int)now.tv_usec, (conf)->name, \
55 strm, (strm ? ((struct stream *)strm)->uniq_id : ~0U), \
56 (strm ? strm->req.analysers : 0), (strm ? strm->res.analysers : 0), \
57 ##__VA_ARGS__); \
58 } while (0)
Christopher Faulete6c3b692015-09-02 17:15:16 +020059
60
61static const char *
62channel_label(const struct channel *chn)
63{
64 return (chn->flags & CF_ISRESP) ? "RESPONSE" : "REQUEST";
65}
66
67static const char *
68proxy_mode(const struct stream *s)
69{
70 struct proxy *px = (s->flags & SF_BE_ASSIGNED ? s->be : strm_fe(s));
71
Christopher Faulet386a0cd2019-07-15 21:22:44 +020072 return ((px->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Christopher Faulete6c3b692015-09-02 17:15:16 +020073}
74
75static const char *
76stream_pos(const struct stream *s)
77{
78 return (s->flags & SF_BE_ASSIGNED) ? "backend" : "frontend";
79}
80
Christopher Faulet31ed32d2016-06-21 11:42:37 +020081static const char *
82filter_type(const struct filter *f)
83{
84 return (f->flags & FLT_FL_IS_BACKEND_FILTER) ? "backend" : "frontend";
85}
86
Christopher Fauletfcd99f82016-10-31 11:27:21 +010087static void
Christopher Faulete0aa6f72018-11-30 22:23:32 +010088trace_hexdump(struct ist ist)
Christopher Fauletfcd99f82016-10-31 11:27:21 +010089{
Christopher Faulete0aa6f72018-11-30 22:23:32 +010090 int i, j, padding;
Christopher Fauletfcd99f82016-10-31 11:27:21 +010091
Christopher Faulete0aa6f72018-11-30 22:23:32 +010092 padding = ((ist.len % 16) ? (16 - ist.len % 16) : 0);
93 for (i = 0; i < ist.len + padding; i++) {
Christopher Fauletfcd99f82016-10-31 11:27:21 +010094 if (!(i % 16))
95 fprintf(stderr, "\t0x%06x: ", i);
96 else if (!(i % 8))
97 fprintf(stderr, " ");
98
Christopher Faulete0aa6f72018-11-30 22:23:32 +010099 if (i < ist.len)
100 fprintf(stderr, "%02x ", (unsigned char)*(ist.ptr+i));
Christopher Fauletfcd99f82016-10-31 11:27:21 +0100101 else
102 fprintf(stderr, " ");
103
104 /* print ASCII dump */
105 if (i % 16 == 15) {
106 fprintf(stderr, " |");
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100107 for(j = i - 15; j <= i && j < ist.len; j++)
Willy Tarreau90807112020-02-25 08:16:33 +0100108 fprintf(stderr, "%c", (isprint((unsigned char)*(ist.ptr+j)) ? *(ist.ptr+j) : '.'));
Christopher Fauletfcd99f82016-10-31 11:27:21 +0100109 fprintf(stderr, "|\n");
110 }
111 }
112}
113
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100114static void
Christopher Fauletb2e58492019-11-12 11:13:01 +0100115trace_raw_hexdump(struct buffer *buf, unsigned int offset, unsigned int len)
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100116{
117 unsigned char p[len];
118 int block1, block2;
119
120 block1 = len;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100121 if (block1 > b_contig_data(buf, offset))
122 block1 = b_contig_data(buf, offset);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100123 block2 = len - block1;
124
Christopher Fauletb2e58492019-11-12 11:13:01 +0100125 memcpy(p, b_peek(buf, offset), block1);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100126 memcpy(p+block1, b_orig(buf), block2);
127 trace_hexdump(ist2(p, len));
128}
129
130static void
131trace_htx_hexdump(struct htx *htx, unsigned int offset, unsigned int len)
132{
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100133 struct htx_blk *blk;
134
Christopher Fauletee847d42019-05-23 11:55:33 +0200135 for (blk = htx_get_first_blk(htx); blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100136 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletee847d42019-05-23 11:55:33 +0200137 uint32_t sz = htx_get_blksz(blk);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100138 struct ist v;
139
Christopher Fauletee847d42019-05-23 11:55:33 +0200140 if (offset >= sz) {
141 offset -= sz;
142 continue;
143 }
144
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100145 v = htx_get_blk_value(htx, blk);
Tim Duesterhus154374c2021-03-02 18:57:27 +0100146 v = istadv(v, offset);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100147 offset = 0;
148
149 if (v.len > len)
150 v.len = len;
151 len -= v.len;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200152 if (type == HTX_BLK_DATA)
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100153 trace_hexdump(v);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100154 }
155}
156
Christopher Fauletb2e58492019-11-12 11:13:01 +0100157static unsigned int
158trace_get_htx_datalen(struct htx *htx, unsigned int offset, unsigned int len)
159{
160 struct htx_blk *blk;
Christopher Faulet24598a42020-02-26 22:06:11 +0100161 struct htx_ret htxret = htx_find_offset(htx, offset);
162 uint32_t data = 0;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100163
Christopher Faulet24598a42020-02-26 22:06:11 +0100164 blk = htxret.blk;
165 if (blk && htxret.ret && htx_get_blk_type(blk) == HTX_BLK_DATA) {
166 data += htxret.ret;
167 blk = htx_get_next_blk(htx, blk);
168 }
169 while (blk) {
170 if (htx_get_blk_type(blk) == HTX_BLK_UNUSED)
171 goto next;
172 else if (htx_get_blk_type(blk) != HTX_BLK_DATA)
Christopher Fauletb2e58492019-11-12 11:13:01 +0100173 break;
Christopher Faulet24598a42020-02-26 22:06:11 +0100174 data += htx_get_blksz(blk);
175 next:
176 blk = htx_get_next_blk(htx, blk);
Christopher Fauletb2e58492019-11-12 11:13:01 +0100177 }
178 return data;
179}
180
Christopher Faulete6c3b692015-09-02 17:15:16 +0200181/***************************************************************************
182 * Hooks that manage the filter lifecycle (init/check/deinit)
183 **************************************************************************/
184/* Initialize the filter. Returns -1 on error, else 0. */
185static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100186trace_init(struct proxy *px, struct flt_conf *fconf)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200187{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100188 struct trace_config *conf = fconf->conf;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200189
190 if (conf->name)
191 memprintf(&conf->name, "%s/%s", conf->name, px->id);
192 else
193 memprintf(&conf->name, "TRACE/%s", px->id);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100194
Christopher Faulet6e540952018-12-03 22:43:41 +0100195 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100196 fconf->conf = conf;
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100197
Christopher Faulet96a577a2020-11-17 10:45:05 +0100198 FLT_TRACE(conf, "filter initialized [quiet=%s - fwd random=%s - hexdump=%s]",
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100199 ((conf->flags & TRACE_F_QUIET) ? "true" : "false"),
200 ((conf->flags & TRACE_F_RAND_FWD) ? "true" : "false"),
201 ((conf->flags & TRACE_F_HEXDUMP) ? "true" : "false"));
Christopher Faulete6c3b692015-09-02 17:15:16 +0200202 return 0;
203}
204
Ilya Shipitsin6b79f382020-07-23 00:32:55 +0500205/* Free resources allocated by the trace filter. */
Christopher Faulete6c3b692015-09-02 17:15:16 +0200206static void
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100207trace_deinit(struct proxy *px, struct flt_conf *fconf)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200208{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100209 struct trace_config *conf = fconf->conf;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200210
211 if (conf) {
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100212 FLT_TRACE(conf, "filter deinitialized");
Christopher Faulete6c3b692015-09-02 17:15:16 +0200213 free(conf->name);
214 free(conf);
215 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100216 fconf->conf = NULL;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200217}
218
219/* Check configuration of a trace filter for a specified proxy.
220 * Return 1 on error, else 0. */
221static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100222trace_check(struct proxy *px, struct flt_conf *fconf)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200223{
224 return 0;
225}
226
Christopher Fauletf2273722017-07-27 16:58:42 +0200227/* Initialize the filter for each thread. Return -1 on error, else 0. */
228static int
229trace_init_per_thread(struct proxy *px, struct flt_conf *fconf)
230{
231 struct trace_config *conf = fconf->conf;
232
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100233 FLT_TRACE(conf, "filter initialized for thread tid %u", tid);
Christopher Fauletf2273722017-07-27 16:58:42 +0200234 return 0;
235}
236
Ilya Shipitsin6b79f382020-07-23 00:32:55 +0500237/* Free resources allocate by the trace filter for each thread. */
Christopher Fauletf2273722017-07-27 16:58:42 +0200238static void
239trace_deinit_per_thread(struct proxy *px, struct flt_conf *fconf)
240{
241 struct trace_config *conf = fconf->conf;
242
243 if (conf)
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100244 FLT_TRACE(conf, "filter deinitialized for thread tid %u", tid);
Christopher Fauletf2273722017-07-27 16:58:42 +0200245}
246
Christopher Faulete6c3b692015-09-02 17:15:16 +0200247/**************************************************************************
248 * Hooks to handle start/stop of streams
249 *************************************************************************/
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200250/* Called when a filter instance is created and attach to a stream */
251static int
252trace_attach(struct stream *s, struct filter *filter)
253{
254 struct trace_config *conf = FLT_CONF(filter);
255
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100256 FLT_STRM_TRACE(conf, s, "%-25s: filter-type=%s",
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200257 __FUNCTION__, filter_type(filter));
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100258
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200259 return 1;
260}
261
262/* Called when a filter instance is detach from a stream, just before its
263 * destruction */
264static void
265trace_detach(struct stream *s, struct filter *filter)
266{
267 struct trace_config *conf = FLT_CONF(filter);
268
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100269 FLT_STRM_TRACE(conf, s, "%-25s: filter-type=%s",
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200270 __FUNCTION__, filter_type(filter));
271}
272
Christopher Faulete6c3b692015-09-02 17:15:16 +0200273/* Called when a stream is created */
274static int
275trace_stream_start(struct stream *s, struct filter *filter)
276{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100277 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200278
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100279 FLT_STRM_TRACE(conf, s, "%-25s",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200280 __FUNCTION__);
281 return 0;
282}
283
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200284
285/* Called when a backend is set for a stream */
286static int
287trace_stream_set_backend(struct stream *s, struct filter *filter,
288 struct proxy *be)
289{
290 struct trace_config *conf = FLT_CONF(filter);
291
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100292 FLT_STRM_TRACE(conf, s, "%-25s: backend=%s",
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200293 __FUNCTION__, be->id);
294 return 0;
295}
296
Christopher Faulete6c3b692015-09-02 17:15:16 +0200297/* Called when a stream is destroyed */
298static void
299trace_stream_stop(struct stream *s, struct filter *filter)
300{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100301 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200302
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100303 FLT_STRM_TRACE(conf, s, "%-25s",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200304 __FUNCTION__);
305}
306
Christopher Fauleta00d8172016-11-10 14:58:05 +0100307/* Called when the stream is woken up because of an expired timer */
308static void
309trace_check_timeouts(struct stream *s, struct filter *filter)
310{
311 struct trace_config *conf = FLT_CONF(filter);
312
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100313 FLT_STRM_TRACE(conf, s, "%-25s",
Christopher Fauleta00d8172016-11-10 14:58:05 +0100314 __FUNCTION__);
315}
316
Christopher Faulete6c3b692015-09-02 17:15:16 +0200317/**************************************************************************
318 * Hooks to handle channels activity
319 *************************************************************************/
320/* Called when analyze starts for a given channel */
321static int
322trace_chn_start_analyze(struct stream *s, struct filter *filter,
323 struct channel *chn)
324{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100325 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200326
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100327 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200328 __FUNCTION__,
329 channel_label(chn), proxy_mode(s), stream_pos(s));
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200330 filter->pre_analyzers |= (AN_REQ_ALL | AN_RES_ALL);
331 filter->post_analyzers |= (AN_REQ_ALL | AN_RES_ALL);
Christopher Fauletfcd99f82016-10-31 11:27:21 +0100332 register_data_filter(s, chn, filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200333 return 1;
334}
335
336/* Called before a processing happens on a given channel */
337static int
338trace_chn_analyze(struct stream *s, struct filter *filter,
339 struct channel *chn, unsigned an_bit)
340{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100341 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200342 char *ana;
343
344 switch (an_bit) {
345 case AN_REQ_INSPECT_FE:
346 ana = "AN_REQ_INSPECT_FE";
347 break;
348 case AN_REQ_WAIT_HTTP:
349 ana = "AN_REQ_WAIT_HTTP";
350 break;
351 case AN_REQ_HTTP_BODY:
352 ana = "AN_REQ_HTTP_BODY";
353 break;
354 case AN_REQ_HTTP_PROCESS_FE:
355 ana = "AN_REQ_HTTP_PROCESS_FE";
356 break;
357 case AN_REQ_SWITCHING_RULES:
358 ana = "AN_REQ_SWITCHING_RULES";
359 break;
360 case AN_REQ_INSPECT_BE:
361 ana = "AN_REQ_INSPECT_BE";
362 break;
363 case AN_REQ_HTTP_PROCESS_BE:
364 ana = "AN_REQ_HTTP_PROCESS_BE";
365 break;
366 case AN_REQ_SRV_RULES:
367 ana = "AN_REQ_SRV_RULES";
368 break;
369 case AN_REQ_HTTP_INNER:
370 ana = "AN_REQ_HTTP_INNER";
371 break;
372 case AN_REQ_HTTP_TARPIT:
373 ana = "AN_REQ_HTTP_TARPIT";
374 break;
375 case AN_REQ_STICKING_RULES:
376 ana = "AN_REQ_STICKING_RULES";
377 break;
378 case AN_REQ_PRST_RDP_COOKIE:
379 ana = "AN_REQ_PRST_RDP_COOKIE";
380 break;
381 case AN_REQ_HTTP_XFER_BODY:
382 ana = "AN_REQ_HTTP_XFER_BODY";
383 break;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200384 case AN_RES_INSPECT:
385 ana = "AN_RES_INSPECT";
386 break;
387 case AN_RES_WAIT_HTTP:
388 ana = "AN_RES_WAIT_HTTP";
389 break;
390 case AN_RES_HTTP_PROCESS_FE: // AN_RES_HTTP_PROCESS_BE
391 ana = "AN_RES_HTTP_PROCESS_FE/BE";
392 break;
393 case AN_RES_STORE_RULES:
394 ana = "AN_RES_STORE_RULES";
395 break;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200396 case AN_RES_HTTP_XFER_BODY:
397 ana = "AN_RES_HTTP_XFER_BODY";
398 break;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200399 default:
400 ana = "unknown";
401 }
402
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100403 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200404 "analyzer=%s - step=%s",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200405 __FUNCTION__,
406 channel_label(chn), proxy_mode(s), stream_pos(s),
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200407 ana, ((chn->analysers & an_bit) ? "PRE" : "POST"));
Christopher Faulete6c3b692015-09-02 17:15:16 +0200408 return 1;
409}
410
411/* Called when analyze ends for a given channel */
412static int
413trace_chn_end_analyze(struct stream *s, struct filter *filter,
414 struct channel *chn)
415{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100416 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200417
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100418 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200419 __FUNCTION__,
420 channel_label(chn), proxy_mode(s), stream_pos(s));
421 return 1;
422}
423
424/**************************************************************************
425 * Hooks to filter HTTP messages
426 *************************************************************************/
427static int
Christopher Faulet1339d742016-05-11 16:48:33 +0200428trace_http_headers(struct stream *s, struct filter *filter,
429 struct http_msg *msg)
430{
431 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200432 struct htx *htx = htxbuf(&msg->chn->buf);
433 struct htx_sl *sl = http_get_stline(htx);
434 int32_t pos;
Christopher Faulet1339d742016-05-11 16:48:33 +0200435
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100436 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)\t%.*s %.*s %.*s",
Christopher Faulet1339d742016-05-11 16:48:33 +0200437 __FUNCTION__,
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200438 channel_label(msg->chn), proxy_mode(s), stream_pos(s),
439 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
440 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
441 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100442
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200443 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
444 struct htx_blk *blk = htx_get_blk(htx, pos);
445 enum htx_blk_type type = htx_get_blk_type(blk);
446 struct ist n, v;
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100447
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200448 if (type == HTX_BLK_EOH)
449 break;
450 if (type != HTX_BLK_HDR)
451 continue;
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100452
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200453 n = htx_get_blk_name(htx, blk);
454 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100455 FLT_STRM_TRACE(conf, s, "\t%.*s: %.*s",
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200456 (int)n.len, n.ptr, (int)v.len, v.ptr);
Christopher Faulet1339d742016-05-11 16:48:33 +0200457 }
Christopher Faulet1339d742016-05-11 16:48:33 +0200458 return 1;
459}
460
461static int
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100462trace_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
463 unsigned int offset, unsigned int len)
464{
465 struct trace_config *conf = FLT_CONF(filter);
466 int ret = len;
467
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100468 if (ret && (conf->flags & TRACE_F_RAND_FWD)) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100469 unsigned int data = trace_get_htx_datalen(htxbuf(&msg->chn->buf), offset, len);
Christopher Faulet0bdeeaa2019-06-04 22:09:53 +0200470
Christopher Fauletb2e58492019-11-12 11:13:01 +0100471 if (data) {
Willy Tarreau52bf8392020-03-08 00:42:37 +0100472 ret = ha_random() % (ret+1);
Christopher Faulet647fe1d2019-06-12 16:07:48 +0200473 if (!ret || ret >= data)
474 ret = len;
475 }
Christopher Faulet0bdeeaa2019-06-04 22:09:53 +0200476 }
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100477
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100478 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100479 "offset=%u - len=%u - forward=%d",
480 __FUNCTION__,
481 channel_label(msg->chn), proxy_mode(s), stream_pos(s),
482 offset, len, ret);
483
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100484 if (conf->flags & TRACE_F_HEXDUMP)
Christopher Fauletb2e58492019-11-12 11:13:01 +0100485 trace_htx_hexdump(htxbuf(&msg->chn->buf), offset, ret);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100486
487 if (ret != len)
488 task_wakeup(s->task, TASK_WOKEN_MSG);
489 return ret;
490}
491
492static int
Christopher Faulete6c3b692015-09-02 17:15:16 +0200493trace_http_end(struct stream *s, struct filter *filter,
494 struct http_msg *msg)
495{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100496 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200497
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100498 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200499 __FUNCTION__,
500 channel_label(msg->chn), proxy_mode(s), stream_pos(s));
501 return 1;
502}
503
504static void
505trace_http_reset(struct stream *s, struct filter *filter,
506 struct http_msg *msg)
507{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100508 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200509
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100510 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200511 __FUNCTION__,
512 channel_label(msg->chn), proxy_mode(s), stream_pos(s));
513}
514
515static void
516trace_http_reply(struct stream *s, struct filter *filter, short status,
Willy Tarreau83061a82018-07-13 11:56:34 +0200517 const struct buffer *msg)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200518{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100519 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200520
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100521 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200522 __FUNCTION__, "-", proxy_mode(s), stream_pos(s));
523}
524
Christopher Faulete6c3b692015-09-02 17:15:16 +0200525/**************************************************************************
526 * Hooks to filter TCP data
527 *************************************************************************/
528static int
Christopher Fauletb2e58492019-11-12 11:13:01 +0100529trace_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn,
530 unsigned int offset, unsigned int len)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200531{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100532 struct trace_config *conf = FLT_CONF(filter);
Christopher Fauletb2e58492019-11-12 11:13:01 +0100533 int ret = len;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200534
Christopher Fauletb2e58492019-11-12 11:13:01 +0100535 if (s->flags & SF_HTX) {
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100536 if (ret && (conf->flags & TRACE_F_RAND_FWD)) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100537 unsigned int data = trace_get_htx_datalen(htxbuf(&chn->buf), offset, len);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200538
Christopher Fauletb2e58492019-11-12 11:13:01 +0100539 if (data) {
Willy Tarreau52bf8392020-03-08 00:42:37 +0100540 ret = ha_random() % (ret+1);
Christopher Fauletb2e58492019-11-12 11:13:01 +0100541 if (!ret || ret >= data)
542 ret = len;
543 }
544 }
Christopher Faulete6c3b692015-09-02 17:15:16 +0200545
Christopher Fauletb2e58492019-11-12 11:13:01 +0100546 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
547 "offset=%u - len=%u - forward=%d",
548 __FUNCTION__,
549 channel_label(chn), proxy_mode(s), stream_pos(s),
550 offset, len, ret);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200551
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100552 if (conf->flags & TRACE_F_HEXDUMP)
Christopher Fauletb2e58492019-11-12 11:13:01 +0100553 trace_htx_hexdump(htxbuf(&chn->buf), offset, ret);
554 }
555 else {
Christopher Faulete6c3b692015-09-02 17:15:16 +0200556
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100557 if (ret && (conf->flags & TRACE_F_RAND_FWD))
Willy Tarreau52bf8392020-03-08 00:42:37 +0100558 ret = ha_random() % (ret+1);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200559
Christopher Fauletb2e58492019-11-12 11:13:01 +0100560 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
561 "offset=%u - len=%u - forward=%d",
562 __FUNCTION__,
563 channel_label(chn), proxy_mode(s), stream_pos(s),
564 offset, len, ret);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200565
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100566 if (conf->flags & TRACE_F_HEXDUMP)
Christopher Fauletb2e58492019-11-12 11:13:01 +0100567 trace_raw_hexdump(&chn->buf, offset, ret);
Christopher Fauletfcd99f82016-10-31 11:27:21 +0100568 }
569
Christopher Fauletb2e58492019-11-12 11:13:01 +0100570 if (ret != len)
571 task_wakeup(s->task, TASK_WOKEN_MSG);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200572 return ret;
573}
Christopher Faulete6c3b692015-09-02 17:15:16 +0200574/********************************************************************
575 * Functions that manage the filter initialization
576 ********************************************************************/
577struct flt_ops trace_ops = {
578 /* Manage trace filter, called for each filter declaration */
Christopher Fauletf2273722017-07-27 16:58:42 +0200579 .init = trace_init,
580 .deinit = trace_deinit,
581 .check = trace_check,
582 .init_per_thread = trace_init_per_thread,
583 .deinit_per_thread = trace_deinit_per_thread,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200584
585 /* Handle start/stop of streams */
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200586 .attach = trace_attach,
587 .detach = trace_detach,
588 .stream_start = trace_stream_start,
589 .stream_set_backend = trace_stream_set_backend,
590 .stream_stop = trace_stream_stop,
Christopher Fauleta00d8172016-11-10 14:58:05 +0100591 .check_timeouts = trace_check_timeouts,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200592
593 /* Handle channels activity */
594 .channel_start_analyze = trace_chn_start_analyze,
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200595 .channel_pre_analyze = trace_chn_analyze,
596 .channel_post_analyze = trace_chn_analyze,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200597 .channel_end_analyze = trace_chn_end_analyze,
598
599 /* Filter HTTP requests and responses */
Christopher Faulet1339d742016-05-11 16:48:33 +0200600 .http_headers = trace_http_headers,
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100601 .http_payload = trace_http_payload,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200602 .http_end = trace_http_end,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200603 .http_reset = trace_http_reset,
604 .http_reply = trace_http_reply,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200605
606 /* Filter TCP data */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100607 .tcp_payload = trace_tcp_payload,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200608};
609
610/* Return -1 on error, else 0 */
611static int
612parse_trace_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +0200613 struct flt_conf *fconf, char **err, void *private)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200614{
615 struct trace_config *conf;
616 int pos = *cur_arg;
617
618 conf = calloc(1, sizeof(*conf));
619 if (!conf) {
620 memprintf(err, "%s: out of memory", args[*cur_arg]);
621 return -1;
622 }
623 conf->proxy = px;
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100624 conf->flags = 0;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100625 if (strcmp(args[pos], "trace") == 0) {
Christopher Faulete6c3b692015-09-02 17:15:16 +0200626 pos++;
627
628 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100629 if (strcmp(args[pos], "name") == 0) {
Christopher Faulete6c3b692015-09-02 17:15:16 +0200630 if (!*args[pos + 1]) {
631 memprintf(err, "'%s' : '%s' option without value",
632 args[*cur_arg], args[pos]);
633 goto error;
634 }
635 conf->name = strdup(args[pos + 1]);
636 if (!conf->name) {
637 memprintf(err, "%s: out of memory", args[*cur_arg]);
638 goto error;
639 }
640 pos++;
641 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100642 else if (strcmp(args[pos], "quiet") == 0)
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100643 conf->flags |= TRACE_F_QUIET;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100644 else if (strcmp(args[pos], "random-parsing") == 0)
Dragan Dosen73389822021-09-20 09:29:19 +0200645 ; // ignore
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100646 else if (strcmp(args[pos], "random-forwarding") == 0)
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100647 conf->flags |= TRACE_F_RAND_FWD;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100648 else if (strcmp(args[pos], "hexdump") == 0)
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100649 conf->flags |= TRACE_F_HEXDUMP;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200650 else
651 break;
652 pos++;
653 }
654 *cur_arg = pos;
Christopher Fauletf4a4ef72018-12-07 17:39:53 +0100655 fconf->id = trace_flt_id;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100656 fconf->ops = &trace_ops;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200657 }
658
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100659 fconf->conf = conf;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200660 return 0;
661
662 error:
663 if (conf->name)
664 free(conf->name);
665 free(conf);
666 return -1;
667}
668
669/* Declare the filter parser for "trace" keyword */
670static struct flt_kw_list flt_kws = { "TRACE", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +0200671 { "trace", parse_trace_flt, NULL },
672 { NULL, NULL, NULL },
Christopher Faulete6c3b692015-09-02 17:15:16 +0200673 }
674};
675
Willy Tarreau0108d902018-11-25 19:14:37 +0100676INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);