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