Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * "tcp" rules processing |
| 3 | * |
| 4 | * Copyright 2000-2016 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
Willy Tarreau | dcc048a | 2020-06-04 19:11:43 +0200 | [diff] [blame] | 12 | #include <haproxy/acl.h> |
Willy Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 13 | #include <haproxy/action.h> |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 14 | #include <haproxy/api.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 15 | #include <haproxy/arg-t.h> |
Willy Tarreau | 278161c | 2020-06-04 11:18:28 +0200 | [diff] [blame] | 16 | #include <haproxy/capture-t.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 17 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 18 | #include <haproxy/channel.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 19 | #include <haproxy/connection.h> |
Christopher Faulet | 9a52123 | 2022-03-30 14:42:50 +0200 | [diff] [blame] | 20 | #include <haproxy/conn_stream.h> |
| 21 | #include <haproxy/cs_utils.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 22 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 23 | #include <haproxy/list.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 24 | #include <haproxy/log.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 25 | #include <haproxy/proxy.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 26 | #include <haproxy/sample.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 27 | #include <haproxy/stick_table.h> |
| 28 | #include <haproxy/stream-t.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 29 | #include <haproxy/stream_interface.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 30 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | c2f7c58 | 2020-06-02 18:15:32 +0200 | [diff] [blame] | 31 | #include <haproxy/ticks.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 32 | #include <haproxy/tools.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 33 | #include <haproxy/trace.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 34 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 35 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 36 | #define TRACE_SOURCE &trace_strm |
| 37 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 38 | /* List head of all known action keywords for "tcp-request connection" */ |
| 39 | struct list tcp_req_conn_keywords = LIST_HEAD_INIT(tcp_req_conn_keywords); |
| 40 | struct list tcp_req_sess_keywords = LIST_HEAD_INIT(tcp_req_sess_keywords); |
| 41 | struct list tcp_req_cont_keywords = LIST_HEAD_INIT(tcp_req_cont_keywords); |
| 42 | struct list tcp_res_cont_keywords = LIST_HEAD_INIT(tcp_res_cont_keywords); |
| 43 | |
| 44 | /* |
| 45 | * Register keywords. |
| 46 | */ |
| 47 | void tcp_req_conn_keywords_register(struct action_kw_list *kw_list) |
| 48 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 49 | LIST_APPEND(&tcp_req_conn_keywords, &kw_list->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void tcp_req_sess_keywords_register(struct action_kw_list *kw_list) |
| 53 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 54 | LIST_APPEND(&tcp_req_sess_keywords, &kw_list->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void tcp_req_cont_keywords_register(struct action_kw_list *kw_list) |
| 58 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 59 | LIST_APPEND(&tcp_req_cont_keywords, &kw_list->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void tcp_res_cont_keywords_register(struct action_kw_list *kw_list) |
| 63 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 64 | LIST_APPEND(&tcp_res_cont_keywords, &kw_list->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Return the struct tcp_req_action_kw associated to a keyword. |
| 69 | */ |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 70 | struct action_kw *tcp_req_conn_action(const char *kw) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 71 | { |
| 72 | return action_lookup(&tcp_req_conn_keywords, kw); |
| 73 | } |
| 74 | |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 75 | struct action_kw *tcp_req_sess_action(const char *kw) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 76 | { |
| 77 | return action_lookup(&tcp_req_sess_keywords, kw); |
| 78 | } |
| 79 | |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 80 | struct action_kw *tcp_req_cont_action(const char *kw) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 81 | { |
| 82 | return action_lookup(&tcp_req_cont_keywords, kw); |
| 83 | } |
| 84 | |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 85 | struct action_kw *tcp_res_cont_action(const char *kw) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 86 | { |
| 87 | return action_lookup(&tcp_res_cont_keywords, kw); |
| 88 | } |
| 89 | |
| 90 | /* This function performs the TCP request analysis on the current request. It |
| 91 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 92 | * needs more data, encounters an error, or wants to immediately abort the |
| 93 | * request. It relies on buffers flags, and updates s->req->analysers. The |
| 94 | * function may be called for frontend rules and backend rules. It only relies |
| 95 | * on the backend pointer so this works for both cases. |
| 96 | */ |
| 97 | int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit) |
| 98 | { |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 99 | struct list *def_rules, *rules; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 100 | struct session *sess = s->sess; |
| 101 | struct act_rule *rule; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 102 | int partial; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 103 | int act_opts = 0; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 104 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 105 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 106 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 107 | def_rules = ((s->be->defpx && (an_bit == AN_REQ_INSPECT_FE || s->be->defpx != sess->fe->defpx)) ? &s->be->defpx->tcp_req.inspect_rules : NULL); |
| 108 | rules = &s->be->tcp_req.inspect_rules; |
| 109 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 110 | /* We don't know whether we have enough data, so must proceed |
| 111 | * this way : |
| 112 | * - iterate through all rules in their declaration order |
| 113 | * - if one rule returns MISS, it means the inspect delay is |
| 114 | * not over yet, then return immediately, otherwise consider |
| 115 | * it as a non-match. |
| 116 | * - if one rule returns OK, then return OK |
| 117 | * - if one rule returns KO, then return KO |
| 118 | */ |
| 119 | |
Christopher Faulet | cb59e0b | 2021-09-30 14:56:30 +0200 | [diff] [blame] | 120 | if ((req->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)) || channel_full(req, global.tune.maxrewrite) || |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame^] | 121 | cs_rx_blocked_room(chn_prod(req)) || |
Christopher Faulet | 2747fbb | 2020-07-28 11:56:13 +0200 | [diff] [blame] | 122 | !s->be->tcp_req.inspect_delay || tick_is_expired(s->rules_exp, now_ms)) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 123 | partial = SMP_OPT_FINAL; |
| 124 | else |
| 125 | partial = 0; |
| 126 | |
| 127 | /* If "the current_rule_list" match the executed rule list, we are in |
| 128 | * resume condition. If a resume is needed it is always in the action |
| 129 | * and never in the ACL or converters. In this case, we initialise the |
| 130 | * current rule, and go to the action execution point. |
| 131 | */ |
| 132 | if (s->current_rule) { |
| 133 | rule = s->current_rule; |
| 134 | s->current_rule = NULL; |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 135 | if ((def_rules && s->current_rule_list == def_rules) || s->current_rule_list == rules) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 136 | goto resume_execution; |
| 137 | } |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 138 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 139 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 140 | restart: |
| 141 | list_for_each_entry(rule, s->current_rule_list, list) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 142 | enum acl_test_res ret = ACL_TEST_PASS; |
| 143 | |
| 144 | if (rule->cond) { |
| 145 | ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial); |
| 146 | if (ret == ACL_TEST_MISS) |
| 147 | goto missing_data; |
| 148 | |
| 149 | ret = acl_pass(ret); |
| 150 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 151 | ret = !ret; |
| 152 | } |
| 153 | |
| 154 | if (ret) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 155 | act_opts |= ACT_OPT_FIRST; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 156 | resume_execution: |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 157 | |
| 158 | /* Always call the action function if defined */ |
| 159 | if (rule->action_ptr) { |
| 160 | if (partial & SMP_OPT_FINAL) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 161 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 162 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 163 | switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 164 | case ACT_RET_CONT: |
| 165 | break; |
| 166 | case ACT_RET_STOP: |
| 167 | case ACT_RET_DONE: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 168 | s->last_rule_file = rule->conf.file; |
| 169 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 170 | goto end; |
| 171 | case ACT_RET_YIELD: |
| 172 | s->current_rule = rule; |
Christopher Faulet | 99aaca9 | 2020-07-28 11:30:19 +0200 | [diff] [blame] | 173 | if (partial & SMP_OPT_FINAL) { |
| 174 | send_log(s->be, LOG_WARNING, |
| 175 | "Internal error: yield not allowed if the inspect-delay expired " |
| 176 | "for the tcp-request content actions."); |
| 177 | goto internal; |
| 178 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 179 | goto missing_data; |
| 180 | case ACT_RET_DENY: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 181 | s->last_rule_file = rule->conf.file; |
| 182 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 183 | goto deny; |
| 184 | case ACT_RET_ABRT: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 185 | s->last_rule_file = rule->conf.file; |
| 186 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 187 | goto abort; |
| 188 | case ACT_RET_ERR: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 189 | s->last_rule_file = rule->conf.file; |
| 190 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 191 | goto internal; |
| 192 | case ACT_RET_INV: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 193 | s->last_rule_file = rule->conf.file; |
| 194 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 195 | goto invalid; |
| 196 | } |
| 197 | continue; /* eval the next rule */ |
| 198 | } |
| 199 | |
| 200 | /* If not action function defined, check for known actions */ |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 201 | if (rule->action == ACT_ACTION_ALLOW) { |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 202 | s->last_rule_file = rule->conf.file; |
| 203 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 204 | goto end; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 205 | } |
| 206 | else if (rule->action == ACT_ACTION_DENY) { |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 207 | s->last_rule_file = rule->conf.file; |
| 208 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 209 | goto deny; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 210 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 214 | if (def_rules && s->current_rule_list == def_rules) { |
| 215 | s->current_rule_list = rules; |
| 216 | goto restart; |
| 217 | } |
| 218 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 219 | end: |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 220 | /* if we get there, it means we have no rule which matches, or |
| 221 | * we have an explicit accept, so we apply the default accept. |
| 222 | */ |
| 223 | req->analysers &= ~an_bit; |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 224 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | 2747fbb | 2020-07-28 11:56:13 +0200 | [diff] [blame] | 225 | req->analyse_exp = s->rules_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 226 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 227 | return 1; |
| 228 | |
| 229 | missing_data: |
| 230 | channel_dont_connect(req); |
| 231 | /* just set the request timeout once at the beginning of the request */ |
Christopher Faulet | 2747fbb | 2020-07-28 11:56:13 +0200 | [diff] [blame] | 232 | if (!tick_isset(s->rules_exp) && s->be->tcp_req.inspect_delay) |
| 233 | s->rules_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay); |
| 234 | req->analyse_exp = tick_first((tick_is_expired(req->analyse_exp, now_ms) ? 0 : req->analyse_exp), s->rules_exp); |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 235 | DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 236 | return 0; |
| 237 | |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 238 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 239 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 240 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 241 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 242 | goto reject; |
| 243 | |
| 244 | internal: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 245 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 246 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 247 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 248 | if (!(s->flags & SF_ERR_MASK)) |
| 249 | s->flags |= SF_ERR_INTERNAL; |
| 250 | goto reject; |
| 251 | |
| 252 | invalid: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 253 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 254 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 255 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 256 | |
| 257 | reject: |
Christopher Faulet | 9a52123 | 2022-03-30 14:42:50 +0200 | [diff] [blame] | 258 | cs_must_kill_conn(chn_prod(req)); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 259 | channel_abort(req); |
| 260 | channel_abort(&s->res); |
| 261 | |
| 262 | abort: |
| 263 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 264 | s->current_rule = s->current_rule_list = NULL; |
| 265 | req->analyse_exp = s->rules_exp = TICK_ETERNITY; |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 266 | |
| 267 | if (!(s->flags & SF_ERR_MASK)) |
| 268 | s->flags |= SF_ERR_PRXCOND; |
| 269 | if (!(s->flags & SF_FINST_MASK)) |
| 270 | s->flags |= SF_FINST_R; |
| 271 | DBG_TRACE_DEVEL("leaving on error|deny|abort", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_TCP_ERR, s); |
| 272 | return 0; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* This function performs the TCP response analysis on the current response. It |
| 276 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 277 | * needs more data, encounters an error, or wants to immediately abort the |
| 278 | * response. It relies on buffers flags, and updates s->rep->analysers. The |
| 279 | * function may be called for backend rules. |
| 280 | */ |
| 281 | int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit) |
| 282 | { |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 283 | struct list *def_rules, *rules; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 284 | struct session *sess = s->sess; |
| 285 | struct act_rule *rule; |
| 286 | int partial; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 287 | int act_opts = 0; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 288 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 289 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 290 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 291 | def_rules = (s->be->defpx ? &s->be->defpx->tcp_rep.inspect_rules : NULL); |
| 292 | rules = &s->be->tcp_rep.inspect_rules; |
| 293 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 294 | /* We don't know whether we have enough data, so must proceed |
| 295 | * this way : |
| 296 | * - iterate through all rules in their declaration order |
| 297 | * - if one rule returns MISS, it means the inspect delay is |
| 298 | * not over yet, then return immediately, otherwise consider |
| 299 | * it as a non-match. |
| 300 | * - if one rule returns OK, then return OK |
| 301 | * - if one rule returns KO, then return KO |
| 302 | */ |
Christopher Faulet | cb59e0b | 2021-09-30 14:56:30 +0200 | [diff] [blame] | 303 | if ((rep->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)) || channel_full(rep, global.tune.maxrewrite) || |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame^] | 304 | cs_rx_blocked_room(chn_prod(rep)) || |
Christopher Faulet | 2747fbb | 2020-07-28 11:56:13 +0200 | [diff] [blame] | 305 | !s->be->tcp_rep.inspect_delay || tick_is_expired(s->rules_exp, now_ms)) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 306 | partial = SMP_OPT_FINAL; |
| 307 | else |
| 308 | partial = 0; |
| 309 | |
| 310 | /* If "the current_rule_list" match the executed rule list, we are in |
| 311 | * resume condition. If a resume is needed it is always in the action |
| 312 | * and never in the ACL or converters. In this case, we initialise the |
| 313 | * current rule, and go to the action execution point. |
| 314 | */ |
| 315 | if (s->current_rule) { |
| 316 | rule = s->current_rule; |
| 317 | s->current_rule = NULL; |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 318 | if ((def_rules && s->current_rule_list == def_rules) || s->current_rule_list == rules) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 319 | goto resume_execution; |
| 320 | } |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 321 | s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 322 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 323 | restart: |
| 324 | list_for_each_entry(rule, s->current_rule_list, list) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 325 | enum acl_test_res ret = ACL_TEST_PASS; |
| 326 | |
| 327 | if (rule->cond) { |
| 328 | ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial); |
Christopher Faulet | 2747fbb | 2020-07-28 11:56:13 +0200 | [diff] [blame] | 329 | if (ret == ACL_TEST_MISS) |
| 330 | goto missing_data; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 331 | |
| 332 | ret = acl_pass(ret); |
| 333 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 334 | ret = !ret; |
| 335 | } |
| 336 | |
| 337 | if (ret) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 338 | act_opts |= ACT_OPT_FIRST; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 339 | resume_execution: |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 340 | /* Always call the action function if defined */ |
| 341 | if (rule->action_ptr) { |
| 342 | if (partial & SMP_OPT_FINAL) |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 343 | act_opts |= ACT_OPT_FINAL; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 344 | |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 345 | switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 346 | case ACT_RET_CONT: |
| 347 | break; |
| 348 | case ACT_RET_STOP: |
| 349 | case ACT_RET_DONE: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 350 | s->last_rule_file = rule->conf.file; |
| 351 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 352 | goto end; |
| 353 | case ACT_RET_YIELD: |
| 354 | s->current_rule = rule; |
Christopher Faulet | 99aaca9 | 2020-07-28 11:30:19 +0200 | [diff] [blame] | 355 | if (partial & SMP_OPT_FINAL) { |
| 356 | send_log(s->be, LOG_WARNING, |
| 357 | "Internal error: yield not allowed if the inspect-delay expired " |
| 358 | "for the tcp-response content actions."); |
| 359 | goto internal; |
| 360 | } |
Christopher Faulet | 2747fbb | 2020-07-28 11:56:13 +0200 | [diff] [blame] | 361 | channel_dont_close(rep); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 362 | goto missing_data; |
| 363 | case ACT_RET_DENY: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 364 | s->last_rule_file = rule->conf.file; |
| 365 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 366 | goto deny; |
| 367 | case ACT_RET_ABRT: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 368 | s->last_rule_file = rule->conf.file; |
| 369 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 370 | goto abort; |
| 371 | case ACT_RET_ERR: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 372 | s->last_rule_file = rule->conf.file; |
| 373 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 374 | goto internal; |
| 375 | case ACT_RET_INV: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 376 | s->last_rule_file = rule->conf.file; |
| 377 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 378 | goto invalid; |
| 379 | } |
| 380 | continue; /* eval the next rule */ |
| 381 | } |
| 382 | |
| 383 | /* If not action function defined, check for known actions */ |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 384 | if (rule->action == ACT_ACTION_ALLOW) { |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 385 | s->last_rule_file = rule->conf.file; |
| 386 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 387 | goto end; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 388 | } |
| 389 | else if (rule->action == ACT_ACTION_DENY) { |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 390 | s->last_rule_file = rule->conf.file; |
| 391 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 392 | goto deny; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 393 | } |
| 394 | else if (rule->action == ACT_TCP_CLOSE) { |
Christopher Faulet | 8abe712 | 2022-03-30 15:10:18 +0200 | [diff] [blame] | 395 | chn_prod(rep)->flags |= CS_FL_NOLINGER | CS_FL_NOHALF; |
Christopher Faulet | 9a52123 | 2022-03-30 14:42:50 +0200 | [diff] [blame] | 396 | cs_must_kill_conn(chn_prod(rep)); |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 397 | cs_shutr(chn_prod(rep)); |
| 398 | cs_shutw(chn_prod(rep)); |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 399 | s->last_rule_file = rule->conf.file; |
| 400 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 401 | goto end; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 406 | if (def_rules && s->current_rule_list == def_rules) { |
| 407 | s->current_rule_list = rules; |
| 408 | goto restart; |
| 409 | } |
| 410 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 411 | end: |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 412 | /* if we get there, it means we have no rule which matches, or |
| 413 | * we have an explicit accept, so we apply the default accept. |
| 414 | */ |
| 415 | rep->analysers &= ~an_bit; |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 416 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | 2747fbb | 2020-07-28 11:56:13 +0200 | [diff] [blame] | 417 | rep->analyse_exp = s->rules_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 418 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 419 | return 1; |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 420 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 421 | missing_data: |
Christopher Faulet | 54f3e18 | 2020-07-29 12:00:23 +0200 | [diff] [blame] | 422 | /* just set the analyser timeout once at the beginning of the response */ |
Christopher Faulet | 2747fbb | 2020-07-28 11:56:13 +0200 | [diff] [blame] | 423 | if (!tick_isset(s->rules_exp) && s->be->tcp_rep.inspect_delay) |
| 424 | s->rules_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay); |
| 425 | rep->analyse_exp = tick_first((tick_is_expired(rep->analyse_exp, now_ms) ? 0 : rep->analyse_exp), s->rules_exp); |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 426 | DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s); |
| 427 | return 0; |
| 428 | |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 429 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 430 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.denied_resp); |
| 431 | _HA_ATOMIC_INC(&s->be->be_counters.denied_resp); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 432 | if (s->sess->listener && s->sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 433 | _HA_ATOMIC_INC(&s->sess->listener->counters->denied_resp); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 434 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 435 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 436 | goto reject; |
| 437 | |
| 438 | internal: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 439 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.internal_errors); |
| 440 | _HA_ATOMIC_INC(&s->be->be_counters.internal_errors); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 441 | if (s->sess->listener && s->sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 442 | _HA_ATOMIC_INC(&s->sess->listener->counters->internal_errors); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 443 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 444 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 445 | if (!(s->flags & SF_ERR_MASK)) |
| 446 | s->flags |= SF_ERR_INTERNAL; |
| 447 | goto reject; |
| 448 | |
| 449 | invalid: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 450 | _HA_ATOMIC_INC(&s->be->be_counters.failed_resp); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 451 | if (objt_server(s->target)) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 452 | _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 453 | |
| 454 | reject: |
Christopher Faulet | 9a52123 | 2022-03-30 14:42:50 +0200 | [diff] [blame] | 455 | cs_must_kill_conn(chn_prod(rep)); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 456 | channel_abort(rep); |
| 457 | channel_abort(&s->req); |
| 458 | |
| 459 | abort: |
Christopher Faulet | 19dbf2d | 2020-07-28 11:40:07 +0200 | [diff] [blame] | 460 | rep->analysers &= AN_RES_FLT_END; |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 461 | s->current_rule = s->current_rule_list = NULL; |
| 462 | rep->analyse_exp = s->rules_exp = TICK_ETERNITY; |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 463 | |
| 464 | if (!(s->flags & SF_ERR_MASK)) |
| 465 | s->flags |= SF_ERR_PRXCOND; |
| 466 | if (!(s->flags & SF_FINST_MASK)) |
| 467 | s->flags |= SF_FINST_D; |
| 468 | DBG_TRACE_DEVEL("leaving on error", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_TCP_ERR, s); |
| 469 | return 0; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
| 473 | /* This function performs the TCP layer4 analysis on the current request. It |
| 474 | * returns 0 if a reject rule matches, otherwise 1 if either an accept rule |
| 475 | * matches or if no more rule matches. It can only use rules which don't need |
| 476 | * any data. This only works on connection-based client-facing stream interfaces. |
| 477 | */ |
| 478 | int tcp_exec_l4_rules(struct session *sess) |
| 479 | { |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 480 | struct proxy *px = sess->fe; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 481 | struct act_rule *rule; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 482 | struct connection *conn = objt_conn(sess->origin); |
| 483 | int result = 1; |
| 484 | enum acl_test_res ret; |
| 485 | |
| 486 | if (!conn) |
| 487 | return result; |
| 488 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 489 | if (sess->fe->defpx) |
| 490 | px = sess->fe->defpx; |
| 491 | |
| 492 | restart: |
| 493 | list_for_each_entry(rule, &px->tcp_req.l4_rules, list) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 494 | ret = ACL_TEST_PASS; |
| 495 | |
| 496 | if (rule->cond) { |
| 497 | ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 498 | ret = acl_pass(ret); |
| 499 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 500 | ret = !ret; |
| 501 | } |
| 502 | |
| 503 | if (ret) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 504 | /* Always call the action function if defined */ |
| 505 | if (rule->action_ptr) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 506 | switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_OPT_FINAL | ACT_OPT_FIRST)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 507 | case ACT_RET_YIELD: |
| 508 | /* yield is not allowed at this point. If this return code is |
| 509 | * used it is a bug, so I prefer to abort the process. |
| 510 | */ |
| 511 | send_log(sess->fe, LOG_WARNING, |
| 512 | "Internal error: yield not allowed with tcp-request connection actions."); |
| 513 | /* fall through */ |
| 514 | case ACT_RET_STOP: |
| 515 | case ACT_RET_DONE: |
| 516 | goto end; |
| 517 | case ACT_RET_CONT: |
| 518 | break; |
| 519 | case ACT_RET_DENY: |
| 520 | case ACT_RET_ABRT: |
| 521 | case ACT_RET_ERR: |
| 522 | case ACT_RET_INV: |
| 523 | result = 0; |
| 524 | goto end; |
| 525 | } |
| 526 | continue; /* eval the next rule */ |
| 527 | } |
| 528 | |
| 529 | /* If not action function defined, check for known actions */ |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 530 | if (rule->action == ACT_ACTION_ALLOW) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 531 | goto end; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 532 | } |
| 533 | else if (rule->action == ACT_ACTION_DENY) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 534 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_conn); |
Willy Tarreau | a12dde0 | 2016-12-22 18:14:41 +0100 | [diff] [blame] | 535 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 536 | _HA_ATOMIC_INC(&sess->listener->counters->denied_conn); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 537 | |
| 538 | result = 0; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 539 | goto end; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 540 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 541 | else if (rule->action == ACT_TCP_EXPECT_PX) { |
Willy Tarreau | 4450b58 | 2020-01-23 15:23:13 +0100 | [diff] [blame] | 542 | if (!(conn->flags & CO_FL_HANDSHAKE)) { |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 543 | if (xprt_add_hs(conn) < 0) { |
| 544 | result = 0; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 545 | goto end; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 546 | } |
| 547 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 548 | conn->flags |= CO_FL_ACCEPT_PROXY; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 549 | } |
| 550 | else if (rule->action == ACT_TCP_EXPECT_CIP) { |
Willy Tarreau | 4450b58 | 2020-01-23 15:23:13 +0100 | [diff] [blame] | 551 | if (!(conn->flags & CO_FL_HANDSHAKE)) { |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 552 | if (xprt_add_hs(conn) < 0) { |
| 553 | result = 0; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 554 | goto end; |
Olivier Houchard | fe50bfb | 2019-05-27 12:09:19 +0200 | [diff] [blame] | 555 | } |
| 556 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 557 | conn->flags |= CO_FL_ACCEPT_CIP; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 558 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 559 | } |
| 560 | } |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 561 | |
| 562 | if (px != sess->fe) { |
| 563 | px = sess->fe; |
| 564 | goto restart; |
| 565 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 566 | end: |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 567 | return result; |
| 568 | } |
| 569 | |
| 570 | /* This function performs the TCP layer5 analysis on the current request. It |
| 571 | * returns 0 if a reject rule matches, otherwise 1 if either an accept rule |
| 572 | * matches or if no more rule matches. It can only use rules which don't need |
| 573 | * any data. This only works on session-based client-facing stream interfaces. |
| 574 | * An example of valid use case is to track a stick-counter on the source |
| 575 | * address extracted from the proxy protocol. |
| 576 | */ |
| 577 | int tcp_exec_l5_rules(struct session *sess) |
| 578 | { |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 579 | struct proxy *px = sess->fe; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 580 | struct act_rule *rule; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 581 | int result = 1; |
| 582 | enum acl_test_res ret; |
| 583 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 584 | if (sess->fe->defpx) |
| 585 | px = sess->fe->defpx; |
| 586 | |
| 587 | restart: |
| 588 | list_for_each_entry(rule, &px->tcp_req.l5_rules, list) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 589 | ret = ACL_TEST_PASS; |
| 590 | |
| 591 | if (rule->cond) { |
| 592 | ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
| 593 | ret = acl_pass(ret); |
| 594 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 595 | ret = !ret; |
| 596 | } |
| 597 | |
| 598 | if (ret) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 599 | /* Always call the action function if defined */ |
| 600 | if (rule->action_ptr) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 601 | switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_OPT_FINAL | ACT_OPT_FIRST)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 602 | case ACT_RET_YIELD: |
| 603 | /* yield is not allowed at this point. If this return code is |
| 604 | * used it is a bug, so I prefer to abort the process. |
| 605 | */ |
| 606 | send_log(sess->fe, LOG_WARNING, |
| 607 | "Internal error: yield not allowed with tcp-request session actions."); |
| 608 | /* fall through */ |
| 609 | case ACT_RET_STOP: |
| 610 | case ACT_RET_DONE: |
| 611 | goto end; |
| 612 | case ACT_RET_CONT: |
| 613 | break; |
| 614 | case ACT_RET_DENY: |
| 615 | case ACT_RET_ABRT: |
| 616 | case ACT_RET_ERR: |
| 617 | case ACT_RET_INV: |
| 618 | result = 0; |
| 619 | goto end; |
| 620 | } |
| 621 | continue; /* eval the next rule */ |
| 622 | } |
| 623 | |
| 624 | /* If not action function defined, check for known actions */ |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 625 | if (rule->action == ACT_ACTION_ALLOW) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 626 | goto end; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 627 | } |
| 628 | else if (rule->action == ACT_ACTION_DENY) { |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 629 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_sess); |
Willy Tarreau | a12dde0 | 2016-12-22 18:14:41 +0100 | [diff] [blame] | 630 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 631 | _HA_ATOMIC_INC(&sess->listener->counters->denied_sess); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 632 | |
| 633 | result = 0; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 634 | goto end; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 635 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 636 | } |
| 637 | } |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 638 | |
| 639 | if (px != sess->fe) { |
| 640 | px = sess->fe; |
| 641 | goto restart; |
| 642 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 643 | end: |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 644 | return result; |
| 645 | } |
| 646 | |
| 647 | /* Parse a tcp-response rule. Return a negative value in case of failure */ |
| 648 | static int tcp_parse_response_rule(char **args, int arg, int section_type, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 649 | struct proxy *curpx, const struct proxy *defpx, |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 650 | struct act_rule *rule, char **err, |
| 651 | unsigned int where, |
| 652 | const char *file, int line) |
| 653 | { |
Christopher Faulet | ee08d6c | 2021-10-13 15:40:15 +0200 | [diff] [blame] | 654 | if ((curpx == defpx && strlen(defpx->id) == 0) || !(curpx->cap & PR_CAP_BE)) { |
| 655 | memprintf(err, "%s %s is only allowed in 'backend' sections or 'defaults' section with a name", |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 656 | args[0], args[1]); |
| 657 | return -1; |
| 658 | } |
| 659 | |
| 660 | if (strcmp(args[arg], "accept") == 0) { |
| 661 | arg++; |
| 662 | rule->action = ACT_ACTION_ALLOW; |
Christopher Faulet | 245cf79 | 2019-12-18 14:58:12 +0100 | [diff] [blame] | 663 | rule->flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 664 | } |
| 665 | else if (strcmp(args[arg], "reject") == 0) { |
| 666 | arg++; |
| 667 | rule->action = ACT_ACTION_DENY; |
Christopher Faulet | 245cf79 | 2019-12-18 14:58:12 +0100 | [diff] [blame] | 668 | rule->flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 669 | } |
| 670 | else if (strcmp(args[arg], "close") == 0) { |
| 671 | arg++; |
| 672 | rule->action = ACT_TCP_CLOSE; |
Christopher Faulet | 245cf79 | 2019-12-18 14:58:12 +0100 | [diff] [blame] | 673 | rule->flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 674 | } |
| 675 | else { |
| 676 | struct action_kw *kw; |
| 677 | kw = tcp_res_cont_action(args[arg]); |
| 678 | if (kw) { |
| 679 | arg++; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 680 | rule->kw = kw; |
| 681 | if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR) |
| 682 | return -1; |
| 683 | } else { |
Willy Tarreau | db67b0e | 2021-03-12 13:46:10 +0100 | [diff] [blame] | 684 | const char *extra[] = { "accept", "reject", "close", NULL }; |
| 685 | const char *best = action_suggest(args[arg], &tcp_res_cont_keywords, extra); |
| 686 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 687 | action_build_list(&tcp_res_cont_keywords, &trash); |
| 688 | memprintf(err, |
Willy Tarreau | db67b0e | 2021-03-12 13:46:10 +0100 | [diff] [blame] | 689 | "'%s %s' expects 'accept', 'close', 'reject', %s in %s '%s' (got '%s').%s%s%s", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 690 | args[0], args[1], trash.area, |
Willy Tarreau | db67b0e | 2021-03-12 13:46:10 +0100 | [diff] [blame] | 691 | proxy_type_str(curpx), curpx->id, args[arg], |
| 692 | best ? " Did you mean '" : "", |
| 693 | best ? best : "", |
| 694 | best ? "' maybe ?" : ""); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 695 | return -1; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) { |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 700 | if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 701 | memprintf(err, |
| 702 | "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s", |
| 703 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err); |
| 704 | return -1; |
| 705 | } |
| 706 | } |
| 707 | else if (*args[arg]) { |
| 708 | memprintf(err, |
| 709 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')", |
| 710 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]); |
| 711 | return -1; |
| 712 | } |
| 713 | return 0; |
| 714 | } |
| 715 | |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 716 | |
| 717 | /* This function executes a track-sc* actions. On success, it returns |
| 718 | * ACT_RET_CONT. If it must yield, it return ACT_RET_YIELD. Otherwsize |
| 719 | * ACT_RET_ERR is returned. |
| 720 | */ |
| 721 | static enum act_return tcp_action_track_sc(struct act_rule *rule, struct proxy *px, |
| 722 | struct session *sess, struct stream *s, int flags) |
| 723 | { |
| 724 | struct stksess *ts; |
| 725 | struct stktable *t; |
| 726 | struct stktable_key *key; |
| 727 | struct sample smp; |
| 728 | int opt; |
| 729 | |
Christopher Faulet | 6730779 | 2020-02-10 09:54:49 +0100 | [diff] [blame] | 730 | opt = SMP_OPT_DIR_REQ; |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 731 | if (flags & ACT_FLAG_FINAL) |
| 732 | opt |= SMP_OPT_FINAL; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 733 | |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 734 | t = rule->arg.trk_ctr.table.t; |
Christopher Faulet | 6730779 | 2020-02-10 09:54:49 +0100 | [diff] [blame] | 735 | if (rule->from == ACT_F_TCP_REQ_CNT) { /* L7 rules: use the stream */ |
| 736 | if (stkctr_entry(&s->stkctr[rule->action])) |
| 737 | goto end; |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 738 | |
Christopher Faulet | 6730779 | 2020-02-10 09:54:49 +0100 | [diff] [blame] | 739 | key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, &smp); |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 740 | |
Christopher Faulet | 6730779 | 2020-02-10 09:54:49 +0100 | [diff] [blame] | 741 | if ((smp.flags & SMP_F_MAY_CHANGE) && !(flags & ACT_FLAG_FINAL)) |
| 742 | return ACT_RET_YIELD; /* key might appear later */ |
| 743 | |
| 744 | if (key && (ts = stktable_get_entry(t, key))) { |
| 745 | stream_track_stkctr(&s->stkctr[rule->action], t, ts); |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 746 | stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT); |
| 747 | if (sess->fe != s->be) |
| 748 | stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND); |
| 749 | } |
| 750 | } |
Christopher Faulet | 6730779 | 2020-02-10 09:54:49 +0100 | [diff] [blame] | 751 | else { /* L4/L5 rules: use the session */ |
| 752 | if (stkctr_entry(&sess->stkctr[rule->action])) |
| 753 | goto end; |
| 754 | |
| 755 | key = stktable_fetch_key(t, sess->fe, sess, NULL, opt, rule->arg.trk_ctr.expr, NULL); |
| 756 | if (key && (ts = stktable_get_entry(t, key))) |
| 757 | stream_track_stkctr(&sess->stkctr[rule->action], t, ts); |
| 758 | } |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 759 | |
| 760 | end: |
| 761 | return ACT_RET_CONT; |
| 762 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 763 | |
Christopher Faulet | d73b96d | 2019-12-19 17:27:03 +0100 | [diff] [blame] | 764 | /* This function executes a capture actions. It executes a fetch expression, |
| 765 | * turns the result into a string and puts it in a capture slot. On success, it |
| 766 | * returns ACT_RET_CONT. If it must yield, it return ACT_RET_YIELD. Otherwsize |
| 767 | * ACT_RET_ERR is returned. |
| 768 | */ |
| 769 | static enum act_return tcp_action_capture(struct act_rule *rule, struct proxy *px, |
| 770 | struct session *sess, struct stream *s, int flags) |
| 771 | { |
| 772 | struct sample *key; |
| 773 | struct cap_hdr *h = rule->arg.cap.hdr; |
| 774 | char **cap = s->req_cap; |
| 775 | int len, opt; |
| 776 | |
| 777 | opt = ((rule->from == ACT_F_TCP_REQ_CNT) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 778 | if (flags & ACT_FLAG_FINAL) |
| 779 | opt |= SMP_OPT_FINAL; |
| 780 | |
| 781 | key = sample_fetch_as_type(s->be, sess, s, opt, rule->arg.cap.expr, SMP_T_STR); |
| 782 | if (!key) |
| 783 | goto end; |
| 784 | |
| 785 | if ((key->flags & SMP_F_MAY_CHANGE) && !(flags & ACT_FLAG_FINAL)) |
| 786 | return ACT_RET_YIELD; /* key might appear later */ |
| 787 | |
| 788 | if (cap[h->index] == NULL) { |
| 789 | cap[h->index] = pool_alloc(h->pool); |
| 790 | if (cap[h->index] == NULL) /* no more capture memory, ignore error */ |
| 791 | goto end; |
| 792 | } |
| 793 | |
| 794 | len = key->data.u.str.data; |
| 795 | if (len > h->len) |
| 796 | len = h->len; |
| 797 | |
| 798 | memcpy(cap[h->index], key->data.u.str.area, len); |
| 799 | cap[h->index][len] = 0; |
| 800 | |
| 801 | end: |
| 802 | return ACT_RET_CONT; |
| 803 | } |
| 804 | |
Christopher Faulet | adfc6e8 | 2020-01-14 15:05:33 +0100 | [diff] [blame] | 805 | static void release_tcp_capture(struct act_rule * rule) |
| 806 | { |
| 807 | release_sample_expr(rule->arg.cap.expr); |
| 808 | } |
| 809 | |
| 810 | |
| 811 | static void release_tcp_track_sc(struct act_rule * rule) |
| 812 | { |
| 813 | release_sample_expr(rule->arg.trk_ctr.expr); |
| 814 | } |
| 815 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 816 | /* Parse a tcp-request rule. Return a negative value in case of failure */ |
| 817 | static int tcp_parse_request_rule(char **args, int arg, int section_type, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 818 | struct proxy *curpx, const struct proxy *defpx, |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 819 | struct act_rule *rule, char **err, |
| 820 | unsigned int where, const char *file, int line) |
| 821 | { |
Christopher Faulet | ee08d6c | 2021-10-13 15:40:15 +0200 | [diff] [blame] | 822 | if (curpx == defpx && strlen(defpx->id) == 0) { |
| 823 | memprintf(err, "%s %s is not allowed in anonymous 'defaults' sections", |
| 824 | args[0], args[1]); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 825 | return -1; |
| 826 | } |
| 827 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 828 | if (strcmp(args[arg], "accept") == 0) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 829 | arg++; |
| 830 | rule->action = ACT_ACTION_ALLOW; |
Christopher Faulet | 245cf79 | 2019-12-18 14:58:12 +0100 | [diff] [blame] | 831 | rule->flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 832 | } |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 833 | else if (strcmp(args[arg], "reject") == 0) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 834 | arg++; |
| 835 | rule->action = ACT_ACTION_DENY; |
Christopher Faulet | 245cf79 | 2019-12-18 14:58:12 +0100 | [diff] [blame] | 836 | rule->flags |= ACT_FLAG_FINAL; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 837 | } |
| 838 | else if (strcmp(args[arg], "capture") == 0) { |
| 839 | struct sample_expr *expr; |
| 840 | struct cap_hdr *hdr; |
| 841 | int kw = arg; |
| 842 | int len = 0; |
| 843 | |
| 844 | if (!(curpx->cap & PR_CAP_FE)) { |
| 845 | memprintf(err, |
| 846 | "'%s %s %s' : proxy '%s' has no frontend capability", |
| 847 | args[0], args[1], args[kw], curpx->id); |
| 848 | return -1; |
| 849 | } |
| 850 | |
| 851 | if (!(where & SMP_VAL_FE_REQ_CNT)) { |
| 852 | memprintf(err, |
| 853 | "'%s %s' is not allowed in '%s %s' rules in %s '%s'", |
| 854 | args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 855 | return -1; |
| 856 | } |
| 857 | |
| 858 | arg++; |
| 859 | |
| 860 | curpx->conf.args.ctx = ARGC_CAP; |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 861 | expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args, NULL); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 862 | if (!expr) { |
| 863 | memprintf(err, |
| 864 | "'%s %s %s' : %s", |
| 865 | args[0], args[1], args[kw], *err); |
| 866 | return -1; |
| 867 | } |
| 868 | |
| 869 | if (!(expr->fetch->val & where)) { |
| 870 | memprintf(err, |
| 871 | "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here", |
| 872 | args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use)); |
Christopher Faulet | fdb6fbf | 2020-01-14 15:05:56 +0100 | [diff] [blame] | 873 | release_sample_expr(expr); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 874 | return -1; |
| 875 | } |
| 876 | |
| 877 | if (strcmp(args[arg], "len") == 0) { |
| 878 | arg++; |
| 879 | if (!args[arg]) { |
| 880 | memprintf(err, |
| 881 | "'%s %s %s' : missing length value", |
| 882 | args[0], args[1], args[kw]); |
Christopher Faulet | fdb6fbf | 2020-01-14 15:05:56 +0100 | [diff] [blame] | 883 | release_sample_expr(expr); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 884 | return -1; |
| 885 | } |
| 886 | /* we copy the table name for now, it will be resolved later */ |
| 887 | len = atoi(args[arg]); |
| 888 | if (len <= 0) { |
| 889 | memprintf(err, |
| 890 | "'%s %s %s' : length must be > 0", |
| 891 | args[0], args[1], args[kw]); |
Christopher Faulet | fdb6fbf | 2020-01-14 15:05:56 +0100 | [diff] [blame] | 892 | release_sample_expr(expr); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 893 | return -1; |
| 894 | } |
| 895 | arg++; |
| 896 | } |
| 897 | |
| 898 | if (!len) { |
| 899 | memprintf(err, |
| 900 | "'%s %s %s' : a positive 'len' argument is mandatory", |
| 901 | args[0], args[1], args[kw]); |
| 902 | free(expr); |
| 903 | return -1; |
| 904 | } |
| 905 | |
| 906 | hdr = calloc(1, sizeof(*hdr)); |
Remi Tricot-Le Breton | 8cb0336 | 2021-05-17 10:08:16 +0200 | [diff] [blame] | 907 | if (!hdr) { |
| 908 | memprintf(err, "parsing [%s:%d] : out of memory", file, line); |
| 909 | release_sample_expr(expr); |
| 910 | return -1; |
| 911 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 912 | hdr->next = curpx->req_cap; |
| 913 | hdr->name = NULL; /* not a header capture */ |
| 914 | hdr->namelen = 0; |
| 915 | hdr->len = len; |
| 916 | hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED); |
| 917 | hdr->index = curpx->nb_req_cap++; |
| 918 | |
| 919 | curpx->req_cap = hdr; |
| 920 | curpx->to_log |= LW_REQHDR; |
| 921 | |
Christopher Faulet | 711ed6a | 2019-07-16 14:16:10 +0200 | [diff] [blame] | 922 | /* check if we need to allocate an http_txn struct for HTTP parsing */ |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 923 | curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 924 | |
| 925 | rule->arg.cap.expr = expr; |
| 926 | rule->arg.cap.hdr = hdr; |
Christopher Faulet | d73b96d | 2019-12-19 17:27:03 +0100 | [diff] [blame] | 927 | rule->action = ACT_CUSTOM; |
| 928 | rule->action_ptr = tcp_action_capture; |
| 929 | rule->check_ptr = check_capture; |
Christopher Faulet | adfc6e8 | 2020-01-14 15:05:33 +0100 | [diff] [blame] | 930 | rule->release_ptr = release_tcp_capture; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 931 | } |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 932 | else if (strncmp(args[arg], "track-sc", 8) == 0) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 933 | struct sample_expr *expr; |
| 934 | int kw = arg; |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 935 | unsigned int tsc_num; |
| 936 | const char *tsc_num_str; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 937 | |
| 938 | arg++; |
| 939 | |
Frédéric Lécaille | a41d531 | 2018-01-29 12:05:07 +0100 | [diff] [blame] | 940 | tsc_num_str = &args[kw][8]; |
| 941 | if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1) { |
| 942 | memprintf(err, "'%s %s %s' : %s", args[0], args[1], args[kw], *err); |
| 943 | return -1; |
| 944 | } |
| 945 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 946 | curpx->conf.args.ctx = ARGC_TRK; |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 947 | expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args, NULL); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 948 | if (!expr) { |
| 949 | memprintf(err, |
| 950 | "'%s %s %s' : %s", |
| 951 | args[0], args[1], args[kw], *err); |
| 952 | return -1; |
| 953 | } |
| 954 | |
| 955 | if (!(expr->fetch->val & where)) { |
| 956 | memprintf(err, |
| 957 | "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here", |
| 958 | args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use)); |
Christopher Faulet | fdb6fbf | 2020-01-14 15:05:56 +0100 | [diff] [blame] | 959 | release_sample_expr(expr); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 960 | return -1; |
| 961 | } |
| 962 | |
Christopher Faulet | 711ed6a | 2019-07-16 14:16:10 +0200 | [diff] [blame] | 963 | /* check if we need to allocate an http_txn struct for HTTP parsing */ |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 964 | curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY); |
| 965 | |
| 966 | if (strcmp(args[arg], "table") == 0) { |
| 967 | arg++; |
| 968 | if (!args[arg]) { |
| 969 | memprintf(err, |
| 970 | "'%s %s %s' : missing table name", |
| 971 | args[0], args[1], args[kw]); |
Christopher Faulet | fdb6fbf | 2020-01-14 15:05:56 +0100 | [diff] [blame] | 972 | release_sample_expr(expr); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 973 | return -1; |
| 974 | } |
| 975 | /* we copy the table name for now, it will be resolved later */ |
| 976 | rule->arg.trk_ctr.table.n = strdup(args[arg]); |
| 977 | arg++; |
| 978 | } |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 979 | rule->action = tsc_num; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 980 | rule->arg.trk_ctr.expr = expr; |
Christopher Faulet | ac98d81 | 2019-12-18 09:20:16 +0100 | [diff] [blame] | 981 | rule->action_ptr = tcp_action_track_sc; |
Christopher Faulet | 78880fb | 2017-09-18 14:43:55 +0200 | [diff] [blame] | 982 | rule->check_ptr = check_trk_action; |
Christopher Faulet | adfc6e8 | 2020-01-14 15:05:33 +0100 | [diff] [blame] | 983 | rule->release_ptr = release_tcp_track_sc; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 984 | } |
| 985 | else if (strcmp(args[arg], "expect-proxy") == 0) { |
| 986 | if (strcmp(args[arg+1], "layer4") != 0) { |
| 987 | memprintf(err, |
| 988 | "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')", |
| 989 | args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]); |
| 990 | return -1; |
| 991 | } |
| 992 | |
| 993 | if (!(where & SMP_VAL_FE_CON_ACC)) { |
| 994 | memprintf(err, |
| 995 | "'%s %s' is not allowed in '%s %s' rules in %s '%s'", |
| 996 | args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 997 | return -1; |
| 998 | } |
| 999 | |
| 1000 | arg += 2; |
| 1001 | rule->action = ACT_TCP_EXPECT_PX; |
| 1002 | } |
| 1003 | else if (strcmp(args[arg], "expect-netscaler-cip") == 0) { |
| 1004 | if (strcmp(args[arg+1], "layer4") != 0) { |
| 1005 | memprintf(err, |
| 1006 | "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')", |
| 1007 | args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]); |
| 1008 | return -1; |
| 1009 | } |
| 1010 | |
| 1011 | if (!(where & SMP_VAL_FE_CON_ACC)) { |
| 1012 | memprintf(err, |
| 1013 | "'%s %s' is not allowed in '%s %s' rules in %s '%s'", |
| 1014 | args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1015 | return -1; |
| 1016 | } |
| 1017 | |
| 1018 | arg += 2; |
| 1019 | rule->action = ACT_TCP_EXPECT_CIP; |
| 1020 | } |
| 1021 | else { |
| 1022 | struct action_kw *kw; |
| 1023 | if (where & SMP_VAL_FE_CON_ACC) { |
| 1024 | /* L4 */ |
| 1025 | kw = tcp_req_conn_action(args[arg]); |
| 1026 | rule->kw = kw; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1027 | } else if (where & SMP_VAL_FE_SES_ACC) { |
| 1028 | /* L5 */ |
| 1029 | kw = tcp_req_sess_action(args[arg]); |
| 1030 | rule->kw = kw; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1031 | } else { |
| 1032 | /* L6 */ |
| 1033 | kw = tcp_req_cont_action(args[arg]); |
| 1034 | rule->kw = kw; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1035 | } |
| 1036 | if (kw) { |
| 1037 | arg++; |
| 1038 | if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR) |
| 1039 | return -1; |
| 1040 | } else { |
Christopher Faulet | a561ffb | 2021-03-19 08:53:26 +0100 | [diff] [blame] | 1041 | const char *extra[] = { "accept", "reject", "capture", "track-sc", "expect-proxy", "expect-netscaler-cip", NULL }; |
Willy Tarreau | db67b0e | 2021-03-12 13:46:10 +0100 | [diff] [blame] | 1042 | const char *best = NULL; |
| 1043 | |
| 1044 | |
| 1045 | if (where & SMP_VAL_FE_CON_ACC) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1046 | action_build_list(&tcp_req_conn_keywords, &trash); |
Willy Tarreau | db67b0e | 2021-03-12 13:46:10 +0100 | [diff] [blame] | 1047 | best = action_suggest(args[arg], &tcp_req_conn_keywords, extra); |
| 1048 | } |
| 1049 | else if (where & SMP_VAL_FE_SES_ACC) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1050 | action_build_list(&tcp_req_sess_keywords, &trash); |
Willy Tarreau | db67b0e | 2021-03-12 13:46:10 +0100 | [diff] [blame] | 1051 | best = action_suggest(args[arg], &tcp_req_sess_keywords, extra); |
| 1052 | } |
| 1053 | else { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1054 | action_build_list(&tcp_req_cont_keywords, &trash); |
Willy Tarreau | db67b0e | 2021-03-12 13:46:10 +0100 | [diff] [blame] | 1055 | best = action_suggest(args[arg], &tcp_req_cont_keywords, extra); |
| 1056 | } |
| 1057 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1058 | memprintf(err, |
Christopher Faulet | a561ffb | 2021-03-19 08:53:26 +0100 | [diff] [blame] | 1059 | "'%s %s' expects 'accept', 'reject', 'capture', 'expect-proxy', 'expect-netscaler-cip', 'track-sc0' ... 'track-sc%d', %s " |
Willy Tarreau | db67b0e | 2021-03-12 13:46:10 +0100 | [diff] [blame] | 1060 | "in %s '%s' (got '%s').%s%s%s\n", |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1061 | args[0], args[1], MAX_SESS_STKCTR-1, |
| 1062 | trash.area, proxy_type_str(curpx), |
Willy Tarreau | db67b0e | 2021-03-12 13:46:10 +0100 | [diff] [blame] | 1063 | curpx->id, args[arg], |
| 1064 | best ? " Did you mean '" : "", |
| 1065 | best ? best : "", |
| 1066 | best ? "' maybe ?" : ""); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1067 | return -1; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) { |
Christopher Faulet | 1b421ea | 2017-09-22 14:38:56 +0200 | [diff] [blame] | 1072 | if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1073 | memprintf(err, |
| 1074 | "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s", |
| 1075 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err); |
| 1076 | return -1; |
| 1077 | } |
| 1078 | } |
| 1079 | else if (*args[arg]) { |
| 1080 | memprintf(err, |
| 1081 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')", |
| 1082 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]); |
| 1083 | return -1; |
| 1084 | } |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | /* This function should be called to parse a line starting with the "tcp-response" |
| 1089 | * keyword. |
| 1090 | */ |
| 1091 | static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1092 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1093 | char **err) |
| 1094 | { |
| 1095 | const char *ptr = NULL; |
| 1096 | unsigned int val; |
| 1097 | int warn = 0; |
| 1098 | int arg; |
| 1099 | struct act_rule *rule; |
| 1100 | unsigned int where; |
| 1101 | const struct acl *acl; |
| 1102 | const char *kw; |
| 1103 | |
| 1104 | if (!*args[1]) { |
| 1105 | memprintf(err, "missing argument for '%s' in %s '%s'", |
| 1106 | args[0], proxy_type_str(curpx), curpx->id); |
| 1107 | return -1; |
| 1108 | } |
| 1109 | |
| 1110 | if (strcmp(args[1], "inspect-delay") == 0) { |
Christopher Faulet | ee08d6c | 2021-10-13 15:40:15 +0200 | [diff] [blame] | 1111 | if ((curpx == defpx && strlen(defpx->id) == 0) || !(curpx->cap & PR_CAP_BE)) { |
| 1112 | memprintf(err, "%s %s is only allowed in 'backend' sections or 'defaults' section with a name", |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1113 | args[0], args[1]); |
| 1114 | return -1; |
| 1115 | } |
| 1116 | |
| 1117 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
| 1118 | memprintf(err, |
| 1119 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
| 1120 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 1121 | |
| 1122 | if (ptr == PARSE_TIME_OVER) |
| 1123 | memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]); |
| 1124 | else if (ptr == PARSE_TIME_UNDER) |
| 1125 | memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]); |
| 1126 | else if (ptr) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1127 | memprintf(err, "%s (unexpected character '%c')", *err, *ptr); |
| 1128 | return -1; |
| 1129 | } |
| 1130 | |
| 1131 | if (curpx->tcp_rep.inspect_delay) { |
| 1132 | memprintf(err, "ignoring %s %s (was already defined) in %s '%s'", |
| 1133 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1134 | return 1; |
| 1135 | } |
| 1136 | curpx->tcp_rep.inspect_delay = val; |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Willy Tarreau | d535f80 | 2021-10-11 08:49:26 +0200 | [diff] [blame] | 1140 | rule = new_act_rule(ACT_F_TCP_RES_CNT, file, line); |
Remi Tricot-Le Breton | 2ca42b4 | 2021-05-12 18:24:18 +0200 | [diff] [blame] | 1141 | if (!rule) { |
| 1142 | memprintf(err, "parsing [%s:%d] : out of memory", file, line); |
| 1143 | return -1; |
| 1144 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1145 | LIST_INIT(&rule->list); |
| 1146 | arg = 1; |
| 1147 | where = 0; |
| 1148 | |
| 1149 | if (strcmp(args[1], "content") == 0) { |
| 1150 | arg++; |
| 1151 | |
| 1152 | if (curpx->cap & PR_CAP_FE) |
| 1153 | where |= SMP_VAL_FE_RES_CNT; |
| 1154 | if (curpx->cap & PR_CAP_BE) |
| 1155 | where |= SMP_VAL_BE_RES_CNT; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1156 | if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0) |
| 1157 | goto error; |
| 1158 | |
| 1159 | acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL; |
| 1160 | if (acl) { |
| 1161 | if (acl->name && *acl->name) |
| 1162 | memprintf(err, |
| 1163 | "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'", |
| 1164 | acl->name, args[0], args[1], sample_ckp_names(where)); |
| 1165 | else |
| 1166 | memprintf(err, |
| 1167 | "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'", |
| 1168 | args[0], args[1], |
| 1169 | LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, |
| 1170 | sample_ckp_names(where)); |
| 1171 | |
| 1172 | warn++; |
| 1173 | } |
| 1174 | else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) { |
| 1175 | if (acl->name && *acl->name) |
| 1176 | memprintf(err, |
| 1177 | "acl '%s' involves keyword '%s' which is incompatible with '%s'", |
| 1178 | acl->name, kw, sample_ckp_names(where)); |
| 1179 | else |
| 1180 | memprintf(err, |
| 1181 | "anonymous acl involves keyword '%s' which is incompatible with '%s'", |
| 1182 | kw, sample_ckp_names(where)); |
| 1183 | warn++; |
| 1184 | } |
| 1185 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1186 | LIST_APPEND(&curpx->tcp_rep.inspect_rules, &rule->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1187 | } |
| 1188 | else { |
| 1189 | memprintf(err, |
| 1190 | "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')", |
| 1191 | args[0], proxy_type_str(curpx), curpx->id, args[1]); |
| 1192 | goto error; |
| 1193 | } |
| 1194 | |
| 1195 | return warn; |
| 1196 | error: |
Willy Tarreau | 388c0f2 | 2022-03-17 20:26:54 +0100 | [diff] [blame] | 1197 | free_act_rule(rule); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1198 | return -1; |
| 1199 | } |
| 1200 | |
| 1201 | |
| 1202 | /* This function should be called to parse a line starting with the "tcp-request" |
| 1203 | * keyword. |
| 1204 | */ |
| 1205 | static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 1206 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1207 | char **err) |
| 1208 | { |
| 1209 | const char *ptr = NULL; |
| 1210 | unsigned int val; |
| 1211 | int warn = 0; |
| 1212 | int arg; |
| 1213 | struct act_rule *rule; |
| 1214 | unsigned int where; |
| 1215 | const struct acl *acl; |
| 1216 | const char *kw; |
| 1217 | |
| 1218 | if (!*args[1]) { |
| 1219 | if (curpx == defpx) |
| 1220 | memprintf(err, "missing argument for '%s' in defaults section", args[0]); |
| 1221 | else |
| 1222 | memprintf(err, "missing argument for '%s' in %s '%s'", |
| 1223 | args[0], proxy_type_str(curpx), curpx->id); |
| 1224 | return -1; |
| 1225 | } |
| 1226 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1227 | if (strcmp(args[1], "inspect-delay") == 0) { |
Christopher Faulet | ee08d6c | 2021-10-13 15:40:15 +0200 | [diff] [blame] | 1228 | if (curpx == defpx && strlen(defpx->id) == 0) { |
| 1229 | memprintf(err, "%s %s is not allowed in anonymous 'defaults' sections", |
| 1230 | args[0], args[1]); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1231 | return -1; |
| 1232 | } |
| 1233 | |
| 1234 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
| 1235 | memprintf(err, |
| 1236 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
| 1237 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 1238 | |
| 1239 | if (ptr == PARSE_TIME_OVER) |
| 1240 | memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]); |
| 1241 | else if (ptr == PARSE_TIME_UNDER) |
| 1242 | memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]); |
| 1243 | else if (ptr) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1244 | memprintf(err, "%s (unexpected character '%c')", *err, *ptr); |
| 1245 | return -1; |
| 1246 | } |
| 1247 | |
| 1248 | if (curpx->tcp_req.inspect_delay) { |
| 1249 | memprintf(err, "ignoring %s %s (was already defined) in %s '%s'", |
| 1250 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1251 | return 1; |
| 1252 | } |
| 1253 | curpx->tcp_req.inspect_delay = val; |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
Willy Tarreau | d535f80 | 2021-10-11 08:49:26 +0200 | [diff] [blame] | 1257 | rule = new_act_rule(0, file, line); |
Remi Tricot-Le Breton | 2ca42b4 | 2021-05-12 18:24:18 +0200 | [diff] [blame] | 1258 | if (!rule) { |
| 1259 | memprintf(err, "parsing [%s:%d] : out of memory", file, line); |
| 1260 | return -1; |
| 1261 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1262 | LIST_INIT(&rule->list); |
| 1263 | arg = 1; |
| 1264 | where = 0; |
| 1265 | |
| 1266 | if (strcmp(args[1], "content") == 0) { |
| 1267 | arg++; |
| 1268 | |
| 1269 | if (curpx->cap & PR_CAP_FE) |
| 1270 | where |= SMP_VAL_FE_REQ_CNT; |
| 1271 | if (curpx->cap & PR_CAP_BE) |
| 1272 | where |= SMP_VAL_BE_REQ_CNT; |
Christopher Faulet | cb9106b | 2019-12-19 15:23:17 +0100 | [diff] [blame] | 1273 | rule->from = ACT_F_TCP_REQ_CNT; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1274 | if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0) |
| 1275 | goto error; |
| 1276 | |
| 1277 | acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL; |
| 1278 | if (acl) { |
| 1279 | if (acl->name && *acl->name) |
| 1280 | memprintf(err, |
| 1281 | "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'", |
| 1282 | acl->name, args[0], args[1], sample_ckp_names(where)); |
| 1283 | else |
| 1284 | memprintf(err, |
| 1285 | "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'", |
| 1286 | args[0], args[1], |
| 1287 | LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, |
| 1288 | sample_ckp_names(where)); |
| 1289 | |
| 1290 | warn++; |
| 1291 | } |
| 1292 | else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) { |
| 1293 | if (acl->name && *acl->name) |
| 1294 | memprintf(err, |
| 1295 | "acl '%s' involves keyword '%s' which is incompatible with '%s'", |
| 1296 | acl->name, kw, sample_ckp_names(where)); |
| 1297 | else |
| 1298 | memprintf(err, |
| 1299 | "anonymous acl involves keyword '%s' which is incompatible with '%s'", |
| 1300 | kw, sample_ckp_names(where)); |
| 1301 | warn++; |
| 1302 | } |
| 1303 | |
| 1304 | /* the following function directly emits the warning */ |
| 1305 | warnif_misplaced_tcp_cont(curpx, file, line, args[0]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1306 | LIST_APPEND(&curpx->tcp_req.inspect_rules, &rule->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1307 | } |
| 1308 | else if (strcmp(args[1], "connection") == 0) { |
| 1309 | arg++; |
| 1310 | |
| 1311 | if (!(curpx->cap & PR_CAP_FE)) { |
| 1312 | memprintf(err, "%s %s is not allowed because %s %s is not a frontend", |
| 1313 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1314 | goto error; |
| 1315 | } |
| 1316 | |
| 1317 | where |= SMP_VAL_FE_CON_ACC; |
Christopher Faulet | cb9106b | 2019-12-19 15:23:17 +0100 | [diff] [blame] | 1318 | rule->from = ACT_F_TCP_REQ_CON; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1319 | if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0) |
| 1320 | goto error; |
| 1321 | |
| 1322 | acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL; |
| 1323 | if (acl) { |
| 1324 | if (acl->name && *acl->name) |
| 1325 | memprintf(err, |
| 1326 | "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'", |
| 1327 | acl->name, args[0], args[1], sample_ckp_names(where)); |
| 1328 | else |
| 1329 | memprintf(err, |
| 1330 | "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'", |
| 1331 | args[0], args[1], |
| 1332 | LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, |
| 1333 | sample_ckp_names(where)); |
| 1334 | |
| 1335 | warn++; |
| 1336 | } |
| 1337 | else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) { |
| 1338 | if (acl->name && *acl->name) |
| 1339 | memprintf(err, |
| 1340 | "acl '%s' involves keyword '%s' which is incompatible with '%s'", |
| 1341 | acl->name, kw, sample_ckp_names(where)); |
| 1342 | else |
| 1343 | memprintf(err, |
| 1344 | "anonymous acl involves keyword '%s' which is incompatible with '%s'", |
| 1345 | kw, sample_ckp_names(where)); |
| 1346 | warn++; |
| 1347 | } |
| 1348 | |
| 1349 | /* the following function directly emits the warning */ |
| 1350 | warnif_misplaced_tcp_conn(curpx, file, line, args[0]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1351 | LIST_APPEND(&curpx->tcp_req.l4_rules, &rule->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1352 | } |
| 1353 | else if (strcmp(args[1], "session") == 0) { |
| 1354 | arg++; |
| 1355 | |
| 1356 | if (!(curpx->cap & PR_CAP_FE)) { |
| 1357 | memprintf(err, "%s %s is not allowed because %s %s is not a frontend", |
| 1358 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1359 | goto error; |
| 1360 | } |
| 1361 | |
| 1362 | where |= SMP_VAL_FE_SES_ACC; |
Christopher Faulet | cb9106b | 2019-12-19 15:23:17 +0100 | [diff] [blame] | 1363 | rule->from = ACT_F_TCP_REQ_SES; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1364 | if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0) |
| 1365 | goto error; |
| 1366 | |
| 1367 | acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL; |
| 1368 | if (acl) { |
| 1369 | if (acl->name && *acl->name) |
| 1370 | memprintf(err, |
| 1371 | "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'", |
| 1372 | acl->name, args[0], args[1], sample_ckp_names(where)); |
| 1373 | else |
| 1374 | memprintf(err, |
| 1375 | "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'", |
| 1376 | args[0], args[1], |
| 1377 | LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw, |
| 1378 | sample_ckp_names(where)); |
| 1379 | warn++; |
| 1380 | } |
| 1381 | else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) { |
| 1382 | if (acl->name && *acl->name) |
| 1383 | memprintf(err, |
| 1384 | "acl '%s' involves keyword '%s' which is incompatible with '%s'", |
| 1385 | acl->name, kw, sample_ckp_names(where)); |
| 1386 | else |
| 1387 | memprintf(err, |
| 1388 | "anonymous acl involves keyword '%s' which is incompatible with '%s'", |
| 1389 | kw, sample_ckp_names(where)); |
| 1390 | warn++; |
| 1391 | } |
| 1392 | |
| 1393 | /* the following function directly emits the warning */ |
| 1394 | warnif_misplaced_tcp_sess(curpx, file, line, args[0]); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1395 | LIST_APPEND(&curpx->tcp_req.l5_rules, &rule->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1396 | } |
| 1397 | else { |
| 1398 | if (curpx == defpx) |
| 1399 | memprintf(err, |
| 1400 | "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')", |
| 1401 | args[0], args[1]); |
| 1402 | else |
| 1403 | memprintf(err, |
| 1404 | "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')", |
| 1405 | args[0], proxy_type_str(curpx), curpx->id, args[1]); |
| 1406 | goto error; |
| 1407 | } |
| 1408 | |
| 1409 | return warn; |
| 1410 | error: |
Willy Tarreau | 388c0f2 | 2022-03-17 20:26:54 +0100 | [diff] [blame] | 1411 | free_act_rule(rule); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1412 | return -1; |
| 1413 | } |
| 1414 | |
| 1415 | static struct cfg_kw_list cfg_kws = {ILH, { |
| 1416 | { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req }, |
| 1417 | { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep }, |
| 1418 | { 0, NULL, NULL }, |
| 1419 | }}; |
| 1420 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 1421 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 1422 | |
| 1423 | /* |
| 1424 | * Local variables: |
| 1425 | * c-indent-level: 8 |
| 1426 | * c-basic-offset: 8 |
| 1427 | * End: |
| 1428 | */ |