blob: f64c192bdbead6e90f82772865cb6c24ee2c240b [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.
133 */
134void
135flt_dump_kws(char **out)
136{
137 struct flt_kw_list *kwl;
138 int index;
139
Christopher Faulet784063e2020-05-18 12:14:18 +0200140 if (!out)
141 return;
142
Christopher Fauletd7c91962015-04-30 11:48:27 +0200143 *out = NULL;
144 list_for_each_entry(kwl, &flt_keywords.list, list) {
145 for (index = 0; kwl->kw[index].kw != NULL; index++) {
146 if (kwl->kw[index].parse ||
147 flt_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
148 memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "",
149 kwl->scope,
150 kwl->kw[index].kw,
151 kwl->kw[index].parse ? "" : " (not supported)");
152 }
153 }
154 }
155}
156
157/*
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100158 * Lists the known filters on <out>
159 */
160void
161list_filters(FILE *out)
162{
163 char *filters, *p, *f;
164
165 fprintf(out, "Available filters :\n");
166 flt_dump_kws(&filters);
167 for (p = filters; (f = strtok_r(p,"\n",&p));)
168 fprintf(out, "\t%s\n", f);
169 free(filters);
170}
171
172/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200173 * Parses the "filter" keyword. All keywords must be handled by filters
174 * themselves
175 */
176static int
177parse_filter(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100178 const struct proxy *defpx, const char *file, int line, char **err)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200179{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100180 struct flt_conf *fconf = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200181
182 /* Filter cannot be defined on a default proxy */
183 if (curpx == defpx) {
Christopher Fauletcc7317d2016-04-04 10:51:17 +0200184 memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.",
Christopher Fauletd7c91962015-04-30 11:48:27 +0200185 file, line, args[0]);
186 return -1;
187 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100188 if (strcmp(args[0], "filter") == 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200189 struct flt_kw *kw;
190 int cur_arg;
191
192 if (!*args[1]) {
193 memprintf(err,
194 "parsing [%s:%d] : missing argument for '%s' in %s '%s'.",
195 file, line, args[0], proxy_type_str(curpx), curpx->id);
196 goto error;
197 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100198 fconf = calloc(1, sizeof(*fconf));
199 if (!fconf) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200200 memprintf(err, "'%s' : out of memory", args[0]);
201 goto error;
202 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200203
204 cur_arg = 1;
205 kw = flt_find_kw(args[cur_arg]);
206 if (kw) {
207 if (!kw->parse) {
208 memprintf(err, "parsing [%s:%d] : '%s' : "
209 "'%s' option is not implemented in this version (check build options).",
210 file, line, args[0], args[cur_arg]);
211 goto error;
212 }
Thierry Fournier3610c392016-04-13 18:27:51 +0200213 if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200214 if (err && *err)
215 memprintf(err, "'%s' : '%s'",
216 args[0], *err);
217 else
218 memprintf(err, "'%s' : error encountered while processing '%s'",
219 args[0], args[cur_arg]);
220 goto error;
221 }
222 }
223 else {
224 flt_dump_kws(err);
225 indent_msg(err, 4);
226 memprintf(err, "'%s' : unknown keyword '%s'.%s%s",
227 args[0], args[cur_arg],
228 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
229 goto error;
230 }
231 if (*args[cur_arg]) {
232 memprintf(err, "'%s %s' : unknown keyword '%s'.",
233 args[0], args[1], args[cur_arg]);
234 goto error;
235 }
Christopher Faulet00e818a2016-04-19 17:00:44 +0200236 if (fconf->ops == NULL) {
237 memprintf(err, "'%s %s' : no callbacks defined.",
238 args[0], args[1]);
239 goto error;
240 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200241
Willy Tarreau2b718102021-04-21 07:32:39 +0200242 LIST_APPEND(&curpx->filter_configs, &fconf->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200243 }
244 return 0;
245
246 error:
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100247 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200248 return -1;
249
250
251}
252
253/*
254 * Calls 'init' callback for all filters attached to a proxy. This happens after
255 * the configuration parsing. Filters can finish to fill their config. Returns
256 * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
257 */
Willy Tarreau64bca592016-12-21 20:13:11 +0100258static int
Christopher Fauletd7c91962015-04-30 11:48:27 +0200259flt_init(struct proxy *proxy)
260{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100261 struct flt_conf *fconf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200262
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100263 list_for_each_entry(fconf, &proxy->filter_configs, list) {
264 if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200265 return ERR_ALERT|ERR_FATAL;
266 }
267 return 0;
268}
269
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200270/*
271 * Calls 'init_per_thread' callback for all filters attached to a proxy for each
272 * threads. This happens after the thread creation. Filters can finish to fill
273 * their config. Returns (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
274 */
275static int
276flt_init_per_thread(struct proxy *proxy)
277{
278 struct flt_conf *fconf;
279
280 list_for_each_entry(fconf, &proxy->filter_configs, list) {
281 if (fconf->ops->init_per_thread && fconf->ops->init_per_thread(proxy, fconf) < 0)
282 return ERR_ALERT|ERR_FATAL;
283 }
284 return 0;
285}
286
Willy Tarreau64bca592016-12-21 20:13:11 +0100287/* Calls flt_init() for all proxies, see above */
288static int
289flt_init_all()
290{
291 struct proxy *px;
Christopher Fauletfc633b62020-11-06 15:24:23 +0100292 int err_code = ERR_NONE;
Willy Tarreau64bca592016-12-21 20:13:11 +0100293
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100294 for (px = proxies_list; px; px = px->next) {
Christopher Faulet400829c2020-11-02 16:08:09 +0100295 if (px->disabled) {
296 flt_deinit(px);
297 continue;
298 }
Willy Tarreau64bca592016-12-21 20:13:11 +0100299 err_code |= flt_init(px);
300 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100301 ha_alert("Failed to initialize filters for proxy '%s'.\n",
302 px->id);
Willy Tarreau64bca592016-12-21 20:13:11 +0100303 return err_code;
304 }
305 }
306 return 0;
307}
308
Joseph Herlantb35ea682018-11-15 12:24:23 -0800309/* Calls flt_init_per_thread() for all proxies, see above. Be careful here, it
310 * returns 0 if an error occurred. This is the opposite of flt_init_all. */
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200311static int
312flt_init_all_per_thread()
313{
314 struct proxy *px;
315 int err_code = 0;
316
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100317 for (px = proxies_list; px; px = px->next) {
Christopher Faulet400829c2020-11-02 16:08:09 +0100318 if (px->disabled)
319 continue;
320
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200321 err_code = flt_init_per_thread(px);
322 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100323 ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n",
324 px->id, tid);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200325 return 0;
326 }
327 }
328 return 1;
329}
330
Christopher Fauletd7c91962015-04-30 11:48:27 +0200331/*
332 * Calls 'check' callback for all filters attached to a proxy. This happens
333 * after the configuration parsing but before filters initialization. Returns
334 * the number of encountered errors.
335 */
336int
337flt_check(struct proxy *proxy)
338{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100339 struct flt_conf *fconf;
340 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200341
Christopher Fauletff17b182019-01-07 15:03:22 +0100342 err += check_implicit_http_comp_flt(proxy);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100343 list_for_each_entry(fconf, &proxy->filter_configs, list) {
344 if (fconf->ops->check)
345 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200346 }
347 return err;
348}
349
350/*
351 * Calls 'denit' callback for all filters attached to a proxy. This happens when
352 * HAProxy is stopped.
353 */
354void
355flt_deinit(struct proxy *proxy)
356{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100357 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200358
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100359 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
Christopher Faulet743bd6a2020-11-03 16:40:37 +0100360 if (fconf->ops->deinit)
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100361 fconf->ops->deinit(proxy, fconf);
Willy Tarreau2b718102021-04-21 07:32:39 +0200362 LIST_DELETE(&fconf->list);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100363 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200364 }
365}
366
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200367/*
368 * Calls 'denit_per_thread' callback for all filters attached to a proxy for
369 * each threads. This happens before exiting a thread.
370 */
371void
372flt_deinit_per_thread(struct proxy *proxy)
373{
374 struct flt_conf *fconf, *back;
375
376 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
377 if (fconf->ops->deinit_per_thread)
378 fconf->ops->deinit_per_thread(proxy, fconf);
379 }
380}
381
382
383/* Calls flt_deinit_per_thread() for all proxies, see above */
384static void
385flt_deinit_all_per_thread()
386{
387 struct proxy *px;
388
Christopher Faulet743bd6a2020-11-03 16:40:37 +0100389 for (px = proxies_list; px; px = px->next)
390 flt_deinit_per_thread(px);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200391}
392
Christopher Faulet92d36382015-11-05 13:35:03 +0100393/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
394static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100395flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100396{
Christopher Faulet75bc9132018-11-30 15:18:09 +0100397 struct filter *f;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200398
Christopher Faulet0f17a9b2019-04-05 10:11:38 +0200399 if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX))
Christopher Faulet75bc9132018-11-30 15:18:09 +0100400 return 0;
401
Willy Tarreau1bbec382021-03-22 21:02:50 +0100402 f = pool_zalloc(pool_head_filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100403 if (!f) /* not enough memory */
404 return -1;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100405 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100406 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200407
408 if (FLT_OPS(f)->attach) {
409 int ret = FLT_OPS(f)->attach(s, f);
410 if (ret <= 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100411 pool_free(pool_head_filter, f);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200412 return ret;
413 }
414 }
415
Willy Tarreau2b718102021-04-21 07:32:39 +0200416 LIST_APPEND(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100417 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100418 return 0;
419}
420
421/*
422 * Called when a stream is created. It attaches all frontend filters to the
423 * stream. Returns -1 if an error occurs, 0 otherwise.
424 */
425int
426flt_stream_init(struct stream *s)
427{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100428 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100429
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100430 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
431 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100432 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
433 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100434 return -1;
435 }
436 return 0;
437}
438
439/*
440 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
441 * happens after each request/response exchange). When analyze ends, backend
442 * filters are removed. When the stream is closed, all filters attached to the
443 * stream are removed.
444 */
445void
446flt_stream_release(struct stream *s, int only_backend)
447{
448 struct filter *filter, *back;
449
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100450 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100451 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200452 if (FLT_OPS(filter)->detach)
453 FLT_OPS(filter)->detach(s, filter);
Willy Tarreau2b718102021-04-21 07:32:39 +0200454 LIST_DELETE(&filter->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100455 pool_free(pool_head_filter, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100456 }
457 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100458 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100459 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100460}
461
Christopher Fauletd7c91962015-04-30 11:48:27 +0200462/*
463 * Calls 'stream_start' for all filters attached to a stream. This happens when
464 * the stream is created, just after calling flt_stream_init
465 * function. Returns -1 if an error occurs, 0 otherwise.
466 */
467int
468flt_stream_start(struct stream *s)
469{
470 struct filter *filter;
471
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100472 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100473 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200474 return -1;
475 }
Christopher Fauletfeca2a42021-08-13 14:00:46 +0200476 if (strm_li(s) && (strm_li(s)->analysers & AN_REQ_FLT_START_FE)) {
Christopher Faulet5647fba2021-03-08 13:40:30 +0100477 s->req.flags |= CF_FLT_ANALYZE;
Christopher Faulet4a9db772021-10-04 07:50:13 +0200478 s->req.analysers |= AN_REQ_FLT_END;
Christopher Fauletfeca2a42021-08-13 14:00:46 +0200479 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200480 return 0;
481}
482
483/*
484 * Calls 'stream_stop' for all filters attached to a stream. This happens when
485 * the stream is stopped, just before calling flt_stream_release function.
486 */
487void
488flt_stream_stop(struct stream *s)
489{
490 struct filter *filter;
491
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100492 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100493 if (FLT_OPS(filter)->stream_stop)
494 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200495 }
496}
497
Christopher Faulet92d36382015-11-05 13:35:03 +0100498/*
Christopher Fauleta00d8172016-11-10 14:58:05 +0100499 * Calls 'check_timeouts' for all filters attached to a stream. This happens when
500 * the stream is woken up because of expired timer.
501 */
502void
503flt_stream_check_timeouts(struct stream *s)
504{
505 struct filter *filter;
506
507 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
508 if (FLT_OPS(filter)->check_timeouts)
509 FLT_OPS(filter)->check_timeouts(s, filter);
510 }
511}
512
513/*
Christopher Faulet92d36382015-11-05 13:35:03 +0100514 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200515 * are not the same, this function attaches all backend filters to the
516 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100517 */
518int
519flt_set_stream_backend(struct stream *s, struct proxy *be)
520{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100521 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200522 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100523
524 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200525 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100526
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100527 list_for_each_entry(fconf, &be->filter_configs, list) {
528 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100529 return -1;
530 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200531
532 end:
533 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
534 if (FLT_OPS(filter)->stream_set_backend &&
535 FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0)
536 return -1;
537 }
Christopher Fauletfeca2a42021-08-13 14:00:46 +0200538 if (be->be_req_ana & AN_REQ_FLT_START_BE) {
Christopher Faulet5647fba2021-03-08 13:40:30 +0100539 s->req.flags |= CF_FLT_ANALYZE;
Christopher Faulet2f3ead12021-09-10 10:29:54 +0200540 s->req.analysers |= AN_REQ_FLT_END;
Christopher Fauletfeca2a42021-08-13 14:00:46 +0200541 }
542 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 +0100543 s->res.flags |= CF_FLT_ANALYZE;
Christopher Fauletfeca2a42021-08-13 14:00:46 +0200544 s->res.analysers |= AN_RES_FLT_END;
545 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200546
Christopher Faulet92d36382015-11-05 13:35:03 +0100547 return 0;
548}
549
Christopher Fauletd7c91962015-04-30 11:48:27 +0200550
551/*
552 * Calls 'http_end' callback for all filters attached to a stream. All filters
553 * are called here, but only if there is at least one "data" filter. This
554 * functions is called when all data were parsed and forwarded. 'http_end'
555 * callback is resumable, so this function returns a negative value if an error
556 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
557 */
558int
559flt_http_end(struct stream *s, struct http_msg *msg)
560{
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100561 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
562 unsigned int offset = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200563 int ret = 1;
564
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100565 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 +0200566 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100567 unsigned long long flt_off = FLT_OFF(filter, msg->chn);
568 offset = flt_off - *strm_off;
569
Christopher Faulet401e6db2020-11-24 09:49:01 +0100570 /* Call http_end for data filters only. But the filter offset is
571 * still valid for all filters
572 . */
573 if (!IS_DATA_FILTER(filter, msg->chn))
574 continue;
575
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100576 if (FLT_OPS(filter)->http_end) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100577 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100578 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200579 if (ret <= 0)
580 BREAK_EXECUTION(s, msg->chn, end);
581 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200582 } RESUME_FILTER_END;
Christopher Faulet22fca1f2020-11-16 10:10:38 +0100583
584 c_adv(msg->chn, offset);
585 *strm_off += offset;
586
Christopher Fauletd7c91962015-04-30 11:48:27 +0200587end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100588 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200589 return ret;
590}
591
592/*
593 * Calls 'http_reset' callback for all filters attached to a stream. This
594 * happens when a 100-continue response is received.
595 */
596void
597flt_http_reset(struct stream *s, struct http_msg *msg)
598{
599 struct filter *filter;
600
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100601 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 +0100602 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100603 if (FLT_OPS(filter)->http_reset) {
604 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100605 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100606 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200607 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100608 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200609}
610
611/*
612 * Calls 'http_reply' callback for all filters attached to a stream when HA
613 * decides to stop the HTTP message processing.
614 */
615void
Willy Tarreau83061a82018-07-13 11:56:34 +0200616flt_http_reply(struct stream *s, short status, const struct buffer *msg)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200617{
618 struct filter *filter;
619
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100620 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 +0100621 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100622 if (FLT_OPS(filter)->http_reply) {
623 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100624 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100625 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200626 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100627 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200628}
629
630/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100631 * Calls 'http_payload' callback for all "data" filters attached to a
632 * stream. This function is called when some data can be forwarded in the
633 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
634 * update the filters and the stream offset to be sure that a filter cannot
635 * forward more data than its predecessors. A filter can choose to not forward
636 * all data. Returns a negative value if an error occurs, else the number of
637 * forwarded bytes.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100638 */
639int
640flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len)
641{
642 struct filter *filter;
643 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
Christopher Faulet421e7692019-06-13 11:16:45 +0200644 unsigned int out = co_data(msg->chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100645 int ret, data;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100646
Christopher Faulet6071c2d2021-01-25 12:02:00 +0100647 strm_flt(s)->flags &= ~STRM_FLT_FL_HOLD_HTTP_HDRS;
648
Christopher Faulet81340d72020-02-26 15:47:22 +0100649 ret = data = len - out;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100650 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 +0100651 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet401e6db2020-11-24 09:49:01 +0100652 unsigned long long *flt_off = &FLT_OFF(filter, msg->chn);
653 unsigned int offset = *flt_off - *strm_off;
654
655 /* Call http_payload for filters only. Forward all data for
656 * others and update the filter offset
657 */
658 if (!IS_DATA_FILTER(filter, msg->chn)) {
659 *flt_off += data - offset;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100660 continue;
Christopher Faulet401e6db2020-11-24 09:49:01 +0100661 }
Christopher Faulet75bc9132018-11-30 15:18:09 +0100662
Christopher Faulet401e6db2020-11-24 09:49:01 +0100663 if (FLT_OPS(filter)->http_payload) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100664 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100665 ret = FLT_OPS(filter)->http_payload(s, filter, msg, out + offset, data - offset);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100666 if (ret < 0)
667 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100668 data = ret + *flt_off - *strm_off;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100669 *flt_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100670 }
671 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100672
Christopher Faulet6071c2d2021-01-25 12:02:00 +0100673 /* If nothing was forwarded yet, we take care to hold the headers if
674 * following conditions are met :
675 *
676 * - *strm_off == 0 (nothing forwarded yet)
677 * - ret == 0 (no data forwarded at all on this turn)
678 * - STRM_FLT_FL_HOLD_HTTP_HDRS flag set (at least one filter want to hold the headers)
679 *
680 * Be careful, STRM_FLT_FL_HOLD_HTTP_HDRS is removed before each http_payload loop.
681 * Thus, it must explicitly be set when necessary. We must do that to hold the headers
682 * when there is no payload.
683 */
684 if (!ret && !*strm_off && (strm_flt(s)->flags & STRM_FLT_FL_HOLD_HTTP_HDRS))
685 goto end;
686
687 ret = data;
688 *strm_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100689 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100690 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100691 return ret;
692}
693
694/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200695 * Calls 'channel_start_analyze' callback for all filters attached to a
696 * stream. This function is called when we start to analyze a request or a
697 * response. For frontend filters, it is called before all other analyzers. For
698 * backend ones, it is called before all backend
699 * analyzers. 'channel_start_analyze' callback is resumable, so this function
700 * returns 0 if an error occurs or if it needs to wait, any other value
701 * otherwise.
702 */
703int
704flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
705{
706 int ret = 1;
707
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100708 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
709
Christopher Fauletd7c91962015-04-30 11:48:27 +0200710 /* If this function is called, this means there is at least one filter,
711 * so we do not need to check the filter list's emptiness. */
712
Christopher Faulete6006242017-03-10 11:52:44 +0100713 /* Set flag on channel to tell that the channel is filtered */
714 chn->flags |= CF_FLT_ANALYZE;
Christopher Faulet2f3ead12021-09-10 10:29:54 +0200715 chn->analysers |= ((chn->flags & CF_ISRESP) ? AN_RES_FLT_END : AN_REQ_FLT_END);
Christopher Faulete6006242017-03-10 11:52:44 +0100716
Christopher Fauletd7c91962015-04-30 11:48:27 +0200717 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100718 if (!(chn->flags & CF_ISRESP)) {
719 if (an_bit == AN_REQ_FLT_START_BE &&
720 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
721 continue;
722 }
723 else {
724 if (an_bit == AN_RES_FLT_START_BE &&
725 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
726 continue;
727 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200728
Christopher Fauletb2e58492019-11-12 11:13:01 +0100729 FLT_OFF(filter, chn) = 0;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100730 if (FLT_OPS(filter)->channel_start_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100731 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100732 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200733 if (ret <= 0)
734 BREAK_EXECUTION(s, chn, end);
735 }
736 } RESUME_FILTER_END;
737
738 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100739 ret = handle_analyzer_result(s, chn, an_bit, ret);
740 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
741 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200742}
743
744/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200745 * Calls 'channel_pre_analyze' callback for all filters attached to a
746 * stream. This function is called BEFORE each analyzer attached to a channel,
747 * expects analyzers responsible for data sending. 'channel_pre_analyze'
748 * callback is resumable, so this function returns 0 if an error occurs or if it
749 * needs to wait, any other value otherwise.
750 *
751 * Note this function can be called many times for the same analyzer. In fact,
752 * it is called until the analyzer finishes its processing.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200753 */
754int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200755flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200756{
757 int ret = 1;
758
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100759 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
760
Christopher Fauletd7c91962015-04-30 11:48:27 +0200761 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200762 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100763 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200764 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200765 if (ret <= 0)
766 BREAK_EXECUTION(s, chn, check_result);
Christopher Faulet476013c2021-05-20 18:00:55 +0200767 filter->pre_analyzers &= ~an_bit;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200768 }
769 } RESUME_FILTER_END;
770
771 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100772 ret = handle_analyzer_result(s, chn, 0, ret);
773 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
774 return ret;
Christopher Faulet309c6412015-12-02 09:57:32 +0100775}
776
777/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200778 * Calls 'channel_post_analyze' callback for all filters attached to a
779 * stream. This function is called AFTER each analyzer attached to a channel,
780 * expects analyzers responsible for data sending. 'channel_post_analyze'
781 * callback is NOT resumable, so this function returns a 0 if an error occurs,
782 * any other value otherwise.
783 *
784 * Here, AFTER means when the analyzer finishes its processing.
785 */
786int
787flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
788{
789 struct filter *filter;
790 int ret = 1;
791
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100792 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
793
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200794 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
795 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100796 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200797 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
798 if (ret < 0)
799 break;
Christopher Faulet476013c2021-05-20 18:00:55 +0200800 filter->post_analyzers &= ~an_bit;
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200801 }
802 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100803 ret = handle_analyzer_result(s, chn, 0, ret);
804 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
805 return ret;
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200806}
807
808/*
Christopher Faulet0184ea72017-01-05 14:06:34 +0100809 * This function is the AN_REQ/RES_FLT_HTTP_HDRS analyzer, used to filter HTTP
810 * headers or a request or a response. Returns 0 if an error occurs or if it
811 * needs to wait, any other value otherwise.
Christopher Faulet309c6412015-12-02 09:57:32 +0100812 */
813int
814flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
815{
Christopher Faulet1339d742016-05-11 16:48:33 +0200816 struct http_msg *msg;
817 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100818
Christopher Faulet1339d742016-05-11 16:48:33 +0200819 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100820 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
821
Christopher Faulet309c6412015-12-02 09:57:32 +0100822 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200823 if (FLT_OPS(filter)->http_headers) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100824 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet1339d742016-05-11 16:48:33 +0200825 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100826 if (ret <= 0)
827 BREAK_EXECUTION(s, chn, check_result);
828 }
829 } RESUME_FILTER_END;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100830
831 if (HAS_DATA_FILTERS(s, chn)) {
832 size_t data = http_get_hdrs_size(htxbuf(&chn->buf));
833 struct filter *f;
834
Christopher Faulet401e6db2020-11-24 09:49:01 +0100835 list_for_each_entry(f, &strm_flt(s)->filters, list)
836 FLT_OFF(f, chn) = data;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100837 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100838
839 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100840 ret = handle_analyzer_result(s, chn, an_bit, ret);
841 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
842 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200843}
844
845/*
846 * Calls 'channel_end_analyze' callback for all filters attached to a
847 * stream. This function is called when we stop to analyze a request or a
848 * response. It is called after all other analyzers. 'channel_end_analyze'
849 * callback is resumable, so this function returns 0 if an error occurs or if it
850 * needs to wait, any other value otherwise.
851 */
852int
853flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
854{
855 int ret = 1;
856
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100857 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
858
Christopher Faulete6006242017-03-10 11:52:44 +0100859 /* Check if all filters attached on the stream have finished their
860 * processing on this channel. */
861 if (!(chn->flags & CF_FLT_ANALYZE))
862 goto sync;
863
Christopher Fauletd7c91962015-04-30 11:48:27 +0200864 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100865 FLT_OFF(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100866 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200867
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100868 if (FLT_OPS(filter)->channel_end_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100869 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100870 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200871 if (ret <= 0)
872 BREAK_EXECUTION(s, chn, end);
873 }
874 } RESUME_FILTER_END;
875
Christopher Faulete6006242017-03-10 11:52:44 +0100876 end:
877 /* We don't remove yet this analyzer because we need to synchronize the
878 * both channels. So here, we just remove the flag CF_FLT_ANALYZE. */
879 ret = handle_analyzer_result(s, chn, 0, ret);
Christopher Faulet570f7992017-07-06 15:53:02 +0200880 if (ret) {
Christopher Faulete6006242017-03-10 11:52:44 +0100881 chn->flags &= ~CF_FLT_ANALYZE;
Christopher Faulet02c7b222015-12-22 12:01:29 +0100882
Christopher Faulet570f7992017-07-06 15:53:02 +0200883 /* Pretend there is an activity on both channels. Flag on the
884 * current one will be automatically removed, so only the other
885 * one will remain. This is a way to be sure that
886 * 'channel_end_analyze' callback will have a chance to be
887 * called at least once for the other side to finish the current
Joseph Herlantb35ea682018-11-15 12:24:23 -0800888 * processing. Of course, this is the filter responsibility to
Christopher Faulet570f7992017-07-06 15:53:02 +0200889 * wakeup the stream if it choose to loop on this callback. */
890 s->req.flags |= CF_WAKE_ONCE;
891 s->res.flags |= CF_WAKE_ONCE;
892 }
893
894
Christopher Faulete6006242017-03-10 11:52:44 +0100895 sync:
896 /* Now we can check if filters have finished their work on the both
897 * channels */
898 if (!(s->req.flags & CF_FLT_ANALYZE) && !(s->res.flags & CF_FLT_ANALYZE)) {
899 /* Sync channels by removing this analyzer for the both channels */
900 s->req.analysers &= ~AN_REQ_FLT_END;
901 s->res.analysers &= ~AN_RES_FLT_END;
Christopher Fauletc6062be2016-10-31 11:22:37 +0100902
Christopher Faulete6006242017-03-10 11:52:44 +0100903 /* Remove backend filters from the list */
904 flt_stream_release(s, 1);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100905 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200906 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100907 else {
908 DBG_TRACE_DEVEL("waiting for sync", STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
909 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200910 return ret;
911}
912
Christopher Fauletd7c91962015-04-30 11:48:27 +0200913
914/*
Christopher Fauletb2e58492019-11-12 11:13:01 +0100915 * Calls 'tcp_payload' callback for all "data" filters attached to a
916 * stream. This function is called when some data can be forwarded in the
917 * AN_REQ_FLT_XFER_BODY and AN_RES_FLT_XFER_BODY analyzers. It takes care to
918 * update the filters and the stream offset to be sure that a filter cannot
919 * forward more data than its predecessors. A filter can choose to not forward
920 * all data. Returns a negative value if an error occurs, else the number of
921 * forwarded bytes.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200922 */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100923int
924flt_tcp_payload(struct stream *s, struct channel *chn, unsigned int len)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200925{
Christopher Fauletda02e172015-12-04 09:25:05 +0100926 struct filter *filter;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100927 unsigned long long *strm_off = &FLT_STRM_OFF(s, chn);
928 unsigned int out = co_data(chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100929 int ret, data;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200930
Christopher Faulet81340d72020-02-26 15:47:22 +0100931 ret = data = len - out;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100932 DBG_TRACE_ENTER(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100933 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet401e6db2020-11-24 09:49:01 +0100934 unsigned long long *flt_off = &FLT_OFF(filter, chn);
935 unsigned int offset = *flt_off - *strm_off;
936
937 /* Call tcp_payload for filters only. Forward all data for
938 * others and update the filter offset
939 */
940 if (!IS_DATA_FILTER(filter, chn)) {
941 *flt_off += data - offset;
Christopher Fauletda02e172015-12-04 09:25:05 +0100942 continue;
Christopher Faulet401e6db2020-11-24 09:49:01 +0100943 }
944
Christopher Fauletb2e58492019-11-12 11:13:01 +0100945 if (FLT_OPS(filter)->tcp_payload) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100946
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100947 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100948 ret = FLT_OPS(filter)->tcp_payload(s, filter, chn, out + offset, data - offset);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200949 if (ret < 0)
950 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100951 data = ret + *flt_off - *strm_off;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100952 *flt_off += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200953 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200954 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100955
956 /* Only forward data if the last filter decides to forward something */
957 if (ret > 0) {
958 ret = data;
959 *strm_off += ret;
960 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200961 end:
Christopher Fauletb2e58492019-11-12 11:13:01 +0100962 DBG_TRACE_LEAVE(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200963 return ret;
964}
965
966/*
967 * Called when TCP data must be filtered on a channel. This function is the
Christopher Faulet0184ea72017-01-05 14:06:34 +0100968 * AN_REQ/RES_FLT_XFER_DATA analyzer. When called, it is responsible to forward
969 * data when the proxy is not in http mode. Behind the scene, it calls
970 * consecutively 'tcp_data' and 'tcp_forward_data' callbacks for all "data"
971 * filters attached to a stream. Returns 0 if an error occurs or if it needs to
972 * wait, any other value otherwise.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200973 */
974int
975flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
976{
Christopher Fauletb2e58492019-11-12 11:13:01 +0100977 unsigned int len;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200978 int ret = 1;
979
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100980 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
981
Christopher Fauletda02e172015-12-04 09:25:05 +0100982 /* If there is no "data" filters, we do nothing */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100983 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletda02e172015-12-04 09:25:05 +0100984 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200985
986 /* Be sure that the output is still opened. Else we stop the data
987 * filtering. */
988 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau44a41a82018-06-19 07:16:31 +0200989 ((chn->flags & CF_SHUTW) && (chn->to_forward || co_data(chn))))
Christopher Fauletd7c91962015-04-30 11:48:27 +0200990 goto end;
991
Christopher Fauletb2e58492019-11-12 11:13:01 +0100992 if (s->flags & SF_HTX) {
993 struct htx *htx = htxbuf(&chn->buf);
994 len = htx->data;
995 }
996 else
997 len = c_data(chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200998
Christopher Fauletb2e58492019-11-12 11:13:01 +0100999 ret = flt_tcp_payload(s, chn, len);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001000 if (ret < 0)
1001 goto end;
Willy Tarreaubcbd3932018-06-06 07:13:22 +02001002 c_adv(chn, ret);
Christopher Fauletda02e172015-12-04 09:25:05 +01001003
Christopher Fauletd7c91962015-04-30 11:48:27 +02001004 /* Stop waiting data if the input in closed and no data is pending or if
1005 * the output is closed. */
Christopher Fauletb2e58492019-11-12 11:13:01 +01001006 if (chn->flags & CF_SHUTW) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001007 ret = 1;
1008 goto end;
1009 }
Christopher Fauletb2e58492019-11-12 11:13:01 +01001010 if (chn->flags & CF_SHUTR) {
1011 if (((s->flags & SF_HTX) && htx_is_empty(htxbuf(&chn->buf))) || c_empty(chn)) {
1012 ret = 1;
1013 goto end;
1014 }
1015 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02001016
1017 /* Wait for data */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001018 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 +02001019 return 0;
1020 end:
1021 /* Terminate the data filtering. If <ret> is negative, an error was
1022 * encountered during the filtering. */
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001023 ret = handle_analyzer_result(s, chn, an_bit, ret);
1024 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
1025 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001026}
1027
1028/*
1029 * Handles result of filter's analyzers. It returns 0 if an error occurs or if
1030 * it needs to wait, any other value otherwise.
1031 */
1032static int
1033handle_analyzer_result(struct stream *s, struct channel *chn,
1034 unsigned int an_bit, int ret)
1035{
1036 int finst;
Christopher Faulete058f732019-09-06 15:24:55 +02001037 int status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001038
1039 if (ret < 0)
1040 goto return_bad_req;
1041 else if (!ret)
1042 goto wait;
1043
1044 /* End of job, return OK */
1045 if (an_bit) {
1046 chn->analysers &= ~an_bit;
1047 chn->analyse_exp = TICK_ETERNITY;
1048 }
1049 return 1;
1050
1051 return_bad_req:
1052 /* An error occurs */
1053 channel_abort(&s->req);
1054 channel_abort(&s->res);
1055
1056 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001057 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001058 finst = SF_FINST_R;
Christopher Faulete058f732019-09-06 15:24:55 +02001059 status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001060 /* FIXME: incr counters */
1061 }
1062 else {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001063 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001064 finst = SF_FINST_H;
Christopher Faulete058f732019-09-06 15:24:55 +02001065 status = 502;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001066 /* FIXME: incr counters */
1067 }
1068
Christopher Faulet3d119692019-07-15 22:04:51 +02001069 if (IS_HTX_STRM(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001070 /* Do not do that when we are waiting for the next request */
Christopher Faulete058f732019-09-06 15:24:55 +02001071 if (s->txn->status > 0)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001072 http_reply_and_close(s, s->txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001073 else {
Christopher Faulete058f732019-09-06 15:24:55 +02001074 s->txn->status = status;
1075 http_reply_and_close(s, status, http_error_message(s));
Christopher Fauletd7c91962015-04-30 11:48:27 +02001076 }
1077 }
1078
1079 if (!(s->flags & SF_ERR_MASK))
1080 s->flags |= SF_ERR_PRXCOND;
1081 if (!(s->flags & SF_FINST_MASK))
1082 s->flags |= finst;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001083 DBG_TRACE_DEVEL("leaving on error", STRM_EV_FLT_ANA|STRM_EV_FLT_ERR, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001084 return 0;
1085
1086 wait:
1087 if (!(chn->flags & CF_ISRESP))
1088 channel_dont_connect(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001089 DBG_TRACE_DEVEL("wairing for more data", STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001090 return 0;
1091}
1092
1093
1094/* Note: must not be declared <const> as its list will be overwritten.
1095 * Please take care of keeping this list alphabetically sorted, doing so helps
1096 * all code contributors.
1097 * Optional keywords are also declared with a NULL ->parse() function so that
1098 * the config parser can report an appropriate error when a known keyword was
1099 * not enabled. */
1100static struct cfg_kw_list cfg_kws = {ILH, {
1101 { CFG_LISTEN, "filter", parse_filter },
1102 { 0, NULL, NULL },
1103 }
1104};
1105
Willy Tarreau0108d902018-11-25 19:14:37 +01001106INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1107
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001108REGISTER_POST_CHECK(flt_init_all);
1109REGISTER_PER_THREAD_INIT(flt_init_all_per_thread);
1110REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread);
1111
Christopher Fauletd7c91962015-04-30 11:48:27 +02001112/*
1113 * Local variables:
1114 * c-indent-level: 8
1115 * c-basic-offset: 8
1116 * End:
1117 */