1 /*
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 #define FLT_ID(flt)   (flt)->config->id
33 #define FLT_CONF(flt) (flt)->config->conf
34 #define FLT_OPS(flt)  (flt)->config->ops
35 
36 /* 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)
39 #define FLT_NXT(flt, chn) ((flt)->next[CHN_IDX(chn)])
40 #define FLT_FWD(flt, chn) ((flt)->fwd[CHN_IDX(chn)])
41 #define flt_req_nxt(flt) ((flt)->next[0])
42 #define flt_rsp_nxt(flt) ((flt)->next[1])
43 #define flt_req_fwd(flt) ((flt)->fwd[0])
44 #define flt_rsp_fwd(flt) ((flt)->fwd[1])
45 
46 #define HAS_FILTERS(strm)           ((strm)->strm_flt.flags & STRM_FLT_FL_HAS_FILTERS)
47 
48 #define HAS_REQ_DATA_FILTERS(strm)  ((strm)->strm_flt.nb_req_data_filters != 0)
49 #define HAS_RSP_DATA_FILTERS(strm)  ((strm)->strm_flt.nb_rsp_data_filters != 0)
50 #define HAS_DATA_FILTERS(strm, chn) (((chn)->flags & CF_ISRESP) ? HAS_RSP_DATA_FILTERS(strm) : HAS_REQ_DATA_FILTERS(strm))
51 
52 #define IS_REQ_DATA_FILTER(flt)  ((flt)->flags & FLT_FL_IS_REQ_DATA_FILTER)
53 #define IS_RSP_DATA_FILTER(flt)  ((flt)->flags & FLT_FL_IS_RSP_DATA_FILTER)
54 #define IS_DATA_FILTER(flt, chn) (((chn)->flags & CF_ISRESP) ? IS_RSP_DATA_FILTER(flt) : IS_REQ_DATA_FILTER(flt))
55 
56 #define FLT_STRM_CB(strm, call)						\
57 	do {								\
58 		if (HAS_FILTERS(strm)) { call; }			\
59 	} while (0)
60 
61 #define FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, default_ret)	        \
62 	(HAS_DATA_FILTERS(strm, chn) ? call : default_ret)
63 #define FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, default_ret, on_error)	\
64 	({								\
65 		int _ret;						\
66 		if (HAS_DATA_FILTERS(strm, chn)) {			\
67 			_ret = call;					\
68 			if (_ret < 0) { on_error; }			\
69 		}							\
70 		else							\
71 			_ret = default_ret;				\
72 		_ret;							\
73 	})
74 #define FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, default_ret, on_error, on_wait) \
75 	({								\
76 		int _ret;						\
77 		if (HAS_DATA_FILTERS(strm, chn)) {			\
78 			_ret = call;					\
79 			if (_ret < 0) { on_error; }			\
80 			if (!_ret)    { on_wait;  }			\
81 		}							\
82 		else							\
83 			_ret = default_ret;				\
84 		_ret;							\
85 	})
86 
87 #define FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, A, B, C, DATA_CB_IMPL, ...) \
88 	DATA_CB_IMPL
89 
90 #define FLT_STRM_DATA_CB(strm, chn, call, ...)				\
91 	FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, ##__VA_ARGS__,		\
92 				FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, ##__VA_ARGS__), \
93 				FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, ##__VA_ARGS__), \
94 				FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, ##__VA_ARGS__))
95 
96 extern struct pool_head *pool2_filter;
97 
98 int  flt_init(struct proxy *p);
99 void flt_deinit(struct proxy *p);
100 int  flt_check(struct proxy *p);
101 
102 int  flt_stream_start(struct stream *s);
103 void flt_stream_stop(struct stream *s);
104 int  flt_set_stream_backend(struct stream *s, struct proxy *be);
105 int  flt_stream_init(struct stream *s);
106 void flt_stream_release(struct stream *s, int only_backend);
107 void flt_stream_check_timeouts(struct stream *s);
108 
109 int  flt_http_data(struct stream *s, struct http_msg *msg);
110 int  flt_http_chunk_trailers(struct stream *s, struct http_msg *msg);
111 int  flt_http_end(struct stream *s, struct http_msg *msg);
112 int  flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len);
113 
114 void flt_http_reset(struct stream *s, struct http_msg *msg);
115 void flt_http_reply(struct stream *s, short status, const struct chunk *msg);
116 
117 int  flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
118 int  flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
119 int  flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
120 int  flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit);
121 int  flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
122 
123 int  flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit);
124 
125 void           flt_register_keywords(struct flt_kw_list *kwl);
126 struct flt_kw *flt_find_kw(const char *kw);
127 void           flt_dump_kws(char **out);
128 void           list_filters(FILE *out);
129 
130 /* Helper function that returns the "global" state of filters attached to a
131  * stream. */
132 static inline struct strm_flt *
strm_flt(struct stream * s)133 strm_flt(struct stream *s)
134 {
135 	return &s->strm_flt;
136 }
137 
138 /* Registers a filter to a channel. If a filter was already registered, this
139  * function do nothing. Once registered, the filter becomes a "data" filter for
140  * this channel. */
141 static inline void
register_data_filter(struct stream * s,struct channel * chn,struct filter * filter)142 register_data_filter(struct stream *s, struct channel *chn, struct filter *filter)
143 {
144 	if (!IS_DATA_FILTER(filter, chn)) {
145 		if (chn->flags & CF_ISRESP) {
146 			filter->flags |= FLT_FL_IS_RSP_DATA_FILTER;
147 			strm_flt(s)->nb_rsp_data_filters++;
148 		}
149 		else  {
150 			filter->flags |= FLT_FL_IS_REQ_DATA_FILTER;
151 			strm_flt(s)->nb_req_data_filters++;
152 		}
153 	}
154 }
155 
156 /* Unregisters a "data" filter from a channel. */
157 static inline void
unregister_data_filter(struct stream * s,struct channel * chn,struct filter * filter)158 unregister_data_filter(struct stream *s, struct channel *chn, struct filter *filter)
159 {
160 	if (IS_DATA_FILTER(filter, chn)) {
161 		if (chn->flags & CF_ISRESP) {
162 			filter->flags &= ~FLT_FL_IS_RSP_DATA_FILTER;
163 			strm_flt(s)->nb_rsp_data_filters--;
164 
165 		}
166 		else  {
167 			filter->flags &= ~FLT_FL_IS_REQ_DATA_FILTER;
168 			strm_flt(s)->nb_req_data_filters--;
169 		}
170 	}
171 }
172 
173 /* This function must be called when a filter alter incoming data. It updates
174  * next offset value of all filter's predecessors. Do not call this function
175  * when a filter change the size of incomding data leads to an undefined
176  * behavior.
177  *
178  * This is the filter's responsiblitiy to update data itself. For now, it is
179  * unclear to know how to handle data updates, so we do the minimum here. For
180  * example, if you filter an HTTP message, we must update msg->next and
181  * msg->chunk_len values.
182  */
183 static inline void
flt_change_next_size(struct filter * filter,struct channel * chn,int len)184 flt_change_next_size(struct filter *filter, struct channel *chn, int len)
185 {
186 	struct stream *s = chn_strm(chn);
187 	struct filter *f;
188 
189 	list_for_each_entry(f, &strm_flt(s)->filters, list) {
190 		if (f == filter)
191 			break;
192 		if (IS_DATA_FILTER(filter, chn))
193 			FLT_NXT(f, chn) += len;
194 	}
195 }
196 
197 /* This function must be called when a filter alter forwarded data. It updates
198  * offset values (next and forward) of all filters. Do not call this function
199  * when a filter change the size of forwarded data leads to an undefined
200  * behavior.
201  *
202  * This is the filter's responsiblitiy to update data itself. For now, it is
203  * unclear to know how to handle data updates, so we do the minimum here. For
204  * example, if you filter an HTTP message, we must update msg->next and
205  * msg->chunk_len values.
206  */
207 static inline void
flt_change_forward_size(struct filter * filter,struct channel * chn,int len)208 flt_change_forward_size(struct filter *filter, struct channel *chn, int len)
209 {
210 	struct stream *s = chn_strm(chn);
211 	struct filter *f;
212 	int before = 1;
213 
214 	list_for_each_entry(f, &strm_flt(s)->filters, list) {
215 		if (f == filter)
216 			before = 0;
217 		if (IS_DATA_FILTER(filter, chn)) {
218 			if (before)
219 				FLT_FWD(f, chn) += len;
220 			FLT_NXT(f, chn) += len;
221 		}
222 	}
223 }
224 
225 
226 #endif /* _PROTO_FILTERS_H */
227