Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 1 | /* |
| 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 Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
| 14 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 15 | #include <common/hathreads.h> |
| 16 | #include <common/initcall.h> |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 17 | #include <common/standard.h> |
| 18 | #include <common/time.h> |
| 19 | #include <common/tools.h> |
| 20 | |
| 21 | #include <types/channel.h> |
| 22 | #include <types/filters.h> |
| 23 | #include <types/global.h> |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 24 | #include <types/h1.h> |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 25 | #include <types/proxy.h> |
| 26 | #include <types/stream.h> |
| 27 | |
| 28 | #include <proto/filters.h> |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 29 | #include <proto/hdr_idx.h> |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 30 | #include <proto/http_htx.h> |
| 31 | #include <proto/htx.h> |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 32 | #include <proto/log.h> |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 33 | #include <proto/proto_http.h> |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 34 | #include <proto/stream.h> |
| 35 | |
| 36 | struct flt_ops trace_ops; |
| 37 | |
| 38 | struct trace_config { |
| 39 | struct proxy *proxy; |
| 40 | char *name; |
| 41 | int rand_parsing; |
| 42 | int rand_forwarding; |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 43 | int hexdump; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | #define TRACE(conf, fmt, ...) \ |
| 47 | fprintf(stderr, "%d.%06d [%-20s] " fmt "\n", \ |
| 48 | (int)now.tv_sec, (int)now.tv_usec, (conf)->name, \ |
| 49 | ##__VA_ARGS__) |
| 50 | |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 51 | #define STRM_TRACE(conf, strm, fmt, ...) \ |
| 52 | 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 Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 56 | ##__VA_ARGS__) |
| 57 | |
| 58 | |
| 59 | static const char * |
| 60 | channel_label(const struct channel *chn) |
| 61 | { |
| 62 | return (chn->flags & CF_ISRESP) ? "RESPONSE" : "REQUEST"; |
| 63 | } |
| 64 | |
| 65 | static const char * |
| 66 | proxy_mode(const struct stream *s) |
| 67 | { |
| 68 | struct proxy *px = (s->flags & SF_BE_ASSIGNED ? s->be : strm_fe(s)); |
| 69 | |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 70 | return ((px->mode == PR_MODE_HTTP) |
| 71 | ? (IS_HTX_STRM(s) ? "HTX" : "HTTP") |
| 72 | : "TCP"); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static const char * |
| 76 | stream_pos(const struct stream *s) |
| 77 | { |
| 78 | return (s->flags & SF_BE_ASSIGNED) ? "backend" : "frontend"; |
| 79 | } |
| 80 | |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 81 | static const char * |
| 82 | filter_type(const struct filter *f) |
| 83 | { |
| 84 | return (f->flags & FLT_FL_IS_BACKEND_FILTER) ? "backend" : "frontend"; |
| 85 | } |
| 86 | |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 87 | static void |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 88 | trace_hexdump(struct ist ist) |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 89 | { |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 90 | int i, j, padding; |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 91 | |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 92 | padding = ((ist.len % 16) ? (16 - ist.len % 16) : 0); |
| 93 | for (i = 0; i < ist.len + padding; i++) { |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 94 | if (!(i % 16)) |
| 95 | fprintf(stderr, "\t0x%06x: ", i); |
| 96 | else if (!(i % 8)) |
| 97 | fprintf(stderr, " "); |
| 98 | |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 99 | if (i < ist.len) |
| 100 | fprintf(stderr, "%02x ", (unsigned char)*(ist.ptr+i)); |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 101 | else |
| 102 | fprintf(stderr, " "); |
| 103 | |
| 104 | /* print ASCII dump */ |
| 105 | if (i % 16 == 15) { |
| 106 | fprintf(stderr, " |"); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 107 | for(j = i - 15; j <= i && j < ist.len; j++) |
| 108 | fprintf(stderr, "%c", (isprint(*(ist.ptr+j)) ? *(ist.ptr+j) : '.')); |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 109 | fprintf(stderr, "|\n"); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 114 | static void |
| 115 | trace_raw_hexdump(struct buffer *buf, int len, int out) |
| 116 | { |
| 117 | unsigned char p[len]; |
| 118 | int block1, block2; |
| 119 | |
| 120 | block1 = len; |
| 121 | if (block1 > b_contig_data(buf, out)) |
| 122 | block1 = b_contig_data(buf, out); |
| 123 | block2 = len - block1; |
| 124 | |
| 125 | memcpy(p, b_head(buf), block1); |
| 126 | memcpy(p+block1, b_orig(buf), block2); |
| 127 | trace_hexdump(ist2(p, len)); |
| 128 | } |
| 129 | |
| 130 | static void |
| 131 | trace_htx_hexdump(struct htx *htx, unsigned int offset, unsigned int len) |
| 132 | { |
| 133 | struct htx_ret htx_ret; |
| 134 | struct htx_blk *blk; |
| 135 | |
| 136 | htx_ret = htx_find_blk(htx, offset); |
| 137 | blk = htx_ret.blk; |
| 138 | offset = htx_ret.ret; |
| 139 | |
| 140 | while (blk) { |
| 141 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 142 | struct ist v; |
| 143 | |
| 144 | v = htx_get_blk_value(htx, blk); |
| 145 | v.ptr += offset; |
| 146 | v.len -= offset; |
| 147 | offset = 0; |
| 148 | |
| 149 | if (v.len > len) |
| 150 | v.len = len; |
| 151 | len -= v.len; |
| 152 | if (type == HTX_BLK_DATA || type == HTX_BLK_TLR) |
| 153 | trace_hexdump(v); |
| 154 | blk = htx_get_next_blk(htx, blk); |
| 155 | } |
| 156 | } |
| 157 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 158 | /*************************************************************************** |
| 159 | * Hooks that manage the filter lifecycle (init/check/deinit) |
| 160 | **************************************************************************/ |
| 161 | /* Initialize the filter. Returns -1 on error, else 0. */ |
| 162 | static int |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 163 | trace_init(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 164 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 165 | struct trace_config *conf = fconf->conf; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 166 | |
| 167 | if (conf->name) |
| 168 | memprintf(&conf->name, "%s/%s", conf->name, px->id); |
| 169 | else |
| 170 | memprintf(&conf->name, "TRACE/%s", px->id); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 171 | |
Christopher Faulet | 6e54095 | 2018-12-03 22:43:41 +0100 | [diff] [blame] | 172 | fconf->flags |= FLT_CFG_FL_HTX; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 173 | fconf->conf = conf; |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 174 | |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 175 | TRACE(conf, "filter initialized [read random=%s - fwd random=%s - hexdump=%s]", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 176 | (conf->rand_parsing ? "true" : "false"), |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 177 | (conf->rand_forwarding ? "true" : "false"), |
| 178 | (conf->hexdump ? "true" : "false")); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /* Free ressources allocated by the trace filter. */ |
| 183 | static void |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 184 | trace_deinit(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 185 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 186 | struct trace_config *conf = fconf->conf; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 187 | |
| 188 | if (conf) { |
| 189 | TRACE(conf, "filter deinitialized"); |
| 190 | free(conf->name); |
| 191 | free(conf); |
| 192 | } |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 193 | fconf->conf = NULL; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* Check configuration of a trace filter for a specified proxy. |
| 197 | * Return 1 on error, else 0. */ |
| 198 | static int |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 199 | trace_check(struct proxy *px, struct flt_conf *fconf) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 200 | { |
| 201 | return 0; |
| 202 | } |
| 203 | |
Christopher Faulet | f227372 | 2017-07-27 16:58:42 +0200 | [diff] [blame] | 204 | /* Initialize the filter for each thread. Return -1 on error, else 0. */ |
| 205 | static int |
| 206 | trace_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 207 | { |
| 208 | struct trace_config *conf = fconf->conf; |
| 209 | |
| 210 | TRACE(conf, "filter initialized for thread tid %u", tid); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | /* Free ressources allocate by the trace filter for each thread. */ |
| 215 | static void |
| 216 | trace_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 217 | { |
| 218 | struct trace_config *conf = fconf->conf; |
| 219 | |
| 220 | if (conf) |
| 221 | TRACE(conf, "filter deinitialized for thread tid %u", tid); |
| 222 | } |
| 223 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 224 | /************************************************************************** |
| 225 | * Hooks to handle start/stop of streams |
| 226 | *************************************************************************/ |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 227 | /* Called when a filter instance is created and attach to a stream */ |
| 228 | static int |
| 229 | trace_attach(struct stream *s, struct filter *filter) |
| 230 | { |
| 231 | struct trace_config *conf = FLT_CONF(filter); |
| 232 | |
| 233 | STRM_TRACE(conf, s, "%-25s: filter-type=%s", |
| 234 | __FUNCTION__, filter_type(filter)); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 235 | |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | /* Called when a filter instance is detach from a stream, just before its |
| 240 | * destruction */ |
| 241 | static void |
| 242 | trace_detach(struct stream *s, struct filter *filter) |
| 243 | { |
| 244 | struct trace_config *conf = FLT_CONF(filter); |
| 245 | |
| 246 | STRM_TRACE(conf, s, "%-25s: filter-type=%s", |
| 247 | __FUNCTION__, filter_type(filter)); |
| 248 | } |
| 249 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 250 | /* Called when a stream is created */ |
| 251 | static int |
| 252 | trace_stream_start(struct stream *s, struct filter *filter) |
| 253 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 254 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 255 | |
| 256 | STRM_TRACE(conf, s, "%-25s", |
| 257 | __FUNCTION__); |
| 258 | return 0; |
| 259 | } |
| 260 | |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 261 | |
| 262 | /* Called when a backend is set for a stream */ |
| 263 | static int |
| 264 | trace_stream_set_backend(struct stream *s, struct filter *filter, |
| 265 | struct proxy *be) |
| 266 | { |
| 267 | struct trace_config *conf = FLT_CONF(filter); |
| 268 | |
| 269 | STRM_TRACE(conf, s, "%-25s: backend=%s", |
| 270 | __FUNCTION__, be->id); |
| 271 | return 0; |
| 272 | } |
| 273 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 274 | /* Called when a stream is destroyed */ |
| 275 | static void |
| 276 | trace_stream_stop(struct stream *s, struct filter *filter) |
| 277 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 278 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 279 | |
| 280 | STRM_TRACE(conf, s, "%-25s", |
| 281 | __FUNCTION__); |
| 282 | } |
| 283 | |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 284 | /* Called when the stream is woken up because of an expired timer */ |
| 285 | static void |
| 286 | trace_check_timeouts(struct stream *s, struct filter *filter) |
| 287 | { |
| 288 | struct trace_config *conf = FLT_CONF(filter); |
| 289 | |
| 290 | STRM_TRACE(conf, s, "%-25s", |
| 291 | __FUNCTION__); |
| 292 | } |
| 293 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 294 | /************************************************************************** |
| 295 | * Hooks to handle channels activity |
| 296 | *************************************************************************/ |
| 297 | /* Called when analyze starts for a given channel */ |
| 298 | static int |
| 299 | trace_chn_start_analyze(struct stream *s, struct filter *filter, |
| 300 | struct channel *chn) |
| 301 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 302 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 303 | |
| 304 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
| 305 | __FUNCTION__, |
| 306 | channel_label(chn), proxy_mode(s), stream_pos(s)); |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 307 | filter->pre_analyzers |= (AN_REQ_ALL | AN_RES_ALL); |
| 308 | filter->post_analyzers |= (AN_REQ_ALL | AN_RES_ALL); |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 309 | register_data_filter(s, chn, filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 310 | return 1; |
| 311 | } |
| 312 | |
| 313 | /* Called before a processing happens on a given channel */ |
| 314 | static int |
| 315 | trace_chn_analyze(struct stream *s, struct filter *filter, |
| 316 | struct channel *chn, unsigned an_bit) |
| 317 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 318 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 319 | char *ana; |
| 320 | |
| 321 | switch (an_bit) { |
| 322 | case AN_REQ_INSPECT_FE: |
| 323 | ana = "AN_REQ_INSPECT_FE"; |
| 324 | break; |
| 325 | case AN_REQ_WAIT_HTTP: |
| 326 | ana = "AN_REQ_WAIT_HTTP"; |
| 327 | break; |
| 328 | case AN_REQ_HTTP_BODY: |
| 329 | ana = "AN_REQ_HTTP_BODY"; |
| 330 | break; |
| 331 | case AN_REQ_HTTP_PROCESS_FE: |
| 332 | ana = "AN_REQ_HTTP_PROCESS_FE"; |
| 333 | break; |
| 334 | case AN_REQ_SWITCHING_RULES: |
| 335 | ana = "AN_REQ_SWITCHING_RULES"; |
| 336 | break; |
| 337 | case AN_REQ_INSPECT_BE: |
| 338 | ana = "AN_REQ_INSPECT_BE"; |
| 339 | break; |
| 340 | case AN_REQ_HTTP_PROCESS_BE: |
| 341 | ana = "AN_REQ_HTTP_PROCESS_BE"; |
| 342 | break; |
| 343 | case AN_REQ_SRV_RULES: |
| 344 | ana = "AN_REQ_SRV_RULES"; |
| 345 | break; |
| 346 | case AN_REQ_HTTP_INNER: |
| 347 | ana = "AN_REQ_HTTP_INNER"; |
| 348 | break; |
| 349 | case AN_REQ_HTTP_TARPIT: |
| 350 | ana = "AN_REQ_HTTP_TARPIT"; |
| 351 | break; |
| 352 | case AN_REQ_STICKING_RULES: |
| 353 | ana = "AN_REQ_STICKING_RULES"; |
| 354 | break; |
| 355 | case AN_REQ_PRST_RDP_COOKIE: |
| 356 | ana = "AN_REQ_PRST_RDP_COOKIE"; |
| 357 | break; |
| 358 | case AN_REQ_HTTP_XFER_BODY: |
| 359 | ana = "AN_REQ_HTTP_XFER_BODY"; |
| 360 | break; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 361 | case AN_RES_INSPECT: |
| 362 | ana = "AN_RES_INSPECT"; |
| 363 | break; |
| 364 | case AN_RES_WAIT_HTTP: |
| 365 | ana = "AN_RES_WAIT_HTTP"; |
| 366 | break; |
| 367 | case AN_RES_HTTP_PROCESS_FE: // AN_RES_HTTP_PROCESS_BE |
| 368 | ana = "AN_RES_HTTP_PROCESS_FE/BE"; |
| 369 | break; |
| 370 | case AN_RES_STORE_RULES: |
| 371 | ana = "AN_RES_STORE_RULES"; |
| 372 | break; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 373 | case AN_RES_HTTP_XFER_BODY: |
| 374 | ana = "AN_RES_HTTP_XFER_BODY"; |
| 375 | break; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 376 | default: |
| 377 | ana = "unknown"; |
| 378 | } |
| 379 | |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 380 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - " |
| 381 | "analyzer=%s - step=%s", |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 382 | __FUNCTION__, |
| 383 | channel_label(chn), proxy_mode(s), stream_pos(s), |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 384 | ana, ((chn->analysers & an_bit) ? "PRE" : "POST")); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 385 | return 1; |
| 386 | } |
| 387 | |
| 388 | /* Called when analyze ends for a given channel */ |
| 389 | static int |
| 390 | trace_chn_end_analyze(struct stream *s, struct filter *filter, |
| 391 | struct channel *chn) |
| 392 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 393 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 394 | |
| 395 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
| 396 | __FUNCTION__, |
| 397 | channel_label(chn), proxy_mode(s), stream_pos(s)); |
| 398 | return 1; |
| 399 | } |
| 400 | |
| 401 | /************************************************************************** |
| 402 | * Hooks to filter HTTP messages |
| 403 | *************************************************************************/ |
| 404 | static int |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 405 | trace_http_headers(struct stream *s, struct filter *filter, |
| 406 | struct http_msg *msg) |
| 407 | { |
| 408 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 409 | |
| 410 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
| 411 | __FUNCTION__, |
| 412 | channel_label(msg->chn), proxy_mode(s), stream_pos(s)); |
| 413 | |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 414 | if (IS_HTX_STRM(s)) { |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 415 | struct htx *htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 416 | struct htx_sl *sl = http_find_stline(htx); |
| 417 | int32_t pos; |
| 418 | |
| 419 | STRM_TRACE(conf, s, "\t%.*s %.*s %.*s", |
| 420 | HTX_SL_P1_LEN(sl), HTX_SL_P1_PTR(sl), |
| 421 | HTX_SL_P2_LEN(sl), HTX_SL_P2_PTR(sl), |
| 422 | HTX_SL_P3_LEN(sl), HTX_SL_P3_PTR(sl)); |
| 423 | |
| 424 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 425 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 426 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 427 | struct ist n, v; |
| 428 | |
| 429 | if (type == HTX_BLK_EOH) |
| 430 | break; |
| 431 | if (type != HTX_BLK_HDR) |
| 432 | continue; |
| 433 | |
| 434 | n = htx_get_blk_name(htx, blk); |
| 435 | v = htx_get_blk_value(htx, blk); |
| 436 | STRM_TRACE(conf, s, "\t%.*s: %.*s", |
| 437 | (int)n.len, n.ptr, (int)v.len, v.ptr); |
| 438 | } |
| 439 | } |
| 440 | else { |
| 441 | struct hdr_idx *hdr_idx; |
| 442 | char *cur_hdr; |
| 443 | int cur_idx; |
| 444 | |
| 445 | STRM_TRACE(conf, s, "\t%.*s", MIN(msg->sl.rq.l, 74), ci_head(msg->chn)); |
| 446 | hdr_idx = &s->txn->hdr_idx; |
| 447 | cur_idx = hdr_idx_first_idx(hdr_idx); |
| 448 | cur_hdr = ci_head(msg->chn) + hdr_idx_first_pos(hdr_idx); |
| 449 | while (cur_idx) { |
| 450 | STRM_TRACE(conf, s, "\t%.*s", |
| 451 | MIN(hdr_idx->v[cur_idx].len, 74), cur_hdr); |
| 452 | cur_hdr += hdr_idx->v[cur_idx].len + hdr_idx->v[cur_idx].cr + 1; |
| 453 | cur_idx = hdr_idx->v[cur_idx].next; |
| 454 | } |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 455 | } |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 456 | return 1; |
| 457 | } |
| 458 | |
| 459 | static int |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 460 | trace_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 461 | unsigned int offset, unsigned int len) |
| 462 | { |
| 463 | struct trace_config *conf = FLT_CONF(filter); |
| 464 | int ret = len; |
| 465 | |
| 466 | if (ret && conf->rand_forwarding) |
| 467 | ret = random() % (ret+1); |
| 468 | |
| 469 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - " |
| 470 | "offset=%u - len=%u - forward=%d", |
| 471 | __FUNCTION__, |
| 472 | channel_label(msg->chn), proxy_mode(s), stream_pos(s), |
| 473 | offset, len, ret); |
| 474 | |
| 475 | if (conf->hexdump) |
Christopher Faulet | 27ba2dc | 2018-12-05 11:53:24 +0100 | [diff] [blame] | 476 | trace_htx_hexdump(htxbuf(&msg->chn->buf), offset, len); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 477 | |
| 478 | if (ret != len) |
| 479 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | static int |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 484 | trace_http_data(struct stream *s, struct filter *filter, |
| 485 | struct http_msg *msg) |
| 486 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 487 | struct trace_config *conf = FLT_CONF(filter); |
Willy Tarreau | f158937 | 2018-06-19 07:22:43 +0200 | [diff] [blame] | 488 | int avail = MIN(msg->chunk_len + msg->next, ci_data(msg->chn)) - FLT_NXT(filter, msg->chn); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 489 | int ret = avail; |
| 490 | |
| 491 | if (ret && conf->rand_parsing) |
| 492 | ret = random() % (ret+1); |
| 493 | |
| 494 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - " |
| 495 | "chunk_len=%llu - next=%u - fwd=%u - avail=%d - consume=%d", |
| 496 | __FUNCTION__, |
| 497 | channel_label(msg->chn), proxy_mode(s), stream_pos(s), |
| 498 | msg->chunk_len, FLT_NXT(filter, msg->chn), |
| 499 | FLT_FWD(filter, msg->chn), avail, ret); |
| 500 | if (ret != avail) |
| 501 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 502 | return ret; |
| 503 | } |
| 504 | |
| 505 | static int |
| 506 | trace_http_chunk_trailers(struct stream *s, struct filter *filter, |
| 507 | struct http_msg *msg) |
| 508 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 509 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 510 | |
| 511 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
| 512 | __FUNCTION__, |
| 513 | channel_label(msg->chn), proxy_mode(s), stream_pos(s)); |
| 514 | return 1; |
| 515 | } |
| 516 | |
| 517 | static int |
| 518 | trace_http_end(struct stream *s, struct filter *filter, |
| 519 | struct http_msg *msg) |
| 520 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 521 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 522 | |
| 523 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
| 524 | __FUNCTION__, |
| 525 | channel_label(msg->chn), proxy_mode(s), stream_pos(s)); |
| 526 | return 1; |
| 527 | } |
| 528 | |
| 529 | static void |
| 530 | trace_http_reset(struct stream *s, struct filter *filter, |
| 531 | struct http_msg *msg) |
| 532 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 533 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 534 | |
| 535 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
| 536 | __FUNCTION__, |
| 537 | channel_label(msg->chn), proxy_mode(s), stream_pos(s)); |
| 538 | } |
| 539 | |
| 540 | static void |
| 541 | trace_http_reply(struct stream *s, struct filter *filter, short status, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 542 | const struct buffer *msg) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 543 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 544 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 545 | |
| 546 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s)", |
| 547 | __FUNCTION__, "-", proxy_mode(s), stream_pos(s)); |
| 548 | } |
| 549 | |
| 550 | static int |
| 551 | trace_http_forward_data(struct stream *s, struct filter *filter, |
| 552 | struct http_msg *msg, unsigned int len) |
| 553 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 554 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 555 | int ret = len; |
| 556 | |
| 557 | if (ret && conf->rand_forwarding) |
| 558 | ret = random() % (ret+1); |
| 559 | |
| 560 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - " |
| 561 | "len=%u - nxt=%u - fwd=%u - forward=%d", |
| 562 | __FUNCTION__, |
| 563 | channel_label(msg->chn), proxy_mode(s), stream_pos(s), len, |
| 564 | FLT_NXT(filter, msg->chn), FLT_FWD(filter, msg->chn), ret); |
| 565 | |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 566 | if (conf->hexdump) { |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 567 | c_adv(msg->chn, FLT_FWD(filter, msg->chn)); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 568 | trace_raw_hexdump(&msg->chn->buf, ret, co_data(msg->chn)); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 569 | c_rew(msg->chn, FLT_FWD(filter, msg->chn)); |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 572 | if ((ret != len) || |
| 573 | (FLT_NXT(filter, msg->chn) != FLT_FWD(filter, msg->chn) + ret)) |
| 574 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 575 | return ret; |
| 576 | } |
| 577 | |
| 578 | /************************************************************************** |
| 579 | * Hooks to filter TCP data |
| 580 | *************************************************************************/ |
| 581 | static int |
| 582 | trace_tcp_data(struct stream *s, struct filter *filter, struct channel *chn) |
| 583 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 584 | struct trace_config *conf = FLT_CONF(filter); |
Willy Tarreau | f158937 | 2018-06-19 07:22:43 +0200 | [diff] [blame] | 585 | int avail = ci_data(chn) - FLT_NXT(filter, chn); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 586 | int ret = avail; |
| 587 | |
| 588 | if (ret && conf->rand_parsing) |
| 589 | ret = random() % (ret+1); |
| 590 | |
| 591 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - next=%u - avail=%u - consume=%d", |
| 592 | __FUNCTION__, |
| 593 | channel_label(chn), proxy_mode(s), stream_pos(s), |
| 594 | FLT_NXT(filter, chn), avail, ret); |
| 595 | |
| 596 | if (ret != avail) |
| 597 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 598 | return ret; |
| 599 | } |
| 600 | |
| 601 | static int |
| 602 | trace_tcp_forward_data(struct stream *s, struct filter *filter, struct channel *chn, |
| 603 | unsigned int len) |
| 604 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 605 | struct trace_config *conf = FLT_CONF(filter); |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 606 | int ret = len; |
| 607 | |
| 608 | if (ret && conf->rand_forwarding) |
| 609 | ret = random() % (ret+1); |
| 610 | |
| 611 | STRM_TRACE(conf, s, "%-25s: channel=%-10s - mode=%-5s (%s) - len=%u - fwd=%u - forward=%d", |
| 612 | __FUNCTION__, |
| 613 | channel_label(chn), proxy_mode(s), stream_pos(s), len, |
| 614 | FLT_FWD(filter, chn), ret); |
| 615 | |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 616 | if (conf->hexdump) { |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 617 | c_adv(chn, FLT_FWD(filter, chn)); |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 618 | trace_raw_hexdump(&chn->buf, ret, co_data(chn)); |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 619 | c_rew(chn, FLT_FWD(filter, chn)); |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 620 | } |
| 621 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 622 | if (ret != len) |
| 623 | task_wakeup(s->task, TASK_WOKEN_MSG); |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | /******************************************************************** |
| 628 | * Functions that manage the filter initialization |
| 629 | ********************************************************************/ |
| 630 | struct flt_ops trace_ops = { |
| 631 | /* Manage trace filter, called for each filter declaration */ |
Christopher Faulet | f227372 | 2017-07-27 16:58:42 +0200 | [diff] [blame] | 632 | .init = trace_init, |
| 633 | .deinit = trace_deinit, |
| 634 | .check = trace_check, |
| 635 | .init_per_thread = trace_init_per_thread, |
| 636 | .deinit_per_thread = trace_deinit_per_thread, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 637 | |
| 638 | /* Handle start/stop of streams */ |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 639 | .attach = trace_attach, |
| 640 | .detach = trace_detach, |
| 641 | .stream_start = trace_stream_start, |
| 642 | .stream_set_backend = trace_stream_set_backend, |
| 643 | .stream_stop = trace_stream_stop, |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 644 | .check_timeouts = trace_check_timeouts, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 645 | |
| 646 | /* Handle channels activity */ |
| 647 | .channel_start_analyze = trace_chn_start_analyze, |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 648 | .channel_pre_analyze = trace_chn_analyze, |
| 649 | .channel_post_analyze = trace_chn_analyze, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 650 | .channel_end_analyze = trace_chn_end_analyze, |
| 651 | |
| 652 | /* Filter HTTP requests and responses */ |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 653 | .http_headers = trace_http_headers, |
Christopher Faulet | e0aa6f7 | 2018-11-30 22:23:32 +0100 | [diff] [blame] | 654 | .http_payload = trace_http_payload, |
| 655 | |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 656 | .http_data = trace_http_data, |
| 657 | .http_chunk_trailers = trace_http_chunk_trailers, |
| 658 | .http_end = trace_http_end, |
| 659 | |
| 660 | .http_reset = trace_http_reset, |
| 661 | .http_reply = trace_http_reply, |
| 662 | .http_forward_data = trace_http_forward_data, |
| 663 | |
| 664 | /* Filter TCP data */ |
| 665 | .tcp_data = trace_tcp_data, |
| 666 | .tcp_forward_data = trace_tcp_forward_data, |
| 667 | }; |
| 668 | |
| 669 | /* Return -1 on error, else 0 */ |
| 670 | static int |
| 671 | parse_trace_flt(char **args, int *cur_arg, struct proxy *px, |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 672 | struct flt_conf *fconf, char **err, void *private) |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 673 | { |
| 674 | struct trace_config *conf; |
| 675 | int pos = *cur_arg; |
| 676 | |
| 677 | conf = calloc(1, sizeof(*conf)); |
| 678 | if (!conf) { |
| 679 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 680 | return -1; |
| 681 | } |
| 682 | conf->proxy = px; |
| 683 | |
| 684 | if (!strcmp(args[pos], "trace")) { |
| 685 | pos++; |
| 686 | |
| 687 | while (*args[pos]) { |
| 688 | if (!strcmp(args[pos], "name")) { |
| 689 | if (!*args[pos + 1]) { |
| 690 | memprintf(err, "'%s' : '%s' option without value", |
| 691 | args[*cur_arg], args[pos]); |
| 692 | goto error; |
| 693 | } |
| 694 | conf->name = strdup(args[pos + 1]); |
| 695 | if (!conf->name) { |
| 696 | memprintf(err, "%s: out of memory", args[*cur_arg]); |
| 697 | goto error; |
| 698 | } |
| 699 | pos++; |
| 700 | } |
| 701 | else if (!strcmp(args[pos], "random-parsing")) |
| 702 | conf->rand_parsing = 1; |
| 703 | else if (!strcmp(args[pos], "random-forwarding")) |
| 704 | conf->rand_forwarding = 1; |
Christopher Faulet | fcd99f8 | 2016-10-31 11:27:21 +0100 | [diff] [blame] | 705 | else if (!strcmp(args[pos], "hexdump")) |
| 706 | conf->hexdump = 1; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 707 | else |
| 708 | break; |
| 709 | pos++; |
| 710 | } |
| 711 | *cur_arg = pos; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 712 | fconf->ops = &trace_ops; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 713 | } |
| 714 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 715 | fconf->conf = conf; |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 716 | return 0; |
| 717 | |
| 718 | error: |
| 719 | if (conf->name) |
| 720 | free(conf->name); |
| 721 | free(conf); |
| 722 | return -1; |
| 723 | } |
| 724 | |
| 725 | /* Declare the filter parser for "trace" keyword */ |
| 726 | static struct flt_kw_list flt_kws = { "TRACE", { }, { |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 727 | { "trace", parse_trace_flt, NULL }, |
| 728 | { NULL, NULL, NULL }, |
Christopher Faulet | e6c3b69 | 2015-09-02 17:15:16 +0200 | [diff] [blame] | 729 | } |
| 730 | }; |
| 731 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 732 | INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws); |