blob: a66cfba40137bfeb9c0b00bf5ae68b39f4be87ad [file] [log] [blame]
Christopher Fauletd7c91962015-04-30 11:48:27 +02001/*
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 Tarreau4c7e4b72020-05-27 12:58:42 +020013#include <haproxy/api.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020014#include <haproxy/buf-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020016#include <haproxy/compression.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020018#include <haproxy/filters.h>
Willy Tarreau7d865a52020-06-04 10:57:05 +020019#include <haproxy/flt_http_comp.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020020#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020021#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020022#include <haproxy/htx.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020023#include <haproxy/namespace.h>
Willy Tarreaudaa6f1a2021-05-08 20:22:17 +020024#include <haproxy/proxy.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020025#include <haproxy/stream.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020026#include <haproxy/tools.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/trace.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020028
Christopher Fauletd7c91962015-04-30 11:48:27 +020029
Christopher Fauleteea8fc72019-11-05 16:18:10 +010030#define TRACE_SOURCE &trace_strm
31
Christopher Fauletd7c91962015-04-30 11:48:27 +020032/* Pool used to allocate filters */
Willy Tarreau8ceae722018-11-26 11:58:30 +010033DECLARE_STATIC_POOL(pool_head_filter, "filter", sizeof(struct filter));
Christopher Fauletd7c91962015-04-30 11:48:27 +020034
35static 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 Jacquin874a35c2018-09-10 21:26:07 +010043 * Here is an example:
Christopher Fauletd7c91962015-04-30 11:48:27 +020044 *
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 Fauletda02e172015-12-04 09:25:05 +010060 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 Fauletd7c91962015-04-30 11:48:27 +020063 goto resume_execution; \
64 } \
65 \
Christopher Fauletfcf035c2015-12-03 11:48:03 +010066 list_for_each_entry(filter, &strm_flt(s)->filters, list) { \
Christopher Fauletda02e172015-12-04 09:25:05 +010067 resume_execution:
Christopher Fauletd7c91962015-04-30 11:48:27 +020068
69#define RESUME_FILTER_END \
70 } \
71 } while(0)
72
Christopher Fauletda02e172015-12-04 09:25:05 +010073#define BREAK_EXECUTION(strm, chn, label) \
74 do { \
75 strm_flt(strm)->current[CHN_IDX(chn)] = filter; \
76 goto label; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020077 } while (0)
78
79
80/* List head of all known filter keywords */
81static 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 */
89void
90flt_register_keywords(struct flt_kw_list *kwl)
91{
Willy Tarreau2b718102021-04-21 07:32:39 +020092 LIST_APPEND(&flt_keywords.list, &kwl->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +020093}
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 */
103struct flt_kw *
104flt_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 Tarreau3b65e142022-03-29 15:03:09 +0200132 * If <out> is NULL, the output is emitted using a more compact format on stdout.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200133 */
134void
135flt_dump_kws(char **out)
136{
137 struct flt_kw_list *kwl;
Willy Tarreau0f996372022-03-30 12:08:00 +0200138 const struct flt_kw *kwp, *kw;
139 const char *scope = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200140 int index;
141
Willy Tarreau3b65e142022-03-29 15:03:09 +0200142 if (out)
143 *out = NULL;
Willy Tarreau0f996372022-03-30 12:08:00 +0200144
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 Fauletd7c91962015-04-30 11:48:27 +0200156 }
157 }
Willy Tarreau0f996372022-03-30 12:08:00 +0200158
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 Fauletd7c91962015-04-30 11:48:27 +0200170 }
171}
172
173/*
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100174 * Lists the known filters on <out>
175 */
176void
177list_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 Fauletd7c91962015-04-30 11:48:27 +0200189 * Parses the "filter" keyword. All keywords must be handled by filters
190 * themselves
191 */
192static int
193parse_filter(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100194 const struct proxy *defpx, const char *file, int line, char **err)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200195{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100196 struct flt_conf *fconf = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200197
198 /* Filter cannot be defined on a default proxy */
199 if (curpx == defpx) {
Christopher Fauletcc7317d2016-04-04 10:51:17 +0200200 memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.",
Christopher Fauletd7c91962015-04-30 11:48:27 +0200201 file, line, args[0]);
202 return -1;
203 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100204 if (strcmp(args[0], "filter") == 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200205 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 Faulet443ea1a2016-02-04 13:40:26 +0100214 fconf = calloc(1, sizeof(*fconf));
215 if (!fconf) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200216 memprintf(err, "'%s' : out of memory", args[0]);
217 goto error;
218 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200219
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 Fournier3610c392016-04-13 18:27:51 +0200229 if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200230 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 Faulet00e818a2016-04-19 17:00:44 +0200252 if (fconf->ops == NULL) {
253 memprintf(err, "'%s %s' : no callbacks defined.",
254 args[0], args[1]);
255 goto error;
256 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200257
Willy Tarreau2b718102021-04-21 07:32:39 +0200258 LIST_APPEND(&curpx->filter_configs, &fconf->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200259 }
260 return 0;
261
262 error:
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100263 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200264 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 Tarreau64bca592016-12-21 20:13:11 +0100274static int
Christopher Fauletd7c91962015-04-30 11:48:27 +0200275flt_init(struct proxy *proxy)
276{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100277 struct flt_conf *fconf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200278
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100279 list_for_each_entry(fconf, &proxy->filter_configs, list) {
280 if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200281 return ERR_ALERT|ERR_FATAL;
282 }
283 return 0;
284}
285
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200286/*
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 */
291static int
292flt_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 Tarreau64bca592016-12-21 20:13:11 +0100303/* Calls flt_init() for all proxies, see above */
304static int
305flt_init_all()
306{
307 struct proxy *px;
Christopher Fauletfc633b62020-11-06 15:24:23 +0100308 int err_code = ERR_NONE;
Willy Tarreau64bca592016-12-21 20:13:11 +0100309
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100310 for (px = proxies_list; px; px = px->next) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200311 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
Christopher Faulet400829c2020-11-02 16:08:09 +0100312 flt_deinit(px);
313 continue;
314 }
Willy Tarreau64bca592016-12-21 20:13:11 +0100315 err_code |= flt_init(px);
316 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100317 ha_alert("Failed to initialize filters for proxy '%s'.\n",
318 px->id);
Willy Tarreau64bca592016-12-21 20:13:11 +0100319 return err_code;
320 }
321 }
322 return 0;
323}
324
Joseph Herlantb35ea682018-11-15 12:24:23 -0800325/* 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 Faulet71a6a8e2017-07-27 16:33:28 +0200327static int
328flt_init_all_per_thread()
329{
330 struct proxy *px;
331 int err_code = 0;
332
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100333 for (px = proxies_list; px; px = px->next) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200334 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
Christopher Faulet400829c2020-11-02 16:08:09 +0100335 continue;
336
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200337 err_code = flt_init_per_thread(px);
338 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100339 ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n",
340 px->id, tid);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200341 return 0;
342 }
343 }
344 return 1;
345}
346
Christopher Fauletd7c91962015-04-30 11:48:27 +0200347/*
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 */
352int
353flt_check(struct proxy *proxy)
354{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100355 struct flt_conf *fconf;
356 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200357
Christopher Fauletff17b182019-01-07 15:03:22 +0100358 err += check_implicit_http_comp_flt(proxy);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100359 list_for_each_entry(fconf, &proxy->filter_configs, list) {
360 if (fconf->ops->check)
361 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200362 }
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 */
370void
371flt_deinit(struct proxy *proxy)
372{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100373 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200374
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100375 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
Christopher Faulet743bd6a2020-11-03 16:40:37 +0100376 if (fconf->ops->deinit)
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100377 fconf->ops->deinit(proxy, fconf);
Willy Tarreau2b718102021-04-21 07:32:39 +0200378 LIST_DELETE(&fconf->list);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100379 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200380 }
381}
382
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200383/*
384 * Calls 'denit_per_thread' callback for all filters attached to a proxy for
385 * each threads. This happens before exiting a thread.
386 */
387void
388flt_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 */
400static void
401flt_deinit_all_per_thread()
402{
403 struct proxy *px;
404
Christopher Faulet743bd6a2020-11-03 16:40:37 +0100405 for (px = proxies_list; px; px = px->next)
406 flt_deinit_per_thread(px);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200407}
408
Christopher Faulet92d36382015-11-05 13:35:03 +0100409/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
410static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100411flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100412{
Christopher Faulet75bc9132018-11-30 15:18:09 +0100413 struct filter *f;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200414
Christopher Faulet0f17a9b2019-04-05 10:11:38 +0200415 if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX))
Christopher Faulet75bc9132018-11-30 15:18:09 +0100416 return 0;
417
Willy Tarreau1bbec382021-03-22 21:02:50 +0100418 f = pool_zalloc(pool_head_filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100419 if (!f) /* not enough memory */
420 return -1;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100421 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100422 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200423
424 if (FLT_OPS(f)->attach) {
425 int ret = FLT_OPS(f)->attach(s, f);
426 if (ret <= 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100427 pool_free(pool_head_filter, f);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200428 return ret;
429 }
430 }
431
Willy Tarreau2b718102021-04-21 07:32:39 +0200432 LIST_APPEND(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100433 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100434 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 */
441int
442flt_stream_init(struct stream *s)
443{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100444 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100445
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100446 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
447 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100448 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
449 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100450 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 */
461void
462flt_stream_release(struct stream *s, int only_backend)
463{
464 struct filter *filter, *back;
465
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100466 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100467 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200468 if (FLT_OPS(filter)->detach)
469 FLT_OPS(filter)->detach(s, filter);
Willy Tarreau2b718102021-04-21 07:32:39 +0200470 LIST_DELETE(&filter->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100471 pool_free(pool_head_filter, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100472 }
473 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100474 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100475 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100476}
477
Christopher Fauletd7c91962015-04-30 11:48:27 +0200478/*
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 */
483int
484flt_stream_start(struct stream *s)
485{
486 struct filter *filter;
487
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100488 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100489 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200490 return -1;
491 }
Willy Tarreau7866e8e2023-01-12 18:39:42 +0100492 if (strm_li(s) && (strm_li(s)->bind_conf->analysers & AN_REQ_FLT_START_FE)) {
Christopher Faulet5647fba2021-03-08 13:40:30 +0100493 s->req.flags |= CF_FLT_ANALYZE;
Christopher Fauletd28b2b22021-10-04 07:50:13 +0200494 s->req.analysers |= AN_REQ_FLT_END;
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200495 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200496 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 */
503void
504flt_stream_stop(struct stream *s)
505{
506 struct filter *filter;
507
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100508 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100509 if (FLT_OPS(filter)->stream_stop)
510 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200511 }
512}
513
Christopher Faulet92d36382015-11-05 13:35:03 +0100514/*
Christopher Fauleta00d8172016-11-10 14:58:05 +0100515 * Calls 'check_timeouts' for all filters attached to a stream. This happens when
516 * the stream is woken up because of expired timer.
517 */
518void
519flt_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 Faulet92d36382015-11-05 13:35:03 +0100530 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200531 * are not the same, this function attaches all backend filters to the
532 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100533 */
534int
535flt_set_stream_backend(struct stream *s, struct proxy *be)
536{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100537 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200538 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100539
540 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200541 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100542
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100543 list_for_each_entry(fconf, &be->filter_configs, list) {
544 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100545 return -1;
546 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200547
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 Faulet26eb5ea2021-08-13 14:00:46 +0200554 if (be->be_req_ana & AN_REQ_FLT_START_BE) {
Christopher Faulet5647fba2021-03-08 13:40:30 +0100555 s->req.flags |= CF_FLT_ANALYZE;
Christopher Faulet949b6ca2021-09-10 10:29:54 +0200556 s->req.analysers |= AN_REQ_FLT_END;
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200557 }
558 if ((strm_fe(s)->fe_rsp_ana | be->be_rsp_ana) & (AN_RES_FLT_START_FE|AN_RES_FLT_START_BE)) {
Christopher Faulet5647fba2021-03-08 13:40:30 +0100559 s->res.flags |= CF_FLT_ANALYZE;
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200560 s->res.analysers |= AN_RES_FLT_END;
561 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200562
Christopher Faulet92d36382015-11-05 13:35:03 +0100563 return 0;
564}
565
Christopher Fauletd7c91962015-04-30 11:48:27 +0200566
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 */
574int
575flt_http_end(struct stream *s, struct http_msg *msg)
576{
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100577 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
578 unsigned int offset = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200579 int ret = 1;
580
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100581 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200582 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100583 unsigned long long flt_off = FLT_OFF(filter, msg->chn);
584 offset = flt_off - *strm_off;
585
Christopher Faulet401e6db2020-11-24 09:49:01 +0100586 /* 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 Faulet443ea1a2016-02-04 13:40:26 +0100592 if (FLT_OPS(filter)->http_end) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100593 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100594 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200595 if (ret <= 0)
596 BREAK_EXECUTION(s, msg->chn, end);
597 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200598 } RESUME_FILTER_END;
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100599
600 c_adv(msg->chn, offset);
601 *strm_off += offset;
602
Christopher Fauletd7c91962015-04-30 11:48:27 +0200603end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100604 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200605 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 */
612void
613flt_http_reset(struct stream *s, struct http_msg *msg)
614{
615 struct filter *filter;
616
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100617 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100618 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100619 if (FLT_OPS(filter)->http_reset) {
620 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100621 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100622 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200623 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100624 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200625}
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 */
631void
Willy Tarreau83061a82018-07-13 11:56:34 +0200632flt_http_reply(struct stream *s, short status, const struct buffer *msg)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200633{
634 struct filter *filter;
635
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100636 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100637 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100638 if (FLT_OPS(filter)->http_reply) {
639 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100640 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100641 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200642 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100643 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200644}
645
646/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100647 * 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 Faulet75bc9132018-11-30 15:18:09 +0100654 */
655int
656flt_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 Faulet421e7692019-06-13 11:16:45 +0200660 unsigned int out = co_data(msg->chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100661 int ret, data;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100662
Christopher Faulet6071c2d2021-01-25 12:02:00 +0100663 strm_flt(s)->flags &= ~STRM_FLT_FL_HOLD_HTTP_HDRS;
664
Christopher Faulet81340d72020-02-26 15:47:22 +0100665 ret = data = len - out;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100666 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100667 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet401e6db2020-11-24 09:49:01 +0100668 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 Faulet75bc9132018-11-30 15:18:09 +0100676 continue;
Christopher Faulet401e6db2020-11-24 09:49:01 +0100677 }
Christopher Faulet75bc9132018-11-30 15:18:09 +0100678
Christopher Faulet401e6db2020-11-24 09:49:01 +0100679 if (FLT_OPS(filter)->http_payload) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100680 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100681 ret = FLT_OPS(filter)->http_payload(s, filter, msg, out + offset, data - offset);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100682 if (ret < 0)
683 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100684 data = ret + *flt_off - *strm_off;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100685 *flt_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100686 }
687 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100688
Christopher Faulet6071c2d2021-01-25 12:02:00 +0100689 /* 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 Faulet75bc9132018-11-30 15:18:09 +0100705 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100706 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100707 return ret;
708}
709
710/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200711 * 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 */
719int
720flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
721{
722 int ret = 1;
723
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100724 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
725
Christopher Fauletd7c91962015-04-30 11:48:27 +0200726 /* 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 Faulete6006242017-03-10 11:52:44 +0100729 /* Set flag on channel to tell that the channel is filtered */
730 chn->flags |= CF_FLT_ANALYZE;
Christopher Faulet949b6ca2021-09-10 10:29:54 +0200731 chn->analysers |= ((chn->flags & CF_ISRESP) ? AN_RES_FLT_END : AN_REQ_FLT_END);
Christopher Faulete6006242017-03-10 11:52:44 +0100732
Christopher Fauletd7c91962015-04-30 11:48:27 +0200733 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100734 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 Fauletd7c91962015-04-30 11:48:27 +0200744
Christopher Fauletb2e58492019-11-12 11:13:01 +0100745 FLT_OFF(filter, chn) = 0;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100746 if (FLT_OPS(filter)->channel_start_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100747 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100748 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200749 if (ret <= 0)
750 BREAK_EXECUTION(s, chn, end);
751 }
752 } RESUME_FILTER_END;
753
754 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100755 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 Fauletd7c91962015-04-30 11:48:27 +0200758}
759
760/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200761 * 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 Fauletd7c91962015-04-30 11:48:27 +0200769 */
770int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200771flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200772{
773 int ret = 1;
774
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100775 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
776
Christopher Fauletd7c91962015-04-30 11:48:27 +0200777 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200778 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100779 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200780 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200781 if (ret <= 0)
782 BREAK_EXECUTION(s, chn, check_result);
Christopher Fauleta6d37042021-05-20 18:00:55 +0200783 filter->pre_analyzers &= ~an_bit;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200784 }
785 } RESUME_FILTER_END;
786
787 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100788 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 Faulet309c6412015-12-02 09:57:32 +0100791}
792
793/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200794 * 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 */
802int
803flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
804{
805 struct filter *filter;
806 int ret = 1;
807
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100808 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
809
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200810 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
811 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100812 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200813 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
814 if (ret < 0)
815 break;
Christopher Fauleta6d37042021-05-20 18:00:55 +0200816 filter->post_analyzers &= ~an_bit;
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200817 }
818 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100819 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 Faulet3a394fa2016-05-11 17:13:39 +0200822}
823
824/*
Christopher Faulet0184ea72017-01-05 14:06:34 +0100825 * 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 Faulet309c6412015-12-02 09:57:32 +0100828 */
829int
830flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
831{
Christopher Faulet1339d742016-05-11 16:48:33 +0200832 struct http_msg *msg;
833 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100834
Christopher Faulet1339d742016-05-11 16:48:33 +0200835 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100836 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
837
Christopher Faulet309c6412015-12-02 09:57:32 +0100838 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200839 if (FLT_OPS(filter)->http_headers) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100840 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet1339d742016-05-11 16:48:33 +0200841 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100842 if (ret <= 0)
843 BREAK_EXECUTION(s, chn, check_result);
844 }
845 } RESUME_FILTER_END;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100846
847 if (HAS_DATA_FILTERS(s, chn)) {
848 size_t data = http_get_hdrs_size(htxbuf(&chn->buf));
849 struct filter *f;
850
Christopher Faulet401e6db2020-11-24 09:49:01 +0100851 list_for_each_entry(f, &strm_flt(s)->filters, list)
852 FLT_OFF(f, chn) = data;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100853 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100854
855 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100856 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 Fauletd7c91962015-04-30 11:48:27 +0200859}
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 */
868int
869flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
870{
871 int ret = 1;
872
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100873 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
874
Christopher Faulete6006242017-03-10 11:52:44 +0100875 /* 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 Fauletd7c91962015-04-30 11:48:27 +0200880 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100881 FLT_OFF(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100882 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200883
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100884 if (FLT_OPS(filter)->channel_end_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100885 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100886 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200887 if (ret <= 0)
888 BREAK_EXECUTION(s, chn, end);
889 }
890 } RESUME_FILTER_END;
891
Christopher Faulete6006242017-03-10 11:52:44 +0100892 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 Faulet570f7992017-07-06 15:53:02 +0200896 if (ret) {
Christopher Faulete6006242017-03-10 11:52:44 +0100897 chn->flags &= ~CF_FLT_ANALYZE;
Christopher Faulet02c7b222015-12-22 12:01:29 +0100898
Christopher Faulet570f7992017-07-06 15:53:02 +0200899 /* 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 Herlantb35ea682018-11-15 12:24:23 -0800904 * processing. Of course, this is the filter responsibility to
Christopher Faulet570f7992017-07-06 15:53:02 +0200905 * 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 Faulete6006242017-03-10 11:52:44 +0100911 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 Fauletc6062be2016-10-31 11:22:37 +0100918
Christopher Faulete6006242017-03-10 11:52:44 +0100919 /* Remove backend filters from the list */
920 flt_stream_release(s, 1);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100921 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200922 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100923 else {
924 DBG_TRACE_DEVEL("waiting for sync", STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
925 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200926 return ret;
927}
928
Christopher Fauletd7c91962015-04-30 11:48:27 +0200929
930/*
Christopher Fauletb2e58492019-11-12 11:13:01 +0100931 * 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 Fauletd7c91962015-04-30 11:48:27 +0200938 */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100939int
940flt_tcp_payload(struct stream *s, struct channel *chn, unsigned int len)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200941{
Christopher Fauletda02e172015-12-04 09:25:05 +0100942 struct filter *filter;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100943 unsigned long long *strm_off = &FLT_STRM_OFF(s, chn);
944 unsigned int out = co_data(chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100945 int ret, data;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200946
Christopher Faulet81340d72020-02-26 15:47:22 +0100947 ret = data = len - out;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100948 DBG_TRACE_ENTER(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100949 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet401e6db2020-11-24 09:49:01 +0100950 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 Fauletda02e172015-12-04 09:25:05 +0100958 continue;
Christopher Faulet401e6db2020-11-24 09:49:01 +0100959 }
960
Christopher Fauletb2e58492019-11-12 11:13:01 +0100961 if (FLT_OPS(filter)->tcp_payload) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100962
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100963 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100964 ret = FLT_OPS(filter)->tcp_payload(s, filter, chn, out + offset, data - offset);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200965 if (ret < 0)
966 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100967 data = ret + *flt_off - *strm_off;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100968 *flt_off += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200969 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200970 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100971
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 Fauletd7c91962015-04-30 11:48:27 +0200977 end:
Christopher Fauletb2e58492019-11-12 11:13:01 +0100978 DBG_TRACE_LEAVE(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200979 return ret;
980}
981
982/*
983 * Called when TCP data must be filtered on a channel. This function is the
Christopher Faulet0184ea72017-01-05 14:06:34 +0100984 * 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 Fauletd7c91962015-04-30 11:48:27 +0200989 */
990int
991flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
992{
Christopher Fauletb2e58492019-11-12 11:13:01 +0100993 unsigned int len;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200994 int ret = 1;
995
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100996 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
997
Christopher Fauletda02e172015-12-04 09:25:05 +0100998 /* If there is no "data" filters, we do nothing */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100999 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletda02e172015-12-04 09:25:05 +01001000 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001001
Christopher Fauletb2e58492019-11-12 11:13:01 +01001002 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 Fauletd7c91962015-04-30 11:48:27 +02001008
Christopher Fauletb2e58492019-11-12 11:13:01 +01001009 ret = flt_tcp_payload(s, chn, len);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001010 if (ret < 0)
1011 goto end;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001012 c_adv(chn, ret);
Christopher Fauletda02e172015-12-04 09:25:05 +01001013
Christopher Faulet2e56a732023-01-26 16:18:09 +01001014 /* 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 Faulet208c7122023-04-13 16:16:15 +02001019 if (chn_cons(chn)->flags & SC_FL_SHUT_DONE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001020 ret = 1;
1021 goto end;
1022 }
Christopher Fauletca5309a2023-04-17 16:17:32 +02001023 if (chn_prod(chn)->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)) {
Christopher Fauletb2e58492019-11-12 11:13:01 +01001024 if (((s->flags & SF_HTX) && htx_is_empty(htxbuf(&chn->buf))) || c_empty(chn)) {
1025 ret = 1;
1026 goto end;
1027 }
1028 }
Christopher Faulet2e56a732023-01-26 16:18:09 +01001029 if (chn->flags & (CF_READ_TIMEOUT|CF_WRITE_TIMEOUT)) {
1030 ret = 1;
1031 goto end;
1032 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02001033
1034 /* Wait for data */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001035 DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001036 return 0;
1037 end:
1038 /* Terminate the data filtering. If <ret> is negative, an error was
1039 * encountered during the filtering. */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001040 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 Fauletd7c91962015-04-30 11:48:27 +02001043}
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 */
1049static int
1050handle_analyzer_result(struct stream *s, struct channel *chn,
1051 unsigned int an_bit, int ret)
1052{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001053 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 Faulet3d119692019-07-15 22:04:51 +02001067 if (IS_HTX_STRM(s)) {
Christopher Faulet0adffb62023-04-13 14:49:04 +02001068 http_set_term_flags(s);
1069
Christopher Faulete058f732019-09-06 15:24:55 +02001070 if (s->txn->status > 0)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001071 http_reply_and_close(s, s->txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001072 else {
Christopher Faulet0adffb62023-04-13 14:49:04 +02001073 s->txn->status = (!(chn->flags & CF_ISRESP)) ? 400 : 502;
1074 http_reply_and_close(s, s->txn->status, http_error_message(s));
Christopher Fauletd7c91962015-04-30 11:48:27 +02001075 }
1076 }
Christopher Faulet0adffb62023-04-13 14:49:04 +02001077 else {
1078 sess_set_term_flags(s);
1079 stream_retnclose(s, NULL);
1080 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02001081
Christopher Faulet0adffb62023-04-13 14:49:04 +02001082 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 Fauleteea8fc72019-11-05 16:18:10 +01001088 DBG_TRACE_DEVEL("leaving on error", STRM_EV_FLT_ANA|STRM_EV_FLT_ERR, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001089 return 0;
1090
1091 wait:
1092 if (!(chn->flags & CF_ISRESP))
1093 channel_dont_connect(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001094 DBG_TRACE_DEVEL("wairing for more data", STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001095 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. */
1105static struct cfg_kw_list cfg_kws = {ILH, {
1106 { CFG_LISTEN, "filter", parse_filter },
1107 { 0, NULL, NULL },
1108 }
1109};
1110
Willy Tarreau0108d902018-11-25 19:14:37 +01001111INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1112
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001113REGISTER_POST_CHECK(flt_init_all);
1114REGISTER_PER_THREAD_INIT(flt_init_all_per_thread);
1115REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread);
1116
Christopher Fauletd7c91962015-04-30 11:48:27 +02001117/*
1118 * Local variables:
1119 * c-indent-level: 8
1120 * c-basic-offset: 8
1121 * End:
1122 */