blob: bbadfe24cdd23eb9fb7bf7cdd510e069d82a61ab [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", \
Willy Tarreaubc3c4e82023-04-27 18:22:34 +020046 (int)date.tv_sec, (int)date.tv_usec, (conf)->name,\
Christopher Faulet96a577a2020-11-17 10:45:05 +010047 ##__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", \
Willy Tarreaubc3c4e82023-04-27 18:22:34 +020054 (int)date.tv_sec, (int)date.tv_usec, (conf)->name, \
Christopher Faulet96a577a2020-11-17 10:45:05 +010055 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
Tim Duesterhus2471f5c2021-11-08 09:05:01 +0100149 v = isttrim(v, len);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100150 len -= v.len;
Christopher Faulet2d7c5392019-06-03 10:41:26 +0200151 if (type == HTX_BLK_DATA)
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100152 trace_hexdump(v);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100153 }
154}
155
Christopher Fauletb2e58492019-11-12 11:13:01 +0100156static unsigned int
157trace_get_htx_datalen(struct htx *htx, unsigned int offset, unsigned int len)
158{
159 struct htx_blk *blk;
Christopher Faulet24598a42020-02-26 22:06:11 +0100160 struct htx_ret htxret = htx_find_offset(htx, offset);
161 uint32_t data = 0;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100162
Christopher Faulet24598a42020-02-26 22:06:11 +0100163 blk = htxret.blk;
164 if (blk && htxret.ret && htx_get_blk_type(blk) == HTX_BLK_DATA) {
165 data += htxret.ret;
166 blk = htx_get_next_blk(htx, blk);
167 }
168 while (blk) {
169 if (htx_get_blk_type(blk) == HTX_BLK_UNUSED)
170 goto next;
171 else if (htx_get_blk_type(blk) != HTX_BLK_DATA)
Christopher Fauletb2e58492019-11-12 11:13:01 +0100172 break;
Christopher Faulet24598a42020-02-26 22:06:11 +0100173 data += htx_get_blksz(blk);
174 next:
175 blk = htx_get_next_blk(htx, blk);
Christopher Fauletb2e58492019-11-12 11:13:01 +0100176 }
177 return data;
178}
179
Christopher Faulete6c3b692015-09-02 17:15:16 +0200180/***************************************************************************
181 * Hooks that manage the filter lifecycle (init/check/deinit)
182 **************************************************************************/
183/* Initialize the filter. Returns -1 on error, else 0. */
184static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100185trace_init(struct proxy *px, struct flt_conf *fconf)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200186{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100187 struct trace_config *conf = fconf->conf;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200188
189 if (conf->name)
190 memprintf(&conf->name, "%s/%s", conf->name, px->id);
191 else
192 memprintf(&conf->name, "TRACE/%s", px->id);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100193
Christopher Faulet6e540952018-12-03 22:43:41 +0100194 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100195 fconf->conf = conf;
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100196
Christopher Faulet96a577a2020-11-17 10:45:05 +0100197 FLT_TRACE(conf, "filter initialized [quiet=%s - fwd random=%s - hexdump=%s]",
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100198 ((conf->flags & TRACE_F_QUIET) ? "true" : "false"),
199 ((conf->flags & TRACE_F_RAND_FWD) ? "true" : "false"),
200 ((conf->flags & TRACE_F_HEXDUMP) ? "true" : "false"));
Christopher Faulete6c3b692015-09-02 17:15:16 +0200201 return 0;
202}
203
Ilya Shipitsin6b79f382020-07-23 00:32:55 +0500204/* Free resources allocated by the trace filter. */
Christopher Faulete6c3b692015-09-02 17:15:16 +0200205static void
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100206trace_deinit(struct proxy *px, struct flt_conf *fconf)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200207{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100208 struct trace_config *conf = fconf->conf;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200209
210 if (conf) {
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100211 FLT_TRACE(conf, "filter deinitialized");
Christopher Faulete6c3b692015-09-02 17:15:16 +0200212 free(conf->name);
213 free(conf);
214 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100215 fconf->conf = NULL;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200216}
217
218/* Check configuration of a trace filter for a specified proxy.
219 * Return 1 on error, else 0. */
220static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100221trace_check(struct proxy *px, struct flt_conf *fconf)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200222{
223 return 0;
224}
225
Christopher Fauletf2273722017-07-27 16:58:42 +0200226/* Initialize the filter for each thread. Return -1 on error, else 0. */
227static int
228trace_init_per_thread(struct proxy *px, struct flt_conf *fconf)
229{
230 struct trace_config *conf = fconf->conf;
231
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100232 FLT_TRACE(conf, "filter initialized for thread tid %u", tid);
Christopher Fauletf2273722017-07-27 16:58:42 +0200233 return 0;
234}
235
Ilya Shipitsin6b79f382020-07-23 00:32:55 +0500236/* Free resources allocate by the trace filter for each thread. */
Christopher Fauletf2273722017-07-27 16:58:42 +0200237static void
238trace_deinit_per_thread(struct proxy *px, struct flt_conf *fconf)
239{
240 struct trace_config *conf = fconf->conf;
241
242 if (conf)
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100243 FLT_TRACE(conf, "filter deinitialized for thread tid %u", tid);
Christopher Fauletf2273722017-07-27 16:58:42 +0200244}
245
Christopher Faulete6c3b692015-09-02 17:15:16 +0200246/**************************************************************************
247 * Hooks to handle start/stop of streams
248 *************************************************************************/
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200249/* Called when a filter instance is created and attach to a stream */
250static int
251trace_attach(struct stream *s, struct filter *filter)
252{
253 struct trace_config *conf = FLT_CONF(filter);
254
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100255 FLT_STRM_TRACE(conf, s, "%-25s: filter-type=%s",
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200256 __FUNCTION__, filter_type(filter));
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100257
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200258 return 1;
259}
260
261/* Called when a filter instance is detach from a stream, just before its
262 * destruction */
263static void
264trace_detach(struct stream *s, struct filter *filter)
265{
266 struct trace_config *conf = FLT_CONF(filter);
267
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100268 FLT_STRM_TRACE(conf, s, "%-25s: filter-type=%s",
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200269 __FUNCTION__, filter_type(filter));
270}
271
Christopher Faulete6c3b692015-09-02 17:15:16 +0200272/* Called when a stream is created */
273static int
274trace_stream_start(struct stream *s, struct filter *filter)
275{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100276 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200277
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100278 FLT_STRM_TRACE(conf, s, "%-25s",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200279 __FUNCTION__);
280 return 0;
281}
282
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200283
284/* Called when a backend is set for a stream */
285static int
286trace_stream_set_backend(struct stream *s, struct filter *filter,
287 struct proxy *be)
288{
289 struct trace_config *conf = FLT_CONF(filter);
290
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100291 FLT_STRM_TRACE(conf, s, "%-25s: backend=%s",
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200292 __FUNCTION__, be->id);
293 return 0;
294}
295
Christopher Faulete6c3b692015-09-02 17:15:16 +0200296/* Called when a stream is destroyed */
297static void
298trace_stream_stop(struct stream *s, struct filter *filter)
299{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100300 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200301
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100302 FLT_STRM_TRACE(conf, s, "%-25s",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200303 __FUNCTION__);
304}
305
Christopher Fauleta00d8172016-11-10 14:58:05 +0100306/* Called when the stream is woken up because of an expired timer */
307static void
308trace_check_timeouts(struct stream *s, struct filter *filter)
309{
310 struct trace_config *conf = FLT_CONF(filter);
311
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100312 FLT_STRM_TRACE(conf, s, "%-25s",
Christopher Fauleta00d8172016-11-10 14:58:05 +0100313 __FUNCTION__);
314}
315
Christopher Faulete6c3b692015-09-02 17:15:16 +0200316/**************************************************************************
317 * Hooks to handle channels activity
318 *************************************************************************/
319/* Called when analyze starts for a given channel */
320static int
321trace_chn_start_analyze(struct stream *s, struct filter *filter,
322 struct channel *chn)
323{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100324 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200325
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100326 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200327 __FUNCTION__,
328 channel_label(chn), proxy_mode(s), stream_pos(s));
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200329 filter->pre_analyzers |= (AN_REQ_ALL | AN_RES_ALL);
330 filter->post_analyzers |= (AN_REQ_ALL | AN_RES_ALL);
Christopher Fauletfcd99f82016-10-31 11:27:21 +0100331 register_data_filter(s, chn, filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200332 return 1;
333}
334
335/* Called before a processing happens on a given channel */
336static int
337trace_chn_analyze(struct stream *s, struct filter *filter,
338 struct channel *chn, unsigned an_bit)
339{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100340 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200341 char *ana;
342
343 switch (an_bit) {
344 case AN_REQ_INSPECT_FE:
345 ana = "AN_REQ_INSPECT_FE";
346 break;
347 case AN_REQ_WAIT_HTTP:
348 ana = "AN_REQ_WAIT_HTTP";
349 break;
350 case AN_REQ_HTTP_BODY:
351 ana = "AN_REQ_HTTP_BODY";
352 break;
353 case AN_REQ_HTTP_PROCESS_FE:
354 ana = "AN_REQ_HTTP_PROCESS_FE";
355 break;
356 case AN_REQ_SWITCHING_RULES:
357 ana = "AN_REQ_SWITCHING_RULES";
358 break;
359 case AN_REQ_INSPECT_BE:
360 ana = "AN_REQ_INSPECT_BE";
361 break;
362 case AN_REQ_HTTP_PROCESS_BE:
363 ana = "AN_REQ_HTTP_PROCESS_BE";
364 break;
365 case AN_REQ_SRV_RULES:
366 ana = "AN_REQ_SRV_RULES";
367 break;
368 case AN_REQ_HTTP_INNER:
369 ana = "AN_REQ_HTTP_INNER";
370 break;
371 case AN_REQ_HTTP_TARPIT:
372 ana = "AN_REQ_HTTP_TARPIT";
373 break;
374 case AN_REQ_STICKING_RULES:
375 ana = "AN_REQ_STICKING_RULES";
376 break;
377 case AN_REQ_PRST_RDP_COOKIE:
378 ana = "AN_REQ_PRST_RDP_COOKIE";
379 break;
380 case AN_REQ_HTTP_XFER_BODY:
381 ana = "AN_REQ_HTTP_XFER_BODY";
382 break;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200383 case AN_RES_INSPECT:
384 ana = "AN_RES_INSPECT";
385 break;
386 case AN_RES_WAIT_HTTP:
387 ana = "AN_RES_WAIT_HTTP";
388 break;
389 case AN_RES_HTTP_PROCESS_FE: // AN_RES_HTTP_PROCESS_BE
390 ana = "AN_RES_HTTP_PROCESS_FE/BE";
391 break;
392 case AN_RES_STORE_RULES:
393 ana = "AN_RES_STORE_RULES";
394 break;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200395 case AN_RES_HTTP_XFER_BODY:
396 ana = "AN_RES_HTTP_XFER_BODY";
397 break;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200398 default:
399 ana = "unknown";
400 }
401
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100402 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200403 "analyzer=%s - step=%s",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200404 __FUNCTION__,
405 channel_label(chn), proxy_mode(s), stream_pos(s),
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200406 ana, ((chn->analysers & an_bit) ? "PRE" : "POST"));
Christopher Faulete6c3b692015-09-02 17:15:16 +0200407 return 1;
408}
409
410/* Called when analyze ends for a given channel */
411static int
412trace_chn_end_analyze(struct stream *s, struct filter *filter,
413 struct channel *chn)
414{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100415 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200416
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100417 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200418 __FUNCTION__,
419 channel_label(chn), proxy_mode(s), stream_pos(s));
420 return 1;
421}
422
423/**************************************************************************
424 * Hooks to filter HTTP messages
425 *************************************************************************/
426static int
Christopher Faulet1339d742016-05-11 16:48:33 +0200427trace_http_headers(struct stream *s, struct filter *filter,
428 struct http_msg *msg)
429{
430 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200431 struct htx *htx = htxbuf(&msg->chn->buf);
432 struct htx_sl *sl = http_get_stline(htx);
433 int32_t pos;
Christopher Faulet1339d742016-05-11 16:48:33 +0200434
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100435 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)\t%.*s %.*s %.*s",
Christopher Faulet1339d742016-05-11 16:48:33 +0200436 __FUNCTION__,
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200437 channel_label(msg->chn), proxy_mode(s), stream_pos(s),
438 HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl),
439 HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl),
440 HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl));
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100441
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200442 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
443 struct htx_blk *blk = htx_get_blk(htx, pos);
444 enum htx_blk_type type = htx_get_blk_type(blk);
445 struct ist n, v;
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100446
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200447 if (type == HTX_BLK_EOH)
448 break;
449 if (type != HTX_BLK_HDR)
450 continue;
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100451
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200452 n = htx_get_blk_name(htx, blk);
453 v = htx_get_blk_value(htx, blk);
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100454 FLT_STRM_TRACE(conf, s, "\t%.*s: %.*s",
Christopher Faulet386a0cd2019-07-15 21:22:44 +0200455 (int)n.len, n.ptr, (int)v.len, v.ptr);
Christopher Faulet1339d742016-05-11 16:48:33 +0200456 }
Christopher Faulet1339d742016-05-11 16:48:33 +0200457 return 1;
458}
459
460static int
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100461trace_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg,
462 unsigned int offset, unsigned int len)
463{
464 struct trace_config *conf = FLT_CONF(filter);
465 int ret = len;
466
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100467 if (ret && (conf->flags & TRACE_F_RAND_FWD)) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100468 unsigned int data = trace_get_htx_datalen(htxbuf(&msg->chn->buf), offset, len);
Christopher Faulet0bdeeaa2019-06-04 22:09:53 +0200469
Christopher Fauletb2e58492019-11-12 11:13:01 +0100470 if (data) {
Willy Tarreau52bf8392020-03-08 00:42:37 +0100471 ret = ha_random() % (ret+1);
Christopher Faulet647fe1d2019-06-12 16:07:48 +0200472 if (!ret || ret >= data)
473 ret = len;
474 }
Christopher Faulet0bdeeaa2019-06-04 22:09:53 +0200475 }
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100476
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100477 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100478 "offset=%u - len=%u - forward=%d",
479 __FUNCTION__,
480 channel_label(msg->chn), proxy_mode(s), stream_pos(s),
481 offset, len, ret);
482
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100483 if (conf->flags & TRACE_F_HEXDUMP)
Christopher Fauletb2e58492019-11-12 11:13:01 +0100484 trace_htx_hexdump(htxbuf(&msg->chn->buf), offset, ret);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100485
486 if (ret != len)
487 task_wakeup(s->task, TASK_WOKEN_MSG);
488 return ret;
489}
490
491static int
Christopher Faulete6c3b692015-09-02 17:15:16 +0200492trace_http_end(struct stream *s, struct filter *filter,
493 struct http_msg *msg)
494{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100495 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200496
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100497 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200498 __FUNCTION__,
499 channel_label(msg->chn), proxy_mode(s), stream_pos(s));
500 return 1;
501}
502
503static void
504trace_http_reset(struct stream *s, struct filter *filter,
505 struct http_msg *msg)
506{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100507 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200508
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100509 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200510 __FUNCTION__,
511 channel_label(msg->chn), proxy_mode(s), stream_pos(s));
512}
513
514static void
515trace_http_reply(struct stream *s, struct filter *filter, short status,
Willy Tarreau83061a82018-07-13 11:56:34 +0200516 const struct buffer *msg)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200517{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100518 struct trace_config *conf = FLT_CONF(filter);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200519
Christopher Fauleta3ed2712019-11-04 11:35:42 +0100520 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200521 __FUNCTION__, "-", proxy_mode(s), stream_pos(s));
522}
523
Christopher Faulete6c3b692015-09-02 17:15:16 +0200524/**************************************************************************
525 * Hooks to filter TCP data
526 *************************************************************************/
527static int
Christopher Fauletb2e58492019-11-12 11:13:01 +0100528trace_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn,
529 unsigned int offset, unsigned int len)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200530{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100531 struct trace_config *conf = FLT_CONF(filter);
Christopher Fauletb2e58492019-11-12 11:13:01 +0100532 int ret = len;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200533
Christopher Fauletb2e58492019-11-12 11:13:01 +0100534 if (s->flags & SF_HTX) {
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100535 if (ret && (conf->flags & TRACE_F_RAND_FWD)) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100536 unsigned int data = trace_get_htx_datalen(htxbuf(&chn->buf), offset, len);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200537
Christopher Fauletb2e58492019-11-12 11:13:01 +0100538 if (data) {
Willy Tarreau52bf8392020-03-08 00:42:37 +0100539 ret = ha_random() % (ret+1);
Christopher Fauletb2e58492019-11-12 11:13:01 +0100540 if (!ret || ret >= data)
541 ret = len;
542 }
543 }
Christopher Faulete6c3b692015-09-02 17:15:16 +0200544
Christopher Fauletb2e58492019-11-12 11:13:01 +0100545 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
546 "offset=%u - len=%u - forward=%d",
547 __FUNCTION__,
548 channel_label(chn), proxy_mode(s), stream_pos(s),
549 offset, len, ret);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200550
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100551 if (conf->flags & TRACE_F_HEXDUMP)
Christopher Fauletb2e58492019-11-12 11:13:01 +0100552 trace_htx_hexdump(htxbuf(&chn->buf), offset, ret);
553 }
554 else {
Christopher Faulete6c3b692015-09-02 17:15:16 +0200555
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100556 if (ret && (conf->flags & TRACE_F_RAND_FWD))
Willy Tarreau52bf8392020-03-08 00:42:37 +0100557 ret = ha_random() % (ret+1);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200558
Christopher Fauletb2e58492019-11-12 11:13:01 +0100559 FLT_STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - "
560 "offset=%u - len=%u - forward=%d",
561 __FUNCTION__,
562 channel_label(chn), proxy_mode(s), stream_pos(s),
563 offset, len, ret);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200564
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100565 if (conf->flags & TRACE_F_HEXDUMP)
Christopher Fauletb2e58492019-11-12 11:13:01 +0100566 trace_raw_hexdump(&chn->buf, offset, ret);
Christopher Fauletfcd99f82016-10-31 11:27:21 +0100567 }
568
Christopher Fauletb2e58492019-11-12 11:13:01 +0100569 if (ret != len)
570 task_wakeup(s->task, TASK_WOKEN_MSG);
Christopher Faulete6c3b692015-09-02 17:15:16 +0200571 return ret;
572}
Christopher Faulete6c3b692015-09-02 17:15:16 +0200573/********************************************************************
574 * Functions that manage the filter initialization
575 ********************************************************************/
576struct flt_ops trace_ops = {
577 /* Manage trace filter, called for each filter declaration */
Christopher Fauletf2273722017-07-27 16:58:42 +0200578 .init = trace_init,
579 .deinit = trace_deinit,
580 .check = trace_check,
581 .init_per_thread = trace_init_per_thread,
582 .deinit_per_thread = trace_deinit_per_thread,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200583
584 /* Handle start/stop of streams */
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200585 .attach = trace_attach,
586 .detach = trace_detach,
587 .stream_start = trace_stream_start,
588 .stream_set_backend = trace_stream_set_backend,
589 .stream_stop = trace_stream_stop,
Christopher Fauleta00d8172016-11-10 14:58:05 +0100590 .check_timeouts = trace_check_timeouts,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200591
592 /* Handle channels activity */
593 .channel_start_analyze = trace_chn_start_analyze,
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200594 .channel_pre_analyze = trace_chn_analyze,
595 .channel_post_analyze = trace_chn_analyze,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200596 .channel_end_analyze = trace_chn_end_analyze,
597
598 /* Filter HTTP requests and responses */
Christopher Faulet1339d742016-05-11 16:48:33 +0200599 .http_headers = trace_http_headers,
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100600 .http_payload = trace_http_payload,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200601 .http_end = trace_http_end,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200602 .http_reset = trace_http_reset,
603 .http_reply = trace_http_reply,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200604
605 /* Filter TCP data */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100606 .tcp_payload = trace_tcp_payload,
Christopher Faulete6c3b692015-09-02 17:15:16 +0200607};
608
609/* Return -1 on error, else 0 */
610static int
611parse_trace_flt(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +0200612 struct flt_conf *fconf, char **err, void *private)
Christopher Faulete6c3b692015-09-02 17:15:16 +0200613{
614 struct trace_config *conf;
615 int pos = *cur_arg;
616
617 conf = calloc(1, sizeof(*conf));
618 if (!conf) {
619 memprintf(err, "%s: out of memory", args[*cur_arg]);
620 return -1;
621 }
622 conf->proxy = px;
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100623 conf->flags = 0;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100624 if (strcmp(args[pos], "trace") == 0) {
Christopher Faulete6c3b692015-09-02 17:15:16 +0200625 pos++;
626
627 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100628 if (strcmp(args[pos], "name") == 0) {
Christopher Faulete6c3b692015-09-02 17:15:16 +0200629 if (!*args[pos + 1]) {
630 memprintf(err, "'%s' : '%s' option without value",
631 args[*cur_arg], args[pos]);
632 goto error;
633 }
634 conf->name = strdup(args[pos + 1]);
635 if (!conf->name) {
636 memprintf(err, "%s: out of memory", args[*cur_arg]);
637 goto error;
638 }
639 pos++;
640 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100641 else if (strcmp(args[pos], "quiet") == 0)
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100642 conf->flags |= TRACE_F_QUIET;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100643 else if (strcmp(args[pos], "random-parsing") == 0)
Dragan Dosena8018eb2021-09-20 09:29:19 +0200644 ; // ignore
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100645 else if (strcmp(args[pos], "random-forwarding") == 0)
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100646 conf->flags |= TRACE_F_RAND_FWD;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100647 else if (strcmp(args[pos], "hexdump") == 0)
Christopher Faulet47d9a4e2020-11-17 11:33:36 +0100648 conf->flags |= TRACE_F_HEXDUMP;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200649 else
650 break;
651 pos++;
652 }
653 *cur_arg = pos;
Christopher Fauletf4a4ef72018-12-07 17:39:53 +0100654 fconf->id = trace_flt_id;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100655 fconf->ops = &trace_ops;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200656 }
657
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100658 fconf->conf = conf;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200659 return 0;
660
661 error:
662 if (conf->name)
663 free(conf->name);
664 free(conf);
665 return -1;
666}
667
668/* Declare the filter parser for "trace" keyword */
669static struct flt_kw_list flt_kws = { "TRACE", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +0200670 { "trace", parse_trace_flt, NULL },
671 { NULL, NULL, NULL },
Christopher Faulete6c3b692015-09-02 17:15:16 +0200672 }
673};
674
Willy Tarreau0108d902018-11-25 19:14:37 +0100675INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);