Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp) |
| 3 | * |
| 4 | * Copyright 2000-2013 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 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <time.h> |
| 20 | |
| 21 | #include <sys/param.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/types.h> |
| 24 | |
| 25 | #include <netinet/tcp.h> |
| 26 | #include <netinet/in.h> |
| 27 | |
| 28 | #include <haproxy/action-t.h> |
| 29 | #include <haproxy/api.h> |
| 30 | #include <haproxy/arg.h> |
| 31 | #include <haproxy/channel.h> |
| 32 | #include <haproxy/connection.h> |
| 33 | #include <haproxy/global.h> |
| 34 | #include <haproxy/http_rules.h> |
| 35 | #include <haproxy/proto_tcp.h> |
| 36 | #include <haproxy/proxy-t.h> |
| 37 | #include <haproxy/sample.h> |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 38 | #include <haproxy/session.h> |
| 39 | #include <haproxy/stream_interface.h> |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 40 | #include <haproxy/tcp_rules.h> |
| 41 | #include <haproxy/tools.h> |
| 42 | |
| 43 | /* |
| 44 | * Execute the "set-src" action. May be called from {tcp,http}request. |
| 45 | * It only changes the address and tries to preserve the original port. If the |
| 46 | * previous family was neither AF_INET nor AF_INET6, the port is set to zero. |
| 47 | */ |
| 48 | static enum act_return tcp_action_req_set_src(struct act_rule *rule, struct proxy *px, |
| 49 | struct session *sess, struct stream *s, int flags) |
| 50 | { |
| 51 | struct connection *cli_conn; |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 52 | struct sockaddr_storage *src; |
| 53 | struct sample *smp; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 54 | |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 55 | switch (rule->from) { |
| 56 | case ACT_F_TCP_REQ_CON: |
| 57 | cli_conn = objt_conn(sess->origin); |
| 58 | if (!cli_conn || !conn_get_src(cli_conn)) |
| 59 | goto end; |
| 60 | src = cli_conn->src; |
| 61 | break; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 62 | |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 63 | case ACT_F_TCP_REQ_SES: |
| 64 | if (!sess_get_src(sess)) |
| 65 | goto end; |
| 66 | src = sess->src; |
| 67 | break; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 68 | |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 69 | case ACT_F_TCP_REQ_CNT: |
| 70 | case ACT_F_HTTP_REQ: |
| 71 | if (!si_get_src(&s->si[0])) |
| 72 | goto end; |
| 73 | src = s->si[0].src; |
| 74 | break; |
| 75 | |
| 76 | default: |
| 77 | goto end; |
| 78 | } |
| 79 | |
| 80 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR); |
| 81 | if (smp) { |
| 82 | int port = get_net_port(src); |
| 83 | |
| 84 | if (smp->data.type == SMP_T_IPV4) { |
| 85 | ((struct sockaddr_in *)src)->sin_family = AF_INET; |
| 86 | ((struct sockaddr_in *)src)->sin_addr.s_addr = smp->data.u.ipv4.s_addr; |
| 87 | ((struct sockaddr_in *)src)->sin_port = port; |
| 88 | } else if (smp->data.type == SMP_T_IPV6) { |
| 89 | ((struct sockaddr_in6 *)src)->sin6_family = AF_INET6; |
| 90 | memcpy(&((struct sockaddr_in6 *)src)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr)); |
| 91 | ((struct sockaddr_in6 *)src)->sin6_port = port; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 92 | } |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 93 | } |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 94 | |
| 95 | end: |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 96 | return ACT_RET_CONT; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Execute the "set-dst" action. May be called from {tcp,http}request. |
| 101 | * It only changes the address and tries to preserve the original port. If the |
| 102 | * previous family was neither AF_INET nor AF_INET6, the port is set to zero. |
| 103 | */ |
| 104 | static enum act_return tcp_action_req_set_dst(struct act_rule *rule, struct proxy *px, |
| 105 | struct session *sess, struct stream *s, int flags) |
| 106 | { |
| 107 | struct connection *cli_conn; |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 108 | struct sockaddr_storage *dst; |
| 109 | struct sample *smp; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 110 | |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 111 | switch (rule->from) { |
| 112 | case ACT_F_TCP_REQ_CON: |
| 113 | cli_conn = objt_conn(sess->origin); |
| 114 | if (!cli_conn || !conn_get_dst(cli_conn)) |
| 115 | goto end; |
| 116 | dst = cli_conn->dst; |
| 117 | break; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 118 | |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 119 | case ACT_F_TCP_REQ_SES: |
| 120 | if (!sess_get_dst(sess)) |
| 121 | goto end; |
| 122 | dst = sess->dst; |
| 123 | break; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 124 | |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 125 | case ACT_F_TCP_REQ_CNT: |
| 126 | case ACT_F_HTTP_REQ: |
| 127 | if (!si_get_dst(&s->si[0])) |
| 128 | goto end; |
| 129 | dst = s->si[0].dst; |
| 130 | break; |
| 131 | |
| 132 | default: |
| 133 | goto end; |
| 134 | } |
| 135 | |
| 136 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_ADDR); |
| 137 | if (smp) { |
| 138 | int port = get_net_port(dst); |
| 139 | |
| 140 | if (smp->data.type == SMP_T_IPV4) { |
| 141 | ((struct sockaddr_in *)dst)->sin_family = AF_INET; |
| 142 | ((struct sockaddr_in *)dst)->sin_addr.s_addr = smp->data.u.ipv4.s_addr; |
| 143 | ((struct sockaddr_in *)dst)->sin_port = port; |
| 144 | } else if (smp->data.type == SMP_T_IPV6) { |
| 145 | ((struct sockaddr_in6 *)dst)->sin6_family = AF_INET6; |
| 146 | memcpy(&((struct sockaddr_in6 *)dst)->sin6_addr, &smp->data.u.ipv6, sizeof(struct in6_addr)); |
| 147 | ((struct sockaddr_in6 *)dst)->sin6_port = port; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 148 | } |
| 149 | } |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 150 | |
| 151 | end: |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 152 | return ACT_RET_CONT; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Execute the "set-src-port" action. May be called from {tcp,http}request. |
| 157 | * We must test the sin_family before setting the port. If the address family |
| 158 | * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0" |
| 159 | * and the port is assigned. |
| 160 | */ |
| 161 | static enum act_return tcp_action_req_set_src_port(struct act_rule *rule, struct proxy *px, |
| 162 | struct session *sess, struct stream *s, int flags) |
| 163 | { |
| 164 | struct connection *cli_conn; |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 165 | struct sockaddr_storage *src; |
| 166 | struct sample *smp; |
| 167 | |
| 168 | switch (rule->from) { |
| 169 | case ACT_F_TCP_REQ_CON: |
| 170 | cli_conn = objt_conn(sess->origin); |
| 171 | if (!cli_conn || !conn_get_src(cli_conn)) |
| 172 | goto end; |
| 173 | src = cli_conn->src; |
| 174 | break; |
| 175 | |
| 176 | case ACT_F_TCP_REQ_SES: |
| 177 | if (!sess_get_src(sess)) |
| 178 | goto end; |
| 179 | src = sess->src; |
| 180 | break; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 181 | |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 182 | case ACT_F_TCP_REQ_CNT: |
| 183 | case ACT_F_HTTP_REQ: |
| 184 | if (!si_get_src(&s->si[0])) |
| 185 | goto end; |
| 186 | src = s->si[0].src; |
| 187 | break; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 188 | |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 189 | default: |
| 190 | goto end; |
| 191 | } |
| 192 | |
| 193 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT); |
| 194 | if (smp) { |
| 195 | if (src->ss_family == AF_INET6) { |
| 196 | ((struct sockaddr_in6 *)src)->sin6_port = htons(smp->data.u.sint); |
| 197 | } else { |
| 198 | if (src->ss_family != AF_INET) { |
| 199 | src->ss_family = AF_INET; |
| 200 | ((struct sockaddr_in *)src)->sin_addr.s_addr = 0; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 201 | } |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 202 | ((struct sockaddr_in *)src)->sin_port = htons(smp->data.u.sint); |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 203 | } |
| 204 | } |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 205 | |
| 206 | end: |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 207 | return ACT_RET_CONT; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Execute the "set-dst-port" action. May be called from {tcp,http}request. |
| 212 | * We must test the sin_family before setting the port. If the address family |
| 213 | * is neither AF_INET nor AF_INET6, the address is forced to AF_INET "0.0.0.0" |
| 214 | * and the port is assigned. |
| 215 | */ |
| 216 | static enum act_return tcp_action_req_set_dst_port(struct act_rule *rule, struct proxy *px, |
| 217 | struct session *sess, struct stream *s, int flags) |
| 218 | { |
| 219 | struct connection *cli_conn; |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 220 | struct sockaddr_storage *dst; |
| 221 | struct sample *smp; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 222 | |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 223 | switch (rule->from) { |
| 224 | case ACT_F_TCP_REQ_CON: |
| 225 | cli_conn = objt_conn(sess->origin); |
| 226 | if (!cli_conn || !conn_get_dst(cli_conn)) |
| 227 | goto end; |
| 228 | dst = cli_conn->dst; |
| 229 | break; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 230 | |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 231 | case ACT_F_TCP_REQ_SES: |
| 232 | if (!sess_get_dst(sess)) |
| 233 | goto end; |
| 234 | dst = sess->dst; |
| 235 | break; |
| 236 | |
| 237 | case ACT_F_TCP_REQ_CNT: |
| 238 | case ACT_F_HTTP_REQ: |
| 239 | if (!si_get_dst(&s->si[0])) |
| 240 | goto end; |
| 241 | dst = s->si[0].dst; |
| 242 | break; |
| 243 | |
| 244 | default: |
| 245 | goto end; |
| 246 | } |
| 247 | |
| 248 | smp = sample_fetch_as_type(px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->arg.expr, SMP_T_SINT); |
| 249 | if (smp) { |
| 250 | if (dst->ss_family == AF_INET6) { |
| 251 | ((struct sockaddr_in6 *)dst)->sin6_port = htons(smp->data.u.sint); |
| 252 | } else { |
| 253 | if (dst->ss_family != AF_INET) { |
| 254 | dst->ss_family = AF_INET; |
| 255 | ((struct sockaddr_in *)dst)->sin_addr.s_addr = 0; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 256 | } |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 257 | ((struct sockaddr_in *)dst)->sin_port = htons(smp->data.u.sint); |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 258 | } |
| 259 | } |
Christopher Faulet | d69377e | 2021-10-25 08:26:34 +0200 | [diff] [blame] | 260 | |
| 261 | end: |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 262 | return ACT_RET_CONT; |
| 263 | } |
| 264 | |
| 265 | /* Executes the "silent-drop" action. May be called from {tcp,http}{request,response} */ |
| 266 | static enum act_return tcp_exec_action_silent_drop(struct act_rule *rule, struct proxy *px, |
| 267 | struct session *sess, struct stream *strm, int flags) |
| 268 | { |
| 269 | struct connection *conn = objt_conn(sess->origin); |
| 270 | |
| 271 | if (!conn) |
| 272 | goto out; |
| 273 | |
| 274 | if (!conn_ctrl_ready(conn)) |
| 275 | goto out; |
| 276 | |
| 277 | #ifdef TCP_QUICKACK |
| 278 | /* drain is needed only to send the quick ACK */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 279 | conn_ctrl_drain(conn); |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 280 | |
| 281 | /* re-enable quickack if it was disabled to ack all data and avoid |
| 282 | * retransmits from the client that might trigger a real reset. |
| 283 | */ |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 284 | setsockopt(conn->handle.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 285 | #endif |
| 286 | /* lingering must absolutely be disabled so that we don't send a |
| 287 | * shutdown(), this is critical to the TCP_REPAIR trick. When no stream |
| 288 | * is present, returning with ERR will cause lingering to be disabled. |
| 289 | */ |
| 290 | if (strm) |
| 291 | strm->si[0].flags |= SI_FL_NOLINGER; |
| 292 | |
| 293 | /* We're on the client-facing side, we must force to disable lingering to |
| 294 | * ensure we will use an RST exclusively and kill any pending data. |
| 295 | */ |
Willy Tarreau | b41a6e9 | 2021-04-06 17:49:19 +0200 | [diff] [blame] | 296 | HA_ATOMIC_OR(&fdtab[conn->handle.fd].state, FD_LINGER_RISK); |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 297 | |
| 298 | #ifdef TCP_REPAIR |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 299 | if (setsockopt(conn->handle.fd, IPPROTO_TCP, TCP_REPAIR, &one, sizeof(one)) == 0) { |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 300 | /* socket will be quiet now */ |
| 301 | goto out; |
| 302 | } |
| 303 | #endif |
| 304 | /* either TCP_REPAIR is not defined or it failed (eg: permissions). |
| 305 | * Let's fall back on the TTL trick, though it only works for routed |
| 306 | * network and has no effect on local net. |
| 307 | */ |
| 308 | #ifdef IP_TTL |
Willy Tarreau | ab79ee8 | 2021-03-30 17:23:50 +0200 | [diff] [blame] | 309 | if (conn->src && conn->src->ss_family == AF_INET) |
Willy Tarreau | 4bfc663 | 2021-03-31 08:45:47 +0200 | [diff] [blame] | 310 | setsockopt(conn->handle.fd, IPPROTO_IP, IP_TTL, &one, sizeof(one)); |
Willy Tarreau | ab79ee8 | 2021-03-30 17:23:50 +0200 | [diff] [blame] | 311 | #endif |
| 312 | #ifdef IPV6_UNICAST_HOPS |
Willy Tarreau | da23195 | 2021-03-31 08:29:27 +0200 | [diff] [blame] | 313 | if (conn->src && conn->src->ss_family == AF_INET6) |
| 314 | setsockopt(conn->handle.fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &one, sizeof(one)); |
| 315 | #endif |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 316 | out: |
| 317 | /* kill the stream if any */ |
| 318 | if (strm) { |
| 319 | channel_abort(&strm->req); |
| 320 | channel_abort(&strm->res); |
| 321 | strm->req.analysers &= AN_REQ_FLT_END; |
| 322 | strm->res.analysers &= AN_RES_FLT_END; |
| 323 | if (strm->flags & SF_BE_ASSIGNED) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 324 | _HA_ATOMIC_INC(&strm->be->be_counters.denied_req); |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 325 | if (!(strm->flags & SF_ERR_MASK)) |
| 326 | strm->flags |= SF_ERR_PRXCOND; |
| 327 | if (!(strm->flags & SF_FINST_MASK)) |
| 328 | strm->flags |= SF_FINST_R; |
| 329 | } |
| 330 | |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 331 | _HA_ATOMIC_INC(&sess->fe->fe_counters.denied_req); |
William Lallemand | 36119de | 2021-03-08 15:26:48 +0100 | [diff] [blame] | 332 | if (sess->listener && sess->listener->counters) |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 333 | _HA_ATOMIC_INC(&sess->listener->counters->denied_req); |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 334 | |
| 335 | return ACT_RET_ABRT; |
| 336 | } |
| 337 | |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 338 | |
David Carlier | bae4cb2 | 2021-07-03 10:15:15 +0100 | [diff] [blame] | 339 | #if defined(SO_MARK) || defined(SO_USER_COOKIE) || defined(SO_RTABLE) |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 340 | static enum act_return tcp_action_set_mark(struct act_rule *rule, struct proxy *px, |
| 341 | struct session *sess, struct stream *s, int flags) |
| 342 | { |
| 343 | conn_set_mark(objt_conn(sess->origin), (uintptr_t)rule->arg.act.p[0]); |
| 344 | return ACT_RET_CONT; |
| 345 | } |
Willy Tarreau | 5bbfff1 | 2021-06-28 07:12:22 +0200 | [diff] [blame] | 346 | #endif |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 347 | |
Willy Tarreau | 5bbfff1 | 2021-06-28 07:12:22 +0200 | [diff] [blame] | 348 | #ifdef IP_TOS |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 349 | static enum act_return tcp_action_set_tos(struct act_rule *rule, struct proxy *px, |
| 350 | struct session *sess, struct stream *s, int flags) |
| 351 | { |
| 352 | conn_set_tos(objt_conn(sess->origin), (uintptr_t)rule->arg.act.p[0]); |
| 353 | return ACT_RET_CONT; |
| 354 | } |
Willy Tarreau | 5bbfff1 | 2021-06-28 07:12:22 +0200 | [diff] [blame] | 355 | #endif |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 356 | |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 357 | /* parse "set-{src,dst}[-port]" action */ |
| 358 | static enum act_parse_ret tcp_parse_set_src_dst(const char **args, int *orig_arg, struct proxy *px, |
| 359 | struct act_rule *rule, char **err) |
| 360 | { |
| 361 | int cur_arg; |
| 362 | struct sample_expr *expr; |
| 363 | unsigned int where; |
| 364 | |
| 365 | cur_arg = *orig_arg; |
| 366 | expr = sample_parse_expr((char **)args, &cur_arg, px->conf.args.file, px->conf.args.line, err, &px->conf.args, NULL); |
| 367 | if (!expr) |
| 368 | return ACT_RET_PRS_ERR; |
| 369 | |
| 370 | where = 0; |
| 371 | if (px->cap & PR_CAP_FE) |
| 372 | where |= SMP_VAL_FE_HRQ_HDR; |
| 373 | if (px->cap & PR_CAP_BE) |
| 374 | where |= SMP_VAL_BE_HRQ_HDR; |
| 375 | |
| 376 | if (!(expr->fetch->val & where)) { |
| 377 | memprintf(err, |
| 378 | "fetch method '%s' extracts information from '%s', none of which is available here", |
| 379 | args[cur_arg-1], sample_src_names(expr->fetch->use)); |
| 380 | free(expr); |
| 381 | return ACT_RET_PRS_ERR; |
| 382 | } |
| 383 | rule->arg.expr = expr; |
| 384 | rule->action = ACT_CUSTOM; |
| 385 | |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 386 | if (strcmp(args[*orig_arg - 1], "set-src") == 0) { |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 387 | rule->action_ptr = tcp_action_req_set_src; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 388 | } else if (strcmp(args[*orig_arg - 1], "set-src-port") == 0) { |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 389 | rule->action_ptr = tcp_action_req_set_src_port; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 390 | } else if (strcmp(args[*orig_arg - 1], "set-dst") == 0) { |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 391 | rule->action_ptr = tcp_action_req_set_dst; |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 392 | } else if (strcmp(args[*orig_arg - 1], "set-dst-port") == 0) { |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 393 | rule->action_ptr = tcp_action_req_set_dst_port; |
| 394 | } else { |
| 395 | return ACT_RET_PRS_ERR; |
| 396 | } |
| 397 | |
| 398 | (*orig_arg)++; |
| 399 | |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 400 | return ACT_RET_PRS_OK; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /* Parse a "set-mark" action. It takes the MARK value as argument. It returns |
| 405 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 406 | */ |
| 407 | static enum act_parse_ret tcp_parse_set_mark(const char **args, int *cur_arg, struct proxy *px, |
| 408 | struct act_rule *rule, char **err) |
| 409 | { |
David Carlier | bae4cb2 | 2021-07-03 10:15:15 +0100 | [diff] [blame] | 410 | #if defined(SO_MARK) || defined(SO_USER_COOKIE) || defined(SO_RTABLE) |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 411 | char *endp; |
| 412 | unsigned int mark; |
| 413 | |
| 414 | if (!*args[*cur_arg]) { |
| 415 | memprintf(err, "expects exactly 1 argument (integer/hex value)"); |
| 416 | return ACT_RET_PRS_ERR; |
| 417 | } |
| 418 | mark = strtoul(args[*cur_arg], &endp, 0); |
| 419 | if (endp && *endp != '\0') { |
| 420 | memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp); |
| 421 | return ACT_RET_PRS_ERR; |
| 422 | } |
| 423 | |
| 424 | (*cur_arg)++; |
| 425 | |
| 426 | /* Register processing function. */ |
| 427 | rule->action_ptr = tcp_action_set_mark; |
| 428 | rule->action = ACT_CUSTOM; |
| 429 | rule->arg.act.p[0] = (void *)(uintptr_t)mark; |
| 430 | global.last_checks |= LSTCHK_NETADM; |
| 431 | return ACT_RET_PRS_OK; |
| 432 | #else |
David Carlier | bae4cb2 | 2021-07-03 10:15:15 +0100 | [diff] [blame] | 433 | memprintf(err, "not supported on this platform (SO_MARK|SO_USER_COOKIE|SO_RTABLE undefined)"); |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 434 | return ACT_RET_PRS_ERR; |
| 435 | #endif |
| 436 | } |
| 437 | |
| 438 | |
| 439 | /* Parse a "set-tos" action. It takes the TOS value as argument. It returns |
| 440 | * ACT_RET_PRS_OK on success, ACT_RET_PRS_ERR on error. |
| 441 | */ |
| 442 | static enum act_parse_ret tcp_parse_set_tos(const char **args, int *cur_arg, struct proxy *px, |
| 443 | struct act_rule *rule, char **err) |
| 444 | { |
| 445 | #ifdef IP_TOS |
| 446 | char *endp; |
| 447 | int tos; |
| 448 | |
| 449 | if (!*args[*cur_arg]) { |
| 450 | memprintf(err, "expects exactly 1 argument (integer/hex value)"); |
| 451 | return ACT_RET_PRS_ERR; |
| 452 | } |
| 453 | tos = strtol(args[*cur_arg], &endp, 0); |
| 454 | if (endp && *endp != '\0') { |
| 455 | memprintf(err, "invalid character starting at '%s' (integer/hex value expected)", endp); |
| 456 | return ACT_RET_PRS_ERR; |
| 457 | } |
| 458 | |
| 459 | (*cur_arg)++; |
| 460 | |
| 461 | /* Register processing function. */ |
| 462 | rule->action_ptr = tcp_action_set_tos; |
| 463 | rule->action = ACT_CUSTOM; |
| 464 | rule->arg.act.p[0] = (void *)(uintptr_t)tos; |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 465 | return ACT_RET_PRS_OK; |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 466 | #else |
| 467 | memprintf(err, "not supported on this platform (IP_TOS undefined)"); |
| 468 | return ACT_RET_PRS_ERR; |
| 469 | #endif |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
| 473 | /* Parse a "silent-drop" action. It takes no argument. It returns ACT_RET_PRS_OK on |
| 474 | * success, ACT_RET_PRS_ERR on error. |
| 475 | */ |
| 476 | static enum act_parse_ret tcp_parse_silent_drop(const char **args, int *orig_arg, struct proxy *px, |
| 477 | struct act_rule *rule, char **err) |
| 478 | { |
| 479 | rule->action = ACT_CUSTOM; |
| 480 | rule->action_ptr = tcp_exec_action_silent_drop; |
| 481 | return ACT_RET_PRS_OK; |
| 482 | } |
| 483 | |
| 484 | |
| 485 | static struct action_kw_list tcp_req_conn_actions = {ILH, { |
Christopher Faulet | ee9c98d | 2021-06-25 15:18:33 +0200 | [diff] [blame] | 486 | { "set-dst" , tcp_parse_set_src_dst }, |
| 487 | { "set-dst-port", tcp_parse_set_src_dst }, |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 488 | { "set-mark", tcp_parse_set_mark }, |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 489 | { "set-src", tcp_parse_set_src_dst }, |
| 490 | { "set-src-port", tcp_parse_set_src_dst }, |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 491 | { "set-tos", tcp_parse_set_tos }, |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 492 | { "silent-drop", tcp_parse_silent_drop }, |
| 493 | { /* END */ } |
| 494 | }}; |
| 495 | |
| 496 | INITCALL1(STG_REGISTER, tcp_req_conn_keywords_register, &tcp_req_conn_actions); |
| 497 | |
| 498 | static struct action_kw_list tcp_req_sess_actions = {ILH, { |
Christopher Faulet | ee9c98d | 2021-06-25 15:18:33 +0200 | [diff] [blame] | 499 | { "set-dst" , tcp_parse_set_src_dst }, |
| 500 | { "set-dst-port", tcp_parse_set_src_dst }, |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 501 | { "set-mark", tcp_parse_set_mark }, |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 502 | { "set-src", tcp_parse_set_src_dst }, |
| 503 | { "set-src-port", tcp_parse_set_src_dst }, |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 504 | { "set-tos", tcp_parse_set_tos }, |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 505 | { "silent-drop", tcp_parse_silent_drop }, |
| 506 | { /* END */ } |
| 507 | }}; |
| 508 | |
| 509 | INITCALL1(STG_REGISTER, tcp_req_sess_keywords_register, &tcp_req_sess_actions); |
| 510 | |
| 511 | static struct action_kw_list tcp_req_cont_actions = {ILH, { |
Christopher Faulet | 1e83b70 | 2021-06-23 12:07:21 +0200 | [diff] [blame^] | 512 | { "set-src", tcp_parse_set_src_dst }, |
| 513 | { "set-src-port", tcp_parse_set_src_dst }, |
Christopher Faulet | ee9c98d | 2021-06-25 15:18:33 +0200 | [diff] [blame] | 514 | { "set-dst" , tcp_parse_set_src_dst }, |
| 515 | { "set-dst-port", tcp_parse_set_src_dst }, |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 516 | { "set-mark", tcp_parse_set_mark }, |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 517 | { "set-tos", tcp_parse_set_tos }, |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 518 | { "silent-drop", tcp_parse_silent_drop }, |
| 519 | { /* END */ } |
| 520 | }}; |
| 521 | |
| 522 | INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_cont_actions); |
| 523 | |
| 524 | static struct action_kw_list tcp_res_cont_actions = {ILH, { |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 525 | { "set-mark", tcp_parse_set_mark }, |
| 526 | { "set-tos", tcp_parse_set_tos }, |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 527 | { "silent-drop", tcp_parse_silent_drop }, |
| 528 | { /* END */ } |
| 529 | }}; |
| 530 | |
| 531 | INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_cont_actions); |
| 532 | |
| 533 | static struct action_kw_list http_req_actions = {ILH, { |
Christopher Faulet | ee9c98d | 2021-06-25 15:18:33 +0200 | [diff] [blame] | 534 | { "set-dst", tcp_parse_set_src_dst }, |
| 535 | { "set-dst-port", tcp_parse_set_src_dst }, |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 536 | { "set-mark", tcp_parse_set_mark }, |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 537 | { "set-src", tcp_parse_set_src_dst }, |
| 538 | { "set-src-port", tcp_parse_set_src_dst }, |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 539 | { "set-tos", tcp_parse_set_tos }, |
Christopher Faulet | ee9c98d | 2021-06-25 15:18:33 +0200 | [diff] [blame] | 540 | { "silent-drop", tcp_parse_silent_drop }, |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 541 | { /* END */ } |
| 542 | }}; |
| 543 | |
| 544 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 545 | |
| 546 | static struct action_kw_list http_res_actions = {ILH, { |
Christopher Faulet | 469c06c | 2021-06-25 15:11:35 +0200 | [diff] [blame] | 547 | { "set-mark", tcp_parse_set_mark }, |
| 548 | { "set-tos", tcp_parse_set_tos }, |
Willy Tarreau | aeae66c | 2020-08-28 11:03:28 +0200 | [diff] [blame] | 549 | { "silent-drop", tcp_parse_silent_drop }, |
| 550 | { /* END */ } |
| 551 | }}; |
| 552 | |
| 553 | INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_actions); |
| 554 | |
| 555 | |
| 556 | /* |
| 557 | * Local variables: |
| 558 | * c-indent-level: 8 |
| 559 | * c-basic-offset: 8 |
| 560 | * End: |
| 561 | */ |