blob: bf9e6ed69b1029a16f7ba32570565c45a2b4d7ad [file] [log] [blame]
Christopher Fauletd7c91962015-04-30 11:48:27 +02001/*
2 * Stream filters related variables and functions.
3 *
4 * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020013#include <haproxy/api.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020014#include <haproxy/buf-t.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020015#include <common/cfgparse.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020016#include <haproxy/errors.h>
Willy Tarreau7d865a52020-06-04 10:57:05 +020017#include <haproxy/flt_http_comp.h>
Willy Tarreau87735332020-06-04 09:08:41 +020018#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020019#include <haproxy/htx.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020020#include <haproxy/namespace.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020021#include <haproxy/tools.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
Willy Tarreau0a3bd392020-06-04 08:52:38 +020026#include <haproxy/compression.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020027#include <proto/filters.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020028#include <proto/http_ana.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020029#include <proto/stream.h>
30#include <proto/stream_interface.h>
31
Christopher Fauleteea8fc72019-11-05 16:18:10 +010032#define TRACE_SOURCE &trace_strm
33
Christopher Fauletd7c91962015-04-30 11:48:27 +020034/* Pool used to allocate filters */
Willy Tarreau8ceae722018-11-26 11:58:30 +010035DECLARE_STATIC_POOL(pool_head_filter, "filter", sizeof(struct filter));
Christopher Fauletd7c91962015-04-30 11:48:27 +020036
37static int handle_analyzer_result(struct stream *s, struct channel *chn, unsigned int an_bit, int ret);
38
39/* - RESUME_FILTER_LOOP and RESUME_FILTER_END must always be used together.
40 * The first one begins a loop and the seconds one ends it.
41 *
42 * - BREAK_EXECUTION must be used to break the loop and set the filter from
43 * which to resume the next time.
44 *
Bertrand Jacquin874a35c2018-09-10 21:26:07 +010045 * Here is an example:
Christopher Fauletd7c91962015-04-30 11:48:27 +020046 *
47 * RESUME_FILTER_LOOP(stream, channel) {
48 * ...
49 * if (cond)
50 * BREAK_EXECUTION(stream, channel, label);
51 * ...
52 * } RESUME_FILTER_END;
53 * ...
54 * label:
55 * ...
56 *
57 */
58#define RESUME_FILTER_LOOP(strm, chn) \
59 do { \
60 struct filter *filter; \
61 \
Christopher Fauletda02e172015-12-04 09:25:05 +010062 if (strm_flt(strm)->current[CHN_IDX(chn)]) { \
63 filter = strm_flt(strm)->current[CHN_IDX(chn)]; \
64 strm_flt(strm)->current[CHN_IDX(chn)] = NULL; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020065 goto resume_execution; \
66 } \
67 \
Christopher Fauletfcf035c2015-12-03 11:48:03 +010068 list_for_each_entry(filter, &strm_flt(s)->filters, list) { \
Christopher Fauletda02e172015-12-04 09:25:05 +010069 resume_execution:
Christopher Fauletd7c91962015-04-30 11:48:27 +020070
71#define RESUME_FILTER_END \
72 } \
73 } while(0)
74
Christopher Fauletda02e172015-12-04 09:25:05 +010075#define BREAK_EXECUTION(strm, chn, label) \
76 do { \
77 strm_flt(strm)->current[CHN_IDX(chn)] = filter; \
78 goto label; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020079 } while (0)
80
81
82/* List head of all known filter keywords */
83static struct flt_kw_list flt_keywords = {
84 .list = LIST_HEAD_INIT(flt_keywords.list)
85};
86
87/*
88 * Registers the filter keyword list <kwl> as a list of valid keywords for next
89 * parsing sessions.
90 */
91void
92flt_register_keywords(struct flt_kw_list *kwl)
93{
94 LIST_ADDQ(&flt_keywords.list, &kwl->list);
95}
96
97/*
98 * Returns a pointer to the filter keyword <kw>, or NULL if not found. If the
99 * keyword is found with a NULL ->parse() function, then an attempt is made to
100 * find one with a valid ->parse() function. This way it is possible to declare
101 * platform-dependant, known keywords as NULL, then only declare them as valid
102 * if some options are met. Note that if the requested keyword contains an
103 * opening parenthesis, everything from this point is ignored.
104 */
105struct flt_kw *
106flt_find_kw(const char *kw)
107{
108 int index;
109 const char *kwend;
110 struct flt_kw_list *kwl;
111 struct flt_kw *ret = NULL;
112
113 kwend = strchr(kw, '(');
114 if (!kwend)
115 kwend = kw + strlen(kw);
116
117 list_for_each_entry(kwl, &flt_keywords.list, list) {
118 for (index = 0; kwl->kw[index].kw != NULL; index++) {
119 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
120 kwl->kw[index].kw[kwend-kw] == 0) {
121 if (kwl->kw[index].parse)
122 return &kwl->kw[index]; /* found it !*/
123 else
124 ret = &kwl->kw[index]; /* may be OK */
125 }
126 }
127 }
128 return ret;
129}
130
131/*
132 * Dumps all registered "filter" keywords to the <out> string pointer. The
133 * unsupported keywords are only dumped if their supported form was not found.
134 */
135void
136flt_dump_kws(char **out)
137{
138 struct flt_kw_list *kwl;
139 int index;
140
Christopher Faulet784063e2020-05-18 12:14:18 +0200141 if (!out)
142 return;
143
Christopher Fauletd7c91962015-04-30 11:48:27 +0200144 *out = NULL;
145 list_for_each_entry(kwl, &flt_keywords.list, list) {
146 for (index = 0; kwl->kw[index].kw != NULL; index++) {
147 if (kwl->kw[index].parse ||
148 flt_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
149 memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "",
150 kwl->scope,
151 kwl->kw[index].kw,
152 kwl->kw[index].parse ? "" : " (not supported)");
153 }
154 }
155 }
156}
157
158/*
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100159 * Lists the known filters on <out>
160 */
161void
162list_filters(FILE *out)
163{
164 char *filters, *p, *f;
165
166 fprintf(out, "Available filters :\n");
167 flt_dump_kws(&filters);
168 for (p = filters; (f = strtok_r(p,"\n",&p));)
169 fprintf(out, "\t%s\n", f);
170 free(filters);
171}
172
173/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200174 * Parses the "filter" keyword. All keywords must be handled by filters
175 * themselves
176 */
177static int
178parse_filter(char **args, int section_type, struct proxy *curpx,
179 struct proxy *defpx, const char *file, int line, char **err)
180{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100181 struct flt_conf *fconf = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200182
183 /* Filter cannot be defined on a default proxy */
184 if (curpx == defpx) {
Christopher Fauletcc7317d2016-04-04 10:51:17 +0200185 memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.",
Christopher Fauletd7c91962015-04-30 11:48:27 +0200186 file, line, args[0]);
187 return -1;
188 }
189 if (!strcmp(args[0], "filter")) {
190 struct flt_kw *kw;
191 int cur_arg;
192
193 if (!*args[1]) {
194 memprintf(err,
195 "parsing [%s:%d] : missing argument for '%s' in %s '%s'.",
196 file, line, args[0], proxy_type_str(curpx), curpx->id);
197 goto error;
198 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100199 fconf = calloc(1, sizeof(*fconf));
200 if (!fconf) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200201 memprintf(err, "'%s' : out of memory", args[0]);
202 goto error;
203 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200204
205 cur_arg = 1;
206 kw = flt_find_kw(args[cur_arg]);
207 if (kw) {
208 if (!kw->parse) {
209 memprintf(err, "parsing [%s:%d] : '%s' : "
210 "'%s' option is not implemented in this version (check build options).",
211 file, line, args[0], args[cur_arg]);
212 goto error;
213 }
Thierry Fournier3610c392016-04-13 18:27:51 +0200214 if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200215 if (err && *err)
216 memprintf(err, "'%s' : '%s'",
217 args[0], *err);
218 else
219 memprintf(err, "'%s' : error encountered while processing '%s'",
220 args[0], args[cur_arg]);
221 goto error;
222 }
223 }
224 else {
225 flt_dump_kws(err);
226 indent_msg(err, 4);
227 memprintf(err, "'%s' : unknown keyword '%s'.%s%s",
228 args[0], args[cur_arg],
229 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
230 goto error;
231 }
232 if (*args[cur_arg]) {
233 memprintf(err, "'%s %s' : unknown keyword '%s'.",
234 args[0], args[1], args[cur_arg]);
235 goto error;
236 }
Christopher Faulet00e818a2016-04-19 17:00:44 +0200237 if (fconf->ops == NULL) {
238 memprintf(err, "'%s %s' : no callbacks defined.",
239 args[0], args[1]);
240 goto error;
241 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200242
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100243 LIST_ADDQ(&curpx->filter_configs, &fconf->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200244 }
245 return 0;
246
247 error:
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100248 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200249 return -1;
250
251
252}
253
254/*
255 * Calls 'init' callback for all filters attached to a proxy. This happens after
256 * the configuration parsing. Filters can finish to fill their config. Returns
257 * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
258 */
Willy Tarreau64bca592016-12-21 20:13:11 +0100259static int
Christopher Fauletd7c91962015-04-30 11:48:27 +0200260flt_init(struct proxy *proxy)
261{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100262 struct flt_conf *fconf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200263
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100264 list_for_each_entry(fconf, &proxy->filter_configs, list) {
265 if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200266 return ERR_ALERT|ERR_FATAL;
267 }
268 return 0;
269}
270
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200271/*
272 * Calls 'init_per_thread' callback for all filters attached to a proxy for each
273 * threads. This happens after the thread creation. Filters can finish to fill
274 * their config. Returns (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
275 */
276static int
277flt_init_per_thread(struct proxy *proxy)
278{
279 struct flt_conf *fconf;
280
281 list_for_each_entry(fconf, &proxy->filter_configs, list) {
282 if (fconf->ops->init_per_thread && fconf->ops->init_per_thread(proxy, fconf) < 0)
283 return ERR_ALERT|ERR_FATAL;
284 }
285 return 0;
286}
287
Willy Tarreau64bca592016-12-21 20:13:11 +0100288/* Calls flt_init() for all proxies, see above */
289static int
290flt_init_all()
291{
292 struct proxy *px;
293 int err_code = 0;
294
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100295 for (px = proxies_list; px; px = px->next) {
Willy Tarreau64bca592016-12-21 20:13:11 +0100296 err_code |= flt_init(px);
297 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100298 ha_alert("Failed to initialize filters for proxy '%s'.\n",
299 px->id);
Willy Tarreau64bca592016-12-21 20:13:11 +0100300 return err_code;
301 }
302 }
303 return 0;
304}
305
Joseph Herlantb35ea682018-11-15 12:24:23 -0800306/* Calls flt_init_per_thread() for all proxies, see above. Be careful here, it
307 * returns 0 if an error occurred. This is the opposite of flt_init_all. */
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200308static int
309flt_init_all_per_thread()
310{
311 struct proxy *px;
312 int err_code = 0;
313
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100314 for (px = proxies_list; px; px = px->next) {
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200315 err_code = flt_init_per_thread(px);
316 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100317 ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n",
318 px->id, tid);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200319 return 0;
320 }
321 }
322 return 1;
323}
324
Christopher Fauletd7c91962015-04-30 11:48:27 +0200325/*
326 * Calls 'check' callback for all filters attached to a proxy. This happens
327 * after the configuration parsing but before filters initialization. Returns
328 * the number of encountered errors.
329 */
330int
331flt_check(struct proxy *proxy)
332{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100333 struct flt_conf *fconf;
334 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200335
Christopher Fauletff17b182019-01-07 15:03:22 +0100336 err += check_implicit_http_comp_flt(proxy);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100337 list_for_each_entry(fconf, &proxy->filter_configs, list) {
338 if (fconf->ops->check)
339 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200340 }
341 return err;
342}
343
344/*
345 * Calls 'denit' callback for all filters attached to a proxy. This happens when
346 * HAProxy is stopped.
347 */
348void
349flt_deinit(struct proxy *proxy)
350{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100351 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200352
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100353 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
354 if (fconf->ops->deinit)
355 fconf->ops->deinit(proxy, fconf);
356 LIST_DEL(&fconf->list);
357 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200358 }
359}
360
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200361/*
362 * Calls 'denit_per_thread' callback for all filters attached to a proxy for
363 * each threads. This happens before exiting a thread.
364 */
365void
366flt_deinit_per_thread(struct proxy *proxy)
367{
368 struct flt_conf *fconf, *back;
369
370 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
371 if (fconf->ops->deinit_per_thread)
372 fconf->ops->deinit_per_thread(proxy, fconf);
373 }
374}
375
376
377/* Calls flt_deinit_per_thread() for all proxies, see above */
378static void
379flt_deinit_all_per_thread()
380{
381 struct proxy *px;
382
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100383 for (px = proxies_list; px; px = px->next)
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200384 flt_deinit_per_thread(px);
385}
386
Christopher Faulet92d36382015-11-05 13:35:03 +0100387/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
388static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100389flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100390{
Christopher Faulet75bc9132018-11-30 15:18:09 +0100391 struct filter *f;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200392
Christopher Faulet0f17a9b2019-04-05 10:11:38 +0200393 if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX))
Christopher Faulet75bc9132018-11-30 15:18:09 +0100394 return 0;
395
396 f = pool_alloc(pool_head_filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100397 if (!f) /* not enough memory */
398 return -1;
399 memset(f, 0, sizeof(*f));
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100400 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100401 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200402
403 if (FLT_OPS(f)->attach) {
404 int ret = FLT_OPS(f)->attach(s, f);
405 if (ret <= 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100406 pool_free(pool_head_filter, f);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200407 return ret;
408 }
409 }
410
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100411 LIST_ADDQ(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100412 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100413 return 0;
414}
415
416/*
417 * Called when a stream is created. It attaches all frontend filters to the
418 * stream. Returns -1 if an error occurs, 0 otherwise.
419 */
420int
421flt_stream_init(struct stream *s)
422{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100423 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100424
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100425 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
426 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100427 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
428 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100429 return -1;
430 }
431 return 0;
432}
433
434/*
435 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
436 * happens after each request/response exchange). When analyze ends, backend
437 * filters are removed. When the stream is closed, all filters attached to the
438 * stream are removed.
439 */
440void
441flt_stream_release(struct stream *s, int only_backend)
442{
443 struct filter *filter, *back;
444
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100445 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100446 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200447 if (FLT_OPS(filter)->detach)
448 FLT_OPS(filter)->detach(s, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100449 LIST_DEL(&filter->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100450 pool_free(pool_head_filter, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100451 }
452 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100453 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100454 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100455}
456
Christopher Fauletd7c91962015-04-30 11:48:27 +0200457/*
458 * Calls 'stream_start' for all filters attached to a stream. This happens when
459 * the stream is created, just after calling flt_stream_init
460 * function. Returns -1 if an error occurs, 0 otherwise.
461 */
462int
463flt_stream_start(struct stream *s)
464{
465 struct filter *filter;
466
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100467 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100468 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200469 return -1;
470 }
471 return 0;
472}
473
474/*
475 * Calls 'stream_stop' for all filters attached to a stream. This happens when
476 * the stream is stopped, just before calling flt_stream_release function.
477 */
478void
479flt_stream_stop(struct stream *s)
480{
481 struct filter *filter;
482
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100483 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100484 if (FLT_OPS(filter)->stream_stop)
485 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200486 }
487}
488
Christopher Faulet92d36382015-11-05 13:35:03 +0100489/*
Christopher Fauleta00d8172016-11-10 14:58:05 +0100490 * Calls 'check_timeouts' for all filters attached to a stream. This happens when
491 * the stream is woken up because of expired timer.
492 */
493void
494flt_stream_check_timeouts(struct stream *s)
495{
496 struct filter *filter;
497
498 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
499 if (FLT_OPS(filter)->check_timeouts)
500 FLT_OPS(filter)->check_timeouts(s, filter);
501 }
502}
503
504/*
Christopher Faulet92d36382015-11-05 13:35:03 +0100505 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200506 * are not the same, this function attaches all backend filters to the
507 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100508 */
509int
510flt_set_stream_backend(struct stream *s, struct proxy *be)
511{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100512 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200513 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100514
515 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200516 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100517
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100518 list_for_each_entry(fconf, &be->filter_configs, list) {
519 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100520 return -1;
521 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200522
523 end:
524 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
525 if (FLT_OPS(filter)->stream_set_backend &&
526 FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0)
527 return -1;
528 }
529
Christopher Faulet92d36382015-11-05 13:35:03 +0100530 return 0;
531}
532
Christopher Fauletd7c91962015-04-30 11:48:27 +0200533
534/*
535 * Calls 'http_end' callback for all filters attached to a stream. All filters
536 * are called here, but only if there is at least one "data" filter. This
537 * functions is called when all data were parsed and forwarded. 'http_end'
538 * callback is resumable, so this function returns a negative value if an error
539 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
540 */
541int
542flt_http_end(struct stream *s, struct http_msg *msg)
543{
544 int ret = 1;
545
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100546 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 +0200547 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100548 if (FLT_OPS(filter)->http_end) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100549 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100550 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200551 if (ret <= 0)
552 BREAK_EXECUTION(s, msg->chn, end);
553 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200554 } RESUME_FILTER_END;
555end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100556 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200557 return ret;
558}
559
560/*
561 * Calls 'http_reset' callback for all filters attached to a stream. This
562 * happens when a 100-continue response is received.
563 */
564void
565flt_http_reset(struct stream *s, struct http_msg *msg)
566{
567 struct filter *filter;
568
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100569 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 +0100570 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100571 if (FLT_OPS(filter)->http_reset) {
572 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100573 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100574 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200575 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100576 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200577}
578
579/*
580 * Calls 'http_reply' callback for all filters attached to a stream when HA
581 * decides to stop the HTTP message processing.
582 */
583void
Willy Tarreau83061a82018-07-13 11:56:34 +0200584flt_http_reply(struct stream *s, short status, const struct buffer *msg)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200585{
586 struct filter *filter;
587
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100588 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 +0100589 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100590 if (FLT_OPS(filter)->http_reply) {
591 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100592 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100593 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200594 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100595 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200596}
597
598/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100599 * Calls 'http_payload' callback for all "data" filters attached to a
600 * stream. This function is called when some data can be forwarded in the
601 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
602 * update the filters and the stream offset to be sure that a filter cannot
603 * forward more data than its predecessors. A filter can choose to not forward
604 * all data. Returns a negative value if an error occurs, else the number of
605 * forwarded bytes.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100606 */
607int
608flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len)
609{
610 struct filter *filter;
611 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
Christopher Faulet421e7692019-06-13 11:16:45 +0200612 unsigned int out = co_data(msg->chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100613 int ret, data;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100614
Christopher Faulet81340d72020-02-26 15:47:22 +0100615 ret = data = len - out;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100616 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100617 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
618 /* Call "data" filters only */
619 if (!IS_DATA_FILTER(filter, msg->chn))
620 continue;
621 if (FLT_OPS(filter)->http_payload) {
622 unsigned long long *flt_off = &FLT_OFF(filter, msg->chn);
623 unsigned int offset = *flt_off - *strm_off;
624
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100625 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100626 ret = FLT_OPS(filter)->http_payload(s, filter, msg, out + offset, data - offset);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100627 if (ret < 0)
628 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100629 data = ret + *flt_off - *strm_off;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100630 *flt_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100631 }
632 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100633
634 /* Only forward data if the last filter decides to forward something */
635 if (ret > 0) {
636 ret = data;
637 *strm_off += ret;
638 }
Christopher Faulet75bc9132018-11-30 15:18:09 +0100639 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100640 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100641 return ret;
642}
643
644/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200645 * Calls 'channel_start_analyze' callback for all filters attached to a
646 * stream. This function is called when we start to analyze a request or a
647 * response. For frontend filters, it is called before all other analyzers. For
648 * backend ones, it is called before all backend
649 * analyzers. 'channel_start_analyze' callback is resumable, so this function
650 * returns 0 if an error occurs or if it needs to wait, any other value
651 * otherwise.
652 */
653int
654flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
655{
656 int ret = 1;
657
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100658 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
659
Christopher Fauletd7c91962015-04-30 11:48:27 +0200660 /* If this function is called, this means there is at least one filter,
661 * so we do not need to check the filter list's emptiness. */
662
Christopher Faulete6006242017-03-10 11:52:44 +0100663 /* Set flag on channel to tell that the channel is filtered */
664 chn->flags |= CF_FLT_ANALYZE;
665
Christopher Fauletd7c91962015-04-30 11:48:27 +0200666 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100667 if (!(chn->flags & CF_ISRESP)) {
668 if (an_bit == AN_REQ_FLT_START_BE &&
669 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
670 continue;
671 }
672 else {
673 if (an_bit == AN_RES_FLT_START_BE &&
674 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
675 continue;
676 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200677
Christopher Fauletb2e58492019-11-12 11:13:01 +0100678 FLT_OFF(filter, chn) = 0;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100679 if (FLT_OPS(filter)->channel_start_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100680 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100681 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200682 if (ret <= 0)
683 BREAK_EXECUTION(s, chn, end);
684 }
685 } RESUME_FILTER_END;
686
687 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100688 ret = handle_analyzer_result(s, chn, an_bit, ret);
689 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
690 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200691}
692
693/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200694 * Calls 'channel_pre_analyze' callback for all filters attached to a
695 * stream. This function is called BEFORE each analyzer attached to a channel,
696 * expects analyzers responsible for data sending. 'channel_pre_analyze'
697 * callback is resumable, so this function returns 0 if an error occurs or if it
698 * needs to wait, any other value otherwise.
699 *
700 * Note this function can be called many times for the same analyzer. In fact,
701 * it is called until the analyzer finishes its processing.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200702 */
703int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200704flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200705{
706 int ret = 1;
707
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100708 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
709
Christopher Fauletd7c91962015-04-30 11:48:27 +0200710 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200711 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100712 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200713 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200714 if (ret <= 0)
715 BREAK_EXECUTION(s, chn, check_result);
716 }
717 } RESUME_FILTER_END;
718
719 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100720 ret = handle_analyzer_result(s, chn, 0, ret);
721 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
722 return ret;
Christopher Faulet309c6412015-12-02 09:57:32 +0100723}
724
725/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200726 * Calls 'channel_post_analyze' callback for all filters attached to a
727 * stream. This function is called AFTER each analyzer attached to a channel,
728 * expects analyzers responsible for data sending. 'channel_post_analyze'
729 * callback is NOT resumable, so this function returns a 0 if an error occurs,
730 * any other value otherwise.
731 *
732 * Here, AFTER means when the analyzer finishes its processing.
733 */
734int
735flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
736{
737 struct filter *filter;
738 int ret = 1;
739
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100740 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
741
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200742 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
743 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100744 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200745 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
746 if (ret < 0)
747 break;
748 }
749 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100750 ret = handle_analyzer_result(s, chn, 0, ret);
751 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
752 return ret;
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200753}
754
755/*
Christopher Faulet0184ea72017-01-05 14:06:34 +0100756 * This function is the AN_REQ/RES_FLT_HTTP_HDRS analyzer, used to filter HTTP
757 * headers or a request or a response. Returns 0 if an error occurs or if it
758 * needs to wait, any other value otherwise.
Christopher Faulet309c6412015-12-02 09:57:32 +0100759 */
760int
761flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
762{
Christopher Faulet1339d742016-05-11 16:48:33 +0200763 struct http_msg *msg;
764 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100765
Christopher Faulet1339d742016-05-11 16:48:33 +0200766 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100767 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
768
Christopher Faulet309c6412015-12-02 09:57:32 +0100769 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200770 if (FLT_OPS(filter)->http_headers) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100771 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet1339d742016-05-11 16:48:33 +0200772 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100773 if (ret <= 0)
774 BREAK_EXECUTION(s, chn, check_result);
775 }
776 } RESUME_FILTER_END;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100777
778 if (HAS_DATA_FILTERS(s, chn)) {
779 size_t data = http_get_hdrs_size(htxbuf(&chn->buf));
780 struct filter *f;
781
782 list_for_each_entry(f, &strm_flt(s)->filters, list) {
783 if (IS_DATA_FILTER(f, chn))
784 FLT_OFF(f, chn) = data;
785 }
786 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100787
788 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100789 ret = handle_analyzer_result(s, chn, an_bit, ret);
790 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
791 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200792}
793
794/*
795 * Calls 'channel_end_analyze' callback for all filters attached to a
796 * stream. This function is called when we stop to analyze a request or a
797 * response. It is called after all other analyzers. 'channel_end_analyze'
798 * callback is resumable, so this function returns 0 if an error occurs or if it
799 * needs to wait, any other value otherwise.
800 */
801int
802flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
803{
804 int ret = 1;
805
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100806 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
807
Christopher Faulete6006242017-03-10 11:52:44 +0100808 /* Check if all filters attached on the stream have finished their
809 * processing on this channel. */
810 if (!(chn->flags & CF_FLT_ANALYZE))
811 goto sync;
812
Christopher Fauletd7c91962015-04-30 11:48:27 +0200813 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100814 FLT_OFF(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100815 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200816
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100817 if (FLT_OPS(filter)->channel_end_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100818 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100819 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200820 if (ret <= 0)
821 BREAK_EXECUTION(s, chn, end);
822 }
823 } RESUME_FILTER_END;
824
Christopher Faulete6006242017-03-10 11:52:44 +0100825 end:
826 /* We don't remove yet this analyzer because we need to synchronize the
827 * both channels. So here, we just remove the flag CF_FLT_ANALYZE. */
828 ret = handle_analyzer_result(s, chn, 0, ret);
Christopher Faulet570f7992017-07-06 15:53:02 +0200829 if (ret) {
Christopher Faulete6006242017-03-10 11:52:44 +0100830 chn->flags &= ~CF_FLT_ANALYZE;
Christopher Faulet02c7b222015-12-22 12:01:29 +0100831
Christopher Faulet570f7992017-07-06 15:53:02 +0200832 /* Pretend there is an activity on both channels. Flag on the
833 * current one will be automatically removed, so only the other
834 * one will remain. This is a way to be sure that
835 * 'channel_end_analyze' callback will have a chance to be
836 * called at least once for the other side to finish the current
Joseph Herlantb35ea682018-11-15 12:24:23 -0800837 * processing. Of course, this is the filter responsibility to
Christopher Faulet570f7992017-07-06 15:53:02 +0200838 * wakeup the stream if it choose to loop on this callback. */
839 s->req.flags |= CF_WAKE_ONCE;
840 s->res.flags |= CF_WAKE_ONCE;
841 }
842
843
Christopher Faulete6006242017-03-10 11:52:44 +0100844 sync:
845 /* Now we can check if filters have finished their work on the both
846 * channels */
847 if (!(s->req.flags & CF_FLT_ANALYZE) && !(s->res.flags & CF_FLT_ANALYZE)) {
848 /* Sync channels by removing this analyzer for the both channels */
849 s->req.analysers &= ~AN_REQ_FLT_END;
850 s->res.analysers &= ~AN_RES_FLT_END;
Christopher Fauletc6062be2016-10-31 11:22:37 +0100851
Christopher Faulete6006242017-03-10 11:52:44 +0100852 /* Remove backend filters from the list */
853 flt_stream_release(s, 1);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100854 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200855 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100856 else {
857 DBG_TRACE_DEVEL("waiting for sync", STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
858 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200859 return ret;
860}
861
Christopher Fauletd7c91962015-04-30 11:48:27 +0200862
863/*
Christopher Fauletb2e58492019-11-12 11:13:01 +0100864 * Calls 'tcp_payload' callback for all "data" filters attached to a
865 * stream. This function is called when some data can be forwarded in the
866 * AN_REQ_FLT_XFER_BODY and AN_RES_FLT_XFER_BODY analyzers. It takes care to
867 * update the filters and the stream offset to be sure that a filter cannot
868 * forward more data than its predecessors. A filter can choose to not forward
869 * all data. Returns a negative value if an error occurs, else the number of
870 * forwarded bytes.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200871 */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100872int
873flt_tcp_payload(struct stream *s, struct channel *chn, unsigned int len)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200874{
Christopher Fauletda02e172015-12-04 09:25:05 +0100875 struct filter *filter;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100876 unsigned long long *strm_off = &FLT_STRM_OFF(s, chn);
877 unsigned int out = co_data(chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100878 int ret, data;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200879
Christopher Faulet81340d72020-02-26 15:47:22 +0100880 ret = data = len - out;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100881 DBG_TRACE_ENTER(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100882 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100883 /* Call "data" filters only */
884 if (!IS_DATA_FILTER(filter, chn))
885 continue;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100886 if (FLT_OPS(filter)->tcp_payload) {
887 unsigned long long *flt_off = &FLT_OFF(filter, chn);
888 unsigned int offset = *flt_off - *strm_off;
Christopher Fauletda02e172015-12-04 09:25:05 +0100889
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100890 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100891 ret = FLT_OPS(filter)->tcp_payload(s, filter, chn, out + offset, data - offset);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200892 if (ret < 0)
893 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100894 data = ret + *flt_off - *strm_off;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100895 *flt_off += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200896 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200897 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100898
899 /* Only forward data if the last filter decides to forward something */
900 if (ret > 0) {
901 ret = data;
902 *strm_off += ret;
903 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200904 end:
Christopher Fauletb2e58492019-11-12 11:13:01 +0100905 DBG_TRACE_LEAVE(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200906 return ret;
907}
908
909/*
910 * Called when TCP data must be filtered on a channel. This function is the
Christopher Faulet0184ea72017-01-05 14:06:34 +0100911 * AN_REQ/RES_FLT_XFER_DATA analyzer. When called, it is responsible to forward
912 * data when the proxy is not in http mode. Behind the scene, it calls
913 * consecutively 'tcp_data' and 'tcp_forward_data' callbacks for all "data"
914 * filters attached to a stream. Returns 0 if an error occurs or if it needs to
915 * wait, any other value otherwise.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200916 */
917int
918flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
919{
Christopher Fauletb2e58492019-11-12 11:13:01 +0100920 unsigned int len;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200921 int ret = 1;
922
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100923 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
924
Christopher Fauletda02e172015-12-04 09:25:05 +0100925 /* If there is no "data" filters, we do nothing */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100926 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletda02e172015-12-04 09:25:05 +0100927 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200928
929 /* Be sure that the output is still opened. Else we stop the data
930 * filtering. */
931 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau44a41a82018-06-19 07:16:31 +0200932 ((chn->flags & CF_SHUTW) && (chn->to_forward || co_data(chn))))
Christopher Fauletd7c91962015-04-30 11:48:27 +0200933 goto end;
934
Christopher Fauletb2e58492019-11-12 11:13:01 +0100935 if (s->flags & SF_HTX) {
936 struct htx *htx = htxbuf(&chn->buf);
937 len = htx->data;
938 }
939 else
940 len = c_data(chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200941
Christopher Fauletb2e58492019-11-12 11:13:01 +0100942 ret = flt_tcp_payload(s, chn, len);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200943 if (ret < 0)
944 goto end;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200945 c_adv(chn, ret);
Christopher Fauletda02e172015-12-04 09:25:05 +0100946
Christopher Fauletd7c91962015-04-30 11:48:27 +0200947 /* Stop waiting data if the input in closed and no data is pending or if
948 * the output is closed. */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100949 if (chn->flags & CF_SHUTW) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200950 ret = 1;
951 goto end;
952 }
Christopher Fauletb2e58492019-11-12 11:13:01 +0100953 if (chn->flags & CF_SHUTR) {
954 if (((s->flags & SF_HTX) && htx_is_empty(htxbuf(&chn->buf))) || c_empty(chn)) {
955 ret = 1;
956 goto end;
957 }
958 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200959
960 /* Wait for data */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100961 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 +0200962 return 0;
963 end:
964 /* Terminate the data filtering. If <ret> is negative, an error was
965 * encountered during the filtering. */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100966 ret = handle_analyzer_result(s, chn, an_bit, ret);
967 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
968 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200969}
970
971/*
972 * Handles result of filter's analyzers. It returns 0 if an error occurs or if
973 * it needs to wait, any other value otherwise.
974 */
975static int
976handle_analyzer_result(struct stream *s, struct channel *chn,
977 unsigned int an_bit, int ret)
978{
979 int finst;
Christopher Faulete058f732019-09-06 15:24:55 +0200980 int status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200981
982 if (ret < 0)
983 goto return_bad_req;
984 else if (!ret)
985 goto wait;
986
987 /* End of job, return OK */
988 if (an_bit) {
989 chn->analysers &= ~an_bit;
990 chn->analyse_exp = TICK_ETERNITY;
991 }
992 return 1;
993
994 return_bad_req:
995 /* An error occurs */
996 channel_abort(&s->req);
997 channel_abort(&s->res);
998
999 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001000 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001001 finst = SF_FINST_R;
Christopher Faulete058f732019-09-06 15:24:55 +02001002 status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001003 /* FIXME: incr counters */
1004 }
1005 else {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001006 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001007 finst = SF_FINST_H;
Christopher Faulete058f732019-09-06 15:24:55 +02001008 status = 502;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001009 /* FIXME: incr counters */
1010 }
1011
Christopher Faulet3d119692019-07-15 22:04:51 +02001012 if (IS_HTX_STRM(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001013 /* Do not do that when we are waiting for the next request */
Christopher Faulete058f732019-09-06 15:24:55 +02001014 if (s->txn->status > 0)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001015 http_reply_and_close(s, s->txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001016 else {
Christopher Faulete058f732019-09-06 15:24:55 +02001017 s->txn->status = status;
1018 http_reply_and_close(s, status, http_error_message(s));
Christopher Fauletd7c91962015-04-30 11:48:27 +02001019 }
1020 }
1021
1022 if (!(s->flags & SF_ERR_MASK))
1023 s->flags |= SF_ERR_PRXCOND;
1024 if (!(s->flags & SF_FINST_MASK))
1025 s->flags |= finst;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001026 DBG_TRACE_DEVEL("leaving on error", STRM_EV_FLT_ANA|STRM_EV_FLT_ERR, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001027 return 0;
1028
1029 wait:
1030 if (!(chn->flags & CF_ISRESP))
1031 channel_dont_connect(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001032 DBG_TRACE_DEVEL("wairing for more data", STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001033 return 0;
1034}
1035
1036
1037/* Note: must not be declared <const> as its list will be overwritten.
1038 * Please take care of keeping this list alphabetically sorted, doing so helps
1039 * all code contributors.
1040 * Optional keywords are also declared with a NULL ->parse() function so that
1041 * the config parser can report an appropriate error when a known keyword was
1042 * not enabled. */
1043static struct cfg_kw_list cfg_kws = {ILH, {
1044 { CFG_LISTEN, "filter", parse_filter },
1045 { 0, NULL, NULL },
1046 }
1047};
1048
Willy Tarreau0108d902018-11-25 19:14:37 +01001049INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1050
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001051REGISTER_POST_CHECK(flt_init_all);
1052REGISTER_PER_THREAD_INIT(flt_init_all_per_thread);
1053REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread);
1054
Christopher Fauletd7c91962015-04-30 11:48:27 +02001055/*
1056 * Local variables:
1057 * c-indent-level: 8
1058 * c-basic-offset: 8
1059 * End:
1060 */