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