Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/types/filteers.h |
| 3 | * This file defines everything related to stream filters. |
| 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 _TYPES_FILTERS_H |
| 22 | #define _TYPES_FILTERS_H |
| 23 | |
| 24 | #include <common/config.h> |
| 25 | #include <common/mini-clist.h> |
| 26 | |
| 27 | struct http_msg; |
| 28 | struct proxy; |
| 29 | struct stream; |
| 30 | struct channel; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 31 | struct flt_conf; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 32 | struct filter; |
| 33 | |
| 34 | /* Descriptor for a "filter" keyword. The ->parse() function returns 0 in case |
| 35 | * of success, or a combination of ERR_* flags if an error is encountered. The |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 36 | * function pointer can be NULL if not implemented. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 37 | */ |
| 38 | struct flt_kw { |
| 39 | const char *kw; |
| 40 | int (*parse)(char **args, int *cur_arg, struct proxy *px, |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 41 | struct flt_conf *fconf, char **err, void *private); |
| 42 | void *private; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * A keyword list. It is a NULL-terminated array of keywords. It embeds a struct |
| 47 | * list in order to be linked to other lists, allowing it to easily be declared |
| 48 | * where it is needed, and linked without duplicating data nor allocating |
| 49 | * memory. It is also possible to indicate a scope for the keywords. |
| 50 | */ |
| 51 | struct flt_kw_list { |
| 52 | const char *scope; |
| 53 | struct list list; |
| 54 | struct flt_kw kw[VAR_ARRAY]; |
| 55 | }; |
| 56 | |
| 57 | /* |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 58 | * Callbacks available on a filter: |
| 59 | * |
| 60 | * - init : Initializes the filter for a proxy. Returns a |
| 61 | * negative value if an error occurs. |
| 62 | * - deinit : Cleans up what the init function has done. |
| 63 | * - check : Check the filter config for a proxy. Returns the |
| 64 | * number of errors encountered. |
Christopher Faulet | 71a6a8e | 2017-07-27 16:33:28 +0200 | [diff] [blame] | 65 | * - init_per_thread : Initializes the filter for a proxy for a specific |
| 66 | * thread. Returns a negative value if an error |
| 67 | * occurs. |
| 68 | * - deinit_per_thread : Cleans up what the init_per_thread funcion has |
| 69 | * done. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 70 | * |
| 71 | * |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 72 | * - attach : Called after a filter instance creation, when it is |
| 73 | * attached to a stream. This happens when the stream |
| 74 | * is started for filters defined on the stream's |
| 75 | * frontend and when the backend is set for filters |
| 76 | * declared on the stream's backend. |
| 77 | * Returns a negative value if an error occurs, 0 if |
| 78 | * the filter must be ignored for the stream, any other |
| 79 | * value otherwise. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 80 | * - stream_start : Called when a stream is started. This callback will |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 81 | * only be called for filters defined on the stream's |
| 82 | * frontend. |
| 83 | * Returns a negative value if an error occurs, any |
| 84 | * other value otherwise. |
| 85 | * - stream_set_backend : Called when a backend is set for a stream. This |
| 86 | * callbacks will be called for all filters attached |
| 87 | * to a stream (frontend and backend). |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 88 | * Returns a negative value if an error occurs, any |
| 89 | * other value otherwise. |
| 90 | * - stream_stop : Called when a stream is stopped. This callback will |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 91 | * only be called for filters defined on the stream's |
| 92 | * frontend. |
| 93 | * - detach : Called when a filter instance is detached from a |
| 94 | * stream, before its destruction. This happens when |
| 95 | * the stream is stopped for filters defined on the |
| 96 | * stream's frontend and when the analyze ends for |
| 97 | * filters defined on the stream's backend. |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 98 | * - check_timeouts : Called when a a stream is woken up because of an |
| 99 | * expired timer. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 100 | * |
| 101 | * |
| 102 | * - channel_start_analyze: Called when a filter starts to analyze a channel. |
| 103 | * Returns a negative value if an error occurs, 0 if |
| 104 | * it needs to wait, any other value otherwise. |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 105 | * - channel_pre_analyze : Called before each analyzer attached to a channel, |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 106 | * expects analyzers responsible for data sending. |
| 107 | * Returns a negative value if an error occurs, 0 if |
| 108 | * it needs to wait, any other value otherwise. |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 109 | * - channel_post_analyze: Called after each analyzer attached to a channel, |
| 110 | * expects analyzers responsible for data sending. |
| 111 | * Returns a negative value if an error occurs, |
| 112 | * any other value otherwise. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 113 | * - channel_end_analyze : Called when all other analyzers have finished their |
| 114 | * processing. |
| 115 | * Returns a negative value if an error occurs, 0 if |
| 116 | * it needs to wait, any other value otherwise. |
| 117 | * |
| 118 | * |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 119 | * - http_headers : Called before the body parsing, after all HTTP |
| 120 | * headers was parsed and analyzed. |
| 121 | * Returns a negative value if an error occurs, 0 if |
| 122 | * it needs to wait, any other value otherwise. |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 123 | * - http_payload : Called when some data can be consumed. |
| 124 | * Returns a negative value if an error occurs, else |
| 125 | * the number of forwarded bytes. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 126 | * - http_data : Called when unparsed body data are available. |
| 127 | * Returns a negative value if an error occurs, else |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 128 | * the number of consumed bytes. [DEPRECATED] |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 129 | * - http_chunk_trailers : Called when part of trailer headers of a |
| 130 | * chunk-encoded request/response are ready to be |
| 131 | * processed. |
| 132 | * Returns a negative value if an error occurs, any |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 133 | * other value otherwise. [DEPRECATED] |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 134 | * - http_end : Called when all the request/response has been |
| 135 | * processed and all body data has been forwarded. |
| 136 | * Returns a negative value if an error occurs, 0 if |
| 137 | * it needs to wait for some reason, any other value |
| 138 | * otherwise. |
| 139 | * - http_reset : Called when the HTTP message is reseted. It happens |
| 140 | * when a 100-continue response is received. |
| 141 | * Returns nothing. |
| 142 | * - http_reply : Called when, at any time, HA proxy decides to stop |
| 143 | * the HTTP message's processing and to send a message |
| 144 | * to the client (mainly, when an error or a redirect |
| 145 | * occur). |
| 146 | * Returns nothing. |
| 147 | * - http_forward_data : Called when some data can be consumed. |
| 148 | * Returns a negative value if an error occurs, else |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 149 | * the number of forwarded bytes. [DEPRECATED] |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 150 | * - tcp_data : Called when unparsed data are available. |
| 151 | * Returns a negative value if an error occurs, else |
| 152 | * the number of consumed bytes. |
| 153 | * - tcp_forward_data : Called when some data can be consumed. |
| 154 | * Returns a negative value if an error occurs, else |
| 155 | * or the number of forwarded bytes. |
| 156 | */ |
| 157 | struct flt_ops { |
| 158 | /* |
| 159 | * Callbacks to manage the filter lifecycle |
| 160 | */ |
Christopher Faulet | 71a6a8e | 2017-07-27 16:33:28 +0200 | [diff] [blame] | 161 | int (*init) (struct proxy *p, struct flt_conf *fconf); |
| 162 | void (*deinit) (struct proxy *p, struct flt_conf *fconf); |
| 163 | int (*check) (struct proxy *p, struct flt_conf *fconf); |
| 164 | int (*init_per_thread) (struct proxy *p, struct flt_conf *fconf); |
| 165 | void (*deinit_per_thread)(struct proxy *p, struct flt_conf *fconf); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 166 | /* |
| 167 | * Stream callbacks |
| 168 | */ |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 169 | int (*attach) (struct stream *s, struct filter *f); |
| 170 | int (*stream_start) (struct stream *s, struct filter *f); |
| 171 | int (*stream_set_backend)(struct stream *s, struct filter *f, struct proxy *be); |
| 172 | void (*stream_stop) (struct stream *s, struct filter *f); |
| 173 | void (*detach) (struct stream *s, struct filter *f); |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 174 | void (*check_timeouts) (struct stream *s, struct filter *f); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 175 | /* |
| 176 | * Channel callbacks |
| 177 | */ |
| 178 | int (*channel_start_analyze)(struct stream *s, struct filter *f, struct channel *chn); |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 179 | int (*channel_pre_analyze) (struct stream *s, struct filter *f, struct channel *chn, unsigned int an_bit); |
| 180 | int (*channel_post_analyze) (struct stream *s, struct filter *f, struct channel *chn, unsigned int an_bit); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 181 | int (*channel_end_analyze) (struct stream *s, struct filter *f, struct channel *chn); |
| 182 | |
| 183 | /* |
| 184 | * HTTP callbacks |
| 185 | */ |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 186 | int (*http_headers) (struct stream *s, struct filter *f, struct http_msg *msg); |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 187 | int (*http_payload) (struct stream *s, struct filter *f, struct http_msg *msg, |
| 188 | unsigned int offset, unsigned int len); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 189 | int (*http_end) (struct stream *s, struct filter *f, struct http_msg *msg); |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 190 | int (*http_data) (struct stream *s, struct filter *f, struct http_msg *msg); // DEPRECATED |
| 191 | int (*http_chunk_trailers)(struct stream *s, struct filter *f, struct http_msg *msg); // DEPRECATED |
| 192 | int (*http_forward_data) (struct stream *s, struct filter *f, struct http_msg *msg, // DEPRECATED |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 193 | unsigned int len); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 194 | |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 195 | void (*http_reset) (struct stream *s, struct filter *f, struct http_msg *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 196 | void (*http_reply) (struct stream *s, struct filter *f, short status, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 197 | const struct buffer *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * TCP callbacks |
| 201 | */ |
| 202 | int (*tcp_data) (struct stream *s, struct filter *f, struct channel *chn); |
| 203 | int (*tcp_forward_data)(struct stream *s, struct filter *f, struct channel *chn, |
| 204 | unsigned int len); |
| 205 | }; |
| 206 | |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 207 | /* Flags set on a filter config */ |
| 208 | #define FLT_CFG_FL_HTX 0x00000001 /* The filter can filter HTX streams */ |
| 209 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 210 | /* Flags set on a filter instance */ |
| 211 | #define FLT_FL_IS_BACKEND_FILTER 0x0001 /* The filter is a backend filter */ |
| 212 | #define FLT_FL_IS_REQ_DATA_FILTER 0x0002 /* The filter will parse data on the request channel */ |
| 213 | #define FLT_FL_IS_RSP_DATA_FILTER 0x0004 /* The filter will parse data on the response channel */ |
| 214 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 215 | /* Flags set on the stream, common to all filters attached to its stream */ |
| 216 | #define STRM_FLT_FL_HAS_FILTERS 0x0001 /* The stream has at least one filter */ |
| 217 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 218 | /* |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 219 | * Structure representing the filter configuration, attached to a proxy and |
| 220 | * accessible from a filter when instantiated in a stream |
| 221 | */ |
| 222 | struct flt_conf { |
| 223 | const char *id; /* The filter id */ |
| 224 | struct flt_ops *ops; /* The filter callbacks */ |
| 225 | void *conf; /* The filter configuration */ |
| 226 | struct list list; /* Next filter for the same proxy */ |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 227 | unsigned int flags; /* FLT_CFG_FL_* */ |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | /* |
| 231 | * Structure reprensenting a filter instance attached to a stream |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 232 | * |
| 233 | * 2D-Array fields are used to store info per channel. The first index stands |
| 234 | * for the request channel, and the second one for the response channel. |
| 235 | * Especially, <next> and <fwd> are offets representing amount of data that the |
| 236 | * filter are, respectively, parsed and forwarded on a channel. Filters can |
| 237 | * access these values using FLT_NXT and FLT_FWD macros. |
| 238 | */ |
| 239 | struct filter { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 240 | struct flt_conf *config; /* the filter's configuration */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 241 | void *ctx; /* The filter context (opaque) */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 242 | unsigned short flags; /* FLT_FL_* */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 243 | unsigned int next[2]; /* Offset, relative to buf->p, to the next byte to parse for a specific channel |
| 244 | * 0: request channel, 1: response channel */ |
| 245 | unsigned int fwd[2]; /* Offset, relative to buf->p, to the next byte to forward for a specific channel |
| 246 | * 0: request channel, 1: response channel */ |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 247 | unsigned long long offset[2]; |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 248 | unsigned int pre_analyzers; /* bit field indicating analyzers to pre-process */ |
| 249 | unsigned int post_analyzers; /* bit field indicating analyzers to post-process */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 250 | struct list list; /* Next filter for the same proxy/stream */ |
| 251 | }; |
| 252 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 253 | /* |
| 254 | * Structure reprensenting the "global" state of filters attached to a stream. |
| 255 | */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 256 | struct strm_flt { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 257 | struct list filters; /* List of filters attached to a stream */ |
| 258 | struct filter *current[2]; /* From which filter resume processing, for a specific channel. |
| 259 | * This is used for resumable callbacks only, |
| 260 | * If NULL, we start from the first filter. |
| 261 | * 0: request channel, 1: response channel */ |
| 262 | unsigned short flags; /* STRM_FL_* */ |
Joseph Herlant | b35ea68 | 2018-11-15 12:24:23 -0800 | [diff] [blame] | 263 | unsigned char nb_req_data_filters; /* Number of data filters registered on the request channel */ |
| 264 | unsigned char nb_rsp_data_filters; /* Number of data filters registered on the response channel */ |
Christopher Faulet | 75bc913 | 2018-11-30 15:18:09 +0100 | [diff] [blame] | 265 | unsigned long long offset[2]; |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 266 | }; |
| 267 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 268 | #endif /* _TYPES_FILTERS_H */ |
| 269 | |
| 270 | /* |
| 271 | * Local variables: |
| 272 | * c-indent-level: 8 |
| 273 | * c-basic-offset: 8 |
| 274 | * End: |
| 275 | */ |