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