blob: e2750504080e1ab3b7e688bee1fb6437298dad93 [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
13#include <common/buffer.h>
14#include <common/debug.h>
15#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/errors.h>
Willy Tarreaub96b77e2018-12-11 10:22:41 +010019#include <common/htx.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010020#include <common/initcall.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020021#include <common/namespace.h>
22#include <common/standard.h>
Christopher Faulet71a6a8e2017-07-27 16:33:28 +020023#include <common/hathreads.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020024
25#include <types/filters.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020026#include <types/http_ana.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020027
28#include <proto/compression.h>
29#include <proto/filters.h>
Christopher Faulet92d36382015-11-05 13:35:03 +010030#include <proto/flt_http_comp.h>
Christopher Faulet75bc9132018-11-30 15:18:09 +010031#include <proto/http_htx.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020032#include <proto/http_ana.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020033#include <proto/stream.h>
34#include <proto/stream_interface.h>
35
Christopher Fauleteea8fc72019-11-05 16:18:10 +010036#define TRACE_SOURCE &trace_strm
37
Christopher Fauletd7c91962015-04-30 11:48:27 +020038/* Pool used to allocate filters */
Willy Tarreau8ceae722018-11-26 11:58:30 +010039DECLARE_STATIC_POOL(pool_head_filter, "filter", sizeof(struct filter));
Christopher Fauletd7c91962015-04-30 11:48:27 +020040
41static int handle_analyzer_result(struct stream *s, struct channel *chn, unsigned int an_bit, int ret);
42
43/* - RESUME_FILTER_LOOP and RESUME_FILTER_END must always be used together.
44 * The first one begins a loop and the seconds one ends it.
45 *
46 * - BREAK_EXECUTION must be used to break the loop and set the filter from
47 * which to resume the next time.
48 *
Bertrand Jacquin874a35c2018-09-10 21:26:07 +010049 * Here is an example:
Christopher Fauletd7c91962015-04-30 11:48:27 +020050 *
51 * RESUME_FILTER_LOOP(stream, channel) {
52 * ...
53 * if (cond)
54 * BREAK_EXECUTION(stream, channel, label);
55 * ...
56 * } RESUME_FILTER_END;
57 * ...
58 * label:
59 * ...
60 *
61 */
62#define RESUME_FILTER_LOOP(strm, chn) \
63 do { \
64 struct filter *filter; \
65 \
Christopher Fauletda02e172015-12-04 09:25:05 +010066 if (strm_flt(strm)->current[CHN_IDX(chn)]) { \
67 filter = strm_flt(strm)->current[CHN_IDX(chn)]; \
68 strm_flt(strm)->current[CHN_IDX(chn)] = NULL; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020069 goto resume_execution; \
70 } \
71 \
Christopher Fauletfcf035c2015-12-03 11:48:03 +010072 list_for_each_entry(filter, &strm_flt(s)->filters, list) { \
Christopher Fauletda02e172015-12-04 09:25:05 +010073 resume_execution:
Christopher Fauletd7c91962015-04-30 11:48:27 +020074
75#define RESUME_FILTER_END \
76 } \
77 } while(0)
78
Christopher Fauletda02e172015-12-04 09:25:05 +010079#define BREAK_EXECUTION(strm, chn, label) \
80 do { \
81 strm_flt(strm)->current[CHN_IDX(chn)] = filter; \
82 goto label; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020083 } while (0)
84
85
86/* List head of all known filter keywords */
87static struct flt_kw_list flt_keywords = {
88 .list = LIST_HEAD_INIT(flt_keywords.list)
89};
90
91/*
92 * Registers the filter keyword list <kwl> as a list of valid keywords for next
93 * parsing sessions.
94 */
95void
96flt_register_keywords(struct flt_kw_list *kwl)
97{
98 LIST_ADDQ(&flt_keywords.list, &kwl->list);
99}
100
101/*
102 * Returns a pointer to the filter keyword <kw>, or NULL if not found. If the
103 * keyword is found with a NULL ->parse() function, then an attempt is made to
104 * find one with a valid ->parse() function. This way it is possible to declare
105 * platform-dependant, known keywords as NULL, then only declare them as valid
106 * if some options are met. Note that if the requested keyword contains an
107 * opening parenthesis, everything from this point is ignored.
108 */
109struct flt_kw *
110flt_find_kw(const char *kw)
111{
112 int index;
113 const char *kwend;
114 struct flt_kw_list *kwl;
115 struct flt_kw *ret = NULL;
116
117 kwend = strchr(kw, '(');
118 if (!kwend)
119 kwend = kw + strlen(kw);
120
121 list_for_each_entry(kwl, &flt_keywords.list, list) {
122 for (index = 0; kwl->kw[index].kw != NULL; index++) {
123 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
124 kwl->kw[index].kw[kwend-kw] == 0) {
125 if (kwl->kw[index].parse)
126 return &kwl->kw[index]; /* found it !*/
127 else
128 ret = &kwl->kw[index]; /* may be OK */
129 }
130 }
131 }
132 return ret;
133}
134
135/*
136 * Dumps all registered "filter" keywords to the <out> string pointer. The
137 * unsupported keywords are only dumped if their supported form was not found.
138 */
139void
140flt_dump_kws(char **out)
141{
142 struct flt_kw_list *kwl;
143 int index;
144
145 *out = NULL;
146 list_for_each_entry(kwl, &flt_keywords.list, list) {
147 for (index = 0; kwl->kw[index].kw != NULL; index++) {
148 if (kwl->kw[index].parse ||
149 flt_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
150 memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "",
151 kwl->scope,
152 kwl->kw[index].kw,
153 kwl->kw[index].parse ? "" : " (not supported)");
154 }
155 }
156 }
157}
158
159/*
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100160 * Lists the known filters on <out>
161 */
162void
163list_filters(FILE *out)
164{
165 char *filters, *p, *f;
166
167 fprintf(out, "Available filters :\n");
168 flt_dump_kws(&filters);
169 for (p = filters; (f = strtok_r(p,"\n",&p));)
170 fprintf(out, "\t%s\n", f);
171 free(filters);
172}
173
174/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200175 * Parses the "filter" keyword. All keywords must be handled by filters
176 * themselves
177 */
178static int
179parse_filter(char **args, int section_type, struct proxy *curpx,
180 struct proxy *defpx, const char *file, int line, char **err)
181{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100182 struct flt_conf *fconf = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200183
184 /* Filter cannot be defined on a default proxy */
185 if (curpx == defpx) {
Christopher Fauletcc7317d2016-04-04 10:51:17 +0200186 memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.",
Christopher Fauletd7c91962015-04-30 11:48:27 +0200187 file, line, args[0]);
188 return -1;
189 }
190 if (!strcmp(args[0], "filter")) {
191 struct flt_kw *kw;
192 int cur_arg;
193
194 if (!*args[1]) {
195 memprintf(err,
196 "parsing [%s:%d] : missing argument for '%s' in %s '%s'.",
197 file, line, args[0], proxy_type_str(curpx), curpx->id);
198 goto error;
199 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100200 fconf = calloc(1, sizeof(*fconf));
201 if (!fconf) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200202 memprintf(err, "'%s' : out of memory", args[0]);
203 goto error;
204 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200205
206 cur_arg = 1;
207 kw = flt_find_kw(args[cur_arg]);
208 if (kw) {
209 if (!kw->parse) {
210 memprintf(err, "parsing [%s:%d] : '%s' : "
211 "'%s' option is not implemented in this version (check build options).",
212 file, line, args[0], args[cur_arg]);
213 goto error;
214 }
Thierry Fournier3610c392016-04-13 18:27:51 +0200215 if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200216 if (err && *err)
217 memprintf(err, "'%s' : '%s'",
218 args[0], *err);
219 else
220 memprintf(err, "'%s' : error encountered while processing '%s'",
221 args[0], args[cur_arg]);
222 goto error;
223 }
224 }
225 else {
226 flt_dump_kws(err);
227 indent_msg(err, 4);
228 memprintf(err, "'%s' : unknown keyword '%s'.%s%s",
229 args[0], args[cur_arg],
230 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
231 goto error;
232 }
233 if (*args[cur_arg]) {
234 memprintf(err, "'%s %s' : unknown keyword '%s'.",
235 args[0], args[1], args[cur_arg]);
236 goto error;
237 }
Christopher Faulet00e818a2016-04-19 17:00:44 +0200238 if (fconf->ops == NULL) {
239 memprintf(err, "'%s %s' : no callbacks defined.",
240 args[0], args[1]);
241 goto error;
242 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200243
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100244 LIST_ADDQ(&curpx->filter_configs, &fconf->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200245 }
246 return 0;
247
248 error:
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100249 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200250 return -1;
251
252
253}
254
255/*
256 * Calls 'init' callback for all filters attached to a proxy. This happens after
257 * the configuration parsing. Filters can finish to fill their config. Returns
258 * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
259 */
Willy Tarreau64bca592016-12-21 20:13:11 +0100260static int
Christopher Fauletd7c91962015-04-30 11:48:27 +0200261flt_init(struct proxy *proxy)
262{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100263 struct flt_conf *fconf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200264
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100265 list_for_each_entry(fconf, &proxy->filter_configs, list) {
266 if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200267 return ERR_ALERT|ERR_FATAL;
268 }
269 return 0;
270}
271
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200272/*
273 * Calls 'init_per_thread' callback for all filters attached to a proxy for each
274 * threads. This happens after the thread creation. Filters can finish to fill
275 * their config. Returns (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
276 */
277static int
278flt_init_per_thread(struct proxy *proxy)
279{
280 struct flt_conf *fconf;
281
282 list_for_each_entry(fconf, &proxy->filter_configs, list) {
283 if (fconf->ops->init_per_thread && fconf->ops->init_per_thread(proxy, fconf) < 0)
284 return ERR_ALERT|ERR_FATAL;
285 }
286 return 0;
287}
288
Willy Tarreau64bca592016-12-21 20:13:11 +0100289/* Calls flt_init() for all proxies, see above */
290static int
291flt_init_all()
292{
293 struct proxy *px;
294 int err_code = 0;
295
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100296 for (px = proxies_list; px; px = px->next) {
Willy Tarreau64bca592016-12-21 20:13:11 +0100297 err_code |= flt_init(px);
298 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100299 ha_alert("Failed to initialize filters for proxy '%s'.\n",
300 px->id);
Willy Tarreau64bca592016-12-21 20:13:11 +0100301 return err_code;
302 }
303 }
304 return 0;
305}
306
Joseph Herlantb35ea682018-11-15 12:24:23 -0800307/* Calls flt_init_per_thread() for all proxies, see above. Be careful here, it
308 * returns 0 if an error occurred. This is the opposite of flt_init_all. */
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200309static int
310flt_init_all_per_thread()
311{
312 struct proxy *px;
313 int err_code = 0;
314
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100315 for (px = proxies_list; px; px = px->next) {
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200316 err_code = flt_init_per_thread(px);
317 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100318 ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n",
319 px->id, tid);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200320 return 0;
321 }
322 }
323 return 1;
324}
325
Christopher Fauletd7c91962015-04-30 11:48:27 +0200326/*
327 * Calls 'check' callback for all filters attached to a proxy. This happens
328 * after the configuration parsing but before filters initialization. Returns
329 * the number of encountered errors.
330 */
331int
332flt_check(struct proxy *proxy)
333{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100334 struct flt_conf *fconf;
335 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200336
Christopher Fauletff17b182019-01-07 15:03:22 +0100337 err += check_implicit_http_comp_flt(proxy);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100338 list_for_each_entry(fconf, &proxy->filter_configs, list) {
339 if (fconf->ops->check)
340 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200341 }
342 return err;
343}
344
345/*
346 * Calls 'denit' callback for all filters attached to a proxy. This happens when
347 * HAProxy is stopped.
348 */
349void
350flt_deinit(struct proxy *proxy)
351{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100352 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200353
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100354 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
355 if (fconf->ops->deinit)
356 fconf->ops->deinit(proxy, fconf);
357 LIST_DEL(&fconf->list);
358 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200359 }
360}
361
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200362/*
363 * Calls 'denit_per_thread' callback for all filters attached to a proxy for
364 * each threads. This happens before exiting a thread.
365 */
366void
367flt_deinit_per_thread(struct proxy *proxy)
368{
369 struct flt_conf *fconf, *back;
370
371 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
372 if (fconf->ops->deinit_per_thread)
373 fconf->ops->deinit_per_thread(proxy, fconf);
374 }
375}
376
377
378/* Calls flt_deinit_per_thread() for all proxies, see above */
379static void
380flt_deinit_all_per_thread()
381{
382 struct proxy *px;
383
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100384 for (px = proxies_list; px; px = px->next)
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200385 flt_deinit_per_thread(px);
386}
387
Christopher Faulet92d36382015-11-05 13:35:03 +0100388/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
389static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100390flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100391{
Christopher Faulet75bc9132018-11-30 15:18:09 +0100392 struct filter *f;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200393
Christopher Faulet0f17a9b2019-04-05 10:11:38 +0200394 if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX))
Christopher Faulet75bc9132018-11-30 15:18:09 +0100395 return 0;
396
397 f = pool_alloc(pool_head_filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100398 if (!f) /* not enough memory */
399 return -1;
400 memset(f, 0, sizeof(*f));
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100401 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100402 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200403
404 if (FLT_OPS(f)->attach) {
405 int ret = FLT_OPS(f)->attach(s, f);
406 if (ret <= 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100407 pool_free(pool_head_filter, f);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200408 return ret;
409 }
410 }
411
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100412 LIST_ADDQ(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100413 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100414 return 0;
415}
416
417/*
418 * Called when a stream is created. It attaches all frontend filters to the
419 * stream. Returns -1 if an error occurs, 0 otherwise.
420 */
421int
422flt_stream_init(struct stream *s)
423{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100424 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100425
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100426 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
427 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100428 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
429 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100430 return -1;
431 }
432 return 0;
433}
434
435/*
436 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
437 * happens after each request/response exchange). When analyze ends, backend
438 * filters are removed. When the stream is closed, all filters attached to the
439 * stream are removed.
440 */
441void
442flt_stream_release(struct stream *s, int only_backend)
443{
444 struct filter *filter, *back;
445
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100446 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100447 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200448 if (FLT_OPS(filter)->detach)
449 FLT_OPS(filter)->detach(s, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100450 LIST_DEL(&filter->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100451 pool_free(pool_head_filter, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100452 }
453 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100454 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100455 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100456}
457
Christopher Fauletd7c91962015-04-30 11:48:27 +0200458/*
459 * Calls 'stream_start' for all filters attached to a stream. This happens when
460 * the stream is created, just after calling flt_stream_init
461 * function. Returns -1 if an error occurs, 0 otherwise.
462 */
463int
464flt_stream_start(struct stream *s)
465{
466 struct filter *filter;
467
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100468 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100469 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200470 return -1;
471 }
472 return 0;
473}
474
475/*
476 * Calls 'stream_stop' for all filters attached to a stream. This happens when
477 * the stream is stopped, just before calling flt_stream_release function.
478 */
479void
480flt_stream_stop(struct stream *s)
481{
482 struct filter *filter;
483
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100484 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100485 if (FLT_OPS(filter)->stream_stop)
486 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200487 }
488}
489
Christopher Faulet92d36382015-11-05 13:35:03 +0100490/*
Christopher Fauleta00d8172016-11-10 14:58:05 +0100491 * Calls 'check_timeouts' for all filters attached to a stream. This happens when
492 * the stream is woken up because of expired timer.
493 */
494void
495flt_stream_check_timeouts(struct stream *s)
496{
497 struct filter *filter;
498
499 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
500 if (FLT_OPS(filter)->check_timeouts)
501 FLT_OPS(filter)->check_timeouts(s, filter);
502 }
503}
504
505/*
Christopher Faulet92d36382015-11-05 13:35:03 +0100506 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200507 * are not the same, this function attaches all backend filters to the
508 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100509 */
510int
511flt_set_stream_backend(struct stream *s, struct proxy *be)
512{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100513 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200514 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100515
516 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200517 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100518
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100519 list_for_each_entry(fconf, &be->filter_configs, list) {
520 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100521 return -1;
522 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200523
524 end:
525 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
526 if (FLT_OPS(filter)->stream_set_backend &&
527 FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0)
528 return -1;
529 }
530
Christopher Faulet92d36382015-11-05 13:35:03 +0100531 return 0;
532}
533
Christopher Fauletd7c91962015-04-30 11:48:27 +0200534
535/*
536 * Calls 'http_end' callback for all filters attached to a stream. All filters
537 * are called here, but only if there is at least one "data" filter. This
538 * functions is called when all data were parsed and forwarded. 'http_end'
539 * callback is resumable, so this function returns a negative value if an error
540 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
541 */
542int
543flt_http_end(struct stream *s, struct http_msg *msg)
544{
545 int ret = 1;
546
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100547 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 +0200548 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100549 if (FLT_OPS(filter)->http_end) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100550 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100551 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200552 if (ret <= 0)
553 BREAK_EXECUTION(s, msg->chn, end);
554 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200555 } RESUME_FILTER_END;
556end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100557 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200558 return ret;
559}
560
561/*
562 * Calls 'http_reset' callback for all filters attached to a stream. This
563 * happens when a 100-continue response is received.
564 */
565void
566flt_http_reset(struct stream *s, struct http_msg *msg)
567{
568 struct filter *filter;
569
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100570 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 +0100571 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100572 if (FLT_OPS(filter)->http_reset) {
573 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100574 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100575 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200576 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100577 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200578}
579
580/*
581 * Calls 'http_reply' callback for all filters attached to a stream when HA
582 * decides to stop the HTTP message processing.
583 */
584void
Willy Tarreau83061a82018-07-13 11:56:34 +0200585flt_http_reply(struct stream *s, short status, const struct buffer *msg)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200586{
587 struct filter *filter;
588
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100589 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 +0100590 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100591 if (FLT_OPS(filter)->http_reply) {
592 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100593 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100594 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200595 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100596 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200597}
598
599/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100600 * Calls 'http_payload' callback for all "data" filters attached to a
601 * stream. This function is called when some data can be forwarded in the
602 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
603 * update the filters and the stream offset to be sure that a filter cannot
604 * forward more data than its predecessors. A filter can choose to not forward
605 * all data. Returns a negative value if an error occurs, else the number of
606 * forwarded bytes.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100607 */
608int
609flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len)
610{
611 struct filter *filter;
612 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
Christopher Faulet421e7692019-06-13 11:16:45 +0200613 unsigned int out = co_data(msg->chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100614 int ret, data;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100615
Christopher Faulet81340d72020-02-26 15:47:22 +0100616 ret = data = len - out;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100617 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100618 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
619 /* Call "data" filters only */
620 if (!IS_DATA_FILTER(filter, msg->chn))
621 continue;
622 if (FLT_OPS(filter)->http_payload) {
623 unsigned long long *flt_off = &FLT_OFF(filter, msg->chn);
624 unsigned int offset = *flt_off - *strm_off;
625
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100626 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100627 ret = FLT_OPS(filter)->http_payload(s, filter, msg, out + offset, data - offset);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100628 if (ret < 0)
629 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100630 data = ret + *flt_off - *strm_off;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100631 *flt_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100632 }
633 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100634
635 /* Only forward data if the last filter decides to forward something */
636 if (ret > 0) {
637 ret = data;
638 *strm_off += ret;
639 }
Christopher Faulet75bc9132018-11-30 15:18:09 +0100640 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100641 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100642 return ret;
643}
644
645/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200646 * Calls 'channel_start_analyze' callback for all filters attached to a
647 * stream. This function is called when we start to analyze a request or a
648 * response. For frontend filters, it is called before all other analyzers. For
649 * backend ones, it is called before all backend
650 * analyzers. 'channel_start_analyze' callback is resumable, so this function
651 * returns 0 if an error occurs or if it needs to wait, any other value
652 * otherwise.
653 */
654int
655flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
656{
657 int ret = 1;
658
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100659 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
660
Christopher Fauletd7c91962015-04-30 11:48:27 +0200661 /* If this function is called, this means there is at least one filter,
662 * so we do not need to check the filter list's emptiness. */
663
Christopher Faulete6006242017-03-10 11:52:44 +0100664 /* Set flag on channel to tell that the channel is filtered */
665 chn->flags |= CF_FLT_ANALYZE;
666
Christopher Fauletd7c91962015-04-30 11:48:27 +0200667 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100668 if (!(chn->flags & CF_ISRESP)) {
669 if (an_bit == AN_REQ_FLT_START_BE &&
670 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
671 continue;
672 }
673 else {
674 if (an_bit == AN_RES_FLT_START_BE &&
675 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
676 continue;
677 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200678
Christopher Fauletb2e58492019-11-12 11:13:01 +0100679 FLT_OFF(filter, chn) = 0;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100680 if (FLT_OPS(filter)->channel_start_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100681 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100682 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200683 if (ret <= 0)
684 BREAK_EXECUTION(s, chn, end);
685 }
686 } RESUME_FILTER_END;
687
688 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100689 ret = handle_analyzer_result(s, chn, an_bit, ret);
690 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
691 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200692}
693
694/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200695 * Calls 'channel_pre_analyze' callback for all filters attached to a
696 * stream. This function is called BEFORE each analyzer attached to a channel,
697 * expects analyzers responsible for data sending. 'channel_pre_analyze'
698 * callback is resumable, so this function returns 0 if an error occurs or if it
699 * needs to wait, any other value otherwise.
700 *
701 * Note this function can be called many times for the same analyzer. In fact,
702 * it is called until the analyzer finishes its processing.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200703 */
704int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200705flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200706{
707 int ret = 1;
708
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100709 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
710
Christopher Fauletd7c91962015-04-30 11:48:27 +0200711 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200712 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100713 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200714 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200715 if (ret <= 0)
716 BREAK_EXECUTION(s, chn, check_result);
717 }
718 } RESUME_FILTER_END;
719
720 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100721 ret = handle_analyzer_result(s, chn, 0, ret);
722 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
723 return ret;
Christopher Faulet309c6412015-12-02 09:57:32 +0100724}
725
726/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200727 * Calls 'channel_post_analyze' callback for all filters attached to a
728 * stream. This function is called AFTER each analyzer attached to a channel,
729 * expects analyzers responsible for data sending. 'channel_post_analyze'
730 * callback is NOT resumable, so this function returns a 0 if an error occurs,
731 * any other value otherwise.
732 *
733 * Here, AFTER means when the analyzer finishes its processing.
734 */
735int
736flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
737{
738 struct filter *filter;
739 int ret = 1;
740
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100741 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
742
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200743 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
744 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100745 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200746 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
747 if (ret < 0)
748 break;
749 }
750 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100751 ret = handle_analyzer_result(s, chn, 0, ret);
752 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
753 return ret;
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200754}
755
756/*
Christopher Faulet0184ea72017-01-05 14:06:34 +0100757 * This function is the AN_REQ/RES_FLT_HTTP_HDRS analyzer, used to filter HTTP
758 * headers or a request or a response. Returns 0 if an error occurs or if it
759 * needs to wait, any other value otherwise.
Christopher Faulet309c6412015-12-02 09:57:32 +0100760 */
761int
762flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
763{
Christopher Faulet1339d742016-05-11 16:48:33 +0200764 struct http_msg *msg;
765 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100766
Christopher Faulet1339d742016-05-11 16:48:33 +0200767 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100768 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
769
Christopher Faulet309c6412015-12-02 09:57:32 +0100770 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200771 if (FLT_OPS(filter)->http_headers) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100772 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet1339d742016-05-11 16:48:33 +0200773 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100774 if (ret <= 0)
775 BREAK_EXECUTION(s, chn, check_result);
776 }
777 } RESUME_FILTER_END;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100778
779 if (HAS_DATA_FILTERS(s, chn)) {
780 size_t data = http_get_hdrs_size(htxbuf(&chn->buf));
781 struct filter *f;
782
783 list_for_each_entry(f, &strm_flt(s)->filters, list) {
784 if (IS_DATA_FILTER(f, chn))
785 FLT_OFF(f, chn) = data;
786 }
787 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100788
789 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100790 ret = handle_analyzer_result(s, chn, an_bit, ret);
791 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
792 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200793}
794
795/*
796 * Calls 'channel_end_analyze' callback for all filters attached to a
797 * stream. This function is called when we stop to analyze a request or a
798 * response. It is called after all other analyzers. 'channel_end_analyze'
799 * callback is resumable, so this function returns 0 if an error occurs or if it
800 * needs to wait, any other value otherwise.
801 */
802int
803flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
804{
805 int ret = 1;
806
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100807 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
808
Christopher Faulete6006242017-03-10 11:52:44 +0100809 /* Check if all filters attached on the stream have finished their
810 * processing on this channel. */
811 if (!(chn->flags & CF_FLT_ANALYZE))
812 goto sync;
813
Christopher Fauletd7c91962015-04-30 11:48:27 +0200814 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100815 FLT_OFF(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100816 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200817
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100818 if (FLT_OPS(filter)->channel_end_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100819 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100820 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200821 if (ret <= 0)
822 BREAK_EXECUTION(s, chn, end);
823 }
824 } RESUME_FILTER_END;
825
Christopher Faulete6006242017-03-10 11:52:44 +0100826 end:
827 /* We don't remove yet this analyzer because we need to synchronize the
828 * both channels. So here, we just remove the flag CF_FLT_ANALYZE. */
829 ret = handle_analyzer_result(s, chn, 0, ret);
Christopher Faulet570f7992017-07-06 15:53:02 +0200830 if (ret) {
Christopher Faulete6006242017-03-10 11:52:44 +0100831 chn->flags &= ~CF_FLT_ANALYZE;
Christopher Faulet02c7b222015-12-22 12:01:29 +0100832
Christopher Faulet570f7992017-07-06 15:53:02 +0200833 /* Pretend there is an activity on both channels. Flag on the
834 * current one will be automatically removed, so only the other
835 * one will remain. This is a way to be sure that
836 * 'channel_end_analyze' callback will have a chance to be
837 * called at least once for the other side to finish the current
Joseph Herlantb35ea682018-11-15 12:24:23 -0800838 * processing. Of course, this is the filter responsibility to
Christopher Faulet570f7992017-07-06 15:53:02 +0200839 * wakeup the stream if it choose to loop on this callback. */
840 s->req.flags |= CF_WAKE_ONCE;
841 s->res.flags |= CF_WAKE_ONCE;
842 }
843
844
Christopher Faulete6006242017-03-10 11:52:44 +0100845 sync:
846 /* Now we can check if filters have finished their work on the both
847 * channels */
848 if (!(s->req.flags & CF_FLT_ANALYZE) && !(s->res.flags & CF_FLT_ANALYZE)) {
849 /* Sync channels by removing this analyzer for the both channels */
850 s->req.analysers &= ~AN_REQ_FLT_END;
851 s->res.analysers &= ~AN_RES_FLT_END;
Christopher Fauletc6062be2016-10-31 11:22:37 +0100852
Christopher Faulete6006242017-03-10 11:52:44 +0100853 /* Remove backend filters from the list */
854 flt_stream_release(s, 1);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100855 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200856 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100857 else {
858 DBG_TRACE_DEVEL("waiting for sync", STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
859 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200860 return ret;
861}
862
Christopher Fauletd7c91962015-04-30 11:48:27 +0200863
864/*
Christopher Fauletb2e58492019-11-12 11:13:01 +0100865 * Calls 'tcp_payload' callback for all "data" filters attached to a
866 * stream. This function is called when some data can be forwarded in the
867 * AN_REQ_FLT_XFER_BODY and AN_RES_FLT_XFER_BODY analyzers. It takes care to
868 * update the filters and the stream offset to be sure that a filter cannot
869 * forward more data than its predecessors. A filter can choose to not forward
870 * all data. Returns a negative value if an error occurs, else the number of
871 * forwarded bytes.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200872 */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100873int
874flt_tcp_payload(struct stream *s, struct channel *chn, unsigned int len)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200875{
Christopher Fauletda02e172015-12-04 09:25:05 +0100876 struct filter *filter;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100877 unsigned long long *strm_off = &FLT_STRM_OFF(s, chn);
878 unsigned int out = co_data(chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100879 int ret, data;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200880
Christopher Faulet81340d72020-02-26 15:47:22 +0100881 ret = data = len - out;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100882 DBG_TRACE_ENTER(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100883 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100884 /* Call "data" filters only */
885 if (!IS_DATA_FILTER(filter, chn))
886 continue;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100887 if (FLT_OPS(filter)->tcp_payload) {
888 unsigned long long *flt_off = &FLT_OFF(filter, chn);
889 unsigned int offset = *flt_off - *strm_off;
Christopher Fauletda02e172015-12-04 09:25:05 +0100890
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100891 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100892 ret = FLT_OPS(filter)->tcp_payload(s, filter, chn, out + offset, data - offset);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200893 if (ret < 0)
894 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100895 data = ret + *flt_off - *strm_off;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100896 *flt_off += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200897 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200898 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100899
900 /* Only forward data if the last filter decides to forward something */
901 if (ret > 0) {
902 ret = data;
903 *strm_off += ret;
904 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200905 end:
Christopher Fauletb2e58492019-11-12 11:13:01 +0100906 DBG_TRACE_LEAVE(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200907 return ret;
908}
909
910/*
911 * Called when TCP data must be filtered on a channel. This function is the
Christopher Faulet0184ea72017-01-05 14:06:34 +0100912 * AN_REQ/RES_FLT_XFER_DATA analyzer. When called, it is responsible to forward
913 * data when the proxy is not in http mode. Behind the scene, it calls
914 * consecutively 'tcp_data' and 'tcp_forward_data' callbacks for all "data"
915 * filters attached to a stream. Returns 0 if an error occurs or if it needs to
916 * wait, any other value otherwise.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200917 */
918int
919flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
920{
Christopher Fauletb2e58492019-11-12 11:13:01 +0100921 unsigned int len;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200922 int ret = 1;
923
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100924 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
925
Christopher Fauletda02e172015-12-04 09:25:05 +0100926 /* If there is no "data" filters, we do nothing */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100927 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletda02e172015-12-04 09:25:05 +0100928 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200929
930 /* Be sure that the output is still opened. Else we stop the data
931 * filtering. */
932 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau44a41a82018-06-19 07:16:31 +0200933 ((chn->flags & CF_SHUTW) && (chn->to_forward || co_data(chn))))
Christopher Fauletd7c91962015-04-30 11:48:27 +0200934 goto end;
935
Christopher Fauletb2e58492019-11-12 11:13:01 +0100936 if (s->flags & SF_HTX) {
937 struct htx *htx = htxbuf(&chn->buf);
938 len = htx->data;
939 }
940 else
941 len = c_data(chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200942
Christopher Fauletb2e58492019-11-12 11:13:01 +0100943 ret = flt_tcp_payload(s, chn, len);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200944 if (ret < 0)
945 goto end;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200946 c_adv(chn, ret);
Christopher Fauletda02e172015-12-04 09:25:05 +0100947
Christopher Fauletd7c91962015-04-30 11:48:27 +0200948 /* Stop waiting data if the input in closed and no data is pending or if
949 * the output is closed. */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100950 if (chn->flags & CF_SHUTW) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200951 ret = 1;
952 goto end;
953 }
Christopher Fauletb2e58492019-11-12 11:13:01 +0100954 if (chn->flags & CF_SHUTR) {
955 if (((s->flags & SF_HTX) && htx_is_empty(htxbuf(&chn->buf))) || c_empty(chn)) {
956 ret = 1;
957 goto end;
958 }
959 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200960
961 /* Wait for data */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100962 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 +0200963 return 0;
964 end:
965 /* Terminate the data filtering. If <ret> is negative, an error was
966 * encountered during the filtering. */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100967 ret = handle_analyzer_result(s, chn, an_bit, ret);
968 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
969 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200970}
971
972/*
973 * Handles result of filter's analyzers. It returns 0 if an error occurs or if
974 * it needs to wait, any other value otherwise.
975 */
976static int
977handle_analyzer_result(struct stream *s, struct channel *chn,
978 unsigned int an_bit, int ret)
979{
980 int finst;
Christopher Faulete058f732019-09-06 15:24:55 +0200981 int status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200982
983 if (ret < 0)
984 goto return_bad_req;
985 else if (!ret)
986 goto wait;
987
988 /* End of job, return OK */
989 if (an_bit) {
990 chn->analysers &= ~an_bit;
991 chn->analyse_exp = TICK_ETERNITY;
992 }
993 return 1;
994
995 return_bad_req:
996 /* An error occurs */
997 channel_abort(&s->req);
998 channel_abort(&s->res);
999
1000 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001001 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001002 finst = SF_FINST_R;
Christopher Faulete058f732019-09-06 15:24:55 +02001003 status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001004 /* FIXME: incr counters */
1005 }
1006 else {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001007 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001008 finst = SF_FINST_H;
Christopher Faulete058f732019-09-06 15:24:55 +02001009 status = 502;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001010 /* FIXME: incr counters */
1011 }
1012
Christopher Faulet3d119692019-07-15 22:04:51 +02001013 if (IS_HTX_STRM(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001014 /* Do not do that when we are waiting for the next request */
Christopher Faulete058f732019-09-06 15:24:55 +02001015 if (s->txn->status > 0)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001016 http_reply_and_close(s, s->txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001017 else {
Christopher Faulete058f732019-09-06 15:24:55 +02001018 s->txn->status = status;
1019 http_reply_and_close(s, status, http_error_message(s));
Christopher Fauletd7c91962015-04-30 11:48:27 +02001020 }
1021 }
1022
1023 if (!(s->flags & SF_ERR_MASK))
1024 s->flags |= SF_ERR_PRXCOND;
1025 if (!(s->flags & SF_FINST_MASK))
1026 s->flags |= finst;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001027 DBG_TRACE_DEVEL("leaving on error", STRM_EV_FLT_ANA|STRM_EV_FLT_ERR, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001028 return 0;
1029
1030 wait:
1031 if (!(chn->flags & CF_ISRESP))
1032 channel_dont_connect(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001033 DBG_TRACE_DEVEL("wairing for more data", STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001034 return 0;
1035}
1036
1037
1038/* Note: must not be declared <const> as its list will be overwritten.
1039 * Please take care of keeping this list alphabetically sorted, doing so helps
1040 * all code contributors.
1041 * Optional keywords are also declared with a NULL ->parse() function so that
1042 * the config parser can report an appropriate error when a known keyword was
1043 * not enabled. */
1044static struct cfg_kw_list cfg_kws = {ILH, {
1045 { CFG_LISTEN, "filter", parse_filter },
1046 { 0, NULL, NULL },
1047 }
1048};
1049
Willy Tarreau0108d902018-11-25 19:14:37 +01001050INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1051
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001052REGISTER_POST_CHECK(flt_init_all);
1053REGISTER_PER_THREAD_INIT(flt_init_all_per_thread);
1054REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread);
1055
Christopher Fauletd7c91962015-04-30 11:48:27 +02001056/*
1057 * Local variables:
1058 * c-indent-level: 8
1059 * c-basic-offset: 8
1060 * End:
1061 */