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