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