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> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 20 | #include <haproxy/global.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 21 | #include <haproxy/list.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 22 | #include <haproxy/log.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 23 | #include <haproxy/proxy.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 24 | #include <haproxy/sample.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 25 | #include <haproxy/sc_strm.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 26 | #include <haproxy/stconn.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 | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 29 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | c2f7c58 | 2020-06-02 18:15:32 +0200 | [diff] [blame] | 30 | #include <haproxy/ticks.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 31 | #include <haproxy/tools.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 32 | #include <haproxy/trace.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 33 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 34 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 35 | #define TRACE_SOURCE &trace_strm |
| 36 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 37 | /* List head of all known action keywords for "tcp-request connection" */ |
| 38 | struct list tcp_req_conn_keywords = LIST_HEAD_INIT(tcp_req_conn_keywords); |
| 39 | struct list tcp_req_sess_keywords = LIST_HEAD_INIT(tcp_req_sess_keywords); |
| 40 | struct list tcp_req_cont_keywords = LIST_HEAD_INIT(tcp_req_cont_keywords); |
| 41 | struct list tcp_res_cont_keywords = LIST_HEAD_INIT(tcp_res_cont_keywords); |
| 42 | |
| 43 | /* |
| 44 | * Register keywords. |
| 45 | */ |
| 46 | void tcp_req_conn_keywords_register(struct action_kw_list *kw_list) |
| 47 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 48 | LIST_APPEND(&tcp_req_conn_keywords, &kw_list->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void tcp_req_sess_keywords_register(struct action_kw_list *kw_list) |
| 52 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 53 | LIST_APPEND(&tcp_req_sess_keywords, &kw_list->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void tcp_req_cont_keywords_register(struct action_kw_list *kw_list) |
| 57 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 58 | LIST_APPEND(&tcp_req_cont_keywords, &kw_list->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void tcp_res_cont_keywords_register(struct action_kw_list *kw_list) |
| 62 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 63 | LIST_APPEND(&tcp_res_cont_keywords, &kw_list->list); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Return the struct tcp_req_action_kw associated to a keyword. |
| 68 | */ |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 69 | struct action_kw *tcp_req_conn_action(const char *kw) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 70 | { |
| 71 | return action_lookup(&tcp_req_conn_keywords, kw); |
| 72 | } |
| 73 | |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 74 | struct action_kw *tcp_req_sess_action(const char *kw) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 75 | { |
| 76 | return action_lookup(&tcp_req_sess_keywords, kw); |
| 77 | } |
| 78 | |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 79 | struct action_kw *tcp_req_cont_action(const char *kw) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 80 | { |
| 81 | return action_lookup(&tcp_req_cont_keywords, kw); |
| 82 | } |
| 83 | |
Thierry Fournier | 7a71a6d | 2020-11-28 17:40:24 +0100 | [diff] [blame] | 84 | struct action_kw *tcp_res_cont_action(const char *kw) |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 85 | { |
| 86 | return action_lookup(&tcp_res_cont_keywords, kw); |
| 87 | } |
| 88 | |
| 89 | /* This function performs the TCP request analysis on the current request. It |
| 90 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 91 | * needs more data, encounters an error, or wants to immediately abort the |
| 92 | * request. It relies on buffers flags, and updates s->req->analysers. The |
| 93 | * function may be called for frontend rules and backend rules. It only relies |
| 94 | * on the backend pointer so this works for both cases. |
| 95 | */ |
| 96 | int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit) |
| 97 | { |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 98 | struct list *def_rules, *rules; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 99 | struct session *sess = s->sess; |
| 100 | struct act_rule *rule; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 101 | int partial; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 102 | int act_opts = 0; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 103 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 104 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 105 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 106 | 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); |
| 107 | rules = &s->be->tcp_req.inspect_rules; |
| 108 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 109 | /* We don't know whether we have enough data, so must proceed |
| 110 | * this way : |
| 111 | * - iterate through all rules in their declaration order |
| 112 | * - if one rule returns MISS, it means the inspect delay is |
| 113 | * not over yet, then return immediately, otherwise consider |
| 114 | * it as a non-match. |
| 115 | * - if one rule returns OK, then return OK |
| 116 | * - if one rule returns KO, then return KO |
| 117 | */ |
| 118 | |
Christopher Faulet | 4b490b7 | 2023-01-04 11:55:15 +0100 | [diff] [blame] | 119 | if ((req->flags & (CF_EOI|CF_SHUTR)) || channel_full(req, global.tune.maxrewrite) || |
Willy Tarreau | 99615ed | 2022-05-25 07:29:36 +0200 | [diff] [blame] | 120 | sc_waiting_room(chn_prod(req)) || |
Christopher Faulet | d1c6cfe | 2022-06-10 16:48:47 +0200 | [diff] [blame] | 121 | !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] | 122 | partial = SMP_OPT_FINAL; |
Christopher Faulet | d1c6cfe | 2022-06-10 16:48:47 +0200 | [diff] [blame] | 123 | /* Action may yield while the inspect_delay is not expired and there is no read error */ |
| 124 | if ((req->flags & CF_READ_ERROR) || !s->be->tcp_req.inspect_delay || tick_is_expired(s->rules_exp, now_ms)) |
| 125 | act_opts |= ACT_OPT_FINAL; |
| 126 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 127 | else |
| 128 | partial = 0; |
| 129 | |
| 130 | /* If "the current_rule_list" match the executed rule list, we are in |
| 131 | * resume condition. If a resume is needed it is always in the action |
| 132 | * and never in the ACL or converters. In this case, we initialise the |
| 133 | * current rule, and go to the action execution point. |
| 134 | */ |
| 135 | if (s->current_rule) { |
| 136 | rule = s->current_rule; |
| 137 | s->current_rule = NULL; |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 138 | 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] | 139 | goto resume_execution; |
| 140 | } |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 141 | 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] | 142 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 143 | restart: |
| 144 | list_for_each_entry(rule, s->current_rule_list, list) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 145 | enum acl_test_res ret = ACL_TEST_PASS; |
| 146 | |
| 147 | if (rule->cond) { |
| 148 | ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial); |
| 149 | if (ret == ACL_TEST_MISS) |
| 150 | goto missing_data; |
| 151 | |
| 152 | ret = acl_pass(ret); |
| 153 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 154 | ret = !ret; |
| 155 | } |
| 156 | |
| 157 | if (ret) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 158 | act_opts |= ACT_OPT_FIRST; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 159 | resume_execution: |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 160 | /* Always call the action function if defined */ |
| 161 | if (rule->action_ptr) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 162 | switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) { |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 163 | case ACT_RET_CONT: |
| 164 | break; |
| 165 | case ACT_RET_STOP: |
| 166 | case ACT_RET_DONE: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 167 | s->last_rule_file = rule->conf.file; |
| 168 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 169 | goto end; |
| 170 | case ACT_RET_YIELD: |
| 171 | s->current_rule = rule; |
Christopher Faulet | d1c6cfe | 2022-06-10 16:48:47 +0200 | [diff] [blame] | 172 | if (act_opts & ACT_OPT_FINAL) { |
Christopher Faulet | 99aaca9 | 2020-07-28 11:30:19 +0200 | [diff] [blame] | 173 | send_log(s->be, LOG_WARNING, |
| 174 | "Internal error: yield not allowed if the inspect-delay expired " |
| 175 | "for the tcp-request content actions."); |
| 176 | goto internal; |
| 177 | } |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 178 | goto missing_data; |
| 179 | case ACT_RET_DENY: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 180 | s->last_rule_file = rule->conf.file; |
| 181 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 182 | goto deny; |
| 183 | case ACT_RET_ABRT: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 184 | s->last_rule_file = rule->conf.file; |
| 185 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 186 | goto abort; |
| 187 | case ACT_RET_ERR: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 188 | s->last_rule_file = rule->conf.file; |
| 189 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 190 | goto internal; |
| 191 | case ACT_RET_INV: |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 192 | s->last_rule_file = rule->conf.file; |
| 193 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 194 | goto invalid; |
| 195 | } |
| 196 | continue; /* eval the next rule */ |
| 197 | } |
| 198 | |
| 199 | /* If not action function defined, check for known actions */ |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 200 | if (rule->action == ACT_ACTION_ALLOW) { |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 201 | s->last_rule_file = rule->conf.file; |
| 202 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 203 | goto end; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 204 | } |
| 205 | else if (rule->action == ACT_ACTION_DENY) { |
Willy Tarreau | c6dae86 | 2022-03-09 17:23:10 +0100 | [diff] [blame] | 206 | s->last_rule_file = rule->conf.file; |
| 207 | s->last_rule_line = rule->conf.line; |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 208 | goto deny; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 209 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 213 | if (def_rules && s->current_rule_list == def_rules) { |
| 214 | s->current_rule_list = rules; |
| 215 | goto restart; |
| 216 | } |
| 217 | |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 218 | end: |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 219 | /* if we get there, it means we have no rule which matches, or |
| 220 | * we have an explicit accept, so we apply the default accept. |
| 221 | */ |
| 222 | req->analysers &= ~an_bit; |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 223 | s->current_rule = s->current_rule_list = NULL; |
Christopher Faulet | 2747fbb | 2020-07-28 11:56:13 +0200 | [diff] [blame] | 224 | req->analyse_exp = s->rules_exp = TICK_ETERNITY; |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 225 | DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 226 | return 1; |
| 227 | |
| 228 | missing_data: |
| 229 | channel_dont_connect(req); |
| 230 | /* just set the request timeout once at the beginning of the request */ |
Christopher Faulet | 2747fbb | 2020-07-28 11:56:13 +0200 | [diff] [blame] | 231 | if (!tick_isset(s->rules_exp) && s->be->tcp_req.inspect_delay) |
| 232 | s->rules_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay); |
| 233 | 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] | 234 | 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] | 235 | return 0; |
| 236 | |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 237 | deny: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 238 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 239 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 240 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 241 | goto reject; |
| 242 | |
| 243 | internal: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 244 | _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 245 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 246 | _HA_ATOMIC_INC(&sess->listener->counters->internal_errors); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 247 | if (!(s->flags & SF_ERR_MASK)) |
| 248 | s->flags |= SF_ERR_INTERNAL; |
| 249 | goto reject; |
| 250 | |
| 251 | invalid: |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 252 | _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 253 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 254 | _HA_ATOMIC_INC(&sess->listener->counters->failed_req); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 255 | |
| 256 | reject: |
Willy Tarreau | f61dd19 | 2022-05-27 09:00:19 +0200 | [diff] [blame] | 257 | sc_must_kill_conn(chn_prod(req)); |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 258 | channel_abort(req); |
| 259 | channel_abort(&s->res); |
| 260 | |
| 261 | abort: |
| 262 | req->analysers &= AN_REQ_FLT_END; |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 263 | s->current_rule = s->current_rule_list = NULL; |
| 264 | req->analyse_exp = s->rules_exp = TICK_ETERNITY; |
Christopher Faulet | 282992e | 2019-12-16 12:34:31 +0100 | [diff] [blame] | 265 | |
| 266 | if (!(s->flags & SF_ERR_MASK)) |
| 267 | s->flags |= SF_ERR_PRXCOND; |
| 268 | if (!(s->flags & SF_FINST_MASK)) |
| 269 | s->flags |= SF_FINST_R; |
| 270 | DBG_TRACE_DEVEL("leaving on error|deny|abort", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_TCP_ERR, s); |
| 271 | return 0; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* This function performs the TCP response analysis on the current response. It |
| 275 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 276 | * needs more data, encounters an error, or wants to immediately abort the |
| 277 | * response. It relies on buffers flags, and updates s->rep->analysers. The |
| 278 | * function may be called for backend rules. |
| 279 | */ |
| 280 | int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit) |
| 281 | { |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 282 | struct list *def_rules, *rules; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 283 | struct session *sess = s->sess; |
| 284 | struct act_rule *rule; |
| 285 | int partial; |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 286 | int act_opts = 0; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 287 | |
Christopher Faulet | eea8fc7 | 2019-11-05 16:18:10 +0100 | [diff] [blame] | 288 | DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s); |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 289 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 290 | def_rules = (s->be->defpx ? &s->be->defpx->tcp_rep.inspect_rules : NULL); |
| 291 | rules = &s->be->tcp_rep.inspect_rules; |
| 292 | |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 293 | /* We don't know whether we have enough data, so must proceed |
| 294 | * this way : |
| 295 | * - iterate through all rules in their declaration order |
| 296 | * - if one rule returns MISS, it means the inspect delay is |
| 297 | * not over yet, then return immediately, otherwise consider |
| 298 | * it as a non-match. |
| 299 | * - if one rule returns OK, then return OK |
| 300 | * - if one rule returns KO, then return KO |
| 301 | */ |
Christopher Faulet | 4b490b7 | 2023-01-04 11:55:15 +0100 | [diff] [blame] | 302 | if ((rep->flags & (CF_EOI|CF_SHUTR)) || channel_full(rep, global.tune.maxrewrite) || |
Willy Tarreau | 99615ed | 2022-05-25 07:29:36 +0200 | [diff] [blame] | 303 | sc_waiting_room(chn_prod(rep)) || |
Christopher Faulet | d1c6cfe | 2022-06-10 16:48:47 +0200 | [diff] [blame] | 304 | !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] | 305 | partial = SMP_OPT_FINAL; |
Christopher Faulet | d1c6cfe | 2022-06-10 16:48:47 +0200 | [diff] [blame] | 306 | /* Action may yield while the inspect_delay is not expired and there is no read error */ |
| 307 | if ((rep->flags & CF_READ_ERROR) || !s->be->tcp_rep.inspect_delay || tick_is_expired(s->rules_exp, now_ms)) |
| 308 | act_opts |= ACT_OPT_FINAL; |
| 309 | } |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 310 | else |
| 311 | partial = 0; |
| 312 | |
| 313 | /* If "the current_rule_list" match the executed rule list, we are in |
| 314 | * resume condition. If a resume is needed it is always in the action |
| 315 | * and never in the ACL or converters. In this case, we initialise the |
| 316 | * current rule, and go to the action execution point. |
| 317 | */ |
| 318 | if (s->current_rule) { |
| 319 | rule = s->current_rule; |
| 320 | s->current_rule = NULL; |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 321 | 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] | 322 | goto resume_execution; |
| 323 | } |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 324 | 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] | 325 | |
Christopher Faulet | c8016d0 | 2021-10-13 15:34:49 +0200 | [diff] [blame] | 326 | restart: |
| 327 | list_for_each_entry(rule, s->current_rule_list, list) { |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 328 | enum acl_test_res ret = ACL_TEST_PASS; |
| 329 | |
| 330 | if (rule->cond) { |
| 331 | 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] | 332 | if (ret == ACL_TEST_MISS) |
| 333 | goto missing_data; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 334 | |
| 335 | ret = acl_pass(ret); |
| 336 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 337 | ret = !ret; |
| 338 | } |
| 339 | |
| 340 | if (ret) { |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 341 | act_opts |= ACT_OPT_FIRST; |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 342 | resume_execution: |
Christopher Faulet | cd26e8a | 2019-12-18 11:13:39 +0100 | [diff] [blame] | 343 | /* Always call the action function if defined */ |
| 344 | if (rule->action_ptr) { |
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 | d1c6cfe | 2022-06-10 16:48:47 +0200 | [diff] [blame] | 355 | if (act_opts & ACT_OPT_FINAL) { |
Christopher Faulet | 99aaca9 | 2020-07-28 11:30:19 +0200 | [diff] [blame] | 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) { |
Willy Tarreau | cb04166 | 2022-05-17 19:44:42 +0200 | [diff] [blame] | 395 | chn_prod(rep)->flags |= SC_FL_NOLINGER | SC_FL_NOHALF; |
Willy Tarreau | f61dd19 | 2022-05-27 09:00:19 +0200 | [diff] [blame] | 396 | sc_must_kill_conn(chn_prod(rep)); |
| 397 | sc_shutr(chn_prod(rep)); |
| 398 | sc_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: |
Willy Tarreau | f61dd19 | 2022-05-27 09:00:19 +0200 | [diff] [blame] | 455 | sc_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 |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 476 | * any data. This only works on connection-based client-facing stream connectors. |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 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 |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 573 | * any data. This only works on session-based client-facing stream connectors. |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 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 | 6c01171 | 2023-01-06 16:09:58 +0100 | [diff] [blame] | 1061 | args[0], args[1], global.tune.nb_stk_ctr-1, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 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 | */ |