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