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