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