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/http_ana.h>
27 #include <types/proxy.h>
28 #include <types/stream.h>
29 
30 #include <proto/channel.h>
31 
32 extern const char *trace_flt_id;
33 extern const char *http_comp_flt_id;
34 extern const char *cache_store_flt_id;
35 extern const char *spoe_filter_id;
36 extern const char *fcgi_flt_id;
37 
38 #define FLT_ID(flt)   (flt)->config->id
39 #define FLT_CONF(flt) (flt)->config->conf
40 #define FLT_OPS(flt)  (flt)->config->ops
41 
42 /* Useful macros to access per-channel values. It can be safely used inside
43  * filters. */
44 #define CHN_IDX(chn)     (((chn)->flags & CF_ISRESP) == CF_ISRESP)
45 #define FLT_STRM_OFF(s, chn) (strm_flt(s)->offset[CHN_IDX(chn)])
46 #define FLT_OFF(flt, chn) ((flt)->offset[CHN_IDX(chn)])
47 
48 #define HAS_FILTERS(strm)           ((strm)->strm_flt.flags & STRM_FLT_FL_HAS_FILTERS)
49 
50 #define HAS_REQ_DATA_FILTERS(strm)  ((strm)->strm_flt.nb_req_data_filters != 0)
51 #define HAS_RSP_DATA_FILTERS(strm)  ((strm)->strm_flt.nb_rsp_data_filters != 0)
52 #define HAS_DATA_FILTERS(strm, chn) (((chn)->flags & CF_ISRESP) ? HAS_RSP_DATA_FILTERS(strm) : HAS_REQ_DATA_FILTERS(strm))
53 
54 #define IS_REQ_DATA_FILTER(flt)  ((flt)->flags & FLT_FL_IS_REQ_DATA_FILTER)
55 #define IS_RSP_DATA_FILTER(flt)  ((flt)->flags & FLT_FL_IS_RSP_DATA_FILTER)
56 #define IS_DATA_FILTER(flt, chn) (((chn)->flags & CF_ISRESP) ? IS_RSP_DATA_FILTER(flt) : IS_REQ_DATA_FILTER(flt))
57 
58 #define FLT_STRM_CB(strm, call)						\
59 	do {								\
60 		if (HAS_FILTERS(strm)) { call; }			\
61 	} while (0)
62 
63 #define FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, default_ret)	        \
64 	(HAS_DATA_FILTERS(strm, chn) ? call : default_ret)
65 #define FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, default_ret, on_error)	\
66 	({								\
67 		int _ret;						\
68 		if (HAS_DATA_FILTERS(strm, chn)) {			\
69 			_ret = call;					\
70 			if (_ret < 0) { on_error; }			\
71 		}							\
72 		else							\
73 			_ret = default_ret;				\
74 		_ret;							\
75 	})
76 #define FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, default_ret, on_error, on_wait) \
77 	({								\
78 		int _ret;						\
79 		if (HAS_DATA_FILTERS(strm, chn)) {			\
80 			_ret = call;					\
81 			if (_ret < 0) { on_error; }			\
82 			if (!_ret)    { on_wait;  }			\
83 		}							\
84 		else							\
85 			_ret = default_ret;				\
86 		_ret;							\
87 	})
88 
89 #define FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, A, B, C, DATA_CB_IMPL, ...) \
90 	DATA_CB_IMPL
91 
92 #define FLT_STRM_DATA_CB(strm, chn, call, ...)				\
93 	FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, ##__VA_ARGS__,		\
94 				FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, ##__VA_ARGS__), \
95 				FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, ##__VA_ARGS__), \
96 				FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, ##__VA_ARGS__))
97 
98 void flt_deinit(struct proxy *p);
99 int  flt_check(struct proxy *p);
100 
101 int  flt_stream_start(struct stream *s);
102 void flt_stream_stop(struct stream *s);
103 int  flt_set_stream_backend(struct stream *s, struct proxy *be);
104 int  flt_stream_init(struct stream *s);
105 void flt_stream_release(struct stream *s, int only_backend);
106 void flt_stream_check_timeouts(struct stream *s);
107 
108 int  flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len);
109 int  flt_http_end(struct stream *s, struct http_msg *msg);
110 
111 void flt_http_reset(struct stream *s, struct http_msg *msg);
112 void flt_http_reply(struct stream *s, short status, const struct buffer *msg);
113 
114 int  flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
115 int  flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
116 int  flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
117 int  flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit);
118 int  flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
119 
120 int  flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit);
121 
122 void           flt_register_keywords(struct flt_kw_list *kwl);
123 struct flt_kw *flt_find_kw(const char *kw);
124 void           flt_dump_kws(char **out);
125 void           list_filters(FILE *out);
126 
127 /* Helper function that returns the "global" state of filters attached to a
128  * stream. */
129 static inline struct strm_flt *
strm_flt(struct stream * s)130 strm_flt(struct stream *s)
131 {
132 	return &s->strm_flt;
133 }
134 
135 /* Registers a filter to a channel. If a filter was already registered, this
136  * function do nothing. Once registered, the filter becomes a "data" filter for
137  * this channel. */
138 static inline void
register_data_filter(struct stream * s,struct channel * chn,struct filter * filter)139 register_data_filter(struct stream *s, struct channel *chn, struct filter *filter)
140 {
141 	if (!IS_DATA_FILTER(filter, chn)) {
142 		if (chn->flags & CF_ISRESP) {
143 			filter->flags |= FLT_FL_IS_RSP_DATA_FILTER;
144 			strm_flt(s)->nb_rsp_data_filters++;
145 		}
146 		else  {
147 			filter->flags |= FLT_FL_IS_REQ_DATA_FILTER;
148 			strm_flt(s)->nb_req_data_filters++;
149 		}
150 	}
151 }
152 
153 /* Unregisters a "data" filter from a channel. */
154 static inline void
unregister_data_filter(struct stream * s,struct channel * chn,struct filter * filter)155 unregister_data_filter(struct stream *s, struct channel *chn, struct filter *filter)
156 {
157 	if (IS_DATA_FILTER(filter, chn)) {
158 		if (chn->flags & CF_ISRESP) {
159 			filter->flags &= ~FLT_FL_IS_RSP_DATA_FILTER;
160 			strm_flt(s)->nb_rsp_data_filters--;
161 
162 		}
163 		else  {
164 			filter->flags &= ~FLT_FL_IS_REQ_DATA_FILTER;
165 			strm_flt(s)->nb_req_data_filters--;
166 		}
167 	}
168 }
169 
170 /* This function must be called when a filter alter payload data. It updates
171  * offsets of all previous filters. Do not call this function when a filter
172  * change the size of payload data leads to an undefined behavior.
173  *
174  * This is the filter's responsiblitiy to update data itself.
175  */
176 static inline void
flt_update_offsets(struct filter * filter,struct channel * chn,int len)177 flt_update_offsets(struct filter *filter, struct channel *chn, int len)
178 {
179 	struct stream *s = chn_strm(chn);
180 	struct filter *f;
181 
182 	list_for_each_entry(f, &strm_flt(s)->filters, list) {
183 		if (f == filter)
184 			break;
185 		FLT_OFF(f, chn) += len;
186 	}
187 }
188 
189 
190 #endif /* _PROTO_FILTERS_H */
191