blob: e349431a692a31e29a2f21dc0af4343e6af2cb00 [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 Tarreau0108d902018-11-25 19:14:37 +010015#include <common/hathreads.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010016#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010017#include <common/initcall.h>
Christopher Faulete6c3b692015-09-02 17:15:16 +020018#include <common/standard.h>
19#include <common/time.h>
20#include <common/tools.h>
21
22#include <types/channel.h>
23#include <types/filters.h>
24#include <types/global.h>
25#include <types/proxy.h>
26#include <types/stream.h>
27
28#include <proto/filters.h>
Christopher Faulete0aa6f72018-11-30 22:23:32 +010029#include <proto/http_htx.h>
Christopher Faulete6c3b692015-09-02 17:15:16 +020030#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020031#include <proto/http_ana.h>
Christopher Faulete6c3b692015-09-02 17:15:16 +020032#include <proto/stream.h>
33
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010034const char *trace_flt_id = "trace filter";
35
Christopher Faulete6c3b692015-09-02 17:15:16 +020036struct flt_ops trace_ops;
37
38struct trace_config {
39 struct proxy *proxy;
40 char *name;
41 int rand_parsing;
42 int rand_forwarding;
Christopher Fauletfcd99f82016-10-31 11:27:21 +010043 int hexdump;
Christopher Faulete6c3b692015-09-02 17:15:16 +020044};
45
Christopher Fauleta3ed2712019-11-04 11:35:42 +010046#define FLT_TRACE(conf, fmt, ...) \
Christopher Faulete6c3b692015-09-02 17:15:16 +020047 fprintf(stderr, "%d.%06d [%-20s] " fmt "\n", \
48 (int)now.tv_sec, (int)now.tv_usec, (conf)->name, \
49 ##__VA_ARGS__)
50
Christopher Fauleta3ed2712019-11-04 11:35:42 +010051#define FLT_STRM_TRACE(conf, strm, fmt, ...) \
Christopher Fauletfcd99f82016-10-31 11:27:21 +010052 fprintf(stderr, "%d.%06d [%-20s] [strm %p(%x) 0x%08x 0x%08x] " fmt "\n", \
53 (int)now.tv_sec, (int)now.tv_usec, (conf)->name, \
54 strm, (strm ? ((struct stream *)strm)->uniq_id : ~0U), \
55 (strm ? strm->req.analysers : 0), (strm ? strm->res.analysers : 0), \
Christopher Faulete6c3b692015-09-02 17:15:16 +020056 ##__VA_ARGS__)
57
58
59static const char *
60channel_label(const struct channel *chn)
61{
62 return (chn->flags & CF_ISRESP) ? "RESPONSE" : "REQUEST";
63}
64
65static const char *
66proxy_mode(const struct stream *s)
67{
68 struct proxy *px = (s->flags & SF_BE_ASSIGNED ? s->be : strm_fe(s));
69
Christopher Faulet386a0cd2019-07-15 21:22:44 +020070 return ((px->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Christopher Faulete6c3b692015-09-02 17:15:16 +020071}
72
73static const char *
74stream_pos(const struct stream *s)
75{
76 return (s->flags & SF_BE_ASSIGNED) ? "backend" : "frontend";
77}
78
Christopher Faulet31ed32d2016-06-21 11:42:37 +020079static const char *
80filter_type(const struct filter *f)
81{
82 return (f->flags & FLT_FL_IS_BACKEND_FILTER) ? "backend" : "frontend";
83}
84
Christopher Fauletfcd99f82016-10-31 11:27:21 +010085static void
Christopher Faulete0aa6f72018-11-30 22:23:32 +010086trace_hexdump(struct ist ist)
Christopher Fauletfcd99f82016-10-31 11:27:21 +010087{
Christopher Faulete0aa6f72018-11-30 22:23:32 +010088 int i, j, padding;
Christopher Fauletfcd99f82016-10-31 11:27:21 +010089
Christopher Faulete0aa6f72018-11-30 22:23:32 +010090 padding = ((ist.len % 16) ? (16 - ist.len % 16) : 0);
91 for (i = 0; i < ist.len + padding; i++) {
Christopher Fauletfcd99f82016-10-31 11:27:21 +010092 if (!(i % 16))
93 fprintf(stderr, "\t0x%06x: ", i);
94 else if (!(i % 8))
95 fprintf(stderr, " ");
96
Christopher Faulete0aa6f72018-11-30 22:23:32 +010097 if (i < ist.len)
98 fprintf(stderr, "%02x ", (unsigned char)*(ist.ptr+i));
Christopher Fauletfcd99f82016-10-31 11:27:21 +010099 else
100 fprintf(stderr, " ");
101
102 /* print ASCII dump */
103 if (i % 16 == 15) {
104 fprintf(stderr, " |");
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100105 for(j = i - 15; j <= i && j < ist.len; j++)
106 fprintf(stderr, "%c", (isprint(*(ist.ptr+j)) ? *(ist.ptr+j) : '.'));
Christopher Fauletfcd99f82016-10-31 11:27:21 +0100107 fprintf(stderr, "|\n");
108 }
109 }
110}
111
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100112static void
Christopher Fauletb2e58492019-11-12 11:13:01 +0100113trace_raw_hexdump(struct buffer *buf, unsigned int offset, unsigned int len)
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100114{
115 unsigned char p[len];
116 int block1, block2;
117
118 block1 = len;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100119 if (block1 > b_contig_data(buf, offset))
120 block1 = b_contig_data(buf, offset);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100121 block2 = len - block1;
122
Christopher Fauletb2e58492019-11-12 11:13:01 +0100123 memcpy(p, b_peek(buf, offset), block1);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100124 memcpy(p+block1, b_orig(buf), block2);
125 trace_hexdump(ist2(p, len));
126}
127
128static void
129trace_htx_hexdump(struct htx *htx, unsigned int offset, unsigned int len)
130{
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100131 struct htx_blk *blk;
132
Christopher Fauletee847d42019-05-23 11:55:33 +0200133 for (blk = htx_get_first_blk(htx); blk && len; blk = htx_get_next_blk(htx, blk)) {
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100134 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Fauletee847d42019-05-23 11:55:33 +0200135 uint32_t sz = htx_get_blksz(blk);
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100136 struct ist v;
137
Christopher Fauletee847d42019-05-23 11:55:33 +0200138 if (offset >= sz) {
139 offset -= sz;
140 continue;
141 }
142
Christopher Faulete0aa6f72018-11-30 22:23:32 +0100143 v = htx_get_blk_value(htx, blk);
144 v.ptr += offset;
145 v.len -= offset;
146 offset = 0;
147
148 if (v.len > len)
149 v.len = len;
150 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;
160 uint32_t sz, data = 0;
161
162 for (blk = htx_get_first_blk(htx); blk; blk = htx_get_next_blk(htx, blk)) {
163 if (htx_get_blk_type(blk) != HTX_BLK_DATA)
164 break;
165
166 sz = htx_get_blksz(blk);
167 if (offset >= sz) {
168 offset -= sz;
169 continue;
170 }
171 data += sz - offset;
172 offset = 0;
173 if (data > len) {
174 data = len;
175 break;
176 }
177 }
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 Fauleta3ed2712019-11-04 11:35:42 +0100198 FLT_TRACE(conf, "filter initialized [read random=%s - fwd random=%s - hexdump=%s]",
Christopher Faulete6c3b692015-09-02 17:15:16 +0200199 (conf->rand_parsing ? "true" : "false"),
Christopher Fauletfcd99f82016-10-31 11:27:21 +0100200 (conf->rand_forwarding ? "true" : "false"),
201 (conf->hexdump ? "true" : "false"));
Christopher Faulete6c3b692015-09-02 17:15:16 +0200202 return 0;
203}
204
205/* Free ressources allocated by the trace filter. */
206static 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
237/* Free ressources allocate by the trace filter for each thread. */
238static 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 Faulet0bdeeaa2019-06-04 22:09:53 +0200468 if (ret && conf->rand_forwarding) {
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) {
Christopher Faulet647fe1d2019-06-12 16:07:48 +0200472 ret = random() % (ret+1);
473 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
484 if (conf->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) {
536 if (ret && conf->rand_forwarding) {
537 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) {
540 ret = random() % (ret+1);
541 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 Fauletb2e58492019-11-12 11:13:01 +0100552 if (conf->hexdump)
553 trace_htx_hexdump(htxbuf(&chn->buf), offset, ret);
554 }
555 else {
Christopher Faulete6c3b692015-09-02 17:15:16 +0200556
Christopher Fauletb2e58492019-11-12 11:13:01 +0100557 if (ret && conf->rand_forwarding)
558 ret = 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 Fauletb2e58492019-11-12 11:13:01 +0100566 if (conf->hexdump)
567 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;
624
625 if (!strcmp(args[pos], "trace")) {
626 pos++;
627
628 while (*args[pos]) {
629 if (!strcmp(args[pos], "name")) {
630 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 }
642 else if (!strcmp(args[pos], "random-parsing"))
643 conf->rand_parsing = 1;
644 else if (!strcmp(args[pos], "random-forwarding"))
645 conf->rand_forwarding = 1;
Christopher Fauletfcd99f82016-10-31 11:27:21 +0100646 else if (!strcmp(args[pos], "hexdump"))
647 conf->hexdump = 1;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200648 else
649 break;
650 pos++;
651 }
652 *cur_arg = pos;
Christopher Fauletf4a4ef72018-12-07 17:39:53 +0100653 fconf->id = trace_flt_id;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100654 fconf->ops = &trace_ops;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200655 }
656
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100657 fconf->conf = conf;
Christopher Faulete6c3b692015-09-02 17:15:16 +0200658 return 0;
659
660 error:
661 if (conf->name)
662 free(conf->name);
663 free(conf);
664 return -1;
665}
666
667/* Declare the filter parser for "trace" keyword */
668static struct flt_kw_list flt_kws = { "TRACE", { }, {
Thierry Fournier3610c392016-04-13 18:27:51 +0200669 { "trace", parse_trace_flt, NULL },
670 { NULL, NULL, NULL },
Christopher Faulete6c3b692015-09-02 17:15:16 +0200671 }
672};
673
Willy Tarreau0108d902018-11-25 19:14:37 +0100674INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);