blob: 7fbc38961a5ab0e7c5b14f5247e8a6a3ecf8f08d [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 */
12#include <common/cfgparse.h>
13#include <common/compat.h>
14#include <common/config.h>
15#include <common/debug.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010016#include <common/initcall.h>
Willy Tarreau39713102016-11-25 15:49:32 +010017#include <common/mini-clist.h>
18#include <common/standard.h>
19#include <common/ticks.h>
20#include <common/time.h>
21
22#include <types/arg.h>
23#include <types/capture.h>
24#include <types/connection.h>
25#include <types/global.h>
26
27#include <proto/acl.h>
28#include <proto/action.h>
29#include <proto/channel.h>
30#include <proto/connection.h>
31#include <proto/log.h>
32#include <proto/proxy.h>
33#include <proto/sample.h>
34#include <proto/stick_table.h>
35#include <proto/stream.h>
36#include <proto/stream_interface.h>
37#include <proto/tcp_rules.h>
38
39/* List head of all known action keywords for "tcp-request connection" */
40struct list tcp_req_conn_keywords = LIST_HEAD_INIT(tcp_req_conn_keywords);
41struct list tcp_req_sess_keywords = LIST_HEAD_INIT(tcp_req_sess_keywords);
42struct list tcp_req_cont_keywords = LIST_HEAD_INIT(tcp_req_cont_keywords);
43struct list tcp_res_cont_keywords = LIST_HEAD_INIT(tcp_res_cont_keywords);
44
45/*
46 * Register keywords.
47 */
48void tcp_req_conn_keywords_register(struct action_kw_list *kw_list)
49{
50 LIST_ADDQ(&tcp_req_conn_keywords, &kw_list->list);
51}
52
53void tcp_req_sess_keywords_register(struct action_kw_list *kw_list)
54{
55 LIST_ADDQ(&tcp_req_sess_keywords, &kw_list->list);
56}
57
58void tcp_req_cont_keywords_register(struct action_kw_list *kw_list)
59{
60 LIST_ADDQ(&tcp_req_cont_keywords, &kw_list->list);
61}
62
63void tcp_res_cont_keywords_register(struct action_kw_list *kw_list)
64{
65 LIST_ADDQ(&tcp_res_cont_keywords, &kw_list->list);
66}
67
68/*
69 * Return the struct tcp_req_action_kw associated to a keyword.
70 */
71static struct action_kw *tcp_req_conn_action(const char *kw)
72{
73 return action_lookup(&tcp_req_conn_keywords, kw);
74}
75
76static struct action_kw *tcp_req_sess_action(const char *kw)
77{
78 return action_lookup(&tcp_req_sess_keywords, kw);
79}
80
81static struct action_kw *tcp_req_cont_action(const char *kw)
82{
83 return action_lookup(&tcp_req_cont_keywords, kw);
84}
85
86static struct action_kw *tcp_res_cont_action(const char *kw)
87{
88 return action_lookup(&tcp_res_cont_keywords, kw);
89}
90
91/* This function performs the TCP request analysis on the current request. It
92 * returns 1 if the processing can continue on next analysers, or zero if it
93 * needs more data, encounters an error, or wants to immediately abort the
94 * request. It relies on buffers flags, and updates s->req->analysers. The
95 * function may be called for frontend rules and backend rules. It only relies
96 * on the backend pointer so this works for both cases.
97 */
98int tcp_inspect_request(struct stream *s, struct channel *req, int an_bit)
99{
100 struct session *sess = s->sess;
101 struct act_rule *rule;
102 struct stksess *ts;
103 struct stktable *t;
104 int partial;
105 int act_flags = 0;
106
Christopher Faulet45073512018-07-20 10:16:29 +0200107 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau39713102016-11-25 15:49:32 +0100108 now_ms, __FUNCTION__,
109 s,
110 req,
111 req->rex, req->wex,
112 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200113 ci_data(req),
Willy Tarreau39713102016-11-25 15:49:32 +0100114 req->analysers);
115
116 /* We don't know whether we have enough data, so must proceed
117 * this way :
118 * - iterate through all rules in their declaration order
119 * - if one rule returns MISS, it means the inspect delay is
120 * not over yet, then return immediately, otherwise consider
121 * it as a non-match.
122 * - if one rule returns OK, then return OK
123 * - if one rule returns KO, then return KO
124 */
125
Willy Tarreau23752332018-06-15 14:54:53 +0200126 if ((req->flags & CF_SHUTR) || channel_full(req, global.tune.maxrewrite) ||
Willy Tarreau39713102016-11-25 15:49:32 +0100127 !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms))
128 partial = SMP_OPT_FINAL;
129 else
130 partial = 0;
131
132 /* If "the current_rule_list" match the executed rule list, we are in
133 * resume condition. If a resume is needed it is always in the action
134 * and never in the ACL or converters. In this case, we initialise the
135 * current rule, and go to the action execution point.
136 */
137 if (s->current_rule) {
138 rule = s->current_rule;
139 s->current_rule = NULL;
140 if (s->current_rule_list == &s->be->tcp_req.inspect_rules)
141 goto resume_execution;
142 }
143 s->current_rule_list = &s->be->tcp_req.inspect_rules;
144
145 list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) {
146 enum acl_test_res ret = ACL_TEST_PASS;
147
148 if (rule->cond) {
149 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ | partial);
150 if (ret == ACL_TEST_MISS)
151 goto missing_data;
152
153 ret = acl_pass(ret);
154 if (rule->cond->pol == ACL_COND_UNLESS)
155 ret = !ret;
156 }
157
158 if (ret) {
159 act_flags |= ACT_FLAG_FIRST;
160resume_execution:
161 /* we have a matching rule. */
162 if (rule->action == ACT_ACTION_ALLOW) {
163 break;
164 }
165 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100166 si_must_kill_conn(chn_prod(req));
Willy Tarreau39713102016-11-25 15:49:32 +0100167 channel_abort(req);
168 channel_abort(&s->res);
169 req->analysers = 0;
170
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100171 _HA_ATOMIC_ADD(&s->be->be_counters.denied_req, 1);
172 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_req, 1);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100173 if (sess->listener && sess->listener->counters)
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100174 _HA_ATOMIC_ADD(&sess->listener->counters->denied_req, 1);
Willy Tarreau39713102016-11-25 15:49:32 +0100175
176 if (!(s->flags & SF_ERR_MASK))
177 s->flags |= SF_ERR_PRXCOND;
178 if (!(s->flags & SF_FINST_MASK))
179 s->flags |= SF_FINST_R;
180 return 0;
181 }
182 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
183 /* Note: only the first valid tracking parameter of each
184 * applies.
185 */
186 struct stktable_key *key;
187 struct sample smp;
188
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200189 if (stkctr_entry(&s->stkctr[trk_idx(rule->action)]))
Willy Tarreau39713102016-11-25 15:49:32 +0100190 continue;
191
192 t = rule->arg.trk_ctr.table.t;
193 key = stktable_fetch_key(t, s->be, sess, s, SMP_OPT_DIR_REQ | partial, rule->arg.trk_ctr.expr, &smp);
194
195 if ((smp.flags & SMP_F_MAY_CHANGE) && !(partial & SMP_OPT_FINAL))
196 goto missing_data; /* key might appear later */
197
198 if (key && (ts = stktable_get_entry(t, key))) {
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200199 stream_track_stkctr(&s->stkctr[trk_idx(rule->action)], t, ts);
200 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
Willy Tarreau39713102016-11-25 15:49:32 +0100201 if (sess->fe != s->be)
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200202 stkctr_set_flags(&s->stkctr[trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
Willy Tarreau39713102016-11-25 15:49:32 +0100203 }
204 }
205 else if (rule->action == ACT_TCP_CAPTURE) {
206 struct sample *key;
207 struct cap_hdr *h = rule->arg.cap.hdr;
208 char **cap = s->req_cap;
209 int len;
210
211 key = sample_fetch_as_type(s->be, sess, s, SMP_OPT_DIR_REQ | partial, rule->arg.cap.expr, SMP_T_STR);
212 if (!key)
213 continue;
214
215 if (key->flags & SMP_F_MAY_CHANGE)
216 goto missing_data;
217
218 if (cap[h->index] == NULL)
Willy Tarreaubafbe012017-11-24 17:34:44 +0100219 cap[h->index] = pool_alloc(h->pool);
Willy Tarreau39713102016-11-25 15:49:32 +0100220
221 if (cap[h->index] == NULL) /* no more capture memory */
222 continue;
223
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200224 len = key->data.u.str.data;
Willy Tarreau39713102016-11-25 15:49:32 +0100225 if (len > h->len)
226 len = h->len;
227
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200228 memcpy(cap[h->index], key->data.u.str.area,
229 len);
Willy Tarreau39713102016-11-25 15:49:32 +0100230 cap[h->index][len] = 0;
231 }
232 else {
233 /* Custom keywords. */
234 if (!rule->action_ptr)
235 continue;
236
237 if (partial & SMP_OPT_FINAL)
238 act_flags |= ACT_FLAG_FINAL;
239
240 switch (rule->action_ptr(rule, s->be, s->sess, s, act_flags)) {
241 case ACT_RET_ERR:
242 case ACT_RET_CONT:
243 continue;
244 case ACT_RET_STOP:
Christopher Faulete445afd2019-07-04 11:08:38 +0200245 case ACT_RET_DONE:
Willy Tarreau39713102016-11-25 15:49:32 +0100246 break;
247 case ACT_RET_YIELD:
248 s->current_rule = rule;
249 goto missing_data;
250 }
Christopher Faulete445afd2019-07-04 11:08:38 +0200251 break; /* ACT_RET_STOP/DONE */
Willy Tarreau39713102016-11-25 15:49:32 +0100252 }
253 }
254 }
255
256 /* if we get there, it means we have no rule which matches, or
257 * we have an explicit accept, so we apply the default accept.
258 */
259 req->analysers &= ~an_bit;
260 req->analyse_exp = TICK_ETERNITY;
261 return 1;
262
263 missing_data:
264 channel_dont_connect(req);
265 /* just set the request timeout once at the beginning of the request */
266 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
267 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
268 return 0;
269
270}
271
272/* This function performs the TCP response analysis on the current response. It
273 * returns 1 if the processing can continue on next analysers, or zero if it
274 * needs more data, encounters an error, or wants to immediately abort the
275 * response. It relies on buffers flags, and updates s->rep->analysers. The
276 * function may be called for backend rules.
277 */
278int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
279{
280 struct session *sess = s->sess;
281 struct act_rule *rule;
282 int partial;
283 int act_flags = 0;
284
Christopher Faulet45073512018-07-20 10:16:29 +0200285 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau39713102016-11-25 15:49:32 +0100286 now_ms, __FUNCTION__,
287 s,
288 rep,
289 rep->rex, rep->wex,
290 rep->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200291 ci_data(rep),
Willy Tarreau39713102016-11-25 15:49:32 +0100292 rep->analysers);
293
294 /* We don't know whether we have enough data, so must proceed
295 * this way :
296 * - iterate through all rules in their declaration order
297 * - if one rule returns MISS, it means the inspect delay is
298 * not over yet, then return immediately, otherwise consider
299 * it as a non-match.
300 * - if one rule returns OK, then return OK
301 * - if one rule returns KO, then return KO
302 */
303
304 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
305 partial = SMP_OPT_FINAL;
306 else
307 partial = 0;
308
309 /* If "the current_rule_list" match the executed rule list, we are in
310 * resume condition. If a resume is needed it is always in the action
311 * and never in the ACL or converters. In this case, we initialise the
312 * current rule, and go to the action execution point.
313 */
314 if (s->current_rule) {
315 rule = s->current_rule;
316 s->current_rule = NULL;
317 if (s->current_rule_list == &s->be->tcp_rep.inspect_rules)
318 goto resume_execution;
319 }
320 s->current_rule_list = &s->be->tcp_rep.inspect_rules;
321
322 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
323 enum acl_test_res ret = ACL_TEST_PASS;
324
325 if (rule->cond) {
326 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
327 if (ret == ACL_TEST_MISS) {
328 /* just set the analyser timeout once at the beginning of the response */
329 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
330 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
331 return 0;
332 }
333
334 ret = acl_pass(ret);
335 if (rule->cond->pol == ACL_COND_UNLESS)
336 ret = !ret;
337 }
338
339 if (ret) {
340 act_flags |= ACT_FLAG_FIRST;
341resume_execution:
342 /* we have a matching rule. */
343 if (rule->action == ACT_ACTION_ALLOW) {
344 break;
345 }
346 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100347 si_must_kill_conn(chn_prod(rep));
Willy Tarreau39713102016-11-25 15:49:32 +0100348 channel_abort(rep);
349 channel_abort(&s->req);
350 rep->analysers = 0;
351
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100352 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
353 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100354 if (sess->listener && sess->listener->counters)
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100355 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreau39713102016-11-25 15:49:32 +0100356
357 if (!(s->flags & SF_ERR_MASK))
358 s->flags |= SF_ERR_PRXCOND;
359 if (!(s->flags & SF_FINST_MASK))
360 s->flags |= SF_FINST_D;
361 return 0;
362 }
363 else if (rule->action == ACT_TCP_CLOSE) {
364 chn_prod(rep)->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100365 si_must_kill_conn(chn_prod(rep));
Willy Tarreau39713102016-11-25 15:49:32 +0100366 si_shutr(chn_prod(rep));
367 si_shutw(chn_prod(rep));
368 break;
369 }
370 else {
371 /* Custom keywords. */
372 if (!rule->action_ptr)
373 continue;
374
375 if (partial & SMP_OPT_FINAL)
376 act_flags |= ACT_FLAG_FINAL;
377
378 switch (rule->action_ptr(rule, s->be, s->sess, s, act_flags)) {
379 case ACT_RET_ERR:
380 case ACT_RET_CONT:
381 continue;
382 case ACT_RET_STOP:
Christopher Faulete445afd2019-07-04 11:08:38 +0200383 case ACT_RET_DONE:
Willy Tarreau39713102016-11-25 15:49:32 +0100384 break;
385 case ACT_RET_YIELD:
386 channel_dont_close(rep);
387 s->current_rule = rule;
388 return 0;
389 }
Christopher Faulete445afd2019-07-04 11:08:38 +0200390 break; /* ACT_RET_STOP/DONE */
Willy Tarreau39713102016-11-25 15:49:32 +0100391 }
392 }
393 }
394
395 /* if we get there, it means we have no rule which matches, or
396 * we have an explicit accept, so we apply the default accept.
397 */
398 rep->analysers &= ~an_bit;
399 rep->analyse_exp = TICK_ETERNITY;
400 return 1;
401}
402
403
404/* This function performs the TCP layer4 analysis on the current request. It
405 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
406 * matches or if no more rule matches. It can only use rules which don't need
407 * any data. This only works on connection-based client-facing stream interfaces.
408 */
409int tcp_exec_l4_rules(struct session *sess)
410{
411 struct act_rule *rule;
412 struct stksess *ts;
413 struct stktable *t = NULL;
414 struct connection *conn = objt_conn(sess->origin);
415 int result = 1;
416 enum acl_test_res ret;
417
418 if (!conn)
419 return result;
420
421 list_for_each_entry(rule, &sess->fe->tcp_req.l4_rules, list) {
422 ret = ACL_TEST_PASS;
423
424 if (rule->cond) {
425 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
426 ret = acl_pass(ret);
427 if (rule->cond->pol == ACL_COND_UNLESS)
428 ret = !ret;
429 }
430
431 if (ret) {
432 /* we have a matching rule. */
433 if (rule->action == ACT_ACTION_ALLOW) {
434 break;
435 }
436 else if (rule->action == ACT_ACTION_DENY) {
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100437 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_conn, 1);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100438 if (sess->listener && sess->listener->counters)
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100439 _HA_ATOMIC_ADD(&sess->listener->counters->denied_conn, 1);
Willy Tarreau39713102016-11-25 15:49:32 +0100440
441 result = 0;
442 break;
443 }
444 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
445 /* Note: only the first valid tracking parameter of each
446 * applies.
447 */
448 struct stktable_key *key;
449
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200450 if (stkctr_entry(&sess->stkctr[trk_idx(rule->action)]))
Willy Tarreau39713102016-11-25 15:49:32 +0100451 continue;
452
453 t = rule->arg.trk_ctr.table.t;
454 key = stktable_fetch_key(t, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
455
456 if (key && (ts = stktable_get_entry(t, key)))
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200457 stream_track_stkctr(&sess->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau39713102016-11-25 15:49:32 +0100458 }
459 else if (rule->action == ACT_TCP_EXPECT_PX) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200460 if (!(conn->flags & (CO_FL_HANDSHAKE_NOSSL))) {
461 if (xprt_add_hs(conn) < 0) {
462 result = 0;
463 break;
464 }
465 }
Willy Tarreau39713102016-11-25 15:49:32 +0100466 conn->flags |= CO_FL_ACCEPT_PROXY;
Willy Tarreau39713102016-11-25 15:49:32 +0100467 }
468 else if (rule->action == ACT_TCP_EXPECT_CIP) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200469 if (!(conn->flags & (CO_FL_HANDSHAKE_NOSSL))) {
470 if (xprt_add_hs(conn) < 0) {
471 result = 0;
472 break;
473 }
474 }
Willy Tarreau39713102016-11-25 15:49:32 +0100475 conn->flags |= CO_FL_ACCEPT_CIP;
Willy Tarreau39713102016-11-25 15:49:32 +0100476 }
477 else {
478 /* Custom keywords. */
479 if (!rule->action_ptr)
480 break;
481 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_FLAG_FINAL | ACT_FLAG_FIRST)) {
482 case ACT_RET_YIELD:
483 /* yield is not allowed at this point. If this return code is
484 * used it is a bug, so I prefer to abort the process.
485 */
486 send_log(sess->fe, LOG_WARNING,
487 "Internal error: yield not allowed with tcp-request connection actions.");
488 case ACT_RET_STOP:
Christopher Faulete445afd2019-07-04 11:08:38 +0200489 case ACT_RET_DONE:
Willy Tarreau39713102016-11-25 15:49:32 +0100490 break;
491 case ACT_RET_CONT:
492 continue;
493 case ACT_RET_ERR:
494 result = 0;
495 break;
496 }
Christopher Faulete445afd2019-07-04 11:08:38 +0200497 break; /* ACT_RET_STOP/DONE */
Willy Tarreau39713102016-11-25 15:49:32 +0100498 }
499 }
500 }
501 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;
514 struct stksess *ts;
515 struct stktable *t = NULL;
516 int result = 1;
517 enum acl_test_res ret;
518
519 list_for_each_entry(rule, &sess->fe->tcp_req.l5_rules, list) {
520 ret = ACL_TEST_PASS;
521
522 if (rule->cond) {
523 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
524 ret = acl_pass(ret);
525 if (rule->cond->pol == ACL_COND_UNLESS)
526 ret = !ret;
527 }
528
529 if (ret) {
530 /* we have a matching rule. */
531 if (rule->action == ACT_ACTION_ALLOW) {
532 break;
533 }
534 else if (rule->action == ACT_ACTION_DENY) {
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100535 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_sess, 1);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100536 if (sess->listener && sess->listener->counters)
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100537 _HA_ATOMIC_ADD(&sess->listener->counters->denied_sess, 1);
Willy Tarreau39713102016-11-25 15:49:32 +0100538
539 result = 0;
540 break;
541 }
542 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
543 /* Note: only the first valid tracking parameter of each
544 * applies.
545 */
546 struct stktable_key *key;
547
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200548 if (stkctr_entry(&sess->stkctr[trk_idx(rule->action)]))
Willy Tarreau39713102016-11-25 15:49:32 +0100549 continue;
550
551 t = rule->arg.trk_ctr.table.t;
552 key = stktable_fetch_key(t, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
553
554 if (key && (ts = stktable_get_entry(t, key)))
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200555 stream_track_stkctr(&sess->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau39713102016-11-25 15:49:32 +0100556 }
557 else {
558 /* Custom keywords. */
559 if (!rule->action_ptr)
560 break;
561 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_FLAG_FINAL | ACT_FLAG_FIRST)) {
562 case ACT_RET_YIELD:
563 /* yield is not allowed at this point. If this return code is
564 * used it is a bug, so I prefer to abort the process.
565 */
566 send_log(sess->fe, LOG_WARNING,
567 "Internal error: yield not allowed with tcp-request session actions.");
568 case ACT_RET_STOP:
Christopher Faulete445afd2019-07-04 11:08:38 +0200569 case ACT_RET_DONE:
Willy Tarreau39713102016-11-25 15:49:32 +0100570 break;
571 case ACT_RET_CONT:
572 continue;
573 case ACT_RET_ERR:
574 result = 0;
575 break;
576 }
Christopher Faulete445afd2019-07-04 11:08:38 +0200577 break; /* ACT_RET_STOP/DONE */
Willy Tarreau39713102016-11-25 15:49:32 +0100578 }
579 }
580 }
581 return result;
582}
583
584/* Parse a tcp-response rule. Return a negative value in case of failure */
585static int tcp_parse_response_rule(char **args, int arg, int section_type,
586 struct proxy *curpx, struct proxy *defpx,
587 struct act_rule *rule, char **err,
588 unsigned int where,
589 const char *file, int line)
590{
591 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
592 memprintf(err, "%s %s is only allowed in 'backend' sections",
593 args[0], args[1]);
594 return -1;
595 }
596
597 if (strcmp(args[arg], "accept") == 0) {
598 arg++;
599 rule->action = ACT_ACTION_ALLOW;
600 }
601 else if (strcmp(args[arg], "reject") == 0) {
602 arg++;
603 rule->action = ACT_ACTION_DENY;
604 }
605 else if (strcmp(args[arg], "close") == 0) {
606 arg++;
607 rule->action = ACT_TCP_CLOSE;
608 }
609 else {
610 struct action_kw *kw;
611 kw = tcp_res_cont_action(args[arg]);
612 if (kw) {
613 arg++;
614 rule->from = ACT_F_TCP_RES_CNT;
615 rule->kw = kw;
616 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
617 return -1;
618 } else {
619 action_build_list(&tcp_res_cont_keywords, &trash);
620 memprintf(err,
621 "'%s %s' expects 'accept', 'close', 'reject', %s in %s '%s' (got '%s')",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200622 args[0], args[1], trash.area,
623 proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau39713102016-11-25 15:49:32 +0100624 return -1;
625 }
626 }
627
628 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +0200629 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +0100630 memprintf(err,
631 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
632 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
633 return -1;
634 }
635 }
636 else if (*args[arg]) {
637 memprintf(err,
638 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
639 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
640 return -1;
641 }
642 return 0;
643}
644
645
646
647/* Parse a tcp-request rule. Return a negative value in case of failure */
648static int tcp_parse_request_rule(char **args, int arg, int section_type,
649 struct proxy *curpx, struct proxy *defpx,
650 struct act_rule *rule, char **err,
651 unsigned int where, const char *file, int line)
652{
653 if (curpx == defpx) {
654 memprintf(err, "%s %s is not allowed in 'defaults' sections",
655 args[0], args[1]);
656 return -1;
657 }
658
659 if (!strcmp(args[arg], "accept")) {
660 arg++;
661 rule->action = ACT_ACTION_ALLOW;
662 }
663 else if (!strcmp(args[arg], "reject")) {
664 arg++;
665 rule->action = ACT_ACTION_DENY;
666 }
667 else if (strcmp(args[arg], "capture") == 0) {
668 struct sample_expr *expr;
669 struct cap_hdr *hdr;
670 int kw = arg;
671 int len = 0;
672
673 if (!(curpx->cap & PR_CAP_FE)) {
674 memprintf(err,
675 "'%s %s %s' : proxy '%s' has no frontend capability",
676 args[0], args[1], args[kw], curpx->id);
677 return -1;
678 }
679
680 if (!(where & SMP_VAL_FE_REQ_CNT)) {
681 memprintf(err,
682 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
683 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
684 return -1;
685 }
686
687 arg++;
688
689 curpx->conf.args.ctx = ARGC_CAP;
690 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
691 if (!expr) {
692 memprintf(err,
693 "'%s %s %s' : %s",
694 args[0], args[1], args[kw], *err);
695 return -1;
696 }
697
698 if (!(expr->fetch->val & where)) {
699 memprintf(err,
700 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
701 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Christopher Fauletff038a62020-01-14 15:05:56 +0100702 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100703 return -1;
704 }
705
706 if (strcmp(args[arg], "len") == 0) {
707 arg++;
708 if (!args[arg]) {
709 memprintf(err,
710 "'%s %s %s' : missing length value",
711 args[0], args[1], args[kw]);
Christopher Fauletff038a62020-01-14 15:05:56 +0100712 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100713 return -1;
714 }
715 /* we copy the table name for now, it will be resolved later */
716 len = atoi(args[arg]);
717 if (len <= 0) {
718 memprintf(err,
719 "'%s %s %s' : length must be > 0",
720 args[0], args[1], args[kw]);
Christopher Fauletff038a62020-01-14 15:05:56 +0100721 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100722 return -1;
723 }
724 arg++;
725 }
726
727 if (!len) {
728 memprintf(err,
729 "'%s %s %s' : a positive 'len' argument is mandatory",
730 args[0], args[1], args[kw]);
731 free(expr);
732 return -1;
733 }
734
735 hdr = calloc(1, sizeof(*hdr));
736 hdr->next = curpx->req_cap;
737 hdr->name = NULL; /* not a header capture */
738 hdr->namelen = 0;
739 hdr->len = len;
740 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
741 hdr->index = curpx->nb_req_cap++;
742
743 curpx->req_cap = hdr;
744 curpx->to_log |= LW_REQHDR;
745
746 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
747 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
748
749 rule->arg.cap.expr = expr;
750 rule->arg.cap.hdr = hdr;
751 rule->action = ACT_TCP_CAPTURE;
752 }
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100753 else if (strncmp(args[arg], "track-sc", 8) == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100754 struct sample_expr *expr;
755 int kw = arg;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100756 unsigned int tsc_num;
757 const char *tsc_num_str;
Willy Tarreau39713102016-11-25 15:49:32 +0100758
759 arg++;
760
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100761 tsc_num_str = &args[kw][8];
762 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1) {
763 memprintf(err, "'%s %s %s' : %s", args[0], args[1], args[kw], *err);
764 return -1;
765 }
766
Willy Tarreau39713102016-11-25 15:49:32 +0100767 curpx->conf.args.ctx = ARGC_TRK;
768 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
769 if (!expr) {
770 memprintf(err,
771 "'%s %s %s' : %s",
772 args[0], args[1], args[kw], *err);
773 return -1;
774 }
775
776 if (!(expr->fetch->val & where)) {
777 memprintf(err,
778 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
779 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
Christopher Fauletff038a62020-01-14 15:05:56 +0100780 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100781 return -1;
782 }
783
784 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
785 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
786
787 if (strcmp(args[arg], "table") == 0) {
788 arg++;
789 if (!args[arg]) {
790 memprintf(err,
791 "'%s %s %s' : missing table name",
792 args[0], args[1], args[kw]);
Christopher Fauletff038a62020-01-14 15:05:56 +0100793 release_sample_expr(expr);
Willy Tarreau39713102016-11-25 15:49:32 +0100794 return -1;
795 }
796 /* we copy the table name for now, it will be resolved later */
797 rule->arg.trk_ctr.table.n = strdup(args[arg]);
798 arg++;
799 }
800 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100801 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +0200802 rule->check_ptr = check_trk_action;
Willy Tarreau39713102016-11-25 15:49:32 +0100803 }
804 else if (strcmp(args[arg], "expect-proxy") == 0) {
805 if (strcmp(args[arg+1], "layer4") != 0) {
806 memprintf(err,
807 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
808 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
809 return -1;
810 }
811
812 if (!(where & SMP_VAL_FE_CON_ACC)) {
813 memprintf(err,
814 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
815 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
816 return -1;
817 }
818
819 arg += 2;
820 rule->action = ACT_TCP_EXPECT_PX;
821 }
822 else if (strcmp(args[arg], "expect-netscaler-cip") == 0) {
823 if (strcmp(args[arg+1], "layer4") != 0) {
824 memprintf(err,
825 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
826 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
827 return -1;
828 }
829
830 if (!(where & SMP_VAL_FE_CON_ACC)) {
831 memprintf(err,
832 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
833 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
834 return -1;
835 }
836
837 arg += 2;
838 rule->action = ACT_TCP_EXPECT_CIP;
839 }
840 else {
841 struct action_kw *kw;
842 if (where & SMP_VAL_FE_CON_ACC) {
843 /* L4 */
844 kw = tcp_req_conn_action(args[arg]);
845 rule->kw = kw;
846 rule->from = ACT_F_TCP_REQ_CON;
847 } else if (where & SMP_VAL_FE_SES_ACC) {
848 /* L5 */
849 kw = tcp_req_sess_action(args[arg]);
850 rule->kw = kw;
851 rule->from = ACT_F_TCP_REQ_SES;
852 } else {
853 /* L6 */
854 kw = tcp_req_cont_action(args[arg]);
855 rule->kw = kw;
856 rule->from = ACT_F_TCP_REQ_CNT;
857 }
858 if (kw) {
859 arg++;
860 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
861 return -1;
862 } else {
863 if (where & SMP_VAL_FE_CON_ACC)
864 action_build_list(&tcp_req_conn_keywords, &trash);
865 else if (where & SMP_VAL_FE_SES_ACC)
866 action_build_list(&tcp_req_sess_keywords, &trash);
867 else
868 action_build_list(&tcp_req_cont_keywords, &trash);
869 memprintf(err,
870 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d', %s "
871 "in %s '%s' (got '%s').\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200872 args[0], args[1], MAX_SESS_STKCTR-1,
873 trash.area, proxy_type_str(curpx),
Willy Tarreau39713102016-11-25 15:49:32 +0100874 curpx->id, args[arg]);
875 return -1;
876 }
877 }
878
879 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +0200880 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +0100881 memprintf(err,
882 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
883 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
884 return -1;
885 }
886 }
887 else if (*args[arg]) {
888 memprintf(err,
889 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
890 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
891 return -1;
892 }
893 return 0;
894}
895
896/* This function should be called to parse a line starting with the "tcp-response"
897 * keyword.
898 */
899static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
900 struct proxy *defpx, const char *file, int line,
901 char **err)
902{
903 const char *ptr = NULL;
904 unsigned int val;
905 int warn = 0;
906 int arg;
907 struct act_rule *rule;
908 unsigned int where;
909 const struct acl *acl;
910 const char *kw;
911
912 if (!*args[1]) {
913 memprintf(err, "missing argument for '%s' in %s '%s'",
914 args[0], proxy_type_str(curpx), curpx->id);
915 return -1;
916 }
917
918 if (strcmp(args[1], "inspect-delay") == 0) {
919 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
920 memprintf(err, "%s %s is only allowed in 'backend' sections",
921 args[0], args[1]);
922 return -1;
923 }
924
925 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
926 memprintf(err,
927 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
928 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200929
930 if (ptr == PARSE_TIME_OVER)
931 memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]);
932 else if (ptr == PARSE_TIME_UNDER)
933 memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]);
934 else if (ptr)
Willy Tarreau39713102016-11-25 15:49:32 +0100935 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
936 return -1;
937 }
938
939 if (curpx->tcp_rep.inspect_delay) {
940 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
941 args[0], args[1], proxy_type_str(curpx), curpx->id);
942 return 1;
943 }
944 curpx->tcp_rep.inspect_delay = val;
945 return 0;
946 }
947
948 rule = calloc(1, sizeof(*rule));
949 LIST_INIT(&rule->list);
950 arg = 1;
951 where = 0;
952
953 if (strcmp(args[1], "content") == 0) {
954 arg++;
955
956 if (curpx->cap & PR_CAP_FE)
957 where |= SMP_VAL_FE_RES_CNT;
958 if (curpx->cap & PR_CAP_BE)
959 where |= SMP_VAL_BE_RES_CNT;
960
961 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
962 goto error;
963
964 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
965 if (acl) {
966 if (acl->name && *acl->name)
967 memprintf(err,
968 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
969 acl->name, args[0], args[1], sample_ckp_names(where));
970 else
971 memprintf(err,
972 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
973 args[0], args[1],
974 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
975 sample_ckp_names(where));
976
977 warn++;
978 }
979 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
980 if (acl->name && *acl->name)
981 memprintf(err,
982 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
983 acl->name, kw, sample_ckp_names(where));
984 else
985 memprintf(err,
986 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
987 kw, sample_ckp_names(where));
988 warn++;
989 }
990
991 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
992 }
993 else {
994 memprintf(err,
995 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
996 args[0], proxy_type_str(curpx), curpx->id, args[1]);
997 goto error;
998 }
999
1000 return warn;
1001 error:
1002 free(rule);
1003 return -1;
1004}
1005
1006
1007/* This function should be called to parse a line starting with the "tcp-request"
1008 * keyword.
1009 */
1010static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
1011 struct proxy *defpx, const char *file, int line,
1012 char **err)
1013{
1014 const char *ptr = NULL;
1015 unsigned int val;
1016 int warn = 0;
1017 int arg;
1018 struct act_rule *rule;
1019 unsigned int where;
1020 const struct acl *acl;
1021 const char *kw;
1022
1023 if (!*args[1]) {
1024 if (curpx == defpx)
1025 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1026 else
1027 memprintf(err, "missing argument for '%s' in %s '%s'",
1028 args[0], proxy_type_str(curpx), curpx->id);
1029 return -1;
1030 }
1031
1032 if (!strcmp(args[1], "inspect-delay")) {
1033 if (curpx == defpx) {
1034 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1035 args[0], args[1]);
1036 return -1;
1037 }
1038
1039 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1040 memprintf(err,
1041 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1042 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001043
1044 if (ptr == PARSE_TIME_OVER)
1045 memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]);
1046 else if (ptr == PARSE_TIME_UNDER)
1047 memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]);
1048 else if (ptr)
Willy Tarreau39713102016-11-25 15:49:32 +01001049 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
1050 return -1;
1051 }
1052
1053 if (curpx->tcp_req.inspect_delay) {
1054 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1055 args[0], args[1], proxy_type_str(curpx), curpx->id);
1056 return 1;
1057 }
1058 curpx->tcp_req.inspect_delay = val;
1059 return 0;
1060 }
1061
1062 rule = calloc(1, sizeof(*rule));
1063 LIST_INIT(&rule->list);
1064 arg = 1;
1065 where = 0;
1066
1067 if (strcmp(args[1], "content") == 0) {
1068 arg++;
1069
1070 if (curpx->cap & PR_CAP_FE)
1071 where |= SMP_VAL_FE_REQ_CNT;
1072 if (curpx->cap & PR_CAP_BE)
1073 where |= SMP_VAL_BE_REQ_CNT;
1074
1075 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1076 goto error;
1077
1078 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1079 if (acl) {
1080 if (acl->name && *acl->name)
1081 memprintf(err,
1082 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1083 acl->name, args[0], args[1], sample_ckp_names(where));
1084 else
1085 memprintf(err,
1086 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1087 args[0], args[1],
1088 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1089 sample_ckp_names(where));
1090
1091 warn++;
1092 }
1093 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1094 if (acl->name && *acl->name)
1095 memprintf(err,
1096 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1097 acl->name, kw, sample_ckp_names(where));
1098 else
1099 memprintf(err,
1100 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1101 kw, sample_ckp_names(where));
1102 warn++;
1103 }
1104
1105 /* the following function directly emits the warning */
1106 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
1107 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
1108 }
1109 else if (strcmp(args[1], "connection") == 0) {
1110 arg++;
1111
1112 if (!(curpx->cap & PR_CAP_FE)) {
1113 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1114 args[0], args[1], proxy_type_str(curpx), curpx->id);
1115 goto error;
1116 }
1117
1118 where |= SMP_VAL_FE_CON_ACC;
1119
1120 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1121 goto error;
1122
1123 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1124 if (acl) {
1125 if (acl->name && *acl->name)
1126 memprintf(err,
1127 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1128 acl->name, args[0], args[1], sample_ckp_names(where));
1129 else
1130 memprintf(err,
1131 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1132 args[0], args[1],
1133 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1134 sample_ckp_names(where));
1135
1136 warn++;
1137 }
1138 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1139 if (acl->name && *acl->name)
1140 memprintf(err,
1141 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1142 acl->name, kw, sample_ckp_names(where));
1143 else
1144 memprintf(err,
1145 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1146 kw, sample_ckp_names(where));
1147 warn++;
1148 }
1149
1150 /* the following function directly emits the warning */
1151 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
1152 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
1153 }
1154 else if (strcmp(args[1], "session") == 0) {
1155 arg++;
1156
1157 if (!(curpx->cap & PR_CAP_FE)) {
1158 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1159 args[0], args[1], proxy_type_str(curpx), curpx->id);
1160 goto error;
1161 }
1162
1163 where |= SMP_VAL_FE_SES_ACC;
1164
1165 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1166 goto error;
1167
1168 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1169 if (acl) {
1170 if (acl->name && *acl->name)
1171 memprintf(err,
1172 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1173 acl->name, args[0], args[1], sample_ckp_names(where));
1174 else
1175 memprintf(err,
1176 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1177 args[0], args[1],
1178 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1179 sample_ckp_names(where));
1180 warn++;
1181 }
1182 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1183 if (acl->name && *acl->name)
1184 memprintf(err,
1185 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1186 acl->name, kw, sample_ckp_names(where));
1187 else
1188 memprintf(err,
1189 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1190 kw, sample_ckp_names(where));
1191 warn++;
1192 }
1193
1194 /* the following function directly emits the warning */
1195 warnif_misplaced_tcp_sess(curpx, file, line, args[0]);
1196 LIST_ADDQ(&curpx->tcp_req.l5_rules, &rule->list);
1197 }
1198 else {
1199 if (curpx == defpx)
1200 memprintf(err,
1201 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1202 args[0], args[1]);
1203 else
1204 memprintf(err,
1205 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1206 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1207 goto error;
1208 }
1209
1210 return warn;
1211 error:
1212 free(rule);
1213 return -1;
1214}
1215
1216static struct cfg_kw_list cfg_kws = {ILH, {
1217 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1218 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
1219 { 0, NULL, NULL },
1220}};
1221
Willy Tarreau0108d902018-11-25 19:14:37 +01001222INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau39713102016-11-25 15:49:32 +01001223
1224/*
1225 * Local variables:
1226 * c-indent-level: 8
1227 * c-basic-offset: 8
1228 * End:
1229 */