blob: d80c27ccff0ea55fbb47a9475e2e145d94175695 [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 Tarreau92b4f132020-06-01 11:05:15 +020030#include <haproxy/time.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{
48 LIST_ADDQ(&tcp_req_conn_keywords, &kw_list->list);
49}
50
51void tcp_req_sess_keywords_register(struct action_kw_list *kw_list)
52{
53 LIST_ADDQ(&tcp_req_sess_keywords, &kw_list->list);
54}
55
56void tcp_req_cont_keywords_register(struct action_kw_list *kw_list)
57{
58 LIST_ADDQ(&tcp_req_cont_keywords, &kw_list->list);
59}
60
61void tcp_res_cont_keywords_register(struct action_kw_list *kw_list)
62{
63 LIST_ADDQ(&tcp_res_cont_keywords, &kw_list->list);
64}
65
66/*
67 * Return the struct tcp_req_action_kw associated to a keyword.
68 */
69static struct action_kw *tcp_req_conn_action(const char *kw)
70{
71 return action_lookup(&tcp_req_conn_keywords, kw);
72}
73
74static struct action_kw *tcp_req_sess_action(const char *kw)
75{
76 return action_lookup(&tcp_req_sess_keywords, kw);
77}
78
79static struct action_kw *tcp_req_cont_action(const char *kw)
80{
81 return action_lookup(&tcp_req_cont_keywords, kw);
82}
83
84static struct action_kw *tcp_res_cont_action(const char *kw)
85{
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{
98 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
105 /* We don't know whether we have enough data, so must proceed
106 * this way :
107 * - iterate through all rules in their declaration order
108 * - if one rule returns MISS, it means the inspect delay is
109 * not over yet, then return immediately, otherwise consider
110 * it as a non-match.
111 * - if one rule returns OK, then return OK
112 * - if one rule returns KO, then return KO
113 */
114
Willy Tarreau23752332018-06-15 14:54:53 +0200115 if ((req->flags & CF_SHUTR) || channel_full(req, global.tune.maxrewrite) ||
Willy Tarreau39713102016-11-25 15:49:32 +0100116 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
117 partial = SMP_OPT_FINAL;
118 else
119 partial = 0;
120
121 /* If "the current_rule_list" match the executed rule list, we are in
122 * resume condition. If a resume is needed it is always in the action
123 * and never in the ACL or converters. In this case, we initialise the
124 * current rule, and go to the action execution point.
125 */
126 if (s->current_rule) {
127 rule = s->current_rule;
128 s->current_rule = NULL;
129 if (s->current_rule_list == &s->be->tcp_req.inspect_rules)
130 goto resume_execution;
131 }
132 s->current_rule_list = &s->be->tcp_req.inspect_rules;
133
134 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
135 enum acl_test_res ret = ACL_TEST_PASS;
136
137 if (rule->cond) {
138 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial);
139 if (ret == ACL_TEST_MISS)
140 goto missing_data;
141
142 ret = acl_pass(ret);
143 if (rule->cond->pol == ACL_COND_UNLESS)
144 ret = !ret;
145 }
146
147 if (ret) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100148 act_opts |= ACT_OPT_FIRST;
Willy Tarreau39713102016-11-25 15:49:32 +0100149resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100150
151 /* Always call the action function if defined */
152 if (rule->action_ptr) {
153 if (partial & SMP_OPT_FINAL)
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100154 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100155
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100156 switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100157 case ACT_RET_CONT:
158 break;
159 case ACT_RET_STOP:
160 case ACT_RET_DONE:
161 goto end;
162 case ACT_RET_YIELD:
163 s->current_rule = rule;
Christopher Faulet99aaca92020-07-28 11:30:19 +0200164 if (partial & SMP_OPT_FINAL) {
165 send_log(s->be, LOG_WARNING,
166 "Internal error: yield not allowed if the inspect-delay expired "
167 "for the tcp-request content actions.");
168 goto internal;
169 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100170 goto missing_data;
171 case ACT_RET_DENY:
172 goto deny;
173 case ACT_RET_ABRT:
174 goto abort;
175 case ACT_RET_ERR:
176 goto internal;
177 case ACT_RET_INV:
178 goto invalid;
179 }
180 continue; /* eval the next rule */
181 }
182
183 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100184 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100185 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100186 }
187 else if (rule->action == ACT_ACTION_DENY) {
Christopher Faulet282992e2019-12-16 12:34:31 +0100188 goto deny;
Willy Tarreau39713102016-11-25 15:49:32 +0100189 }
Willy Tarreau39713102016-11-25 15:49:32 +0100190 }
191 }
192
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100193 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100194 /* if we get there, it means we have no rule which matches, or
195 * we have an explicit accept, so we apply the default accept.
196 */
197 req->analysers &= ~an_bit;
198 req->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100199 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100200 return 1;
201
202 missing_data:
203 channel_dont_connect(req);
204 /* just set the request timeout once at the beginning of the request */
205 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
206 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100207 DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100208 return 0;
209
Christopher Faulet282992e2019-12-16 12:34:31 +0100210 deny:
211 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
212 if (sess->listener && sess->listener->counters)
213 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
214 goto reject;
215
216 internal:
217 _HA_ATOMIC_ADD(&sess->fe->fe_counters.internal_errors, 1);
218 if (sess->listener && sess->listener->counters)
219 _HA_ATOMIC_ADD(&sess->listener->counters->internal_errors, 1);
220 if (!(s->flags & SF_ERR_MASK))
221 s->flags |= SF_ERR_INTERNAL;
222 goto reject;
223
224 invalid:
225 _HA_ATOMIC_ADD(&sess->fe->fe_counters.failed_req, 1);
226 if (sess->listener && sess->listener->counters)
227 _HA_ATOMIC_ADD(&sess->listener->counters->failed_req, 1);
228
229 reject:
230 si_must_kill_conn(chn_prod(req));
231 channel_abort(req);
232 channel_abort(&s->res);
233
234 abort:
235 req->analysers &= AN_REQ_FLT_END;
236
237 if (!(s->flags & SF_ERR_MASK))
238 s->flags |= SF_ERR_PRXCOND;
239 if (!(s->flags & SF_FINST_MASK))
240 s->flags |= SF_FINST_R;
241 DBG_TRACE_DEVEL("leaving on error|deny|abort", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_TCP_ERR, s);
242 return 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100243}
244
245/* This function performs the TCP response analysis on the current response. It
246 * returns 1 if the processing can continue on next analysers, or zero if it
247 * needs more data, encounters an error, or wants to immediately abort the
248 * response. It relies on buffers flags, and updates s->rep->analysers. The
249 * function may be called for backend rules.
250 */
251int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
252{
253 struct session *sess = s->sess;
254 struct act_rule *rule;
255 int partial;
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100256 int act_opts = 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100257
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100258 DBG_TRACE_ENTER(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100259
260 /* We don't know whether we have enough data, so must proceed
261 * this way :
262 * - iterate through all rules in their declaration order
263 * - if one rule returns MISS, it means the inspect delay is
264 * not over yet, then return immediately, otherwise consider
265 * it as a non-match.
266 * - if one rule returns OK, then return OK
267 * - if one rule returns KO, then return KO
268 */
Willy Tarreau55ae1ab2020-06-15 18:08:07 +0200269 if ((rep->flags & CF_SHUTR) || channel_full(rep, global.tune.maxrewrite) ||
270 !s->be->tcp_rep.inspect_delay || tick_is_expired(rep->analyse_exp, now_ms))
Willy Tarreau39713102016-11-25 15:49:32 +0100271 partial = SMP_OPT_FINAL;
272 else
273 partial = 0;
274
275 /* If "the current_rule_list" match the executed rule list, we are in
276 * resume condition. If a resume is needed it is always in the action
277 * and never in the ACL or converters. In this case, we initialise the
278 * current rule, and go to the action execution point.
279 */
280 if (s->current_rule) {
281 rule = s->current_rule;
282 s->current_rule = NULL;
283 if (s->current_rule_list == &s->be->tcp_rep.inspect_rules)
284 goto resume_execution;
285 }
286 s->current_rule_list = &s->be->tcp_rep.inspect_rules;
287
288 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
289 enum acl_test_res ret = ACL_TEST_PASS;
290
291 if (rule->cond) {
292 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
293 if (ret == ACL_TEST_MISS) {
294 /* just set the analyser timeout once at the beginning of the response */
295 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
296 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100297 DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100298 return 0;
299 }
300
301 ret = acl_pass(ret);
302 if (rule->cond->pol == ACL_COND_UNLESS)
303 ret = !ret;
304 }
305
306 if (ret) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100307 act_opts |= ACT_OPT_FIRST;
Willy Tarreau39713102016-11-25 15:49:32 +0100308resume_execution:
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100309 /* Always call the action function if defined */
310 if (rule->action_ptr) {
311 if (partial & SMP_OPT_FINAL)
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100312 act_opts |= ACT_OPT_FINAL;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100313
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100314 switch (rule->action_ptr(rule, s->be, s->sess, s, act_opts)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100315 case ACT_RET_CONT:
316 break;
317 case ACT_RET_STOP:
318 case ACT_RET_DONE:
319 goto end;
320 case ACT_RET_YIELD:
321 s->current_rule = rule;
Christopher Faulet99aaca92020-07-28 11:30:19 +0200322 if (partial & SMP_OPT_FINAL) {
323 send_log(s->be, LOG_WARNING,
324 "Internal error: yield not allowed if the inspect-delay expired "
325 "for the tcp-response content actions.");
326 goto internal;
327 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100328 goto missing_data;
329 case ACT_RET_DENY:
330 goto deny;
331 case ACT_RET_ABRT:
332 goto abort;
333 case ACT_RET_ERR:
334 goto internal;
335 case ACT_RET_INV:
336 goto invalid;
337 }
338 continue; /* eval the next rule */
339 }
340
341 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100342 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100343 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100344 }
345 else if (rule->action == ACT_ACTION_DENY) {
Christopher Faulet282992e2019-12-16 12:34:31 +0100346 goto deny;
Willy Tarreau39713102016-11-25 15:49:32 +0100347 }
348 else if (rule->action == ACT_TCP_CLOSE) {
349 chn_prod(rep)->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100350 si_must_kill_conn(chn_prod(rep));
Willy Tarreau39713102016-11-25 15:49:32 +0100351 si_shutr(chn_prod(rep));
352 si_shutw(chn_prod(rep));
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100353 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100354 }
355 }
356 }
357
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100358 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100359 /* if we get there, it means we have no rule which matches, or
360 * we have an explicit accept, so we apply the default accept.
361 */
362 rep->analysers &= ~an_bit;
363 rep->analyse_exp = TICK_ETERNITY;
Christopher Fauleteea8fc72019-11-05 16:18:10 +0100364 DBG_TRACE_LEAVE(STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
Willy Tarreau39713102016-11-25 15:49:32 +0100365 return 1;
Christopher Faulet282992e2019-12-16 12:34:31 +0100366
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100367 missing_data:
368 channel_dont_close(rep);
Christopher Faulet54f3e182020-07-29 12:00:23 +0200369 /* just set the analyser timeout once at the beginning of the response */
370 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
371 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100372 DBG_TRACE_DEVEL("waiting for more data", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA, s);
373 return 0;
374
Christopher Faulet282992e2019-12-16 12:34:31 +0100375 deny:
Christopher Fauletcff0f732019-12-16 16:13:44 +0100376 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.denied_resp, 1);
Christopher Faulet282992e2019-12-16 12:34:31 +0100377 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100378 if (s->sess->listener->counters)
379 _HA_ATOMIC_ADD(&s->sess->listener->counters->denied_resp, 1);
Christopher Faulet282992e2019-12-16 12:34:31 +0100380 if (objt_server(s->target))
381 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.denied_resp, 1);
382 goto reject;
383
384 internal:
Christopher Fauletcff0f732019-12-16 16:13:44 +0100385 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.internal_errors, 1);
Christopher Faulet282992e2019-12-16 12:34:31 +0100386 _HA_ATOMIC_ADD(&s->be->be_counters.internal_errors, 1);
Christopher Fauletcff0f732019-12-16 16:13:44 +0100387 if (s->sess->listener->counters)
388 _HA_ATOMIC_ADD(&s->sess->listener->counters->internal_errors, 1);
Christopher Faulet282992e2019-12-16 12:34:31 +0100389 if (objt_server(s->target))
390 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.internal_errors, 1);
391 if (!(s->flags & SF_ERR_MASK))
392 s->flags |= SF_ERR_INTERNAL;
393 goto reject;
394
395 invalid:
396 _HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
397 if (objt_server(s->target))
398 _HA_ATOMIC_ADD(&__objt_server(s->target)->counters.failed_resp, 1);
399
400 reject:
401 si_must_kill_conn(chn_prod(rep));
402 channel_abort(rep);
403 channel_abort(&s->req);
404
405 abort:
Christopher Faulet19dbf2d2020-07-28 11:40:07 +0200406 rep->analysers &= AN_RES_FLT_END;
Christopher Faulet282992e2019-12-16 12:34:31 +0100407
408 if (!(s->flags & SF_ERR_MASK))
409 s->flags |= SF_ERR_PRXCOND;
410 if (!(s->flags & SF_FINST_MASK))
411 s->flags |= SF_FINST_D;
412 DBG_TRACE_DEVEL("leaving on error", STRM_EV_STRM_ANA|STRM_EV_TCP_ANA|STRM_EV_TCP_ERR, s);
413 return 0;
Willy Tarreau39713102016-11-25 15:49:32 +0100414}
415
416
417/* This function performs the TCP layer4 analysis on the current request. It
418 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
419 * matches or if no more rule matches. It can only use rules which don't need
420 * any data. This only works on connection-based client-facing stream interfaces.
421 */
422int tcp_exec_l4_rules(struct session *sess)
423{
424 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100425 struct connection *conn = objt_conn(sess->origin);
426 int result = 1;
427 enum acl_test_res ret;
428
429 if (!conn)
430 return result;
431
432 list_for_each_entry(rule, &sess->fe->tcp_req.l4_rules, list) {
433 ret = ACL_TEST_PASS;
434
435 if (rule->cond) {
436 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
437 ret = acl_pass(ret);
438 if (rule->cond->pol == ACL_COND_UNLESS)
439 ret = !ret;
440 }
441
442 if (ret) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100443 /* Always call the action function if defined */
444 if (rule->action_ptr) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100445 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_OPT_FINAL | ACT_OPT_FIRST)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100446 case ACT_RET_YIELD:
447 /* yield is not allowed at this point. If this return code is
448 * used it is a bug, so I prefer to abort the process.
449 */
450 send_log(sess->fe, LOG_WARNING,
451 "Internal error: yield not allowed with tcp-request connection actions.");
452 /* fall through */
453 case ACT_RET_STOP:
454 case ACT_RET_DONE:
455 goto end;
456 case ACT_RET_CONT:
457 break;
458 case ACT_RET_DENY:
459 case ACT_RET_ABRT:
460 case ACT_RET_ERR:
461 case ACT_RET_INV:
462 result = 0;
463 goto end;
464 }
465 continue; /* eval the next rule */
466 }
467
468 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100469 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100470 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100471 }
472 else if (rule->action == ACT_ACTION_DENY) {
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100473 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_conn, 1);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100474 if (sess->listener && sess->listener->counters)
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100475 _HA_ATOMIC_ADD(&sess->listener->counters->denied_conn, 1);
Willy Tarreau39713102016-11-25 15:49:32 +0100476
477 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100478 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100479 }
Willy Tarreau39713102016-11-25 15:49:32 +0100480 else if (rule->action == ACT_TCP_EXPECT_PX) {
Willy Tarreau4450b582020-01-23 15:23:13 +0100481 if (!(conn->flags & CO_FL_HANDSHAKE)) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200482 if (xprt_add_hs(conn) < 0) {
483 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100484 goto end;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200485 }
486 }
Willy Tarreau39713102016-11-25 15:49:32 +0100487 conn->flags |= CO_FL_ACCEPT_PROXY;
Willy Tarreau39713102016-11-25 15:49:32 +0100488 }
489 else if (rule->action == ACT_TCP_EXPECT_CIP) {
Willy Tarreau4450b582020-01-23 15:23:13 +0100490 if (!(conn->flags & CO_FL_HANDSHAKE)) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200491 if (xprt_add_hs(conn) < 0) {
492 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100493 goto end;
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200494 }
495 }
Willy Tarreau39713102016-11-25 15:49:32 +0100496 conn->flags |= CO_FL_ACCEPT_CIP;
Willy Tarreau39713102016-11-25 15:49:32 +0100497 }
Willy Tarreau39713102016-11-25 15:49:32 +0100498 }
499 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100500 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100501 return result;
502}
503
504/* This function performs the TCP layer5 analysis on the current request. It
505 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
506 * matches or if no more rule matches. It can only use rules which don't need
507 * any data. This only works on session-based client-facing stream interfaces.
508 * An example of valid use case is to track a stick-counter on the source
509 * address extracted from the proxy protocol.
510 */
511int tcp_exec_l5_rules(struct session *sess)
512{
513 struct act_rule *rule;
Willy Tarreau39713102016-11-25 15:49:32 +0100514 int result = 1;
515 enum acl_test_res ret;
516
517 list_for_each_entry(rule, &sess->fe->tcp_req.l5_rules, list) {
518 ret = ACL_TEST_PASS;
519
520 if (rule->cond) {
521 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
522 ret = acl_pass(ret);
523 if (rule->cond->pol == ACL_COND_UNLESS)
524 ret = !ret;
525 }
526
527 if (ret) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100528 /* Always call the action function if defined */
529 if (rule->action_ptr) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +0100530 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_OPT_FINAL | ACT_OPT_FIRST)) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100531 case ACT_RET_YIELD:
532 /* yield is not allowed at this point. If this return code is
533 * used it is a bug, so I prefer to abort the process.
534 */
535 send_log(sess->fe, LOG_WARNING,
536 "Internal error: yield not allowed with tcp-request session actions.");
537 /* fall through */
538 case ACT_RET_STOP:
539 case ACT_RET_DONE:
540 goto end;
541 case ACT_RET_CONT:
542 break;
543 case ACT_RET_DENY:
544 case ACT_RET_ABRT:
545 case ACT_RET_ERR:
546 case ACT_RET_INV:
547 result = 0;
548 goto end;
549 }
550 continue; /* eval the next rule */
551 }
552
553 /* If not action function defined, check for known actions */
Willy Tarreau39713102016-11-25 15:49:32 +0100554 if (rule->action == ACT_ACTION_ALLOW) {
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100555 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100556 }
557 else if (rule->action == ACT_ACTION_DENY) {
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100558 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_sess, 1);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100559 if (sess->listener && sess->listener->counters)
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100560 _HA_ATOMIC_ADD(&sess->listener->counters->denied_sess, 1);
Willy Tarreau39713102016-11-25 15:49:32 +0100561
562 result = 0;
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100563 goto end;
Willy Tarreau39713102016-11-25 15:49:32 +0100564 }
Willy Tarreau39713102016-11-25 15:49:32 +0100565 }
566 }
Christopher Fauletcd26e8a2019-12-18 11:13:39 +0100567 end:
Willy Tarreau39713102016-11-25 15:49:32 +0100568 return result;
569}
570
571/* Parse a tcp-response rule. Return a negative value in case of failure */
572static int tcp_parse_response_rule(char **args, int arg, int section_type,
573 struct proxy *curpx, struct proxy *defpx,
574 struct act_rule *rule, char **err,
575 unsigned int where,
576 const char *file, int line)
577{
578 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
579 memprintf(err, "%s %s is only allowed in 'backend' sections",
580 args[0], args[1]);
581 return -1;
582 }
583
584 if (strcmp(args[arg], "accept") == 0) {
585 arg++;
586 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100587 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100588 }
589 else if (strcmp(args[arg], "reject") == 0) {
590 arg++;
591 rule->action = ACT_ACTION_DENY;
Christopher Faulet245cf792019-12-18 14:58:12 +0100592 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100593 }
594 else if (strcmp(args[arg], "close") == 0) {
595 arg++;
596 rule->action = ACT_TCP_CLOSE;
Christopher Faulet245cf792019-12-18 14:58:12 +0100597 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100598 }
599 else {
600 struct action_kw *kw;
601 kw = tcp_res_cont_action(args[arg]);
602 if (kw) {
603 arg++;
Willy Tarreau39713102016-11-25 15:49:32 +0100604 rule->kw = kw;
605 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
606 return -1;
607 } else {
608 action_build_list(&tcp_res_cont_keywords, &trash);
609 memprintf(err,
610 "'%s %s' expects 'accept', 'close', 'reject', %s in %s '%s' (got '%s')",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200611 args[0], args[1], trash.area,
612 proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau39713102016-11-25 15:49:32 +0100613 return -1;
614 }
615 }
616
617 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +0200618 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +0100619 memprintf(err,
620 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
621 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
622 return -1;
623 }
624 }
625 else if (*args[arg]) {
626 memprintf(err,
627 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
628 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
629 return -1;
630 }
631 return 0;
632}
633
Christopher Fauletac98d812019-12-18 09:20:16 +0100634
635/* This function executes a track-sc* actions. On success, it returns
636 * ACT_RET_CONT. If it must yield, it return ACT_RET_YIELD. Otherwsize
637 * ACT_RET_ERR is returned.
638 */
639static enum act_return tcp_action_track_sc(struct act_rule *rule, struct proxy *px,
640 struct session *sess, struct stream *s, int flags)
641{
642 struct stksess *ts;
643 struct stktable *t;
644 struct stktable_key *key;
645 struct sample smp;
646 int opt;
647
Christopher Faulet67307792020-02-10 09:54:49 +0100648 opt = SMP_OPT_DIR_REQ;
Christopher Fauletac98d812019-12-18 09:20:16 +0100649 if (flags & ACT_FLAG_FINAL)
650 opt |= SMP_OPT_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100651
Christopher Fauletac98d812019-12-18 09:20:16 +0100652 t = rule->arg.trk_ctr.table.t;
Christopher Faulet67307792020-02-10 09:54:49 +0100653 if (rule->from == ACT_F_TCP_REQ_CNT) { /* L7 rules: use the stream */
654 if (stkctr_entry(&s->stkctr[rule->action]))
655 goto end;
Christopher Fauletac98d812019-12-18 09:20:16 +0100656
Christopher Faulet67307792020-02-10 09:54:49 +0100657 key = stktable_fetch_key(t, s->be, sess, s, opt, rule->arg.trk_ctr.expr, &smp);
Christopher Fauletac98d812019-12-18 09:20:16 +0100658
Christopher Faulet67307792020-02-10 09:54:49 +0100659 if ((smp.flags & SMP_F_MAY_CHANGE) && !(flags & ACT_FLAG_FINAL))
660 return ACT_RET_YIELD; /* key might appear later */
661
662 if (key && (ts = stktable_get_entry(t, key))) {
663 stream_track_stkctr(&s->stkctr[rule->action], t, ts);
Christopher Fauletac98d812019-12-18 09:20:16 +0100664 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_CONTENT);
665 if (sess->fe != s->be)
666 stkctr_set_flags(&s->stkctr[rule->action], STKCTR_TRACK_BACKEND);
667 }
668 }
Christopher Faulet67307792020-02-10 09:54:49 +0100669 else { /* L4/L5 rules: use the session */
670 if (stkctr_entry(&sess->stkctr[rule->action]))
671 goto end;
672
673 key = stktable_fetch_key(t, sess->fe, sess, NULL, opt, rule->arg.trk_ctr.expr, NULL);
674 if (key && (ts = stktable_get_entry(t, key)))
675 stream_track_stkctr(&sess->stkctr[rule->action], t, ts);
676 }
Christopher Fauletac98d812019-12-18 09:20:16 +0100677
678 end:
679 return ACT_RET_CONT;
680}
Willy Tarreau39713102016-11-25 15:49:32 +0100681
Christopher Fauletd73b96d2019-12-19 17:27:03 +0100682/* This function executes a capture actions. It executes a fetch expression,
683 * turns the result into a string and puts it in a capture slot. On success, it
684 * returns ACT_RET_CONT. If it must yield, it return ACT_RET_YIELD. Otherwsize
685 * ACT_RET_ERR is returned.
686 */
687static enum act_return tcp_action_capture(struct act_rule *rule, struct proxy *px,
688 struct session *sess, struct stream *s, int flags)
689{
690 struct sample *key;
691 struct cap_hdr *h = rule->arg.cap.hdr;
692 char **cap = s->req_cap;
693 int len, opt;
694
695 opt = ((rule->from == ACT_F_TCP_REQ_CNT) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
696 if (flags & ACT_FLAG_FINAL)
697 opt |= SMP_OPT_FINAL;
698
699 key = sample_fetch_as_type(s->be, sess, s, opt, rule->arg.cap.expr, SMP_T_STR);
700 if (!key)
701 goto end;
702
703 if ((key->flags & SMP_F_MAY_CHANGE) && !(flags & ACT_FLAG_FINAL))
704 return ACT_RET_YIELD; /* key might appear later */
705
706 if (cap[h->index] == NULL) {
707 cap[h->index] = pool_alloc(h->pool);
708 if (cap[h->index] == NULL) /* no more capture memory, ignore error */
709 goto end;
710 }
711
712 len = key->data.u.str.data;
713 if (len > h->len)
714 len = h->len;
715
716 memcpy(cap[h->index], key->data.u.str.area, len);
717 cap[h->index][len] = 0;
718
719 end:
720 return ACT_RET_CONT;
721}
722
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100723static void release_tcp_capture(struct act_rule * rule)
724{
725 release_sample_expr(rule->arg.cap.expr);
726}
727
728
729static void release_tcp_track_sc(struct act_rule * rule)
730{
731 release_sample_expr(rule->arg.trk_ctr.expr);
732}
733
Willy Tarreau39713102016-11-25 15:49:32 +0100734/* Parse a tcp-request rule. Return a negative value in case of failure */
735static int tcp_parse_request_rule(char **args, int arg, int section_type,
736 struct proxy *curpx, struct proxy *defpx,
737 struct act_rule *rule, char **err,
738 unsigned int where, const char *file, int line)
739{
740 if (curpx == defpx) {
741 memprintf(err, "%s %s is not allowed in 'defaults' sections",
742 args[0], args[1]);
743 return -1;
744 }
745
746 if (!strcmp(args[arg], "accept")) {
747 arg++;
748 rule->action = ACT_ACTION_ALLOW;
Christopher Faulet245cf792019-12-18 14:58:12 +0100749 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100750 }
751 else if (!strcmp(args[arg], "reject")) {
752 arg++;
753 rule->action = ACT_ACTION_DENY;
Christopher Faulet245cf792019-12-18 14:58:12 +0100754 rule->flags |= ACT_FLAG_FINAL;
Willy Tarreau39713102016-11-25 15:49:32 +0100755 }
756 else if (strcmp(args[arg], "capture") == 0) {
757 struct sample_expr *expr;
758 struct cap_hdr *hdr;
759 int kw = arg;
760 int len = 0;
761
762 if (!(curpx->cap & PR_CAP_FE)) {
763 memprintf(err,
764 "'%s %s %s' : proxy '%s' has no frontend capability",
765 args[0], args[1], args[kw], curpx->id);
766 return -1;
767 }
768
769 if (!(where & SMP_VAL_FE_REQ_CNT)) {
770 memprintf(err,
771 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
772 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
773 return -1;
774 }
775
776 arg++;
777
778 curpx->conf.args.ctx = ARGC_CAP;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100779 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args, NULL);
Willy Tarreau39713102016-11-25 15:49:32 +0100780 if (!expr) {
781 memprintf(err,
782 "'%s %s %s' : %s",
783 args[0], args[1], args[kw], *err);
784 return -1;
785 }
786
787 if (!(expr->fetch->val & where)) {
788 memprintf(err,
789 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
790 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100791 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100792 return -1;
793 }
794
795 if (strcmp(args[arg], "len") == 0) {
796 arg++;
797 if (!args[arg]) {
798 memprintf(err,
799 "'%s %s %s' : missing length value",
800 args[0], args[1], args[kw]);
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100801 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100802 return -1;
803 }
804 /* we copy the table name for now, it will be resolved later */
805 len = atoi(args[arg]);
806 if (len <= 0) {
807 memprintf(err,
808 "'%s %s %s' : length must be > 0",
809 args[0], args[1], args[kw]);
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100810 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100811 return -1;
812 }
813 arg++;
814 }
815
816 if (!len) {
817 memprintf(err,
818 "'%s %s %s' : a positive 'len' argument is mandatory",
819 args[0], args[1], args[kw]);
820 free(expr);
821 return -1;
822 }
823
824 hdr = calloc(1, sizeof(*hdr));
825 hdr->next = curpx->req_cap;
826 hdr->name = NULL; /* not a header capture */
827 hdr->namelen = 0;
828 hdr->len = len;
829 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
830 hdr->index = curpx->nb_req_cap++;
831
832 curpx->req_cap = hdr;
833 curpx->to_log |= LW_REQHDR;
834
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200835 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreau39713102016-11-25 15:49:32 +0100836 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
837
838 rule->arg.cap.expr = expr;
839 rule->arg.cap.hdr = hdr;
Christopher Fauletd73b96d2019-12-19 17:27:03 +0100840 rule->action = ACT_CUSTOM;
841 rule->action_ptr = tcp_action_capture;
842 rule->check_ptr = check_capture;
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100843 rule->release_ptr = release_tcp_capture;
Willy Tarreau39713102016-11-25 15:49:32 +0100844 }
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100845 else if (strncmp(args[arg], "track-sc", 8) == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100846 struct sample_expr *expr;
847 int kw = arg;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100848 unsigned int tsc_num;
849 const char *tsc_num_str;
Willy Tarreau39713102016-11-25 15:49:32 +0100850
851 arg++;
852
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100853 tsc_num_str = &args[kw][8];
854 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1) {
855 memprintf(err, "'%s %s %s' : %s", args[0], args[1], args[kw], *err);
856 return -1;
857 }
858
Willy Tarreau39713102016-11-25 15:49:32 +0100859 curpx->conf.args.ctx = ARGC_TRK;
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
Christopher Faulet711ed6a2019-07-16 14:16:10 +0200876 /* check if we need to allocate an http_txn struct for HTTP parsing */
Willy Tarreau39713102016-11-25 15:49:32 +0100877 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
878
879 if (strcmp(args[arg], "table") == 0) {
880 arg++;
881 if (!args[arg]) {
882 memprintf(err,
883 "'%s %s %s' : missing table name",
884 args[0], args[1], args[kw]);
Christopher Fauletfdb6fbf2020-01-14 15:05:56 +0100885 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100886 return -1;
887 }
888 /* we copy the table name for now, it will be resolved later */
889 rule->arg.trk_ctr.table.n = strdup(args[arg]);
890 arg++;
891 }
Christopher Fauletac98d812019-12-18 09:20:16 +0100892 rule->action = tsc_num;
Willy Tarreau39713102016-11-25 15:49:32 +0100893 rule->arg.trk_ctr.expr = expr;
Christopher Fauletac98d812019-12-18 09:20:16 +0100894 rule->action_ptr = tcp_action_track_sc;
Christopher Faulet78880fb2017-09-18 14:43:55 +0200895 rule->check_ptr = check_trk_action;
Christopher Fauletadfc6e82020-01-14 15:05:33 +0100896 rule->release_ptr = release_tcp_track_sc;
Willy Tarreau39713102016-11-25 15:49:32 +0100897 }
898 else if (strcmp(args[arg], "expect-proxy") == 0) {
899 if (strcmp(args[arg+1], "layer4") != 0) {
900 memprintf(err,
901 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
902 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
903 return -1;
904 }
905
906 if (!(where & SMP_VAL_FE_CON_ACC)) {
907 memprintf(err,
908 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
909 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
910 return -1;
911 }
912
913 arg += 2;
914 rule->action = ACT_TCP_EXPECT_PX;
915 }
916 else if (strcmp(args[arg], "expect-netscaler-cip") == 0) {
917 if (strcmp(args[arg+1], "layer4") != 0) {
918 memprintf(err,
919 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
920 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
921 return -1;
922 }
923
924 if (!(where & SMP_VAL_FE_CON_ACC)) {
925 memprintf(err,
926 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
927 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
928 return -1;
929 }
930
931 arg += 2;
932 rule->action = ACT_TCP_EXPECT_CIP;
933 }
934 else {
935 struct action_kw *kw;
936 if (where & SMP_VAL_FE_CON_ACC) {
937 /* L4 */
938 kw = tcp_req_conn_action(args[arg]);
939 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +0100940 } else if (where & SMP_VAL_FE_SES_ACC) {
941 /* L5 */
942 kw = tcp_req_sess_action(args[arg]);
943 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +0100944 } else {
945 /* L6 */
946 kw = tcp_req_cont_action(args[arg]);
947 rule->kw = kw;
Willy Tarreau39713102016-11-25 15:49:32 +0100948 }
949 if (kw) {
950 arg++;
951 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
952 return -1;
953 } else {
954 if (where & SMP_VAL_FE_CON_ACC)
955 action_build_list(&tcp_req_conn_keywords, &trash);
956 else if (where & SMP_VAL_FE_SES_ACC)
957 action_build_list(&tcp_req_sess_keywords, &trash);
958 else
959 action_build_list(&tcp_req_cont_keywords, &trash);
960 memprintf(err,
961 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d', %s "
962 "in %s '%s' (got '%s').\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200963 args[0], args[1], MAX_SESS_STKCTR-1,
964 trash.area, proxy_type_str(curpx),
Willy Tarreau39713102016-11-25 15:49:32 +0100965 curpx->id, args[arg]);
966 return -1;
967 }
968 }
969
970 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +0200971 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +0100972 memprintf(err,
973 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
974 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
975 return -1;
976 }
977 }
978 else if (*args[arg]) {
979 memprintf(err,
980 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
981 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
982 return -1;
983 }
984 return 0;
985}
986
987/* This function should be called to parse a line starting with the "tcp-response"
988 * keyword.
989 */
990static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
991 struct proxy *defpx, const char *file, int line,
992 char **err)
993{
994 const char *ptr = NULL;
995 unsigned int val;
996 int warn = 0;
997 int arg;
998 struct act_rule *rule;
999 unsigned int where;
1000 const struct acl *acl;
1001 const char *kw;
1002
1003 if (!*args[1]) {
1004 memprintf(err, "missing argument for '%s' in %s '%s'",
1005 args[0], proxy_type_str(curpx), curpx->id);
1006 return -1;
1007 }
1008
1009 if (strcmp(args[1], "inspect-delay") == 0) {
1010 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
1011 memprintf(err, "%s %s is only allowed in 'backend' sections",
1012 args[0], args[1]);
1013 return -1;
1014 }
1015
1016 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1017 memprintf(err,
1018 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1019 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001020
1021 if (ptr == PARSE_TIME_OVER)
1022 memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]);
1023 else if (ptr == PARSE_TIME_UNDER)
1024 memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]);
1025 else if (ptr)
Willy Tarreau39713102016-11-25 15:49:32 +01001026 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
1027 return -1;
1028 }
1029
1030 if (curpx->tcp_rep.inspect_delay) {
1031 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1032 args[0], args[1], proxy_type_str(curpx), curpx->id);
1033 return 1;
1034 }
1035 curpx->tcp_rep.inspect_delay = val;
1036 return 0;
1037 }
1038
1039 rule = calloc(1, sizeof(*rule));
1040 LIST_INIT(&rule->list);
1041 arg = 1;
1042 where = 0;
1043
1044 if (strcmp(args[1], "content") == 0) {
1045 arg++;
1046
1047 if (curpx->cap & PR_CAP_FE)
1048 where |= SMP_VAL_FE_RES_CNT;
1049 if (curpx->cap & PR_CAP_BE)
1050 where |= SMP_VAL_BE_RES_CNT;
Christopher Fauletcb9106b2019-12-19 15:23:17 +01001051 rule->from = ACT_F_TCP_RES_CNT;
Willy Tarreau39713102016-11-25 15:49:32 +01001052 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1053 goto error;
1054
1055 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1056 if (acl) {
1057 if (acl->name && *acl->name)
1058 memprintf(err,
1059 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1060 acl->name, args[0], args[1], sample_ckp_names(where));
1061 else
1062 memprintf(err,
1063 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1064 args[0], args[1],
1065 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1066 sample_ckp_names(where));
1067
1068 warn++;
1069 }
1070 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1071 if (acl->name && *acl->name)
1072 memprintf(err,
1073 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1074 acl->name, kw, sample_ckp_names(where));
1075 else
1076 memprintf(err,
1077 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1078 kw, sample_ckp_names(where));
1079 warn++;
1080 }
1081
1082 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
1083 }
1084 else {
1085 memprintf(err,
1086 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
1087 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1088 goto error;
1089 }
1090
1091 return warn;
1092 error:
1093 free(rule);
1094 return -1;
1095}
1096
1097
1098/* This function should be called to parse a line starting with the "tcp-request"
1099 * keyword.
1100 */
1101static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
1102 struct proxy *defpx, const char *file, int line,
1103 char **err)
1104{
1105 const char *ptr = NULL;
1106 unsigned int val;
1107 int warn = 0;
1108 int arg;
1109 struct act_rule *rule;
1110 unsigned int where;
1111 const struct acl *acl;
1112 const char *kw;
1113
1114 if (!*args[1]) {
1115 if (curpx == defpx)
1116 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1117 else
1118 memprintf(err, "missing argument for '%s' in %s '%s'",
1119 args[0], proxy_type_str(curpx), curpx->id);
1120 return -1;
1121 }
1122
1123 if (!strcmp(args[1], "inspect-delay")) {
1124 if (curpx == defpx) {
1125 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1126 args[0], args[1]);
1127 return -1;
1128 }
1129
1130 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1131 memprintf(err,
1132 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1133 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001134
1135 if (ptr == PARSE_TIME_OVER)
1136 memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]);
1137 else if (ptr == PARSE_TIME_UNDER)
1138 memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]);
1139 else if (ptr)
Willy Tarreau39713102016-11-25 15:49:32 +01001140 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
1141 return -1;
1142 }
1143
1144 if (curpx->tcp_req.inspect_delay) {
1145 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1146 args[0], args[1], proxy_type_str(curpx), curpx->id);
1147 return 1;
1148 }
1149 curpx->tcp_req.inspect_delay = val;
1150 return 0;
1151 }
1152
1153 rule = calloc(1, sizeof(*rule));
1154 LIST_INIT(&rule->list);
1155 arg = 1;
1156 where = 0;
1157
1158 if (strcmp(args[1], "content") == 0) {
1159 arg++;
1160
1161 if (curpx->cap & PR_CAP_FE)
1162 where |= SMP_VAL_FE_REQ_CNT;
1163 if (curpx->cap & PR_CAP_BE)
1164 where |= SMP_VAL_BE_REQ_CNT;
Christopher Fauletcb9106b2019-12-19 15:23:17 +01001165 rule->from = ACT_F_TCP_REQ_CNT;
Willy Tarreau39713102016-11-25 15:49:32 +01001166 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1167 goto error;
1168
1169 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1170 if (acl) {
1171 if (acl->name && *acl->name)
1172 memprintf(err,
1173 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1174 acl->name, args[0], args[1], sample_ckp_names(where));
1175 else
1176 memprintf(err,
1177 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1178 args[0], args[1],
1179 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1180 sample_ckp_names(where));
1181
1182 warn++;
1183 }
1184 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1185 if (acl->name && *acl->name)
1186 memprintf(err,
1187 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1188 acl->name, kw, sample_ckp_names(where));
1189 else
1190 memprintf(err,
1191 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1192 kw, sample_ckp_names(where));
1193 warn++;
1194 }
1195
1196 /* the following function directly emits the warning */
1197 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
1198 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
1199 }
1200 else if (strcmp(args[1], "connection") == 0) {
1201 arg++;
1202
1203 if (!(curpx->cap & PR_CAP_FE)) {
1204 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1205 args[0], args[1], proxy_type_str(curpx), curpx->id);
1206 goto error;
1207 }
1208
1209 where |= SMP_VAL_FE_CON_ACC;
Christopher Fauletcb9106b2019-12-19 15:23:17 +01001210 rule->from = ACT_F_TCP_REQ_CON;
Willy Tarreau39713102016-11-25 15:49:32 +01001211 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1212 goto error;
1213
1214 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1215 if (acl) {
1216 if (acl->name && *acl->name)
1217 memprintf(err,
1218 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1219 acl->name, args[0], args[1], sample_ckp_names(where));
1220 else
1221 memprintf(err,
1222 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1223 args[0], args[1],
1224 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1225 sample_ckp_names(where));
1226
1227 warn++;
1228 }
1229 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1230 if (acl->name && *acl->name)
1231 memprintf(err,
1232 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1233 acl->name, kw, sample_ckp_names(where));
1234 else
1235 memprintf(err,
1236 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1237 kw, sample_ckp_names(where));
1238 warn++;
1239 }
1240
1241 /* the following function directly emits the warning */
1242 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
1243 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
1244 }
1245 else if (strcmp(args[1], "session") == 0) {
1246 arg++;
1247
1248 if (!(curpx->cap & PR_CAP_FE)) {
1249 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1250 args[0], args[1], proxy_type_str(curpx), curpx->id);
1251 goto error;
1252 }
1253
1254 where |= SMP_VAL_FE_SES_ACC;
Christopher Fauletcb9106b2019-12-19 15:23:17 +01001255 rule->from = ACT_F_TCP_REQ_SES;
Willy Tarreau39713102016-11-25 15:49:32 +01001256 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1257 goto error;
1258
1259 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1260 if (acl) {
1261 if (acl->name && *acl->name)
1262 memprintf(err,
1263 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1264 acl->name, args[0], args[1], sample_ckp_names(where));
1265 else
1266 memprintf(err,
1267 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1268 args[0], args[1],
1269 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1270 sample_ckp_names(where));
1271 warn++;
1272 }
1273 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1274 if (acl->name && *acl->name)
1275 memprintf(err,
1276 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1277 acl->name, kw, sample_ckp_names(where));
1278 else
1279 memprintf(err,
1280 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1281 kw, sample_ckp_names(where));
1282 warn++;
1283 }
1284
1285 /* the following function directly emits the warning */
1286 warnif_misplaced_tcp_sess(curpx, file, line, args[0]);
1287 LIST_ADDQ(&curpx->tcp_req.l5_rules, &rule->list);
1288 }
1289 else {
1290 if (curpx == defpx)
1291 memprintf(err,
1292 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1293 args[0], args[1]);
1294 else
1295 memprintf(err,
1296 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1297 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1298 goto error;
1299 }
1300
1301 return warn;
1302 error:
1303 free(rule);
1304 return -1;
1305}
1306
1307static struct cfg_kw_list cfg_kws = {ILH, {
1308 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1309 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
1310 { 0, NULL, NULL },
1311}};
1312
Willy Tarreau0108d902018-11-25 19:14:37 +01001313INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau39713102016-11-25 15:49:32 +01001314
1315/*
1316 * Local variables:
1317 * c-indent-level: 8
1318 * c-basic-offset: 8
1319 * End:
1320 */