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