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