blob: 9b6bd5b3e698fd8fa6a450142e56d294fdf984a8 [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>
Christopher Fauletd7c91962015-04-30 11:48:27 +020014#include <common/buffer.h>
15#include <common/debug.h>
16#include <common/cfgparse.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020017#include <common/errors.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010018#include <common/htx.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020019#include <common/namespace.h>
20#include <common/standard.h>
Christopher Faulet71a6a8e2017-07-27 16:33:28 +020021#include <common/hathreads.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020022
23#include <types/filters.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020024#include <types/http_ana.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020025
26#include <proto/compression.h>
27#include <proto/filters.h>
Christopher Faulet92d36382015-11-05 13:35:03 +010028#include <proto/flt_http_comp.h>
Christopher Faulet75bc9132018-11-30 15:18:09 +010029#include <proto/http_htx.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020030#include <proto/http_ana.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020031#include <proto/stream.h>
32#include <proto/stream_interface.h>
33
Christopher Fauleteea8fc72019-11-05 16:18:10 +010034#define TRACE_SOURCE &trace_strm
35
Christopher Fauletd7c91962015-04-30 11:48:27 +020036/* Pool used to allocate filters */
Willy Tarreau8ceae722018-11-26 11:58:30 +010037DECLARE_STATIC_POOL(pool_head_filter, "filter", sizeof(struct filter));
Christopher Fauletd7c91962015-04-30 11:48:27 +020038
39static int handle_analyzer_result(struct stream *s, struct channel *chn, unsigned int an_bit, int ret);
40
41/* - RESUME_FILTER_LOOP and RESUME_FILTER_END must always be used together.
42 * The first one begins a loop and the seconds one ends it.
43 *
44 * - BREAK_EXECUTION must be used to break the loop and set the filter from
45 * which to resume the next time.
46 *
Bertrand Jacquin874a35c2018-09-10 21:26:07 +010047 * Here is an example:
Christopher Fauletd7c91962015-04-30 11:48:27 +020048 *
49 * RESUME_FILTER_LOOP(stream, channel) {
50 * ...
51 * if (cond)
52 * BREAK_EXECUTION(stream, channel, label);
53 * ...
54 * } RESUME_FILTER_END;
55 * ...
56 * label:
57 * ...
58 *
59 */
60#define RESUME_FILTER_LOOP(strm, chn) \
61 do { \
62 struct filter *filter; \
63 \
Christopher Fauletda02e172015-12-04 09:25:05 +010064 if (strm_flt(strm)->current[CHN_IDX(chn)]) { \
65 filter = strm_flt(strm)->current[CHN_IDX(chn)]; \
66 strm_flt(strm)->current[CHN_IDX(chn)] = NULL; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020067 goto resume_execution; \
68 } \
69 \
Christopher Fauletfcf035c2015-12-03 11:48:03 +010070 list_for_each_entry(filter, &strm_flt(s)->filters, list) { \
Christopher Fauletda02e172015-12-04 09:25:05 +010071 resume_execution:
Christopher Fauletd7c91962015-04-30 11:48:27 +020072
73#define RESUME_FILTER_END \
74 } \
75 } while(0)
76
Christopher Fauletda02e172015-12-04 09:25:05 +010077#define BREAK_EXECUTION(strm, chn, label) \
78 do { \
79 strm_flt(strm)->current[CHN_IDX(chn)] = filter; \
80 goto label; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020081 } while (0)
82
83
84/* List head of all known filter keywords */
85static struct flt_kw_list flt_keywords = {
86 .list = LIST_HEAD_INIT(flt_keywords.list)
87};
88
89/*
90 * Registers the filter keyword list <kwl> as a list of valid keywords for next
91 * parsing sessions.
92 */
93void
94flt_register_keywords(struct flt_kw_list *kwl)
95{
96 LIST_ADDQ(&flt_keywords.list, &kwl->list);
97}
98
99/*
100 * Returns a pointer to the filter keyword <kw>, or NULL if not found. If the
101 * keyword is found with a NULL ->parse() function, then an attempt is made to
102 * find one with a valid ->parse() function. This way it is possible to declare
103 * platform-dependant, known keywords as NULL, then only declare them as valid
104 * if some options are met. Note that if the requested keyword contains an
105 * opening parenthesis, everything from this point is ignored.
106 */
107struct flt_kw *
108flt_find_kw(const char *kw)
109{
110 int index;
111 const char *kwend;
112 struct flt_kw_list *kwl;
113 struct flt_kw *ret = NULL;
114
115 kwend = strchr(kw, '(');
116 if (!kwend)
117 kwend = kw + strlen(kw);
118
119 list_for_each_entry(kwl, &flt_keywords.list, list) {
120 for (index = 0; kwl->kw[index].kw != NULL; index++) {
121 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
122 kwl->kw[index].kw[kwend-kw] == 0) {
123 if (kwl->kw[index].parse)
124 return &kwl->kw[index]; /* found it !*/
125 else
126 ret = &kwl->kw[index]; /* may be OK */
127 }
128 }
129 }
130 return ret;
131}
132
133/*
134 * Dumps all registered "filter" keywords to the <out> string pointer. The
135 * unsupported keywords are only dumped if their supported form was not found.
136 */
137void
138flt_dump_kws(char **out)
139{
140 struct flt_kw_list *kwl;
141 int index;
142
Christopher Faulet784063e2020-05-18 12:14:18 +0200143 if (!out)
144 return;
145
Christopher Fauletd7c91962015-04-30 11:48:27 +0200146 *out = NULL;
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 memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "",
152 kwl->scope,
153 kwl->kw[index].kw,
154 kwl->kw[index].parse ? "" : " (not supported)");
155 }
156 }
157 }
158}
159
160/*
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100161 * Lists the known filters on <out>
162 */
163void
164list_filters(FILE *out)
165{
166 char *filters, *p, *f;
167
168 fprintf(out, "Available filters :\n");
169 flt_dump_kws(&filters);
170 for (p = filters; (f = strtok_r(p,"\n",&p));)
171 fprintf(out, "\t%s\n", f);
172 free(filters);
173}
174
175/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200176 * Parses the "filter" keyword. All keywords must be handled by filters
177 * themselves
178 */
179static int
180parse_filter(char **args, int section_type, struct proxy *curpx,
181 struct proxy *defpx, const char *file, int line, char **err)
182{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100183 struct flt_conf *fconf = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200184
185 /* Filter cannot be defined on a default proxy */
186 if (curpx == defpx) {
Christopher Fauletcc7317d2016-04-04 10:51:17 +0200187 memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.",
Christopher Fauletd7c91962015-04-30 11:48:27 +0200188 file, line, args[0]);
189 return -1;
190 }
191 if (!strcmp(args[0], "filter")) {
192 struct flt_kw *kw;
193 int cur_arg;
194
195 if (!*args[1]) {
196 memprintf(err,
197 "parsing [%s:%d] : missing argument for '%s' in %s '%s'.",
198 file, line, args[0], proxy_type_str(curpx), curpx->id);
199 goto error;
200 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100201 fconf = calloc(1, sizeof(*fconf));
202 if (!fconf) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200203 memprintf(err, "'%s' : out of memory", args[0]);
204 goto error;
205 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200206
207 cur_arg = 1;
208 kw = flt_find_kw(args[cur_arg]);
209 if (kw) {
210 if (!kw->parse) {
211 memprintf(err, "parsing [%s:%d] : '%s' : "
212 "'%s' option is not implemented in this version (check build options).",
213 file, line, args[0], args[cur_arg]);
214 goto error;
215 }
Thierry Fournier3610c392016-04-13 18:27:51 +0200216 if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200217 if (err && *err)
218 memprintf(err, "'%s' : '%s'",
219 args[0], *err);
220 else
221 memprintf(err, "'%s' : error encountered while processing '%s'",
222 args[0], args[cur_arg]);
223 goto error;
224 }
225 }
226 else {
227 flt_dump_kws(err);
228 indent_msg(err, 4);
229 memprintf(err, "'%s' : unknown keyword '%s'.%s%s",
230 args[0], args[cur_arg],
231 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
232 goto error;
233 }
234 if (*args[cur_arg]) {
235 memprintf(err, "'%s %s' : unknown keyword '%s'.",
236 args[0], args[1], args[cur_arg]);
237 goto error;
238 }
Christopher Faulet00e818a2016-04-19 17:00:44 +0200239 if (fconf->ops == NULL) {
240 memprintf(err, "'%s %s' : no callbacks defined.",
241 args[0], args[1]);
242 goto error;
243 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200244
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100245 LIST_ADDQ(&curpx->filter_configs, &fconf->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200246 }
247 return 0;
248
249 error:
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100250 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200251 return -1;
252
253
254}
255
256/*
257 * Calls 'init' callback for all filters attached to a proxy. This happens after
258 * the configuration parsing. Filters can finish to fill their config. Returns
259 * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
260 */
Willy Tarreau64bca592016-12-21 20:13:11 +0100261static int
Christopher Fauletd7c91962015-04-30 11:48:27 +0200262flt_init(struct proxy *proxy)
263{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100264 struct flt_conf *fconf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200265
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100266 list_for_each_entry(fconf, &proxy->filter_configs, list) {
267 if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200268 return ERR_ALERT|ERR_FATAL;
269 }
270 return 0;
271}
272
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200273/*
274 * Calls 'init_per_thread' callback for all filters attached to a proxy for each
275 * threads. This happens after the thread creation. Filters can finish to fill
276 * their config. Returns (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
277 */
278static int
279flt_init_per_thread(struct proxy *proxy)
280{
281 struct flt_conf *fconf;
282
283 list_for_each_entry(fconf, &proxy->filter_configs, list) {
284 if (fconf->ops->init_per_thread && fconf->ops->init_per_thread(proxy, fconf) < 0)
285 return ERR_ALERT|ERR_FATAL;
286 }
287 return 0;
288}
289
Willy Tarreau64bca592016-12-21 20:13:11 +0100290/* Calls flt_init() for all proxies, see above */
291static int
292flt_init_all()
293{
294 struct proxy *px;
295 int err_code = 0;
296
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100297 for (px = proxies_list; px; px = px->next) {
Willy Tarreau64bca592016-12-21 20:13:11 +0100298 err_code |= flt_init(px);
299 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100300 ha_alert("Failed to initialize filters for proxy '%s'.\n",
301 px->id);
Willy Tarreau64bca592016-12-21 20:13:11 +0100302 return err_code;
303 }
304 }
305 return 0;
306}
307
Joseph Herlantb35ea682018-11-15 12:24:23 -0800308/* Calls flt_init_per_thread() for all proxies, see above. Be careful here, it
309 * returns 0 if an error occurred. This is the opposite of flt_init_all. */
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200310static int
311flt_init_all_per_thread()
312{
313 struct proxy *px;
314 int err_code = 0;
315
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100316 for (px = proxies_list; px; px = px->next) {
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200317 err_code = flt_init_per_thread(px);
318 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100319 ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n",
320 px->id, tid);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200321 return 0;
322 }
323 }
324 return 1;
325}
326
Christopher Fauletd7c91962015-04-30 11:48:27 +0200327/*
328 * Calls 'check' callback for all filters attached to a proxy. This happens
329 * after the configuration parsing but before filters initialization. Returns
330 * the number of encountered errors.
331 */
332int
333flt_check(struct proxy *proxy)
334{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100335 struct flt_conf *fconf;
336 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200337
Christopher Fauletff17b182019-01-07 15:03:22 +0100338 err += check_implicit_http_comp_flt(proxy);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100339 list_for_each_entry(fconf, &proxy->filter_configs, list) {
340 if (fconf->ops->check)
341 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200342 }
343 return err;
344}
345
346/*
347 * Calls 'denit' callback for all filters attached to a proxy. This happens when
348 * HAProxy is stopped.
349 */
350void
351flt_deinit(struct proxy *proxy)
352{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100353 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200354
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100355 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
356 if (fconf->ops->deinit)
357 fconf->ops->deinit(proxy, fconf);
358 LIST_DEL(&fconf->list);
359 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200360 }
361}
362
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200363/*
364 * Calls 'denit_per_thread' callback for all filters attached to a proxy for
365 * each threads. This happens before exiting a thread.
366 */
367void
368flt_deinit_per_thread(struct proxy *proxy)
369{
370 struct flt_conf *fconf, *back;
371
372 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
373 if (fconf->ops->deinit_per_thread)
374 fconf->ops->deinit_per_thread(proxy, fconf);
375 }
376}
377
378
379/* Calls flt_deinit_per_thread() for all proxies, see above */
380static void
381flt_deinit_all_per_thread()
382{
383 struct proxy *px;
384
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100385 for (px = proxies_list; px; px = px->next)
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200386 flt_deinit_per_thread(px);
387}
388
Christopher Faulet92d36382015-11-05 13:35:03 +0100389/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
390static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100391flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100392{
Christopher Faulet75bc9132018-11-30 15:18:09 +0100393 struct filter *f;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200394
Christopher Faulet0f17a9b2019-04-05 10:11:38 +0200395 if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX))
Christopher Faulet75bc9132018-11-30 15:18:09 +0100396 return 0;
397
398 f = pool_alloc(pool_head_filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100399 if (!f) /* not enough memory */
400 return -1;
401 memset(f, 0, sizeof(*f));
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100402 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100403 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200404
405 if (FLT_OPS(f)->attach) {
406 int ret = FLT_OPS(f)->attach(s, f);
407 if (ret <= 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100408 pool_free(pool_head_filter, f);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200409 return ret;
410 }
411 }
412
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100413 LIST_ADDQ(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100414 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100415 return 0;
416}
417
418/*
419 * Called when a stream is created. It attaches all frontend filters to the
420 * stream. Returns -1 if an error occurs, 0 otherwise.
421 */
422int
423flt_stream_init(struct stream *s)
424{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100425 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100426
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100427 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
428 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100429 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
430 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100431 return -1;
432 }
433 return 0;
434}
435
436/*
437 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
438 * happens after each request/response exchange). When analyze ends, backend
439 * filters are removed. When the stream is closed, all filters attached to the
440 * stream are removed.
441 */
442void
443flt_stream_release(struct stream *s, int only_backend)
444{
445 struct filter *filter, *back;
446
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100447 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100448 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200449 if (FLT_OPS(filter)->detach)
450 FLT_OPS(filter)->detach(s, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100451 LIST_DEL(&filter->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100452 pool_free(pool_head_filter, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100453 }
454 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100455 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100456 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100457}
458
Christopher Fauletd7c91962015-04-30 11:48:27 +0200459/*
460 * Calls 'stream_start' for all filters attached to a stream. This happens when
461 * the stream is created, just after calling flt_stream_init
462 * function. Returns -1 if an error occurs, 0 otherwise.
463 */
464int
465flt_stream_start(struct stream *s)
466{
467 struct filter *filter;
468
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100469 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100470 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200471 return -1;
472 }
473 return 0;
474}
475
476/*
477 * Calls 'stream_stop' for all filters attached to a stream. This happens when
478 * the stream is stopped, just before calling flt_stream_release function.
479 */
480void
481flt_stream_stop(struct stream *s)
482{
483 struct filter *filter;
484
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100485 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100486 if (FLT_OPS(filter)->stream_stop)
487 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200488 }
489}
490
Christopher Faulet92d36382015-11-05 13:35:03 +0100491/*
Christopher Fauleta00d8172016-11-10 14:58:05 +0100492 * Calls 'check_timeouts' for all filters attached to a stream. This happens when
493 * the stream is woken up because of expired timer.
494 */
495void
496flt_stream_check_timeouts(struct stream *s)
497{
498 struct filter *filter;
499
500 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
501 if (FLT_OPS(filter)->check_timeouts)
502 FLT_OPS(filter)->check_timeouts(s, filter);
503 }
504}
505
506/*
Christopher Faulet92d36382015-11-05 13:35:03 +0100507 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200508 * are not the same, this function attaches all backend filters to the
509 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100510 */
511int
512flt_set_stream_backend(struct stream *s, struct proxy *be)
513{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100514 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200515 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100516
517 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200518 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100519
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100520 list_for_each_entry(fconf, &be->filter_configs, list) {
521 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100522 return -1;
523 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200524
525 end:
526 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
527 if (FLT_OPS(filter)->stream_set_backend &&
528 FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0)
529 return -1;
530 }
531
Christopher Faulet92d36382015-11-05 13:35:03 +0100532 return 0;
533}
534
Christopher Fauletd7c91962015-04-30 11:48:27 +0200535
536/*
537 * Calls 'http_end' callback for all filters attached to a stream. All filters
538 * are called here, but only if there is at least one "data" filter. This
539 * functions is called when all data were parsed and forwarded. 'http_end'
540 * callback is resumable, so this function returns a negative value if an error
541 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
542 */
543int
544flt_http_end(struct stream *s, struct http_msg *msg)
545{
546 int ret = 1;
547
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100548 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 +0200549 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100550 if (FLT_OPS(filter)->http_end) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100551 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100552 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200553 if (ret <= 0)
554 BREAK_EXECUTION(s, msg->chn, end);
555 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200556 } RESUME_FILTER_END;
557end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100558 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200559 return ret;
560}
561
562/*
563 * Calls 'http_reset' callback for all filters attached to a stream. This
564 * happens when a 100-continue response is received.
565 */
566void
567flt_http_reset(struct stream *s, struct http_msg *msg)
568{
569 struct filter *filter;
570
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100571 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 +0100572 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100573 if (FLT_OPS(filter)->http_reset) {
574 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100575 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100576 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200577 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100578 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200579}
580
581/*
582 * Calls 'http_reply' callback for all filters attached to a stream when HA
583 * decides to stop the HTTP message processing.
584 */
585void
Willy Tarreau83061a82018-07-13 11:56:34 +0200586flt_http_reply(struct stream *s, short status, const struct buffer *msg)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200587{
588 struct filter *filter;
589
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100590 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 +0100591 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100592 if (FLT_OPS(filter)->http_reply) {
593 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100594 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100595 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200596 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100597 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200598}
599
600/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100601 * Calls 'http_payload' callback for all "data" filters attached to a
602 * stream. This function is called when some data can be forwarded in the
603 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
604 * update the filters and the stream offset to be sure that a filter cannot
605 * forward more data than its predecessors. A filter can choose to not forward
606 * all data. Returns a negative value if an error occurs, else the number of
607 * forwarded bytes.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100608 */
609int
610flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len)
611{
612 struct filter *filter;
613 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
Christopher Faulet421e7692019-06-13 11:16:45 +0200614 unsigned int out = co_data(msg->chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100615 int ret, data;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100616
Christopher Faulet81340d72020-02-26 15:47:22 +0100617 ret = data = len - out;
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 Faulet75bc9132018-11-30 15:18:09 +0100619 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
620 /* Call "data" filters only */
621 if (!IS_DATA_FILTER(filter, msg->chn))
622 continue;
623 if (FLT_OPS(filter)->http_payload) {
624 unsigned long long *flt_off = &FLT_OFF(filter, msg->chn);
625 unsigned int offset = *flt_off - *strm_off;
626
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100627 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100628 ret = FLT_OPS(filter)->http_payload(s, filter, msg, out + offset, data - offset);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100629 if (ret < 0)
630 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100631 data = ret + *flt_off - *strm_off;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100632 *flt_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100633 }
634 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100635
636 /* Only forward data if the last filter decides to forward something */
637 if (ret > 0) {
638 ret = data;
639 *strm_off += ret;
640 }
Christopher Faulet75bc9132018-11-30 15:18:09 +0100641 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100642 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100643 return ret;
644}
645
646/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200647 * Calls 'channel_start_analyze' callback for all filters attached to a
648 * stream. This function is called when we start to analyze a request or a
649 * response. For frontend filters, it is called before all other analyzers. For
650 * backend ones, it is called before all backend
651 * analyzers. 'channel_start_analyze' callback is resumable, so this function
652 * returns 0 if an error occurs or if it needs to wait, any other value
653 * otherwise.
654 */
655int
656flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
657{
658 int ret = 1;
659
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100660 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
661
Christopher Fauletd7c91962015-04-30 11:48:27 +0200662 /* If this function is called, this means there is at least one filter,
663 * so we do not need to check the filter list's emptiness. */
664
Christopher Faulete6006242017-03-10 11:52:44 +0100665 /* Set flag on channel to tell that the channel is filtered */
666 chn->flags |= CF_FLT_ANALYZE;
667
Christopher Fauletd7c91962015-04-30 11:48:27 +0200668 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100669 if (!(chn->flags & CF_ISRESP)) {
670 if (an_bit == AN_REQ_FLT_START_BE &&
671 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
672 continue;
673 }
674 else {
675 if (an_bit == AN_RES_FLT_START_BE &&
676 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
677 continue;
678 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200679
Christopher Fauletb2e58492019-11-12 11:13:01 +0100680 FLT_OFF(filter, chn) = 0;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100681 if (FLT_OPS(filter)->channel_start_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100682 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100683 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200684 if (ret <= 0)
685 BREAK_EXECUTION(s, chn, end);
686 }
687 } RESUME_FILTER_END;
688
689 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100690 ret = handle_analyzer_result(s, chn, an_bit, ret);
691 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
692 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200693}
694
695/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200696 * Calls 'channel_pre_analyze' callback for all filters attached to a
697 * stream. This function is called BEFORE each analyzer attached to a channel,
698 * expects analyzers responsible for data sending. 'channel_pre_analyze'
699 * callback is resumable, so this function returns 0 if an error occurs or if it
700 * needs to wait, any other value otherwise.
701 *
702 * Note this function can be called many times for the same analyzer. In fact,
703 * it is called until the analyzer finishes its processing.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200704 */
705int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200706flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200707{
708 int ret = 1;
709
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100710 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
711
Christopher Fauletd7c91962015-04-30 11:48:27 +0200712 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200713 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100714 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200715 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200716 if (ret <= 0)
717 BREAK_EXECUTION(s, chn, check_result);
718 }
719 } RESUME_FILTER_END;
720
721 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100722 ret = handle_analyzer_result(s, chn, 0, ret);
723 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
724 return ret;
Christopher Faulet309c6412015-12-02 09:57:32 +0100725}
726
727/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200728 * Calls 'channel_post_analyze' callback for all filters attached to a
729 * stream. This function is called AFTER each analyzer attached to a channel,
730 * expects analyzers responsible for data sending. 'channel_post_analyze'
731 * callback is NOT resumable, so this function returns a 0 if an error occurs,
732 * any other value otherwise.
733 *
734 * Here, AFTER means when the analyzer finishes its processing.
735 */
736int
737flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
738{
739 struct filter *filter;
740 int ret = 1;
741
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100742 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
743
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200744 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
745 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100746 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200747 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
748 if (ret < 0)
749 break;
750 }
751 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100752 ret = handle_analyzer_result(s, chn, 0, ret);
753 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
754 return ret;
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200755}
756
757/*
Christopher Faulet0184ea72017-01-05 14:06:34 +0100758 * This function is the AN_REQ/RES_FLT_HTTP_HDRS analyzer, used to filter HTTP
759 * headers or a request or a response. Returns 0 if an error occurs or if it
760 * needs to wait, any other value otherwise.
Christopher Faulet309c6412015-12-02 09:57:32 +0100761 */
762int
763flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
764{
Christopher Faulet1339d742016-05-11 16:48:33 +0200765 struct http_msg *msg;
766 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100767
Christopher Faulet1339d742016-05-11 16:48:33 +0200768 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100769 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
770
Christopher Faulet309c6412015-12-02 09:57:32 +0100771 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200772 if (FLT_OPS(filter)->http_headers) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100773 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet1339d742016-05-11 16:48:33 +0200774 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100775 if (ret <= 0)
776 BREAK_EXECUTION(s, chn, check_result);
777 }
778 } RESUME_FILTER_END;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100779
780 if (HAS_DATA_FILTERS(s, chn)) {
781 size_t data = http_get_hdrs_size(htxbuf(&chn->buf));
782 struct filter *f;
783
784 list_for_each_entry(f, &strm_flt(s)->filters, list) {
785 if (IS_DATA_FILTER(f, chn))
786 FLT_OFF(f, chn) = data;
787 }
788 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100789
790 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100791 ret = handle_analyzer_result(s, chn, an_bit, ret);
792 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
793 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200794}
795
796/*
797 * Calls 'channel_end_analyze' callback for all filters attached to a
798 * stream. This function is called when we stop to analyze a request or a
799 * response. It is called after all other analyzers. 'channel_end_analyze'
800 * callback is resumable, so this function returns 0 if an error occurs or if it
801 * needs to wait, any other value otherwise.
802 */
803int
804flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
805{
806 int ret = 1;
807
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100808 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
809
Christopher Faulete6006242017-03-10 11:52:44 +0100810 /* Check if all filters attached on the stream have finished their
811 * processing on this channel. */
812 if (!(chn->flags & CF_FLT_ANALYZE))
813 goto sync;
814
Christopher Fauletd7c91962015-04-30 11:48:27 +0200815 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100816 FLT_OFF(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100817 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200818
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100819 if (FLT_OPS(filter)->channel_end_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100820 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100821 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200822 if (ret <= 0)
823 BREAK_EXECUTION(s, chn, end);
824 }
825 } RESUME_FILTER_END;
826
Christopher Faulete6006242017-03-10 11:52:44 +0100827 end:
828 /* We don't remove yet this analyzer because we need to synchronize the
829 * both channels. So here, we just remove the flag CF_FLT_ANALYZE. */
830 ret = handle_analyzer_result(s, chn, 0, ret);
Christopher Faulet570f7992017-07-06 15:53:02 +0200831 if (ret) {
Christopher Faulete6006242017-03-10 11:52:44 +0100832 chn->flags &= ~CF_FLT_ANALYZE;
Christopher Faulet02c7b222015-12-22 12:01:29 +0100833
Christopher Faulet570f7992017-07-06 15:53:02 +0200834 /* Pretend there is an activity on both channels. Flag on the
835 * current one will be automatically removed, so only the other
836 * one will remain. This is a way to be sure that
837 * 'channel_end_analyze' callback will have a chance to be
838 * called at least once for the other side to finish the current
Joseph Herlantb35ea682018-11-15 12:24:23 -0800839 * processing. Of course, this is the filter responsibility to
Christopher Faulet570f7992017-07-06 15:53:02 +0200840 * wakeup the stream if it choose to loop on this callback. */
841 s->req.flags |= CF_WAKE_ONCE;
842 s->res.flags |= CF_WAKE_ONCE;
843 }
844
845
Christopher Faulete6006242017-03-10 11:52:44 +0100846 sync:
847 /* Now we can check if filters have finished their work on the both
848 * channels */
849 if (!(s->req.flags & CF_FLT_ANALYZE) && !(s->res.flags & CF_FLT_ANALYZE)) {
850 /* Sync channels by removing this analyzer for the both channels */
851 s->req.analysers &= ~AN_REQ_FLT_END;
852 s->res.analysers &= ~AN_RES_FLT_END;
Christopher Fauletc6062be2016-10-31 11:22:37 +0100853
Christopher Faulete6006242017-03-10 11:52:44 +0100854 /* Remove backend filters from the list */
855 flt_stream_release(s, 1);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100856 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200857 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100858 else {
859 DBG_TRACE_DEVEL("waiting for sync", STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
860 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200861 return ret;
862}
863
Christopher Fauletd7c91962015-04-30 11:48:27 +0200864
865/*
Christopher Fauletb2e58492019-11-12 11:13:01 +0100866 * Calls 'tcp_payload' callback for all "data" filters attached to a
867 * stream. This function is called when some data can be forwarded in the
868 * AN_REQ_FLT_XFER_BODY and AN_RES_FLT_XFER_BODY analyzers. It takes care to
869 * update the filters and the stream offset to be sure that a filter cannot
870 * forward more data than its predecessors. A filter can choose to not forward
871 * all data. Returns a negative value if an error occurs, else the number of
872 * forwarded bytes.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200873 */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100874int
875flt_tcp_payload(struct stream *s, struct channel *chn, unsigned int len)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200876{
Christopher Fauletda02e172015-12-04 09:25:05 +0100877 struct filter *filter;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100878 unsigned long long *strm_off = &FLT_STRM_OFF(s, chn);
879 unsigned int out = co_data(chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100880 int ret, data;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200881
Christopher Faulet81340d72020-02-26 15:47:22 +0100882 ret = data = len - out;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100883 DBG_TRACE_ENTER(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100884 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100885 /* Call "data" filters only */
886 if (!IS_DATA_FILTER(filter, chn))
887 continue;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100888 if (FLT_OPS(filter)->tcp_payload) {
889 unsigned long long *flt_off = &FLT_OFF(filter, chn);
890 unsigned int offset = *flt_off - *strm_off;
Christopher Fauletda02e172015-12-04 09:25:05 +0100891
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100892 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100893 ret = FLT_OPS(filter)->tcp_payload(s, filter, chn, out + offset, data - offset);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200894 if (ret < 0)
895 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100896 data = ret + *flt_off - *strm_off;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100897 *flt_off += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200898 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200899 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100900
901 /* Only forward data if the last filter decides to forward something */
902 if (ret > 0) {
903 ret = data;
904 *strm_off += ret;
905 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200906 end:
Christopher Fauletb2e58492019-11-12 11:13:01 +0100907 DBG_TRACE_LEAVE(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200908 return ret;
909}
910
911/*
912 * Called when TCP data must be filtered on a channel. This function is the
Christopher Faulet0184ea72017-01-05 14:06:34 +0100913 * AN_REQ/RES_FLT_XFER_DATA analyzer. When called, it is responsible to forward
914 * data when the proxy is not in http mode. Behind the scene, it calls
915 * consecutively 'tcp_data' and 'tcp_forward_data' callbacks for all "data"
916 * filters attached to a stream. Returns 0 if an error occurs or if it needs to
917 * wait, any other value otherwise.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200918 */
919int
920flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
921{
Christopher Fauletb2e58492019-11-12 11:13:01 +0100922 unsigned int len;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200923 int ret = 1;
924
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100925 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
926
Christopher Fauletda02e172015-12-04 09:25:05 +0100927 /* If there is no "data" filters, we do nothing */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100928 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletda02e172015-12-04 09:25:05 +0100929 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200930
931 /* Be sure that the output is still opened. Else we stop the data
932 * filtering. */
933 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau44a41a82018-06-19 07:16:31 +0200934 ((chn->flags & CF_SHUTW) && (chn->to_forward || co_data(chn))))
Christopher Fauletd7c91962015-04-30 11:48:27 +0200935 goto end;
936
Christopher Fauletb2e58492019-11-12 11:13:01 +0100937 if (s->flags & SF_HTX) {
938 struct htx *htx = htxbuf(&chn->buf);
939 len = htx->data;
940 }
941 else
942 len = c_data(chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200943
Christopher Fauletb2e58492019-11-12 11:13:01 +0100944 ret = flt_tcp_payload(s, chn, len);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200945 if (ret < 0)
946 goto end;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200947 c_adv(chn, ret);
Christopher Fauletda02e172015-12-04 09:25:05 +0100948
Christopher Fauletd7c91962015-04-30 11:48:27 +0200949 /* Stop waiting data if the input in closed and no data is pending or if
950 * the output is closed. */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100951 if (chn->flags & CF_SHUTW) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200952 ret = 1;
953 goto end;
954 }
Christopher Fauletb2e58492019-11-12 11:13:01 +0100955 if (chn->flags & CF_SHUTR) {
956 if (((s->flags & SF_HTX) && htx_is_empty(htxbuf(&chn->buf))) || c_empty(chn)) {
957 ret = 1;
958 goto end;
959 }
960 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200961
962 /* Wait for data */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100963 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 +0200964 return 0;
965 end:
966 /* Terminate the data filtering. If <ret> is negative, an error was
967 * encountered during the filtering. */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100968 ret = handle_analyzer_result(s, chn, an_bit, ret);
969 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
970 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200971}
972
973/*
974 * Handles result of filter's analyzers. It returns 0 if an error occurs or if
975 * it needs to wait, any other value otherwise.
976 */
977static int
978handle_analyzer_result(struct stream *s, struct channel *chn,
979 unsigned int an_bit, int ret)
980{
981 int finst;
Christopher Faulete058f732019-09-06 15:24:55 +0200982 int status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200983
984 if (ret < 0)
985 goto return_bad_req;
986 else if (!ret)
987 goto wait;
988
989 /* End of job, return OK */
990 if (an_bit) {
991 chn->analysers &= ~an_bit;
992 chn->analyse_exp = TICK_ETERNITY;
993 }
994 return 1;
995
996 return_bad_req:
997 /* An error occurs */
998 channel_abort(&s->req);
999 channel_abort(&s->res);
1000
1001 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001002 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001003 finst = SF_FINST_R;
Christopher Faulete058f732019-09-06 15:24:55 +02001004 status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001005 /* FIXME: incr counters */
1006 }
1007 else {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001008 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001009 finst = SF_FINST_H;
Christopher Faulete058f732019-09-06 15:24:55 +02001010 status = 502;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001011 /* FIXME: incr counters */
1012 }
1013
Christopher Faulet3d119692019-07-15 22:04:51 +02001014 if (IS_HTX_STRM(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001015 /* Do not do that when we are waiting for the next request */
Christopher Faulete058f732019-09-06 15:24:55 +02001016 if (s->txn->status > 0)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001017 http_reply_and_close(s, s->txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001018 else {
Christopher Faulete058f732019-09-06 15:24:55 +02001019 s->txn->status = status;
1020 http_reply_and_close(s, status, http_error_message(s));
Christopher Fauletd7c91962015-04-30 11:48:27 +02001021 }
1022 }
1023
1024 if (!(s->flags & SF_ERR_MASK))
1025 s->flags |= SF_ERR_PRXCOND;
1026 if (!(s->flags & SF_FINST_MASK))
1027 s->flags |= finst;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001028 DBG_TRACE_DEVEL("leaving on error", STRM_EV_FLT_ANA|STRM_EV_FLT_ERR, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001029 return 0;
1030
1031 wait:
1032 if (!(chn->flags & CF_ISRESP))
1033 channel_dont_connect(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001034 DBG_TRACE_DEVEL("wairing for more data", STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001035 return 0;
1036}
1037
1038
1039/* Note: must not be declared <const> as its list will be overwritten.
1040 * Please take care of keeping this list alphabetically sorted, doing so helps
1041 * all code contributors.
1042 * Optional keywords are also declared with a NULL ->parse() function so that
1043 * the config parser can report an appropriate error when a known keyword was
1044 * not enabled. */
1045static struct cfg_kw_list cfg_kws = {ILH, {
1046 { CFG_LISTEN, "filter", parse_filter },
1047 { 0, NULL, NULL },
1048 }
1049};
1050
Willy Tarreau0108d902018-11-25 19:14:37 +01001051INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1052
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001053REGISTER_POST_CHECK(flt_init_all);
1054REGISTER_PER_THREAD_INIT(flt_init_all_per_thread);
1055REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread);
1056
Christopher Fauletd7c91962015-04-30 11:48:27 +02001057/*
1058 * Local variables:
1059 * c-indent-level: 8
1060 * c-basic-offset: 8
1061 * End:
1062 */