blob: 886dd9ed43cb4f17fb359f24928768d3818569f4 [file] [log] [blame]
Willy Tarreau39713102016-11-25 15:49:32 +01001/*
2 * "tcp" rules processing
3 *
4 * Copyright 2000-2016 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
Willy Tarreaudcc048a2020-06-04 19:11:43 +020012#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020013#include <haproxy/action.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020014#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <haproxy/arg-t.h>
Willy Tarreau278161c2020-06-04 11:18:28 +020016#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020017#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020018#include <haproxy/channel.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020019#include <haproxy/connection.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020020#include <haproxy/global.h>
Willy Tarreau853b2972020-05-27 18:01:47 +020021#include <haproxy/list.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020022#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020023#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020024#include <haproxy/sample.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020025#include <haproxy/stick_table.h>
26#include <haproxy/stream-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020027#include <haproxy/stream_interface.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020028#include <haproxy/tcp_rules.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020029#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/tools.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020031#include <haproxy/trace.h>
Willy Tarreau39713102016-11-25 15:49:32 +010032
Willy Tarreau39713102016-11-25 15:49:32 +010033
Christopher Fauleteea8fc72019-11-05 16:18:10 +010034#define TRACE_SOURCE &trace_strm
35
Willy Tarreau39713102016-11-25 15:49:32 +010036/* List head of all known action keywords for "tcp-request connection" */
37struct list tcp_req_conn_keywords = LIST_HEAD_INIT(tcp_req_conn_keywords);
38struct list tcp_req_sess_keywords = LIST_HEAD_INIT(tcp_req_sess_keywords);
39struct list tcp_req_cont_keywords = LIST_HEAD_INIT(tcp_req_cont_keywords);
40struct list tcp_res_cont_keywords = LIST_HEAD_INIT(tcp_res_cont_keywords);
41
42/*
43 * Register keywords.
44 */
45void tcp_req_conn_keywords_register(struct action_kw_list *kw_list)
46{
Willy Tarreau2b718102021-04-21 07:32:39 +020047 LIST_APPEND(&tcp_req_conn_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010048}
49
50void tcp_req_sess_keywords_register(struct action_kw_list *kw_list)
51{
Willy Tarreau2b718102021-04-21 07:32:39 +020052 LIST_APPEND(&tcp_req_sess_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010053}
54
55void tcp_req_cont_keywords_register(struct action_kw_list *kw_list)
56{
Willy Tarreau2b718102021-04-21 07:32:39 +020057 LIST_APPEND(&tcp_req_cont_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010058}
59
60void tcp_res_cont_keywords_register(struct action_kw_list *kw_list)
61{
Willy Tarreau2b718102021-04-21 07:32:39 +020062 LIST_APPEND(&tcp_res_cont_keywords, &kw_list->list);
Willy Tarreau39713102016-11-25 15:49:32 +010063}
64
65/*
66 * Return the struct tcp_req_action_kw associated to a keyword.
67 */
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010068struct action_kw *tcp_req_conn_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010069{
70 return action_lookup(&tcp_req_conn_keywords, kw);
71}
72
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010073struct action_kw *tcp_req_sess_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010074{
75 return action_lookup(&tcp_req_sess_keywords, kw);
76}
77
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010078struct action_kw *tcp_req_cont_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010079{
80 return action_lookup(&tcp_req_cont_keywords, kw);
81}
82
Thierry Fournier7a71a6d2020-11-28 17:40:24 +010083struct action_kw *tcp_res_cont_action(const char *kw)
Willy Tarreau39713102016-11-25 15:49:32 +010084{
85 return action_lookup(&tcp_res_cont_keywords, kw);
86}
87
88/* This function performs the TCP request analysis on the current request. It
89 * returns 1 if the processing can continue on next analysers, or zero if it
90 * needs more data, encounters an error, or wants to immediately abort the
91 * request. It relies on buffers flags, and updates s->req->analysers. The
92 * function may be called for frontend rules and backend rules. It only relies
93 * on the backend pointer so this works for both cases.
94 */
95int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
96{
Christopher Fauletc8016d02021-10-13 15:34:49 +020097 struct list *def_rules, *rules;
Willy Tarreau39713102016-11-25 15:49:32 +010098 struct session *sess = s->sess;
99 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100100 int partial;
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100101 int act_opts = 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100102
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100103 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100104
Christopher Fauletc8016d02021-10-13 15:34:49 +0200105 def_rules = ((s->be->defpx && (an_bit == AN_REQ_INSPECT_FE || s->be->defpx != sess->fe->defpx)) ? &s->be->defpx->tcp_req.inspect_rules : NULL);
106 rules = &s->be->tcp_req.inspect_rules;
107
Willy Tarreau39713102016-11-25 15:49:32 +0100108 /* We don't know whether we have enough data, so must proceed
109 * this way :
110 * - iterate through all rules in their declaration order
111 * - if one rule returns MISS, it means the inspect delay is
112 * not over yet, then return immediately, otherwise consider
113 * it as a non-match.
114 * - if one rule returns OK, then return OK
115 * - if one rule returns KO, then return KO
116 */
117
Christopher Fauletcb59e0b2021-09-30 14:56:30 +0200118 if ((req->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)) || channel_full(req, global.tune.maxrewrite) ||
Christopher Faulet78335962021-09-23 14:46:32 +0200119 si_rx_blocked_room(chn_prod(req)) ||
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200120 !s->be->tcp_req.inspect_delay || tick_is_expired(s->rules_exp, now_ms))
Willy Tarreau39713102016-11-25 15:49:32 +0100121 partial = SMP_OPT_FINAL;
122 else
123 partial = 0;
124
125 /* If "the current_rule_list" match the executed rule list, we are in
126 * resume condition. If a resume is needed it is always in the action
127 * and never in the ACL or converters. In this case, we initialise the
128 * current rule, and go to the action execution point.
129 */
130 if (s->current_rule) {
131 rule = s->current_rule;
132 s->current_rule = NULL;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200133 if ((def_rules && s->current_rule_list == def_rules) || s->current_rule_list == rules)
Willy Tarreau39713102016-11-25 15:49:32 +0100134 goto resume_execution;
135 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200136 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Willy Tarreau39713102016-11-25 15:49:32 +0100137
Christopher Fauletc8016d02021-10-13 15:34:49 +0200138 restart:
139 list_for_each_entry(rule, s->current_rule_list, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100140 enum acl_test_res ret = ACL_TEST_PASS;
141
142 if (rule->cond) {
143 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial);
144 if (ret == ACL_TEST_MISS)
145 goto missing_data;
146
147 ret = acl_pass(ret);
148 if (rule->cond->pol == ACL_COND_UNLESS)
149 ret = !ret;
150 }
151
152 if (ret) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100153 act_opts |= ACT_OPT_FIRST;
Willy Tarreau39713102016-11-25 15:49:32 +0100154resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100155
156 /* Always call the action function if defined */
157 if (rule->action_ptr) {
158 if (partial & SMP_OPT_FINAL)
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100159 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100160
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100161 switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100162 case ACT_RET_CONT:
163 break;
164 case ACT_RET_STOP:
165 case ACT_RET_DONE:
166 goto end;
167 case ACT_RET_YIELD:
168 s->current_rule = rule;
Christopher Faulet99aaca92020-07-28 11:30:19 +0200169 if (partial & SMP_OPT_FINAL) {
170 send_log(s->be, LOG_WARNING,
171 "Internal error: yield not allowed if the inspect-delay expired "
172 "for the tcp-request content actions.");
173 goto internal;
174 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100175 goto missing_data;
176 case ACT_RET_DENY:
177 goto deny;
178 case ACT_RET_ABRT:
179 goto abort;
180 case ACT_RET_ERR:
181 goto internal;
182 case ACT_RET_INV:
183 goto invalid;
184 }
185 continue; /* eval the next rule */
186 }
187
188 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100189 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100190 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100191 }
192 else if (rule->action == ACT_ACTION_DENY) {
Christopher Faulet282992e2019-12-16 12:34:31 +0100193 goto deny;
Willy Tarreau39713102016-11-25 15:49:32 +0100194 }
Willy Tarreau39713102016-11-25 15:49:32 +0100195 }
196 }
197
Christopher Fauletc8016d02021-10-13 15:34:49 +0200198 if (def_rules && s->current_rule_list == def_rules) {
199 s->current_rule_list = rules;
200 goto restart;
201 }
202
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100203 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100204 /* if we get there, it means we have no rule which matches, or
205 * we have an explicit accept, so we apply the default accept.
206 */
207 req->analysers &= ~an_bit;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200208 s->current_rule = s->current_rule_list = NULL;
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200209 req->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100210 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100211 return 1;
212
213 missing_data:
214 channel_dont_connect(req);
215 /* just set the request timeout once at the beginning of the request */
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200216 if (!tick_isset(s->rules_exp) && s->be->tcp_req.inspect_delay)
217 s->rules_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
218 req->analyse_exp = tick_first((tick_is_expired(req->analyse_exp, now_ms) ? 0 : req->analyse_exp), s->rules_exp);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100219 DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100220 return 0;
221
Christopher Faulet282992e2019-12-16 12:34:31 +0100222 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +0200223 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100224 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200225 _HA_ATOMIC_INC(&sess->listener->counters->denied_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100226 goto reject;
227
228 internal:
Willy Tarreau4781b152021-04-06 13:53:36 +0200229 _HA_ATOMIC_INC(&sess->fe->fe_counters.internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100230 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200231 _HA_ATOMIC_INC(&sess->listener->counters->internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100232 if (!(s->flags & SF_ERR_MASK))
233 s->flags |= SF_ERR_INTERNAL;
234 goto reject;
235
236 invalid:
Willy Tarreau4781b152021-04-06 13:53:36 +0200237 _HA_ATOMIC_INC(&sess->fe->fe_counters.failed_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100238 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200239 _HA_ATOMIC_INC(&sess->listener->counters->failed_req);
Christopher Faulet282992e2019-12-16 12:34:31 +0100240
241 reject:
242 si_must_kill_conn(chn_prod(req));
243 channel_abort(req);
244 channel_abort(&s->res);
245
246 abort:
247 req->analysers &= AN_REQ_FLT_END;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200248 s->current_rule = s->current_rule_list = NULL;
249 req->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Faulet282992e2019-12-16 12:34:31 +0100250
251 if (!(s->flags & SF_ERR_MASK))
252 s->flags |= SF_ERR_PRXCOND;
253 if (!(s->flags & SF_FINST_MASK))
254 s->flags |= SF_FINST_R;
255 DBG_TRACE_DEVEL("leaving on error|deny|abort", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_TCP_ERR, s);
256 return 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100257}
258
259/* This function performs the TCP response analysis on the current response. It
260 * returns 1 if the processing can continue on next analysers, or zero if it
261 * needs more data, encounters an error, or wants to immediately abort the
262 * response. It relies on buffers flags, and updates s->rep->analysers. The
263 * function may be called for backend rules.
264 */
265int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
266{
Christopher Fauletc8016d02021-10-13 15:34:49 +0200267 struct list *def_rules, *rules;
Willy Tarreau39713102016-11-25 15:49:32 +0100268 struct session *sess = s->sess;
269 struct act_rule *rule;
270 int partial;
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100271 int act_opts = 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100272
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100273 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100274
Christopher Fauletc8016d02021-10-13 15:34:49 +0200275 def_rules = (s->be->defpx ? &s->be->defpx->tcp_rep.inspect_rules : NULL);
276 rules = &s->be->tcp_rep.inspect_rules;
277
Willy Tarreau39713102016-11-25 15:49:32 +0100278 /* We don't know whether we have enough data, so must proceed
279 * this way :
280 * - iterate through all rules in their declaration order
281 * - if one rule returns MISS, it means the inspect delay is
282 * not over yet, then return immediately, otherwise consider
283 * it as a non-match.
284 * - if one rule returns OK, then return OK
285 * - if one rule returns KO, then return KO
286 */
Christopher Fauletcb59e0b2021-09-30 14:56:30 +0200287 if ((rep->flags & (CF_EOI|CF_SHUTR|CF_READ_ERROR)) || channel_full(rep, global.tune.maxrewrite) ||
Christopher Faulet78335962021-09-23 14:46:32 +0200288 si_rx_blocked_room(chn_prod(rep)) ||
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200289 !s->be->tcp_rep.inspect_delay || tick_is_expired(s->rules_exp, now_ms))
Willy Tarreau39713102016-11-25 15:49:32 +0100290 partial = SMP_OPT_FINAL;
291 else
292 partial = 0;
293
294 /* If "the current_rule_list" match the executed rule list, we are in
295 * resume condition. If a resume is needed it is always in the action
296 * and never in the ACL or converters. In this case, we initialise the
297 * current rule, and go to the action execution point.
298 */
299 if (s->current_rule) {
300 rule = s->current_rule;
301 s->current_rule = NULL;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200302 if ((def_rules && s->current_rule_list == def_rules) || s->current_rule_list == rules)
Willy Tarreau39713102016-11-25 15:49:32 +0100303 goto resume_execution;
304 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200305 s->current_rule_list = ((!def_rules || s->current_rule_list == def_rules) ? rules : def_rules);
Willy Tarreau39713102016-11-25 15:49:32 +0100306
Christopher Fauletc8016d02021-10-13 15:34:49 +0200307 restart:
308 list_for_each_entry(rule, s->current_rule_list, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100309 enum acl_test_res ret = ACL_TEST_PASS;
310
311 if (rule->cond) {
312 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200313 if (ret == ACL_TEST_MISS)
314 goto missing_data;
Willy Tarreau39713102016-11-25 15:49:32 +0100315
316 ret = acl_pass(ret);
317 if (rule->cond->pol == ACL_COND_UNLESS)
318 ret = !ret;
319 }
320
321 if (ret) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100322 act_opts |= ACT_OPT_FIRST;
Willy Tarreau39713102016-11-25 15:49:32 +0100323resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100324 /* Always call the action function if defined */
325 if (rule->action_ptr) {
326 if (partial & SMP_OPT_FINAL)
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100327 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100328
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100329 switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100330 case ACT_RET_CONT:
331 break;
332 case ACT_RET_STOP:
333 case ACT_RET_DONE:
334 goto end;
335 case ACT_RET_YIELD:
336 s->current_rule = rule;
Christopher Faulet99aaca92020-07-28 11:30:19 +0200337 if (partial & SMP_OPT_FINAL) {
338 send_log(s->be, LOG_WARNING,
339 "Internal error: yield not allowed if the inspect-delay expired "
340 "for the tcp-response content actions.");
341 goto internal;
342 }
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200343 channel_dont_close(rep);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100344 goto missing_data;
345 case ACT_RET_DENY:
346 goto deny;
347 case ACT_RET_ABRT:
348 goto abort;
349 case ACT_RET_ERR:
350 goto internal;
351 case ACT_RET_INV:
352 goto invalid;
353 }
354 continue; /* eval the next rule */
355 }
356
357 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100358 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100359 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100360 }
361 else if (rule->action == ACT_ACTION_DENY) {
Christopher Faulet282992e2019-12-16 12:34:31 +0100362 goto deny;
Willy Tarreau39713102016-11-25 15:49:32 +0100363 }
364 else if (rule->action == ACT_TCP_CLOSE) {
365 chn_prod(rep)->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100366 si_must_kill_conn(chn_prod(rep));
Willy Tarreau39713102016-11-25 15:49:32 +0100367 si_shutr(chn_prod(rep));
368 si_shutw(chn_prod(rep));
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100369 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100370 }
371 }
372 }
373
Christopher Fauletc8016d02021-10-13 15:34:49 +0200374 if (def_rules && s->current_rule_list == def_rules) {
375 s->current_rule_list = rules;
376 goto restart;
377 }
378
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100379 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100380 /* if we get there, it means we have no rule which matches, or
381 * we have an explicit accept, so we apply the default accept.
382 */
383 rep->analysers &= ~an_bit;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200384 s->current_rule = s->current_rule_list = NULL;
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200385 rep->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100386 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100387 return 1;
Christopher Faulet282992e2019-12-16 12:34:31 +0100388
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100389 missing_data:
Christopher Faulet54f3e182020-07-29 12:00:23 +0200390 /* just set the analyser timeout once at the beginning of the response */
Christopher Faulet2747fbb2020-07-28 11:56:13 +0200391 if (!tick_isset(s->rules_exp) && s->be->tcp_rep.inspect_delay)
392 s->rules_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
393 rep->analyse_exp = tick_first((tick_is_expired(rep->analyse_exp, now_ms) ? 0 : rep->analyse_exp), s->rules_exp);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100394 DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
395 return 0;
396
Christopher Faulet282992e2019-12-16 12:34:31 +0100397 deny:
Willy Tarreau4781b152021-04-06 13:53:36 +0200398 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.denied_resp);
399 _HA_ATOMIC_INC(&s->be->be_counters.denied_resp);
William Lallemand36119de2021-03-08 15:26:48 +0100400 if (s->sess->listener && s->sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200401 _HA_ATOMIC_INC(&s->sess->listener->counters->denied_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100402 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200403 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.denied_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100404 goto reject;
405
406 internal:
Willy Tarreau4781b152021-04-06 13:53:36 +0200407 _HA_ATOMIC_INC(&s->sess->fe->fe_counters.internal_errors);
408 _HA_ATOMIC_INC(&s->be->be_counters.internal_errors);
William Lallemand36119de2021-03-08 15:26:48 +0100409 if (s->sess->listener && s->sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200410 _HA_ATOMIC_INC(&s->sess->listener->counters->internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100411 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200412 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.internal_errors);
Christopher Faulet282992e2019-12-16 12:34:31 +0100413 if (!(s->flags & SF_ERR_MASK))
414 s->flags |= SF_ERR_INTERNAL;
415 goto reject;
416
417 invalid:
Willy Tarreau4781b152021-04-06 13:53:36 +0200418 _HA_ATOMIC_INC(&s->be->be_counters.failed_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100419 if (objt_server(s->target))
Willy Tarreau4781b152021-04-06 13:53:36 +0200420 _HA_ATOMIC_INC(&__objt_server(s->target)->counters.failed_resp);
Christopher Faulet282992e2019-12-16 12:34:31 +0100421
422 reject:
423 si_must_kill_conn(chn_prod(rep));
424 channel_abort(rep);
425 channel_abort(&s->req);
426
427 abort:
Christopher Faulet19dbf2d2020-07-28 11:40:07 +0200428 rep->analysers &= AN_RES_FLT_END;
Christopher Fauletc8016d02021-10-13 15:34:49 +0200429 s->current_rule = s->current_rule_list = NULL;
430 rep->analyse_exp = s->rules_exp = TICK_ETERNITY;
Christopher Faulet282992e2019-12-16 12:34:31 +0100431
432 if (!(s->flags & SF_ERR_MASK))
433 s->flags |= SF_ERR_PRXCOND;
434 if (!(s->flags & SF_FINST_MASK))
435 s->flags |= SF_FINST_D;
436 DBG_TRACE_DEVEL("leaving on error", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_TCP_ERR, s);
437 return 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100438}
439
440
441/* This function performs the TCP layer4 analysis on the current request. It
442 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
443 * matches or if no more rule matches. It can only use rules which don't need
444 * any data. This only works on connection-based client-facing stream interfaces.
445 */
446int tcp_exec_l4_rules(struct session *sess)
447{
Christopher Fauletc8016d02021-10-13 15:34:49 +0200448 struct proxy *px = sess->fe;
Willy Tarreau39713102016-11-25 15:49:32 +0100449 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100450 struct connection *conn = objt_conn(sess->origin);
451 int result = 1;
452 enum acl_test_res ret;
453
454 if (!conn)
455 return result;
456
Christopher Fauletc8016d02021-10-13 15:34:49 +0200457 if (sess->fe->defpx)
458 px = sess->fe->defpx;
459
460 restart:
461 list_for_each_entry(rule, &px->tcp_req.l4_rules, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100462 ret = ACL_TEST_PASS;
463
464 if (rule->cond) {
465 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
466 ret = acl_pass(ret);
467 if (rule->cond->pol == ACL_COND_UNLESS)
468 ret = !ret;
469 }
470
471 if (ret) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100472 /* Always call the action function if defined */
473 if (rule->action_ptr) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100474 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_OPT_FINAL | ACT_OPT_FIRST)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100475 case ACT_RET_YIELD:
476 /* yield is not allowed at this point. If this return code is
477 * used it is a bug, so I prefer to abort the process.
478 */
479 send_log(sess->fe, LOG_WARNING,
480 "Internal error: yield not allowed with tcp-request connection actions.");
481 /* fall through */
482 case ACT_RET_STOP:
483 case ACT_RET_DONE:
484 goto end;
485 case ACT_RET_CONT:
486 break;
487 case ACT_RET_DENY:
488 case ACT_RET_ABRT:
489 case ACT_RET_ERR:
490 case ACT_RET_INV:
491 result = 0;
492 goto end;
493 }
494 continue; /* eval the next rule */
495 }
496
497 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100498 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100499 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100500 }
501 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200502 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_conn);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100503 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200504 _HA_ATOMIC_INC(&sess->listener->counters->denied_conn);
Willy Tarreau39713102016-11-25 15:49:32 +0100505
506 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100507 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100508 }
Willy Tarreau39713102016-11-25 15:49:32 +0100509 else if (rule->action == ACT_TCP_EXPECT_PX) {
Willy Tarreau4450b582020-01-23 15:23:13 +0100510 if (!(conn->flags & CO_FL_HANDSHAKE)) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200511 if (xprt_add_hs(conn) < 0) {
512 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100513 goto end;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200514 }
515 }
Willy Tarreau39713102016-11-25 15:49:32 +0100516 conn->flags |= CO_FL_ACCEPT_PROXY;
Willy Tarreau39713102016-11-25 15:49:32 +0100517 }
518 else if (rule->action == ACT_TCP_EXPECT_CIP) {
Willy Tarreau4450b582020-01-23 15:23:13 +0100519 if (!(conn->flags & CO_FL_HANDSHAKE)) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200520 if (xprt_add_hs(conn) < 0) {
521 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100522 goto end;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200523 }
524 }
Willy Tarreau39713102016-11-25 15:49:32 +0100525 conn->flags |= CO_FL_ACCEPT_CIP;
Willy Tarreau39713102016-11-25 15:49:32 +0100526 }
Willy Tarreau39713102016-11-25 15:49:32 +0100527 }
528 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200529
530 if (px != sess->fe) {
531 px = sess->fe;
532 goto restart;
533 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100534 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100535 return result;
536}
537
538/* This function performs the TCP layer5 analysis on the current request. It
539 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
540 * matches or if no more rule matches. It can only use rules which don't need
541 * any data. This only works on session-based client-facing stream interfaces.
542 * An example of valid use case is to track a stick-counter on the source
543 * address extracted from the proxy protocol.
544 */
545int tcp_exec_l5_rules(struct session *sess)
546{
Christopher Fauletc8016d02021-10-13 15:34:49 +0200547 struct proxy *px = sess->fe;
Willy Tarreau39713102016-11-25 15:49:32 +0100548 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100549 int result = 1;
550 enum acl_test_res ret;
551
Christopher Fauletc8016d02021-10-13 15:34:49 +0200552 if (sess->fe->defpx)
553 px = sess->fe->defpx;
554
555 restart:
556 list_for_each_entry(rule, &px->tcp_req.l5_rules, list) {
Willy Tarreau39713102016-11-25 15:49:32 +0100557 ret = ACL_TEST_PASS;
558
559 if (rule->cond) {
560 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
561 ret = acl_pass(ret);
562 if (rule->cond->pol == ACL_COND_UNLESS)
563 ret = !ret;
564 }
565
566 if (ret) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100567 /* Always call the action function if defined */
568 if (rule->action_ptr) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100569 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_OPT_FINAL | ACT_OPT_FIRST)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100570 case ACT_RET_YIELD:
571 /* yield is not allowed at this point. If this return code is
572 * used it is a bug, so I prefer to abort the process.
573 */
574 send_log(sess->fe, LOG_WARNING,
575 "Internal error: yield not allowed with tcp-request session actions.");
576 /* fall through */
577 case ACT_RET_STOP:
578 case ACT_RET_DONE:
579 goto end;
580 case ACT_RET_CONT:
581 break;
582 case ACT_RET_DENY:
583 case ACT_RET_ABRT:
584 case ACT_RET_ERR:
585 case ACT_RET_INV:
586 result = 0;
587 goto end;
588 }
589 continue; /* eval the next rule */
590 }
591
592 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100593 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100594 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100595 }
596 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau4781b152021-04-06 13:53:36 +0200597 _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_sess);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100598 if (sess->listener && sess->listener->counters)
Willy Tarreau4781b152021-04-06 13:53:36 +0200599 _HA_ATOMIC_INC(&sess->listener->counters->denied_sess);
Willy Tarreau39713102016-11-25 15:49:32 +0100600
601 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100602 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100603 }
Willy Tarreau39713102016-11-25 15:49:32 +0100604 }
605 }
Christopher Fauletc8016d02021-10-13 15:34:49 +0200606
607 if (px != sess->fe) {
608 px = sess->fe;
609 goto restart;
610 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100611 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100612 return result;
613}
614
615/* Parse a tcp-response rule. Return a negative value in case of failure */
616static int tcp_parse_response_rule(char **args, int arg, int section_type,
Willy Tarreau01825162021-03-09 09:53:46 +0100617 struct proxy *curpx, const struct proxy *defpx,
Willy Tarreau39713102016-11-25 15:49:32 +0100618 struct act_rule *rule, char **err,
619 unsigned int where,
620 const char *file, int line)
621{
Christopher Fauletee08d6c2021-10-13 15:40:15 +0200622 if ((curpx == defpx && strlen(defpx->id) == 0) || !(curpx->cap & PR_CAP_BE)) {
623 memprintf(err, "%s %s is only allowed in 'backend' sections or 'defaults' section with a name",
Willy Tarreau39713102016-11-25 15:49:32 +0100624 args[0], args[1]);
625 return -1;
626 }
627
628 if (strcmp(args[arg], "accept") == 0) {
629 arg++;
630 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100631 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100632 }
633 else if (strcmp(args[arg], "reject") == 0) {
634 arg++;
635 rule->action = ACT_ACTION_DENY;
Christopher Faulet245cf792019-12-18 14:58:12 +0100636 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100637 }
638 else if (strcmp(args[arg], "close") == 0) {
639 arg++;
640 rule->action = ACT_TCP_CLOSE;
Christopher Faulet245cf792019-12-18 14:58:12 +0100641 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100642 }
643 else {
644 struct action_kw *kw;
645 kw = tcp_res_cont_action(args[arg]);
646 if (kw) {
647 arg++;
Willy Tarreau39713102016-11-25 15:49:32 +0100648 rule->kw = kw;
649 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
650 return -1;
651 } else {
Willy Tarreaudb67b0e2021-03-12 13:46:10 +0100652 const char *extra[] = { "accept", "reject", "close", NULL };
653 const char *best = action_suggest(args[arg], &tcp_res_cont_keywords, extra);
654
Willy Tarreau39713102016-11-25 15:49:32 +0100655 action_build_list(&tcp_res_cont_keywords, &trash);
656 memprintf(err,
Willy Tarreaudb67b0e2021-03-12 13:46:10 +0100657 "'%s %s' expects 'accept', 'close', 'reject', %s in %s '%s' (got '%s').%s%s%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200658 args[0], args[1], trash.area,
Willy Tarreaudb67b0e2021-03-12 13:46:10 +0100659 proxy_type_str(curpx), curpx->id, args[arg],
660 best ? " Did you mean '" : "",
661 best ? best : "",
662 best ? "' maybe ?" : "");
Willy Tarreau39713102016-11-25 15:49:32 +0100663 return -1;
664 }
665 }
666
667 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +0200668 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +0100669 memprintf(err,
670 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
671 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
672 return -1;
673 }
674 }
675 else if (*args[arg]) {
676 memprintf(err,
677 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
678 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
679 return -1;
680 }
681 return 0;
682}
683
Christopher Fauletac98d812019-12-18 09:20:16 +0100684
685/* This function executes a track-sc* actions. On success, it returns
686 * ACT_RET_CONT. If it must yield, it return ACT_RET_YIELD. Otherwsize
687 * ACT_RET_ERR is returned.
688 */
689static enum act_return tcp_action_track_sc(struct act_rule *rule, struct proxy *px,
690 struct session *sess, struct stream *s, int flags)
691{
692 struct stksess *ts;
693 struct stktable *t;
694 struct stktable_key *key;
695 struct sample smp;
696 int opt;
697
Christopher Faulet67307792020-02-10 09:54:49 +0100698 opt = SMP_OPT_DIR_REQ;
Christopher Fauletac98d812019-12-18 09:20:16 +0100699 if (flags & ACT_FLAG_FINAL)
700 opt |= SMP_OPT_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100701
Christopher Fauletac98d812019-12-18 09:20:16 +0100702 t = rule->arg.trk_ctr.table.t;
Christopher Faulet67307792020-02-10 09:54:49 +0100703 if (rule->from == ACT_F_TCP_REQ_CNT) { /* L7 rules: use the stream */
704 if (stkctr_entry(&s->stkctr[rule->action]))
705 goto end;
Christopher Fauletac98d812019-12-18 09:20:16 +0100706
Christopher Faulet67307792020-02-10 09:54:49 +0100707 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, &smp);
Christopher Fauletac98d812019-12-18 09:20:16 +0100708
Christopher Faulet67307792020-02-10 09:54:49 +0100709 if ((smp.flags & SMP_F_MAY_CHANGE) && !(flags & ACT_FLAG_FINAL))
710 return ACT_RET_YIELD; /* key might appear later */
711
712 if (key && (ts = stktable_get_entry(t, key))) {
713 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
Christopher Fauletac98d812019-12-18 09:20:16 +0100714 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
715 if (sess->fe != s->be)
716 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
717 }
718 }
Christopher Faulet67307792020-02-10 09:54:49 +0100719 else { /* L4/L5 rules: use the session */
720 if (stkctr_entry(&sess->stkctr[rule->action]))
721 goto end;
722
723 key = stktable_fetch_key(t, sess->fe, sess, NULL, opt, rule->arg.trk_ctr.expr, NULL);
724 if (key && (ts = stktable_get_entry(t, key)))
725 stream_track_stkctr(&sess->stkctr[rule->action], t, ts);
726 }
Christopher Fauletac98d812019-12-18 09:20:16 +0100727
728 end:
729 return ACT_RET_CONT;
730}
Willy Tarreau39713102016-11-25 15:49:32 +0100731
Christopher Fauletd73b96d2019-12-19 17:27:03 +0100732/* This function executes a capture actions. It executes a fetch expression,
733 * turns the result into a string and puts it in a capture slot. On success, it
734 * returns ACT_RET_CONT. If it must yield, it return ACT_RET_YIELD. Otherwsize
735 * ACT_RET_ERR is returned.
736 */
737static enum act_return tcp_action_capture(struct act_rule *rule, struct proxy *px,
738 struct session *sess, struct stream *s, int flags)
739{
740 struct sample *key;
741 struct cap_hdr *h = rule->arg.cap.hdr;
742 char **cap = s->req_cap;
743 int len, opt;
744
745 opt = ((rule->from == ACT_F_TCP_REQ_CNT) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
746 if (flags & ACT_FLAG_FINAL)
747 opt |= SMP_OPT_FINAL;
748
749 key = sample_fetch_as_type(s->be, sess, s, opt, rule->arg.cap.expr, SMP_T_STR);
750 if (!key)
751 goto end;
752
753 if ((key->flags & SMP_F_MAY_CHANGE) && !(flags & ACT_FLAG_FINAL))
754 return ACT_RET_YIELD; /* key might appear later */
755
756 if (cap[h->index] == NULL) {
757 cap[h->index] = pool_alloc(h->pool);
758 if (cap[h->index] == NULL) /* no more capture memory, ignore error */
759 goto end;
760 }
761
762 len = key->data.u.str.data;
763 if (len > h->len)
764 len = h->len;
765
766 memcpy(cap[h->index], key->data.u.str.area, len);
767 cap[h->index][len] = 0;
768
769 end:
770 return ACT_RET_CONT;
771}
772
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100773static void release_tcp_capture(struct act_rule * rule)
774{
775 release_sample_expr(rule->arg.cap.expr);
776}
777
778
779static void release_tcp_track_sc(struct act_rule * rule)
780{
781 release_sample_expr(rule->arg.trk_ctr.expr);
782}
783
Willy Tarreau39713102016-11-25 15:49:32 +0100784/* Parse a tcp-request rule. Return a negative value in case of failure */
785static int tcp_parse_request_rule(char **args, int arg, int section_type,
Willy Tarreau01825162021-03-09 09:53:46 +0100786 struct proxy *curpx, const struct proxy *defpx,
Willy Tarreau39713102016-11-25 15:49:32 +0100787 struct act_rule *rule, char **err,
788 unsigned int where, const char *file, int line)
789{
Christopher Fauletee08d6c2021-10-13 15:40:15 +0200790 if (curpx == defpx && strlen(defpx->id) == 0) {
791 memprintf(err, "%s %s is not allowed in anonymous 'defaults' sections",
792 args[0], args[1]);
Willy Tarreau39713102016-11-25 15:49:32 +0100793 return -1;
794 }
795
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100796 if (strcmp(args[arg], "accept") == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100797 arg++;
798 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100799 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100800 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100801 else if (strcmp(args[arg], "reject") == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100802 arg++;
803 rule->action = ACT_ACTION_DENY;
Christopher Faulet245cf792019-12-18 14:58:12 +0100804 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100805 }
806 else if (strcmp(args[arg], "capture") == 0) {
807 struct sample_expr *expr;
808 struct cap_hdr *hdr;
809 int kw = arg;
810 int len = 0;
811
812 if (!(curpx->cap & PR_CAP_FE)) {
813 memprintf(err,
814 "'%s %s %s' : proxy '%s' has no frontend capability",
815 args[0], args[1], args[kw], curpx->id);
816 return -1;
817 }
818
819 if (!(where & SMP_VAL_FE_REQ_CNT)) {
820 memprintf(err,
821 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
822 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
823 return -1;
824 }
825
826 arg++;
827
828 curpx->conf.args.ctx = ARGC_CAP;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100829 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args, NULL);
Willy Tarreau39713102016-11-25 15:49:32 +0100830 if (!expr) {
831 memprintf(err,
832 "'%s %s %s' : %s",
833 args[0], args[1], args[kw], *err);
834 return -1;
835 }
836
837 if (!(expr->fetch->val & where)) {
838 memprintf(err,
839 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
840 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100841 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100842 return -1;
843 }
844
845 if (strcmp(args[arg], "len") == 0) {
846 arg++;
847 if (!args[arg]) {
848 memprintf(err,
849 "'%s %s %s' : missing length value",
850 args[0], args[1], args[kw]);
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100851 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100852 return -1;
853 }
854 /* we copy the table name for now, it will be resolved later */
855 len = atoi(args[arg]);
856 if (len <= 0) {
857 memprintf(err,
858 "'%s %s %s' : length must be > 0",
859 args[0], args[1], args[kw]);
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100860 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100861 return -1;
862 }
863 arg++;
864 }
865
866 if (!len) {
867 memprintf(err,
868 "'%s %s %s' : a positive 'len' argument is mandatory",
869 args[0], args[1], args[kw]);
870 free(expr);
871 return -1;
872 }
873
874 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Breton8cb03362021-05-17 10:08:16 +0200875 if (!hdr) {
876 memprintf(err, "parsing [%s:%d] : out of memory", file, line);
877 release_sample_expr(expr);
878 return -1;
879 }
Willy Tarreau39713102016-11-25 15:49:32 +0100880 hdr->next = curpx->req_cap;
881 hdr->name = NULL; /* not a header capture */
882 hdr->namelen = 0;
883 hdr->len = len;
884 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
885 hdr->index = curpx->nb_req_cap++;
886
887 curpx->req_cap = hdr;
888 curpx->to_log |= LW_REQHDR;
889
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200890 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreau39713102016-11-25 15:49:32 +0100891 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
892
893 rule->arg.cap.expr = expr;
894 rule->arg.cap.hdr = hdr;
Christopher Fauletd73b96d2019-12-19 17:27:03 +0100895 rule->action = ACT_CUSTOM;
896 rule->action_ptr = tcp_action_capture;
897 rule->check_ptr = check_capture;
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100898 rule->release_ptr = release_tcp_capture;
Willy Tarreau39713102016-11-25 15:49:32 +0100899 }
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100900 else if (strncmp(args[arg], "track-sc", 8) == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100901 struct sample_expr *expr;
902 int kw = arg;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100903 unsigned int tsc_num;
904 const char *tsc_num_str;
Willy Tarreau39713102016-11-25 15:49:32 +0100905
906 arg++;
907
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100908 tsc_num_str = &args[kw][8];
909 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1) {
910 memprintf(err, "'%s %s %s' : %s", args[0], args[1], args[kw], *err);
911 return -1;
912 }
913
Willy Tarreau39713102016-11-25 15:49:32 +0100914 curpx->conf.args.ctx = ARGC_TRK;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100915 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args, NULL);
Willy Tarreau39713102016-11-25 15:49:32 +0100916 if (!expr) {
917 memprintf(err,
918 "'%s %s %s' : %s",
919 args[0], args[1], args[kw], *err);
920 return -1;
921 }
922
923 if (!(expr->fetch->val & where)) {
924 memprintf(err,
925 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
926 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100927 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100928 return -1;
929 }
930
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200931 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreau39713102016-11-25 15:49:32 +0100932 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
933
934 if (strcmp(args[arg], "table") == 0) {
935 arg++;
936 if (!args[arg]) {
937 memprintf(err,
938 "'%s %s %s' : missing table name",
939 args[0], args[1], args[kw]);
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100940 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100941 return -1;
942 }
943 /* we copy the table name for now, it will be resolved later */
944 rule->arg.trk_ctr.table.n = strdup(args[arg]);
945 arg++;
946 }
Christopher Fauletac98d812019-12-18 09:20:16 +0100947 rule->action = tsc_num;
Willy Tarreau39713102016-11-25 15:49:32 +0100948 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +0100949 rule->action_ptr = tcp_action_track_sc;
Christopher Faulet78880fb2017-09-18 14:43:55 +0200950 rule->check_ptr = check_trk_action;
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100951 rule->release_ptr = release_tcp_track_sc;
Willy Tarreau39713102016-11-25 15:49:32 +0100952 }
953 else if (strcmp(args[arg], "expect-proxy") == 0) {
954 if (strcmp(args[arg+1], "layer4") != 0) {
955 memprintf(err,
956 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
957 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
958 return -1;
959 }
960
961 if (!(where & SMP_VAL_FE_CON_ACC)) {
962 memprintf(err,
963 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
964 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
965 return -1;
966 }
967
968 arg += 2;
969 rule->action = ACT_TCP_EXPECT_PX;
970 }
971 else if (strcmp(args[arg], "expect-netscaler-cip") == 0) {
972 if (strcmp(args[arg+1], "layer4") != 0) {
973 memprintf(err,
974 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
975 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
976 return -1;
977 }
978
979 if (!(where & SMP_VAL_FE_CON_ACC)) {
980 memprintf(err,
981 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
982 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
983 return -1;
984 }
985
986 arg += 2;
987 rule->action = ACT_TCP_EXPECT_CIP;
988 }
989 else {
990 struct action_kw *kw;
991 if (where & SMP_VAL_FE_CON_ACC) {
992 /* L4 */
993 kw = tcp_req_conn_action(args[arg]);
994 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +0100995 } else if (where & SMP_VAL_FE_SES_ACC) {
996 /* L5 */
997 kw = tcp_req_sess_action(args[arg]);
998 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +0100999 } else {
1000 /* L6 */
1001 kw = tcp_req_cont_action(args[arg]);
1002 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +01001003 }
1004 if (kw) {
1005 arg++;
1006 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
1007 return -1;
1008 } else {
Christopher Fauleta561ffb2021-03-19 08:53:26 +01001009 const char *extra[] = { "accept", "reject", "capture", "track-sc", "expect-proxy", "expect-netscaler-cip", NULL };
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001010 const char *best = NULL;
1011
1012
1013 if (where & SMP_VAL_FE_CON_ACC) {
Willy Tarreau39713102016-11-25 15:49:32 +01001014 action_build_list(&tcp_req_conn_keywords, &trash);
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001015 best = action_suggest(args[arg], &tcp_req_conn_keywords, extra);
1016 }
1017 else if (where & SMP_VAL_FE_SES_ACC) {
Willy Tarreau39713102016-11-25 15:49:32 +01001018 action_build_list(&tcp_req_sess_keywords, &trash);
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001019 best = action_suggest(args[arg], &tcp_req_sess_keywords, extra);
1020 }
1021 else {
Willy Tarreau39713102016-11-25 15:49:32 +01001022 action_build_list(&tcp_req_cont_keywords, &trash);
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001023 best = action_suggest(args[arg], &tcp_req_cont_keywords, extra);
1024 }
1025
Willy Tarreau39713102016-11-25 15:49:32 +01001026 memprintf(err,
Christopher Fauleta561ffb2021-03-19 08:53:26 +01001027 "'%s %s' expects 'accept', 'reject', 'capture', 'expect-proxy', 'expect-netscaler-cip', 'track-sc0' ... 'track-sc%d', %s "
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001028 "in %s '%s' (got '%s').%s%s%s\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001029 args[0], args[1], MAX_SESS_STKCTR-1,
1030 trash.area, proxy_type_str(curpx),
Willy Tarreaudb67b0e2021-03-12 13:46:10 +01001031 curpx->id, args[arg],
1032 best ? " Did you mean '" : "",
1033 best ? best : "",
1034 best ? "' maybe ?" : "");
Willy Tarreau39713102016-11-25 15:49:32 +01001035 return -1;
1036 }
1037 }
1038
1039 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +02001040 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +01001041 memprintf(err,
1042 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
1043 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
1044 return -1;
1045 }
1046 }
1047 else if (*args[arg]) {
1048 memprintf(err,
1049 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
1050 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
1051 return -1;
1052 }
1053 return 0;
1054}
1055
1056/* This function should be called to parse a line starting with the "tcp-response"
1057 * keyword.
1058 */
1059static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001060 const struct proxy *defpx, const char *file, int line,
Willy Tarreau39713102016-11-25 15:49:32 +01001061 char **err)
1062{
1063 const char *ptr = NULL;
1064 unsigned int val;
1065 int warn = 0;
1066 int arg;
1067 struct act_rule *rule;
1068 unsigned int where;
1069 const struct acl *acl;
1070 const char *kw;
1071
1072 if (!*args[1]) {
1073 memprintf(err, "missing argument for '%s' in %s '%s'",
1074 args[0], proxy_type_str(curpx), curpx->id);
1075 return -1;
1076 }
1077
1078 if (strcmp(args[1], "inspect-delay") == 0) {
Christopher Fauletee08d6c2021-10-13 15:40:15 +02001079 if ((curpx == defpx && strlen(defpx->id) == 0) || !(curpx->cap & PR_CAP_BE)) {
1080 memprintf(err, "%s %s is only allowed in 'backend' sections or 'defaults' section with a name",
Willy Tarreau39713102016-11-25 15:49:32 +01001081 args[0], args[1]);
1082 return -1;
1083 }
1084
1085 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1086 memprintf(err,
1087 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1088 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001089
1090 if (ptr == PARSE_TIME_OVER)
1091 memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]);
1092 else if (ptr == PARSE_TIME_UNDER)
1093 memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]);
1094 else if (ptr)
Willy Tarreau39713102016-11-25 15:49:32 +01001095 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
1096 return -1;
1097 }
1098
1099 if (curpx->tcp_rep.inspect_delay) {
1100 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1101 args[0], args[1], proxy_type_str(curpx), curpx->id);
1102 return 1;
1103 }
1104 curpx->tcp_rep.inspect_delay = val;
1105 return 0;
1106 }
1107
Willy Tarreaud535f802021-10-11 08:49:26 +02001108 rule = new_act_rule(ACT_F_TCP_RES_CNT, file, line);
Remi Tricot-Le Breton2ca42b42021-05-12 18:24:18 +02001109 if (!rule) {
1110 memprintf(err, "parsing [%s:%d] : out of memory", file, line);
1111 return -1;
1112 }
Willy Tarreau39713102016-11-25 15:49:32 +01001113 LIST_INIT(&rule->list);
1114 arg = 1;
1115 where = 0;
1116
1117 if (strcmp(args[1], "content") == 0) {
1118 arg++;
1119
1120 if (curpx->cap & PR_CAP_FE)
1121 where |= SMP_VAL_FE_RES_CNT;
1122 if (curpx->cap & PR_CAP_BE)
1123 where |= SMP_VAL_BE_RES_CNT;
Willy Tarreau39713102016-11-25 15:49:32 +01001124 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1125 goto error;
1126
1127 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1128 if (acl) {
1129 if (acl->name && *acl->name)
1130 memprintf(err,
1131 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1132 acl->name, args[0], args[1], sample_ckp_names(where));
1133 else
1134 memprintf(err,
1135 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1136 args[0], args[1],
1137 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1138 sample_ckp_names(where));
1139
1140 warn++;
1141 }
1142 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1143 if (acl->name && *acl->name)
1144 memprintf(err,
1145 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1146 acl->name, kw, sample_ckp_names(where));
1147 else
1148 memprintf(err,
1149 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1150 kw, sample_ckp_names(where));
1151 warn++;
1152 }
1153
Willy Tarreau2b718102021-04-21 07:32:39 +02001154 LIST_APPEND(&curpx->tcp_rep.inspect_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001155 }
1156 else {
1157 memprintf(err,
1158 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1159 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1160 goto error;
1161 }
1162
1163 return warn;
1164 error:
1165 free(rule);
1166 return -1;
1167}
1168
1169
1170/* This function should be called to parse a line starting with the "tcp-request"
1171 * keyword.
1172 */
1173static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01001174 const struct proxy *defpx, const char *file, int line,
Willy Tarreau39713102016-11-25 15:49:32 +01001175 char **err)
1176{
1177 const char *ptr = NULL;
1178 unsigned int val;
1179 int warn = 0;
1180 int arg;
1181 struct act_rule *rule;
1182 unsigned int where;
1183 const struct acl *acl;
1184 const char *kw;
1185
1186 if (!*args[1]) {
1187 if (curpx == defpx)
1188 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1189 else
1190 memprintf(err, "missing argument for '%s' in %s '%s'",
1191 args[0], proxy_type_str(curpx), curpx->id);
1192 return -1;
1193 }
1194
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001195 if (strcmp(args[1], "inspect-delay") == 0) {
Christopher Fauletee08d6c2021-10-13 15:40:15 +02001196 if (curpx == defpx && strlen(defpx->id) == 0) {
1197 memprintf(err, "%s %s is not allowed in anonymous 'defaults' sections",
1198 args[0], args[1]);
Willy Tarreau39713102016-11-25 15:49:32 +01001199 return -1;
1200 }
1201
1202 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1203 memprintf(err,
1204 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1205 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001206
1207 if (ptr == PARSE_TIME_OVER)
1208 memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]);
1209 else if (ptr == PARSE_TIME_UNDER)
1210 memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]);
1211 else if (ptr)
Willy Tarreau39713102016-11-25 15:49:32 +01001212 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
1213 return -1;
1214 }
1215
1216 if (curpx->tcp_req.inspect_delay) {
1217 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1218 args[0], args[1], proxy_type_str(curpx), curpx->id);
1219 return 1;
1220 }
1221 curpx->tcp_req.inspect_delay = val;
1222 return 0;
1223 }
1224
Willy Tarreaud535f802021-10-11 08:49:26 +02001225 rule = new_act_rule(0, file, line);
Remi Tricot-Le Breton2ca42b42021-05-12 18:24:18 +02001226 if (!rule) {
1227 memprintf(err, "parsing [%s:%d] : out of memory", file, line);
1228 return -1;
1229 }
Willy Tarreau39713102016-11-25 15:49:32 +01001230 LIST_INIT(&rule->list);
1231 arg = 1;
1232 where = 0;
1233
1234 if (strcmp(args[1], "content") == 0) {
1235 arg++;
1236
1237 if (curpx->cap & PR_CAP_FE)
1238 where |= SMP_VAL_FE_REQ_CNT;
1239 if (curpx->cap & PR_CAP_BE)
1240 where |= SMP_VAL_BE_REQ_CNT;
Christopher Fauletcb9106b2019-12-19 15:23:17 +01001241 rule->from = ACT_F_TCP_REQ_CNT;
Willy Tarreau39713102016-11-25 15:49:32 +01001242 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1243 goto error;
1244
1245 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1246 if (acl) {
1247 if (acl->name && *acl->name)
1248 memprintf(err,
1249 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1250 acl->name, args[0], args[1], sample_ckp_names(where));
1251 else
1252 memprintf(err,
1253 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1254 args[0], args[1],
1255 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1256 sample_ckp_names(where));
1257
1258 warn++;
1259 }
1260 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1261 if (acl->name && *acl->name)
1262 memprintf(err,
1263 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1264 acl->name, kw, sample_ckp_names(where));
1265 else
1266 memprintf(err,
1267 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1268 kw, sample_ckp_names(where));
1269 warn++;
1270 }
1271
1272 /* the following function directly emits the warning */
1273 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001274 LIST_APPEND(&curpx->tcp_req.inspect_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001275 }
1276 else if (strcmp(args[1], "connection") == 0) {
1277 arg++;
1278
1279 if (!(curpx->cap & PR_CAP_FE)) {
1280 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1281 args[0], args[1], proxy_type_str(curpx), curpx->id);
1282 goto error;
1283 }
1284
1285 where |= SMP_VAL_FE_CON_ACC;
Christopher Fauletcb9106b2019-12-19 15:23:17 +01001286 rule->from = ACT_F_TCP_REQ_CON;
Willy Tarreau39713102016-11-25 15:49:32 +01001287 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1288 goto error;
1289
1290 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1291 if (acl) {
1292 if (acl->name && *acl->name)
1293 memprintf(err,
1294 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1295 acl->name, args[0], args[1], sample_ckp_names(where));
1296 else
1297 memprintf(err,
1298 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1299 args[0], args[1],
1300 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1301 sample_ckp_names(where));
1302
1303 warn++;
1304 }
1305 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1306 if (acl->name && *acl->name)
1307 memprintf(err,
1308 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1309 acl->name, kw, sample_ckp_names(where));
1310 else
1311 memprintf(err,
1312 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1313 kw, sample_ckp_names(where));
1314 warn++;
1315 }
1316
1317 /* the following function directly emits the warning */
1318 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001319 LIST_APPEND(&curpx->tcp_req.l4_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001320 }
1321 else if (strcmp(args[1], "session") == 0) {
1322 arg++;
1323
1324 if (!(curpx->cap & PR_CAP_FE)) {
1325 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1326 args[0], args[1], proxy_type_str(curpx), curpx->id);
1327 goto error;
1328 }
1329
1330 where |= SMP_VAL_FE_SES_ACC;
Christopher Fauletcb9106b2019-12-19 15:23:17 +01001331 rule->from = ACT_F_TCP_REQ_SES;
Willy Tarreau39713102016-11-25 15:49:32 +01001332 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1333 goto error;
1334
1335 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1336 if (acl) {
1337 if (acl->name && *acl->name)
1338 memprintf(err,
1339 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1340 acl->name, args[0], args[1], sample_ckp_names(where));
1341 else
1342 memprintf(err,
1343 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1344 args[0], args[1],
1345 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1346 sample_ckp_names(where));
1347 warn++;
1348 }
1349 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1350 if (acl->name && *acl->name)
1351 memprintf(err,
1352 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1353 acl->name, kw, sample_ckp_names(where));
1354 else
1355 memprintf(err,
1356 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1357 kw, sample_ckp_names(where));
1358 warn++;
1359 }
1360
1361 /* the following function directly emits the warning */
1362 warnif_misplaced_tcp_sess(curpx, file, line, args[0]);
Willy Tarreau2b718102021-04-21 07:32:39 +02001363 LIST_APPEND(&curpx->tcp_req.l5_rules, &rule->list);
Willy Tarreau39713102016-11-25 15:49:32 +01001364 }
1365 else {
1366 if (curpx == defpx)
1367 memprintf(err,
1368 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1369 args[0], args[1]);
1370 else
1371 memprintf(err,
1372 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1373 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1374 goto error;
1375 }
1376
1377 return warn;
1378 error:
1379 free(rule);
1380 return -1;
1381}
1382
1383static struct cfg_kw_list cfg_kws = {ILH, {
1384 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1385 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
1386 { 0, NULL, NULL },
1387}};
1388
Willy Tarreau0108d902018-11-25 19:14:37 +01001389INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau39713102016-11-25 15:49:32 +01001390
1391/*
1392 * Local variables:
1393 * c-indent-level: 8
1394 * c-basic-offset: 8
1395 * End:
1396 */