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