blob: 1d4119b6ce30dd187575a82eec93ec3fc2d707ad [file] [log] [blame]
Christopher Fauletd7c91962015-04-30 11:48:27 +02001/*
2 * Stream filters related variables and functions.
3 *
4 * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020013#include <haproxy/api.h>
Willy Tarreau2741c8c2020-06-02 11:28:02 +020014#include <haproxy/buf-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020015#include <haproxy/cfgparse.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020016#include <haproxy/compression.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020017#include <haproxy/errors.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020018#include <haproxy/filters.h>
Willy Tarreau7d865a52020-06-04 10:57:05 +020019#include <haproxy/flt_http_comp.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020020#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020021#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020022#include <haproxy/htx.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020023#include <haproxy/namespace.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020024#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020025#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020026#include <haproxy/tools.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/trace.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020028
Christopher Fauletd7c91962015-04-30 11:48:27 +020029
Christopher Fauleteea8fc72019-11-05 16:18:10 +010030#define TRACE_SOURCE &trace_strm
31
Christopher Fauletd7c91962015-04-30 11:48:27 +020032/* Pool used to allocate filters */
Willy Tarreau8ceae722018-11-26 11:58:30 +010033DECLARE_STATIC_POOL(pool_head_filter, "filter", sizeof(struct filter));
Christopher Fauletd7c91962015-04-30 11:48:27 +020034
35static int handle_analyzer_result(struct stream *s, struct channel *chn, unsigned int an_bit, int ret);
36
37/* - RESUME_FILTER_LOOP and RESUME_FILTER_END must always be used together.
38 * The first one begins a loop and the seconds one ends it.
39 *
40 * - BREAK_EXECUTION must be used to break the loop and set the filter from
41 * which to resume the next time.
42 *
Bertrand Jacquin874a35c2018-09-10 21:26:07 +010043 * Here is an example:
Christopher Fauletd7c91962015-04-30 11:48:27 +020044 *
45 * RESUME_FILTER_LOOP(stream, channel) {
46 * ...
47 * if (cond)
48 * BREAK_EXECUTION(stream, channel, label);
49 * ...
50 * } RESUME_FILTER_END;
51 * ...
52 * label:
53 * ...
54 *
55 */
56#define RESUME_FILTER_LOOP(strm, chn) \
57 do { \
58 struct filter *filter; \
59 \
Christopher Fauletda02e172015-12-04 09:25:05 +010060 if (strm_flt(strm)->current[CHN_IDX(chn)]) { \
61 filter = strm_flt(strm)->current[CHN_IDX(chn)]; \
62 strm_flt(strm)->current[CHN_IDX(chn)] = NULL; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020063 goto resume_execution; \
64 } \
65 \
Christopher Fauletfcf035c2015-12-03 11:48:03 +010066 list_for_each_entry(filter, &strm_flt(s)->filters, list) { \
Christopher Fauletda02e172015-12-04 09:25:05 +010067 resume_execution:
Christopher Fauletd7c91962015-04-30 11:48:27 +020068
69#define RESUME_FILTER_END \
70 } \
71 } while(0)
72
Christopher Fauletda02e172015-12-04 09:25:05 +010073#define BREAK_EXECUTION(strm, chn, label) \
74 do { \
75 strm_flt(strm)->current[CHN_IDX(chn)] = filter; \
76 goto label; \
Christopher Fauletd7c91962015-04-30 11:48:27 +020077 } while (0)
78
79
80/* List head of all known filter keywords */
81static struct flt_kw_list flt_keywords = {
82 .list = LIST_HEAD_INIT(flt_keywords.list)
83};
84
85/*
86 * Registers the filter keyword list <kwl> as a list of valid keywords for next
87 * parsing sessions.
88 */
89void
90flt_register_keywords(struct flt_kw_list *kwl)
91{
92 LIST_ADDQ(&flt_keywords.list, &kwl->list);
93}
94
95/*
96 * Returns a pointer to the filter keyword <kw>, or NULL if not found. If the
97 * keyword is found with a NULL ->parse() function, then an attempt is made to
98 * find one with a valid ->parse() function. This way it is possible to declare
99 * platform-dependant, known keywords as NULL, then only declare them as valid
100 * if some options are met. Note that if the requested keyword contains an
101 * opening parenthesis, everything from this point is ignored.
102 */
103struct flt_kw *
104flt_find_kw(const char *kw)
105{
106 int index;
107 const char *kwend;
108 struct flt_kw_list *kwl;
109 struct flt_kw *ret = NULL;
110
111 kwend = strchr(kw, '(');
112 if (!kwend)
113 kwend = kw + strlen(kw);
114
115 list_for_each_entry(kwl, &flt_keywords.list, list) {
116 for (index = 0; kwl->kw[index].kw != NULL; index++) {
117 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
118 kwl->kw[index].kw[kwend-kw] == 0) {
119 if (kwl->kw[index].parse)
120 return &kwl->kw[index]; /* found it !*/
121 else
122 ret = &kwl->kw[index]; /* may be OK */
123 }
124 }
125 }
126 return ret;
127}
128
129/*
130 * Dumps all registered "filter" keywords to the <out> string pointer. The
131 * unsupported keywords are only dumped if their supported form was not found.
132 */
133void
134flt_dump_kws(char **out)
135{
136 struct flt_kw_list *kwl;
137 int index;
138
Christopher Faulet784063e2020-05-18 12:14:18 +0200139 if (!out)
140 return;
141
Christopher Fauletd7c91962015-04-30 11:48:27 +0200142 *out = NULL;
143 list_for_each_entry(kwl, &flt_keywords.list, list) {
144 for (index = 0; kwl->kw[index].kw != NULL; index++) {
145 if (kwl->kw[index].parse ||
146 flt_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
147 memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "",
148 kwl->scope,
149 kwl->kw[index].kw,
150 kwl->kw[index].parse ? "" : " (not supported)");
151 }
152 }
153 }
154}
155
156/*
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100157 * Lists the known filters on <out>
158 */
159void
160list_filters(FILE *out)
161{
162 char *filters, *p, *f;
163
164 fprintf(out, "Available filters :\n");
165 flt_dump_kws(&filters);
166 for (p = filters; (f = strtok_r(p,"\n",&p));)
167 fprintf(out, "\t%s\n", f);
168 free(filters);
169}
170
171/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200172 * Parses the "filter" keyword. All keywords must be handled by filters
173 * themselves
174 */
175static int
176parse_filter(char **args, int section_type, struct proxy *curpx,
177 struct proxy *defpx, const char *file, int line, char **err)
178{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100179 struct flt_conf *fconf = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200180
181 /* Filter cannot be defined on a default proxy */
182 if (curpx == defpx) {
Christopher Fauletcc7317d2016-04-04 10:51:17 +0200183 memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.",
Christopher Fauletd7c91962015-04-30 11:48:27 +0200184 file, line, args[0]);
185 return -1;
186 }
187 if (!strcmp(args[0], "filter")) {
188 struct flt_kw *kw;
189 int cur_arg;
190
191 if (!*args[1]) {
192 memprintf(err,
193 "parsing [%s:%d] : missing argument for '%s' in %s '%s'.",
194 file, line, args[0], proxy_type_str(curpx), curpx->id);
195 goto error;
196 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100197 fconf = calloc(1, sizeof(*fconf));
198 if (!fconf) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200199 memprintf(err, "'%s' : out of memory", args[0]);
200 goto error;
201 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200202
203 cur_arg = 1;
204 kw = flt_find_kw(args[cur_arg]);
205 if (kw) {
206 if (!kw->parse) {
207 memprintf(err, "parsing [%s:%d] : '%s' : "
208 "'%s' option is not implemented in this version (check build options).",
209 file, line, args[0], args[cur_arg]);
210 goto error;
211 }
Thierry Fournier3610c392016-04-13 18:27:51 +0200212 if (kw->parse(args, &cur_arg, curpx, fconf, err, kw->private) != 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200213 if (err && *err)
214 memprintf(err, "'%s' : '%s'",
215 args[0], *err);
216 else
217 memprintf(err, "'%s' : error encountered while processing '%s'",
218 args[0], args[cur_arg]);
219 goto error;
220 }
221 }
222 else {
223 flt_dump_kws(err);
224 indent_msg(err, 4);
225 memprintf(err, "'%s' : unknown keyword '%s'.%s%s",
226 args[0], args[cur_arg],
227 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
228 goto error;
229 }
230 if (*args[cur_arg]) {
231 memprintf(err, "'%s %s' : unknown keyword '%s'.",
232 args[0], args[1], args[cur_arg]);
233 goto error;
234 }
Christopher Faulet00e818a2016-04-19 17:00:44 +0200235 if (fconf->ops == NULL) {
236 memprintf(err, "'%s %s' : no callbacks defined.",
237 args[0], args[1]);
238 goto error;
239 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200240
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100241 LIST_ADDQ(&curpx->filter_configs, &fconf->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200242 }
243 return 0;
244
245 error:
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100246 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200247 return -1;
248
249
250}
251
252/*
253 * Calls 'init' callback for all filters attached to a proxy. This happens after
254 * the configuration parsing. Filters can finish to fill their config. Returns
255 * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
256 */
Willy Tarreau64bca592016-12-21 20:13:11 +0100257static int
Christopher Fauletd7c91962015-04-30 11:48:27 +0200258flt_init(struct proxy *proxy)
259{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100260 struct flt_conf *fconf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200261
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100262 list_for_each_entry(fconf, &proxy->filter_configs, list) {
263 if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200264 return ERR_ALERT|ERR_FATAL;
265 }
266 return 0;
267}
268
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200269/*
270 * Calls 'init_per_thread' callback for all filters attached to a proxy for each
271 * threads. This happens after the thread creation. Filters can finish to fill
272 * their config. Returns (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
273 */
274static int
275flt_init_per_thread(struct proxy *proxy)
276{
277 struct flt_conf *fconf;
278
279 list_for_each_entry(fconf, &proxy->filter_configs, list) {
280 if (fconf->ops->init_per_thread && fconf->ops->init_per_thread(proxy, fconf) < 0)
281 return ERR_ALERT|ERR_FATAL;
282 }
283 return 0;
284}
285
Willy Tarreau64bca592016-12-21 20:13:11 +0100286/* Calls flt_init() for all proxies, see above */
287static int
288flt_init_all()
289{
290 struct proxy *px;
291 int err_code = 0;
292
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100293 for (px = proxies_list; px; px = px->next) {
Willy Tarreau64bca592016-12-21 20:13:11 +0100294 err_code |= flt_init(px);
295 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100296 ha_alert("Failed to initialize filters for proxy '%s'.\n",
297 px->id);
Willy Tarreau64bca592016-12-21 20:13:11 +0100298 return err_code;
299 }
300 }
301 return 0;
302}
303
Joseph Herlantb35ea682018-11-15 12:24:23 -0800304/* Calls flt_init_per_thread() for all proxies, see above. Be careful here, it
305 * returns 0 if an error occurred. This is the opposite of flt_init_all. */
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200306static int
307flt_init_all_per_thread()
308{
309 struct proxy *px;
310 int err_code = 0;
311
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100312 for (px = proxies_list; px; px = px->next) {
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200313 err_code = flt_init_per_thread(px);
314 if (err_code & (ERR_ABORT|ERR_FATAL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100315 ha_alert("Failed to initialize filters for proxy '%s' for thread %u.\n",
316 px->id, tid);
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200317 return 0;
318 }
319 }
320 return 1;
321}
322
Christopher Fauletd7c91962015-04-30 11:48:27 +0200323/*
324 * Calls 'check' callback for all filters attached to a proxy. This happens
325 * after the configuration parsing but before filters initialization. Returns
326 * the number of encountered errors.
327 */
328int
329flt_check(struct proxy *proxy)
330{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100331 struct flt_conf *fconf;
332 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200333
Christopher Fauletff17b182019-01-07 15:03:22 +0100334 err += check_implicit_http_comp_flt(proxy);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100335 list_for_each_entry(fconf, &proxy->filter_configs, list) {
336 if (fconf->ops->check)
337 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200338 }
339 return err;
340}
341
342/*
343 * Calls 'denit' callback for all filters attached to a proxy. This happens when
344 * HAProxy is stopped.
345 */
346void
347flt_deinit(struct proxy *proxy)
348{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100349 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200350
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100351 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
352 if (fconf->ops->deinit)
353 fconf->ops->deinit(proxy, fconf);
354 LIST_DEL(&fconf->list);
355 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200356 }
357}
358
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200359/*
360 * Calls 'denit_per_thread' callback for all filters attached to a proxy for
361 * each threads. This happens before exiting a thread.
362 */
363void
364flt_deinit_per_thread(struct proxy *proxy)
365{
366 struct flt_conf *fconf, *back;
367
368 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
369 if (fconf->ops->deinit_per_thread)
370 fconf->ops->deinit_per_thread(proxy, fconf);
371 }
372}
373
374
375/* Calls flt_deinit_per_thread() for all proxies, see above */
376static void
377flt_deinit_all_per_thread()
378{
379 struct proxy *px;
380
Olivier Houchardfbc74e82017-11-24 16:54:05 +0100381 for (px = proxies_list; px; px = px->next)
Christopher Faulet71a6a8e2017-07-27 16:33:28 +0200382 flt_deinit_per_thread(px);
383}
384
Christopher Faulet92d36382015-11-05 13:35:03 +0100385/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
386static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100387flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100388{
Christopher Faulet75bc9132018-11-30 15:18:09 +0100389 struct filter *f;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200390
Christopher Faulet0f17a9b2019-04-05 10:11:38 +0200391 if (IS_HTX_STRM(s) && !(fconf->flags & FLT_CFG_FL_HTX))
Christopher Faulet75bc9132018-11-30 15:18:09 +0100392 return 0;
393
394 f = pool_alloc(pool_head_filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100395 if (!f) /* not enough memory */
396 return -1;
397 memset(f, 0, sizeof(*f));
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100398 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100399 f->flags |= flags;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200400
401 if (FLT_OPS(f)->attach) {
402 int ret = FLT_OPS(f)->attach(s, f);
403 if (ret <= 0) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100404 pool_free(pool_head_filter, f);
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200405 return ret;
406 }
407 }
408
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100409 LIST_ADDQ(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100410 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100411 return 0;
412}
413
414/*
415 * Called when a stream is created. It attaches all frontend filters to the
416 * stream. Returns -1 if an error occurs, 0 otherwise.
417 */
418int
419flt_stream_init(struct stream *s)
420{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100421 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100422
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100423 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
424 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100425 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
426 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100427 return -1;
428 }
429 return 0;
430}
431
432/*
433 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
434 * happens after each request/response exchange). When analyze ends, backend
435 * filters are removed. When the stream is closed, all filters attached to the
436 * stream are removed.
437 */
438void
439flt_stream_release(struct stream *s, int only_backend)
440{
441 struct filter *filter, *back;
442
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100443 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100444 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200445 if (FLT_OPS(filter)->detach)
446 FLT_OPS(filter)->detach(s, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100447 LIST_DEL(&filter->list);
Willy Tarreaubafbe012017-11-24 17:34:44 +0100448 pool_free(pool_head_filter, filter);
Christopher Faulet92d36382015-11-05 13:35:03 +0100449 }
450 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100451 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100452 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100453}
454
Christopher Fauletd7c91962015-04-30 11:48:27 +0200455/*
456 * Calls 'stream_start' for all filters attached to a stream. This happens when
457 * the stream is created, just after calling flt_stream_init
458 * function. Returns -1 if an error occurs, 0 otherwise.
459 */
460int
461flt_stream_start(struct stream *s)
462{
463 struct filter *filter;
464
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100465 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100466 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200467 return -1;
468 }
469 return 0;
470}
471
472/*
473 * Calls 'stream_stop' for all filters attached to a stream. This happens when
474 * the stream is stopped, just before calling flt_stream_release function.
475 */
476void
477flt_stream_stop(struct stream *s)
478{
479 struct filter *filter;
480
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100481 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100482 if (FLT_OPS(filter)->stream_stop)
483 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200484 }
485}
486
Christopher Faulet92d36382015-11-05 13:35:03 +0100487/*
Christopher Fauleta00d8172016-11-10 14:58:05 +0100488 * Calls 'check_timeouts' for all filters attached to a stream. This happens when
489 * the stream is woken up because of expired timer.
490 */
491void
492flt_stream_check_timeouts(struct stream *s)
493{
494 struct filter *filter;
495
496 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
497 if (FLT_OPS(filter)->check_timeouts)
498 FLT_OPS(filter)->check_timeouts(s, filter);
499 }
500}
501
502/*
Christopher Faulet92d36382015-11-05 13:35:03 +0100503 * Called when a backend is set for a stream. If the frontend and the backend
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200504 * are not the same, this function attaches all backend filters to the
505 * stream. Returns -1 if an error occurs, 0 otherwise.
Christopher Faulet92d36382015-11-05 13:35:03 +0100506 */
507int
508flt_set_stream_backend(struct stream *s, struct proxy *be)
509{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100510 struct flt_conf *fconf;
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200511 struct filter *filter;
Christopher Faulet92d36382015-11-05 13:35:03 +0100512
513 if (strm_fe(s) == be)
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200514 goto end;
Christopher Faulet92d36382015-11-05 13:35:03 +0100515
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100516 list_for_each_entry(fconf, &be->filter_configs, list) {
517 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100518 return -1;
519 }
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200520
521 end:
522 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
523 if (FLT_OPS(filter)->stream_set_backend &&
524 FLT_OPS(filter)->stream_set_backend(s, filter, be) < 0)
525 return -1;
526 }
527
Christopher Faulet92d36382015-11-05 13:35:03 +0100528 return 0;
529}
530
Christopher Fauletd7c91962015-04-30 11:48:27 +0200531
532/*
533 * Calls 'http_end' callback for all filters attached to a stream. All filters
534 * are called here, but only if there is at least one "data" filter. This
535 * functions is called when all data were parsed and forwarded. 'http_end'
536 * callback is resumable, so this function returns a negative value if an error
537 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
538 */
539int
540flt_http_end(struct stream *s, struct http_msg *msg)
541{
542 int ret = 1;
543
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100544 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 +0200545 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100546 if (FLT_OPS(filter)->http_end) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100547 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100548 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200549 if (ret <= 0)
550 BREAK_EXECUTION(s, msg->chn, end);
551 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200552 } RESUME_FILTER_END;
553end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100554 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200555 return ret;
556}
557
558/*
559 * Calls 'http_reset' callback for all filters attached to a stream. This
560 * happens when a 100-continue response is received.
561 */
562void
563flt_http_reset(struct stream *s, struct http_msg *msg)
564{
565 struct filter *filter;
566
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100567 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 +0100568 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100569 if (FLT_OPS(filter)->http_reset) {
570 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100571 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100572 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200573 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100574 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200575}
576
577/*
578 * Calls 'http_reply' callback for all filters attached to a stream when HA
579 * decides to stop the HTTP message processing.
580 */
581void
Willy Tarreau83061a82018-07-13 11:56:34 +0200582flt_http_reply(struct stream *s, short status, const struct buffer *msg)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200583{
584 struct filter *filter;
585
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100586 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 +0100587 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100588 if (FLT_OPS(filter)->http_reply) {
589 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100590 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100591 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200592 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100593 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200594}
595
596/*
Christopher Faulet75bc9132018-11-30 15:18:09 +0100597 * Calls 'http_payload' callback for all "data" filters attached to a
598 * stream. This function is called when some data can be forwarded in the
599 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
600 * update the filters and the stream offset to be sure that a filter cannot
601 * forward more data than its predecessors. A filter can choose to not forward
602 * all data. Returns a negative value if an error occurs, else the number of
603 * forwarded bytes.
Christopher Faulet75bc9132018-11-30 15:18:09 +0100604 */
605int
606flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len)
607{
608 struct filter *filter;
609 unsigned long long *strm_off = &FLT_STRM_OFF(s, msg->chn);
Christopher Faulet421e7692019-06-13 11:16:45 +0200610 unsigned int out = co_data(msg->chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100611 int ret, data;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100612
Christopher Faulet81340d72020-02-26 15:47:22 +0100613 ret = data = len - out;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100614 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 +0100615 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
616 /* Call "data" filters only */
617 if (!IS_DATA_FILTER(filter, msg->chn))
618 continue;
619 if (FLT_OPS(filter)->http_payload) {
620 unsigned long long *flt_off = &FLT_OFF(filter, msg->chn);
621 unsigned int offset = *flt_off - *strm_off;
622
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100623 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100624 ret = FLT_OPS(filter)->http_payload(s, filter, msg, out + offset, data - offset);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100625 if (ret < 0)
626 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100627 data = ret + *flt_off - *strm_off;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100628 *flt_off += ret;
Christopher Faulet75bc9132018-11-30 15:18:09 +0100629 }
630 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100631
632 /* Only forward data if the last filter decides to forward something */
633 if (ret > 0) {
634 ret = data;
635 *strm_off += ret;
636 }
Christopher Faulet75bc9132018-11-30 15:18:09 +0100637 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100638 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet75bc9132018-11-30 15:18:09 +0100639 return ret;
640}
641
642/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200643 * Calls 'channel_start_analyze' callback for all filters attached to a
644 * stream. This function is called when we start to analyze a request or a
645 * response. For frontend filters, it is called before all other analyzers. For
646 * backend ones, it is called before all backend
647 * analyzers. 'channel_start_analyze' callback is resumable, so this function
648 * returns 0 if an error occurs or if it needs to wait, any other value
649 * otherwise.
650 */
651int
652flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
653{
654 int ret = 1;
655
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100656 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
657
Christopher Fauletd7c91962015-04-30 11:48:27 +0200658 /* If this function is called, this means there is at least one filter,
659 * so we do not need to check the filter list's emptiness. */
660
Christopher Faulete6006242017-03-10 11:52:44 +0100661 /* Set flag on channel to tell that the channel is filtered */
662 chn->flags |= CF_FLT_ANALYZE;
663
Christopher Fauletd7c91962015-04-30 11:48:27 +0200664 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100665 if (!(chn->flags & CF_ISRESP)) {
666 if (an_bit == AN_REQ_FLT_START_BE &&
667 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
668 continue;
669 }
670 else {
671 if (an_bit == AN_RES_FLT_START_BE &&
672 !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
673 continue;
674 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200675
Christopher Fauletb2e58492019-11-12 11:13:01 +0100676 FLT_OFF(filter, chn) = 0;
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100677 if (FLT_OPS(filter)->channel_start_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100678 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100679 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200680 if (ret <= 0)
681 BREAK_EXECUTION(s, chn, end);
682 }
683 } RESUME_FILTER_END;
684
685 end:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100686 ret = handle_analyzer_result(s, chn, an_bit, ret);
687 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
688 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200689}
690
691/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200692 * Calls 'channel_pre_analyze' callback for all filters attached to a
693 * stream. This function is called BEFORE each analyzer attached to a channel,
694 * expects analyzers responsible for data sending. 'channel_pre_analyze'
695 * callback is resumable, so this function returns 0 if an error occurs or if it
696 * needs to wait, any other value otherwise.
697 *
698 * Note this function can be called many times for the same analyzer. In fact,
699 * it is called until the analyzer finishes its processing.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200700 */
701int
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200702flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200703{
704 int ret = 1;
705
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100706 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
707
Christopher Fauletd7c91962015-04-30 11:48:27 +0200708 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200709 if (FLT_OPS(filter)->channel_pre_analyze && (filter->pre_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100710 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200711 ret = FLT_OPS(filter)->channel_pre_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200712 if (ret <= 0)
713 BREAK_EXECUTION(s, chn, check_result);
714 }
715 } RESUME_FILTER_END;
716
717 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100718 ret = handle_analyzer_result(s, chn, 0, ret);
719 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
720 return ret;
Christopher Faulet309c6412015-12-02 09:57:32 +0100721}
722
723/*
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200724 * Calls 'channel_post_analyze' callback for all filters attached to a
725 * stream. This function is called AFTER each analyzer attached to a channel,
726 * expects analyzers responsible for data sending. 'channel_post_analyze'
727 * callback is NOT resumable, so this function returns a 0 if an error occurs,
728 * any other value otherwise.
729 *
730 * Here, AFTER means when the analyzer finishes its processing.
731 */
732int
733flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
734{
735 struct filter *filter;
736 int ret = 1;
737
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100738 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
739
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200740 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
741 if (FLT_OPS(filter)->channel_post_analyze && (filter->post_analyzers & an_bit)) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100742 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200743 ret = FLT_OPS(filter)->channel_post_analyze(s, filter, chn, an_bit);
744 if (ret < 0)
745 break;
746 }
747 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100748 ret = handle_analyzer_result(s, chn, 0, ret);
749 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
750 return ret;
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200751}
752
753/*
Christopher Faulet0184ea72017-01-05 14:06:34 +0100754 * This function is the AN_REQ/RES_FLT_HTTP_HDRS analyzer, used to filter HTTP
755 * headers or a request or a response. Returns 0 if an error occurs or if it
756 * needs to wait, any other value otherwise.
Christopher Faulet309c6412015-12-02 09:57:32 +0100757 */
758int
759flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
760{
Christopher Faulet1339d742016-05-11 16:48:33 +0200761 struct http_msg *msg;
762 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100763
Christopher Faulet1339d742016-05-11 16:48:33 +0200764 msg = ((chn->flags & CF_ISRESP) ? &s->txn->rsp : &s->txn->req);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100765 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s, s->txn, msg);
766
Christopher Faulet309c6412015-12-02 09:57:32 +0100767 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet1339d742016-05-11 16:48:33 +0200768 if (FLT_OPS(filter)->http_headers) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100769 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet1339d742016-05-11 16:48:33 +0200770 ret = FLT_OPS(filter)->http_headers(s, filter, msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100771 if (ret <= 0)
772 BREAK_EXECUTION(s, chn, check_result);
773 }
774 } RESUME_FILTER_END;
Christopher Faulet9c44e482020-02-12 15:31:20 +0100775
776 if (HAS_DATA_FILTERS(s, chn)) {
777 size_t data = http_get_hdrs_size(htxbuf(&chn->buf));
778 struct filter *f;
779
780 list_for_each_entry(f, &strm_flt(s)->filters, list) {
781 if (IS_DATA_FILTER(f, chn))
782 FLT_OFF(f, chn) = data;
783 }
784 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100785
786 check_result:
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100787 ret = handle_analyzer_result(s, chn, an_bit, ret);
788 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_HTTP_ANA|STRM_EV_FLT_ANA, s);
789 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200790}
791
792/*
793 * Calls 'channel_end_analyze' callback for all filters attached to a
794 * stream. This function is called when we stop to analyze a request or a
795 * response. It is called after all other analyzers. 'channel_end_analyze'
796 * callback is resumable, so this function returns 0 if an error occurs or if it
797 * needs to wait, any other value otherwise.
798 */
799int
800flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
801{
802 int ret = 1;
803
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100804 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
805
Christopher Faulete6006242017-03-10 11:52:44 +0100806 /* Check if all filters attached on the stream have finished their
807 * processing on this channel. */
808 if (!(chn->flags & CF_FLT_ANALYZE))
809 goto sync;
810
Christopher Fauletd7c91962015-04-30 11:48:27 +0200811 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletb2e58492019-11-12 11:13:01 +0100812 FLT_OFF(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100813 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200814
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100815 if (FLT_OPS(filter)->channel_end_analyze) {
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100816 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_FLT_ANA, s);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100817 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200818 if (ret <= 0)
819 BREAK_EXECUTION(s, chn, end);
820 }
821 } RESUME_FILTER_END;
822
Christopher Faulete6006242017-03-10 11:52:44 +0100823 end:
824 /* We don't remove yet this analyzer because we need to synchronize the
825 * both channels. So here, we just remove the flag CF_FLT_ANALYZE. */
826 ret = handle_analyzer_result(s, chn, 0, ret);
Christopher Faulet570f7992017-07-06 15:53:02 +0200827 if (ret) {
Christopher Faulete6006242017-03-10 11:52:44 +0100828 chn->flags &= ~CF_FLT_ANALYZE;
Christopher Faulet02c7b222015-12-22 12:01:29 +0100829
Christopher Faulet570f7992017-07-06 15:53:02 +0200830 /* Pretend there is an activity on both channels. Flag on the
831 * current one will be automatically removed, so only the other
832 * one will remain. This is a way to be sure that
833 * 'channel_end_analyze' callback will have a chance to be
834 * called at least once for the other side to finish the current
Joseph Herlantb35ea682018-11-15 12:24:23 -0800835 * processing. Of course, this is the filter responsibility to
Christopher Faulet570f7992017-07-06 15:53:02 +0200836 * wakeup the stream if it choose to loop on this callback. */
837 s->req.flags |= CF_WAKE_ONCE;
838 s->res.flags |= CF_WAKE_ONCE;
839 }
840
841
Christopher Faulete6006242017-03-10 11:52:44 +0100842 sync:
843 /* Now we can check if filters have finished their work on the both
844 * channels */
845 if (!(s->req.flags & CF_FLT_ANALYZE) && !(s->res.flags & CF_FLT_ANALYZE)) {
846 /* Sync channels by removing this analyzer for the both channels */
847 s->req.analysers &= ~AN_REQ_FLT_END;
848 s->res.analysers &= ~AN_RES_FLT_END;
Christopher Fauletc6062be2016-10-31 11:22:37 +0100849
Christopher Faulete6006242017-03-10 11:52:44 +0100850 /* Remove backend filters from the list */
851 flt_stream_release(s, 1);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100852 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200853 }
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100854 else {
855 DBG_TRACE_DEVEL("waiting for sync", STRM_EV_STRM_ANA|STRM_EV_FLT_ANA, s);
856 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200857 return ret;
858}
859
Christopher Fauletd7c91962015-04-30 11:48:27 +0200860
861/*
Christopher Fauletb2e58492019-11-12 11:13:01 +0100862 * Calls 'tcp_payload' callback for all "data" filters attached to a
863 * stream. This function is called when some data can be forwarded in the
864 * AN_REQ_FLT_XFER_BODY and AN_RES_FLT_XFER_BODY analyzers. It takes care to
865 * update the filters and the stream offset to be sure that a filter cannot
866 * forward more data than its predecessors. A filter can choose to not forward
867 * all data. Returns a negative value if an error occurs, else the number of
868 * forwarded bytes.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200869 */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100870int
871flt_tcp_payload(struct stream *s, struct channel *chn, unsigned int len)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200872{
Christopher Fauletda02e172015-12-04 09:25:05 +0100873 struct filter *filter;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100874 unsigned long long *strm_off = &FLT_STRM_OFF(s, chn);
875 unsigned int out = co_data(chn);
Christopher Faulet81340d72020-02-26 15:47:22 +0100876 int ret, data;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200877
Christopher Faulet81340d72020-02-26 15:47:22 +0100878 ret = data = len - out;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100879 DBG_TRACE_ENTER(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100880 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100881 /* Call "data" filters only */
882 if (!IS_DATA_FILTER(filter, chn))
883 continue;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100884 if (FLT_OPS(filter)->tcp_payload) {
885 unsigned long long *flt_off = &FLT_OFF(filter, chn);
886 unsigned int offset = *flt_off - *strm_off;
Christopher Fauletda02e172015-12-04 09:25:05 +0100887
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100888 DBG_TRACE_DEVEL(FLT_ID(filter), STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Faulet71179a32020-02-07 16:40:33 +0100889 ret = FLT_OPS(filter)->tcp_payload(s, filter, chn, out + offset, data - offset);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200890 if (ret < 0)
891 goto end;
Christopher Fauletc50ee0b2020-02-24 16:20:09 +0100892 data = ret + *flt_off - *strm_off;
Christopher Fauletb2e58492019-11-12 11:13:01 +0100893 *flt_off += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200894 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200895 }
Christopher Faulet71179a32020-02-07 16:40:33 +0100896
897 /* Only forward data if the last filter decides to forward something */
898 if (ret > 0) {
899 ret = data;
900 *strm_off += ret;
901 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200902 end:
Christopher Fauletb2e58492019-11-12 11:13:01 +0100903 DBG_TRACE_LEAVE(STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200904 return ret;
905}
906
907/*
908 * Called when TCP data must be filtered on a channel. This function is the
Christopher Faulet0184ea72017-01-05 14:06:34 +0100909 * AN_REQ/RES_FLT_XFER_DATA analyzer. When called, it is responsible to forward
910 * data when the proxy is not in http mode. Behind the scene, it calls
911 * consecutively 'tcp_data' and 'tcp_forward_data' callbacks for all "data"
912 * filters attached to a stream. Returns 0 if an error occurs or if it needs to
913 * wait, any other value otherwise.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200914 */
915int
916flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
917{
Christopher Fauletb2e58492019-11-12 11:13:01 +0100918 unsigned int len;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200919 int ret = 1;
920
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100921 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
922
Christopher Fauletda02e172015-12-04 09:25:05 +0100923 /* If there is no "data" filters, we do nothing */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100924 if (!HAS_DATA_FILTERS(s, chn))
Christopher Fauletda02e172015-12-04 09:25:05 +0100925 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200926
927 /* Be sure that the output is still opened. Else we stop the data
928 * filtering. */
929 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau44a41a82018-06-19 07:16:31 +0200930 ((chn->flags & CF_SHUTW) && (chn->to_forward || co_data(chn))))
Christopher Fauletd7c91962015-04-30 11:48:27 +0200931 goto end;
932
Christopher Fauletb2e58492019-11-12 11:13:01 +0100933 if (s->flags & SF_HTX) {
934 struct htx *htx = htxbuf(&chn->buf);
935 len = htx->data;
936 }
937 else
938 len = c_data(chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200939
Christopher Fauletb2e58492019-11-12 11:13:01 +0100940 ret = flt_tcp_payload(s, chn, len);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200941 if (ret < 0)
942 goto end;
Willy Tarreaubcbd3932018-06-06 07:13:22 +0200943 c_adv(chn, ret);
Christopher Fauletda02e172015-12-04 09:25:05 +0100944
Christopher Fauletd7c91962015-04-30 11:48:27 +0200945 /* Stop waiting data if the input in closed and no data is pending or if
946 * the output is closed. */
Christopher Fauletb2e58492019-11-12 11:13:01 +0100947 if (chn->flags & CF_SHUTW) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200948 ret = 1;
949 goto end;
950 }
Christopher Fauletb2e58492019-11-12 11:13:01 +0100951 if (chn->flags & CF_SHUTR) {
952 if (((s->flags & SF_HTX) && htx_is_empty(htxbuf(&chn->buf))) || c_empty(chn)) {
953 ret = 1;
954 goto end;
955 }
956 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200957
958 /* Wait for data */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100959 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 +0200960 return 0;
961 end:
962 /* Terminate the data filtering. If <ret> is negative, an error was
963 * encountered during the filtering. */
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100964 ret = handle_analyzer_result(s, chn, an_bit, ret);
965 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_FLT_ANA, s);
966 return ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200967}
968
969/*
970 * Handles result of filter's analyzers. It returns 0 if an error occurs or if
971 * it needs to wait, any other value otherwise.
972 */
973static int
974handle_analyzer_result(struct stream *s, struct channel *chn,
975 unsigned int an_bit, int ret)
976{
977 int finst;
Christopher Faulete058f732019-09-06 15:24:55 +0200978 int status = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200979
980 if (ret < 0)
981 goto return_bad_req;
982 else if (!ret)
983 goto wait;
984
985 /* End of job, return OK */
986 if (an_bit) {
987 chn->analysers &= ~an_bit;
988 chn->analyse_exp = TICK_ETERNITY;
989 }
990 return 1;
991
992 return_bad_req:
993 /* An error occurs */
994 channel_abort(&s->req);
995 channel_abort(&s->res);
996
997 if (!(chn->flags & CF_ISRESP)) {
Christopher Faulet0184ea72017-01-05 14:06:34 +0100998 s->req.analysers &= AN_REQ_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200999 finst = SF_FINST_R;
Christopher Faulete058f732019-09-06 15:24:55 +02001000 status = 400;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001001 /* FIXME: incr counters */
1002 }
1003 else {
Christopher Faulet0184ea72017-01-05 14:06:34 +01001004 s->res.analysers &= AN_RES_FLT_END;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001005 finst = SF_FINST_H;
Christopher Faulete058f732019-09-06 15:24:55 +02001006 status = 502;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001007 /* FIXME: incr counters */
1008 }
1009
Christopher Faulet3d119692019-07-15 22:04:51 +02001010 if (IS_HTX_STRM(s)) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001011 /* Do not do that when we are waiting for the next request */
Christopher Faulete058f732019-09-06 15:24:55 +02001012 if (s->txn->status > 0)
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02001013 http_reply_and_close(s, s->txn->status, NULL);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001014 else {
Christopher Faulete058f732019-09-06 15:24:55 +02001015 s->txn->status = status;
1016 http_reply_and_close(s, status, http_error_message(s));
Christopher Fauletd7c91962015-04-30 11:48:27 +02001017 }
1018 }
1019
1020 if (!(s->flags & SF_ERR_MASK))
1021 s->flags |= SF_ERR_PRXCOND;
1022 if (!(s->flags & SF_FINST_MASK))
1023 s->flags |= finst;
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001024 DBG_TRACE_DEVEL("leaving on error", STRM_EV_FLT_ANA|STRM_EV_FLT_ERR, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001025 return 0;
1026
1027 wait:
1028 if (!(chn->flags & CF_ISRESP))
1029 channel_dont_connect(chn);
Christopher Fauleteea8fc72019-11-05 16:18:10 +01001030 DBG_TRACE_DEVEL("wairing for more data", STRM_EV_FLT_ANA, s);
Christopher Fauletd7c91962015-04-30 11:48:27 +02001031 return 0;
1032}
1033
1034
1035/* Note: must not be declared <const> as its list will be overwritten.
1036 * Please take care of keeping this list alphabetically sorted, doing so helps
1037 * all code contributors.
1038 * Optional keywords are also declared with a NULL ->parse() function so that
1039 * the config parser can report an appropriate error when a known keyword was
1040 * not enabled. */
1041static struct cfg_kw_list cfg_kws = {ILH, {
1042 { CFG_LISTEN, "filter", parse_filter },
1043 { 0, NULL, NULL },
1044 }
1045};
1046
Willy Tarreau0108d902018-11-25 19:14:37 +01001047INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1048
Willy Tarreau172f5ce2018-11-26 11:21:50 +01001049REGISTER_POST_CHECK(flt_init_all);
1050REGISTER_PER_THREAD_INIT(flt_init_all_per_thread);
1051REGISTER_PER_THREAD_DEINIT(flt_deinit_all_per_thread);
1052
Christopher Fauletd7c91962015-04-30 11:48:27 +02001053/*
1054 * Local variables:
1055 * c-indent-level: 8
1056 * c-basic-offset: 8
1057 * End:
1058 */