blob: baa4ba71fdc9bc2f7d1d69f975f65d9b47b57c53 [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>
Willy Tarreauf268ee82020-06-04 17:05:57 +020021#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020022#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020023#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020024#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020025#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020026#include <haproxy/sc_strm.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 Fauletcb59e0b2021-09-30 14:56:30 +0200119 if ((req->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)) || channel_full(req, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +0200120 sc_waiting_room(chn_prod(req)) ||
Christopher Faulet2747fbb2020-07-28 11:56:13 +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;
123 else
124 partial = 0;
125
126 /* If "the current_rule_list" match the executed rule list, we are in
127 * resume condition. If a resume is needed it is always in the action
128 * and never in the ACL or converters. In this case, we initialise the
129 * current rule, and go to the action execution point.
130 */
131 if (s->current_rule) {
132 rule = s->current_rule;
133 s->current_rule = NULL;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200134 if ((def_rules && s->current_rule_list == def_rules) || s->current_rule_list == rules)
Willy Tarreau39713102016-11-25 15:49:32 +0100135 goto resume_execution;
136 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200137 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Willy Tarreau39713102016-11-25 15:49:32 +0100138
Christopher Fauletc8016d02021-10-13 15:34:49 +0200139 restart:
140 list_for_each_entry(rule, s->current_rule_list, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100141 enum acl_test_res ret = ACL_TEST_PASS;
142
143 if (rule->cond) {
144 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial);
145 if (ret == ACL_TEST_MISS)
146 goto missing_data;
147
148 ret = acl_pass(ret);
149 if (rule->cond->pol == ACL_COND_UNLESS)
150 ret = !ret;
151 }
152
153 if (ret) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100154 act_opts |= ACT_OPT_FIRST;
Willy Tarreau39713102016-11-25 15:49:32 +0100155resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100156
157 /* Always call the action function if defined */
158 if (rule->action_ptr) {
159 if (partial & SMP_OPT_FINAL)
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100160 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100161
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 Faulet99aaca92020-07-28 11:30:19 +0200172 if (partial & SMP_OPT_FINAL) {
173 send_log(s->be, LOG_WARNING,
174 "Internal error: yield not allowed if the inspect-delay expired "
175 "for the tcp-request content actions.");
176 goto internal;
177 }
Christopher 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 Fauletcb59e0b2021-09-30 14:56:30 +0200302 if ((rep->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)) || channel_full(rep, global.tune.maxrewrite) ||
Willy Tarreau99615ed2022-05-25 07:29:36 +0200303 sc_waiting_room(chn_prod(rep)) ||
Christopher Faulet2747fbb2020-07-28 11:56:13 +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;
306 else
307 partial = 0;
308
309 /* If "the current_rule_list" match the executed rule list, we are in
310 * resume condition. If a resume is needed it is always in the action
311 * and never in the ACL or converters. In this case, we initialise the
312 * current rule, and go to the action execution point.
313 */
314 if (s->current_rule) {
315 rule = s->current_rule;
316 s->current_rule = NULL;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200317 if ((def_rules && s->current_rule_list == def_rules) || s->current_rule_list == rules)
Willy Tarreau39713102016-11-25 15:49:32 +0100318 goto resume_execution;
319 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200320 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Willy Tarreau39713102016-11-25 15:49:32 +0100321
Christopher Fauletc8016d02021-10-13 15:34:49 +0200322 restart:
323 list_for_each_entry(rule, s->current_rule_list, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100324 enum acl_test_res ret = ACL_TEST_PASS;
325
326 if (rule->cond) {
327 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200328 if (ret == ACL_TEST_MISS)
329 goto missing_data;
Willy Tarreau39713102016-11-25 15:49:32 +0100330
331 ret = acl_pass(ret);
332 if (rule->cond->pol == ACL_COND_UNLESS)
333 ret = !ret;
334 }
335
336 if (ret) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100337 act_opts |= ACT_OPT_FIRST;
Willy Tarreau39713102016-11-25 15:49:32 +0100338resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100339 /* Always call the action function if defined */
340 if (rule->action_ptr) {
341 if (partial & SMP_OPT_FINAL)
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100342 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100343
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100344 switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100345 case ACT_RET_CONT:
346 break;
347 case ACT_RET_STOP:
348 case ACT_RET_DONE:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100349 s->last_rule_file = rule->conf.file;
350 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100351 goto end;
352 case ACT_RET_YIELD:
353 s->current_rule = rule;
Christopher Faulet99aaca92020-07-28 11:30:19 +0200354 if (partial & SMP_OPT_FINAL) {
355 send_log(s->be, LOG_WARNING,
356 "Internal error: yield not allowed if the inspect-delay expired "
357 "for the tcp-response content actions.");
358 goto internal;
359 }
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200360 channel_dont_close(rep);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100361 goto missing_data;
362 case ACT_RET_DENY:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100363 s->last_rule_file = rule->conf.file;
364 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100365 goto deny;
366 case ACT_RET_ABRT:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100367 s->last_rule_file = rule->conf.file;
368 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100369 goto abort;
370 case ACT_RET_ERR:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100371 s->last_rule_file = rule->conf.file;
372 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100373 goto internal;
374 case ACT_RET_INV:
Willy Tarreauc6dae862022-03-09 17:23:10 +0100375 s->last_rule_file = rule->conf.file;
376 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100377 goto invalid;
378 }
379 continue; /* eval the next rule */
380 }
381
382 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100383 if (rule->action == ACT_ACTION_ALLOW) {
Willy Tarreauc6dae862022-03-09 17:23:10 +0100384 s->last_rule_file = rule->conf.file;
385 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100386 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100387 }
388 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreauc6dae862022-03-09 17:23:10 +0100389 s->last_rule_file = rule->conf.file;
390 s->last_rule_line = rule->conf.line;
Christopher Faulet282992e2019-12-16 12:34:31 +0100391 goto deny;
Willy Tarreau39713102016-11-25 15:49:32 +0100392 }
393 else if (rule->action == ACT_TCP_CLOSE) {
Willy Tarreaucb041662022-05-17 19:44:42 +0200394 chn_prod(rep)->flags |= SC_FL_NOLINGER | SC_FL_NOHALF;
Willy Tarreauf61dd192022-05-27 09:00:19 +0200395 sc_must_kill_conn(chn_prod(rep));
396 sc_shutr(chn_prod(rep));
397 sc_shutw(chn_prod(rep));
Willy Tarreauc6dae862022-03-09 17:23:10 +0100398 s->last_rule_file = rule->conf.file;
399 s->last_rule_line = rule->conf.line;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100400 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100401 }
402 }
403 }
404
Christopher Fauletc8016d02021-10-13 15:34:49 +0200405 if (def_rules && s->current_rule_list == def_rules) {
406 s->current_rule_list = rules;
407 goto restart;
408 }
409
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100410 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100411 /* if we get there, it means we have no rule which matches, or
412 * we have an explicit accept, so we apply the default accept.
413 */
414 rep->analysers &= ~an_bit;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200415 s->current_rule = s->current_rule_list = NULL;
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200416 rep->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100417 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100418 return 1;
Christopher Faulet282992e2019-12-16 12:34:31 +0100419
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100420 missing_data:
Christopher Faulet54f3e182020-07-29 12:00:23 +0200421 /* just set the analyser timeout once at the beginning of the response */
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200422 if (!tick_isset(s->rules_exp) && s->be->tcp_rep.inspect_delay)
423 s->rules_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
424 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 +0100425 DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
426 return 0;
427
Christopher Faulet282992e2019-12-16 12:34:31 +0100428 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +0200429 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.denied_resp);
430 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +0100431 if (s->sess->listener && s->sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200432 _HA_ATOMIC_INC(&s->sess->listener->counters->denied_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100433 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200434 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100435 goto reject;
436
437 internal:
Willy Tarreau4781b152021-04-06 13:53:36 +0200438 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.internal_errors);
439 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100440 if (s->sess->listener && s->sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200441 _HA_ATOMIC_INC(&s->sess->listener->counters->internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100442 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200443 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100444 if (!(s->flags & SF_ERR_MASK))
445 s->flags |= SF_ERR_INTERNAL;
446 goto reject;
447
448 invalid:
Willy Tarreau4781b152021-04-06 13:53:36 +0200449 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100450 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200451 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100452
453 reject:
Willy Tarreauf61dd192022-05-27 09:00:19 +0200454 sc_must_kill_conn(chn_prod(rep));
Christopher Faulet282992e2019-12-16 12:34:31 +0100455 channel_abort(rep);
456 channel_abort(&s->req);
457
458 abort:
Christopher Faulet19dbf2d2020-07-28 11:40:07 +0200459 rep->analysers &= AN_RES_FLT_END;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200460 s->current_rule = s->current_rule_list = NULL;
461 rep->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Faulet282992e2019-12-16 12:34:31 +0100462
463 if (!(s->flags & SF_ERR_MASK))
464 s->flags |= SF_ERR_PRXCOND;
465 if (!(s->flags & SF_FINST_MASK))
466 s->flags |= SF_FINST_D;
467 DBG_TRACE_DEVEL("leaving on error", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_TCP_ERR, s);
468 return 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100469}
470
471
472/* This function performs the TCP layer4 analysis on the current request. It
473 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
474 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreau4596fe22022-05-17 19:07:51 +0200475 * any data. This only works on connection-based client-facing stream connectors.
Willy Tarreau39713102016-11-25 15:49:32 +0100476 */
477int tcp_exec_l4_rules(struct session *sess)
478{
Christopher Fauletc8016d02021-10-13 15:34:49 +0200479 struct proxy *px = sess->fe;
Willy Tarreau39713102016-11-25 15:49:32 +0100480 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100481 struct connection *conn = objt_conn(sess->origin);
482 int result = 1;
483 enum acl_test_res ret;
484
485 if (!conn)
486 return result;
487
Christopher Fauletc8016d02021-10-13 15:34:49 +0200488 if (sess->fe->defpx)
489 px = sess->fe->defpx;
490
491 restart:
492 list_for_each_entry(rule, &px->tcp_req.l4_rules, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100493 ret = ACL_TEST_PASS;
494
495 if (rule->cond) {
496 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
497 ret = acl_pass(ret);
498 if (rule->cond->pol == ACL_COND_UNLESS)
499 ret = !ret;
500 }
501
502 if (ret) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100503 /* Always call the action function if defined */
504 if (rule->action_ptr) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100505 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_OPT_FINAL | ACT_OPT_FIRST)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100506 case ACT_RET_YIELD:
507 /* yield is not allowed at this point. If this return code is
508 * used it is a bug, so I prefer to abort the process.
509 */
510 send_log(sess->fe, LOG_WARNING,
511 "Internal error: yield not allowed with tcp-request connection actions.");
512 /* fall through */
513 case ACT_RET_STOP:
514 case ACT_RET_DONE:
515 goto end;
516 case ACT_RET_CONT:
517 break;
518 case ACT_RET_DENY:
519 case ACT_RET_ABRT:
520 case ACT_RET_ERR:
521 case ACT_RET_INV:
522 result = 0;
523 goto end;
524 }
525 continue; /* eval the next rule */
526 }
527
528 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100529 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100530 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100531 }
532 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200533 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_conn);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100534 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200535 _HA_ATOMIC_INC(&sess->listener->counters->denied_conn);
Willy Tarreau39713102016-11-25 15:49:32 +0100536
537 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100538 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100539 }
Willy Tarreau39713102016-11-25 15:49:32 +0100540 else if (rule->action == ACT_TCP_EXPECT_PX) {
Willy Tarreau4450b582020-01-23 15:23:13 +0100541 if (!(conn->flags & CO_FL_HANDSHAKE)) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200542 if (xprt_add_hs(conn) < 0) {
543 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100544 goto end;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200545 }
546 }
Willy Tarreau39713102016-11-25 15:49:32 +0100547 conn->flags |= CO_FL_ACCEPT_PROXY;
Willy Tarreau39713102016-11-25 15:49:32 +0100548 }
549 else if (rule->action == ACT_TCP_EXPECT_CIP) {
Willy Tarreau4450b582020-01-23 15:23:13 +0100550 if (!(conn->flags & CO_FL_HANDSHAKE)) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200551 if (xprt_add_hs(conn) < 0) {
552 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100553 goto end;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200554 }
555 }
Willy Tarreau39713102016-11-25 15:49:32 +0100556 conn->flags |= CO_FL_ACCEPT_CIP;
Willy Tarreau39713102016-11-25 15:49:32 +0100557 }
Willy Tarreau39713102016-11-25 15:49:32 +0100558 }
559 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200560
561 if (px != sess->fe) {
562 px = sess->fe;
563 goto restart;
564 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100565 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100566 return result;
567}
568
569/* This function performs the TCP layer5 analysis on the current request. It
570 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
571 * matches or if no more rule matches. It can only use rules which don't need
Willy Tarreau4596fe22022-05-17 19:07:51 +0200572 * any data. This only works on session-based client-facing stream connectors.
Willy Tarreau39713102016-11-25 15:49:32 +0100573 * An example of valid use case is to track a stick-counter on the source
574 * address extracted from the proxy protocol.
575 */
576int tcp_exec_l5_rules(struct session *sess)
577{
Christopher Fauletc8016d02021-10-13 15:34:49 +0200578 struct proxy *px = sess->fe;
Willy Tarreau39713102016-11-25 15:49:32 +0100579 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100580 int result = 1;
581 enum acl_test_res ret;
582
Christopher Fauletc8016d02021-10-13 15:34:49 +0200583 if (sess->fe->defpx)
584 px = sess->fe->defpx;
585
586 restart:
587 list_for_each_entry(rule, &px->tcp_req.l5_rules, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100588 ret = ACL_TEST_PASS;
589
590 if (rule->cond) {
591 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
592 ret = acl_pass(ret);
593 if (rule->cond->pol == ACL_COND_UNLESS)
594 ret = !ret;
595 }
596
597 if (ret) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100598 /* Always call the action function if defined */
599 if (rule->action_ptr) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100600 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_OPT_FINAL | ACT_OPT_FIRST)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100601 case ACT_RET_YIELD:
602 /* yield is not allowed at this point. If this return code is
603 * used it is a bug, so I prefer to abort the process.
604 */
605 send_log(sess->fe, LOG_WARNING,
606 "Internal error: yield not allowed with tcp-request session actions.");
607 /* fall through */
608 case ACT_RET_STOP:
609 case ACT_RET_DONE:
610 goto end;
611 case ACT_RET_CONT:
612 break;
613 case ACT_RET_DENY:
614 case ACT_RET_ABRT:
615 case ACT_RET_ERR:
616 case ACT_RET_INV:
617 result = 0;
618 goto end;
619 }
620 continue; /* eval the next rule */
621 }
622
623 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100624 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100625 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100626 }
627 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200628 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_sess);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100629 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200630 _HA_ATOMIC_INC(&sess->listener->counters->denied_sess);
Willy Tarreau39713102016-11-25 15:49:32 +0100631
632 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100633 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100634 }
Willy Tarreau39713102016-11-25 15:49:32 +0100635 }
636 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200637
638 if (px != sess->fe) {
639 px = sess->fe;
640 goto restart;
641 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100642 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100643 return result;
644}
645
646/* Parse a tcp-response rule. Return a negative value in case of failure */
647static int tcp_parse_response_rule(char **args, int arg, int section_type,
Willy Tarreau01825162021-03-09 09:53:46 +0100648 struct proxy *curpx, const struct proxy *defpx,
Willy Tarreau39713102016-11-25 15:49:32 +0100649 struct act_rule *rule, char **err,
650 unsigned int where,
651 const char *file, int line)
652{
Christopher Fauletee08d6c2021-10-13 15:40:15 +0200653 if ((curpx == defpx && strlen(defpx->id) == 0) || !(curpx->cap & PR_CAP_BE)) {
654 memprintf(err, "%s %s is only allowed in 'backend' sections or 'defaults' section with a name",
Willy Tarreau39713102016-11-25 15:49:32 +0100655 args[0], args[1]);
656 return -1;
657 }
658
659 if (strcmp(args[arg], "accept") == 0) {
660 arg++;
661 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100662 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100663 }
664 else if (strcmp(args[arg], "reject") == 0) {
665 arg++;
666 rule->action = ACT_ACTION_DENY;
Christopher Faulet245cf792019-12-18 14:58:12 +0100667 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100668 }
669 else if (strcmp(args[arg], "close") == 0) {
670 arg++;
671 rule->action = ACT_TCP_CLOSE;
Christopher Faulet245cf792019-12-18 14:58:12 +0100672 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100673 }
674 else {
675 struct action_kw *kw;
676 kw = tcp_res_cont_action(args[arg]);
677 if (kw) {
678 arg++;
Willy Tarreau39713102016-11-25 15:49:32 +0100679 rule->kw = kw;
680 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
681 return -1;
682 } else {
Willy Tarreaudb67b0e2021-03-12 13:46:10 +0100683 const char *extra[] = { "accept", "reject", "close", NULL };
684 const char *best = action_suggest(args[arg], &tcp_res_cont_keywords, extra);
685
Willy Tarreau39713102016-11-25 15:49:32 +0100686 action_build_list(&tcp_res_cont_keywords, &trash);
687 memprintf(err,
Willy Tarreaudb67b0e2021-03-12 13:46:10 +0100688 "'%s %s' expects 'accept', 'close', 'reject', %s in %s '%s' (got '%s').%s%s%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 args[0], args[1], trash.area,
Willy Tarreaudb67b0e2021-03-12 13:46:10 +0100690 proxy_type_str(curpx), curpx->id, args[arg],
691 best ? " Did you mean '" : "",
692 best ? best : "",
693 best ? "' maybe ?" : "");
Willy Tarreau39713102016-11-25 15:49:32 +0100694 return -1;
695 }
696 }
697
698 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +0200699 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +0100700 memprintf(err,
701 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
702 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
703 return -1;
704 }
705 }
706 else if (*args[arg]) {
707 memprintf(err,
708 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
709 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
710 return -1;
711 }
712 return 0;
713}
714
Christopher Fauletac98d812019-12-18 09:20:16 +0100715
716/* This function executes a track-sc* actions. On success, it returns
717 * ACT_RET_CONT. If it must yield, it return ACT_RET_YIELD. Otherwsize
718 * ACT_RET_ERR is returned.
719 */
720static enum act_return tcp_action_track_sc(struct act_rule *rule, struct proxy *px,
721 struct session *sess, struct stream *s, int flags)
722{
723 struct stksess *ts;
724 struct stktable *t;
725 struct stktable_key *key;
726 struct sample smp;
727 int opt;
728
Christopher Faulet67307792020-02-10 09:54:49 +0100729 opt = SMP_OPT_DIR_REQ;
Christopher Fauletac98d812019-12-18 09:20:16 +0100730 if (flags & ACT_FLAG_FINAL)
731 opt |= SMP_OPT_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100732
Christopher Fauletac98d812019-12-18 09:20:16 +0100733 t = rule->arg.trk_ctr.table.t;
Christopher Faulet67307792020-02-10 09:54:49 +0100734 if (rule->from == ACT_F_TCP_REQ_CNT) { /* L7 rules: use the stream */
735 if (stkctr_entry(&s->stkctr[rule->action]))
736 goto end;
Christopher Fauletac98d812019-12-18 09:20:16 +0100737
Christopher Faulet67307792020-02-10 09:54:49 +0100738 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, &smp);
Christopher Fauletac98d812019-12-18 09:20:16 +0100739
Christopher Faulet67307792020-02-10 09:54:49 +0100740 if ((smp.flags & SMP_F_MAY_CHANGE) && !(flags & ACT_FLAG_FINAL))
741 return ACT_RET_YIELD; /* key might appear later */
742
743 if (key && (ts = stktable_get_entry(t, key))) {
744 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
Christopher Fauletac98d812019-12-18 09:20:16 +0100745 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
746 if (sess->fe != s->be)
747 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
748 }
749 }
Christopher Faulet67307792020-02-10 09:54:49 +0100750 else { /* L4/L5 rules: use the session */
751 if (stkctr_entry(&sess->stkctr[rule->action]))
752 goto end;
753
754 key = stktable_fetch_key(t, sess->fe, sess, NULL, opt, rule->arg.trk_ctr.expr, NULL);
755 if (key && (ts = stktable_get_entry(t, key)))
756 stream_track_stkctr(&sess->stkctr[rule->action], t, ts);
757 }
Christopher Fauletac98d812019-12-18 09:20:16 +0100758
759 end:
760 return ACT_RET_CONT;
761}
Willy Tarreau39713102016-11-25 15:49:32 +0100762
Christopher Fauletd73b96d2019-12-19 17:27:03 +0100763/* This function executes a capture actions. It executes a fetch expression,
764 * turns the result into a string and puts it in a capture slot. On success, it
765 * returns ACT_RET_CONT. If it must yield, it return ACT_RET_YIELD. Otherwsize
766 * ACT_RET_ERR is returned.
767 */
768static enum act_return tcp_action_capture(struct act_rule *rule, struct proxy *px,
769 struct session *sess, struct stream *s, int flags)
770{
771 struct sample *key;
772 struct cap_hdr *h = rule->arg.cap.hdr;
773 char **cap = s->req_cap;
774 int len, opt;
775
776 opt = ((rule->from == ACT_F_TCP_REQ_CNT) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
777 if (flags & ACT_FLAG_FINAL)
778 opt |= SMP_OPT_FINAL;
779
780 key = sample_fetch_as_type(s->be, sess, s, opt, rule->arg.cap.expr, SMP_T_STR);
781 if (!key)
782 goto end;
783
784 if ((key->flags & SMP_F_MAY_CHANGE) && !(flags & ACT_FLAG_FINAL))
785 return ACT_RET_YIELD; /* key might appear later */
786
787 if (cap[h->index] == NULL) {
788 cap[h->index] = pool_alloc(h->pool);
789 if (cap[h->index] == NULL) /* no more capture memory, ignore error */
790 goto end;
791 }
792
793 len = key->data.u.str.data;
794 if (len > h->len)
795 len = h->len;
796
797 memcpy(cap[h->index], key->data.u.str.area, len);
798 cap[h->index][len] = 0;
799
800 end:
801 return ACT_RET_CONT;
802}
803
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100804static void release_tcp_capture(struct act_rule * rule)
805{
806 release_sample_expr(rule->arg.cap.expr);
807}
808
809
810static void release_tcp_track_sc(struct act_rule * rule)
811{
812 release_sample_expr(rule->arg.trk_ctr.expr);
813}
814
Willy Tarreau39713102016-11-25 15:49:32 +0100815/* Parse a tcp-request rule. Return a negative value in case of failure */
816static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau01825162021-03-09 09:53:46 +0100817 struct proxy *curpx, const struct proxy *defpx,
Willy Tarreau39713102016-11-25 15:49:32 +0100818 struct act_rule *rule, char **err,
819 unsigned int where, const char *file, int line)
820{
Christopher Fauletee08d6c2021-10-13 15:40:15 +0200821 if (curpx == defpx && strlen(defpx->id) == 0) {
822 memprintf(err, "%s %s is not allowed in anonymous 'defaults' sections",
823 args[0], args[1]);
Willy Tarreau39713102016-11-25 15:49:32 +0100824 return -1;
825 }
826
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100827 if (strcmp(args[arg], "accept") == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100828 arg++;
829 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100830 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100831 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100832 else if (strcmp(args[arg], "reject") == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100833 arg++;
834 rule->action = ACT_ACTION_DENY;
Christopher Faulet245cf792019-12-18 14:58:12 +0100835 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100836 }
837 else if (strcmp(args[arg], "capture") == 0) {
838 struct sample_expr *expr;
839 struct cap_hdr *hdr;
840 int kw = arg;
841 int len = 0;
842
843 if (!(curpx->cap & PR_CAP_FE)) {
844 memprintf(err,
845 "'%s %s %s' : proxy '%s' has no frontend capability",
846 args[0], args[1], args[kw], curpx->id);
847 return -1;
848 }
849
850 if (!(where & SMP_VAL_FE_REQ_CNT)) {
851 memprintf(err,
852 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
853 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
854 return -1;
855 }
856
857 arg++;
858
859 curpx->conf.args.ctx = ARGC_CAP;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100860 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args, NULL);
Willy Tarreau39713102016-11-25 15:49:32 +0100861 if (!expr) {
862 memprintf(err,
863 "'%s %s %s' : %s",
864 args[0], args[1], args[kw], *err);
865 return -1;
866 }
867
868 if (!(expr->fetch->val & where)) {
869 memprintf(err,
870 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
871 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100872 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100873 return -1;
874 }
875
876 if (strcmp(args[arg], "len") == 0) {
877 arg++;
878 if (!args[arg]) {
879 memprintf(err,
880 "'%s %s %s' : missing length value",
881 args[0], args[1], args[kw]);
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100882 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100883 return -1;
884 }
885 /* we copy the table name for now, it will be resolved later */
886 len = atoi(args[arg]);
887 if (len <= 0) {
888 memprintf(err,
889 "'%s %s %s' : length must be > 0",
890 args[0], args[1], args[kw]);
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100891 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100892 return -1;
893 }
894 arg++;
895 }
896
897 if (!len) {
898 memprintf(err,
899 "'%s %s %s' : a positive 'len' argument is mandatory",
900 args[0], args[1], args[kw]);
901 free(expr);
902 return -1;
903 }
904
905 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Breton8cb03362021-05-17 10:08:16 +0200906 if (!hdr) {
907 memprintf(err, "parsing [%s:%d] : out of memory", file, line);
908 release_sample_expr(expr);
909 return -1;
910 }
Willy Tarreau39713102016-11-25 15:49:32 +0100911 hdr->next = curpx->req_cap;
912 hdr->name = NULL; /* not a header capture */
913 hdr->namelen = 0;
914 hdr->len = len;
915 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
916 hdr->index = curpx->nb_req_cap++;
917
918 curpx->req_cap = hdr;
919 curpx->to_log |= LW_REQHDR;
920
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200921 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreau39713102016-11-25 15:49:32 +0100922 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
923
924 rule->arg.cap.expr = expr;
925 rule->arg.cap.hdr = hdr;
Christopher Fauletd73b96d2019-12-19 17:27:03 +0100926 rule->action = ACT_CUSTOM;
927 rule->action_ptr = tcp_action_capture;
928 rule->check_ptr = check_capture;
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100929 rule->release_ptr = release_tcp_capture;
Willy Tarreau39713102016-11-25 15:49:32 +0100930 }
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100931 else if (strncmp(args[arg], "track-sc", 8) == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100932 struct sample_expr *expr;
933 int kw = arg;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100934 unsigned int tsc_num;
935 const char *tsc_num_str;
Willy Tarreau39713102016-11-25 15:49:32 +0100936
937 arg++;
938
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100939 tsc_num_str = &args[kw][8];
940 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1) {
941 memprintf(err, "'%s %s %s' : %s", args[0], args[1], args[kw], *err);
942 return -1;
943 }
944
Willy Tarreau39713102016-11-25 15:49:32 +0100945 curpx->conf.args.ctx = ARGC_TRK;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100946 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args, NULL);
Willy Tarreau39713102016-11-25 15:49:32 +0100947 if (!expr) {
948 memprintf(err,
949 "'%s %s %s' : %s",
950 args[0], args[1], args[kw], *err);
951 return -1;
952 }
953
954 if (!(expr->fetch->val & where)) {
955 memprintf(err,
956 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
957 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100958 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100959 return -1;
960 }
961
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200962 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreau39713102016-11-25 15:49:32 +0100963 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
964
965 if (strcmp(args[arg], "table") == 0) {
966 arg++;
967 if (!args[arg]) {
968 memprintf(err,
969 "'%s %s %s' : missing table name",
970 args[0], args[1], args[kw]);
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100971 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100972 return -1;
973 }
974 /* we copy the table name for now, it will be resolved later */
975 rule->arg.trk_ctr.table.n = strdup(args[arg]);
976 arg++;
977 }
Christopher Fauletac98d812019-12-18 09:20:16 +0100978 rule->action = tsc_num;
Willy Tarreau39713102016-11-25 15:49:32 +0100979 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +0100980 rule->action_ptr = tcp_action_track_sc;
Christopher Faulet78880fb2017-09-18 14:43:55 +0200981 rule->check_ptr = check_trk_action;
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100982 rule->release_ptr = release_tcp_track_sc;
Willy Tarreau39713102016-11-25 15:49:32 +0100983 }
984 else if (strcmp(args[arg], "expect-proxy") == 0) {
985 if (strcmp(args[arg+1], "layer4") != 0) {
986 memprintf(err,
987 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
988 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
989 return -1;
990 }
991
992 if (!(where & SMP_VAL_FE_CON_ACC)) {
993 memprintf(err,
994 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
995 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
996 return -1;
997 }
998
999 arg += 2;
1000 rule->action = ACT_TCP_EXPECT_PX;
1001 }
1002 else if (strcmp(args[arg], "expect-netscaler-cip") == 0) {
1003 if (strcmp(args[arg+1], "layer4") != 0) {
1004 memprintf(err,
1005 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
1006 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
1007 return -1;
1008 }
1009
1010 if (!(where & SMP_VAL_FE_CON_ACC)) {
1011 memprintf(err,
1012 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
1013 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
1014 return -1;
1015 }
1016
1017 arg += 2;
1018 rule->action = ACT_TCP_EXPECT_CIP;
1019 }
1020 else {
1021 struct action_kw *kw;
1022 if (where & SMP_VAL_FE_CON_ACC) {
1023 /* L4 */
1024 kw = tcp_req_conn_action(args[arg]);
1025 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +01001026 } else if (where & SMP_VAL_FE_SES_ACC) {
1027 /* L5 */
1028 kw = tcp_req_sess_action(args[arg]);
1029 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +01001030 } else {
1031 /* L6 */
1032 kw = tcp_req_cont_action(args[arg]);
1033 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +01001034 }
1035 if (kw) {
1036 arg++;
1037 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
1038 return -1;
1039 } else {
Christopher Fauleta561ffb2021-03-19 08:53:26 +01001040 const char *extra[] = { "accept", "reject", "capture", "track-sc", "expect-proxy", "expect-netscaler-cip", NULL };
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001041 const char *best = NULL;
1042
1043
1044 if (where & SMP_VAL_FE_CON_ACC) {
Willy Tarreau39713102016-11-25 15:49:32 +01001045 action_build_list(&tcp_req_conn_keywords, &trash);
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001046 best = action_suggest(args[arg], &tcp_req_conn_keywords, extra);
1047 }
1048 else if (where & SMP_VAL_FE_SES_ACC) {
Willy Tarreau39713102016-11-25 15:49:32 +01001049 action_build_list(&tcp_req_sess_keywords, &trash);
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001050 best = action_suggest(args[arg], &tcp_req_sess_keywords, extra);
1051 }
1052 else {
Willy Tarreau39713102016-11-25 15:49:32 +01001053 action_build_list(&tcp_req_cont_keywords, &trash);
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001054 best = action_suggest(args[arg], &tcp_req_cont_keywords, extra);
1055 }
1056
Willy Tarreau39713102016-11-25 15:49:32 +01001057 memprintf(err,
Christopher Fauleta561ffb2021-03-19 08:53:26 +01001058 "'%s %s' expects 'accept', 'reject', 'capture', 'expect-proxy', 'expect-netscaler-cip', 'track-sc0' ... 'track-sc%d', %s "
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001059 "in %s '%s' (got '%s').%s%s%s\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001060 args[0], args[1], MAX_SESS_STKCTR-1,
1061 trash.area, proxy_type_str(curpx),
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001062 curpx->id, args[arg],
1063 best ? " Did you mean '" : "",
1064 best ? best : "",
1065 best ? "' maybe ?" : "");
Willy Tarreau39713102016-11-25 15:49:32 +01001066 return -1;
1067 }
1068 }
1069
1070 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +02001071 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +01001072 memprintf(err,
1073 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1074 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
1075 return -1;
1076 }
1077 }
1078 else if (*args[arg]) {
1079 memprintf(err,
1080 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
1081 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1082 return -1;
1083 }
1084 return 0;
1085}
1086
1087/* This function should be called to parse a line starting with the "tcp-response"
1088 * keyword.
1089 */
1090static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001091 const struct proxy *defpx, const char *file, int line,
Willy Tarreau39713102016-11-25 15:49:32 +01001092 char **err)
1093{
1094 const char *ptr = NULL;
1095 unsigned int val;
1096 int warn = 0;
1097 int arg;
1098 struct act_rule *rule;
1099 unsigned int where;
1100 const struct acl *acl;
1101 const char *kw;
1102
1103 if (!*args[1]) {
1104 memprintf(err, "missing argument for '%s' in %s '%s'",
1105 args[0], proxy_type_str(curpx), curpx->id);
1106 return -1;
1107 }
1108
1109 if (strcmp(args[1], "inspect-delay") == 0) {
Christopher Fauletee08d6c2021-10-13 15:40:15 +02001110 if ((curpx == defpx && strlen(defpx->id) == 0) || !(curpx->cap & PR_CAP_BE)) {
1111 memprintf(err, "%s %s is only allowed in 'backend' sections or 'defaults' section with a name",
Willy Tarreau39713102016-11-25 15:49:32 +01001112 args[0], args[1]);
1113 return -1;
1114 }
1115
1116 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1117 memprintf(err,
1118 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1119 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001120
1121 if (ptr == PARSE_TIME_OVER)
1122 memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]);
1123 else if (ptr == PARSE_TIME_UNDER)
1124 memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]);
1125 else if (ptr)
Willy Tarreau39713102016-11-25 15:49:32 +01001126 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
1127 return -1;
1128 }
1129
1130 if (curpx->tcp_rep.inspect_delay) {
1131 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1132 args[0], args[1], proxy_type_str(curpx), curpx->id);
1133 return 1;
1134 }
1135 curpx->tcp_rep.inspect_delay = val;
1136 return 0;
1137 }
1138
Willy Tarreaud535f802021-10-11 08:49:26 +02001139 rule = new_act_rule(ACT_F_TCP_RES_CNT, file, line);
Remi Tricot-Le Breton2ca42b42021-05-12 18:24:18 +02001140 if (!rule) {
1141 memprintf(err, "parsing [%s:%d] : out of memory", file, line);
1142 return -1;
1143 }
Willy Tarreau39713102016-11-25 15:49:32 +01001144 LIST_INIT(&rule->list);
1145 arg = 1;
1146 where = 0;
1147
1148 if (strcmp(args[1], "content") == 0) {
1149 arg++;
1150
1151 if (curpx->cap & PR_CAP_FE)
1152 where |= SMP_VAL_FE_RES_CNT;
1153 if (curpx->cap & PR_CAP_BE)
1154 where |= SMP_VAL_BE_RES_CNT;
Willy Tarreau39713102016-11-25 15:49:32 +01001155 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1156 goto error;
1157
1158 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1159 if (acl) {
1160 if (acl->name && *acl->name)
1161 memprintf(err,
1162 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1163 acl->name, args[0], args[1], sample_ckp_names(where));
1164 else
1165 memprintf(err,
1166 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1167 args[0], args[1],
1168 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1169 sample_ckp_names(where));
1170
1171 warn++;
1172 }
1173 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1174 if (acl->name && *acl->name)
1175 memprintf(err,
1176 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1177 acl->name, kw, sample_ckp_names(where));
1178 else
1179 memprintf(err,
1180 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1181 kw, sample_ckp_names(where));
1182 warn++;
1183 }
1184
Willy Tarreau2b718102021-04-21 07:32:39 +02001185 LIST_APPEND(&curpx->tcp_rep.inspect_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001186 }
1187 else {
1188 memprintf(err,
1189 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1190 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1191 goto error;
1192 }
1193
1194 return warn;
1195 error:
Willy Tarreau388c0f22022-03-17 20:26:54 +01001196 free_act_rule(rule);
Willy Tarreau39713102016-11-25 15:49:32 +01001197 return -1;
1198}
1199
1200
1201/* This function should be called to parse a line starting with the "tcp-request"
1202 * keyword.
1203 */
1204static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001205 const struct proxy *defpx, const char *file, int line,
Willy Tarreau39713102016-11-25 15:49:32 +01001206 char **err)
1207{
1208 const char *ptr = NULL;
1209 unsigned int val;
1210 int warn = 0;
1211 int arg;
1212 struct act_rule *rule;
1213 unsigned int where;
1214 const struct acl *acl;
1215 const char *kw;
1216
1217 if (!*args[1]) {
1218 if (curpx == defpx)
1219 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1220 else
1221 memprintf(err, "missing argument for '%s' in %s '%s'",
1222 args[0], proxy_type_str(curpx), curpx->id);
1223 return -1;
1224 }
1225
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001226 if (strcmp(args[1], "inspect-delay") == 0) {
Christopher Fauletee08d6c2021-10-13 15:40:15 +02001227 if (curpx == defpx && strlen(defpx->id) == 0) {
1228 memprintf(err, "%s %s is not allowed in anonymous 'defaults' sections",
1229 args[0], args[1]);
Willy Tarreau39713102016-11-25 15:49:32 +01001230 return -1;
1231 }
1232
1233 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1234 memprintf(err,
1235 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1236 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001237
1238 if (ptr == PARSE_TIME_OVER)
1239 memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]);
1240 else if (ptr == PARSE_TIME_UNDER)
1241 memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]);
1242 else if (ptr)
Willy Tarreau39713102016-11-25 15:49:32 +01001243 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
1244 return -1;
1245 }
1246
1247 if (curpx->tcp_req.inspect_delay) {
1248 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1249 args[0], args[1], proxy_type_str(curpx), curpx->id);
1250 return 1;
1251 }
1252 curpx->tcp_req.inspect_delay = val;
1253 return 0;
1254 }
1255
Willy Tarreaud535f802021-10-11 08:49:26 +02001256 rule = new_act_rule(0, file, line);
Remi Tricot-Le Breton2ca42b42021-05-12 18:24:18 +02001257 if (!rule) {
1258 memprintf(err, "parsing [%s:%d] : out of memory", file, line);
1259 return -1;
1260 }
Willy Tarreau39713102016-11-25 15:49:32 +01001261 LIST_INIT(&rule->list);
1262 arg = 1;
1263 where = 0;
1264
1265 if (strcmp(args[1], "content") == 0) {
1266 arg++;
1267
1268 if (curpx->cap & PR_CAP_FE)
1269 where |= SMP_VAL_FE_REQ_CNT;
1270 if (curpx->cap & PR_CAP_BE)
1271 where |= SMP_VAL_BE_REQ_CNT;
Christopher Fauletcb9106b2019-12-19 15:23:17 +01001272 rule->from = ACT_F_TCP_REQ_CNT;
Willy Tarreau39713102016-11-25 15:49:32 +01001273 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1274 goto error;
1275
1276 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1277 if (acl) {
1278 if (acl->name && *acl->name)
1279 memprintf(err,
1280 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1281 acl->name, args[0], args[1], sample_ckp_names(where));
1282 else
1283 memprintf(err,
1284 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1285 args[0], args[1],
1286 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1287 sample_ckp_names(where));
1288
1289 warn++;
1290 }
1291 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1292 if (acl->name && *acl->name)
1293 memprintf(err,
1294 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1295 acl->name, kw, sample_ckp_names(where));
1296 else
1297 memprintf(err,
1298 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1299 kw, sample_ckp_names(where));
1300 warn++;
1301 }
1302
1303 /* the following function directly emits the warning */
1304 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001305 LIST_APPEND(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001306 }
1307 else if (strcmp(args[1], "connection") == 0) {
1308 arg++;
1309
1310 if (!(curpx->cap & PR_CAP_FE)) {
1311 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1312 args[0], args[1], proxy_type_str(curpx), curpx->id);
1313 goto error;
1314 }
1315
1316 where |= SMP_VAL_FE_CON_ACC;
Christopher Fauletcb9106b2019-12-19 15:23:17 +01001317 rule->from = ACT_F_TCP_REQ_CON;
Willy Tarreau39713102016-11-25 15:49:32 +01001318 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1319 goto error;
1320
1321 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1322 if (acl) {
1323 if (acl->name && *acl->name)
1324 memprintf(err,
1325 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1326 acl->name, args[0], args[1], sample_ckp_names(where));
1327 else
1328 memprintf(err,
1329 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1330 args[0], args[1],
1331 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1332 sample_ckp_names(where));
1333
1334 warn++;
1335 }
1336 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1337 if (acl->name && *acl->name)
1338 memprintf(err,
1339 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1340 acl->name, kw, sample_ckp_names(where));
1341 else
1342 memprintf(err,
1343 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1344 kw, sample_ckp_names(where));
1345 warn++;
1346 }
1347
1348 /* the following function directly emits the warning */
1349 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001350 LIST_APPEND(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001351 }
1352 else if (strcmp(args[1], "session") == 0) {
1353 arg++;
1354
1355 if (!(curpx->cap & PR_CAP_FE)) {
1356 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1357 args[0], args[1], proxy_type_str(curpx), curpx->id);
1358 goto error;
1359 }
1360
1361 where |= SMP_VAL_FE_SES_ACC;
Christopher Fauletcb9106b2019-12-19 15:23:17 +01001362 rule->from = ACT_F_TCP_REQ_SES;
Willy Tarreau39713102016-11-25 15:49:32 +01001363 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1364 goto error;
1365
1366 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1367 if (acl) {
1368 if (acl->name && *acl->name)
1369 memprintf(err,
1370 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1371 acl->name, args[0], args[1], sample_ckp_names(where));
1372 else
1373 memprintf(err,
1374 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1375 args[0], args[1],
1376 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1377 sample_ckp_names(where));
1378 warn++;
1379 }
1380 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1381 if (acl->name && *acl->name)
1382 memprintf(err,
1383 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1384 acl->name, kw, sample_ckp_names(where));
1385 else
1386 memprintf(err,
1387 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1388 kw, sample_ckp_names(where));
1389 warn++;
1390 }
1391
1392 /* the following function directly emits the warning */
1393 warnif_misplaced_tcp_sess(curpx, file, line, args[0]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001394 LIST_APPEND(&curpx->tcp_req.l5_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001395 }
1396 else {
1397 if (curpx == defpx)
1398 memprintf(err,
1399 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1400 args[0], args[1]);
1401 else
1402 memprintf(err,
1403 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1404 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1405 goto error;
1406 }
1407
1408 return warn;
1409 error:
Willy Tarreau388c0f22022-03-17 20:26:54 +01001410 free_act_rule(rule);
Willy Tarreau39713102016-11-25 15:49:32 +01001411 return -1;
1412}
1413
1414static struct cfg_kw_list cfg_kws = {ILH, {
1415 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1416 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
1417 { 0, NULL, NULL },
1418}};
1419
Willy Tarreau0108d902018-11-25 19:14:37 +01001420INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau39713102016-11-25 15:49:32 +01001421
1422/*
1423 * Local variables:
1424 * c-indent-level: 8
1425 * c-basic-offset: 8
1426 * End:
1427 */