blob: c277c12b86fe7e5b5dea78928b6956f03b2ec1f3 [file] [log] [blame]
Christopher Fauletd7c91962015-04-30 11:48:27 +02001/*
2 * Stream filters related variables and functions.
3 *
4 * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <common/buffer.h>
14#include <common/debug.h>
15#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/errors.h>
19#include <common/namespace.h>
20#include <common/standard.h>
21
22#include <types/filters.h>
23#include <types/proto_http.h>
24
25#include <proto/compression.h>
26#include <proto/filters.h>
Christopher Faulet92d36382015-11-05 13:35:03 +010027#include <proto/flt_http_comp.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020028#include <proto/proto_http.h>
29#include <proto/stream.h>
30#include <proto/stream_interface.h>
31
32/* Pool used to allocate filters */
33struct pool_head *pool2_filter = NULL;
34
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 *
43 * Here is an exemple:
44 *
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
139 *out = NULL;
140 list_for_each_entry(kwl, &flt_keywords.list, list) {
141 for (index = 0; kwl->kw[index].kw != NULL; index++) {
142 if (kwl->kw[index].parse ||
143 flt_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
144 memprintf(out, "%s[%4s] %s%s\n", *out ? *out : "",
145 kwl->scope,
146 kwl->kw[index].kw,
147 kwl->kw[index].parse ? "" : " (not supported)");
148 }
149 }
150 }
151}
152
153/*
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100154 * Lists the known filters on <out>
155 */
156void
157list_filters(FILE *out)
158{
159 char *filters, *p, *f;
160
161 fprintf(out, "Available filters :\n");
162 flt_dump_kws(&filters);
163 for (p = filters; (f = strtok_r(p,"\n",&p));)
164 fprintf(out, "\t%s\n", f);
165 free(filters);
166}
167
168/*
Christopher Fauletd7c91962015-04-30 11:48:27 +0200169 * Parses the "filter" keyword. All keywords must be handled by filters
170 * themselves
171 */
172static int
173parse_filter(char **args, int section_type, struct proxy *curpx,
174 struct proxy *defpx, const char *file, int line, char **err)
175{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100176 struct flt_conf *fconf = NULL;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200177
178 /* Filter cannot be defined on a default proxy */
179 if (curpx == defpx) {
Christopher Fauletcc7317d2016-04-04 10:51:17 +0200180 memprintf(err, "parsing [%s:%d] : %s is not allowed in a 'default' section.",
Christopher Fauletd7c91962015-04-30 11:48:27 +0200181 file, line, args[0]);
182 return -1;
183 }
184 if (!strcmp(args[0], "filter")) {
185 struct flt_kw *kw;
186 int cur_arg;
187
188 if (!*args[1]) {
189 memprintf(err,
190 "parsing [%s:%d] : missing argument for '%s' in %s '%s'.",
191 file, line, args[0], proxy_type_str(curpx), curpx->id);
192 goto error;
193 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100194 fconf = calloc(1, sizeof(*fconf));
195 if (!fconf) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200196 memprintf(err, "'%s' : out of memory", args[0]);
197 goto error;
198 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200199
200 cur_arg = 1;
201 kw = flt_find_kw(args[cur_arg]);
202 if (kw) {
203 if (!kw->parse) {
204 memprintf(err, "parsing [%s:%d] : '%s' : "
205 "'%s' option is not implemented in this version (check build options).",
206 file, line, args[0], args[cur_arg]);
207 goto error;
208 }
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100209 if (kw->parse(args, &cur_arg, curpx, fconf, err) != 0) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200210 if (err && *err)
211 memprintf(err, "'%s' : '%s'",
212 args[0], *err);
213 else
214 memprintf(err, "'%s' : error encountered while processing '%s'",
215 args[0], args[cur_arg]);
216 goto error;
217 }
218 }
219 else {
220 flt_dump_kws(err);
221 indent_msg(err, 4);
222 memprintf(err, "'%s' : unknown keyword '%s'.%s%s",
223 args[0], args[cur_arg],
224 err && *err ? " Registered keywords :" : "", err && *err ? *err : "");
225 goto error;
226 }
227 if (*args[cur_arg]) {
228 memprintf(err, "'%s %s' : unknown keyword '%s'.",
229 args[0], args[1], args[cur_arg]);
230 goto error;
231 }
232
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100233 LIST_ADDQ(&curpx->filter_configs, &fconf->list);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200234 }
235 return 0;
236
237 error:
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100238 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200239 return -1;
240
241
242}
243
244/*
245 * Calls 'init' callback for all filters attached to a proxy. This happens after
246 * the configuration parsing. Filters can finish to fill their config. Returns
247 * (ERR_ALERT|ERR_FATAL) if an error occurs, 0 otherwise.
248 */
249int
250flt_init(struct proxy *proxy)
251{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100252 struct flt_conf *fconf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200253
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100254 list_for_each_entry(fconf, &proxy->filter_configs, list) {
255 if (fconf->ops->init && fconf->ops->init(proxy, fconf) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200256 return ERR_ALERT|ERR_FATAL;
257 }
258 return 0;
259}
260
261/*
262 * Calls 'check' callback for all filters attached to a proxy. This happens
263 * after the configuration parsing but before filters initialization. Returns
264 * the number of encountered errors.
265 */
266int
267flt_check(struct proxy *proxy)
268{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100269 struct flt_conf *fconf;
270 int err = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200271
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100272 list_for_each_entry(fconf, &proxy->filter_configs, list) {
273 if (fconf->ops->check)
274 err += fconf->ops->check(proxy, fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200275 }
Christopher Faulet92d36382015-11-05 13:35:03 +0100276 err += check_legacy_http_comp_flt(proxy);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200277 return err;
278}
279
280/*
281 * Calls 'denit' callback for all filters attached to a proxy. This happens when
282 * HAProxy is stopped.
283 */
284void
285flt_deinit(struct proxy *proxy)
286{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100287 struct flt_conf *fconf, *back;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200288
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100289 list_for_each_entry_safe(fconf, back, &proxy->filter_configs, list) {
290 if (fconf->ops->deinit)
291 fconf->ops->deinit(proxy, fconf);
292 LIST_DEL(&fconf->list);
293 free(fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200294 }
295}
296
Christopher Faulet92d36382015-11-05 13:35:03 +0100297/* Attaches a filter to a stream. Returns -1 if an error occurs, 0 otherwise. */
298static int
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100299flt_stream_add_filter(struct stream *s, struct flt_conf *fconf, unsigned int flags)
Christopher Faulet92d36382015-11-05 13:35:03 +0100300{
301 struct filter *f = pool_alloc2(pool2_filter);
302 if (!f) /* not enough memory */
303 return -1;
304 memset(f, 0, sizeof(*f));
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100305 f->config = fconf;
Christopher Fauletda02e172015-12-04 09:25:05 +0100306 f->flags |= flags;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100307 LIST_ADDQ(&strm_flt(s)->filters, &f->list);
Christopher Fauletda02e172015-12-04 09:25:05 +0100308 strm_flt(s)->flags |= STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100309 return 0;
310}
311
312/*
313 * Called when a stream is created. It attaches all frontend filters to the
314 * stream. Returns -1 if an error occurs, 0 otherwise.
315 */
316int
317flt_stream_init(struct stream *s)
318{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100319 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100320
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100321 memset(strm_flt(s), 0, sizeof(*strm_flt(s)));
322 LIST_INIT(&strm_flt(s)->filters);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100323 list_for_each_entry(fconf, &strm_fe(s)->filter_configs, list) {
324 if (flt_stream_add_filter(s, fconf, 0) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100325 return -1;
326 }
327 return 0;
328}
329
330/*
331 * Called when a stream is closed or when analyze ends (For an HTTP stream, this
332 * happens after each request/response exchange). When analyze ends, backend
333 * filters are removed. When the stream is closed, all filters attached to the
334 * stream are removed.
335 */
336void
337flt_stream_release(struct stream *s, int only_backend)
338{
339 struct filter *filter, *back;
340
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100341 list_for_each_entry_safe(filter, back, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100342 if (!only_backend || (filter->flags & FLT_FL_IS_BACKEND_FILTER)) {
Christopher Faulet92d36382015-11-05 13:35:03 +0100343 LIST_DEL(&filter->list);
344 pool_free2(pool2_filter, filter);
345 }
346 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100347 if (LIST_ISEMPTY(&strm_flt(s)->filters))
Christopher Fauletda02e172015-12-04 09:25:05 +0100348 strm_flt(s)->flags &= ~STRM_FLT_FL_HAS_FILTERS;
Christopher Faulet92d36382015-11-05 13:35:03 +0100349}
350
Christopher Fauletd7c91962015-04-30 11:48:27 +0200351/*
352 * Calls 'stream_start' for all filters attached to a stream. This happens when
353 * the stream is created, just after calling flt_stream_init
354 * function. Returns -1 if an error occurs, 0 otherwise.
355 */
356int
357flt_stream_start(struct stream *s)
358{
359 struct filter *filter;
360
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100361 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100362 if (FLT_OPS(filter)->stream_start && FLT_OPS(filter)->stream_start(s, filter) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200363 return -1;
364 }
365 return 0;
366}
367
368/*
369 * Calls 'stream_stop' for all filters attached to a stream. This happens when
370 * the stream is stopped, just before calling flt_stream_release function.
371 */
372void
373flt_stream_stop(struct stream *s)
374{
375 struct filter *filter;
376
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100377 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100378 if (FLT_OPS(filter)->stream_stop)
379 FLT_OPS(filter)->stream_stop(s, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200380 }
381}
382
Christopher Faulet92d36382015-11-05 13:35:03 +0100383/*
384 * Called when a backend is set for a stream. If the frontend and the backend
385 * are the same, this function does nothing. Else it attaches all backend
386 * filters to the stream. Returns -1 if an error occurs, 0 otherwise.
387 */
388int
389flt_set_stream_backend(struct stream *s, struct proxy *be)
390{
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100391 struct flt_conf *fconf;
Christopher Faulet92d36382015-11-05 13:35:03 +0100392
393 if (strm_fe(s) == be)
394 return 0;
395
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100396 list_for_each_entry(fconf, &be->filter_configs, list) {
397 if (flt_stream_add_filter(s, fconf, FLT_FL_IS_BACKEND_FILTER) < 0)
Christopher Faulet92d36382015-11-05 13:35:03 +0100398 return -1;
399 }
400 return 0;
401}
402
Christopher Fauletd7c91962015-04-30 11:48:27 +0200403/*
404 * Calls 'http_data' callback for all "data" filters attached to a stream. This
405 * function is called when incoming data are available (excluding chunks
406 * envelope for chunked messages) in the AN_REQ_HTTP_XFER_BODY and
407 * AN_RES_HTTP_XFER_BODY analyzers. It takes care to update the next offset of
408 * filters and adjusts available data to be sure that a filter cannot parse more
409 * data than its predecessors. A filter can choose to not consume all available
410 * data. Returns -1 if an error occurs, the number of consumed bytes otherwise.
411 */
412int
413flt_http_data(struct stream *s, struct http_msg *msg)
414{
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100415 struct filter *filter;
416 struct buffer *buf = msg->chn->buf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200417 unsigned int buf_i;
418 int ret = 0;
419
Christopher Fauletd7c91962015-04-30 11:48:27 +0200420 /* Save buffer state */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100421 buf_i = buf->i;
422
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100423 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100424 unsigned int *nxt;
425
426 /* Call "data" filters only */
427 if (!IS_DATA_FILTER(filter, msg->chn))
428 continue;
429
Christopher Faulet2fb28802015-12-01 10:40:57 +0100430 /* If the HTTP parser is ahead, we update the next offset of the
431 * current filter. This happens for chunked messages, at the
432 * begining of a new chunk. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100433 nxt = &FLT_NXT(filter, msg->chn);
434 if (msg->next > *nxt)
435 *nxt = msg->next;
436
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100437 if (FLT_OPS(filter)->http_data) {
438 ret = FLT_OPS(filter)->http_data(s, filter, msg);
Christopher Fauletda02e172015-12-04 09:25:05 +0100439 if (ret < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200440 break;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100441
442 /* Update the next offset of the current filter */
Christopher Fauletda02e172015-12-04 09:25:05 +0100443 *nxt += ret;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100444
445 /* And set this value as the bound for the next
446 * filter. It will not able to parse more data than this
447 * one. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100448 buf->i = *nxt;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200449 }
450 else {
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100451 /* Consume all available data and update the next offset
452 * of the current filter. buf->i is untouched here. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100453 ret = MIN(msg->chunk_len + msg->next, buf->i) - *nxt;
454 *nxt += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200455 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200456 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100457
Christopher Fauletd7c91962015-04-30 11:48:27 +0200458 /* Restore the original buffer state */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100459 buf->i = buf_i;
460
Christopher Fauletd7c91962015-04-30 11:48:27 +0200461 return ret;
462}
463
Christopher Fauletd7c91962015-04-30 11:48:27 +0200464/*
465 * Calls 'http_chunk_trailers' callback for all "data" filters attached to a
466 * stream. This function is called for chunked messages only when a part of the
467 * trailers was parsed in the AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY
468 * analyzers. Filters can know how much data were parsed by the HTTP parsing
469 * until the last call with the msg->sol value. Returns a negative value if an
470 * error occurs, any other value otherwise.
471 */
472int
473flt_http_chunk_trailers(struct stream *s, struct http_msg *msg)
474{
Christopher Faulet2fb28802015-12-01 10:40:57 +0100475 struct filter *filter;
Christopher Fauletda02e172015-12-04 09:25:05 +0100476 int ret = 1;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200477
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100478 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100479 unsigned int *nxt;
480
481 /* Call "data" filters only */
482 if (!IS_DATA_FILTER(filter, msg->chn))
483 continue;
484
Christopher Faulet2fb28802015-12-01 10:40:57 +0100485 /* Be sure to set the next offset of the filter at the right
486 * place. This is really useful when the first part of the
487 * trailers was parsed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100488 nxt = &FLT_NXT(filter, msg->chn);
489 *nxt = msg->next;
490
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100491 if (FLT_OPS(filter)->http_chunk_trailers) {
492 ret = FLT_OPS(filter)->http_chunk_trailers(s, filter, msg);
Christopher Faulet2fb28802015-12-01 10:40:57 +0100493 if (ret < 0)
494 break;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200495 }
Christopher Faulet2fb28802015-12-01 10:40:57 +0100496 /* Update the next offset of the current filter. Here all data
497 * are always consumed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100498 *nxt += msg->sol;
Christopher Faulet2fb28802015-12-01 10:40:57 +0100499 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200500 return ret;
501}
502
503/*
504 * Calls 'http_end' callback for all filters attached to a stream. All filters
505 * are called here, but only if there is at least one "data" filter. This
506 * functions is called when all data were parsed and forwarded. 'http_end'
507 * callback is resumable, so this function returns a negative value if an error
508 * occurs, 0 if it needs to wait for some reason, any other value otherwise.
509 */
510int
511flt_http_end(struct stream *s, struct http_msg *msg)
512{
513 int ret = 1;
514
Christopher Fauletd7c91962015-04-30 11:48:27 +0200515 RESUME_FILTER_LOOP(s, msg->chn) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100516 if (FLT_OPS(filter)->http_end) {
517 ret = FLT_OPS(filter)->http_end(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200518 if (ret <= 0)
519 BREAK_EXECUTION(s, msg->chn, end);
520 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200521 } RESUME_FILTER_END;
522end:
523 return ret;
524}
525
526/*
527 * Calls 'http_reset' callback for all filters attached to a stream. This
528 * happens when a 100-continue response is received.
529 */
530void
531flt_http_reset(struct stream *s, struct http_msg *msg)
532{
533 struct filter *filter;
534
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100535 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100536 if (FLT_OPS(filter)->http_reset)
537 FLT_OPS(filter)->http_reset(s, filter, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200538 }
539}
540
541/*
542 * Calls 'http_reply' callback for all filters attached to a stream when HA
543 * decides to stop the HTTP message processing.
544 */
545void
546flt_http_reply(struct stream *s, short status, const struct chunk *msg)
547{
548 struct filter *filter;
549
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100550 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100551 if (FLT_OPS(filter)->http_reply)
552 FLT_OPS(filter)->http_reply(s, filter, status, msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200553 }
554}
555
556/*
557 * Calls 'http_forward_data' callback for all "data" filters attached to a
558 * stream. This function is called when some data can be forwarded in the
559 * AN_REQ_HTTP_XFER_BODY and AN_RES_HTTP_XFER_BODY analyzers. It takes care to
560 * update the forward offset of filters and adjusts "forwardable" data to be
561 * sure that a filter cannot forward more data than its predecessors. A filter
562 * can choose to not forward all parsed data. Returns a negative value if an
563 * error occurs, else the number of forwarded bytes.
564 */
565int
566flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len)
567{
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100568 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200569 int ret = len;
570
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100571 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100572 unsigned int *nxt, *fwd;
573
574 /* Call "data" filters only */
575 if (!IS_DATA_FILTER(filter, msg->chn))
576 continue;
577
Christopher Faulet2fb28802015-12-01 10:40:57 +0100578 /* If the HTTP parser is ahead, we update the next offset of the
579 * current filter. This happens for chunked messages, when the
580 * chunk envelope is parsed. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100581 nxt = &FLT_NXT(filter, msg->chn);
582 fwd = &FLT_FWD(filter, msg->chn);
583 if (msg->next > *nxt)
584 *nxt = msg->next;
585
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100586 if (FLT_OPS(filter)->http_forward_data) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100587 /* Remove bytes that the current filter considered as
588 * forwarded */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100589 ret = FLT_OPS(filter)->http_forward_data(s, filter, msg, ret - *fwd);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200590 if (ret < 0)
591 goto end;
592 }
593
594 /* Adjust bytes that the current filter considers as
595 * forwarded */
Christopher Fauletda02e172015-12-04 09:25:05 +0100596 *fwd += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200597
598 /* And set this value as the bound for the next filter. It will
599 * not able to forward more data than the current one. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100600 ret = *fwd;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200601 }
602
603 if (!ret)
604 goto end;
605
606 /* Finally, adjust filters offsets by removing data that HAProxy will
607 * forward. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100608 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100609 if (!IS_DATA_FILTER(filter, msg->chn))
610 continue;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200611 FLT_NXT(filter, msg->chn) -= ret;
612 FLT_FWD(filter, msg->chn) -= ret;
613 }
614 end:
615 return ret;
616}
617
618/*
619 * Calls 'channel_start_analyze' callback for all filters attached to a
620 * stream. This function is called when we start to analyze a request or a
621 * response. For frontend filters, it is called before all other analyzers. For
622 * backend ones, it is called before all backend
623 * analyzers. 'channel_start_analyze' callback is resumable, so this function
624 * returns 0 if an error occurs or if it needs to wait, any other value
625 * otherwise.
626 */
627int
628flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
629{
630 int ret = 1;
631
632 /* If this function is called, this means there is at least one filter,
633 * so we do not need to check the filter list's emptiness. */
634
635 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100636 if (an_bit == AN_FLT_START_BE && !(filter->flags & FLT_FL_IS_BACKEND_FILTER))
Christopher Fauletd7c91962015-04-30 11:48:27 +0200637 continue;
638
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100639 FLT_NXT(filter, chn) = 0;
640 FLT_FWD(filter, chn) = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200641
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100642 if (FLT_OPS(filter)->channel_start_analyze) {
643 ret = FLT_OPS(filter)->channel_start_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200644 if (ret <= 0)
645 BREAK_EXECUTION(s, chn, end);
646 }
647 } RESUME_FILTER_END;
648
649 end:
650 return handle_analyzer_result(s, chn, an_bit, ret);
651}
652
653/*
654 * Calls 'channel_analyze' callback for all filters attached to a stream. This
655 * function is called before each analyzer attached to a channel, expects
656 * analyzers responsible for data sending. 'channel_analyze' callback is
657 * resumable, so this function returns 0 if an error occurs or if it needs to
658 * wait, any other value otherwise.
659 */
660int
661flt_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
662{
663 int ret = 1;
664
Christopher Fauletd7c91962015-04-30 11:48:27 +0200665 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100666 if (FLT_OPS(filter)->channel_analyze) {
667 ret = FLT_OPS(filter)->channel_analyze(s, filter, chn, an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200668 if (ret <= 0)
669 BREAK_EXECUTION(s, chn, check_result);
670 }
671 } RESUME_FILTER_END;
672
673 check_result:
Christopher Faulet309c6412015-12-02 09:57:32 +0100674 return handle_analyzer_result(s, chn, 0, ret);
675}
676
677/*
678 * This function do the same that the previsous one, but for the
679 * AN_FLT_HTTP_HDRS analyzer. The difference is what is done when all filters
680 * have been called. Returns 0 if an error occurs or if it needs to wait, any
681 * other value otherwise.
682 */
683int
684flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit)
685{
Christopher Fauletda02e172015-12-04 09:25:05 +0100686 struct filter *filter;
687 int ret = 1;
Christopher Faulet309c6412015-12-02 09:57:32 +0100688
689 RESUME_FILTER_LOOP(s, chn) {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100690 if (FLT_OPS(filter)->channel_analyze) {
691 ret = FLT_OPS(filter)->channel_analyze(s, filter, chn, an_bit);
Christopher Faulet309c6412015-12-02 09:57:32 +0100692 if (ret <= 0)
693 BREAK_EXECUTION(s, chn, check_result);
694 }
695 } RESUME_FILTER_END;
696
697 /* We increase next offset of all "data" filters after all processing on
698 * headers because any filter can alter them. So the definitive size of
699 * headers (msg->sov) is only known when all filters have been
700 * called. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100701 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100702 /* Handle "data" filters only */
703 if (!IS_DATA_FILTER(filter, chn))
704 continue;
705
706 FLT_NXT(filter, chn) = ((chn->flags & CF_ISRESP)
707 ? s->txn->rsp.sov : s->txn->req.sov);
Christopher Faulet309c6412015-12-02 09:57:32 +0100708 }
709
710 check_result:
711 return handle_analyzer_result(s, chn, an_bit, ret);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200712}
713
714/*
715 * Calls 'channel_end_analyze' callback for all filters attached to a
716 * stream. This function is called when we stop to analyze a request or a
717 * response. It is called after all other analyzers. 'channel_end_analyze'
718 * callback is resumable, so this function returns 0 if an error occurs or if it
719 * needs to wait, any other value otherwise.
720 */
721int
722flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit)
723{
724 int ret = 1;
725
Christopher Fauletd7c91962015-04-30 11:48:27 +0200726 RESUME_FILTER_LOOP(s, chn) {
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100727 FLT_NXT(filter, chn) = 0;
728 FLT_FWD(filter, chn) = 0;
Christopher Fauletda02e172015-12-04 09:25:05 +0100729 unregister_data_filter(s, chn, filter);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200730
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100731 if (FLT_OPS(filter)->channel_end_analyze) {
732 ret = FLT_OPS(filter)->channel_end_analyze(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200733 if (ret <= 0)
734 BREAK_EXECUTION(s, chn, end);
735 }
736 } RESUME_FILTER_END;
737
738end:
739 ret = handle_analyzer_result(s, chn, an_bit, ret);
Christopher Faulet02c7b222015-12-22 12:01:29 +0100740
741 /* Check if 'channel_end_analyze' callback has been called for the
742 * request and the response. */
743 if (!(s->req.analysers & AN_FLT_END) && !(s->res.analysers & AN_FLT_END)) {
Christopher Faulet02c7b222015-12-22 12:01:29 +0100744 /* When we are waiting for a new request, so we must reset
745 * stream analyzers. The input must not be closed the request
746 * channel, else it is useless to wait. */
747 if (s->txn && (s->txn->flags & TX_WAIT_NEXT_RQ) && !channel_input_closed(&s->req)) {
748 s->req.analysers = strm_li(s) ? strm_li(s)->analysers : 0;
749 s->res.analysers = 0;
750 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200751
Christopher Faulet92d36382015-11-05 13:35:03 +0100752 /* Remove backend filters from the list */
753 flt_stream_release(s, 1);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200754 }
755 else if (ret) {
756 /* Analyzer ends only for one channel. So wake up the stream to
757 * be sure to process it for the other side as soon as
758 * possible. */
759 task_wakeup(s->task, TASK_WOKEN_MSG);
760 }
761 return ret;
762}
763
764
765/*
766 * Calls 'tcp_data' callback for all "data" filters attached to a stream. This
767 * function is called when incoming data are available. It takes care to update
768 * the next offset of filters and adjusts available data to be sure that a
769 * filter cannot parse more data than its predecessors. A filter can choose to
770 * not consume all available data. Returns -1 if an error occurs, the number of
771 * consumed bytes otherwise.
772 */
773static int
774flt_data(struct stream *s, struct channel *chn)
775{
Christopher Fauletda02e172015-12-04 09:25:05 +0100776 struct filter *filter;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100777 struct buffer *buf = chn->buf;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200778 unsigned int buf_i;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100779 int ret = 0;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200780
781 /* Save buffer state */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100782 buf_i = buf->i;
783
784 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100785 unsigned int *nxt;
786
787 /* Call "data" filters only */
788 if (!IS_DATA_FILTER(filter, chn))
789 continue;
790
791 nxt = &FLT_NXT(filter, chn);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100792 if (FLT_OPS(filter)->tcp_data) {
793 ret = FLT_OPS(filter)->tcp_data(s, filter, chn);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200794 if (ret < 0)
795 break;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200796
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100797 /* Increase next offset of the current filter */
Christopher Fauletda02e172015-12-04 09:25:05 +0100798 *nxt += ret;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100799
800 /* And set this value as the bound for the next
801 * filter. It will not able to parse more data than the
802 * current one. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100803 buf->i = *nxt;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100804 }
805 else {
806 /* Consume all available data */
Christopher Fauletda02e172015-12-04 09:25:05 +0100807 *nxt = buf->i;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100808 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200809
810 /* Update <ret> value to be sure to have the last one when we
Christopher Fauletda02e172015-12-04 09:25:05 +0100811 * exit from the loop. This value will be used to know how much
812 * data are "forwardable" */
813 ret = *nxt;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200814 }
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100815
816 /* Restore the original buffer state */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200817 chn->buf->i = buf_i;
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100818
Christopher Fauletd7c91962015-04-30 11:48:27 +0200819 return ret;
820}
821
822/*
823 * Calls 'tcp_forward_data' callback for all "data" filters attached to a
824 * stream. This function is called when some data can be forwarded. It takes
825 * care to update the forward offset of filters and adjusts "forwardable" data
826 * to be sure that a filter cannot forward more data than its predecessors. A
827 * filter can choose to not forward all parsed data. Returns a negative value if
828 * an error occurs, else the number of forwarded bytes.
829 */
830static int
831flt_forward_data(struct stream *s, struct channel *chn, unsigned int len)
832{
Christopher Fauletda02e172015-12-04 09:25:05 +0100833 struct filter *filter;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200834 int ret = len;
835
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100836 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100837 unsigned int *fwd;
838
839 /* Call "data" filters only */
840 if (!IS_DATA_FILTER(filter, chn))
841 continue;
842
843 fwd = &FLT_FWD(filter, chn);
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100844 if (FLT_OPS(filter)->tcp_forward_data) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200845 /* Remove bytes that the current filter considered as
846 * forwarded */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100847 ret = FLT_OPS(filter)->tcp_forward_data(s, filter, chn, ret - *fwd);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200848 if (ret < 0)
849 goto end;
850 }
851
Christopher Fauletda02e172015-12-04 09:25:05 +0100852 /* Adjust bytes that the current filter considers as
Christopher Fauletd7c91962015-04-30 11:48:27 +0200853 * forwarded */
Christopher Fauletda02e172015-12-04 09:25:05 +0100854 *fwd += ret;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200855
856 /* And set this value as the bound for the next filter. It will
857 * not able to forward more data than the current one. */
Christopher Fauletda02e172015-12-04 09:25:05 +0100858 ret = *fwd;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200859 }
860
861 if (!ret)
862 goto end;
863
Christopher Fauletda02e172015-12-04 09:25:05 +0100864 /* Finally, adjust filters offsets by removing data that HAProxy will
865 * forward. */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100866 list_for_each_entry(filter, &strm_flt(s)->filters, list) {
Christopher Fauletda02e172015-12-04 09:25:05 +0100867 if (!IS_DATA_FILTER(filter, chn))
868 continue;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200869 FLT_NXT(filter, chn) -= ret;
870 FLT_FWD(filter, chn) -= ret;
871 }
872
Christopher Fauletd7c91962015-04-30 11:48:27 +0200873 end:
874 return ret;
875}
876
877/*
878 * Called when TCP data must be filtered on a channel. This function is the
879 * AN_FLT_XFER_DATA analyzer. When called, it is responsible to forward data
880 * when the proxy is not in http mode. Behind the scene, it calls consecutively
881 * 'tcp_data' and 'tcp_forward_data' callbacks for all "data" filters attached
882 * to a stream. Returns 0 if an error occurs or if it needs to wait, any other
883 * value otherwise.
884 */
885int
886flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit)
887{
888 int ret = 1;
889
Christopher Fauletda02e172015-12-04 09:25:05 +0100890 /* If there is no "data" filters, we do nothing */
891 if (!HAS_DATA_FILTERS(s, chn))
892 goto end;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200893
894 /* Be sure that the output is still opened. Else we stop the data
895 * filtering. */
896 if ((chn->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
897 ((chn->flags & CF_SHUTW) && (chn->to_forward || chn->buf->o)))
898 goto end;
899
900 /* Let all "data" filters parsing incoming data */
901 ret = flt_data(s, chn);
902 if (ret < 0)
903 goto end;
904
905 /* And forward them */
906 ret = flt_forward_data(s, chn, ret);
907 if (ret < 0)
908 goto end;
909
Christopher Fauletda02e172015-12-04 09:25:05 +0100910 /* Consume data that all filters consider as forwarded. */
911 b_adv(chn->buf, ret);
912
Christopher Fauletd7c91962015-04-30 11:48:27 +0200913 /* Stop waiting data if the input in closed and no data is pending or if
914 * the output is closed. */
915 if ((chn->flags & CF_SHUTW) ||
916 ((chn->flags & CF_SHUTR) && !buffer_pending(chn->buf))) {
917 ret = 1;
918 goto end;
919 }
920
921 /* Wait for data */
922 return 0;
923 end:
924 /* Terminate the data filtering. If <ret> is negative, an error was
925 * encountered during the filtering. */
926 return handle_analyzer_result(s, chn, an_bit, ret);
927}
928
929/*
930 * Handles result of filter's analyzers. It returns 0 if an error occurs or if
931 * it needs to wait, any other value otherwise.
932 */
933static int
934handle_analyzer_result(struct stream *s, struct channel *chn,
935 unsigned int an_bit, int ret)
936{
937 int finst;
938
939 if (ret < 0)
940 goto return_bad_req;
941 else if (!ret)
942 goto wait;
943
944 /* End of job, return OK */
945 if (an_bit) {
946 chn->analysers &= ~an_bit;
947 chn->analyse_exp = TICK_ETERNITY;
948 }
949 return 1;
950
951 return_bad_req:
952 /* An error occurs */
953 channel_abort(&s->req);
954 channel_abort(&s->res);
955
956 if (!(chn->flags & CF_ISRESP)) {
957 s->req.analysers &= AN_FLT_END;
958 finst = SF_FINST_R;
959 /* FIXME: incr counters */
960 }
961 else {
962 s->res.analysers &= AN_FLT_END;
963 finst = SF_FINST_H;
964 /* FIXME: incr counters */
965 }
966
967 if (s->txn) {
968 /* Do not do that when we are waiting for the next request */
969 if (s->txn->status)
970 http_reply_and_close(s, s->txn->status, NULL);
971 else {
972 s->txn->status = 400;
973 http_reply_and_close(s, 400, http_error_message(s, HTTP_ERR_400));
974 }
975 }
976
977 if (!(s->flags & SF_ERR_MASK))
978 s->flags |= SF_ERR_PRXCOND;
979 if (!(s->flags & SF_FINST_MASK))
980 s->flags |= finst;
981 return 0;
982
983 wait:
984 if (!(chn->flags & CF_ISRESP))
985 channel_dont_connect(chn);
986 return 0;
987}
988
989
990/* Note: must not be declared <const> as its list will be overwritten.
991 * Please take care of keeping this list alphabetically sorted, doing so helps
992 * all code contributors.
993 * Optional keywords are also declared with a NULL ->parse() function so that
994 * the config parser can report an appropriate error when a known keyword was
995 * not enabled. */
996static struct cfg_kw_list cfg_kws = {ILH, {
997 { CFG_LISTEN, "filter", parse_filter },
998 { 0, NULL, NULL },
999 }
1000};
1001
1002__attribute__((constructor))
1003static void
1004__filters_init(void)
1005{
1006 pool2_filter = create_pool("filter", sizeof(struct filter), MEM_F_SHARED);
1007 cfg_register_keywords(&cfg_kws);
1008}
1009
1010__attribute__((destructor))
1011static void
1012__filters_deinit(void)
1013{
1014 pool_destroy2(pool2_filter);
1015}
1016
1017/*
1018 * Local variables:
1019 * c-indent-level: 8
1020 * c-basic-offset: 8
1021 * End:
1022 */