blob: bce62f0090f172d5e9960d7b0a4600a649d015ea [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>
Christopher Faulet9a521232022-03-30 14:42:50 +020020#include <haproxy/conn_stream.h>
21#include <haproxy/cs_utils.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020022#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020023#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020024#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020025#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020026#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/stick_table.h>
28#include <haproxy/stream-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020029#include <haproxy/stream_interface.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020030#include <haproxy/tcp_rules.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020031#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/tools.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/trace.h>
Willy Tarreau39713102016-11-25 15:49:32 +010034
Willy Tarreau39713102016-11-25 15:49:32 +010035
Christopher Fauleteea8fc72019-11-05 16:18:10 +010036#define TRACE_SOURCE &trace_strm
37
Willy Tarreau39713102016-11-25 15:49:32 +010038/* List head of all known action keywords for "tcp-request connection" */
39struct list tcp_req_conn_keywords = LIST_HEAD_INIT(tcp_req_conn_keywords);
40struct list tcp_req_sess_keywords = LIST_HEAD_INIT(tcp_req_sess_keywords);
41struct list tcp_req_cont_keywords = LIST_HEAD_INIT(tcp_req_cont_keywords);
42struct list tcp_res_cont_keywords = LIST_HEAD_INIT(tcp_res_cont_keywords);
43
44/*
45 * Register keywords.
46 */
47void tcp_req_conn_keywords_register(struct action_kw_list *kw_list)
48{
Willy Tarreau2b718102021-04-21 07:32:39 +020049 LIST_APPEND(&tcp_req_conn_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010050}
51
52void tcp_req_sess_keywords_register(struct action_kw_list *kw_list)
53{
Willy Tarreau2b718102021-04-21 07:32:39 +020054 LIST_APPEND(&tcp_req_sess_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010055}
56
57void tcp_req_cont_keywords_register(struct action_kw_list *kw_list)
58{
Willy Tarreau2b718102021-04-21 07:32:39 +020059 LIST_APPEND(&tcp_req_cont_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010060}
61
62void tcp_res_cont_keywords_register(struct action_kw_list *kw_list)
63{
Willy Tarreau2b718102021-04-21 07:32:39 +020064 LIST_APPEND(&tcp_res_cont_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010065}
66
67/*
68 * Return the struct tcp_req_action_kw associated to a keyword.
69 */
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010070struct action_kw *tcp_req_conn_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010071{
72 return action_lookup(&tcp_req_conn_keywords, kw);
73}
74
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010075struct action_kw *tcp_req_sess_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010076{
77 return action_lookup(&tcp_req_sess_keywords, kw);
78}
79
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010080struct action_kw *tcp_req_cont_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010081{
82 return action_lookup(&tcp_req_cont_keywords, kw);
83}
84
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010085struct action_kw *tcp_res_cont_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010086{
87 return action_lookup(&tcp_res_cont_keywords, kw);
88}
89
90/* This function performs the TCP request analysis on the current request. It
91 * returns 1 if the processing can continue on next analysers, or zero if it
92 * needs more data, encounters an error, or wants to immediately abort the
93 * request. It relies on buffers flags, and updates s->req->analysers. The
94 * function may be called for frontend rules and backend rules. It only relies
95 * on the backend pointer so this works for both cases.
96 */
97int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
98{
Christopher Fauletc8016d02021-10-13 15:34:49 +020099 struct list *def_rules, *rules;
Willy Tarreau39713102016-11-25 15:49:32 +0100100 struct session *sess = s->sess;
101 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100102 int partial;
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100103 int act_opts = 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100104
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100105 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100106
Christopher Fauletc8016d02021-10-13 15:34:49 +0200107 def_rules = ((s->be->defpx && (an_bit == AN_REQ_INSPECT_FE || s->be->defpx != sess->fe->defpx)) ? &s->be->defpx->tcp_req.inspect_rules : NULL);
108 rules = &s->be->tcp_req.inspect_rules;
109
Willy Tarreau39713102016-11-25 15:49:32 +0100110 /* We don't know whether we have enough data, so must proceed
111 * this way :
112 * - iterate through all rules in their declaration order
113 * - if one rule returns MISS, it means the inspect delay is
114 * not over yet, then return immediately, otherwise consider
115 * it as a non-match.
116 * - if one rule returns OK, then return OK
117 * - if one rule returns KO, then return KO
118 */
119
Christopher Fauletcb59e0b2021-09-30 14:56:30 +0200120 if ((req->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)) || channel_full(req, global.tune.maxrewrite) ||
Christopher Faulet7b5ca8f2022-03-30 14:37:49 +0200121 si_rx_blocked_room(chn_prod(req)->si) ||
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200122 !s->be->tcp_req.inspect_delay || tick_is_expired(s->rules_exp, now_ms))
Willy Tarreau39713102016-11-25 15:49:32 +0100123 partial = SMP_OPT_FINAL;
124 else
125 partial = 0;
126
127 /* If "the current_rule_list" match the executed rule list, we are in
128 * resume condition. If a resume is needed it is always in the action
129 * and never in the ACL or converters. In this case, we initialise the
130 * current rule, and go to the action execution point.
131 */
132 if (s->current_rule) {
133 rule = s->current_rule;
134 s->current_rule = NULL;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200135 if ((def_rules && s->current_rule_list == def_rules) || s->current_rule_list == rules)
Willy Tarreau39713102016-11-25 15:49:32 +0100136 goto resume_execution;
137 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200138 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Willy Tarreau39713102016-11-25 15:49:32 +0100139
Christopher Fauletc8016d02021-10-13 15:34:49 +0200140 restart:
141 list_for_each_entry(rule, s->current_rule_list, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100142 enum acl_test_res ret = ACL_TEST_PASS;
143
144 if (rule->cond) {
145 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial);
146 if (ret == ACL_TEST_MISS)
147 goto missing_data;
148
149 ret = acl_pass(ret);
150 if (rule->cond->pol == ACL_COND_UNLESS)
151 ret = !ret;
152 }
153
154 if (ret) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100155 act_opts |= ACT_OPT_FIRST;
Willy Tarreau39713102016-11-25 15:49:32 +0100156resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100157
158 /* Always call the action function if defined */
159 if (rule->action_ptr) {
160 if (partial & SMP_OPT_FINAL)
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100161 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100162
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100163 switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100164 case ACT_RET_CONT:
165 break;
166 case ACT_RET_STOP:
167 case ACT_RET_DONE:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100168 s->last_rule_file = rule->conf.file;
169 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100170 goto end;
171 case ACT_RET_YIELD:
172 s->current_rule = rule;
Christopher Faulet99aaca92020-07-28 11:30:19 +0200173 if (partial & SMP_OPT_FINAL) {
174 send_log(s->be, LOG_WARNING,
175 "Internal error: yield not allowed if the inspect-delay expired "
176 "for the tcp-request content actions.");
177 goto internal;
178 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100179 goto missing_data;
180 case ACT_RET_DENY:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100181 s->last_rule_file = rule->conf.file;
182 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100183 goto deny;
184 case ACT_RET_ABRT:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100185 s->last_rule_file = rule->conf.file;
186 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100187 goto abort;
188 case ACT_RET_ERR:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100189 s->last_rule_file = rule->conf.file;
190 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100191 goto internal;
192 case ACT_RET_INV:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100193 s->last_rule_file = rule->conf.file;
194 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100195 goto invalid;
196 }
197 continue; /* eval the next rule */
198 }
199
200 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100201 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreauc6dae862022-03-09 17:23:10 +0100202 s->last_rule_file = rule->conf.file;
203 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100204 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100205 }
206 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreauc6dae862022-03-09 17:23:10 +0100207 s->last_rule_file = rule->conf.file;
208 s->last_rule_line = rule->conf.line;
Christopher Faulet282992e2019-12-16 12:34:31 +0100209 goto deny;
Willy Tarreau39713102016-11-25 15:49:32 +0100210 }
Willy Tarreau39713102016-11-25 15:49:32 +0100211 }
212 }
213
Christopher Fauletc8016d02021-10-13 15:34:49 +0200214 if (def_rules && s->current_rule_list == def_rules) {
215 s->current_rule_list = rules;
216 goto restart;
217 }
218
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100219 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100220 /* if we get there, it means we have no rule which matches, or
221 * we have an explicit accept, so we apply the default accept.
222 */
223 req->analysers &= ~an_bit;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200224 s->current_rule = s->current_rule_list = NULL;
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200225 req->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100226 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100227 return 1;
228
229 missing_data:
230 channel_dont_connect(req);
231 /* just set the request timeout once at the beginning of the request */
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200232 if (!tick_isset(s->rules_exp) && s->be->tcp_req.inspect_delay)
233 s->rules_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
234 req->analyse_exp = tick_first((tick_is_expired(req->analyse_exp, now_ms) ? 0 : req->analyse_exp), s->rules_exp);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100235 DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100236 return 0;
237
Christopher Faulet282992e2019-12-16 12:34:31 +0100238 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +0200239 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100240 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200241 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100242 goto reject;
243
244 internal:
Willy Tarreau4781b152021-04-06 13:53:36 +0200245 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100246 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200247 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100248 if (!(s->flags & SF_ERR_MASK))
249 s->flags |= SF_ERR_INTERNAL;
250 goto reject;
251
252 invalid:
Willy Tarreau4781b152021-04-06 13:53:36 +0200253 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100254 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200255 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100256
257 reject:
Christopher Faulet9a521232022-03-30 14:42:50 +0200258 cs_must_kill_conn(chn_prod(req));
Christopher Faulet282992e2019-12-16 12:34:31 +0100259 channel_abort(req);
260 channel_abort(&s->res);
261
262 abort:
263 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200264 s->current_rule = s->current_rule_list = NULL;
265 req->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Faulet282992e2019-12-16 12:34:31 +0100266
267 if (!(s->flags & SF_ERR_MASK))
268 s->flags |= SF_ERR_PRXCOND;
269 if (!(s->flags & SF_FINST_MASK))
270 s->flags |= SF_FINST_R;
271 DBG_TRACE_DEVEL("leaving on error|deny|abort", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_TCP_ERR, s);
272 return 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100273}
274
275/* This function performs the TCP response analysis on the current response. It
276 * returns 1 if the processing can continue on next analysers, or zero if it
277 * needs more data, encounters an error, or wants to immediately abort the
278 * response. It relies on buffers flags, and updates s->rep->analysers. The
279 * function may be called for backend rules.
280 */
281int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
282{
Christopher Fauletc8016d02021-10-13 15:34:49 +0200283 struct list *def_rules, *rules;
Willy Tarreau39713102016-11-25 15:49:32 +0100284 struct session *sess = s->sess;
285 struct act_rule *rule;
286 int partial;
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100287 int act_opts = 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100288
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100289 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100290
Christopher Fauletc8016d02021-10-13 15:34:49 +0200291 def_rules = (s->be->defpx ? &s->be->defpx->tcp_rep.inspect_rules : NULL);
292 rules = &s->be->tcp_rep.inspect_rules;
293
Willy Tarreau39713102016-11-25 15:49:32 +0100294 /* We don't know whether we have enough data, so must proceed
295 * this way :
296 * - iterate through all rules in their declaration order
297 * - if one rule returns MISS, it means the inspect delay is
298 * not over yet, then return immediately, otherwise consider
299 * it as a non-match.
300 * - if one rule returns OK, then return OK
301 * - if one rule returns KO, then return KO
302 */
Christopher Fauletcb59e0b2021-09-30 14:56:30 +0200303 if ((rep->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)) || channel_full(rep, global.tune.maxrewrite) ||
Christopher Faulet7b5ca8f2022-03-30 14:37:49 +0200304 si_rx_blocked_room(chn_prod(rep)->si) ||
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200305 !s->be->tcp_rep.inspect_delay || tick_is_expired(s->rules_exp, now_ms))
Willy Tarreau39713102016-11-25 15:49:32 +0100306 partial = SMP_OPT_FINAL;
307 else
308 partial = 0;
309
310 /* If "the current_rule_list" match the executed rule list, we are in
311 * resume condition. If a resume is needed it is always in the action
312 * and never in the ACL or converters. In this case, we initialise the
313 * current rule, and go to the action execution point.
314 */
315 if (s->current_rule) {
316 rule = s->current_rule;
317 s->current_rule = NULL;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200318 if ((def_rules && s->current_rule_list == def_rules) || s->current_rule_list == rules)
Willy Tarreau39713102016-11-25 15:49:32 +0100319 goto resume_execution;
320 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200321 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Willy Tarreau39713102016-11-25 15:49:32 +0100322
Christopher Fauletc8016d02021-10-13 15:34:49 +0200323 restart:
324 list_for_each_entry(rule, s->current_rule_list, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100325 enum acl_test_res ret = ACL_TEST_PASS;
326
327 if (rule->cond) {
328 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200329 if (ret == ACL_TEST_MISS)
330 goto missing_data;
Willy Tarreau39713102016-11-25 15:49:32 +0100331
332 ret = acl_pass(ret);
333 if (rule->cond->pol == ACL_COND_UNLESS)
334 ret = !ret;
335 }
336
337 if (ret) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100338 act_opts |= ACT_OPT_FIRST;
Willy Tarreau39713102016-11-25 15:49:32 +0100339resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100340 /* Always call the action function if defined */
341 if (rule->action_ptr) {
342 if (partial & SMP_OPT_FINAL)
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100343 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100344
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 Faulet99aaca92020-07-28 11:30:19 +0200355 if (partial & SMP_OPT_FINAL) {
356 send_log(s->be, LOG_WARNING,
357 "Internal error: yield not allowed if the inspect-delay expired "
358 "for the tcp-response content actions.");
359 goto internal;
360 }
Christopher 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) {
Christopher Faulet8abe7122022-03-30 15:10:18 +0200395 chn_prod(rep)->flags |= CS_FL_NOLINGER | CS_FL_NOHALF;
Christopher Faulet9a521232022-03-30 14:42:50 +0200396 cs_must_kill_conn(chn_prod(rep));
Christopher Fauletda098e62022-03-31 17:44:45 +0200397 cs_shutr(chn_prod(rep));
398 cs_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:
Christopher Faulet9a521232022-03-30 14:42:50 +0200455 cs_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
476 * any data. This only works on connection-based client-facing stream interfaces.
477 */
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
573 * any data. This only works on session-based client-facing stream interfaces.
574 * An example of valid use case is to track a stick-counter on the source
575 * address extracted from the proxy protocol.
576 */
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 Tarreau843b7cb2018-07-13 10:54:26 +02001061 args[0], args[1], MAX_SESS_STKCTR-1,
1062 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 */