blob: 346d8a012fdaae29205a1b90bd4ea5dfde83a5cb [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
Christopher Faulet443ea1a2016-02-04 13:40:26 +010032#define FLT_ID(flt) (flt)->config->id
33#define FLT_CONF(flt) (flt)->config->conf
34#define FLT_OPS(flt) (flt)->config->ops
35
Christopher Fauletd7c91962015-04-30 11:48:27 +020036/* Useful macros to access per-channel values. It can be safely used inside
37 * filters. */
38#define CHN_IDX(chn) (((chn)->flags & CF_ISRESP) == CF_ISRESP)
Christopher Faulet75bc9132018-11-30 15:18:09 +010039#define FLT_STRM_OFF(s, chn) (strm_flt(s)->offset[CHN_IDX(chn)])
40#define FLT_OFF(flt, chn) ((flt)->offset[CHN_IDX(chn)])
41
Christopher Fauletd7c91962015-04-30 11:48:27 +020042#define FLT_NXT(flt, chn) ((flt)->next[CHN_IDX(chn)])
43#define FLT_FWD(flt, chn) ((flt)->fwd[CHN_IDX(chn)])
Christopher Faulet3e7bc672015-12-07 13:39:08 +010044#define flt_req_nxt(flt) ((flt)->next[0])
45#define flt_rsp_nxt(flt) ((flt)->next[1])
46#define flt_req_fwd(flt) ((flt)->fwd[0])
47#define flt_rsp_fwd(flt) ((flt)->fwd[1])
Christopher Fauletd7c91962015-04-30 11:48:27 +020048
Christopher Fauletda02e172015-12-04 09:25:05 +010049#define HAS_FILTERS(strm) ((strm)->strm_flt.flags & STRM_FLT_FL_HAS_FILTERS)
Christopher Faulet3e344292015-11-24 16:24:13 +010050
Christopher Fauletda02e172015-12-04 09:25:05 +010051#define HAS_REQ_DATA_FILTERS(strm) ((strm)->strm_flt.nb_req_data_filters != 0)
52#define HAS_RSP_DATA_FILTERS(strm) ((strm)->strm_flt.nb_rsp_data_filters != 0)
Christopher Faulet4aad8332016-11-28 10:01:32 +010053#define HAS_DATA_FILTERS(strm, chn) (((chn)->flags & CF_ISRESP) ? HAS_RSP_DATA_FILTERS(strm) : HAS_REQ_DATA_FILTERS(strm))
Christopher Fauletda02e172015-12-04 09:25:05 +010054
55#define IS_REQ_DATA_FILTER(flt) ((flt)->flags & FLT_FL_IS_REQ_DATA_FILTER)
56#define IS_RSP_DATA_FILTER(flt) ((flt)->flags & FLT_FL_IS_RSP_DATA_FILTER)
Christopher Faulet4aad8332016-11-28 10:01:32 +010057#define IS_DATA_FILTER(flt, chn) (((chn)->flags & CF_ISRESP) ? IS_RSP_DATA_FILTER(flt) : IS_REQ_DATA_FILTER(flt))
Christopher Fauletda02e172015-12-04 09:25:05 +010058
59#define FLT_STRM_CB(strm, call) \
Christopher Faulet3e344292015-11-24 16:24:13 +010060 do { \
61 if (HAS_FILTERS(strm)) { call; } \
62 } while (0)
Christopher Fauletda02e172015-12-04 09:25:05 +010063
64#define FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, default_ret) \
65 (HAS_DATA_FILTERS(strm, chn) ? call : default_ret)
66#define FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, default_ret, on_error) \
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 } \
73 else \
74 _ret = default_ret; \
75 _ret; \
76 })
Christopher Fauletda02e172015-12-04 09:25:05 +010077#define FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, default_ret, on_error, on_wait) \
Christopher Faulet3e344292015-11-24 16:24:13 +010078 ({ \
79 int _ret; \
Christopher Fauletda02e172015-12-04 09:25:05 +010080 if (HAS_DATA_FILTERS(strm, chn)) { \
Christopher Faulet3e344292015-11-24 16:24:13 +010081 _ret = call; \
82 if (_ret < 0) { on_error; } \
83 if (!_ret) { on_wait; } \
84 } \
85 else \
86 _ret = default_ret; \
87 _ret; \
88 })
89
Christopher Fauletda02e172015-12-04 09:25:05 +010090#define FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, A, B, C, DATA_CB_IMPL, ...) \
91 DATA_CB_IMPL
Christopher Faulet3e344292015-11-24 16:24:13 +010092
Christopher Fauletda02e172015-12-04 09:25:05 +010093#define FLT_STRM_DATA_CB(strm, chn, call, ...) \
94 FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, ##__VA_ARGS__, \
95 FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, ##__VA_ARGS__), \
96 FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, ##__VA_ARGS__), \
97 FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, ##__VA_ARGS__))
Christopher Faulet3e344292015-11-24 16:24:13 +010098
Christopher Fauletd7c91962015-04-30 11:48:27 +020099void flt_deinit(struct proxy *p);
100int flt_check(struct proxy *p);
101
102int flt_stream_start(struct stream *s);
103void flt_stream_stop(struct stream *s);
Christopher Faulet92d36382015-11-05 13:35:03 +0100104int flt_set_stream_backend(struct stream *s, struct proxy *be);
105int flt_stream_init(struct stream *s);
106void flt_stream_release(struct stream *s, int only_backend);
Christopher Fauleta00d8172016-11-10 14:58:05 +0100107void flt_stream_check_timeouts(struct stream *s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200108
Christopher Faulet75bc9132018-11-30 15:18:09 +0100109int flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len);
110int flt_http_end(struct stream *s, struct http_msg *msg);
111
Christopher Fauletd7c91962015-04-30 11:48:27 +0200112int flt_http_data(struct stream *s, struct http_msg *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200113int flt_http_chunk_trailers(struct stream *s, struct http_msg *msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100114int flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200115
Christopher Faulet309c6412015-12-02 09:57:32 +0100116void flt_http_reset(struct stream *s, struct http_msg *msg);
Willy Tarreau83061a82018-07-13 11:56:34 +0200117void flt_http_reply(struct stream *s, short status, const struct buffer *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200118
119int flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200120int flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
121int flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
Christopher Faulet309c6412015-12-02 09:57:32 +0100122int flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200123int flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
124
125int flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit);
126
127void flt_register_keywords(struct flt_kw_list *kwl);
128struct flt_kw *flt_find_kw(const char *kw);
129void flt_dump_kws(char **out);
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100130void list_filters(FILE *out);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200131
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100132/* Helper function that returns the "global" state of filters attached to a
133 * stream. */
134static inline struct strm_flt *
135strm_flt(struct stream *s)
136{
137 return &s->strm_flt;
138}
139
Christopher Fauletda02e172015-12-04 09:25:05 +0100140/* Registers a filter to a channel. If a filter was already registered, this
141 * function do nothing. Once registered, the filter becomes a "data" filter for
142 * this channel. */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200143static inline void
Christopher Fauletda02e172015-12-04 09:25:05 +0100144register_data_filter(struct stream *s, struct channel *chn, struct filter *filter)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200145{
Christopher Fauletda02e172015-12-04 09:25:05 +0100146 if (!IS_DATA_FILTER(filter, chn)) {
147 if (chn->flags & CF_ISRESP) {
148 filter->flags |= FLT_FL_IS_RSP_DATA_FILTER;
149 strm_flt(s)->nb_rsp_data_filters++;
150 }
151 else {
152 filter->flags |= FLT_FL_IS_REQ_DATA_FILTER;
153 strm_flt(s)->nb_req_data_filters++;
154 }
155 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200156}
157
Christopher Fauletda02e172015-12-04 09:25:05 +0100158/* Unregisters a "data" filter from a channel. */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200159static inline void
Christopher Fauletda02e172015-12-04 09:25:05 +0100160unregister_data_filter(struct stream *s, struct channel *chn, struct filter *filter)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200161{
Christopher Fauletda02e172015-12-04 09:25:05 +0100162 if (IS_DATA_FILTER(filter, chn)) {
163 if (chn->flags & CF_ISRESP) {
164 filter->flags &= ~FLT_FL_IS_RSP_DATA_FILTER;
165 strm_flt(s)->nb_rsp_data_filters--;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200166
Christopher Fauletda02e172015-12-04 09:25:05 +0100167 }
168 else {
169 filter->flags &= ~FLT_FL_IS_REQ_DATA_FILTER;
170 strm_flt(s)->nb_req_data_filters--;
171 }
172 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200173}
174
Christopher Fauletd7c91962015-04-30 11:48:27 +0200175/* This function must be called when a filter alter incoming data. It updates
176 * next offset value of all filter's predecessors. Do not call this function
177 * when a filter change the size of incomding data leads to an undefined
178 * behavior.
179 *
180 * This is the filter's responsiblitiy to update data itself. For now, it is
181 * unclear to know how to handle data updates, so we do the minimum here. For
182 * example, if you filter an HTTP message, we must update msg->next and
183 * msg->chunk_len values.
184 */
185static inline void
186flt_change_next_size(struct filter *filter, struct channel *chn, int len)
187{
188 struct stream *s = chn_strm(chn);
189 struct filter *f;
190
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100191 list_for_each_entry(f, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200192 if (f == filter)
193 break;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100194 if (IS_DATA_FILTER(filter, chn))
195 FLT_NXT(f, chn) += len;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200196 }
197}
198
199/* This function must be called when a filter alter forwarded data. It updates
200 * offset values (next and forward) of all filters. Do not call this function
201 * when a filter change the size of forwarded data leads to an undefined
202 * behavior.
203 *
204 * This is the filter's responsiblitiy to update data itself. For now, it is
205 * unclear to know how to handle data updates, so we do the minimum here. For
206 * example, if you filter an HTTP message, we must update msg->next and
207 * msg->chunk_len values.
208 */
209static inline void
210flt_change_forward_size(struct filter *filter, struct channel *chn, int len)
211{
212 struct stream *s = chn_strm(chn);
213 struct filter *f;
214 int before = 1;
215
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100216 list_for_each_entry(f, &strm_flt(s)->filters, list) {
Christopher Fauletd7c91962015-04-30 11:48:27 +0200217 if (f == filter)
218 before = 0;
Christopher Faulet3e7bc672015-12-07 13:39:08 +0100219 if (IS_DATA_FILTER(filter, chn)) {
220 if (before)
221 FLT_FWD(f, chn) += len;
222 FLT_NXT(f, chn) += len;
223 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200224 }
225}
226
Christopher Faulet75bc9132018-11-30 15:18:09 +0100227/* This function must be called when a filter alter payload data. It updates
228 * offsets of all previous filters and the offset of the stream. Do not call
229 * this function when a filter change the size of payload data leads to an
230 * undefined behavior.
231 *
232 * This is the filter's responsiblitiy to update data itself..
233 */
234static inline void
235flt_update_offsets(struct filter *filter, struct channel *chn, int len)
236{
237 struct stream *s = chn_strm(chn);
238 struct filter *f;
239
240 list_for_each_entry(f, &strm_flt(s)->filters, list) {
241 if (f == filter)
242 break;
243 if (IS_DATA_FILTER(filter, chn))
244 FLT_OFF(f, chn) += len;
245 }
246}
247
Christopher Fauletd7c91962015-04-30 11:48:27 +0200248
249#endif /* _PROTO_FILTERS_H */