blob: b87ab4b65b7cddb43a4699942dfba985ac044d81 [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
169 s->be->be_counters.denied_req++;
170 sess->fe->fe_counters.denied_req++;
Willy Tarreaua12dde02016-12-22 18:14:41 +0100171 if (sess->listener && sess->listener->counters)
Willy Tarreau39713102016-11-25 15:49:32 +0100172 sess->listener->counters->denied_req++;
173
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
187 if (stkctr_entry(&s->stkctr[tcp_trk_idx(rule->action)]))
188 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))) {
197 stream_track_stkctr(&s->stkctr[tcp_trk_idx(rule->action)], t, ts);
198 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
199 if (sess->fe != s->be)
200 stkctr_set_flags(&s->stkctr[tcp_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
201 }
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)
217 cap[h->index] = pool_alloc2(h->pool);
218
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
347 s->be->be_counters.denied_resp++;
348 sess->fe->fe_counters.denied_resp++;
Willy Tarreaua12dde02016-12-22 18:14:41 +0100349 if (sess->listener && sess->listener->counters)
Willy Tarreau39713102016-11-25 15:49:32 +0100350 sess->listener->counters->denied_resp++;
351
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) {
430 sess->fe->fe_counters.denied_conn++;
Willy Tarreaua12dde02016-12-22 18:14:41 +0100431 if (sess->listener && sess->listener->counters)
Willy Tarreau39713102016-11-25 15:49:32 +0100432 sess->listener->counters->denied_conn++;
433
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
443 if (stkctr_entry(&sess->stkctr[tcp_trk_idx(rule->action)]))
444 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)))
450 stream_track_stkctr(&sess->stkctr[tcp_trk_idx(rule->action)], t, ts);
451 }
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) {
517 sess->fe->fe_counters.denied_sess++;
Willy Tarreaua12dde02016-12-22 18:14:41 +0100518 if (sess->listener && sess->listener->counters)
Willy Tarreau39713102016-11-25 15:49:32 +0100519 sess->listener->counters->denied_sess++;
520
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
530 if (stkctr_entry(&sess->stkctr[tcp_trk_idx(rule->action)]))
531 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)))
537 stream_track_stkctr(&sess->stkctr[tcp_trk_idx(rule->action)], t, ts);
538 }
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) {
609 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
610 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 }
733 else if (strncmp(args[arg], "track-sc", 8) == 0 &&
734 args[arg][9] == '\0' && args[arg][8] >= '0' &&
735 args[arg][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
736 struct sample_expr *expr;
737 int kw = arg;
738
739 arg++;
740
741 curpx->conf.args.ctx = ARGC_TRK;
742 expr = sample_parse_expr(args, &arg, file, line, err, &curpx->conf.args);
743 if (!expr) {
744 memprintf(err,
745 "'%s %s %s' : %s",
746 args[0], args[1], args[kw], *err);
747 return -1;
748 }
749
750 if (!(expr->fetch->val & where)) {
751 memprintf(err,
752 "'%s %s %s' : fetch method '%s' extracts information from '%s', none of which is available here",
753 args[0], args[1], args[kw], args[arg-1], sample_src_names(expr->fetch->use));
754 free(expr);
755 return -1;
756 }
757
758 /* check if we need to allocate an hdr_idx struct for HTTP parsing */
759 curpx->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
760
761 if (strcmp(args[arg], "table") == 0) {
762 arg++;
763 if (!args[arg]) {
764 memprintf(err,
765 "'%s %s %s' : missing table name",
766 args[0], args[1], args[kw]);
767 free(expr);
768 return -1;
769 }
770 /* we copy the table name for now, it will be resolved later */
771 rule->arg.trk_ctr.table.n = strdup(args[arg]);
772 arg++;
773 }
774 rule->arg.trk_ctr.expr = expr;
775 rule->action = ACT_ACTION_TRK_SC0 + args[kw][8] - '0';
776 }
777 else if (strcmp(args[arg], "expect-proxy") == 0) {
778 if (strcmp(args[arg+1], "layer4") != 0) {
779 memprintf(err,
780 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
781 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
782 return -1;
783 }
784
785 if (!(where & SMP_VAL_FE_CON_ACC)) {
786 memprintf(err,
787 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
788 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
789 return -1;
790 }
791
792 arg += 2;
793 rule->action = ACT_TCP_EXPECT_PX;
794 }
795 else if (strcmp(args[arg], "expect-netscaler-cip") == 0) {
796 if (strcmp(args[arg+1], "layer4") != 0) {
797 memprintf(err,
798 "'%s %s %s' only supports 'layer4' in %s '%s' (got '%s')",
799 args[0], args[1], args[arg], proxy_type_str(curpx), curpx->id, args[arg+1]);
800 return -1;
801 }
802
803 if (!(where & SMP_VAL_FE_CON_ACC)) {
804 memprintf(err,
805 "'%s %s' is not allowed in '%s %s' rules in %s '%s'",
806 args[arg], args[arg+1], args[0], args[1], proxy_type_str(curpx), curpx->id);
807 return -1;
808 }
809
810 arg += 2;
811 rule->action = ACT_TCP_EXPECT_CIP;
812 }
813 else {
814 struct action_kw *kw;
815 if (where & SMP_VAL_FE_CON_ACC) {
816 /* L4 */
817 kw = tcp_req_conn_action(args[arg]);
818 rule->kw = kw;
819 rule->from = ACT_F_TCP_REQ_CON;
820 } else if (where & SMP_VAL_FE_SES_ACC) {
821 /* L5 */
822 kw = tcp_req_sess_action(args[arg]);
823 rule->kw = kw;
824 rule->from = ACT_F_TCP_REQ_SES;
825 } else {
826 /* L6 */
827 kw = tcp_req_cont_action(args[arg]);
828 rule->kw = kw;
829 rule->from = ACT_F_TCP_REQ_CNT;
830 }
831 if (kw) {
832 arg++;
833 if (kw->parse((const char **)args, &arg, curpx, rule, err) == ACT_RET_PRS_ERR)
834 return -1;
835 } else {
836 if (where & SMP_VAL_FE_CON_ACC)
837 action_build_list(&tcp_req_conn_keywords, &trash);
838 else if (where & SMP_VAL_FE_SES_ACC)
839 action_build_list(&tcp_req_sess_keywords, &trash);
840 else
841 action_build_list(&tcp_req_cont_keywords, &trash);
842 memprintf(err,
843 "'%s %s' expects 'accept', 'reject', 'track-sc0' ... 'track-sc%d', %s "
844 "in %s '%s' (got '%s').\n",
845 args[0], args[1], MAX_SESS_STKCTR-1, trash.str, proxy_type_str(curpx),
846 curpx->id, args[arg]);
847 return -1;
848 }
849 }
850
851 if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) {
852 if ((rule->cond = build_acl_cond(file, line, curpx, (const char **)args+arg, err)) == NULL) {
853 memprintf(err,
854 "'%s %s %s' : error detected in %s '%s' while parsing '%s' condition : %s",
855 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg], *err);
856 return -1;
857 }
858 }
859 else if (*args[arg]) {
860 memprintf(err,
861 "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (got '%s')",
862 args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]);
863 return -1;
864 }
865 return 0;
866}
867
868/* This function should be called to parse a line starting with the "tcp-response"
869 * keyword.
870 */
871static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx,
872 struct proxy *defpx, const char *file, int line,
873 char **err)
874{
875 const char *ptr = NULL;
876 unsigned int val;
877 int warn = 0;
878 int arg;
879 struct act_rule *rule;
880 unsigned int where;
881 const struct acl *acl;
882 const char *kw;
883
884 if (!*args[1]) {
885 memprintf(err, "missing argument for '%s' in %s '%s'",
886 args[0], proxy_type_str(curpx), curpx->id);
887 return -1;
888 }
889
890 if (strcmp(args[1], "inspect-delay") == 0) {
891 if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) {
892 memprintf(err, "%s %s is only allowed in 'backend' sections",
893 args[0], args[1]);
894 return -1;
895 }
896
897 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
898 memprintf(err,
899 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
900 args[0], args[1], proxy_type_str(curpx), curpx->id);
901 if (ptr)
902 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
903 return -1;
904 }
905
906 if (curpx->tcp_rep.inspect_delay) {
907 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
908 args[0], args[1], proxy_type_str(curpx), curpx->id);
909 return 1;
910 }
911 curpx->tcp_rep.inspect_delay = val;
912 return 0;
913 }
914
915 rule = calloc(1, sizeof(*rule));
916 LIST_INIT(&rule->list);
917 arg = 1;
918 where = 0;
919
920 if (strcmp(args[1], "content") == 0) {
921 arg++;
922
923 if (curpx->cap & PR_CAP_FE)
924 where |= SMP_VAL_FE_RES_CNT;
925 if (curpx->cap & PR_CAP_BE)
926 where |= SMP_VAL_BE_RES_CNT;
927
928 if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
929 goto error;
930
931 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
932 if (acl) {
933 if (acl->name && *acl->name)
934 memprintf(err,
935 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
936 acl->name, args[0], args[1], sample_ckp_names(where));
937 else
938 memprintf(err,
939 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
940 args[0], args[1],
941 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
942 sample_ckp_names(where));
943
944 warn++;
945 }
946 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
947 if (acl->name && *acl->name)
948 memprintf(err,
949 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
950 acl->name, kw, sample_ckp_names(where));
951 else
952 memprintf(err,
953 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
954 kw, sample_ckp_names(where));
955 warn++;
956 }
957
958 LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list);
959 }
960 else {
961 memprintf(err,
962 "'%s' expects 'inspect-delay' or 'content' in %s '%s' (got '%s')",
963 args[0], proxy_type_str(curpx), curpx->id, args[1]);
964 goto error;
965 }
966
967 return warn;
968 error:
969 free(rule);
970 return -1;
971}
972
973
974/* This function should be called to parse a line starting with the "tcp-request"
975 * keyword.
976 */
977static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx,
978 struct proxy *defpx, const char *file, int line,
979 char **err)
980{
981 const char *ptr = NULL;
982 unsigned int val;
983 int warn = 0;
984 int arg;
985 struct act_rule *rule;
986 unsigned int where;
987 const struct acl *acl;
988 const char *kw;
989
990 if (!*args[1]) {
991 if (curpx == defpx)
992 memprintf(err, "missing argument for '%s' in defaults section", args[0]);
993 else
994 memprintf(err, "missing argument for '%s' in %s '%s'",
995 args[0], proxy_type_str(curpx), curpx->id);
996 return -1;
997 }
998
999 if (!strcmp(args[1], "inspect-delay")) {
1000 if (curpx == defpx) {
1001 memprintf(err, "%s %s is not allowed in 'defaults' sections",
1002 args[0], args[1]);
1003 return -1;
1004 }
1005
1006 if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) {
1007 memprintf(err,
1008 "'%s %s' expects a positive delay in milliseconds, in %s '%s'",
1009 args[0], args[1], proxy_type_str(curpx), curpx->id);
1010 if (ptr)
1011 memprintf(err, "%s (unexpected character '%c')", *err, *ptr);
1012 return -1;
1013 }
1014
1015 if (curpx->tcp_req.inspect_delay) {
1016 memprintf(err, "ignoring %s %s (was already defined) in %s '%s'",
1017 args[0], args[1], proxy_type_str(curpx), curpx->id);
1018 return 1;
1019 }
1020 curpx->tcp_req.inspect_delay = val;
1021 return 0;
1022 }
1023
1024 rule = calloc(1, sizeof(*rule));
1025 LIST_INIT(&rule->list);
1026 arg = 1;
1027 where = 0;
1028
1029 if (strcmp(args[1], "content") == 0) {
1030 arg++;
1031
1032 if (curpx->cap & PR_CAP_FE)
1033 where |= SMP_VAL_FE_REQ_CNT;
1034 if (curpx->cap & PR_CAP_BE)
1035 where |= SMP_VAL_BE_REQ_CNT;
1036
1037 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1038 goto error;
1039
1040 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1041 if (acl) {
1042 if (acl->name && *acl->name)
1043 memprintf(err,
1044 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1045 acl->name, args[0], args[1], sample_ckp_names(where));
1046 else
1047 memprintf(err,
1048 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1049 args[0], args[1],
1050 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1051 sample_ckp_names(where));
1052
1053 warn++;
1054 }
1055 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1056 if (acl->name && *acl->name)
1057 memprintf(err,
1058 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1059 acl->name, kw, sample_ckp_names(where));
1060 else
1061 memprintf(err,
1062 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1063 kw, sample_ckp_names(where));
1064 warn++;
1065 }
1066
1067 /* the following function directly emits the warning */
1068 warnif_misplaced_tcp_cont(curpx, file, line, args[0]);
1069 LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list);
1070 }
1071 else if (strcmp(args[1], "connection") == 0) {
1072 arg++;
1073
1074 if (!(curpx->cap & PR_CAP_FE)) {
1075 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1076 args[0], args[1], proxy_type_str(curpx), curpx->id);
1077 goto error;
1078 }
1079
1080 where |= SMP_VAL_FE_CON_ACC;
1081
1082 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1083 goto error;
1084
1085 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1086 if (acl) {
1087 if (acl->name && *acl->name)
1088 memprintf(err,
1089 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1090 acl->name, args[0], args[1], sample_ckp_names(where));
1091 else
1092 memprintf(err,
1093 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1094 args[0], args[1],
1095 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1096 sample_ckp_names(where));
1097
1098 warn++;
1099 }
1100 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1101 if (acl->name && *acl->name)
1102 memprintf(err,
1103 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1104 acl->name, kw, sample_ckp_names(where));
1105 else
1106 memprintf(err,
1107 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1108 kw, sample_ckp_names(where));
1109 warn++;
1110 }
1111
1112 /* the following function directly emits the warning */
1113 warnif_misplaced_tcp_conn(curpx, file, line, args[0]);
1114 LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list);
1115 }
1116 else if (strcmp(args[1], "session") == 0) {
1117 arg++;
1118
1119 if (!(curpx->cap & PR_CAP_FE)) {
1120 memprintf(err, "%s %s is not allowed because %s %s is not a frontend",
1121 args[0], args[1], proxy_type_str(curpx), curpx->id);
1122 goto error;
1123 }
1124
1125 where |= SMP_VAL_FE_SES_ACC;
1126
1127 if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, where, file, line) < 0)
1128 goto error;
1129
1130 acl = rule->cond ? acl_cond_conflicts(rule->cond, where) : NULL;
1131 if (acl) {
1132 if (acl->name && *acl->name)
1133 memprintf(err,
1134 "acl '%s' will never match in '%s %s' because it only involves keywords that are incompatible with '%s'",
1135 acl->name, args[0], args[1], sample_ckp_names(where));
1136 else
1137 memprintf(err,
1138 "anonymous acl will never match in '%s %s' because it uses keyword '%s' which is incompatible with '%s'",
1139 args[0], args[1],
1140 LIST_ELEM(acl->expr.n, struct acl_expr *, list)->kw,
1141 sample_ckp_names(where));
1142 warn++;
1143 }
1144 else if (rule->cond && acl_cond_kw_conflicts(rule->cond, where, &acl, &kw)) {
1145 if (acl->name && *acl->name)
1146 memprintf(err,
1147 "acl '%s' involves keyword '%s' which is incompatible with '%s'",
1148 acl->name, kw, sample_ckp_names(where));
1149 else
1150 memprintf(err,
1151 "anonymous acl involves keyword '%s' which is incompatible with '%s'",
1152 kw, sample_ckp_names(where));
1153 warn++;
1154 }
1155
1156 /* the following function directly emits the warning */
1157 warnif_misplaced_tcp_sess(curpx, file, line, args[0]);
1158 LIST_ADDQ(&curpx->tcp_req.l5_rules, &rule->list);
1159 }
1160 else {
1161 if (curpx == defpx)
1162 memprintf(err,
1163 "'%s' expects 'inspect-delay', 'connection', or 'content' in defaults section (got '%s')",
1164 args[0], args[1]);
1165 else
1166 memprintf(err,
1167 "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (got '%s')",
1168 args[0], proxy_type_str(curpx), curpx->id, args[1]);
1169 goto error;
1170 }
1171
1172 return warn;
1173 error:
1174 free(rule);
1175 return -1;
1176}
1177
1178static struct cfg_kw_list cfg_kws = {ILH, {
1179 { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req },
1180 { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep },
1181 { 0, NULL, NULL },
1182}};
1183
1184
1185__attribute__((constructor))
1186static void __tcp_protocol_init(void)
1187{
1188 cfg_register_keywords(&cfg_kws);
1189}
1190
1191/*
1192 * Local variables:
1193 * c-indent-level: 8
1194 * c-basic-offset: 8
1195 * End:
1196 */