blob: dd7490fc0085161c48b4991d6d76f4829ac558cc [file] [log] [blame]
Christopher Fauletd7c91962015-04-30 11:48:27 +02001/*
2 * include/proto/filters.h
3 * This file defines function prototypes for stream filters management.
4 *
5 * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21#ifndef _PROTO_FILTERS_H
22#define _PROTO_FILTERS_H
23
24#include <types/channel.h>
25#include <types/filters.h>
26#include <types/proto_http.h>
27#include <types/proxy.h>
28#include <types/stream.h>
29
30#include <proto/channel.h>
31
32/* Useful macros to access per-channel values. It can be safely used inside
33 * filters. */
34#define CHN_IDX(chn) (((chn)->flags & CF_ISRESP) == CF_ISRESP)
35#define FLT_NXT(flt, chn) ((flt)->next[CHN_IDX(chn)])
36#define FLT_FWD(flt, chn) ((flt)->fwd[CHN_IDX(chn)])
37
Christopher Fauletda02e172015-12-04 09:25:05 +010038#define HAS_FILTERS(strm) ((strm)->strm_flt.flags & STRM_FLT_FL_HAS_FILTERS)
Christopher Faulet3e344292015-11-24 16:24:13 +010039
Christopher Fauletda02e172015-12-04 09:25:05 +010040#define HAS_REQ_DATA_FILTERS(strm) ((strm)->strm_flt.nb_req_data_filters != 0)
41#define HAS_RSP_DATA_FILTERS(strm) ((strm)->strm_flt.nb_rsp_data_filters != 0)
42#define HAS_DATA_FILTERS(strm, chn) ((chn->flags & CF_ISRESP) ? HAS_RSP_DATA_FILTERS(strm) : HAS_REQ_DATA_FILTERS(strm))
43
44#define IS_REQ_DATA_FILTER(flt) ((flt)->flags & FLT_FL_IS_REQ_DATA_FILTER)
45#define IS_RSP_DATA_FILTER(flt) ((flt)->flags & FLT_FL_IS_RSP_DATA_FILTER)
46#define IS_DATA_FILTER(flt, chn) ((chn->flags & CF_ISRESP) ? IS_RSP_DATA_FILTER(flt) : IS_REQ_DATA_FILTER(flt))
47
48#define FLT_STRM_CB(strm, call) \
Christopher Faulet3e344292015-11-24 16:24:13 +010049 do { \
50 if (HAS_FILTERS(strm)) { call; } \
51 } while (0)
Christopher Fauletda02e172015-12-04 09:25:05 +010052
53#define FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, default_ret) \
54 (HAS_DATA_FILTERS(strm, chn) ? call : default_ret)
55#define FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, default_ret, on_error) \
Christopher Faulet3e344292015-11-24 16:24:13 +010056 ({ \
57 int _ret; \
Christopher Fauletda02e172015-12-04 09:25:05 +010058 if (HAS_DATA_FILTERS(strm, chn)) { \
Christopher Faulet3e344292015-11-24 16:24:13 +010059 _ret = call; \
60 if (_ret < 0) { on_error; } \
61 } \
62 else \
63 _ret = default_ret; \
64 _ret; \
65 })
Christopher Fauletda02e172015-12-04 09:25:05 +010066#define FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, default_ret, on_error, on_wait) \
Christopher Faulet3e344292015-11-24 16:24:13 +010067 ({ \
68 int _ret; \
Christopher Fauletda02e172015-12-04 09:25:05 +010069 if (HAS_DATA_FILTERS(strm, chn)) { \
Christopher Faulet3e344292015-11-24 16:24:13 +010070 _ret = call; \
71 if (_ret < 0) { on_error; } \
72 if (!_ret) { on_wait; } \
73 } \
74 else \
75 _ret = default_ret; \
76 _ret; \
77 })
78
Christopher Fauletda02e172015-12-04 09:25:05 +010079#define FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, A, B, C, DATA_CB_IMPL, ...) \
80 DATA_CB_IMPL
Christopher Faulet3e344292015-11-24 16:24:13 +010081
Christopher Fauletda02e172015-12-04 09:25:05 +010082#define FLT_STRM_DATA_CB(strm, chn, call, ...) \
83 FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, ##__VA_ARGS__, \
84 FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, ##__VA_ARGS__), \
85 FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, ##__VA_ARGS__), \
86 FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, ##__VA_ARGS__))
Christopher Faulet3e344292015-11-24 16:24:13 +010087
88#define CALL_FILTER_ANALYZER(analyzer, strm, chn, bit) \
89 if (!HAS_FILTERS(strm) || analyzer((strm), (chn), bit)) ; else break
90
Christopher Fauletd7c91962015-04-30 11:48:27 +020091extern struct pool_head *pool2_filter;
92
93int flt_init(struct proxy *p);
94void flt_deinit(struct proxy *p);
95int flt_check(struct proxy *p);
96
97int flt_stream_start(struct stream *s);
98void flt_stream_stop(struct stream *s);
Christopher Faulet92d36382015-11-05 13:35:03 +010099int flt_set_stream_backend(struct stream *s, struct proxy *be);
100int flt_stream_init(struct stream *s);
101void flt_stream_release(struct stream *s, int only_backend);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200102
Christopher Fauletd7c91962015-04-30 11:48:27 +0200103int flt_http_data(struct stream *s, struct http_msg *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200104int flt_http_chunk_trailers(struct stream *s, struct http_msg *msg);
105int flt_http_end(struct stream *s, struct http_msg *msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100106int flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200107
Christopher Faulet309c6412015-12-02 09:57:32 +0100108void flt_http_reset(struct stream *s, struct http_msg *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200109void flt_http_reply(struct stream *s, short status, const struct chunk *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200110
111int flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
112int flt_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
Christopher Faulet309c6412015-12-02 09:57:32 +0100113int flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200114int flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
115
116int flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit);
117
118void flt_register_keywords(struct flt_kw_list *kwl);
119struct flt_kw *flt_find_kw(const char *kw);
120void flt_dump_kws(char **out);
121
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100122/* Helper function that returns the "global" state of filters attached to a
123 * stream. */
124static inline struct strm_flt *
125strm_flt(struct stream *s)
126{
127 return &s->strm_flt;
128}
129
Christopher Fauletda02e172015-12-04 09:25:05 +0100130/* Registers a filter to a channel. If a filter was already registered, this
131 * function do nothing. Once registered, the filter becomes a "data" filter for
132 * this channel. */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200133static inline void
Christopher Fauletda02e172015-12-04 09:25:05 +0100134register_data_filter(struct stream *s, struct channel *chn, struct filter *filter)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200135{
Christopher Fauletda02e172015-12-04 09:25:05 +0100136 if (!IS_DATA_FILTER(filter, chn)) {
137 if (chn->flags & CF_ISRESP) {
138 filter->flags |= FLT_FL_IS_RSP_DATA_FILTER;
139 strm_flt(s)->nb_rsp_data_filters++;
140 }
141 else {
142 filter->flags |= FLT_FL_IS_REQ_DATA_FILTER;
143 strm_flt(s)->nb_req_data_filters++;
144 }
145 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200146}
147
Christopher Fauletda02e172015-12-04 09:25:05 +0100148/* Unregisters a "data" filter from a channel. */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200149static inline void
Christopher Fauletda02e172015-12-04 09:25:05 +0100150unregister_data_filter(struct stream *s, struct channel *chn, struct filter *filter)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200151{
Christopher Fauletda02e172015-12-04 09:25:05 +0100152 if (IS_DATA_FILTER(filter, chn)) {
153 if (chn->flags & CF_ISRESP) {
154 filter->flags &= ~FLT_FL_IS_RSP_DATA_FILTER;
155 strm_flt(s)->nb_rsp_data_filters--;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200156
Christopher Fauletda02e172015-12-04 09:25:05 +0100157 }
158 else {
159 filter->flags &= ~FLT_FL_IS_REQ_DATA_FILTER;
160 strm_flt(s)->nb_req_data_filters--;
161 }
162 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200163}
164
Christopher Fauletd7c91962015-04-30 11:48:27 +0200165/* This function must be called when a filter alter incoming data. It updates
166 * next offset value of all filter's predecessors. Do not call this function
167 * when a filter change the size of incomding data leads to an undefined
168 * behavior.
169 *
170 * This is the filter's responsiblitiy to update data itself. For now, it is
171 * unclear to know how to handle data updates, so we do the minimum here. For
172 * example, if you filter an HTTP message, we must update msg->next and
173 * msg->chunk_len values.
174 */
175static inline void
176flt_change_next_size(struct filter *filter, struct channel *chn, int len)
177{
178 struct stream *s = chn_strm(chn);
179 struct filter *f;
180
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100181 list_for_each_entry(f, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200182 if (f == filter)
183 break;
184 FLT_NXT(f, chn) += len;
185 }
186}
187
188/* This function must be called when a filter alter forwarded data. It updates
189 * offset values (next and forward) of all filters. Do not call this function
190 * when a filter change the size of forwarded data leads to an undefined
191 * behavior.
192 *
193 * This is the filter's responsiblitiy to update data itself. For now, it is
194 * unclear to know how to handle data updates, so we do the minimum here. For
195 * example, if you filter an HTTP message, we must update msg->next and
196 * msg->chunk_len values.
197 */
198static inline void
199flt_change_forward_size(struct filter *filter, struct channel *chn, int len)
200{
201 struct stream *s = chn_strm(chn);
202 struct filter *f;
203 int before = 1;
204
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100205 list_for_each_entry(f, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200206 if (f == filter)
207 before = 0;
208 if (before)
209 FLT_FWD(f, chn) += len;
210 FLT_NXT(f, chn) += len;
211 }
212}
213
214
215#endif /* _PROTO_FILTERS_H */