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