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 | |
| 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 Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 38 | #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] | 39 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 40 | #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 Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 49 | do { \ |
| 50 | if (HAS_FILTERS(strm)) { call; } \ |
| 51 | } while (0) |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 52 | |
| 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 Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 56 | ({ \ |
| 57 | int _ret; \ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 58 | if (HAS_DATA_FILTERS(strm, chn)) { \ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 59 | _ret = call; \ |
| 60 | if (_ret < 0) { on_error; } \ |
| 61 | } \ |
| 62 | else \ |
| 63 | _ret = default_ret; \ |
| 64 | _ret; \ |
| 65 | }) |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 66 | #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] | 67 | ({ \ |
| 68 | int _ret; \ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 69 | if (HAS_DATA_FILTERS(strm, chn)) { \ |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 70 | _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 Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 79 | #define FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, A, B, C, DATA_CB_IMPL, ...) \ |
| 80 | DATA_CB_IMPL |
Christopher Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 81 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 82 | #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 Faulet | 3e34429 | 2015-11-24 16:24:13 +0100 | [diff] [blame] | 87 | |
| 88 | #define CALL_FILTER_ANALYZER(analyzer, strm, chn, bit) \ |
| 89 | if (!HAS_FILTERS(strm) || analyzer((strm), (chn), bit)) ; else break |
| 90 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 91 | extern struct pool_head *pool2_filter; |
| 92 | |
| 93 | int flt_init(struct proxy *p); |
| 94 | void flt_deinit(struct proxy *p); |
| 95 | int flt_check(struct proxy *p); |
| 96 | |
| 97 | int flt_stream_start(struct stream *s); |
| 98 | void flt_stream_stop(struct stream *s); |
Christopher Faulet | 92d3638 | 2015-11-05 13:35:03 +0100 | [diff] [blame] | 99 | int flt_set_stream_backend(struct stream *s, struct proxy *be); |
| 100 | int flt_stream_init(struct stream *s); |
| 101 | void flt_stream_release(struct stream *s, int only_backend); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 102 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 103 | int flt_http_data(struct stream *s, struct http_msg *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 104 | int flt_http_chunk_trailers(struct stream *s, struct http_msg *msg); |
| 105 | int flt_http_end(struct stream *s, struct http_msg *msg); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 106 | 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] | 107 | |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 108 | void flt_http_reset(struct stream *s, struct http_msg *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 109 | void flt_http_reply(struct stream *s, short status, const struct chunk *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 110 | |
| 111 | int flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit); |
| 112 | int flt_analyze(struct stream *s, struct channel *chn, unsigned int an_bit); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 113 | 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] | 114 | int flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit); |
| 115 | |
| 116 | int flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit); |
| 117 | |
| 118 | void flt_register_keywords(struct flt_kw_list *kwl); |
| 119 | struct flt_kw *flt_find_kw(const char *kw); |
| 120 | void flt_dump_kws(char **out); |
| 121 | |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 122 | /* Helper function that returns the "global" state of filters attached to a |
| 123 | * stream. */ |
| 124 | static inline struct strm_flt * |
| 125 | strm_flt(struct stream *s) |
| 126 | { |
| 127 | return &s->strm_flt; |
| 128 | } |
| 129 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 130 | /* 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 Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 133 | static inline void |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 134 | register_data_filter(struct stream *s, struct channel *chn, struct filter *filter) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 135 | { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 136 | 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 Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 148 | /* Unregisters a "data" filter from a channel. */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 149 | static inline void |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 150 | unregister_data_filter(struct stream *s, struct channel *chn, struct filter *filter) |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 151 | { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 152 | 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 Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 156 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame^] | 157 | } |
| 158 | else { |
| 159 | filter->flags &= ~FLT_FL_IS_REQ_DATA_FILTER; |
| 160 | strm_flt(s)->nb_req_data_filters--; |
| 161 | } |
| 162 | } |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 165 | /* 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 | */ |
| 175 | static inline void |
| 176 | flt_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 Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 181 | list_for_each_entry(f, &strm_flt(s)->filters, list) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 182 | 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 | */ |
| 198 | static inline void |
| 199 | flt_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 Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 205 | list_for_each_entry(f, &strm_flt(s)->filters, list) { |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 206 | 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 */ |