blob: 12159f2b239d1caf5e2acbcce6801de42a717e75 [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 Tarreau5e539c92020-06-04 20:45:39 +020026#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020027#include <haproxy/tools.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020028#include <haproxy/trace.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020029
Christopher Fauletd7c91962015-04-30 11:48:27 +020030
Christopher Fauleteea8fc72019-11-05 16:18:10 +010031#define TRACE_SOURCE &trace_strm
32
Christopher Fauletd7c91962015-04-30 11:48:27 +020033/* Pool used to allocate filters */
Willy Tarreau8ceae722018-11-26 11:58:30 +010034DECLARE_STATIC_POOL(pool_head_filter, "filter", sizeof(struct filter));
Christopher Fauletd7c91962015-04-30 11:48:27 +020035
36static int handle_analyzer_result(struct stream *s, struct channel *chn, unsigned int an_bit, int ret);
37
38/* - RESUME_FILTER_LOOP and RESUME_FILTER_END must always be used together.
39 * The first one begins a loop and the seconds one ends it.
40 *
41 * - BREAK_EXECUTION must be used to break the loop and set the filter from
42 * which to resume the next time.
43 *
Bertrand Jacquin874a35c2018-09-10 21:26:07 +010044 * Here is an example:
Christopher Fauletd7c91962015-04-30 11:48:27 +020045 *
46 * RESUME_FILTER_LOOP(stream, channel) {
47 * ...
48 * if (cond)
49 * BREAK_EXECUTION(stream, channel, label);
50 * ...
51 * } RESUME_FILTER_END;
52 * ...
53 * label:
54 * ...
55 *
56 */
57#define RESUME_FILTER_LOOP(strm, chn) \
58 do { \
59 struct filter *filter; \
60 \
Christopher Fauletda02e172015-12-04 09:25:05 +010061 if (strm_flt(strm)->current[CHN_IDX(chn)]) { \
62 filter = strm_flt(strm)->current[CHN_IDX(chn)]; \
63 strm_flt(strm)->current[CHN_IDX(chn)] = NULL; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020064 goto resume_execution; \
65 } \
66 \
Christopher Fauletfcf035c2015-12-03 11:48:03 +010067 list_for_each_entry(filter, &strm_flt(s)->filters, list) { \
Christopher Fauletda02e172015-12-04 09:25:05 +010068 resume_execution:
Christopher Fauletd7c91962015-04-30 11:48:27 +020069
70#define RESUME_FILTER_END \
71 } \
72 } while(0)
73
Christopher Fauletda02e172015-12-04 09:25:05 +010074#define BREAK_EXECUTION(strm, chn, label) \
75 do { \
76 strm_flt(strm)->current[CHN_IDX(chn)] = filter; \
77 goto label; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020078 } while (0)
79
80
81/* List head of all known filter keywords */
82static struct flt_kw_list flt_keywords = {
83 .list = LIST_HEAD_INIT(flt_keywords.list)
84};
85
86/*
87 * Registers the filter keyword list <kwl> as a list of valid keywords for next
88 * parsing sessions.
89 */
90void
91flt_register_keywords(struct flt_kw_list *kwl)
92{
Willy Tarreau2b718102021-04-21 07:32:39 +020093 LIST_APPEND(&flt_keywords.list, &kwl->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +020094}
95
96/*
97 * Returns a pointer to the filter keyword <kw>, or NULL if not found. If the
98 * keyword is found with a NULL ->parse() function, then an attempt is made to
99 * find one with a valid ->parse() function. This way it is possible to declare
100 * platform-dependant, known keywords as NULL, then only declare them as valid
101 * if some options are met. Note that if the requested keyword contains an
102 * opening parenthesis, everything from this point is ignored.
103 */
104struct flt_kw *
105flt_find_kw(const char *kw)
106{
107 int index;
108 const char *kwend;
109 struct flt_kw_list *kwl;
110 struct flt_kw *ret = NULL;
111
112 kwend = strchr(kw, '(');
113 if (!kwend)
114 kwend = kw + strlen(kw);
115
116 list_for_each_entry(kwl, &flt_keywords.list, list) {
117 for (index = 0; kwl->kw[index].kw != NULL; index++) {
118 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
119 kwl->kw[index].kw[kwend-kw] == 0) {
120 if (kwl->kw[index].parse)
121 return &kwl->kw[index]; /* found it !*/
122 else
123 ret = &kwl->kw[index]; /* may be OK */
124 }
125 }
126 }
127 return ret;
128}
129
130/*
131 * Dumps all registered "filter" keywords to the <out> string pointer. The
132 * unsupported keywords are only dumped if their supported form was not found.
Willy Tarreau3b65e142022-03-29 15:03:09 +0200133 * If <out> is NULL, the output is emitted using a more compact format on stdout.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200134 */
135void
136flt_dump_kws(char **out)
137{
138 struct flt_kw_list *kwl;
Willy Tarreau0f996372022-03-30 12:08:00 +0200139 const struct flt_kw *kwp, *kw;
140 const char *scope = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200141 int index;
142
Willy Tarreau3b65e142022-03-29 15:03:09 +0200143 if (out)
144 *out = NULL;
Willy Tarreau0f996372022-03-30 12:08:00 +0200145
146 for (kw = kwp = NULL;; kwp = kw) {
147 list_for_each_entry(kwl, &flt_keywords.list, list) {
148 for (index = 0; kwl->kw[index].kw != NULL; index++) {
149 if ((kwl->kw[index].parse ||
150 flt_find_kw(kwl->kw[index].kw) == &kwl->kw[index])
151 && strordered(kwp ? kwp->kw : NULL,
152 kwl->kw[index].kw,
153 kw != kwp ? kw->kw : NULL)) {
154 kw = &kwl->kw[index];
155 scope = kwl->scope;
156 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200157 }
158 }
Willy Tarreau0f996372022-03-30 12:08:00 +0200159
160 if (kw == kwp)
161 break;
162
163 if (out)
164 memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "",
165 scope,
166 kw->kw,
167 kw->parse ? "" : " (not supported)");
168 else
169 printf("%s [%s]\n",
170 kw->kw, scope);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200171 }
172}
173
174/*
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100175 * Lists the known filters on <out>
176 */
177void
178list_filters(FILE *out)
179{
180 char *filters, *p, *f;
181
182 fprintf(out, "Available filters :\n");
183 flt_dump_kws(&filters);
184 for (p = filters; (f = strtok_r(p,"\n",&p));)
185 fprintf(out, "\t%s\n", f);
186 free(filters);
187}
188
189/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200190 * Parses the "filter" keyword. All keywords must be handled by filters
191 * themselves
192 */
193static int
194parse_filter(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100195 const struct proxy *defpx, const char *file, int line, char **err)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200196{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100197 struct flt_conf *fconf = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200198
199 /* Filter cannot be defined on a default proxy */
200 if (curpx == defpx) {
Christopher Fauletcc7317d2016-04-04 10:51:17 +0200201 memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.",
Christopher Fauletd7c91962015-04-30 11:48:27 +0200202 file, line, args[0]);
203 return -1;
204 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100205 if (strcmp(args[0], "filter") == 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200206 struct flt_kw *kw;
207 int cur_arg;
208
209 if (!*args[1]) {
210 memprintf(err,
211 "parsing [%s:%d] : missing argument for '%s' in %s '%s'.",
212 file, line, args[0], proxy_type_str(curpx), curpx->id);
213 goto error;
214 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100215 fconf = calloc(1, sizeof(*fconf));
216 if (!fconf) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200217 memprintf(err, "'%s' : out of memory", args[0]);
218 goto error;
219 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200220
221 cur_arg = 1;
222 kw = flt_find_kw(args[cur_arg]);
223 if (kw) {
224 if (!kw->parse) {
225 memprintf(err, "parsing [%s:%d] : '%s' : "
226 "'%s' option is not implemented in this version (check build options).",
227 file, line, args[0], args[cur_arg]);
228 goto error;
229 }
Thierry Fournier3610c392016-04-13 18:27:51 +0200230 if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200231 if (err && *err)
232 memprintf(err, "'%s' : '%s'",
233 args[0], *err);
234 else
235 memprintf(err, "'%s' : error encountered while processing '%s'",
236 args[0], args[cur_arg]);
237 goto error;
238 }
239 }
240 else {
241 flt_dump_kws(err);
242 indent_msg(err, 4);
243 memprintf(err, "'%s' : unknown keyword '%s'.%s%s",
244 args[0], args[cur_arg],
245 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
246 goto error;
247 }
248 if (*args[cur_arg]) {
249 memprintf(err, "'%s %s' : unknown keyword '%s'.",
250 args[0], args[1], args[cur_arg]);
251 goto error;
252 }
Christopher Faulet00e818a2016-04-19 17:00:44 +0200253 if (fconf->ops == NULL) {
254 memprintf(err, "'%s %s' : no callbacks defined.",
255 args[0], args[1]);
256 goto error;
257 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200258
Willy Tarreau2b718102021-04-21 07:32:39 +0200259 LIST_APPEND(&curpx->filter_configs, &fconf->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200260 }
261 return 0;
262
263 error:
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100264 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200265 return -1;
266
267
268}
269
270/*
271 * Calls 'init' callback for all filters attached to a proxy. This happens after
272 * the configuration parsing. Filters can finish to fill their config. Returns
273 * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
274 */
Willy Tarreau64bca592016-12-21 20:13:11 +0100275static int
Christopher Fauletd7c91962015-04-30 11:48:27 +0200276flt_init(struct proxy *proxy)
277{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100278 struct flt_conf *fconf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200279
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100280 list_for_each_entry(fconf, &proxy->filter_configs, list) {
281 if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200282 return ERR_ALERT|ERR_FATAL;
283 }
284 return 0;
285}
286
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200287/*
288 * Calls 'init_per_thread' callback for all filters attached to a proxy for each
289 * threads. This happens after the thread creation. Filters can finish to fill
290 * their config. Returns (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
291 */
292static int
293flt_init_per_thread(struct proxy *proxy)
294{
295 struct flt_conf *fconf;
296
297 list_for_each_entry(fconf, &proxy->filter_configs, list) {
298 if (fconf->ops->init_per_thread && fconf->ops->init_per_thread(proxy, fconf) < 0)
299 return ERR_ALERT|ERR_FATAL;
300 }
301 return 0;
302}
303
Willy Tarreau64bca592016-12-21 20:13:11 +0100304/* Calls flt_init() for all proxies, see above */
305static int
306flt_init_all()
307{
308 struct proxy *px;
Christopher Fauletfc633b62020-11-06 15:24:23 +0100309 int err_code = ERR_NONE;
Willy Tarreau64bca592016-12-21 20:13:11 +0100310
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100311 for (px = proxies_list; px; px = px->next) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200312 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
Christopher Faulet400829c2020-11-02 16:08:09 +0100313 flt_deinit(px);
314 continue;
315 }
Willy Tarreau64bca592016-12-21 20:13:11 +0100316 err_code |= flt_init(px);
317 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100318 ha_alert("Failed to initialize filters for proxy '%s'.\n",
319 px->id);
Willy Tarreau64bca592016-12-21 20:13:11 +0100320 return err_code;
321 }
322 }
323 return 0;
324}
325
Joseph Herlantb35ea682018-11-15 12:24:23 -0800326/* Calls flt_init_per_thread() for all proxies, see above. Be careful here, it
327 * returns 0 if an error occurred. This is the opposite of flt_init_all. */
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200328static int
329flt_init_all_per_thread()
330{
331 struct proxy *px;
332 int err_code = 0;
333
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100334 for (px = proxies_list; px; px = px->next) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200335 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
Christopher Faulet400829c2020-11-02 16:08:09 +0100336 continue;
337
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200338 err_code = flt_init_per_thread(px);
339 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100340 ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n",
341 px->id, tid);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200342 return 0;
343 }
344 }
345 return 1;
346}
347
Christopher Fauletd7c91962015-04-30 11:48:27 +0200348/*
349 * Calls 'check' callback for all filters attached to a proxy. This happens
350 * after the configuration parsing but before filters initialization. Returns
351 * the number of encountered errors.
352 */
353int
354flt_check(struct proxy *proxy)
355{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100356 struct flt_conf *fconf;
357 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200358
Christopher Fauletff17b182019-01-07 15:03:22 +0100359 err += check_implicit_http_comp_flt(proxy);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100360 list_for_each_entry(fconf, &proxy->filter_configs, list) {
361 if (fconf->ops->check)
362 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200363 }
364 return err;
365}
366
367/*
368 * Calls 'denit' callback for all filters attached to a proxy. This happens when
369 * HAProxy is stopped.
370 */
371void
372flt_deinit(struct proxy *proxy)
373{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100374 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200375
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100376 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
Christopher Faulet743bd6a2020-11-03 16:40:37 +0100377 if (fconf->ops->deinit)
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100378 fconf->ops->deinit(proxy, fconf);
Willy Tarreau2b718102021-04-21 07:32:39 +0200379 LIST_DELETE(&fconf->list);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100380 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200381 }
382}
383
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200384/*
385 * Calls 'denit_per_thread' callback for all filters attached to a proxy for
386 * each threads. This happens before exiting a thread.
387 */
388void
389flt_deinit_per_thread(struct proxy *proxy)
390{
391 struct flt_conf *fconf, *back;
392
393 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
394 if (fconf->ops->deinit_per_thread)
395 fconf->ops->deinit_per_thread(proxy, fconf);
396 }
397}
398
399
400/* Calls flt_deinit_per_thread() for all proxies, see above */
401static void
402flt_deinit_all_per_thread()
403{
404 struct proxy *px;
405
Christopher Faulet743bd6a2020-11-03 16:40:37 +0100406 for (px = proxies_list; px; px = px->next)
407 flt_deinit_per_thread(px);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200408}
409
Christopher Faulet92d36382015-11-05 13:35:03 +0100410/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
411static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100412flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100413{
Christopher Faulet75bc9132018-11-30 15:18:09 +0100414 struct filter *f;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200415
Christopher Faulet0f17a9b2019-04-05 10:11:38 +0200416 if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX))
Christopher Faulet75bc9132018-11-30 15:18:09 +0100417 return 0;
418
Willy Tarreau1bbec382021-03-22 21:02:50 +0100419 f = pool_zalloc(pool_head_filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100420 if (!f) /* not enough memory */
421 return -1;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100422 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100423 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200424
425 if (FLT_OPS(f)->attach) {
426 int ret = FLT_OPS(f)->attach(s, f);
427 if (ret <= 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100428 pool_free(pool_head_filter, f);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200429 return ret;
430 }
431 }
432
Willy Tarreau2b718102021-04-21 07:32:39 +0200433 LIST_APPEND(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100434 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100435 return 0;
436}
437
438/*
439 * Called when a stream is created. It attaches all frontend filters to the
440 * stream. Returns -1 if an error occurs, 0 otherwise.
441 */
442int
443flt_stream_init(struct stream *s)
444{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100445 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100446
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100447 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
448 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100449 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
450 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100451 return -1;
452 }
453 return 0;
454}
455
456/*
457 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
458 * happens after each request/response exchange). When analyze ends, backend
459 * filters are removed. When the stream is closed, all filters attached to the
460 * stream are removed.
461 */
462void
463flt_stream_release(struct stream *s, int only_backend)
464{
465 struct filter *filter, *back;
466
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100467 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100468 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200469 if (FLT_OPS(filter)->detach)
470 FLT_OPS(filter)->detach(s, filter);
Willy Tarreau2b718102021-04-21 07:32:39 +0200471 LIST_DELETE(&filter->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100472 pool_free(pool_head_filter, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100473 }
474 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100475 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100476 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100477}
478
Christopher Fauletd7c91962015-04-30 11:48:27 +0200479/*
480 * Calls 'stream_start' for all filters attached to a stream. This happens when
481 * the stream is created, just after calling flt_stream_init
482 * function. Returns -1 if an error occurs, 0 otherwise.
483 */
484int
485flt_stream_start(struct stream *s)
486{
487 struct filter *filter;
488
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100489 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100490 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200491 return -1;
492 }
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200493 if (strm_li(s) && (strm_li(s)->analysers & AN_REQ_FLT_START_FE)) {
Christopher Faulet5647fba2021-03-08 13:40:30 +0100494 s->req.flags |= CF_FLT_ANALYZE;
Christopher Fauletd28b2b22021-10-04 07:50:13 +0200495 s->req.analysers |= AN_REQ_FLT_END;
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200496 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200497 return 0;
498}
499
500/*
501 * Calls 'stream_stop' for all filters attached to a stream. This happens when
502 * the stream is stopped, just before calling flt_stream_release function.
503 */
504void
505flt_stream_stop(struct stream *s)
506{
507 struct filter *filter;
508
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100509 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100510 if (FLT_OPS(filter)->stream_stop)
511 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200512 }
513}
514
Christopher Faulet92d36382015-11-05 13:35:03 +0100515/*
Christopher Fauleta00d8172016-11-10 14:58:05 +0100516 * Calls 'check_timeouts' for all filters attached to a stream. This happens when
517 * the stream is woken up because of expired timer.
518 */
519void
520flt_stream_check_timeouts(struct stream *s)
521{
522 struct filter *filter;
523
524 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
525 if (FLT_OPS(filter)->check_timeouts)
526 FLT_OPS(filter)->check_timeouts(s, filter);
527 }
528}
529
530/*
Christopher Faulet92d36382015-11-05 13:35:03 +0100531 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200532 * are not the same, this function attaches all backend filters to the
533 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100534 */
535int
536flt_set_stream_backend(struct stream *s, struct proxy *be)
537{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100538 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200539 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100540
541 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200542 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100543
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100544 list_for_each_entry(fconf, &be->filter_configs, list) {
545 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100546 return -1;
547 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200548
549 end:
550 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
551 if (FLT_OPS(filter)->stream_set_backend &&
552 FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0)
553 return -1;
554 }
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200555 if (be->be_req_ana & AN_REQ_FLT_START_BE) {
Christopher Faulet5647fba2021-03-08 13:40:30 +0100556 s->req.flags |= CF_FLT_ANALYZE;
Christopher Faulet949b6ca2021-09-10 10:29:54 +0200557 s->req.analysers |= AN_REQ_FLT_END;
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200558 }
559 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 +0100560 s->res.flags |= CF_FLT_ANALYZE;
Christopher Faulet26eb5ea2021-08-13 14:00:46 +0200561 s->res.analysers |= AN_RES_FLT_END;
562 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200563
Christopher Faulet92d36382015-11-05 13:35:03 +0100564 return 0;
565}
566
Christopher Fauletd7c91962015-04-30 11:48:27 +0200567
568/*
569 * Calls 'http_end' callback for all filters attached to a stream. All filters
570 * are called here, but only if there is at least one "data" filter. This
571 * functions is called when all data were parsed and forwarded. 'http_end'
572 * callback is resumable, so this function returns a negative value if an error
573 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
574 */
575int
576flt_http_end(struct stream *s, struct http_msg *msg)
577{
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100578 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
579 unsigned int offset = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200580 int ret = 1;
581
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100582 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 +0200583 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100584 unsigned long long flt_off = FLT_OFF(filter, msg->chn);
585 offset = flt_off - *strm_off;
586
Christopher Faulet401e6db2020-11-24 09:49:01 +0100587 /* Call http_end for data filters only. But the filter offset is
588 * still valid for all filters
589 . */
590 if (!IS_DATA_FILTER(filter, msg->chn))
591 continue;
592
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100593 if (FLT_OPS(filter)->http_end) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100594 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100595 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200596 if (ret <= 0)
597 BREAK_EXECUTION(s, msg->chn, end);
598 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200599 } RESUME_FILTER_END;
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100600
601 c_adv(msg->chn, offset);
602 *strm_off += offset;
603
Christopher Fauletd7c91962015-04-30 11:48:27 +0200604end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100605 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200606 return ret;
607}
608
609/*
610 * Calls 'http_reset' callback for all filters attached to a stream. This
611 * happens when a 100-continue response is received.
612 */
613void
614flt_http_reset(struct stream *s, struct http_msg *msg)
615{
616 struct filter *filter;
617
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100618 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 +0100619 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100620 if (FLT_OPS(filter)->http_reset) {
621 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100622 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100623 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200624 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100625 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200626}
627
628/*
629 * Calls 'http_reply' callback for all filters attached to a stream when HA
630 * decides to stop the HTTP message processing.
631 */
632void
Willy Tarreau83061a82018-07-13 11:56:34 +0200633flt_http_reply(struct stream *s, short status, const struct buffer *msg)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200634{
635 struct filter *filter;
636
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100637 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 +0100638 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100639 if (FLT_OPS(filter)->http_reply) {
640 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100641 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100642 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200643 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100644 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200645}
646
647/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100648 * Calls 'http_payload' callback for all "data" filters attached to a
649 * stream. This function is called when some data can be forwarded in the
650 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
651 * update the filters and the stream offset to be sure that a filter cannot
652 * forward more data than its predecessors. A filter can choose to not forward
653 * all data. Returns a negative value if an error occurs, else the number of
654 * forwarded bytes.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100655 */
656int
657flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len)
658{
659 struct filter *filter;
660 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
Christopher Faulet421e7692019-06-13 11:16:45 +0200661 unsigned int out = co_data(msg->chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100662 int ret, data;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100663
Christopher Faulet6071c2d2021-01-25 12:02:00 +0100664 strm_flt(s)->flags &= ~STRM_FLT_FL_HOLD_HTTP_HDRS;
665
Christopher Faulet81340d72020-02-26 15:47:22 +0100666 ret = data = len - out;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100667 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 +0100668 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet401e6db2020-11-24 09:49:01 +0100669 unsigned long long *flt_off = &FLT_OFF(filter, msg->chn);
670 unsigned int offset = *flt_off - *strm_off;
671
672 /* Call http_payload for filters only. Forward all data for
673 * others and update the filter offset
674 */
675 if (!IS_DATA_FILTER(filter, msg->chn)) {
676 *flt_off += data - offset;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100677 continue;
Christopher Faulet401e6db2020-11-24 09:49:01 +0100678 }
Christopher Faulet75bc9132018-11-30 15:18:09 +0100679
Christopher Faulet401e6db2020-11-24 09:49:01 +0100680 if (FLT_OPS(filter)->http_payload) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100681 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100682 ret = FLT_OPS(filter)->http_payload(s, filter, msg, out + offset, data - offset);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100683 if (ret < 0)
684 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100685 data = ret + *flt_off - *strm_off;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100686 *flt_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100687 }
688 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100689
Christopher Faulet6071c2d2021-01-25 12:02:00 +0100690 /* If nothing was forwarded yet, we take care to hold the headers if
691 * following conditions are met :
692 *
693 * - *strm_off == 0 (nothing forwarded yet)
694 * - ret == 0 (no data forwarded at all on this turn)
695 * - STRM_FLT_FL_HOLD_HTTP_HDRS flag set (at least one filter want to hold the headers)
696 *
697 * Be careful, STRM_FLT_FL_HOLD_HTTP_HDRS is removed before each http_payload loop.
698 * Thus, it must explicitly be set when necessary. We must do that to hold the headers
699 * when there is no payload.
700 */
701 if (!ret && !*strm_off && (strm_flt(s)->flags & STRM_FLT_FL_HOLD_HTTP_HDRS))
702 goto end;
703
704 ret = data;
705 *strm_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100706 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100707 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100708 return ret;
709}
710
711/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200712 * Calls 'channel_start_analyze' callback for all filters attached to a
713 * stream. This function is called when we start to analyze a request or a
714 * response. For frontend filters, it is called before all other analyzers. For
715 * backend ones, it is called before all backend
716 * analyzers. 'channel_start_analyze' callback is resumable, so this function
717 * returns 0 if an error occurs or if it needs to wait, any other value
718 * otherwise.
719 */
720int
721flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
722{
723 int ret = 1;
724
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100725 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
726
Christopher Fauletd7c91962015-04-30 11:48:27 +0200727 /* If this function is called, this means there is at least one filter,
728 * so we do not need to check the filter list's emptiness. */
729
Christopher Faulete6006242017-03-10 11:52:44 +0100730 /* Set flag on channel to tell that the channel is filtered */
731 chn->flags |= CF_FLT_ANALYZE;
Christopher Faulet949b6ca2021-09-10 10:29:54 +0200732 chn->analysers |= ((chn->flags & CF_ISRESP) ? AN_RES_FLT_END : AN_REQ_FLT_END);
Christopher Faulete6006242017-03-10 11:52:44 +0100733
Christopher Fauletd7c91962015-04-30 11:48:27 +0200734 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100735 if (!(chn->flags & CF_ISRESP)) {
736 if (an_bit == AN_REQ_FLT_START_BE &&
737 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
738 continue;
739 }
740 else {
741 if (an_bit == AN_RES_FLT_START_BE &&
742 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
743 continue;
744 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200745
Christopher Fauletb2e58492019-11-12 11:13:01 +0100746 FLT_OFF(filter, chn) = 0;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100747 if (FLT_OPS(filter)->channel_start_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100748 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100749 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200750 if (ret <= 0)
751 BREAK_EXECUTION(s, chn, end);
752 }
753 } RESUME_FILTER_END;
754
755 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100756 ret = handle_analyzer_result(s, chn, an_bit, ret);
757 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
758 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200759}
760
761/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200762 * Calls 'channel_pre_analyze' callback for all filters attached to a
763 * stream. This function is called BEFORE each analyzer attached to a channel,
764 * expects analyzers responsible for data sending. 'channel_pre_analyze'
765 * callback is resumable, so this function returns 0 if an error occurs or if it
766 * needs to wait, any other value otherwise.
767 *
768 * Note this function can be called many times for the same analyzer. In fact,
769 * it is called until the analyzer finishes its processing.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200770 */
771int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200772flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200773{
774 int ret = 1;
775
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100776 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
777
Christopher Fauletd7c91962015-04-30 11:48:27 +0200778 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200779 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100780 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200781 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200782 if (ret <= 0)
783 BREAK_EXECUTION(s, chn, check_result);
Christopher Fauleta6d37042021-05-20 18:00:55 +0200784 filter->pre_analyzers &= ~an_bit;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200785 }
786 } RESUME_FILTER_END;
787
788 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100789 ret = handle_analyzer_result(s, chn, 0, ret);
790 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
791 return ret;
Christopher Faulet309c6412015-12-02 09:57:32 +0100792}
793
794/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200795 * Calls 'channel_post_analyze' callback for all filters attached to a
796 * stream. This function is called AFTER each analyzer attached to a channel,
797 * expects analyzers responsible for data sending. 'channel_post_analyze'
798 * callback is NOT resumable, so this function returns a 0 if an error occurs,
799 * any other value otherwise.
800 *
801 * Here, AFTER means when the analyzer finishes its processing.
802 */
803int
804flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
805{
806 struct filter *filter;
807 int ret = 1;
808
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100809 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
810
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200811 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
812 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100813 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200814 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
815 if (ret < 0)
816 break;
Christopher Fauleta6d37042021-05-20 18:00:55 +0200817 filter->post_analyzers &= ~an_bit;
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200818 }
819 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100820 ret = handle_analyzer_result(s, chn, 0, ret);
821 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
822 return ret;
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200823}
824
825/*
Christopher Faulet0184ea72017-01-05 14:06:34 +0100826 * This function is the AN_REQ/RES_FLT_HTTP_HDRS analyzer, used to filter HTTP
827 * headers or a request or a response. Returns 0 if an error occurs or if it
828 * needs to wait, any other value otherwise.
Christopher Faulet309c6412015-12-02 09:57:32 +0100829 */
830int
831flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
832{
Christopher Faulet1339d742016-05-11 16:48:33 +0200833 struct http_msg *msg;
834 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100835
Christopher Faulet1339d742016-05-11 16:48:33 +0200836 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100837 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
838
Christopher Faulet309c6412015-12-02 09:57:32 +0100839 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200840 if (FLT_OPS(filter)->http_headers) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100841 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet1339d742016-05-11 16:48:33 +0200842 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100843 if (ret <= 0)
844 BREAK_EXECUTION(s, chn, check_result);
845 }
846 } RESUME_FILTER_END;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100847
848 if (HAS_DATA_FILTERS(s, chn)) {
849 size_t data = http_get_hdrs_size(htxbuf(&chn->buf));
850 struct filter *f;
851
Christopher Faulet401e6db2020-11-24 09:49:01 +0100852 list_for_each_entry(f, &strm_flt(s)->filters, list)
853 FLT_OFF(f, chn) = data;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100854 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100855
856 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100857 ret = handle_analyzer_result(s, chn, an_bit, ret);
858 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
859 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200860}
861
862/*
863 * Calls 'channel_end_analyze' callback for all filters attached to a
864 * stream. This function is called when we stop to analyze a request or a
865 * response. It is called after all other analyzers. 'channel_end_analyze'
866 * callback is resumable, so this function returns 0 if an error occurs or if it
867 * needs to wait, any other value otherwise.
868 */
869int
870flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
871{
872 int ret = 1;
873
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100874 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
875
Christopher Faulete6006242017-03-10 11:52:44 +0100876 /* Check if all filters attached on the stream have finished their
877 * processing on this channel. */
878 if (!(chn->flags & CF_FLT_ANALYZE))
879 goto sync;
880
Christopher Fauletd7c91962015-04-30 11:48:27 +0200881 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100882 FLT_OFF(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100883 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200884
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100885 if (FLT_OPS(filter)->channel_end_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100886 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100887 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200888 if (ret <= 0)
889 BREAK_EXECUTION(s, chn, end);
890 }
891 } RESUME_FILTER_END;
892
Christopher Faulete6006242017-03-10 11:52:44 +0100893 end:
894 /* We don't remove yet this analyzer because we need to synchronize the
895 * both channels. So here, we just remove the flag CF_FLT_ANALYZE. */
896 ret = handle_analyzer_result(s, chn, 0, ret);
Christopher Faulet570f7992017-07-06 15:53:02 +0200897 if (ret) {
Christopher Faulete6006242017-03-10 11:52:44 +0100898 chn->flags &= ~CF_FLT_ANALYZE;
Christopher Faulet02c7b222015-12-22 12:01:29 +0100899
Christopher Faulet570f7992017-07-06 15:53:02 +0200900 /* Pretend there is an activity on both channels. Flag on the
901 * current one will be automatically removed, so only the other
902 * one will remain. This is a way to be sure that
903 * 'channel_end_analyze' callback will have a chance to be
904 * called at least once for the other side to finish the current
Joseph Herlantb35ea682018-11-15 12:24:23 -0800905 * processing. Of course, this is the filter responsibility to
Christopher Faulet570f7992017-07-06 15:53:02 +0200906 * wakeup the stream if it choose to loop on this callback. */
907 s->req.flags |= CF_WAKE_ONCE;
908 s->res.flags |= CF_WAKE_ONCE;
909 }
910
911
Christopher Faulete6006242017-03-10 11:52:44 +0100912 sync:
913 /* Now we can check if filters have finished their work on the both
914 * channels */
915 if (!(s->req.flags & CF_FLT_ANALYZE) && !(s->res.flags & CF_FLT_ANALYZE)) {
916 /* Sync channels by removing this analyzer for the both channels */
917 s->req.analysers &= ~AN_REQ_FLT_END;
918 s->res.analysers &= ~AN_RES_FLT_END;
Christopher Fauletc6062be2016-10-31 11:22:37 +0100919
Christopher Faulete6006242017-03-10 11:52:44 +0100920 /* Remove backend filters from the list */
921 flt_stream_release(s, 1);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100922 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200923 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100924 else {
925 DBG_TRACE_DEVEL("waiting for sync", STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
926 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200927 return ret;
928}
929
Christopher Fauletd7c91962015-04-30 11:48:27 +0200930
931/*
Christopher Fauletb2e58492019-11-12 11:13:01 +0100932 * Calls 'tcp_payload' callback for all "data" filters attached to a
933 * stream. This function is called when some data can be forwarded in the
934 * AN_REQ_FLT_XFER_BODY and AN_RES_FLT_XFER_BODY analyzers. It takes care to
935 * update the filters and the stream offset to be sure that a filter cannot
936 * forward more data than its predecessors. A filter can choose to not forward
937 * all data. Returns a negative value if an error occurs, else the number of
938 * forwarded bytes.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200939 */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100940int
941flt_tcp_payload(struct stream *s, struct channel *chn, unsigned int len)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200942{
Christopher Fauletda02e172015-12-04 09:25:05 +0100943 struct filter *filter;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100944 unsigned long long *strm_off = &FLT_STRM_OFF(s, chn);
945 unsigned int out = co_data(chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100946 int ret, data;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200947
Christopher Faulet81340d72020-02-26 15:47:22 +0100948 ret = data = len - out;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100949 DBG_TRACE_ENTER(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100950 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet401e6db2020-11-24 09:49:01 +0100951 unsigned long long *flt_off = &FLT_OFF(filter, chn);
952 unsigned int offset = *flt_off - *strm_off;
953
954 /* Call tcp_payload for filters only. Forward all data for
955 * others and update the filter offset
956 */
957 if (!IS_DATA_FILTER(filter, chn)) {
958 *flt_off += data - offset;
Christopher Fauletda02e172015-12-04 09:25:05 +0100959 continue;
Christopher Faulet401e6db2020-11-24 09:49:01 +0100960 }
961
Christopher Fauletb2e58492019-11-12 11:13:01 +0100962 if (FLT_OPS(filter)->tcp_payload) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100963
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100964 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100965 ret = FLT_OPS(filter)->tcp_payload(s, filter, chn, out + offset, data - offset);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200966 if (ret < 0)
967 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100968 data = ret + *flt_off - *strm_off;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100969 *flt_off += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200970 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200971 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100972
973 /* Only forward data if the last filter decides to forward something */
974 if (ret > 0) {
975 ret = data;
976 *strm_off += ret;
977 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200978 end:
Christopher Fauletb2e58492019-11-12 11:13:01 +0100979 DBG_TRACE_LEAVE(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200980 return ret;
981}
982
983/*
984 * Called when TCP data must be filtered on a channel. This function is the
Christopher Faulet0184ea72017-01-05 14:06:34 +0100985 * AN_REQ/RES_FLT_XFER_DATA analyzer. When called, it is responsible to forward
986 * data when the proxy is not in http mode. Behind the scene, it calls
987 * consecutively 'tcp_data' and 'tcp_forward_data' callbacks for all "data"
988 * filters attached to a stream. Returns 0 if an error occurs or if it needs to
989 * wait, any other value otherwise.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200990 */
991int
992flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
993{
Christopher Fauletb2e58492019-11-12 11:13:01 +0100994 unsigned int len;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200995 int ret = 1;
996
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100997 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
998
Christopher Fauletda02e172015-12-04 09:25:05 +0100999 /* If there is no "data" filters, we do nothing */
Christopher Fauletb2e58492019-11-12 11:13:01 +01001000 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletda02e172015-12-04 09:25:05 +01001001 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001002
1003 /* Be sure that the output is still opened. Else we stop the data
1004 * filtering. */
1005 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau44a41a82018-06-19 07:16:31 +02001006 ((chn->flags & CF_SHUTW) && (chn->to_forward || co_data(chn))))
Christopher Fauletd7c91962015-04-30 11:48:27 +02001007 goto end;
1008
Christopher Fauletb2e58492019-11-12 11:13:01 +01001009 if (s->flags & SF_HTX) {
1010 struct htx *htx = htxbuf(&chn->buf);
1011 len = htx->data;
1012 }
1013 else
1014 len = c_data(chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001015
Christopher Fauletb2e58492019-11-12 11:13:01 +01001016 ret = flt_tcp_payload(s, chn, len);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001017 if (ret < 0)
1018 goto end;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001019 c_adv(chn, ret);
Christopher Fauletda02e172015-12-04 09:25:05 +01001020
Christopher Fauletd7c91962015-04-30 11:48:27 +02001021 /* Stop waiting data if the input in closed and no data is pending or if
1022 * the output is closed. */
Christopher Fauletb2e58492019-11-12 11:13:01 +01001023 if (chn->flags & CF_SHUTW) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001024 ret = 1;
1025 goto end;
1026 }
Christopher Fauletb2e58492019-11-12 11:13:01 +01001027 if (chn->flags & CF_SHUTR) {
1028 if (((s->flags & SF_HTX) && htx_is_empty(htxbuf(&chn->buf))) || c_empty(chn)) {
1029 ret = 1;
1030 goto end;
1031 }
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{
1053 int finst;
Christopher Faulete058f732019-09-06 15:24:55 +02001054 int status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001055
1056 if (ret < 0)
1057 goto return_bad_req;
1058 else if (!ret)
1059 goto wait;
1060
1061 /* End of job, return OK */
1062 if (an_bit) {
1063 chn->analysers &= ~an_bit;
1064 chn->analyse_exp = TICK_ETERNITY;
1065 }
1066 return 1;
1067
1068 return_bad_req:
1069 /* An error occurs */
1070 channel_abort(&s->req);
1071 channel_abort(&s->res);
1072
1073 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001074 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001075 finst = SF_FINST_R;
Christopher Faulete058f732019-09-06 15:24:55 +02001076 status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001077 /* FIXME: incr counters */
1078 }
1079 else {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001080 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001081 finst = SF_FINST_H;
Christopher Faulete058f732019-09-06 15:24:55 +02001082 status = 502;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001083 /* FIXME: incr counters */
1084 }
1085
Christopher Faulet3d119692019-07-15 22:04:51 +02001086 if (IS_HTX_STRM(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001087 /* Do not do that when we are waiting for the next request */
Christopher Faulete058f732019-09-06 15:24:55 +02001088 if (s->txn->status > 0)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001089 http_reply_and_close(s, s->txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001090 else {
Christopher Faulete058f732019-09-06 15:24:55 +02001091 s->txn->status = status;
1092 http_reply_and_close(s, status, http_error_message(s));
Christopher Fauletd7c91962015-04-30 11:48:27 +02001093 }
1094 }
1095
1096 if (!(s->flags & SF_ERR_MASK))
1097 s->flags |= SF_ERR_PRXCOND;
1098 if (!(s->flags & SF_FINST_MASK))
1099 s->flags |= finst;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001100 DBG_TRACE_DEVEL("leaving on error", STRM_EV_FLT_ANA|STRM_EV_FLT_ERR, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001101 return 0;
1102
1103 wait:
1104 if (!(chn->flags & CF_ISRESP))
1105 channel_dont_connect(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001106 DBG_TRACE_DEVEL("wairing for more data", STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001107 return 0;
1108}
1109
1110
1111/* Note: must not be declared <const> as its list will be overwritten.
1112 * Please take care of keeping this list alphabetically sorted, doing so helps
1113 * all code contributors.
1114 * Optional keywords are also declared with a NULL ->parse() function so that
1115 * the config parser can report an appropriate error when a known keyword was
1116 * not enabled. */
1117static struct cfg_kw_list cfg_kws = {ILH, {
1118 { CFG_LISTEN, "filter", parse_filter },
1119 { 0, NULL, NULL },
1120 }
1121};
1122
Willy Tarreau0108d902018-11-25 19:14:37 +01001123INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1124
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001125REGISTER_POST_CHECK(flt_init_all);
1126REGISTER_PER_THREAD_INIT(flt_init_all_per_thread);
1127REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread);
1128
Christopher Fauletd7c91962015-04-30 11:48:27 +02001129/*
1130 * Local variables:
1131 * c-indent-level: 8
1132 * c-basic-offset: 8
1133 * End:
1134 */