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