blob: 72b11ae369559064c76231966b8d19d9dba56937 [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:
245 break;
246 case ACT_RET_YIELD:
247 s->current_rule = rule;
248 goto missing_data;
249 }
250 break; /* ACT_RET_STOP */
251 }
252 }
253 }
254
255 /* if we get there, it means we have no rule which matches, or
256 * we have an explicit accept, so we apply the default accept.
257 */
258 req->analysers &= ~an_bit;
259 req->analyse_exp = TICK_ETERNITY;
260 return 1;
261
262 missing_data:
263 channel_dont_connect(req);
264 /* just set the request timeout once at the beginning of the request */
265 if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay)
266 req->analyse_exp = tick_add(now_ms, s->be->tcp_req.inspect_delay);
267 return 0;
268
269}
270
271/* This function performs the TCP response analysis on the current response. It
272 * returns 1 if the processing can continue on next analysers, or zero if it
273 * needs more data, encounters an error, or wants to immediately abort the
274 * response. It relies on buffers flags, and updates s->rep->analysers. The
275 * function may be called for backend rules.
276 */
277int tcp_inspect_response(struct stream *s, struct channel *rep, int an_bit)
278{
279 struct session *sess = s->sess;
280 struct act_rule *rule;
281 int partial;
282 int act_flags = 0;
283
Christopher Faulet45073512018-07-20 10:16:29 +0200284 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 +0100285 now_ms, __FUNCTION__,
286 s,
287 rep,
288 rep->rex, rep->wex,
289 rep->flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200290 ci_data(rep),
Willy Tarreau39713102016-11-25 15:49:32 +0100291 rep->analysers);
292
293 /* We don't know whether we have enough data, so must proceed
294 * this way :
295 * - iterate through all rules in their declaration order
296 * - if one rule returns MISS, it means the inspect delay is
297 * not over yet, then return immediately, otherwise consider
298 * it as a non-match.
299 * - if one rule returns OK, then return OK
300 * - if one rule returns KO, then return KO
301 */
302
303 if (rep->flags & CF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms))
304 partial = SMP_OPT_FINAL;
305 else
306 partial = 0;
307
308 /* If "the current_rule_list" match the executed rule list, we are in
309 * resume condition. If a resume is needed it is always in the action
310 * and never in the ACL or converters. In this case, we initialise the
311 * current rule, and go to the action execution point.
312 */
313 if (s->current_rule) {
314 rule = s->current_rule;
315 s->current_rule = NULL;
316 if (s->current_rule_list == &s->be->tcp_rep.inspect_rules)
317 goto resume_execution;
318 }
319 s->current_rule_list = &s->be->tcp_rep.inspect_rules;
320
321 list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) {
322 enum acl_test_res ret = ACL_TEST_PASS;
323
324 if (rule->cond) {
325 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_RES | partial);
326 if (ret == ACL_TEST_MISS) {
327 /* just set the analyser timeout once at the beginning of the response */
328 if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay)
329 rep->analyse_exp = tick_add(now_ms, s->be->tcp_rep.inspect_delay);
330 return 0;
331 }
332
333 ret = acl_pass(ret);
334 if (rule->cond->pol == ACL_COND_UNLESS)
335 ret = !ret;
336 }
337
338 if (ret) {
339 act_flags |= ACT_FLAG_FIRST;
340resume_execution:
341 /* we have a matching rule. */
342 if (rule->action == ACT_ACTION_ALLOW) {
343 break;
344 }
345 else if (rule->action == ACT_ACTION_DENY) {
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100346 si_must_kill_conn(chn_prod(rep));
Willy Tarreau39713102016-11-25 15:49:32 +0100347 channel_abort(rep);
348 channel_abort(&s->req);
349 rep->analysers = 0;
350
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100351 _HA_ATOMIC_ADD(&s->be->be_counters.denied_resp, 1);
352 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_resp, 1);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100353 if (sess->listener && sess->listener->counters)
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100354 _HA_ATOMIC_ADD(&sess->listener->counters->denied_resp, 1);
Willy Tarreau39713102016-11-25 15:49:32 +0100355
356 if (!(s->flags & SF_ERR_MASK))
357 s->flags |= SF_ERR_PRXCOND;
358 if (!(s->flags & SF_FINST_MASK))
359 s->flags |= SF_FINST_D;
360 return 0;
361 }
362 else if (rule->action == ACT_TCP_CLOSE) {
363 chn_prod(rep)->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau0f9cd7b2019-01-31 19:02:43 +0100364 si_must_kill_conn(chn_prod(rep));
Willy Tarreau39713102016-11-25 15:49:32 +0100365 si_shutr(chn_prod(rep));
366 si_shutw(chn_prod(rep));
367 break;
368 }
369 else {
370 /* Custom keywords. */
371 if (!rule->action_ptr)
372 continue;
373
374 if (partial & SMP_OPT_FINAL)
375 act_flags |= ACT_FLAG_FINAL;
376
377 switch (rule->action_ptr(rule, s->be, s->sess, s, act_flags)) {
378 case ACT_RET_ERR:
379 case ACT_RET_CONT:
380 continue;
381 case ACT_RET_STOP:
382 break;
383 case ACT_RET_YIELD:
384 channel_dont_close(rep);
385 s->current_rule = rule;
386 return 0;
387 }
388 break; /* ACT_RET_STOP */
389 }
390 }
391 }
392
393 /* if we get there, it means we have no rule which matches, or
394 * we have an explicit accept, so we apply the default accept.
395 */
396 rep->analysers &= ~an_bit;
397 rep->analyse_exp = TICK_ETERNITY;
398 return 1;
399}
400
401
402/* This function performs the TCP layer4 analysis on the current request. It
403 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
404 * matches or if no more rule matches. It can only use rules which don't need
405 * any data. This only works on connection-based client-facing stream interfaces.
406 */
407int tcp_exec_l4_rules(struct session *sess)
408{
409 struct act_rule *rule;
410 struct stksess *ts;
411 struct stktable *t = NULL;
412 struct connection *conn = objt_conn(sess->origin);
413 int result = 1;
414 enum acl_test_res ret;
415
416 if (!conn)
417 return result;
418
419 list_for_each_entry(rule, &sess->fe->tcp_req.l4_rules, list) {
420 ret = ACL_TEST_PASS;
421
422 if (rule->cond) {
423 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
424 ret = acl_pass(ret);
425 if (rule->cond->pol == ACL_COND_UNLESS)
426 ret = !ret;
427 }
428
429 if (ret) {
430 /* we have a matching rule. */
431 if (rule->action == ACT_ACTION_ALLOW) {
432 break;
433 }
434 else if (rule->action == ACT_ACTION_DENY) {
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100435 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_conn, 1);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100436 if (sess->listener && sess->listener->counters)
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100437 _HA_ATOMIC_ADD(&sess->listener->counters->denied_conn, 1);
Willy Tarreau39713102016-11-25 15:49:32 +0100438
439 result = 0;
440 break;
441 }
442 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
443 /* Note: only the first valid tracking parameter of each
444 * applies.
445 */
446 struct stktable_key *key;
447
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200448 if (stkctr_entry(&sess->stkctr[trk_idx(rule->action)]))
Willy Tarreau39713102016-11-25 15:49:32 +0100449 continue;
450
451 t = rule->arg.trk_ctr.table.t;
452 key = stktable_fetch_key(t, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
453
454 if (key && (ts = stktable_get_entry(t, key)))
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200455 stream_track_stkctr(&sess->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau39713102016-11-25 15:49:32 +0100456 }
457 else if (rule->action == ACT_TCP_EXPECT_PX) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200458 if (!(conn->flags & (CO_FL_HANDSHAKE_NOSSL))) {
459 if (xprt_add_hs(conn) < 0) {
460 result = 0;
461 break;
462 }
463 }
Willy Tarreau39713102016-11-25 15:49:32 +0100464 conn->flags |= CO_FL_ACCEPT_PROXY;
Willy Tarreau39713102016-11-25 15:49:32 +0100465 }
466 else if (rule->action == ACT_TCP_EXPECT_CIP) {
Olivier Houchardfe50bfb2019-05-27 12:09:19 +0200467 if (!(conn->flags & (CO_FL_HANDSHAKE_NOSSL))) {
468 if (xprt_add_hs(conn) < 0) {
469 result = 0;
470 break;
471 }
472 }
Willy Tarreau39713102016-11-25 15:49:32 +0100473 conn->flags |= CO_FL_ACCEPT_CIP;
Willy Tarreau39713102016-11-25 15:49:32 +0100474 }
475 else {
476 /* Custom keywords. */
477 if (!rule->action_ptr)
478 break;
479 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_FLAG_FINAL | ACT_FLAG_FIRST)) {
480 case ACT_RET_YIELD:
481 /* yield is not allowed at this point. If this return code is
482 * used it is a bug, so I prefer to abort the process.
483 */
484 send_log(sess->fe, LOG_WARNING,
485 "Internal error: yield not allowed with tcp-request connection actions.");
486 case ACT_RET_STOP:
487 break;
488 case ACT_RET_CONT:
489 continue;
490 case ACT_RET_ERR:
491 result = 0;
492 break;
493 }
494 break; /* ACT_RET_STOP */
495 }
496 }
497 }
498 return result;
499}
500
501/* This function performs the TCP layer5 analysis on the current request. It
502 * returns 0 if a reject rule matches, otherwise 1 if either an accept rule
503 * matches or if no more rule matches. It can only use rules which don't need
504 * any data. This only works on session-based client-facing stream interfaces.
505 * An example of valid use case is to track a stick-counter on the source
506 * address extracted from the proxy protocol.
507 */
508int tcp_exec_l5_rules(struct session *sess)
509{
510 struct act_rule *rule;
511 struct stksess *ts;
512 struct stktable *t = NULL;
513 int result = 1;
514 enum acl_test_res ret;
515
516 list_for_each_entry(rule, &sess->fe->tcp_req.l5_rules, list) {
517 ret = ACL_TEST_PASS;
518
519 if (rule->cond) {
520 ret = acl_exec_cond(rule->cond, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
521 ret = acl_pass(ret);
522 if (rule->cond->pol == ACL_COND_UNLESS)
523 ret = !ret;
524 }
525
526 if (ret) {
527 /* we have a matching rule. */
528 if (rule->action == ACT_ACTION_ALLOW) {
529 break;
530 }
531 else if (rule->action == ACT_ACTION_DENY) {
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100532 _HA_ATOMIC_ADD(&sess->fe->fe_counters.denied_sess, 1);
Willy Tarreaua12dde02016-12-22 18:14:41 +0100533 if (sess->listener && sess->listener->counters)
Olivier Houchard64dbb2d2019-03-08 18:55:10 +0100534 _HA_ATOMIC_ADD(&sess->listener->counters->denied_sess, 1);
Willy Tarreau39713102016-11-25 15:49:32 +0100535
536 result = 0;
537 break;
538 }
539 else if (rule->action >= ACT_ACTION_TRK_SC0 && rule->action <= ACT_ACTION_TRK_SCMAX) {
540 /* Note: only the first valid tracking parameter of each
541 * applies.
542 */
543 struct stktable_key *key;
544
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200545 if (stkctr_entry(&sess->stkctr[trk_idx(rule->action)]))
Willy Tarreau39713102016-11-25 15:49:32 +0100546 continue;
547
548 t = rule->arg.trk_ctr.table.t;
549 key = stktable_fetch_key(t, sess->fe, sess, NULL, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.trk_ctr.expr, NULL);
550
551 if (key && (ts = stktable_get_entry(t, key)))
Christopher Faulet4fce0d82017-09-18 11:57:31 +0200552 stream_track_stkctr(&sess->stkctr[trk_idx(rule->action)], t, ts);
Willy Tarreau39713102016-11-25 15:49:32 +0100553 }
554 else {
555 /* Custom keywords. */
556 if (!rule->action_ptr)
557 break;
558 switch (rule->action_ptr(rule, sess->fe, sess, NULL, ACT_FLAG_FINAL | ACT_FLAG_FIRST)) {
559 case ACT_RET_YIELD:
560 /* yield is not allowed at this point. If this return code is
561 * used it is a bug, so I prefer to abort the process.
562 */
563 send_log(sess->fe, LOG_WARNING,
564 "Internal error: yield not allowed with tcp-request session actions.");
565 case ACT_RET_STOP:
566 break;
567 case ACT_RET_CONT:
568 continue;
569 case ACT_RET_ERR:
570 result = 0;
571 break;
572 }
573 break; /* ACT_RET_STOP */
574 }
575 }
576 }
577 return result;
578}
579
580/* Parse a tcp-response rule. Return a negative value in case of failure */
581static int tcp_parse_response_rule(char **args, int arg, int section_type,
582 struct proxy *curpx, struct proxy *defpx,
583 struct act_rule *rule, char **err,
584 unsigned int where,
585 const char *file, int line)
586{
587 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
588 memprintf(err, "%s %s is only allowed in 'backend' sections",
589 args[0], args[1]);
590 return -1;
591 }
592
593 if (strcmp(args[arg], "accept") == 0) {
594 arg++;
595 rule->action = ACT_ACTION_ALLOW;
596 }
597 else if (strcmp(args[arg], "reject") == 0) {
598 arg++;
599 rule->action = ACT_ACTION_DENY;
600 }
601 else if (strcmp(args[arg], "close") == 0) {
602 arg++;
603 rule->action = ACT_TCP_CLOSE;
604 }
605 else {
606 struct action_kw *kw;
607 kw = tcp_res_cont_action(args[arg]);
608 if (kw) {
609 arg++;
610 rule->from = ACT_F_TCP_RES_CNT;
611 rule->kw = kw;
612 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
613 return -1;
614 } else {
615 action_build_list(&tcp_res_cont_keywords, &trash);
616 memprintf(err,
617 "'%s %s' expects 'accept', 'close', 'reject', %s in %s '%s' (got '%s')",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200618 args[0], args[1], trash.area,
619 proxy_type_str(curpx), curpx->id, args[arg]);
Willy Tarreau39713102016-11-25 15:49:32 +0100620 return -1;
621 }
622 }
623
624 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +0200625 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +0100626 memprintf(err,
627 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
628 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
629 return -1;
630 }
631 }
632 else if (*args[arg]) {
633 memprintf(err,
634 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
635 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
636 return -1;
637 }
638 return 0;
639}
640
641
642
643/* Parse a tcp-request rule. Return a negative value in case of failure */
644static int tcp_parse_request_rule(char **args, int arg, int section_type,
645 struct proxy *curpx, struct proxy *defpx,
646 struct act_rule *rule, char **err,
647 unsigned int where, const char *file, int line)
648{
649 if (curpx == defpx) {
650 memprintf(err, "%s %s is not allowed in 'defaults' sections",
651 args[0], args[1]);
652 return -1;
653 }
654
655 if (!strcmp(args[arg], "accept")) {
656 arg++;
657 rule->action = ACT_ACTION_ALLOW;
658 }
659 else if (!strcmp(args[arg], "reject")) {
660 arg++;
661 rule->action = ACT_ACTION_DENY;
662 }
663 else if (strcmp(args[arg], "capture") == 0) {
664 struct sample_expr *expr;
665 struct cap_hdr *hdr;
666 int kw = arg;
667 int len = 0;
668
669 if (!(curpx->cap & PR_CAP_FE)) {
670 memprintf(err,
671 "'%s %s %s' : proxy '%s' has no frontend capability",
672 args[0], args[1], args[kw], curpx->id);
673 return -1;
674 }
675
676 if (!(where & SMP_VAL_FE_REQ_CNT)) {
677 memprintf(err,
678 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
679 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
680 return -1;
681 }
682
683 arg++;
684
685 curpx->conf.args.ctx = ARGC_CAP;
686 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
687 if (!expr) {
688 memprintf(err,
689 "'%s %s %s' : %s",
690 args[0], args[1], args[kw], *err);
691 return -1;
692 }
693
694 if (!(expr->fetch->val & where)) {
695 memprintf(err,
696 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
697 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
698 free(expr);
699 return -1;
700 }
701
702 if (strcmp(args[arg], "len") == 0) {
703 arg++;
704 if (!args[arg]) {
705 memprintf(err,
706 "'%s %s %s' : missing length value",
707 args[0], args[1], args[kw]);
708 free(expr);
709 return -1;
710 }
711 /* we copy the table name for now, it will be resolved later */
712 len = atoi(args[arg]);
713 if (len <= 0) {
714 memprintf(err,
715 "'%s %s %s' : length must be > 0",
716 args[0], args[1], args[kw]);
717 free(expr);
718 return -1;
719 }
720 arg++;
721 }
722
723 if (!len) {
724 memprintf(err,
725 "'%s %s %s' : a positive 'len' argument is mandatory",
726 args[0], args[1], args[kw]);
727 free(expr);
728 return -1;
729 }
730
731 hdr = calloc(1, sizeof(*hdr));
732 hdr->next = curpx->req_cap;
733 hdr->name = NULL; /* not a header capture */
734 hdr->namelen = 0;
735 hdr->len = len;
736 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
737 hdr->index = curpx->nb_req_cap++;
738
739 curpx->req_cap = hdr;
740 curpx->to_log |= LW_REQHDR;
741
742 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
743 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
744
745 rule->arg.cap.expr = expr;
746 rule->arg.cap.hdr = hdr;
747 rule->action = ACT_TCP_CAPTURE;
748 }
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100749 else if (strncmp(args[arg], "track-sc", 8) == 0) {
Willy Tarreau39713102016-11-25 15:49:32 +0100750 struct sample_expr *expr;
751 int kw = arg;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100752 unsigned int tsc_num;
753 const char *tsc_num_str;
Willy Tarreau39713102016-11-25 15:49:32 +0100754
755 arg++;
756
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100757 tsc_num_str = &args[kw][8];
758 if (cfg_parse_track_sc_num(&tsc_num, tsc_num_str, tsc_num_str + strlen(tsc_num_str), err) == -1) {
759 memprintf(err, "'%s %s %s' : %s", args[0], args[1], args[kw], *err);
760 return -1;
761 }
762
Willy Tarreau39713102016-11-25 15:49:32 +0100763 curpx->conf.args.ctx = ARGC_TRK;
764 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
765 if (!expr) {
766 memprintf(err,
767 "'%s %s %s' : %s",
768 args[0], args[1], args[kw], *err);
769 return -1;
770 }
771
772 if (!(expr->fetch->val & where)) {
773 memprintf(err,
774 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
775 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
776 free(expr);
777 return -1;
778 }
779
780 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
781 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
782
783 if (strcmp(args[arg], "table") == 0) {
784 arg++;
785 if (!args[arg]) {
786 memprintf(err,
787 "'%s %s %s' : missing table name",
788 args[0], args[1], args[kw]);
789 free(expr);
790 return -1;
791 }
792 /* we copy the table name for now, it will be resolved later */
793 rule->arg.trk_ctr.table.n = strdup(args[arg]);
794 arg++;
795 }
796 rule->arg.trk_ctr.expr = expr;
Frédéric Lécaillea41d5312018-01-29 12:05:07 +0100797 rule->action = ACT_ACTION_TRK_SC0 + tsc_num;
Christopher Faulet78880fb2017-09-18 14:43:55 +0200798 rule->check_ptr = check_trk_action;
Willy Tarreau39713102016-11-25 15:49:32 +0100799 }
800 else if (strcmp(args[arg], "expect-proxy") == 0) {
801 if (strcmp(args[arg+1], "layer4") != 0) {
802 memprintf(err,
803 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
804 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
805 return -1;
806 }
807
808 if (!(where & SMP_VAL_FE_CON_ACC)) {
809 memprintf(err,
810 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
811 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
812 return -1;
813 }
814
815 arg += 2;
816 rule->action = ACT_TCP_EXPECT_PX;
817 }
818 else if (strcmp(args[arg], "expect-netscaler-cip") == 0) {
819 if (strcmp(args[arg+1], "layer4") != 0) {
820 memprintf(err,
821 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
822 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
823 return -1;
824 }
825
826 if (!(where & SMP_VAL_FE_CON_ACC)) {
827 memprintf(err,
828 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
829 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
830 return -1;
831 }
832
833 arg += 2;
834 rule->action = ACT_TCP_EXPECT_CIP;
835 }
836 else {
837 struct action_kw *kw;
838 if (where & SMP_VAL_FE_CON_ACC) {
839 /* L4 */
840 kw = tcp_req_conn_action(args[arg]);
841 rule->kw = kw;
842 rule->from = ACT_F_TCP_REQ_CON;
843 } else if (where & SMP_VAL_FE_SES_ACC) {
844 /* L5 */
845 kw = tcp_req_sess_action(args[arg]);
846 rule->kw = kw;
847 rule->from = ACT_F_TCP_REQ_SES;
848 } else {
849 /* L6 */
850 kw = tcp_req_cont_action(args[arg]);
851 rule->kw = kw;
852 rule->from = ACT_F_TCP_REQ_CNT;
853 }
854 if (kw) {
855 arg++;
856 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
857 return -1;
858 } else {
859 if (where & SMP_VAL_FE_CON_ACC)
860 action_build_list(&tcp_req_conn_keywords, &trash);
861 else if (where & SMP_VAL_FE_SES_ACC)
862 action_build_list(&tcp_req_sess_keywords, &trash);
863 else
864 action_build_list(&tcp_req_cont_keywords, &trash);
865 memprintf(err,
866 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d', %s "
867 "in %s '%s' (got '%s').\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200868 args[0], args[1], MAX_SESS_STKCTR-1,
869 trash.area, proxy_type_str(curpx),
Willy Tarreau39713102016-11-25 15:49:32 +0100870 curpx->id, args[arg]);
871 return -1;
872 }
873 }
874
875 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
Christopher Faulet1b421ea2017-09-22 14:38:56 +0200876 if ((rule->cond = build_acl_cond(file, line, &curpx->acl, curpx, (const char **)args+arg, err)) == NULL) {
Willy Tarreau39713102016-11-25 15:49:32 +0100877 memprintf(err,
878 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
879 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
880 return -1;
881 }
882 }
883 else if (*args[arg]) {
884 memprintf(err,
885 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
886 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
887 return -1;
888 }
889 return 0;
890}
891
892/* This function should be called to parse a line starting with the "tcp-response"
893 * keyword.
894 */
895static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
896 struct proxy *defpx, const char *file, int line,
897 char **err)
898{
899 const char *ptr = NULL;
900 unsigned int val;
901 int warn = 0;
902 int arg;
903 struct act_rule *rule;
904 unsigned int where;
905 const struct acl *acl;
906 const char *kw;
907
908 if (!*args[1]) {
909 memprintf(err, "missing argument for '%s' in %s '%s'",
910 args[0], proxy_type_str(curpx), curpx->id);
911 return -1;
912 }
913
914 if (strcmp(args[1], "inspect-delay") == 0) {
915 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
916 memprintf(err, "%s %s is only allowed in 'backend' sections",
917 args[0], args[1]);
918 return -1;
919 }
920
921 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
922 memprintf(err,
923 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
924 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200925
926 if (ptr == PARSE_TIME_OVER)
927 memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]);
928 else if (ptr == PARSE_TIME_UNDER)
929 memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]);
930 else if (ptr)
Willy Tarreau39713102016-11-25 15:49:32 +0100931 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
932 return -1;
933 }
934
935 if (curpx->tcp_rep.inspect_delay) {
936 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
937 args[0], args[1], proxy_type_str(curpx), curpx->id);
938 return 1;
939 }
940 curpx->tcp_rep.inspect_delay = val;
941 return 0;
942 }
943
944 rule = calloc(1, sizeof(*rule));
945 LIST_INIT(&rule->list);
946 arg = 1;
947 where = 0;
948
949 if (strcmp(args[1], "content") == 0) {
950 arg++;
951
952 if (curpx->cap & PR_CAP_FE)
953 where |= SMP_VAL_FE_RES_CNT;
954 if (curpx->cap & PR_CAP_BE)
955 where |= SMP_VAL_BE_RES_CNT;
956
957 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
958 goto error;
959
960 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
961 if (acl) {
962 if (acl->name && *acl->name)
963 memprintf(err,
964 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
965 acl->name, args[0], args[1], sample_ckp_names(where));
966 else
967 memprintf(err,
968 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
969 args[0], args[1],
970 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
971 sample_ckp_names(where));
972
973 warn++;
974 }
975 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
976 if (acl->name && *acl->name)
977 memprintf(err,
978 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
979 acl->name, kw, sample_ckp_names(where));
980 else
981 memprintf(err,
982 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
983 kw, sample_ckp_names(where));
984 warn++;
985 }
986
987 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
988 }
989 else {
990 memprintf(err,
991 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
992 args[0], proxy_type_str(curpx), curpx->id, args[1]);
993 goto error;
994 }
995
996 return warn;
997 error:
998 free(rule);
999 return -1;
1000}
1001
1002
1003/* This function should be called to parse a line starting with the "tcp-request"
1004 * keyword.
1005 */
1006static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
1007 struct proxy *defpx, const char *file, int line,
1008 char **err)
1009{
1010 const char *ptr = NULL;
1011 unsigned int val;
1012 int warn = 0;
1013 int arg;
1014 struct act_rule *rule;
1015 unsigned int where;
1016 const struct acl *acl;
1017 const char *kw;
1018
1019 if (!*args[1]) {
1020 if (curpx == defpx)
1021 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
1022 else
1023 memprintf(err, "missing argument for '%s' in %s '%s'",
1024 args[0], proxy_type_str(curpx), curpx->id);
1025 return -1;
1026 }
1027
1028 if (!strcmp(args[1], "inspect-delay")) {
1029 if (curpx == defpx) {
1030 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1031 args[0], args[1]);
1032 return -1;
1033 }
1034
1035 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1036 memprintf(err,
1037 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1038 args[0], args[1], proxy_type_str(curpx), curpx->id);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001039
1040 if (ptr == PARSE_TIME_OVER)
1041 memprintf(err, "%s (timer overflow in '%s', maximum value is 2147483647 ms or ~24.8 days)", *err, args[2]);
1042 else if (ptr == PARSE_TIME_UNDER)
1043 memprintf(err, "%s (timer underflow in '%s', minimum non-null value is 1 ms)", *err, args[2]);
1044 else if (ptr)
Willy Tarreau39713102016-11-25 15:49:32 +01001045 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
1046 return -1;
1047 }
1048
1049 if (curpx->tcp_req.inspect_delay) {
1050 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1051 args[0], args[1], proxy_type_str(curpx), curpx->id);
1052 return 1;
1053 }
1054 curpx->tcp_req.inspect_delay = val;
1055 return 0;
1056 }
1057
1058 rule = calloc(1, sizeof(*rule));
1059 LIST_INIT(&rule->list);
1060 arg = 1;
1061 where = 0;
1062
1063 if (strcmp(args[1], "content") == 0) {
1064 arg++;
1065
1066 if (curpx->cap & PR_CAP_FE)
1067 where |= SMP_VAL_FE_REQ_CNT;
1068 if (curpx->cap & PR_CAP_BE)
1069 where |= SMP_VAL_BE_REQ_CNT;
1070
1071 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1072 goto error;
1073
1074 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1075 if (acl) {
1076 if (acl->name && *acl->name)
1077 memprintf(err,
1078 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1079 acl->name, args[0], args[1], sample_ckp_names(where));
1080 else
1081 memprintf(err,
1082 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1083 args[0], args[1],
1084 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1085 sample_ckp_names(where));
1086
1087 warn++;
1088 }
1089 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1090 if (acl->name && *acl->name)
1091 memprintf(err,
1092 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1093 acl->name, kw, sample_ckp_names(where));
1094 else
1095 memprintf(err,
1096 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1097 kw, sample_ckp_names(where));
1098 warn++;
1099 }
1100
1101 /* the following function directly emits the warning */
1102 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
1103 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
1104 }
1105 else if (strcmp(args[1], "connection") == 0) {
1106 arg++;
1107
1108 if (!(curpx->cap & PR_CAP_FE)) {
1109 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1110 args[0], args[1], proxy_type_str(curpx), curpx->id);
1111 goto error;
1112 }
1113
1114 where |= SMP_VAL_FE_CON_ACC;
1115
1116 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1117 goto error;
1118
1119 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1120 if (acl) {
1121 if (acl->name && *acl->name)
1122 memprintf(err,
1123 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1124 acl->name, args[0], args[1], sample_ckp_names(where));
1125 else
1126 memprintf(err,
1127 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1128 args[0], args[1],
1129 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1130 sample_ckp_names(where));
1131
1132 warn++;
1133 }
1134 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1135 if (acl->name && *acl->name)
1136 memprintf(err,
1137 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1138 acl->name, kw, sample_ckp_names(where));
1139 else
1140 memprintf(err,
1141 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1142 kw, sample_ckp_names(where));
1143 warn++;
1144 }
1145
1146 /* the following function directly emits the warning */
1147 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
1148 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
1149 }
1150 else if (strcmp(args[1], "session") == 0) {
1151 arg++;
1152
1153 if (!(curpx->cap & PR_CAP_FE)) {
1154 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1155 args[0], args[1], proxy_type_str(curpx), curpx->id);
1156 goto error;
1157 }
1158
1159 where |= SMP_VAL_FE_SES_ACC;
1160
1161 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1162 goto error;
1163
1164 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1165 if (acl) {
1166 if (acl->name && *acl->name)
1167 memprintf(err,
1168 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1169 acl->name, args[0], args[1], sample_ckp_names(where));
1170 else
1171 memprintf(err,
1172 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1173 args[0], args[1],
1174 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1175 sample_ckp_names(where));
1176 warn++;
1177 }
1178 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1179 if (acl->name && *acl->name)
1180 memprintf(err,
1181 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1182 acl->name, kw, sample_ckp_names(where));
1183 else
1184 memprintf(err,
1185 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1186 kw, sample_ckp_names(where));
1187 warn++;
1188 }
1189
1190 /* the following function directly emits the warning */
1191 warnif_misplaced_tcp_sess(curpx, file, line, args[0]);
1192 LIST_ADDQ(&curpx->tcp_req.l5_rules, &rule->list);
1193 }
1194 else {
1195 if (curpx == defpx)
1196 memprintf(err,
1197 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1198 args[0], args[1]);
1199 else
1200 memprintf(err,
1201 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1202 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1203 goto error;
1204 }
1205
1206 return warn;
1207 error:
1208 free(rule);
1209 return -1;
1210}
1211
1212static struct cfg_kw_list cfg_kws = {ILH, {
1213 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1214 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
1215 { 0, NULL, NULL },
1216}};
1217
Willy Tarreau0108d902018-11-25 19:14:37 +01001218INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
Willy Tarreau39713102016-11-25 15:49:32 +01001219
1220/*
1221 * Local variables:
1222 * c-indent-level: 8
1223 * c-basic-offset: 8
1224 * End:
1225 */