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