blob: 8910fc2684847b6b4916433a4b3be3d579fb3beb [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 Faulet0fda8d22023-05-11 09:11:57 +0200311 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
Christopher Faulet400829c2020-11-02 16:08:09 +0100312 continue;
Christopher Faulet0fda8d22023-05-11 09:11:57 +0200313
Willy Tarreau64bca592016-12-21 20:13:11 +0100314 err_code |= flt_init(px);
315 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100316 ha_alert("Failed to initialize filters for proxy '%s'.\n",
317 px->id);
Willy Tarreau64bca592016-12-21 20:13:11 +0100318 return err_code;
319 }
320 }
321 return 0;
322}
323
Joseph Herlantb35ea682018-11-15 12:24:23 -0800324/* 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 Faulet71a6a8e2017-07-27 16:33:28 +0200326static int
327flt_init_all_per_thread()
328{
329 struct proxy *px;
330 int err_code = 0;
331
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100332 for (px = proxies_list; px; px = px->next) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200333 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
Christopher Faulet400829c2020-11-02 16:08:09 +0100334 continue;
335
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200336 err_code = flt_init_per_thread(px);
337 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100338 ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n",
339 px->id, tid);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200340 return 0;
341 }
342 }
343 return 1;
344}
345
Christopher Fauletd7c91962015-04-30 11:48:27 +0200346/*
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 */
351int
352flt_check(struct proxy *proxy)
353{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100354 struct flt_conf *fconf;
355 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200356
Christopher Fauletff17b182019-01-07 15:03:22 +0100357 err += check_implicit_http_comp_flt(proxy);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100358 list_for_each_entry(fconf, &proxy->filter_configs, list) {
359 if (fconf->ops->check)
360 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200361 }
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 */
369void
370flt_deinit(struct proxy *proxy)
371{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100372 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200373
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100374 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
Christopher Faulet743bd6a2020-11-03 16:40:37 +0100375 if (fconf->ops->deinit)
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100376 fconf->ops->deinit(proxy, fconf);
Willy Tarreau2b718102021-04-21 07:32:39 +0200377 LIST_DELETE(&fconf->list);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100378 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200379 }
380}
381
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200382/*
383 * Calls 'denit_per_thread' callback for all filters attached to a proxy for
384 * each threads. This happens before exiting a thread.
385 */
386void
387flt_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 */
399static void
400flt_deinit_all_per_thread()
401{
402 struct proxy *px;
403
Christopher Faulet743bd6a2020-11-03 16:40:37 +0100404 for (px = proxies_list; px; px = px->next)
405 flt_deinit_per_thread(px);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200406}
407
Christopher Faulet92d36382015-11-05 13:35:03 +0100408/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
409static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100410flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100411{
Christopher Faulet75bc9132018-11-30 15:18:09 +0100412 struct filter *f;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200413
Christopher Faulet0f17a9b2019-04-05 10:11:38 +0200414 if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX))
Christopher Faulet75bc9132018-11-30 15:18:09 +0100415 return 0;
416
Willy Tarreau1bbec382021-03-22 21:02:50 +0100417 f = pool_zalloc(pool_head_filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100418 if (!f) /* not enough memory */
419 return -1;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100420 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100421 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200422
423 if (FLT_OPS(f)->attach) {
424 int ret = FLT_OPS(f)->attach(s, f);
425 if (ret <= 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100426 pool_free(pool_head_filter, f);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200427 return ret;
428 }
429 }
430
Willy Tarreau2b718102021-04-21 07:32:39 +0200431 LIST_APPEND(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100432 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100433 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 */
440int
441flt_stream_init(struct stream *s)
442{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100443 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100444
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100445 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
446 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100447 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
448 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100449 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 */
460void
461flt_stream_release(struct stream *s, int only_backend)
462{
463 struct filter *filter, *back;
464
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100465 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100466 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200467 if (FLT_OPS(filter)->detach)
468 FLT_OPS(filter)->detach(s, filter);
Willy Tarreau2b718102021-04-21 07:32:39 +0200469 LIST_DELETE(&filter->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100470 pool_free(pool_head_filter, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100471 }
472 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100473 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100474 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100475}
476
Christopher Fauletd7c91962015-04-30 11:48:27 +0200477/*
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 */
482int
483flt_stream_start(struct stream *s)
484{
485 struct filter *filter;
486
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100487 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100488 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200489 return -1;
490 }
Willy Tarreau7866e8e2023-01-12 18:39:42 +0100491 if (strm_li(s) && (strm_li(s)->bind_conf->analysers & AN_REQ_FLT_START_FE)) {
Christopher Faulet5647fba2021-03-08 13:40:30 +0100492 s->req.flags |= CF_FLT_ANALYZE;
Christopher Fauletd28b2b22021-10-04 07:50:13 +0200493 s->req.analysers |= AN_REQ_FLT_END;
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200494 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200495 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 */
502void
503flt_stream_stop(struct stream *s)
504{
505 struct filter *filter;
506
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100507 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100508 if (FLT_OPS(filter)->stream_stop)
509 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200510 }
511}
512
Christopher Faulet92d36382015-11-05 13:35:03 +0100513/*
Christopher Fauleta00d8172016-11-10 14:58:05 +0100514 * Calls 'check_timeouts' for all filters attached to a stream. This happens when
515 * the stream is woken up because of expired timer.
516 */
517void
518flt_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 Faulet92d36382015-11-05 13:35:03 +0100529 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200530 * are not the same, this function attaches all backend filters to the
531 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100532 */
533int
534flt_set_stream_backend(struct stream *s, struct proxy *be)
535{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100536 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200537 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100538
539 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200540 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100541
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100542 list_for_each_entry(fconf, &be->filter_configs, list) {
543 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100544 return -1;
545 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200546
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 Faulet26eb5ea2021-08-13 14:00:46 +0200553 if (be->be_req_ana & AN_REQ_FLT_START_BE) {
Christopher Faulet5647fba2021-03-08 13:40:30 +0100554 s->req.flags |= CF_FLT_ANALYZE;
Christopher Faulet949b6ca2021-09-10 10:29:54 +0200555 s->req.analysers |= AN_REQ_FLT_END;
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200556 }
557 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 +0100558 s->res.flags |= CF_FLT_ANALYZE;
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200559 s->res.analysers |= AN_RES_FLT_END;
560 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200561
Christopher Faulet92d36382015-11-05 13:35:03 +0100562 return 0;
563}
564
Christopher Fauletd7c91962015-04-30 11:48:27 +0200565
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 */
573int
574flt_http_end(struct stream *s, struct http_msg *msg)
575{
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100576 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
577 unsigned int offset = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200578 int ret = 1;
579
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100580 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 +0200581 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100582 unsigned long long flt_off = FLT_OFF(filter, msg->chn);
583 offset = flt_off - *strm_off;
584
Christopher Faulet401e6db2020-11-24 09:49:01 +0100585 /* 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 Faulet443ea1a2016-02-04 13:40:26 +0100591 if (FLT_OPS(filter)->http_end) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100592 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100593 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200594 if (ret <= 0)
595 BREAK_EXECUTION(s, msg->chn, end);
596 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200597 } RESUME_FILTER_END;
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100598
599 c_adv(msg->chn, offset);
600 *strm_off += offset;
601
Christopher Fauletd7c91962015-04-30 11:48:27 +0200602end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100603 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200604 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 */
611void
612flt_http_reset(struct stream *s, struct http_msg *msg)
613{
614 struct filter *filter;
615
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100616 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 +0100617 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100618 if (FLT_OPS(filter)->http_reset) {
619 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100620 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100621 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200622 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100623 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200624}
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 */
630void
Willy Tarreau83061a82018-07-13 11:56:34 +0200631flt_http_reply(struct stream *s, short status, const struct buffer *msg)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200632{
633 struct filter *filter;
634
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100635 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 +0100636 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100637 if (FLT_OPS(filter)->http_reply) {
638 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100639 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100640 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200641 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100642 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200643}
644
645/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100646 * 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 Faulet75bc9132018-11-30 15:18:09 +0100653 */
654int
655flt_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 Faulet421e7692019-06-13 11:16:45 +0200659 unsigned int out = co_data(msg->chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100660 int ret, data;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100661
Christopher Faulet6071c2d2021-01-25 12:02:00 +0100662 strm_flt(s)->flags &= ~STRM_FLT_FL_HOLD_HTTP_HDRS;
663
Christopher Faulet81340d72020-02-26 15:47:22 +0100664 ret = data = len - out;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100665 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 +0100666 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet401e6db2020-11-24 09:49:01 +0100667 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 Faulet75bc9132018-11-30 15:18:09 +0100675 continue;
Christopher Faulet401e6db2020-11-24 09:49:01 +0100676 }
Christopher Faulet75bc9132018-11-30 15:18:09 +0100677
Christopher Faulet401e6db2020-11-24 09:49:01 +0100678 if (FLT_OPS(filter)->http_payload) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100679 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100680 ret = FLT_OPS(filter)->http_payload(s, filter, msg, out + offset, data - offset);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100681 if (ret < 0)
682 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100683 data = ret + *flt_off - *strm_off;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100684 *flt_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100685 }
686 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100687
Christopher Faulet6071c2d2021-01-25 12:02:00 +0100688 /* 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 Faulet75bc9132018-11-30 15:18:09 +0100704 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100705 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100706 return ret;
707}
708
709/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200710 * 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 */
718int
719flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
720{
721 int ret = 1;
722
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100723 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
724
Christopher Fauletd7c91962015-04-30 11:48:27 +0200725 /* 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 Faulete6006242017-03-10 11:52:44 +0100728 /* Set flag on channel to tell that the channel is filtered */
729 chn->flags |= CF_FLT_ANALYZE;
Christopher Faulet949b6ca2021-09-10 10:29:54 +0200730 chn->analysers |= ((chn->flags & CF_ISRESP) ? AN_RES_FLT_END : AN_REQ_FLT_END);
Christopher Faulete6006242017-03-10 11:52:44 +0100731
Christopher Fauletd7c91962015-04-30 11:48:27 +0200732 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100733 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 Fauletd7c91962015-04-30 11:48:27 +0200743
Christopher Fauletb2e58492019-11-12 11:13:01 +0100744 FLT_OFF(filter, chn) = 0;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100745 if (FLT_OPS(filter)->channel_start_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100746 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100747 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200748 if (ret <= 0)
749 BREAK_EXECUTION(s, chn, end);
750 }
751 } RESUME_FILTER_END;
752
753 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100754 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 Fauletd7c91962015-04-30 11:48:27 +0200757}
758
759/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200760 * 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 Fauletd7c91962015-04-30 11:48:27 +0200768 */
769int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200770flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200771{
772 int ret = 1;
773
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100774 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
775
Christopher Fauletd7c91962015-04-30 11:48:27 +0200776 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200777 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100778 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200779 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200780 if (ret <= 0)
781 BREAK_EXECUTION(s, chn, check_result);
Christopher Fauleta6d37042021-05-20 18:00:55 +0200782 filter->pre_analyzers &= ~an_bit;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200783 }
784 } RESUME_FILTER_END;
785
786 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100787 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 Faulet309c6412015-12-02 09:57:32 +0100790}
791
792/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200793 * 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 */
801int
802flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
803{
804 struct filter *filter;
805 int ret = 1;
806
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100807 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
808
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200809 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
810 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100811 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200812 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
813 if (ret < 0)
814 break;
Christopher Fauleta6d37042021-05-20 18:00:55 +0200815 filter->post_analyzers &= ~an_bit;
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200816 }
817 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100818 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 Faulet3a394fa2016-05-11 17:13:39 +0200821}
822
823/*
Christopher Faulet0184ea72017-01-05 14:06:34 +0100824 * 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 Faulet309c6412015-12-02 09:57:32 +0100827 */
828int
829flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
830{
Christopher Faulet1339d742016-05-11 16:48:33 +0200831 struct http_msg *msg;
832 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100833
Christopher Faulet1339d742016-05-11 16:48:33 +0200834 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100835 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
836
Christopher Faulet309c6412015-12-02 09:57:32 +0100837 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200838 if (FLT_OPS(filter)->http_headers) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100839 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet1339d742016-05-11 16:48:33 +0200840 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100841 if (ret <= 0)
842 BREAK_EXECUTION(s, chn, check_result);
843 }
844 } RESUME_FILTER_END;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100845
846 if (HAS_DATA_FILTERS(s, chn)) {
847 size_t data = http_get_hdrs_size(htxbuf(&chn->buf));
848 struct filter *f;
849
Christopher Faulet401e6db2020-11-24 09:49:01 +0100850 list_for_each_entry(f, &strm_flt(s)->filters, list)
851 FLT_OFF(f, chn) = data;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100852 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100853
854 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100855 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 Fauletd7c91962015-04-30 11:48:27 +0200858}
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 */
867int
868flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
869{
870 int ret = 1;
871
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100872 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
873
Christopher Faulete6006242017-03-10 11:52:44 +0100874 /* 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 Fauletd7c91962015-04-30 11:48:27 +0200879 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100880 FLT_OFF(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100881 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200882
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100883 if (FLT_OPS(filter)->channel_end_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100884 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100885 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200886 if (ret <= 0)
887 BREAK_EXECUTION(s, chn, end);
888 }
889 } RESUME_FILTER_END;
890
Christopher Faulete6006242017-03-10 11:52:44 +0100891 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 Faulet570f7992017-07-06 15:53:02 +0200895 if (ret) {
Christopher Faulete6006242017-03-10 11:52:44 +0100896 chn->flags &= ~CF_FLT_ANALYZE;
Christopher Faulet02c7b222015-12-22 12:01:29 +0100897
Christopher Faulet570f7992017-07-06 15:53:02 +0200898 /* 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 Herlantb35ea682018-11-15 12:24:23 -0800903 * processing. Of course, this is the filter responsibility to
Christopher Faulet570f7992017-07-06 15:53:02 +0200904 * 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 Faulete6006242017-03-10 11:52:44 +0100910 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 Fauletc6062be2016-10-31 11:22:37 +0100917
Christopher Faulete6006242017-03-10 11:52:44 +0100918 /* Remove backend filters from the list */
919 flt_stream_release(s, 1);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100920 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200921 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100922 else {
923 DBG_TRACE_DEVEL("waiting for sync", STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
924 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200925 return ret;
926}
927
Christopher Fauletd7c91962015-04-30 11:48:27 +0200928
929/*
Christopher Fauletb2e58492019-11-12 11:13:01 +0100930 * 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 Fauletd7c91962015-04-30 11:48:27 +0200937 */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100938int
939flt_tcp_payload(struct stream *s, struct channel *chn, unsigned int len)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200940{
Christopher Fauletda02e172015-12-04 09:25:05 +0100941 struct filter *filter;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100942 unsigned long long *strm_off = &FLT_STRM_OFF(s, chn);
943 unsigned int out = co_data(chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100944 int ret, data;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200945
Christopher Faulet81340d72020-02-26 15:47:22 +0100946 ret = data = len - out;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100947 DBG_TRACE_ENTER(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100948 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet401e6db2020-11-24 09:49:01 +0100949 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 Fauletda02e172015-12-04 09:25:05 +0100957 continue;
Christopher Faulet401e6db2020-11-24 09:49:01 +0100958 }
959
Christopher Fauletb2e58492019-11-12 11:13:01 +0100960 if (FLT_OPS(filter)->tcp_payload) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100961
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100962 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100963 ret = FLT_OPS(filter)->tcp_payload(s, filter, chn, out + offset, data - offset);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200964 if (ret < 0)
965 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100966 data = ret + *flt_off - *strm_off;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100967 *flt_off += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200968 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200969 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100970
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 Fauletd7c91962015-04-30 11:48:27 +0200976 end:
Christopher Fauletb2e58492019-11-12 11:13:01 +0100977 DBG_TRACE_LEAVE(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200978 return ret;
979}
980
981/*
982 * Called when TCP data must be filtered on a channel. This function is the
Christopher Faulet0184ea72017-01-05 14:06:34 +0100983 * 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 Fauletd7c91962015-04-30 11:48:27 +0200988 */
989int
990flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
991{
Christopher Fauletb2e58492019-11-12 11:13:01 +0100992 unsigned int len;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200993 int ret = 1;
994
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100995 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
996
Christopher Fauletda02e172015-12-04 09:25:05 +0100997 /* If there is no "data" filters, we do nothing */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100998 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletda02e172015-12-04 09:25:05 +0100999 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001000
Christopher Fauletb2e58492019-11-12 11:13:01 +01001001 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 Fauletd7c91962015-04-30 11:48:27 +02001007
Christopher Fauletb2e58492019-11-12 11:13:01 +01001008 ret = flt_tcp_payload(s, chn, len);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001009 if (ret < 0)
1010 goto end;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001011 c_adv(chn, ret);
Christopher Fauletda02e172015-12-04 09:25:05 +01001012
Christopher Faulet2e56a732023-01-26 16:18:09 +01001013 /* 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 Faulet208c7122023-04-13 16:16:15 +02001018 if (chn_cons(chn)->flags & SC_FL_SHUT_DONE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001019 ret = 1;
1020 goto end;
1021 }
Christopher Fauletca5309a2023-04-17 16:17:32 +02001022 if (chn_prod(chn)->flags & (SC_FL_ABRT_DONE|SC_FL_EOS)) {
Christopher Fauletb2e58492019-11-12 11:13:01 +01001023 if (((s->flags & SF_HTX) && htx_is_empty(htxbuf(&chn->buf))) || c_empty(chn)) {
1024 ret = 1;
1025 goto end;
1026 }
1027 }
Christopher Faulet2e56a732023-01-26 16:18:09 +01001028 if (chn->flags & (CF_READ_TIMEOUT|CF_WRITE_TIMEOUT)) {
1029 ret = 1;
1030 goto end;
1031 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02001032
1033 /* Wait for data */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001034 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 +02001035 return 0;
1036 end:
1037 /* Terminate the data filtering. If <ret> is negative, an error was
1038 * encountered during the filtering. */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001039 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 Fauletd7c91962015-04-30 11:48:27 +02001042}
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 */
1048static int
1049handle_analyzer_result(struct stream *s, struct channel *chn,
1050 unsigned int an_bit, int ret)
1051{
Christopher Fauletd7c91962015-04-30 11:48:27 +02001052 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 Faulet3d119692019-07-15 22:04:51 +02001066 if (IS_HTX_STRM(s)) {
Christopher Faulet0adffb62023-04-13 14:49:04 +02001067 http_set_term_flags(s);
1068
Christopher Faulete058f732019-09-06 15:24:55 +02001069 if (s->txn->status > 0)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001070 http_reply_and_close(s, s->txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001071 else {
Christopher Faulet0adffb62023-04-13 14:49:04 +02001072 s->txn->status = (!(chn->flags & CF_ISRESP)) ? 400 : 502;
1073 http_reply_and_close(s, s->txn->status, http_error_message(s));
Christopher Fauletd7c91962015-04-30 11:48:27 +02001074 }
1075 }
Christopher Faulet0adffb62023-04-13 14:49:04 +02001076 else {
1077 sess_set_term_flags(s);
1078 stream_retnclose(s, NULL);
1079 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02001080
Christopher Faulet0adffb62023-04-13 14:49:04 +02001081 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 Fauleteea8fc72019-11-05 16:18:10 +01001087 DBG_TRACE_DEVEL("leaving on error", STRM_EV_FLT_ANA|STRM_EV_FLT_ERR, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001088 return 0;
1089
1090 wait:
1091 if (!(chn->flags & CF_ISRESP))
1092 channel_dont_connect(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001093 DBG_TRACE_DEVEL("wairing for more data", STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001094 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. */
1104static struct cfg_kw_list cfg_kws = {ILH, {
1105 { CFG_LISTEN, "filter", parse_filter },
1106 { 0, NULL, NULL },
1107 }
1108};
1109
Willy Tarreau0108d902018-11-25 19:14:37 +01001110INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1111
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001112REGISTER_POST_CHECK(flt_init_all);
1113REGISTER_PER_THREAD_INIT(flt_init_all_per_thread);
1114REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread);
1115
Christopher Fauletd7c91962015-04-30 11:48:27 +02001116/*
1117 * Local variables:
1118 * c-indent-level: 8
1119 * c-basic-offset: 8
1120 * End:
1121 */