blob: 1d1ce7d8cafd68d05a77bb47c8093b082d57571d [file] [log] [blame]
Willy Tarreau39713102016-11-25 15:49:32 +01001/*
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 Tarreaudcc048a2020-06-04 19:11:43 +020012#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020013#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <haproxy/arg-t.h>
Willy Tarreau278161c2020-06-04 11:18:28 +020016#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020017#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020018#include <haproxy/channel.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020019#include <haproxy/connection.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020020#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020021#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020022#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020023#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020024#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020025#include <haproxy/sc_strm.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020026#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/stick_table.h>
28#include <haproxy/stream-t.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020029#include <haproxy/tcp_rules.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020030#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/tools.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020032#include <haproxy/trace.h>
Willy Tarreau39713102016-11-25 15:49:32 +010033
Willy Tarreau39713102016-11-25 15:49:32 +010034
Christopher Fauleteea8fc72019-11-05 16:18:10 +010035#define TRACE_SOURCE &trace_strm
36
Willy Tarreau39713102016-11-25 15:49:32 +010037/* List head of all known action keywords for "tcp-request connection" */
38struct list tcp_req_conn_keywords = LIST_HEAD_INIT(tcp_req_conn_keywords);
39struct list tcp_req_sess_keywords = LIST_HEAD_INIT(tcp_req_sess_keywords);
40struct list tcp_req_cont_keywords = LIST_HEAD_INIT(tcp_req_cont_keywords);
41struct list tcp_res_cont_keywords = LIST_HEAD_INIT(tcp_res_cont_keywords);
42
43/*
44 * Register keywords.
45 */
46void tcp_req_conn_keywords_register(struct action_kw_list *kw_list)
47{
Willy Tarreau2b718102021-04-21 07:32:39 +020048 LIST_APPEND(&tcp_req_conn_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010049}
50
51void tcp_req_sess_keywords_register(struct action_kw_list *kw_list)
52{
Willy Tarreau2b718102021-04-21 07:32:39 +020053 LIST_APPEND(&tcp_req_sess_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010054}
55
56void tcp_req_cont_keywords_register(struct action_kw_list *kw_list)
57{
Willy Tarreau2b718102021-04-21 07:32:39 +020058 LIST_APPEND(&tcp_req_cont_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010059}
60
61void tcp_res_cont_keywords_register(struct action_kw_list *kw_list)
62{
Willy Tarreau2b718102021-04-21 07:32:39 +020063 LIST_APPEND(&tcp_res_cont_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010064}
65
66/*
67 * Return the struct tcp_req_action_kw associated to a keyword.
68 */
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010069struct action_kw *tcp_req_conn_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010070{
71 return action_lookup(&tcp_req_conn_keywords, kw);
72}
73
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010074struct action_kw *tcp_req_sess_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010075{
76 return action_lookup(&tcp_req_sess_keywords, kw);
77}
78
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010079struct action_kw *tcp_req_cont_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010080{
81 return action_lookup(&tcp_req_cont_keywords, kw);
82}
83
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010084struct action_kw *tcp_res_cont_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010085{
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 */
96int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
97{
Christopher Fauletc8016d02021-10-13 15:34:49 +020098 struct list *def_rules, *rules;
Willy Tarreau39713102016-11-25 15:49:32 +010099 struct session *sess = s->sess;
100 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100101 int partial;
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100102 int act_opts = 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100103
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100104 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100105
Christopher Fauletc8016d02021-10-13 15:34:49 +0200106 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 Tarreau39713102016-11-25 15:49:32 +0100109 /* 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 Faulet4b490b72023-01-04 11:55:15 +0100119 if ((req->flags & (CF_EOI|CF_SHUTR)) || channel_full(req, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +0200120 sc_waiting_room(chn_prod(req)) ||
Christopher Fauletd1c6cfe2022-06-10 16:48:47 +0200121 !s->be->tcp_req.inspect_delay || tick_is_expired(s->rules_exp, now_ms)) {
Willy Tarreau39713102016-11-25 15:49:32 +0100122 partial = SMP_OPT_FINAL;
Christopher Fauletd1c6cfe2022-06-10 16:48:47 +0200123 /* Action may yield while the inspect_delay is not expired and there is no read error */
Christopher Faulet2e56a732023-01-26 16:18:09 +0100124 if (sc_ep_test(s->scf, SE_FL_ERROR) || !s->be->tcp_req.inspect_delay || tick_is_expired(s->rules_exp, now_ms))
Christopher Fauletd1c6cfe2022-06-10 16:48:47 +0200125 act_opts |= ACT_OPT_FINAL;
126 }
Willy Tarreau39713102016-11-25 15:49:32 +0100127 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 Fauletc8016d02021-10-13 15:34:49 +0200138 if ((def_rules && s->current_rule_list == def_rules) || s->current_rule_list == rules)
Willy Tarreau39713102016-11-25 15:49:32 +0100139 goto resume_execution;
140 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200141 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Willy Tarreau39713102016-11-25 15:49:32 +0100142
Christopher Fauletc8016d02021-10-13 15:34:49 +0200143 restart:
144 list_for_each_entry(rule, s->current_rule_list, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100145 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 Faulet105ba6c2019-12-18 14:41:51 +0100158 act_opts |= ACT_OPT_FIRST;
Willy Tarreau39713102016-11-25 15:49:32 +0100159resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100160 /* Always call the action function if defined */
161 if (rule->action_ptr) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100162 switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100163 case ACT_RET_CONT:
164 break;
165 case ACT_RET_STOP:
166 case ACT_RET_DONE:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100167 s->last_rule_file = rule->conf.file;
168 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100169 goto end;
170 case ACT_RET_YIELD:
171 s->current_rule = rule;
Christopher Fauletd1c6cfe2022-06-10 16:48:47 +0200172 if (act_opts & ACT_OPT_FINAL) {
Christopher Faulet99aaca92020-07-28 11:30:19 +0200173 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 Fauletcd26e8a2019-12-18 11:13:39 +0100178 goto missing_data;
179 case ACT_RET_DENY:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100180 s->last_rule_file = rule->conf.file;
181 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100182 goto deny;
183 case ACT_RET_ABRT:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100184 s->last_rule_file = rule->conf.file;
185 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100186 goto abort;
187 case ACT_RET_ERR:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100188 s->last_rule_file = rule->conf.file;
189 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100190 goto internal;
191 case ACT_RET_INV:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100192 s->last_rule_file = rule->conf.file;
193 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100194 goto invalid;
195 }
196 continue; /* eval the next rule */
197 }
198
199 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100200 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreauc6dae862022-03-09 17:23:10 +0100201 s->last_rule_file = rule->conf.file;
202 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100203 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100204 }
205 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreauc6dae862022-03-09 17:23:10 +0100206 s->last_rule_file = rule->conf.file;
207 s->last_rule_line = rule->conf.line;
Christopher Faulet282992e2019-12-16 12:34:31 +0100208 goto deny;
Willy Tarreau39713102016-11-25 15:49:32 +0100209 }
Willy Tarreau39713102016-11-25 15:49:32 +0100210 }
211 }
212
Christopher Fauletc8016d02021-10-13 15:34:49 +0200213 if (def_rules && s->current_rule_list == def_rules) {
214 s->current_rule_list = rules;
215 goto restart;
216 }
217
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100218 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100219 /* 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 Fauletc8016d02021-10-13 15:34:49 +0200223 s->current_rule = s->current_rule_list = NULL;
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200224 req->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100225 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100226 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 Faulet2747fbb2020-07-28 11:56:13 +0200231 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 Fauleteea8fc72019-11-05 16:18:10 +0100234 DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100235 return 0;
236
Christopher Faulet282992e2019-12-16 12:34:31 +0100237 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +0200238 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100239 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200240 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100241 goto reject;
242
243 internal:
Willy Tarreau4781b152021-04-06 13:53:36 +0200244 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100245 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200246 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100247 if (!(s->flags & SF_ERR_MASK))
248 s->flags |= SF_ERR_INTERNAL;
249 goto reject;
250
251 invalid:
Willy Tarreau4781b152021-04-06 13:53:36 +0200252 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100253 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200254 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100255
256 reject:
Willy Tarreauf61dd192022-05-27 09:00:19 +0200257 sc_must_kill_conn(chn_prod(req));
Christopher Faulet282992e2019-12-16 12:34:31 +0100258 channel_abort(req);
259 channel_abort(&s->res);
260
261 abort:
262 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200263 s->current_rule = s->current_rule_list = NULL;
264 req->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Faulet282992e2019-12-16 12:34:31 +0100265
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 Tarreau39713102016-11-25 15:49:32 +0100272}
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 */
280int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
281{
Christopher Fauletc8016d02021-10-13 15:34:49 +0200282 struct list *def_rules, *rules;
Willy Tarreau39713102016-11-25 15:49:32 +0100283 struct session *sess = s->sess;
284 struct act_rule *rule;
285 int partial;
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100286 int act_opts = 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100287
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100288 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100289
Christopher Fauletc8016d02021-10-13 15:34:49 +0200290 def_rules = (s->be->defpx ? &s->be->defpx->tcp_rep.inspect_rules : NULL);
291 rules = &s->be->tcp_rep.inspect_rules;
292
Willy Tarreau39713102016-11-25 15:49:32 +0100293 /* 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 Faulet4b490b72023-01-04 11:55:15 +0100302 if ((rep->flags & (CF_EOI|CF_SHUTR)) || channel_full(rep, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +0200303 sc_waiting_room(chn_prod(rep)) ||
Christopher Fauletd1c6cfe2022-06-10 16:48:47 +0200304 !s->be->tcp_rep.inspect_delay || tick_is_expired(s->rules_exp, now_ms)) {
Willy Tarreau39713102016-11-25 15:49:32 +0100305 partial = SMP_OPT_FINAL;
Christopher Fauletd1c6cfe2022-06-10 16:48:47 +0200306 /* Action may yield while the inspect_delay is not expired and there is no read error */
Christopher Faulet2e56a732023-01-26 16:18:09 +0100307 if (sc_ep_test(s->scb, SE_FL_ERROR) || !s->be->tcp_rep.inspect_delay || tick_is_expired(s->rules_exp, now_ms))
Christopher Fauletd1c6cfe2022-06-10 16:48:47 +0200308 act_opts |= ACT_OPT_FINAL;
309 }
Willy Tarreau39713102016-11-25 15:49:32 +0100310 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 Fauletc8016d02021-10-13 15:34:49 +0200321 if ((def_rules && s->current_rule_list == def_rules) || s->current_rule_list == rules)
Willy Tarreau39713102016-11-25 15:49:32 +0100322 goto resume_execution;
323 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200324 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Willy Tarreau39713102016-11-25 15:49:32 +0100325
Christopher Fauletc8016d02021-10-13 15:34:49 +0200326 restart:
327 list_for_each_entry(rule, s->current_rule_list, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100328 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 Faulet2747fbb2020-07-28 11:56:13 +0200332 if (ret == ACL_TEST_MISS)
333 goto missing_data;
Willy Tarreau39713102016-11-25 15:49:32 +0100334
335 ret = acl_pass(ret);
336 if (rule->cond->pol == ACL_COND_UNLESS)
337 ret = !ret;
338 }
339
340 if (ret) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100341 act_opts |= ACT_OPT_FIRST;
Willy Tarreau39713102016-11-25 15:49:32 +0100342resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100343 /* Always call the action function if defined */
344 if (rule->action_ptr) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100345 switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100346 case ACT_RET_CONT:
347 break;
348 case ACT_RET_STOP:
349 case ACT_RET_DONE:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100350 s->last_rule_file = rule->conf.file;
351 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100352 goto end;
353 case ACT_RET_YIELD:
354 s->current_rule = rule;
Christopher Fauletd1c6cfe2022-06-10 16:48:47 +0200355 if (act_opts & ACT_OPT_FINAL) {
Christopher Faulet99aaca92020-07-28 11:30:19 +0200356 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 Faulet2747fbb2020-07-28 11:56:13 +0200361 channel_dont_close(rep);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100362 goto missing_data;
363 case ACT_RET_DENY:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100364 s->last_rule_file = rule->conf.file;
365 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100366 goto deny;
367 case ACT_RET_ABRT:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100368 s->last_rule_file = rule->conf.file;
369 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100370 goto abort;
371 case ACT_RET_ERR:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100372 s->last_rule_file = rule->conf.file;
373 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100374 goto internal;
375 case ACT_RET_INV:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100376 s->last_rule_file = rule->conf.file;
377 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100378 goto invalid;
379 }
380 continue; /* eval the next rule */
381 }
382
383 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100384 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreauc6dae862022-03-09 17:23:10 +0100385 s->last_rule_file = rule->conf.file;
386 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100387 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100388 }
389 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreauc6dae862022-03-09 17:23:10 +0100390 s->last_rule_file = rule->conf.file;
391 s->last_rule_line = rule->conf.line;
Christopher Faulet282992e2019-12-16 12:34:31 +0100392 goto deny;
Willy Tarreau39713102016-11-25 15:49:32 +0100393 }
394 else if (rule->action == ACT_TCP_CLOSE) {
Willy Tarreaucb041662022-05-17 19:44:42 +0200395 chn_prod(rep)->flags |= SC_FL_NOLINGER | SC_FL_NOHALF;
Willy Tarreauf61dd192022-05-27 09:00:19 +0200396 sc_must_kill_conn(chn_prod(rep));
397 sc_shutr(chn_prod(rep));
398 sc_shutw(chn_prod(rep));
Willy Tarreauc6dae862022-03-09 17:23:10 +0100399 s->last_rule_file = rule->conf.file;
400 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100401 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100402 }
403 }
404 }
405
Christopher Fauletc8016d02021-10-13 15:34:49 +0200406 if (def_rules && s->current_rule_list == def_rules) {
407 s->current_rule_list = rules;
408 goto restart;
409 }
410
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100411 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100412 /* 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 Fauletc8016d02021-10-13 15:34:49 +0200416 s->current_rule = s->current_rule_list = NULL;
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200417 rep->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100418 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100419 return 1;
Christopher Faulet282992e2019-12-16 12:34:31 +0100420
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100421 missing_data:
Christopher Faulet54f3e182020-07-29 12:00:23 +0200422 /* just set the analyser timeout once at the beginning of the response */
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200423 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 Fauletcd26e8a2019-12-18 11:13:39 +0100426 DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
427 return 0;
428
Christopher Faulet282992e2019-12-16 12:34:31 +0100429 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +0200430 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.denied_resp);
431 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +0100432 if (s->sess->listener && s->sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200433 _HA_ATOMIC_INC(&s->sess->listener->counters->denied_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100434 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200435 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100436 goto reject;
437
438 internal:
Willy Tarreau4781b152021-04-06 13:53:36 +0200439 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.internal_errors);
440 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100441 if (s->sess->listener && s->sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200442 _HA_ATOMIC_INC(&s->sess->listener->counters->internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100443 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200444 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100445 if (!(s->flags & SF_ERR_MASK))
446 s->flags |= SF_ERR_INTERNAL;
447 goto reject;
448
449 invalid:
Willy Tarreau4781b152021-04-06 13:53:36 +0200450 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100451 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200452 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100453
454 reject:
Willy Tarreauf61dd192022-05-27 09:00:19 +0200455 sc_must_kill_conn(chn_prod(rep));
Christopher Faulet282992e2019-12-16 12:34:31 +0100456 channel_abort(rep);
457 channel_abort(&s->req);
458
459 abort:
Christopher Faulet19dbf2d2020-07-28 11:40:07 +0200460 rep->analysers &= AN_RES_FLT_END;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200461 s->current_rule = s->current_rule_list = NULL;
462 rep->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Faulet282992e2019-12-16 12:34:31 +0100463
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 Tarreau39713102016-11-25 15:49:32 +0100470}
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 Tarreau4596fe22022-05-17 19:07:51 +0200476 * any data. This only works on connection-based client-facing stream connectors.
Willy Tarreau39713102016-11-25 15:49:32 +0100477 */
478int tcp_exec_l4_rules(struct session *sess)
479{
Christopher Fauletc8016d02021-10-13 15:34:49 +0200480 struct proxy *px = sess->fe;
Willy Tarreau39713102016-11-25 15:49:32 +0100481 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100482 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 Fauletc8016d02021-10-13 15:34:49 +0200489 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 Tarreau39713102016-11-25 15:49:32 +0100494 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 Fauletcd26e8a2019-12-18 11:13:39 +0100504 /* Always call the action function if defined */
505 if (rule->action_ptr) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100506 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_OPT_FINAL | ACT_OPT_FIRST)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100507 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 Tarreau39713102016-11-25 15:49:32 +0100530 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100531 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100532 }
533 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200534 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_conn);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100535 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200536 _HA_ATOMIC_INC(&sess->listener->counters->denied_conn);
Willy Tarreau39713102016-11-25 15:49:32 +0100537
538 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100539 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100540 }
Willy Tarreau39713102016-11-25 15:49:32 +0100541 else if (rule->action == ACT_TCP_EXPECT_PX) {
Willy Tarreau4450b582020-01-23 15:23:13 +0100542 if (!(conn->flags & CO_FL_HANDSHAKE)) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200543 if (xprt_add_hs(conn) < 0) {
544 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100545 goto end;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200546 }
547 }
Willy Tarreau39713102016-11-25 15:49:32 +0100548 conn->flags |= CO_FL_ACCEPT_PROXY;
Willy Tarreau39713102016-11-25 15:49:32 +0100549 }
550 else if (rule->action == ACT_TCP_EXPECT_CIP) {
Willy Tarreau4450b582020-01-23 15:23:13 +0100551 if (!(conn->flags & CO_FL_HANDSHAKE)) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200552 if (xprt_add_hs(conn) < 0) {
553 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100554 goto end;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200555 }
556 }
Willy Tarreau39713102016-11-25 15:49:32 +0100557 conn->flags |= CO_FL_ACCEPT_CIP;
Willy Tarreau39713102016-11-25 15:49:32 +0100558 }
Willy Tarreau39713102016-11-25 15:49:32 +0100559 }
560 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200561
562 if (px != sess->fe) {
563 px = sess->fe;
564 goto restart;
565 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100566 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100567 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 Tarreau4596fe22022-05-17 19:07:51 +0200573 * any data. This only works on session-based client-facing stream connectors.
Willy Tarreau39713102016-11-25 15:49:32 +0100574 * An example of valid use case is to track a stick-counter on the source
575 * address extracted from the proxy protocol.
576 */
577int tcp_exec_l5_rules(struct session *sess)
578{
Christopher Fauletc8016d02021-10-13 15:34:49 +0200579 struct proxy *px = sess->fe;
Willy Tarreau39713102016-11-25 15:49:32 +0100580 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100581 int result = 1;
582 enum acl_test_res ret;
583
Christopher Fauletc8016d02021-10-13 15:34:49 +0200584 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 Tarreau39713102016-11-25 15:49:32 +0100589 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 Fauletcd26e8a2019-12-18 11:13:39 +0100599 /* Always call the action function if defined */
600 if (rule->action_ptr) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100601 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_OPT_FINAL | ACT_OPT_FIRST)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100602 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 Tarreau39713102016-11-25 15:49:32 +0100625 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100626 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100627 }
628 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200629 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_sess);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100630 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200631 _HA_ATOMIC_INC(&sess->listener->counters->denied_sess);
Willy Tarreau39713102016-11-25 15:49:32 +0100632
633 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100634 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100635 }
Willy Tarreau39713102016-11-25 15:49:32 +0100636 }
637 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200638
639 if (px != sess->fe) {
640 px = sess->fe;
641 goto restart;
642 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100643 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100644 return result;
645}
646
647/* Parse a tcp-response rule. Return a negative value in case of failure */
648static int tcp_parse_response_rule(char **args, int arg, int section_type,
Willy Tarreau01825162021-03-09 09:53:46 +0100649 struct proxy *curpx, const struct proxy *defpx,
Willy Tarreau39713102016-11-25 15:49:32 +0100650 struct act_rule *rule, char **err,
651 unsigned int where,
652 const char *file, int line)
653{
Christopher Fauletee08d6c2021-10-13 15:40:15 +0200654 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 Tarreau39713102016-11-25 15:49:32 +0100656 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 Faulet245cf792019-12-18 14:58:12 +0100663 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100664 }
665 else if (strcmp(args[arg], "reject") == 0) {
666 arg++;
667 rule->action = ACT_ACTION_DENY;
Christopher Faulet245cf792019-12-18 14:58:12 +0100668 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100669 }
670 else if (strcmp(args[arg], "close") == 0) {
671 arg++;
672 rule->action = ACT_TCP_CLOSE;
Christopher Faulet245cf792019-12-18 14:58:12 +0100673 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100674 }
675 else {
676 struct action_kw *kw;
677 kw = tcp_res_cont_action(args[arg]);
678 if (kw) {
679 arg++;
Willy Tarreau39713102016-11-25 15:49:32 +0100680 rule->kw = kw;
681 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
682 return -1;
683 } else {
Willy Tarreaudb67b0e2021-03-12 13:46:10 +0100684 const char *extra[] = { "accept", "reject", "close", NULL };
685 const char *best = action_suggest(args[arg], &tcp_res_cont_keywords, extra);
686
Willy Tarreau39713102016-11-25 15:49:32 +0100687 action_build_list(&tcp_res_cont_keywords, &trash);
688 memprintf(err,
Willy Tarreaudb67b0e2021-03-12 13:46:10 +0100689 "'%s %s' expects 'accept', 'close', 'reject', %s in %s '%s' (got '%s').%s%s%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200690 args[0], args[1], trash.area,
Willy Tarreaudb67b0e2021-03-12 13:46:10 +0100691 proxy_type_str(curpx), curpx->id, args[arg],
692 best ? " Did you mean '" : "",
693 best ? best : "",
694 best ? "' maybe ?" : "");
Willy Tarreau39713102016-11-25 15:49:32 +0100695 return -1;
696 }
697 }
698
699 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +0200700 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +0100701 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 Fauletac98d812019-12-18 09:20:16 +0100716
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 */
721static 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 Faulet67307792020-02-10 09:54:49 +0100730 opt = SMP_OPT_DIR_REQ;
Christopher Fauletac98d812019-12-18 09:20:16 +0100731 if (flags & ACT_FLAG_FINAL)
732 opt |= SMP_OPT_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100733
Christopher Fauletac98d812019-12-18 09:20:16 +0100734 t = rule->arg.trk_ctr.table.t;
Christopher Faulet67307792020-02-10 09:54:49 +0100735 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 Fauletac98d812019-12-18 09:20:16 +0100738
Christopher Faulet67307792020-02-10 09:54:49 +0100739 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, &smp);
Christopher Fauletac98d812019-12-18 09:20:16 +0100740
Christopher Faulet67307792020-02-10 09:54:49 +0100741 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 Fauletac98d812019-12-18 09:20:16 +0100746 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 Faulet67307792020-02-10 09:54:49 +0100751 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 Fauletac98d812019-12-18 09:20:16 +0100759
760 end:
761 return ACT_RET_CONT;
762}
Willy Tarreau39713102016-11-25 15:49:32 +0100763
Christopher Fauletd73b96d2019-12-19 17:27:03 +0100764/* 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 */
769static 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 Fauletadfc6e82020-01-14 15:05:33 +0100805static void release_tcp_capture(struct act_rule * rule)
806{
807 release_sample_expr(rule->arg.cap.expr);
808}
809
810
811static void release_tcp_track_sc(struct act_rule * rule)
812{
813 release_sample_expr(rule->arg.trk_ctr.expr);
814}
815
Willy Tarreau39713102016-11-25 15:49:32 +0100816/* Parse a tcp-request rule. Return a negative value in case of failure */
817static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau01825162021-03-09 09:53:46 +0100818 struct proxy *curpx, const struct proxy *defpx,
Willy Tarreau39713102016-11-25 15:49:32 +0100819 struct act_rule *rule, char **err,
820 unsigned int where, const char *file, int line)
821{
Christopher Fauletee08d6c2021-10-13 15:40:15 +0200822 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 Tarreau39713102016-11-25 15:49:32 +0100825 return -1;
826 }
827
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100828 if (strcmp(args[arg], "accept") == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100829 arg++;
830 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100831 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100832 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100833 else if (strcmp(args[arg], "reject") == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100834 arg++;
835 rule->action = ACT_ACTION_DENY;
Christopher Faulet245cf792019-12-18 14:58:12 +0100836 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100837 }
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 Tarreaue3b57bf2020-02-14 16:50:14 +0100861 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args, NULL);
Willy Tarreau39713102016-11-25 15:49:32 +0100862 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 Fauletfdb6fbf2020-01-14 15:05:56 +0100873 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100874 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 Fauletfdb6fbf2020-01-14 15:05:56 +0100883 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100884 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 Fauletfdb6fbf2020-01-14 15:05:56 +0100892 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100893 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 Breton8cb03362021-05-17 10:08:16 +0200907 if (!hdr) {
908 memprintf(err, "parsing [%s:%d] : out of memory", file, line);
909 release_sample_expr(expr);
910 return -1;
911 }
Willy Tarreau39713102016-11-25 15:49:32 +0100912 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 Faulet711ed6a2019-07-16 14:16:10 +0200922 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreau39713102016-11-25 15:49:32 +0100923 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
924
925 rule->arg.cap.expr = expr;
926 rule->arg.cap.hdr = hdr;
Christopher Fauletd73b96d2019-12-19 17:27:03 +0100927 rule->action = ACT_CUSTOM;
928 rule->action_ptr = tcp_action_capture;
929 rule->check_ptr = check_capture;
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100930 rule->release_ptr = release_tcp_capture;
Willy Tarreau39713102016-11-25 15:49:32 +0100931 }
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100932 else if (strncmp(args[arg], "track-sc", 8) == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100933 struct sample_expr *expr;
934 int kw = arg;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100935 unsigned int tsc_num;
936 const char *tsc_num_str;
Willy Tarreau39713102016-11-25 15:49:32 +0100937
938 arg++;
939
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100940 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 Tarreau39713102016-11-25 15:49:32 +0100946 curpx->conf.args.ctx = ARGC_TRK;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100947 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args, NULL);
Willy Tarreau39713102016-11-25 15:49:32 +0100948 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 Fauletfdb6fbf2020-01-14 15:05:56 +0100959 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100960 return -1;
961 }
962
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200963 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreau39713102016-11-25 15:49:32 +0100964 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 Fauletfdb6fbf2020-01-14 15:05:56 +0100972 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100973 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 Fauletac98d812019-12-18 09:20:16 +0100979 rule->action = tsc_num;
Willy Tarreau39713102016-11-25 15:49:32 +0100980 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +0100981 rule->action_ptr = tcp_action_track_sc;
Christopher Faulet78880fb2017-09-18 14:43:55 +0200982 rule->check_ptr = check_trk_action;
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100983 rule->release_ptr = release_tcp_track_sc;
Willy Tarreau39713102016-11-25 15:49:32 +0100984 }
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 Tarreau39713102016-11-25 15:49:32 +01001027 } else if (where & SMP_VAL_FE_SES_ACC) {
1028 /* L5 */
1029 kw = tcp_req_sess_action(args[arg]);
1030 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +01001031 } else {
1032 /* L6 */
1033 kw = tcp_req_cont_action(args[arg]);
1034 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +01001035 }
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 Fauleta561ffb2021-03-19 08:53:26 +01001041 const char *extra[] = { "accept", "reject", "capture", "track-sc", "expect-proxy", "expect-netscaler-cip", NULL };
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001042 const char *best = NULL;
1043
1044
1045 if (where & SMP_VAL_FE_CON_ACC) {
Willy Tarreau39713102016-11-25 15:49:32 +01001046 action_build_list(&tcp_req_conn_keywords, &trash);
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001047 best = action_suggest(args[arg], &tcp_req_conn_keywords, extra);
1048 }
1049 else if (where & SMP_VAL_FE_SES_ACC) {
Willy Tarreau39713102016-11-25 15:49:32 +01001050 action_build_list(&tcp_req_sess_keywords, &trash);
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001051 best = action_suggest(args[arg], &tcp_req_sess_keywords, extra);
1052 }
1053 else {
Willy Tarreau39713102016-11-25 15:49:32 +01001054 action_build_list(&tcp_req_cont_keywords, &trash);
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001055 best = action_suggest(args[arg], &tcp_req_cont_keywords, extra);
1056 }
1057
Willy Tarreau39713102016-11-25 15:49:32 +01001058 memprintf(err,
Christopher Fauleta561ffb2021-03-19 08:53:26 +01001059 "'%s %s' expects 'accept', 'reject', 'capture', 'expect-proxy', 'expect-netscaler-cip', 'track-sc0' ... 'track-sc%d', %s "
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001060 "in %s '%s' (got '%s').%s%s%s\n",
Willy Tarreau6c011712023-01-06 16:09:58 +01001061 args[0], args[1], global.tune.nb_stk_ctr-1,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001062 trash.area, proxy_type_str(curpx),
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001063 curpx->id, args[arg],
1064 best ? " Did you mean '" : "",
1065 best ? best : "",
1066 best ? "' maybe ?" : "");
Willy Tarreau39713102016-11-25 15:49:32 +01001067 return -1;
1068 }
1069 }
1070
1071 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +02001072 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +01001073 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 */
1091static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001092 const struct proxy *defpx, const char *file, int line,
Willy Tarreau39713102016-11-25 15:49:32 +01001093 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 Fauletee08d6c2021-10-13 15:40:15 +02001111 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 Tarreau39713102016-11-25 15:49:32 +01001113 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 Tarreau9faebe32019-06-07 19:00:37 +02001121
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 Tarreau39713102016-11-25 15:49:32 +01001127 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 Tarreaud535f802021-10-11 08:49:26 +02001140 rule = new_act_rule(ACT_F_TCP_RES_CNT, file, line);
Remi Tricot-Le Breton2ca42b42021-05-12 18:24:18 +02001141 if (!rule) {
1142 memprintf(err, "parsing [%s:%d] : out of memory", file, line);
1143 return -1;
1144 }
Willy Tarreau39713102016-11-25 15:49:32 +01001145 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 Tarreau39713102016-11-25 15:49:32 +01001156 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 Tarreau2b718102021-04-21 07:32:39 +02001186 LIST_APPEND(&curpx->tcp_rep.inspect_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001187 }
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 Tarreau388c0f22022-03-17 20:26:54 +01001197 free_act_rule(rule);
Willy Tarreau39713102016-11-25 15:49:32 +01001198 return -1;
1199}
1200
1201
1202/* This function should be called to parse a line starting with the "tcp-request"
1203 * keyword.
1204 */
1205static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001206 const struct proxy *defpx, const char *file, int line,
Willy Tarreau39713102016-11-25 15:49:32 +01001207 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 Duesterhuse5ff1412021-01-02 22:31:53 +01001227 if (strcmp(args[1], "inspect-delay") == 0) {
Christopher Fauletee08d6c2021-10-13 15:40:15 +02001228 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 Tarreau39713102016-11-25 15:49:32 +01001231 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 Tarreau9faebe32019-06-07 19:00:37 +02001238
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 Tarreau39713102016-11-25 15:49:32 +01001244 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 Tarreaud535f802021-10-11 08:49:26 +02001257 rule = new_act_rule(0, file, line);
Remi Tricot-Le Breton2ca42b42021-05-12 18:24:18 +02001258 if (!rule) {
1259 memprintf(err, "parsing [%s:%d] : out of memory", file, line);
1260 return -1;
1261 }
Willy Tarreau39713102016-11-25 15:49:32 +01001262 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 Fauletcb9106b2019-12-19 15:23:17 +01001273 rule->from = ACT_F_TCP_REQ_CNT;
Willy Tarreau39713102016-11-25 15:49:32 +01001274 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 Tarreau2b718102021-04-21 07:32:39 +02001306 LIST_APPEND(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001307 }
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 Fauletcb9106b2019-12-19 15:23:17 +01001318 rule->from = ACT_F_TCP_REQ_CON;
Willy Tarreau39713102016-11-25 15:49:32 +01001319 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 Tarreau2b718102021-04-21 07:32:39 +02001351 LIST_APPEND(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001352 }
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 Fauletcb9106b2019-12-19 15:23:17 +01001363 rule->from = ACT_F_TCP_REQ_SES;
Willy Tarreau39713102016-11-25 15:49:32 +01001364 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 Tarreau2b718102021-04-21 07:32:39 +02001395 LIST_APPEND(&curpx->tcp_req.l5_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001396 }
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 Tarreau388c0f22022-03-17 20:26:54 +01001411 free_act_rule(rule);
Willy Tarreau39713102016-11-25 15:49:32 +01001412 return -1;
1413}
1414
1415static 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 Tarreau0108d902018-11-25 19:14:37 +01001421INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau39713102016-11-25 15:49:32 +01001422
1423/*
1424 * Local variables:
1425 * c-indent-level: 8
1426 * c-basic-offset: 8
1427 * End:
1428 */