Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 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 | |
Christopher Faulet | f4a4ef7 | 2018-12-07 17:39:53 +0100 | [diff] [blame] | 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 | |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 37 | #define FLT_ID(flt) (flt)->config->id |
| 38 | #define FLT_CONF(flt) (flt)->config->conf |
| 39 | #define FLT_OPS(flt) (flt)->config->ops |
| 40 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 41 | /* Useful macros to access per-channel values. It can be safely used inside |
| 42 | * filters. */ |
| 43 | #define CHN_IDX(chn) (((chn)->flags & CF_ISRESP) == CF_ISRESP) |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 44 | #define FLT_STRM_OFF(s, chn) (strm_flt(s)->offset[CHN_IDX(chn)]) |
| 45 | #define FLT_OFF(flt, chn) ((flt)->offset[CHN_IDX(chn)]) |
| 46 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 47 | #define FLT_NXT(flt, chn) ((flt)->next[CHN_IDX(chn)]) |
| 48 | #define FLT_FWD(flt, chn) ((flt)->fwd[CHN_IDX(chn)]) |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 49 | #define flt_req_nxt(flt) ((flt)->next[0]) |
| 50 | #define flt_rsp_nxt(flt) ((flt)->next[1]) |
| 51 | #define flt_req_fwd(flt) ((flt)->fwd[0]) |
| 52 | #define flt_rsp_fwd(flt) ((flt)->fwd[1]) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 53 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 54 | #define HAS_FILTERS(strm) ((strm)->strm_flt.flags & STRM_FLT_FL_HAS_FILTERS) |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 55 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 56 | #define HAS_REQ_DATA_FILTERS(strm) ((strm)->strm_flt.nb_req_data_filters != 0) |
| 57 | #define HAS_RSP_DATA_FILTERS(strm) ((strm)->strm_flt.nb_rsp_data_filters != 0) |
Christopher Faulet | 4aad833 | 2016-11-28 10:01:32 +0100 | [diff] [blame] | 58 | #define HAS_DATA_FILTERS(strm, chn) (((chn)->flags & CF_ISRESP) ? HAS_RSP_DATA_FILTERS(strm) : HAS_REQ_DATA_FILTERS(strm)) |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 59 | |
| 60 | #define IS_REQ_DATA_FILTER(flt) ((flt)->flags & FLT_FL_IS_REQ_DATA_FILTER) |
| 61 | #define IS_RSP_DATA_FILTER(flt) ((flt)->flags & FLT_FL_IS_RSP_DATA_FILTER) |
Christopher Faulet | 4aad833 | 2016-11-28 10:01:32 +0100 | [diff] [blame] | 62 | #define IS_DATA_FILTER(flt, chn) (((chn)->flags & CF_ISRESP) ? IS_RSP_DATA_FILTER(flt) : IS_REQ_DATA_FILTER(flt)) |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 63 | |
| 64 | #define FLT_STRM_CB(strm, call) \ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 65 | do { \ |
| 66 | if (HAS_FILTERS(strm)) { call; } \ |
| 67 | } while (0) |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 68 | |
| 69 | #define FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, default_ret) \ |
| 70 | (HAS_DATA_FILTERS(strm, chn) ? call : default_ret) |
| 71 | #define FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, default_ret, on_error) \ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 72 | ({ \ |
| 73 | int _ret; \ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 74 | if (HAS_DATA_FILTERS(strm, chn)) { \ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 75 | _ret = call; \ |
| 76 | if (_ret < 0) { on_error; } \ |
| 77 | } \ |
| 78 | else \ |
| 79 | _ret = default_ret; \ |
| 80 | _ret; \ |
| 81 | }) |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 82 | #define FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, default_ret, on_error, on_wait) \ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 83 | ({ \ |
| 84 | int _ret; \ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 85 | if (HAS_DATA_FILTERS(strm, chn)) { \ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 86 | _ret = call; \ |
| 87 | if (_ret < 0) { on_error; } \ |
| 88 | if (!_ret) { on_wait; } \ |
| 89 | } \ |
| 90 | else \ |
| 91 | _ret = default_ret; \ |
| 92 | _ret; \ |
| 93 | }) |
| 94 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 95 | #define FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, A, B, C, DATA_CB_IMPL, ...) \ |
| 96 | DATA_CB_IMPL |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 97 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 98 | #define FLT_STRM_DATA_CB(strm, chn, call, ...) \ |
| 99 | FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, ##__VA_ARGS__, \ |
| 100 | FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, ##__VA_ARGS__), \ |
| 101 | FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, ##__VA_ARGS__), \ |
| 102 | FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, ##__VA_ARGS__)) |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 103 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 104 | void flt_deinit(struct proxy *p); |
| 105 | int flt_check(struct proxy *p); |
| 106 | |
| 107 | int flt_stream_start(struct stream *s); |
| 108 | void flt_stream_stop(struct stream *s); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 109 | int flt_set_stream_backend(struct stream *s, struct proxy *be); |
| 110 | int flt_stream_init(struct stream *s); |
| 111 | void flt_stream_release(struct stream *s, int only_backend); |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 112 | void flt_stream_check_timeouts(struct stream *s); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 113 | |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 114 | int flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len); |
| 115 | int flt_http_end(struct stream *s, struct http_msg *msg); |
| 116 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 117 | int flt_http_data(struct stream *s, struct http_msg *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 118 | int flt_http_chunk_trailers(struct stream *s, struct http_msg *msg); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 119 | int flt_http_forward_data(struct stream *s, struct http_msg *msg, unsigned int len); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 120 | |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 121 | void flt_http_reset(struct stream *s, struct http_msg *msg); |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 122 | void flt_http_reply(struct stream *s, short status, const struct buffer *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 123 | |
| 124 | int flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit); |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 125 | int flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit); |
| 126 | int flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 127 | int flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 128 | int flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit); |
| 129 | |
| 130 | int flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit); |
| 131 | |
| 132 | void flt_register_keywords(struct flt_kw_list *kwl); |
| 133 | struct flt_kw *flt_find_kw(const char *kw); |
| 134 | void flt_dump_kws(char **out); |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 135 | void list_filters(FILE *out); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 136 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 137 | /* Helper function that returns the "global" state of filters attached to a |
| 138 | * stream. */ |
| 139 | static inline struct strm_flt * |
| 140 | strm_flt(struct stream *s) |
| 141 | { |
| 142 | return &s->strm_flt; |
| 143 | } |
| 144 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 145 | /* Registers a filter to a channel. If a filter was already registered, this |
| 146 | * function do nothing. Once registered, the filter becomes a "data" filter for |
| 147 | * this channel. */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 148 | static inline void |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 149 | register_data_filter(struct stream *s, struct channel *chn, struct filter *filter) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 150 | { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 151 | if (!IS_DATA_FILTER(filter, chn)) { |
| 152 | if (chn->flags & CF_ISRESP) { |
| 153 | filter->flags |= FLT_FL_IS_RSP_DATA_FILTER; |
| 154 | strm_flt(s)->nb_rsp_data_filters++; |
| 155 | } |
| 156 | else { |
| 157 | filter->flags |= FLT_FL_IS_REQ_DATA_FILTER; |
| 158 | strm_flt(s)->nb_req_data_filters++; |
| 159 | } |
| 160 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 163 | /* Unregisters a "data" filter from a channel. */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 164 | static inline void |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 165 | unregister_data_filter(struct stream *s, struct channel *chn, struct filter *filter) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 166 | { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 167 | if (IS_DATA_FILTER(filter, chn)) { |
| 168 | if (chn->flags & CF_ISRESP) { |
| 169 | filter->flags &= ~FLT_FL_IS_RSP_DATA_FILTER; |
| 170 | strm_flt(s)->nb_rsp_data_filters--; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 171 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 172 | } |
| 173 | else { |
| 174 | filter->flags &= ~FLT_FL_IS_REQ_DATA_FILTER; |
| 175 | strm_flt(s)->nb_req_data_filters--; |
| 176 | } |
| 177 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 180 | /* This function must be called when a filter alter incoming data. It updates |
| 181 | * next offset value of all filter's predecessors. Do not call this function |
| 182 | * when a filter change the size of incomding data leads to an undefined |
| 183 | * behavior. |
| 184 | * |
| 185 | * This is the filter's responsiblitiy to update data itself. For now, it is |
| 186 | * unclear to know how to handle data updates, so we do the minimum here. For |
| 187 | * example, if you filter an HTTP message, we must update msg->next and |
| 188 | * msg->chunk_len values. |
| 189 | */ |
| 190 | static inline void |
| 191 | flt_change_next_size(struct filter *filter, struct channel *chn, int len) |
| 192 | { |
| 193 | struct stream *s = chn_strm(chn); |
| 194 | struct filter *f; |
| 195 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 196 | list_for_each_entry(f, &strm_flt(s)->filters, list) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 197 | if (f == filter) |
| 198 | break; |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 199 | if (IS_DATA_FILTER(filter, chn)) |
| 200 | FLT_NXT(f, chn) += len; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | /* This function must be called when a filter alter forwarded data. It updates |
| 205 | * offset values (next and forward) of all filters. Do not call this function |
| 206 | * when a filter change the size of forwarded data leads to an undefined |
| 207 | * behavior. |
| 208 | * |
| 209 | * This is the filter's responsiblitiy to update data itself. For now, it is |
| 210 | * unclear to know how to handle data updates, so we do the minimum here. For |
| 211 | * example, if you filter an HTTP message, we must update msg->next and |
| 212 | * msg->chunk_len values. |
| 213 | */ |
| 214 | static inline void |
| 215 | flt_change_forward_size(struct filter *filter, struct channel *chn, int len) |
| 216 | { |
| 217 | struct stream *s = chn_strm(chn); |
| 218 | struct filter *f; |
| 219 | int before = 1; |
| 220 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 221 | list_for_each_entry(f, &strm_flt(s)->filters, list) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 222 | if (f == filter) |
| 223 | before = 0; |
Christopher Faulet | 3e7bc67 | 2015-12-07 13:39:08 +0100 | [diff] [blame] | 224 | if (IS_DATA_FILTER(filter, chn)) { |
| 225 | if (before) |
| 226 | FLT_FWD(f, chn) += len; |
| 227 | FLT_NXT(f, chn) += len; |
| 228 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 232 | /* This function must be called when a filter alter payload data. It updates |
| 233 | * offsets of all previous filters and the offset of the stream. Do not call |
| 234 | * this function when a filter change the size of payload data leads to an |
| 235 | * undefined behavior. |
| 236 | * |
| 237 | * This is the filter's responsiblitiy to update data itself.. |
| 238 | */ |
| 239 | static inline void |
| 240 | flt_update_offsets(struct filter *filter, struct channel *chn, int len) |
| 241 | { |
| 242 | struct stream *s = chn_strm(chn); |
| 243 | struct filter *f; |
| 244 | |
| 245 | list_for_each_entry(f, &strm_flt(s)->filters, list) { |
| 246 | if (f == filter) |
| 247 | break; |
| 248 | if (IS_DATA_FILTER(filter, chn)) |
| 249 | FLT_OFF(f, chn) += len; |
| 250 | } |
| 251 | } |
| 252 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 253 | |
| 254 | #endif /* _PROTO_FILTERS_H */ |