Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +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 | |
| 13 | #include <common/buffer.h> |
| 14 | #include <common/debug.h> |
| 15 | #include <common/cfgparse.h> |
| 16 | #include <common/compat.h> |
| 17 | #include <common/config.h> |
| 18 | #include <common/errors.h> |
Willy Tarreau | b96b77e | 2018-12-11 10:22:41 +0100 | [diff] [blame] | 19 | #include <common/htx.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 20 | #include <common/initcall.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 21 | #include <common/namespace.h> |
| 22 | #include <common/standard.h> |
Christopher Faulet | 71a6a8e | 2017-07-27 16:33:28 +0200 | [diff] [blame] | 23 | #include <common/hathreads.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 24 | |
| 25 | #include <types/filters.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 26 | #include <types/http_ana.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 27 | |
| 28 | #include <proto/compression.h> |
| 29 | #include <proto/filters.h> |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 30 | #include <proto/flt_http_comp.h> |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 31 | #include <proto/http_htx.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 32 | #include <proto/http_ana.h> |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 33 | #include <proto/stream.h> |
| 34 | #include <proto/stream_interface.h> |
| 35 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 36 | #define TRACE_SOURCE &trace_strm |
| 37 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 38 | /* Pool used to allocate filters */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 39 | DECLARE_STATIC_POOL(pool_head_filter, "filter", sizeof(struct filter)); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 40 | |
| 41 | static int handle_analyzer_result(struct stream *s, struct channel *chn, unsigned int an_bit, int ret); |
| 42 | |
| 43 | /* - RESUME_FILTER_LOOP and RESUME_FILTER_END must always be used together. |
| 44 | * The first one begins a loop and the seconds one ends it. |
| 45 | * |
| 46 | * - BREAK_EXECUTION must be used to break the loop and set the filter from |
| 47 | * which to resume the next time. |
| 48 | * |
Bertrand Jacquin | 874a35c | 2018-09-10 21:26:07 +0100 | [diff] [blame] | 49 | * Here is an example: |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 50 | * |
| 51 | * RESUME_FILTER_LOOP(stream, channel) { |
| 52 | * ... |
| 53 | * if (cond) |
| 54 | * BREAK_EXECUTION(stream, channel, label); |
| 55 | * ... |
| 56 | * } RESUME_FILTER_END; |
| 57 | * ... |
| 58 | * label: |
| 59 | * ... |
| 60 | * |
| 61 | */ |
| 62 | #define RESUME_FILTER_LOOP(strm, chn) \ |
| 63 | do { \ |
| 64 | struct filter *filter; \ |
| 65 | \ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 66 | if (strm_flt(strm)->current[CHN_IDX(chn)]) { \ |
| 67 | filter = strm_flt(strm)->current[CHN_IDX(chn)]; \ |
| 68 | strm_flt(strm)->current[CHN_IDX(chn)] = NULL; \ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 69 | goto resume_execution; \ |
| 70 | } \ |
| 71 | \ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 72 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { \ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 73 | resume_execution: |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 74 | |
| 75 | #define RESUME_FILTER_END \ |
| 76 | } \ |
| 77 | } while(0) |
| 78 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 79 | #define BREAK_EXECUTION(strm, chn, label) \ |
| 80 | do { \ |
| 81 | strm_flt(strm)->current[CHN_IDX(chn)] = filter; \ |
| 82 | goto label; \ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 83 | } while (0) |
| 84 | |
| 85 | |
| 86 | /* List head of all known filter keywords */ |
| 87 | static struct flt_kw_list flt_keywords = { |
| 88 | .list = LIST_HEAD_INIT(flt_keywords.list) |
| 89 | }; |
| 90 | |
| 91 | /* |
| 92 | * Registers the filter keyword list <kwl> as a list of valid keywords for next |
| 93 | * parsing sessions. |
| 94 | */ |
| 95 | void |
| 96 | flt_register_keywords(struct flt_kw_list *kwl) |
| 97 | { |
| 98 | LIST_ADDQ(&flt_keywords.list, &kwl->list); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Returns a pointer to the filter keyword <kw>, or NULL if not found. If the |
| 103 | * keyword is found with a NULL ->parse() function, then an attempt is made to |
| 104 | * find one with a valid ->parse() function. This way it is possible to declare |
| 105 | * platform-dependant, known keywords as NULL, then only declare them as valid |
| 106 | * if some options are met. Note that if the requested keyword contains an |
| 107 | * opening parenthesis, everything from this point is ignored. |
| 108 | */ |
| 109 | struct flt_kw * |
| 110 | flt_find_kw(const char *kw) |
| 111 | { |
| 112 | int index; |
| 113 | const char *kwend; |
| 114 | struct flt_kw_list *kwl; |
| 115 | struct flt_kw *ret = NULL; |
| 116 | |
| 117 | kwend = strchr(kw, '('); |
| 118 | if (!kwend) |
| 119 | kwend = kw + strlen(kw); |
| 120 | |
| 121 | list_for_each_entry(kwl, &flt_keywords.list, list) { |
| 122 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 123 | if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) && |
| 124 | kwl->kw[index].kw[kwend-kw] == 0) { |
| 125 | if (kwl->kw[index].parse) |
| 126 | return &kwl->kw[index]; /* found it !*/ |
| 127 | else |
| 128 | ret = &kwl->kw[index]; /* may be OK */ |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Dumps all registered "filter" keywords to the <out> string pointer. The |
| 137 | * unsupported keywords are only dumped if their supported form was not found. |
| 138 | */ |
| 139 | void |
| 140 | flt_dump_kws(char **out) |
| 141 | { |
| 142 | struct flt_kw_list *kwl; |
| 143 | int index; |
| 144 | |
Christopher Faulet | 784063e | 2020-05-18 12:14:18 +0200 | [diff] [blame] | 145 | if (!out) |
| 146 | return; |
| 147 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 148 | *out = NULL; |
| 149 | list_for_each_entry(kwl, &flt_keywords.list, list) { |
| 150 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 151 | if (kwl->kw[index].parse || |
| 152 | flt_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) { |
| 153 | memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "", |
| 154 | kwl->scope, |
| 155 | kwl->kw[index].kw, |
| 156 | kwl->kw[index].parse ? "" : " (not supported)"); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /* |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 163 | * Lists the known filters on <out> |
| 164 | */ |
| 165 | void |
| 166 | list_filters(FILE *out) |
| 167 | { |
| 168 | char *filters, *p, *f; |
| 169 | |
| 170 | fprintf(out, "Available filters :\n"); |
| 171 | flt_dump_kws(&filters); |
| 172 | for (p = filters; (f = strtok_r(p,"\n",&p));) |
| 173 | fprintf(out, "\t%s\n", f); |
| 174 | free(filters); |
| 175 | } |
| 176 | |
| 177 | /* |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 178 | * Parses the "filter" keyword. All keywords must be handled by filters |
| 179 | * themselves |
| 180 | */ |
| 181 | static int |
| 182 | parse_filter(char **args, int section_type, struct proxy *curpx, |
| 183 | struct proxy *defpx, const char *file, int line, char **err) |
| 184 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 185 | struct flt_conf *fconf = NULL; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 186 | |
| 187 | /* Filter cannot be defined on a default proxy */ |
| 188 | if (curpx == defpx) { |
Christopher Faulet | cc7317d | 2016-04-04 10:51:17 +0200 | [diff] [blame] | 189 | memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.", |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 190 | file, line, args[0]); |
| 191 | return -1; |
| 192 | } |
| 193 | if (!strcmp(args[0], "filter")) { |
| 194 | struct flt_kw *kw; |
| 195 | int cur_arg; |
| 196 | |
| 197 | if (!*args[1]) { |
| 198 | memprintf(err, |
| 199 | "parsing [%s:%d] : missing argument for '%s' in %s '%s'.", |
| 200 | file, line, args[0], proxy_type_str(curpx), curpx->id); |
| 201 | goto error; |
| 202 | } |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 203 | fconf = calloc(1, sizeof(*fconf)); |
| 204 | if (!fconf) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 205 | memprintf(err, "'%s' : out of memory", args[0]); |
| 206 | goto error; |
| 207 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 208 | |
| 209 | cur_arg = 1; |
| 210 | kw = flt_find_kw(args[cur_arg]); |
| 211 | if (kw) { |
| 212 | if (!kw->parse) { |
| 213 | memprintf(err, "parsing [%s:%d] : '%s' : " |
| 214 | "'%s' option is not implemented in this version (check build options).", |
| 215 | file, line, args[0], args[cur_arg]); |
| 216 | goto error; |
| 217 | } |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 218 | if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 219 | if (err && *err) |
| 220 | memprintf(err, "'%s' : '%s'", |
| 221 | args[0], *err); |
| 222 | else |
| 223 | memprintf(err, "'%s' : error encountered while processing '%s'", |
| 224 | args[0], args[cur_arg]); |
| 225 | goto error; |
| 226 | } |
| 227 | } |
| 228 | else { |
| 229 | flt_dump_kws(err); |
| 230 | indent_msg(err, 4); |
| 231 | memprintf(err, "'%s' : unknown keyword '%s'.%s%s", |
| 232 | args[0], args[cur_arg], |
| 233 | err && *err ? " Registered keywords :" : "", err && *err ? *err : ""); |
| 234 | goto error; |
| 235 | } |
| 236 | if (*args[cur_arg]) { |
| 237 | memprintf(err, "'%s %s' : unknown keyword '%s'.", |
| 238 | args[0], args[1], args[cur_arg]); |
| 239 | goto error; |
| 240 | } |
Christopher Faulet | 00e818a | 2016-04-19 17:00:44 +0200 | [diff] [blame] | 241 | if (fconf->ops == NULL) { |
| 242 | memprintf(err, "'%s %s' : no callbacks defined.", |
| 243 | args[0], args[1]); |
| 244 | goto error; |
| 245 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 246 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 247 | LIST_ADDQ(&curpx->filter_configs, &fconf->list); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 248 | } |
| 249 | return 0; |
| 250 | |
| 251 | error: |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 252 | free(fconf); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 253 | return -1; |
| 254 | |
| 255 | |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Calls 'init' callback for all filters attached to a proxy. This happens after |
| 260 | * the configuration parsing. Filters can finish to fill their config. Returns |
| 261 | * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise. |
| 262 | */ |
Willy Tarreau | 64bca59 | 2016-12-21 20:13:11 +0100 | [diff] [blame] | 263 | static int |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 264 | flt_init(struct proxy *proxy) |
| 265 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 266 | struct flt_conf *fconf; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 267 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 268 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 269 | if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 270 | return ERR_ALERT|ERR_FATAL; |
| 271 | } |
| 272 | return 0; |
| 273 | } |
| 274 | |
Christopher Faulet | 71a6a8e | 2017-07-27 16:33:28 +0200 | [diff] [blame] | 275 | /* |
| 276 | * Calls 'init_per_thread' callback for all filters attached to a proxy for each |
| 277 | * threads. This happens after the thread creation. Filters can finish to fill |
| 278 | * their config. Returns (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise. |
| 279 | */ |
| 280 | static int |
| 281 | flt_init_per_thread(struct proxy *proxy) |
| 282 | { |
| 283 | struct flt_conf *fconf; |
| 284 | |
| 285 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 286 | if (fconf->ops->init_per_thread && fconf->ops->init_per_thread(proxy, fconf) < 0) |
| 287 | return ERR_ALERT|ERR_FATAL; |
| 288 | } |
| 289 | return 0; |
| 290 | } |
| 291 | |
Willy Tarreau | 64bca59 | 2016-12-21 20:13:11 +0100 | [diff] [blame] | 292 | /* Calls flt_init() for all proxies, see above */ |
| 293 | static int |
| 294 | flt_init_all() |
| 295 | { |
| 296 | struct proxy *px; |
| 297 | int err_code = 0; |
| 298 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 299 | for (px = proxies_list; px; px = px->next) { |
Willy Tarreau | 64bca59 | 2016-12-21 20:13:11 +0100 | [diff] [blame] | 300 | err_code |= flt_init(px); |
| 301 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 302 | ha_alert("Failed to initialize filters for proxy '%s'.\n", |
| 303 | px->id); |
Willy Tarreau | 64bca59 | 2016-12-21 20:13:11 +0100 | [diff] [blame] | 304 | return err_code; |
| 305 | } |
| 306 | } |
| 307 | return 0; |
| 308 | } |
| 309 | |
Joseph Herlant | b35ea68 | 2018-11-15 12:24:23 -0800 | [diff] [blame] | 310 | /* Calls flt_init_per_thread() for all proxies, see above. Be careful here, it |
| 311 | * returns 0 if an error occurred. This is the opposite of flt_init_all. */ |
Christopher Faulet | 71a6a8e | 2017-07-27 16:33:28 +0200 | [diff] [blame] | 312 | static int |
| 313 | flt_init_all_per_thread() |
| 314 | { |
| 315 | struct proxy *px; |
| 316 | int err_code = 0; |
| 317 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 318 | for (px = proxies_list; px; px = px->next) { |
Christopher Faulet | 71a6a8e | 2017-07-27 16:33:28 +0200 | [diff] [blame] | 319 | err_code = flt_init_per_thread(px); |
| 320 | if (err_code & (ERR_ABORT|ERR_FATAL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 321 | ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n", |
| 322 | px->id, tid); |
Christopher Faulet | 71a6a8e | 2017-07-27 16:33:28 +0200 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | } |
| 326 | return 1; |
| 327 | } |
| 328 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 329 | /* |
| 330 | * Calls 'check' callback for all filters attached to a proxy. This happens |
| 331 | * after the configuration parsing but before filters initialization. Returns |
| 332 | * the number of encountered errors. |
| 333 | */ |
| 334 | int |
| 335 | flt_check(struct proxy *proxy) |
| 336 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 337 | struct flt_conf *fconf; |
| 338 | int err = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 339 | |
Christopher Faulet | ff17b18 | 2019-01-07 15:03:22 +0100 | [diff] [blame] | 340 | err += check_implicit_http_comp_flt(proxy); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 341 | list_for_each_entry(fconf, &proxy->filter_configs, list) { |
| 342 | if (fconf->ops->check) |
| 343 | err += fconf->ops->check(proxy, fconf); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 344 | } |
| 345 | return err; |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Calls 'denit' callback for all filters attached to a proxy. This happens when |
| 350 | * HAProxy is stopped. |
| 351 | */ |
| 352 | void |
| 353 | flt_deinit(struct proxy *proxy) |
| 354 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 355 | struct flt_conf *fconf, *back; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 356 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 357 | list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) { |
| 358 | if (fconf->ops->deinit) |
| 359 | fconf->ops->deinit(proxy, fconf); |
| 360 | LIST_DEL(&fconf->list); |
| 361 | free(fconf); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Christopher Faulet | 71a6a8e | 2017-07-27 16:33:28 +0200 | [diff] [blame] | 365 | /* |
| 366 | * Calls 'denit_per_thread' callback for all filters attached to a proxy for |
| 367 | * each threads. This happens before exiting a thread. |
| 368 | */ |
| 369 | void |
| 370 | flt_deinit_per_thread(struct proxy *proxy) |
| 371 | { |
| 372 | struct flt_conf *fconf, *back; |
| 373 | |
| 374 | list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) { |
| 375 | if (fconf->ops->deinit_per_thread) |
| 376 | fconf->ops->deinit_per_thread(proxy, fconf); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | |
| 381 | /* Calls flt_deinit_per_thread() for all proxies, see above */ |
| 382 | static void |
| 383 | flt_deinit_all_per_thread() |
| 384 | { |
| 385 | struct proxy *px; |
| 386 | |
Olivier Houchard | fbc74e8 | 2017-11-24 16:54:05 +0100 | [diff] [blame] | 387 | for (px = proxies_list; px; px = px->next) |
Christopher Faulet | 71a6a8e | 2017-07-27 16:33:28 +0200 | [diff] [blame] | 388 | flt_deinit_per_thread(px); |
| 389 | } |
| 390 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 391 | /* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */ |
| 392 | static int |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 393 | flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 394 | { |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 395 | struct filter *f; |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 396 | |
Christopher Faulet | 0f17a9b | 2019-04-05 10:11:38 +0200 | [diff] [blame] | 397 | if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX)) |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 398 | return 0; |
| 399 | |
| 400 | f = pool_alloc(pool_head_filter); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 401 | if (!f) /* not enough memory */ |
| 402 | return -1; |
| 403 | memset(f, 0, sizeof(*f)); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 404 | f->config = fconf; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 405 | f->flags |= flags; |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 406 | |
| 407 | if (FLT_OPS(f)->attach) { |
| 408 | int ret = FLT_OPS(f)->attach(s, f); |
| 409 | if (ret <= 0) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 410 | pool_free(pool_head_filter, f); |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 411 | return ret; |
| 412 | } |
| 413 | } |
| 414 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 415 | LIST_ADDQ(&strm_flt(s)->filters, &f->list); |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 416 | strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Called when a stream is created. It attaches all frontend filters to the |
| 422 | * stream. Returns -1 if an error occurs, 0 otherwise. |
| 423 | */ |
| 424 | int |
| 425 | flt_stream_init(struct stream *s) |
| 426 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 427 | struct flt_conf *fconf; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 428 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 429 | memset(strm_flt(s), 0, sizeof(*strm_flt(s))); |
| 430 | LIST_INIT(&strm_flt(s)->filters); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 431 | list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) { |
| 432 | if (flt_stream_add_filter(s, fconf, 0) < 0) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 433 | return -1; |
| 434 | } |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Called when a stream is closed or when analyze ends (For an HTTP stream, this |
| 440 | * happens after each request/response exchange). When analyze ends, backend |
| 441 | * filters are removed. When the stream is closed, all filters attached to the |
| 442 | * stream are removed. |
| 443 | */ |
| 444 | void |
| 445 | flt_stream_release(struct stream *s, int only_backend) |
| 446 | { |
| 447 | struct filter *filter, *back; |
| 448 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 449 | list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 450 | if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) { |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 451 | if (FLT_OPS(filter)->detach) |
| 452 | FLT_OPS(filter)->detach(s, filter); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 453 | LIST_DEL(&filter->list); |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 454 | pool_free(pool_head_filter, filter); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 455 | } |
| 456 | } |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 457 | if (LIST_ISEMPTY(&strm_flt(s)->filters)) |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 458 | strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 459 | } |
| 460 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 461 | /* |
| 462 | * Calls 'stream_start' for all filters attached to a stream. This happens when |
| 463 | * the stream is created, just after calling flt_stream_init |
| 464 | * function. Returns -1 if an error occurs, 0 otherwise. |
| 465 | */ |
| 466 | int |
| 467 | flt_stream_start(struct stream *s) |
| 468 | { |
| 469 | struct filter *filter; |
| 470 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 471 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 472 | if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 473 | return -1; |
| 474 | } |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Calls 'stream_stop' for all filters attached to a stream. This happens when |
| 480 | * the stream is stopped, just before calling flt_stream_release function. |
| 481 | */ |
| 482 | void |
| 483 | flt_stream_stop(struct stream *s) |
| 484 | { |
| 485 | struct filter *filter; |
| 486 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 487 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 488 | if (FLT_OPS(filter)->stream_stop) |
| 489 | FLT_OPS(filter)->stream_stop(s, filter); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 493 | /* |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 494 | * Calls 'check_timeouts' for all filters attached to a stream. This happens when |
| 495 | * the stream is woken up because of expired timer. |
| 496 | */ |
| 497 | void |
| 498 | flt_stream_check_timeouts(struct stream *s) |
| 499 | { |
| 500 | struct filter *filter; |
| 501 | |
| 502 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
| 503 | if (FLT_OPS(filter)->check_timeouts) |
| 504 | FLT_OPS(filter)->check_timeouts(s, filter); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 509 | * Called when a backend is set for a stream. If the frontend and the backend |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 510 | * are not the same, this function attaches all backend filters to the |
| 511 | * stream. Returns -1 if an error occurs, 0 otherwise. |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 512 | */ |
| 513 | int |
| 514 | flt_set_stream_backend(struct stream *s, struct proxy *be) |
| 515 | { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 516 | struct flt_conf *fconf; |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 517 | struct filter *filter; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 518 | |
| 519 | if (strm_fe(s) == be) |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 520 | goto end; |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 521 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 522 | list_for_each_entry(fconf, &be->filter_configs, list) { |
| 523 | if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0) |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 524 | return -1; |
| 525 | } |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 526 | |
| 527 | end: |
| 528 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
| 529 | if (FLT_OPS(filter)->stream_set_backend && |
| 530 | FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0) |
| 531 | return -1; |
| 532 | } |
| 533 | |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | * Calls 'http_end' callback for all filters attached to a stream. All filters |
| 540 | * are called here, but only if there is at least one "data" filter. This |
| 541 | * functions is called when all data were parsed and forwarded. 'http_end' |
| 542 | * callback is resumable, so this function returns a negative value if an error |
| 543 | * occurs, 0 if it needs to wait for some reason, any other value otherwise. |
| 544 | */ |
| 545 | int |
| 546 | flt_http_end(struct stream *s, struct http_msg *msg) |
| 547 | { |
| 548 | int ret = 1; |
| 549 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 550 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 551 | RESUME_FILTER_LOOP(s, msg->chn) { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 552 | if (FLT_OPS(filter)->http_end) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 553 | DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 554 | ret = FLT_OPS(filter)->http_end(s, filter, msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 555 | if (ret <= 0) |
| 556 | BREAK_EXECUTION(s, msg->chn, end); |
| 557 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 558 | } RESUME_FILTER_END; |
| 559 | end: |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 560 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 561 | return ret; |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * Calls 'http_reset' callback for all filters attached to a stream. This |
| 566 | * happens when a 100-continue response is received. |
| 567 | */ |
| 568 | void |
| 569 | flt_http_reset(struct stream *s, struct http_msg *msg) |
| 570 | { |
| 571 | struct filter *filter; |
| 572 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 573 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg); |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 574 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 575 | if (FLT_OPS(filter)->http_reset) { |
| 576 | DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 577 | FLT_OPS(filter)->http_reset(s, filter, msg); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 578 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 579 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 580 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Calls 'http_reply' callback for all filters attached to a stream when HA |
| 585 | * decides to stop the HTTP message processing. |
| 586 | */ |
| 587 | void |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 588 | flt_http_reply(struct stream *s, short status, const struct buffer *msg) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 589 | { |
| 590 | struct filter *filter; |
| 591 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 592 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg); |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 593 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 594 | if (FLT_OPS(filter)->http_reply) { |
| 595 | DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 596 | FLT_OPS(filter)->http_reply(s, filter, status, msg); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 597 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 598 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 599 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | /* |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 603 | * Calls 'http_payload' callback for all "data" filters attached to a |
| 604 | * stream. This function is called when some data can be forwarded in the |
| 605 | * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to |
| 606 | * update the filters and the stream offset to be sure that a filter cannot |
| 607 | * forward more data than its predecessors. A filter can choose to not forward |
| 608 | * all data. Returns a negative value if an error occurs, else the number of |
| 609 | * forwarded bytes. |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 610 | */ |
| 611 | int |
| 612 | flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len) |
| 613 | { |
| 614 | struct filter *filter; |
| 615 | unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn); |
Christopher Faulet | 421e769 | 2019-06-13 11:16:45 +0200 | [diff] [blame] | 616 | unsigned int out = co_data(msg->chn); |
Christopher Faulet | 81340d7 | 2020-02-26 15:47:22 +0100 | [diff] [blame] | 617 | int ret, data; |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 618 | |
Christopher Faulet | 81340d7 | 2020-02-26 15:47:22 +0100 | [diff] [blame] | 619 | ret = data = len - out; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 620 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg); |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 621 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
| 622 | /* Call "data" filters only */ |
| 623 | if (!IS_DATA_FILTER(filter, msg->chn)) |
| 624 | continue; |
| 625 | if (FLT_OPS(filter)->http_payload) { |
| 626 | unsigned long long *flt_off = &FLT_OFF(filter, msg->chn); |
| 627 | unsigned int offset = *flt_off - *strm_off; |
| 628 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 629 | DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | 71179a3 | 2020-02-07 16:40:33 +0100 | [diff] [blame] | 630 | ret = FLT_OPS(filter)->http_payload(s, filter, msg, out + offset, data - offset); |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 631 | if (ret < 0) |
| 632 | goto end; |
Christopher Faulet | c50ee0b | 2020-02-24 16:20:09 +0100 | [diff] [blame] | 633 | data = ret + *flt_off - *strm_off; |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 634 | *flt_off += ret; |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 635 | } |
| 636 | } |
Christopher Faulet | 71179a3 | 2020-02-07 16:40:33 +0100 | [diff] [blame] | 637 | |
| 638 | /* Only forward data if the last filter decides to forward something */ |
| 639 | if (ret > 0) { |
| 640 | ret = data; |
| 641 | *strm_off += ret; |
| 642 | } |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 643 | end: |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 644 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | /* |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 649 | * Calls 'channel_start_analyze' callback for all filters attached to a |
| 650 | * stream. This function is called when we start to analyze a request or a |
| 651 | * response. For frontend filters, it is called before all other analyzers. For |
| 652 | * backend ones, it is called before all backend |
| 653 | * analyzers. 'channel_start_analyze' callback is resumable, so this function |
| 654 | * returns 0 if an error occurs or if it needs to wait, any other value |
| 655 | * otherwise. |
| 656 | */ |
| 657 | int |
| 658 | flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 659 | { |
| 660 | int ret = 1; |
| 661 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 662 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s); |
| 663 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 664 | /* If this function is called, this means there is at least one filter, |
| 665 | * so we do not need to check the filter list's emptiness. */ |
| 666 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 667 | /* Set flag on channel to tell that the channel is filtered */ |
| 668 | chn->flags |= CF_FLT_ANALYZE; |
| 669 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 670 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 671 | if (!(chn->flags & CF_ISRESP)) { |
| 672 | if (an_bit == AN_REQ_FLT_START_BE && |
| 673 | !(filter->flags & FLT_FL_IS_BACKEND_FILTER)) |
| 674 | continue; |
| 675 | } |
| 676 | else { |
| 677 | if (an_bit == AN_RES_FLT_START_BE && |
| 678 | !(filter->flags & FLT_FL_IS_BACKEND_FILTER)) |
| 679 | continue; |
| 680 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 681 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 682 | FLT_OFF(filter, chn) = 0; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 683 | if (FLT_OPS(filter)->channel_start_analyze) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 684 | DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 685 | ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 686 | if (ret <= 0) |
| 687 | BREAK_EXECUTION(s, chn, end); |
| 688 | } |
| 689 | } RESUME_FILTER_END; |
| 690 | |
| 691 | end: |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 692 | ret = handle_analyzer_result(s, chn, an_bit, ret); |
| 693 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s); |
| 694 | return ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /* |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 698 | * Calls 'channel_pre_analyze' callback for all filters attached to a |
| 699 | * stream. This function is called BEFORE each analyzer attached to a channel, |
| 700 | * expects analyzers responsible for data sending. 'channel_pre_analyze' |
| 701 | * callback is resumable, so this function returns 0 if an error occurs or if it |
| 702 | * needs to wait, any other value otherwise. |
| 703 | * |
| 704 | * Note this function can be called many times for the same analyzer. In fact, |
| 705 | * it is called until the analyzer finishes its processing. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 706 | */ |
| 707 | int |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 708 | flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 709 | { |
| 710 | int ret = 1; |
| 711 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 712 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s); |
| 713 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 714 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 715 | if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 716 | DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s); |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 717 | ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 718 | if (ret <= 0) |
| 719 | BREAK_EXECUTION(s, chn, check_result); |
| 720 | } |
| 721 | } RESUME_FILTER_END; |
| 722 | |
| 723 | check_result: |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 724 | ret = handle_analyzer_result(s, chn, 0, ret); |
| 725 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s); |
| 726 | return ret; |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /* |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 730 | * Calls 'channel_post_analyze' callback for all filters attached to a |
| 731 | * stream. This function is called AFTER each analyzer attached to a channel, |
| 732 | * expects analyzers responsible for data sending. 'channel_post_analyze' |
| 733 | * callback is NOT resumable, so this function returns a 0 if an error occurs, |
| 734 | * any other value otherwise. |
| 735 | * |
| 736 | * Here, AFTER means when the analyzer finishes its processing. |
| 737 | */ |
| 738 | int |
| 739 | flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 740 | { |
| 741 | struct filter *filter; |
| 742 | int ret = 1; |
| 743 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 744 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s); |
| 745 | |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 746 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
| 747 | if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 748 | DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s); |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 749 | ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit); |
| 750 | if (ret < 0) |
| 751 | break; |
| 752 | } |
| 753 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 754 | ret = handle_analyzer_result(s, chn, 0, ret); |
| 755 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s); |
| 756 | return ret; |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 760 | * This function is the AN_REQ/RES_FLT_HTTP_HDRS analyzer, used to filter HTTP |
| 761 | * headers or a request or a response. Returns 0 if an error occurs or if it |
| 762 | * needs to wait, any other value otherwise. |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 763 | */ |
| 764 | int |
| 765 | flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 766 | { |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 767 | struct http_msg *msg; |
| 768 | int ret = 1; |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 769 | |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 770 | msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 771 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg); |
| 772 | |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 773 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 774 | if (FLT_OPS(filter)->http_headers) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 775 | DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 776 | ret = FLT_OPS(filter)->http_headers(s, filter, msg); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 777 | if (ret <= 0) |
| 778 | BREAK_EXECUTION(s, chn, check_result); |
| 779 | } |
| 780 | } RESUME_FILTER_END; |
Christopher Faulet | 9c44e48 | 2020-02-12 15:31:20 +0100 | [diff] [blame] | 781 | |
| 782 | if (HAS_DATA_FILTERS(s, chn)) { |
| 783 | size_t data = http_get_hdrs_size(htxbuf(&chn->buf)); |
| 784 | struct filter *f; |
| 785 | |
| 786 | list_for_each_entry(f, &strm_flt(s)->filters, list) { |
| 787 | if (IS_DATA_FILTER(f, chn)) |
| 788 | FLT_OFF(f, chn) = data; |
| 789 | } |
| 790 | } |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 791 | |
| 792 | check_result: |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 793 | ret = handle_analyzer_result(s, chn, an_bit, ret); |
| 794 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s); |
| 795 | return ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /* |
| 799 | * Calls 'channel_end_analyze' callback for all filters attached to a |
| 800 | * stream. This function is called when we stop to analyze a request or a |
| 801 | * response. It is called after all other analyzers. 'channel_end_analyze' |
| 802 | * callback is resumable, so this function returns 0 if an error occurs or if it |
| 803 | * needs to wait, any other value otherwise. |
| 804 | */ |
| 805 | int |
| 806 | flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 807 | { |
| 808 | int ret = 1; |
| 809 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 810 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s); |
| 811 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 812 | /* Check if all filters attached on the stream have finished their |
| 813 | * processing on this channel. */ |
| 814 | if (!(chn->flags & CF_FLT_ANALYZE)) |
| 815 | goto sync; |
| 816 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 817 | RESUME_FILTER_LOOP(s, chn) { |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 818 | FLT_OFF(filter, chn) = 0; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 819 | unregister_data_filter(s, chn, filter); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 820 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 821 | if (FLT_OPS(filter)->channel_end_analyze) { |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 822 | DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s); |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 823 | ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 824 | if (ret <= 0) |
| 825 | BREAK_EXECUTION(s, chn, end); |
| 826 | } |
| 827 | } RESUME_FILTER_END; |
| 828 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 829 | end: |
| 830 | /* We don't remove yet this analyzer because we need to synchronize the |
| 831 | * both channels. So here, we just remove the flag CF_FLT_ANALYZE. */ |
| 832 | ret = handle_analyzer_result(s, chn, 0, ret); |
Christopher Faulet | 570f799 | 2017-07-06 15:53:02 +0200 | [diff] [blame] | 833 | if (ret) { |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 834 | chn->flags &= ~CF_FLT_ANALYZE; |
Christopher Faulet | 02c7b22 | 2015-12-22 12:01:29 +0100 | [diff] [blame] | 835 | |
Christopher Faulet | 570f799 | 2017-07-06 15:53:02 +0200 | [diff] [blame] | 836 | /* Pretend there is an activity on both channels. Flag on the |
| 837 | * current one will be automatically removed, so only the other |
| 838 | * one will remain. This is a way to be sure that |
| 839 | * 'channel_end_analyze' callback will have a chance to be |
| 840 | * called at least once for the other side to finish the current |
Joseph Herlant | b35ea68 | 2018-11-15 12:24:23 -0800 | [diff] [blame] | 841 | * processing. Of course, this is the filter responsibility to |
Christopher Faulet | 570f799 | 2017-07-06 15:53:02 +0200 | [diff] [blame] | 842 | * wakeup the stream if it choose to loop on this callback. */ |
| 843 | s->req.flags |= CF_WAKE_ONCE; |
| 844 | s->res.flags |= CF_WAKE_ONCE; |
| 845 | } |
| 846 | |
| 847 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 848 | sync: |
| 849 | /* Now we can check if filters have finished their work on the both |
| 850 | * channels */ |
| 851 | if (!(s->req.flags & CF_FLT_ANALYZE) && !(s->res.flags & CF_FLT_ANALYZE)) { |
| 852 | /* Sync channels by removing this analyzer for the both channels */ |
| 853 | s->req.analysers &= ~AN_REQ_FLT_END; |
| 854 | s->res.analysers &= ~AN_RES_FLT_END; |
Christopher Faulet | c6062be | 2016-10-31 11:22:37 +0100 | [diff] [blame] | 855 | |
Christopher Faulet | e600624 | 2017-03-10 11:52:44 +0100 | [diff] [blame] | 856 | /* Remove backend filters from the list */ |
| 857 | flt_stream_release(s, 1); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 858 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 859 | } |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 860 | else { |
| 861 | DBG_TRACE_DEVEL("waiting for sync", STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s); |
| 862 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 863 | return ret; |
| 864 | } |
| 865 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 866 | |
| 867 | /* |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 868 | * Calls 'tcp_payload' callback for all "data" filters attached to a |
| 869 | * stream. This function is called when some data can be forwarded in the |
| 870 | * AN_REQ_FLT_XFER_BODY and AN_RES_FLT_XFER_BODY analyzers. It takes care to |
| 871 | * update the filters and the stream offset to be sure that a filter cannot |
| 872 | * forward more data than its predecessors. A filter can choose to not forward |
| 873 | * all data. Returns a negative value if an error occurs, else the number of |
| 874 | * forwarded bytes. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 875 | */ |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 876 | int |
| 877 | flt_tcp_payload(struct stream *s, struct channel *chn, unsigned int len) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 878 | { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 879 | struct filter *filter; |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 880 | unsigned long long *strm_off = &FLT_STRM_OFF(s, chn); |
| 881 | unsigned int out = co_data(chn); |
Christopher Faulet | 81340d7 | 2020-02-26 15:47:22 +0100 | [diff] [blame] | 882 | int ret, data; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 883 | |
Christopher Faulet | 81340d7 | 2020-02-26 15:47:22 +0100 | [diff] [blame] | 884 | ret = data = len - out; |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 885 | DBG_TRACE_ENTER(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 886 | list_for_each_entry(filter, &strm_flt(s)->filters, list) { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 887 | /* Call "data" filters only */ |
| 888 | if (!IS_DATA_FILTER(filter, chn)) |
| 889 | continue; |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 890 | if (FLT_OPS(filter)->tcp_payload) { |
| 891 | unsigned long long *flt_off = &FLT_OFF(filter, chn); |
| 892 | unsigned int offset = *flt_off - *strm_off; |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 893 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 894 | DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | 71179a3 | 2020-02-07 16:40:33 +0100 | [diff] [blame] | 895 | ret = FLT_OPS(filter)->tcp_payload(s, filter, chn, out + offset, data - offset); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 896 | if (ret < 0) |
| 897 | goto end; |
Christopher Faulet | c50ee0b | 2020-02-24 16:20:09 +0100 | [diff] [blame] | 898 | data = ret + *flt_off - *strm_off; |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 899 | *flt_off += ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 900 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 901 | } |
Christopher Faulet | 71179a3 | 2020-02-07 16:40:33 +0100 | [diff] [blame] | 902 | |
| 903 | /* Only forward data if the last filter decides to forward something */ |
| 904 | if (ret > 0) { |
| 905 | ret = data; |
| 906 | *strm_off += ret; |
| 907 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 908 | end: |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 909 | DBG_TRACE_LEAVE(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 910 | return ret; |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * Called when TCP data must be filtered on a channel. This function is the |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 915 | * AN_REQ/RES_FLT_XFER_DATA analyzer. When called, it is responsible to forward |
| 916 | * data when the proxy is not in http mode. Behind the scene, it calls |
| 917 | * consecutively 'tcp_data' and 'tcp_forward_data' callbacks for all "data" |
| 918 | * filters attached to a stream. Returns 0 if an error occurs or if it needs to |
| 919 | * wait, any other value otherwise. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 920 | */ |
| 921 | int |
| 922 | flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit) |
| 923 | { |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 924 | unsigned int len; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 925 | int ret = 1; |
| 926 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 927 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s); |
| 928 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 929 | /* If there is no "data" filters, we do nothing */ |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 930 | if (!HAS_DATA_FILTERS(s, chn)) |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 931 | goto end; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 932 | |
| 933 | /* Be sure that the output is still opened. Else we stop the data |
| 934 | * filtering. */ |
| 935 | if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) || |
Willy Tarreau | 44a41a8 | 2018-06-19 07:16:31 +0200 | [diff] [blame] | 936 | ((chn->flags & CF_SHUTW) && (chn->to_forward || co_data(chn)))) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 937 | goto end; |
| 938 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 939 | if (s->flags & SF_HTX) { |
| 940 | struct htx *htx = htxbuf(&chn->buf); |
| 941 | len = htx->data; |
| 942 | } |
| 943 | else |
| 944 | len = c_data(chn); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 945 | |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 946 | ret = flt_tcp_payload(s, chn, len); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 947 | if (ret < 0) |
| 948 | goto end; |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 949 | c_adv(chn, ret); |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 950 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 951 | /* Stop waiting data if the input in closed and no data is pending or if |
| 952 | * the output is closed. */ |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 953 | if (chn->flags & CF_SHUTW) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 954 | ret = 1; |
| 955 | goto end; |
| 956 | } |
Christopher Faulet | b2e5849 | 2019-11-12 11:13:01 +0100 | [diff] [blame] | 957 | if (chn->flags & CF_SHUTR) { |
| 958 | if (((s->flags & SF_HTX) && htx_is_empty(htxbuf(&chn->buf))) || c_empty(chn)) { |
| 959 | ret = 1; |
| 960 | goto end; |
| 961 | } |
| 962 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 963 | |
| 964 | /* Wait for data */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 965 | DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 966 | return 0; |
| 967 | end: |
| 968 | /* Terminate the data filtering. If <ret> is negative, an error was |
| 969 | * encountered during the filtering. */ |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 970 | ret = handle_analyzer_result(s, chn, an_bit, ret); |
| 971 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s); |
| 972 | return ret; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Handles result of filter's analyzers. It returns 0 if an error occurs or if |
| 977 | * it needs to wait, any other value otherwise. |
| 978 | */ |
| 979 | static int |
| 980 | handle_analyzer_result(struct stream *s, struct channel *chn, |
| 981 | unsigned int an_bit, int ret) |
| 982 | { |
| 983 | int finst; |
Christopher Faulet | e058f73 | 2019-09-06 15:24:55 +0200 | [diff] [blame] | 984 | int status = 0; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 985 | |
| 986 | if (ret < 0) |
| 987 | goto return_bad_req; |
| 988 | else if (!ret) |
| 989 | goto wait; |
| 990 | |
| 991 | /* End of job, return OK */ |
| 992 | if (an_bit) { |
| 993 | chn->analysers &= ~an_bit; |
| 994 | chn->analyse_exp = TICK_ETERNITY; |
| 995 | } |
| 996 | return 1; |
| 997 | |
| 998 | return_bad_req: |
| 999 | /* An error occurs */ |
| 1000 | channel_abort(&s->req); |
| 1001 | channel_abort(&s->res); |
| 1002 | |
| 1003 | if (!(chn->flags & CF_ISRESP)) { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1004 | s->req.analysers &= AN_REQ_FLT_END; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1005 | finst = SF_FINST_R; |
Christopher Faulet | e058f73 | 2019-09-06 15:24:55 +0200 | [diff] [blame] | 1006 | status = 400; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1007 | /* FIXME: incr counters */ |
| 1008 | } |
| 1009 | else { |
Christopher Faulet | 0184ea7 | 2017-01-05 14:06:34 +0100 | [diff] [blame] | 1010 | s->res.analysers &= AN_RES_FLT_END; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1011 | finst = SF_FINST_H; |
Christopher Faulet | e058f73 | 2019-09-06 15:24:55 +0200 | [diff] [blame] | 1012 | status = 502; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1013 | /* FIXME: incr counters */ |
| 1014 | } |
| 1015 | |
Christopher Faulet | 3d11969 | 2019-07-15 22:04:51 +0200 | [diff] [blame] | 1016 | if (IS_HTX_STRM(s)) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1017 | /* Do not do that when we are waiting for the next request */ |
Christopher Faulet | e058f73 | 2019-09-06 15:24:55 +0200 | [diff] [blame] | 1018 | if (s->txn->status > 0) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 1019 | http_reply_and_close(s, s->txn->status, NULL); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1020 | else { |
Christopher Faulet | e058f73 | 2019-09-06 15:24:55 +0200 | [diff] [blame] | 1021 | s->txn->status = status; |
| 1022 | http_reply_and_close(s, status, http_error_message(s)); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | if (!(s->flags & SF_ERR_MASK)) |
| 1027 | s->flags |= SF_ERR_PRXCOND; |
| 1028 | if (!(s->flags & SF_FINST_MASK)) |
| 1029 | s->flags |= finst; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1030 | DBG_TRACE_DEVEL("leaving on error", STRM_EV_FLT_ANA|STRM_EV_FLT_ERR, s); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1031 | return 0; |
| 1032 | |
| 1033 | wait: |
| 1034 | if (!(chn->flags & CF_ISRESP)) |
| 1035 | channel_dont_connect(chn); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 1036 | DBG_TRACE_DEVEL("wairing for more data", STRM_EV_FLT_ANA, s); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | |
| 1041 | /* Note: must not be declared <const> as its list will be overwritten. |
| 1042 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 1043 | * all code contributors. |
| 1044 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 1045 | * the config parser can report an appropriate error when a known keyword was |
| 1046 | * not enabled. */ |
| 1047 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1048 | { CFG_LISTEN, "filter", parse_filter }, |
| 1049 | { 0, NULL, NULL }, |
| 1050 | } |
| 1051 | }; |
| 1052 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1053 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 1054 | |
Willy Tarreau | 172f5ce | 2018-11-26 11:21:50 +0100 | [diff] [blame] | 1055 | REGISTER_POST_CHECK(flt_init_all); |
| 1056 | REGISTER_PER_THREAD_INIT(flt_init_all_per_thread); |
| 1057 | REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread); |
| 1058 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1059 | /* |
| 1060 | * Local variables: |
| 1061 | * c-indent-level: 8 |
| 1062 | * c-basic-offset: 8 |
| 1063 | * End: |
| 1064 | */ |