Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp) |
| 3 | * |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 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/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/un.h> |
| 26 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 27 | #include <netinet/tcp.h> |
| 28 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 29 | #include <common/cfgparse.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
| 32 | #include <common/debug.h> |
| 33 | #include <common/errors.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 36 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 37 | #include <types/global.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 38 | #include <types/server.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 39 | |
| 40 | #include <proto/acl.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 41 | #include <proto/buffers.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame^] | 42 | #include <proto/frontend.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 43 | #include <proto/log.h> |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 44 | #include <proto/pattern.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 45 | #include <proto/port_range.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 46 | #include <proto/protocols.h> |
| 47 | #include <proto/proto_tcp.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 48 | #include <proto/proxy.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 49 | #include <proto/stream_sock.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 50 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 51 | #ifdef CONFIG_HAP_CTTPROXY |
| 52 | #include <import/ip_tproxy.h> |
| 53 | #endif |
| 54 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 55 | static int tcp_bind_listeners(struct protocol *proto); |
| 56 | |
| 57 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 58 | static struct protocol proto_tcpv4 = { |
| 59 | .name = "tcpv4", |
| 60 | .sock_domain = AF_INET, |
| 61 | .sock_type = SOCK_STREAM, |
| 62 | .sock_prot = IPPROTO_TCP, |
| 63 | .sock_family = AF_INET, |
| 64 | .sock_addrlen = sizeof(struct sockaddr_in), |
| 65 | .l3_addrlen = 32/8, |
| 66 | .read = &stream_sock_read, |
| 67 | .write = &stream_sock_write, |
| 68 | .bind_all = tcp_bind_listeners, |
| 69 | .unbind_all = unbind_all_listeners, |
| 70 | .enable_all = enable_all_listeners, |
| 71 | .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners), |
| 72 | .nb_listeners = 0, |
| 73 | }; |
| 74 | |
| 75 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 76 | static struct protocol proto_tcpv6 = { |
| 77 | .name = "tcpv6", |
| 78 | .sock_domain = AF_INET6, |
| 79 | .sock_type = SOCK_STREAM, |
| 80 | .sock_prot = IPPROTO_TCP, |
| 81 | .sock_family = AF_INET6, |
| 82 | .sock_addrlen = sizeof(struct sockaddr_in6), |
| 83 | .l3_addrlen = 128/8, |
| 84 | .read = &stream_sock_read, |
| 85 | .write = &stream_sock_write, |
| 86 | .bind_all = tcp_bind_listeners, |
| 87 | .unbind_all = unbind_all_listeners, |
| 88 | .enable_all = enable_all_listeners, |
| 89 | .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners), |
| 90 | .nb_listeners = 0, |
| 91 | }; |
| 92 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 93 | |
| 94 | /* Binds ipv4 address <local> to socket <fd>, unless <flags> is set, in which |
| 95 | * case we try to bind <remote>. <flags> is a 2-bit field consisting of : |
| 96 | * - 0 : ignore remote address (may even be a NULL pointer) |
| 97 | * - 1 : use provided address |
| 98 | * - 2 : use provided port |
| 99 | * - 3 : use both |
| 100 | * |
| 101 | * The function supports multiple foreign binding methods : |
| 102 | * - linux_tproxy: we directly bind to the foreign address |
| 103 | * - cttproxy: we bind to a local address then nat. |
| 104 | * The second one can be used as a fallback for the first one. |
| 105 | * This function returns 0 when everything's OK, 1 if it could not bind, to the |
| 106 | * local address, 2 if it could not bind to the foreign address. |
| 107 | */ |
| 108 | int tcpv4_bind_socket(int fd, int flags, struct sockaddr_in *local, struct sockaddr_in *remote) |
| 109 | { |
| 110 | struct sockaddr_in bind_addr; |
| 111 | int foreign_ok = 0; |
| 112 | int ret; |
| 113 | |
| 114 | #ifdef CONFIG_HAP_LINUX_TPROXY |
| 115 | static int ip_transp_working = 1; |
| 116 | if (flags && ip_transp_working) { |
| 117 | if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == 0 |
| 118 | || setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == 0) |
| 119 | foreign_ok = 1; |
| 120 | else |
| 121 | ip_transp_working = 0; |
| 122 | } |
| 123 | #endif |
| 124 | if (flags) { |
| 125 | memset(&bind_addr, 0, sizeof(bind_addr)); |
| 126 | if (flags & 1) |
| 127 | bind_addr.sin_addr = remote->sin_addr; |
| 128 | if (flags & 2) |
| 129 | bind_addr.sin_port = remote->sin_port; |
| 130 | } |
| 131 | |
| 132 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); |
| 133 | if (foreign_ok) { |
| 134 | ret = bind(fd, (struct sockaddr *)&bind_addr, sizeof(bind_addr)); |
| 135 | if (ret < 0) |
| 136 | return 2; |
| 137 | } |
| 138 | else { |
| 139 | ret = bind(fd, (struct sockaddr *)local, sizeof(*local)); |
| 140 | if (ret < 0) |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | if (!flags) |
| 145 | return 0; |
| 146 | |
| 147 | #ifdef CONFIG_HAP_CTTPROXY |
| 148 | if (!foreign_ok) { |
| 149 | struct in_tproxy itp1, itp2; |
| 150 | memset(&itp1, 0, sizeof(itp1)); |
| 151 | |
| 152 | itp1.op = TPROXY_ASSIGN; |
| 153 | itp1.v.addr.faddr = bind_addr.sin_addr; |
| 154 | itp1.v.addr.fport = bind_addr.sin_port; |
| 155 | |
| 156 | /* set connect flag on socket */ |
| 157 | itp2.op = TPROXY_FLAGS; |
| 158 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 159 | |
| 160 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 && |
| 161 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) { |
| 162 | foreign_ok = 1; |
| 163 | } |
| 164 | } |
| 165 | #endif |
| 166 | if (!foreign_ok) |
| 167 | /* we could not bind to a foreign address */ |
| 168 | return 2; |
| 169 | |
| 170 | return 0; |
| 171 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 172 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * This function initiates a connection to the server assigned to this session |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 176 | * (s->srv, s->srv_addr). It will assign a server if none is assigned yet. A |
| 177 | * source address may be pointed to by <from_addr>. Note that this is only used |
| 178 | * in case of transparent proxying. Normal source bind addresses are still |
| 179 | * determined locally (due to the possible need of a source port). |
| 180 | * |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 181 | * It can return one of : |
| 182 | * - SN_ERR_NONE if everything's OK |
| 183 | * - SN_ERR_SRVTO if there are no more servers |
| 184 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 185 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 186 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 187 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 188 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 189 | */ |
| 190 | int tcpv4_connect_server(struct stream_interface *si, |
| 191 | struct proxy *be, struct server *srv, |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 192 | struct sockaddr *srv_addr, struct sockaddr *from_addr) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 193 | { |
| 194 | int fd; |
| 195 | |
| 196 | if ((fd = si->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 197 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 198 | |
| 199 | if (errno == ENFILE) |
| 200 | send_log(be, LOG_EMERG, |
| 201 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 202 | be->id, maxfd); |
| 203 | else if (errno == EMFILE) |
| 204 | send_log(be, LOG_EMERG, |
| 205 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 206 | be->id, maxfd); |
| 207 | else if (errno == ENOBUFS || errno == ENOMEM) |
| 208 | send_log(be, LOG_EMERG, |
| 209 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 210 | be->id, maxfd); |
| 211 | /* this is a resource error */ |
| 212 | return SN_ERR_RESOURCE; |
| 213 | } |
| 214 | |
| 215 | if (fd >= global.maxsock) { |
| 216 | /* do not log anything there, it's a normal condition when this option |
| 217 | * is used to serialize connections to a server ! |
| 218 | */ |
| 219 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 220 | close(fd); |
| 221 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
| 222 | } |
| 223 | |
| 224 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
| 225 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1)) { |
| 226 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 227 | close(fd); |
| 228 | return SN_ERR_INTERNAL; |
| 229 | } |
| 230 | |
| 231 | if (be->options & PR_O_TCP_SRV_KA) |
| 232 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 233 | |
| 234 | if (be->options & PR_O_TCP_NOLING) |
| 235 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 236 | |
| 237 | /* allow specific binding : |
| 238 | * - server-specific at first |
| 239 | * - proxy-specific next |
| 240 | */ |
| 241 | if (srv != NULL && srv->state & SRV_BIND_SRC) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 242 | int ret, flags = 0; |
| 243 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 244 | switch (srv->state & SRV_TPROXY_MASK) { |
| 245 | case SRV_TPROXY_ADDR: |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 246 | case SRV_TPROXY_CLI: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 247 | flags = 3; |
| 248 | break; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 249 | case SRV_TPROXY_CIP: |
Willy Tarreau | 090466c | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 250 | case SRV_TPROXY_DYN: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 251 | flags = 1; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 252 | break; |
| 253 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 254 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 255 | #ifdef SO_BINDTODEVICE |
| 256 | /* Note: this might fail if not CAP_NET_RAW */ |
| 257 | if (srv->iface_name) |
| 258 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1); |
| 259 | #endif |
| 260 | |
| 261 | if (srv->sport_range) { |
| 262 | int attempts = 10; /* should be more than enough to find a spare port */ |
| 263 | struct sockaddr_in src; |
| 264 | |
| 265 | ret = 1; |
| 266 | src = srv->source_addr; |
| 267 | |
| 268 | do { |
| 269 | /* note: in case of retry, we may have to release a previously |
| 270 | * allocated port, hence this loop's construct. |
| 271 | */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 272 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 273 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 274 | |
| 275 | if (!attempts) |
| 276 | break; |
| 277 | attempts--; |
| 278 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 279 | fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range); |
| 280 | if (!fdinfo[fd].local_port) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 281 | break; |
| 282 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 283 | fdinfo[fd].port_range = srv->sport_range; |
| 284 | src.sin_port = htons(fdinfo[fd].local_port); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 285 | |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 286 | ret = tcpv4_bind_socket(fd, flags, &src, (struct sockaddr_in *)from_addr); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 287 | } while (ret != 0); /* binding NOK */ |
| 288 | } |
| 289 | else { |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 290 | ret = tcpv4_bind_socket(fd, flags, &srv->source_addr, (struct sockaddr_in *)from_addr); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (ret) { |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 294 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 295 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 296 | close(fd); |
| 297 | |
| 298 | if (ret == 1) { |
| 299 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 300 | be->id, srv->id); |
| 301 | send_log(be, LOG_EMERG, |
| 302 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 303 | be->id, srv->id); |
| 304 | } else { |
| 305 | Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n", |
| 306 | be->id, srv->id); |
| 307 | send_log(be, LOG_EMERG, |
| 308 | "Cannot bind to tproxy source address before connect() for server %s/%s.\n", |
| 309 | be->id, srv->id); |
| 310 | } |
| 311 | return SN_ERR_RESOURCE; |
| 312 | } |
| 313 | } |
| 314 | else if (be->options & PR_O_BIND_SRC) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 315 | int ret, flags = 0; |
| 316 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 317 | switch (be->options & PR_O_TPXY_MASK) { |
| 318 | case PR_O_TPXY_ADDR: |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 319 | case PR_O_TPXY_CLI: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 320 | flags = 3; |
| 321 | break; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 322 | case PR_O_TPXY_CIP: |
Willy Tarreau | 090466c | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 323 | case PR_O_TPXY_DYN: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 324 | flags = 1; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 325 | break; |
| 326 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 327 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 328 | #ifdef SO_BINDTODEVICE |
| 329 | /* Note: this might fail if not CAP_NET_RAW */ |
| 330 | if (be->iface_name) |
| 331 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1); |
| 332 | #endif |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 333 | ret = tcpv4_bind_socket(fd, flags, &be->source_addr, (struct sockaddr_in *)from_addr); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 334 | if (ret) { |
| 335 | close(fd); |
| 336 | if (ret == 1) { |
| 337 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", |
| 338 | be->id); |
| 339 | send_log(be, LOG_EMERG, |
| 340 | "Cannot bind to source address before connect() for proxy %s.\n", |
| 341 | be->id); |
| 342 | } else { |
| 343 | Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n", |
| 344 | be->id); |
| 345 | send_log(be, LOG_EMERG, |
| 346 | "Cannot bind to tproxy source address before connect() for proxy %s.\n", |
| 347 | be->id); |
| 348 | } |
| 349 | return SN_ERR_RESOURCE; |
| 350 | } |
| 351 | } |
| 352 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 353 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 354 | /* disabling tcp quick ack now allows the first request to leave the |
| 355 | * machine with the first ACK. We only do this if there are pending |
| 356 | * data in the buffer. |
| 357 | */ |
| 358 | if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max) |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 359 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 360 | #endif |
| 361 | |
Willy Tarreau | e803de2 | 2010-01-21 17:43:04 +0100 | [diff] [blame] | 362 | if (global.tune.server_sndbuf) |
| 363 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 364 | |
| 365 | if (global.tune.server_rcvbuf) |
| 366 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 367 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 368 | if ((connect(fd, (struct sockaddr *)srv_addr, sizeof(struct sockaddr_in)) == -1) && |
| 369 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 370 | |
| 371 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 372 | char *msg; |
| 373 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 374 | msg = "no free ports"; |
| 375 | else |
| 376 | msg = "local address already in use"; |
| 377 | |
| 378 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 379 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 380 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 381 | close(fd); |
| 382 | send_log(be, LOG_EMERG, |
| 383 | "Connect() failed for server %s/%s: %s.\n", |
| 384 | be->id, srv->id, msg); |
| 385 | return SN_ERR_RESOURCE; |
| 386 | } else if (errno == ETIMEDOUT) { |
| 387 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 388 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 389 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 390 | close(fd); |
| 391 | return SN_ERR_SRVTO; |
| 392 | } else { |
| 393 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 394 | //qfprintf(stderr,"Connect(): %d", errno); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 395 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 396 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 397 | close(fd); |
| 398 | return SN_ERR_SRVCL; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | fdtab[fd].owner = si; |
| 403 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 404 | fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY; |
| 405 | fdtab[fd].cb[DIR_RD].f = &stream_sock_read; |
| 406 | fdtab[fd].cb[DIR_RD].b = si->ib; |
| 407 | fdtab[fd].cb[DIR_WR].f = &stream_sock_write; |
| 408 | fdtab[fd].cb[DIR_WR].b = si->ob; |
| 409 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 410 | fdinfo[fd].peeraddr = (struct sockaddr *)srv_addr; |
| 411 | fdinfo[fd].peerlen = sizeof(struct sockaddr_in); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 412 | |
| 413 | fd_insert(fd); |
| 414 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
| 415 | |
| 416 | si->state = SI_ST_CON; |
| 417 | si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */ |
| 418 | si->exp = tick_add_ifset(now_ms, be->timeout.connect); |
| 419 | |
| 420 | return SN_ERR_NONE; /* connection is OK */ |
| 421 | } |
| 422 | |
| 423 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 424 | /* This function tries to bind a TCPv4/v6 listener. It may return a warning or |
| 425 | * an error message in <err> if the message is at most <errlen> bytes long |
| 426 | * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN, |
| 427 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 428 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 429 | * error occurred but that it may vanish after a retry (eg: port in use), and |
| 430 | * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter |
| 431 | * the meaning of the error, but just indicate that a message is present which |
| 432 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 433 | * that it's pointless to try to start other listeners. No error message is |
| 434 | * returned if errlen is NULL. |
| 435 | */ |
| 436 | int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 437 | { |
| 438 | __label__ tcp_return, tcp_close_return; |
| 439 | int fd, err; |
| 440 | const char *msg = NULL; |
| 441 | |
| 442 | /* ensure we never return garbage */ |
| 443 | if (errmsg && errlen) |
| 444 | *errmsg = 0; |
| 445 | |
| 446 | if (listener->state != LI_ASSIGNED) |
| 447 | return ERR_NONE; /* already bound */ |
| 448 | |
| 449 | err = ERR_NONE; |
| 450 | |
| 451 | if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 452 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 453 | msg = "cannot create listening socket"; |
| 454 | goto tcp_return; |
| 455 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 456 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 457 | if (fd >= global.maxsock) { |
| 458 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 459 | msg = "not enough free sockets (raise '-n' parameter)"; |
| 460 | goto tcp_close_return; |
| 461 | } |
| 462 | |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 463 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 464 | err |= ERR_FATAL | ERR_ALERT; |
| 465 | msg = "cannot make socket non-blocking"; |
| 466 | goto tcp_close_return; |
| 467 | } |
| 468 | |
| 469 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)) == -1) { |
| 470 | /* not fatal but should be reported */ |
| 471 | msg = "cannot do so_reuseaddr"; |
| 472 | err |= ERR_ALERT; |
| 473 | } |
| 474 | |
| 475 | if (listener->options & LI_O_NOLINGER) |
| 476 | setsockopt(fd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 477 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 478 | #ifdef SO_REUSEPORT |
| 479 | /* OpenBSD supports this. As it's present in old libc versions of Linux, |
| 480 | * it might return an error that we will silently ignore. |
| 481 | */ |
| 482 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof(one)); |
| 483 | #endif |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 484 | #ifdef CONFIG_HAP_LINUX_TPROXY |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 485 | if ((listener->options & LI_O_FOREIGN) |
Willy Tarreau | 0a45989 | 2008-01-13 17:37:16 +0100 | [diff] [blame] | 486 | && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, (char *) &one, sizeof(one)) == -1) |
| 487 | && (setsockopt(fd, SOL_IP, IP_FREEBIND, (char *) &one, sizeof(one)) == -1)) { |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 488 | msg = "cannot make listening socket transparent"; |
| 489 | err |= ERR_ALERT; |
| 490 | } |
| 491 | #endif |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 492 | #ifdef SO_BINDTODEVICE |
| 493 | /* Note: this might fail if not CAP_NET_RAW */ |
| 494 | if (listener->interface) { |
| 495 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 496 | listener->interface, strlen(listener->interface) + 1) == -1) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 497 | msg = "cannot bind listener to device"; |
| 498 | err |= ERR_WARN; |
| 499 | } |
| 500 | } |
| 501 | #endif |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 502 | #if defined(TCP_MAXSEG) |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 503 | if (listener->maxseg) { |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 504 | if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 505 | &listener->maxseg, sizeof(listener->maxseg)) == -1) { |
| 506 | msg = "cannot set MSS"; |
| 507 | err |= ERR_WARN; |
| 508 | } |
| 509 | } |
| 510 | #endif |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 511 | #if defined(TCP_DEFER_ACCEPT) |
| 512 | if (listener->options & LI_O_DEF_ACCEPT) { |
| 513 | /* defer accept by up to one second */ |
| 514 | int accept_delay = 1; |
| 515 | if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) { |
| 516 | msg = "cannot enable DEFER_ACCEPT"; |
| 517 | err |= ERR_WARN; |
| 518 | } |
| 519 | } |
| 520 | #endif |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 521 | if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) { |
| 522 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 523 | msg = "cannot bind socket"; |
| 524 | goto tcp_close_return; |
| 525 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 526 | |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 527 | if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 528 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 529 | msg = "cannot listen to socket"; |
| 530 | goto tcp_close_return; |
| 531 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 532 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 533 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 534 | if (listener->options & LI_O_NOQUICKACK) |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 535 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, (char *) &zero, sizeof(zero)); |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 536 | #endif |
| 537 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 538 | /* the socket is ready */ |
| 539 | listener->fd = fd; |
| 540 | listener->state = LI_LISTEN; |
| 541 | |
| 542 | /* the function for the accept() event */ |
| 543 | fd_insert(fd); |
| 544 | fdtab[fd].cb[DIR_RD].f = listener->accept; |
| 545 | fdtab[fd].cb[DIR_WR].f = NULL; /* never called */ |
| 546 | fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL; |
Willy Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 547 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 548 | fdtab[fd].state = FD_STLISTEN; |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 549 | fdtab[fd].flags = FD_FL_TCP; |
Willy Tarreau | 5d707e1 | 2009-06-28 11:09:07 +0200 | [diff] [blame] | 550 | if (listener->options & LI_O_NOLINGER) |
| 551 | fdtab[fd].flags |= FD_FL_TCP_NOLING; |
| 552 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 553 | fdinfo[fd].peeraddr = NULL; |
| 554 | fdinfo[fd].peerlen = 0; |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 555 | tcp_return: |
| 556 | if (msg && errlen) |
| 557 | strlcpy2(errmsg, msg, errlen); |
| 558 | return err; |
| 559 | |
| 560 | tcp_close_return: |
| 561 | close(fd); |
| 562 | goto tcp_return; |
| 563 | } |
| 564 | |
| 565 | /* This function creates all TCP sockets bound to the protocol entry <proto>. |
| 566 | * It is intended to be used as the protocol's bind_all() function. |
| 567 | * The sockets will be registered but not added to any fd_set, in order not to |
| 568 | * loose them across the fork(). A call to enable_all_listeners() is needed |
| 569 | * to complete initialization. The return value is composed from ERR_*. |
| 570 | */ |
| 571 | static int tcp_bind_listeners(struct protocol *proto) |
| 572 | { |
| 573 | struct listener *listener; |
| 574 | int err = ERR_NONE; |
| 575 | |
| 576 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
| 577 | err |= tcp_bind_listener(listener, NULL, 0); |
| 578 | if ((err & ERR_CODE) == ERR_ABORT) |
| 579 | break; |
| 580 | } |
| 581 | |
| 582 | return err; |
| 583 | } |
| 584 | |
| 585 | /* Add listener to the list of tcpv4 listeners. The listener's state |
| 586 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 587 | * listeners is updated. This is the function to use to add a new listener. |
| 588 | */ |
| 589 | void tcpv4_add_listener(struct listener *listener) |
| 590 | { |
| 591 | if (listener->state != LI_INIT) |
| 592 | return; |
| 593 | listener->state = LI_ASSIGNED; |
| 594 | listener->proto = &proto_tcpv4; |
| 595 | LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list); |
| 596 | proto_tcpv4.nb_listeners++; |
| 597 | } |
| 598 | |
| 599 | /* Add listener to the list of tcpv4 listeners. The listener's state |
| 600 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 601 | * listeners is updated. This is the function to use to add a new listener. |
| 602 | */ |
| 603 | void tcpv6_add_listener(struct listener *listener) |
| 604 | { |
| 605 | if (listener->state != LI_INIT) |
| 606 | return; |
| 607 | listener->state = LI_ASSIGNED; |
| 608 | listener->proto = &proto_tcpv6; |
| 609 | LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list); |
| 610 | proto_tcpv6.nb_listeners++; |
| 611 | } |
| 612 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 613 | /* This function performs the TCP request analysis on the current request. It |
| 614 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 615 | * needs more data, encounters an error, or wants to immediately abort the |
| 616 | * request. It relies on buffers flags, and updates s->req->analysers. Its |
| 617 | * behaviour is rather simple: |
Willy Tarreau | 86ef7dc | 2009-03-15 22:55:47 +0100 | [diff] [blame] | 618 | * - the analyser should check for errors and timeouts, and react as expected. |
| 619 | * It does not have to close anything upon error, the caller will. Note that |
| 620 | * the caller also knows how to report errors and timeouts. |
| 621 | * - if the analyser does not have enough data, it must return 0 without calling |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 622 | * other ones. It should also probably do a buffer_write_dis() to ensure |
| 623 | * that unprocessed data will not be forwarded. But that probably depends on |
| 624 | * the protocol. |
| 625 | * - if an analyser has enough data, it just has to pass on to the next |
| 626 | * analyser without using buffer_write_dis() (enabled by default). |
| 627 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 628 | * reset its bit from the analysers flags in order not to be called anymore. |
| 629 | * |
| 630 | * In the future, analysers should be able to indicate that they want to be |
| 631 | * called after XXX bytes have been received (or transfered), and the min of |
| 632 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 633 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 634 | int tcp_inspect_request(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 635 | { |
| 636 | struct tcp_rule *rule; |
| 637 | int partial; |
| 638 | |
| 639 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 640 | now_ms, __FUNCTION__, |
| 641 | s, |
| 642 | req, |
| 643 | req->rex, req->wex, |
| 644 | req->flags, |
| 645 | req->l, |
| 646 | req->analysers); |
| 647 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 648 | /* We don't know whether we have enough data, so must proceed |
| 649 | * this way : |
| 650 | * - iterate through all rules in their declaration order |
| 651 | * - if one rule returns MISS, it means the inspect delay is |
| 652 | * not over yet, then return immediately, otherwise consider |
| 653 | * it as a non-match. |
| 654 | * - if one rule returns OK, then return OK |
| 655 | * - if one rule returns KO, then return KO |
| 656 | */ |
| 657 | |
Willy Tarreau | d869b24 | 2009-03-15 14:43:58 +0100 | [diff] [blame] | 658 | if (req->flags & BF_SHUTR || !s->fe->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms)) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 659 | partial = 0; |
| 660 | else |
| 661 | partial = ACL_PARTIAL; |
| 662 | |
| 663 | list_for_each_entry(rule, &s->fe->tcp_req.inspect_rules, list) { |
| 664 | int ret = ACL_PAT_PASS; |
| 665 | |
| 666 | if (rule->cond) { |
Willy Tarreau | 51d5dad | 2009-07-12 10:10:05 +0200 | [diff] [blame] | 667 | ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, ACL_DIR_REQ | partial); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 668 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 669 | buffer_dont_connect(req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 670 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | d869b24 | 2009-03-15 14:43:58 +0100 | [diff] [blame] | 671 | if (!tick_isset(req->analyse_exp) && s->fe->tcp_req.inspect_delay) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 672 | req->analyse_exp = tick_add_ifset(now_ms, s->fe->tcp_req.inspect_delay); |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | ret = acl_pass(ret); |
| 677 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 678 | ret = !ret; |
| 679 | } |
| 680 | |
| 681 | if (ret) { |
| 682 | /* we have a matching rule. */ |
| 683 | if (rule->action == TCP_ACT_REJECT) { |
| 684 | buffer_abort(req); |
| 685 | buffer_abort(s->rep); |
| 686 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 687 | |
Willy Tarreau | 23968d8 | 2010-05-23 23:50:44 +0200 | [diff] [blame] | 688 | s->fe->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 689 | if (s->listener->counters) |
Willy Tarreau | 23968d8 | 2010-05-23 23:50:44 +0200 | [diff] [blame] | 690 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 691 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 692 | if (!(s->flags & SN_ERR_MASK)) |
| 693 | s->flags |= SN_ERR_PRXCOND; |
| 694 | if (!(s->flags & SN_FINST_MASK)) |
| 695 | s->flags |= SN_FINST_R; |
| 696 | return 0; |
| 697 | } |
| 698 | /* otherwise accept */ |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | /* if we get there, it means we have no rule which matches, or |
| 704 | * we have an explicit accept, so we apply the default accept. |
| 705 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 706 | req->analysers &= ~an_bit; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 707 | req->analyse_exp = TICK_ETERNITY; |
| 708 | return 1; |
| 709 | } |
| 710 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 711 | /* This function should be called to parse a line starting with the "tcp-request" |
| 712 | * keyword. |
| 713 | */ |
| 714 | static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx, |
| 715 | struct proxy *defpx, char *err, int errlen) |
| 716 | { |
| 717 | const char *ptr = NULL; |
Willy Tarreau | c7e961e | 2008-08-17 17:13:47 +0200 | [diff] [blame] | 718 | unsigned int val; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 719 | int retlen; |
| 720 | |
| 721 | if (!*args[1]) { |
| 722 | snprintf(err, errlen, "missing argument for '%s' in %s '%s'", |
| 723 | args[0], proxy_type_str(proxy), curpx->id); |
| 724 | return -1; |
| 725 | } |
| 726 | |
| 727 | if (!strcmp(args[1], "inspect-delay")) { |
| 728 | if (curpx == defpx) { |
| 729 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 730 | args[0], args[1]); |
| 731 | return -1; |
| 732 | } |
| 733 | |
| 734 | if (!(curpx->cap & PR_CAP_FE)) { |
| 735 | snprintf(err, errlen, "%s %s will be ignored because %s '%s' has no %s capability", |
| 736 | args[0], args[1], proxy_type_str(proxy), curpx->id, |
| 737 | "frontend"); |
| 738 | return 1; |
| 739 | } |
| 740 | |
| 741 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
| 742 | retlen = snprintf(err, errlen, |
| 743 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
| 744 | args[0], args[1], proxy_type_str(proxy), curpx->id); |
| 745 | if (ptr && retlen < errlen) |
| 746 | retlen += snprintf(err+retlen, errlen - retlen, |
| 747 | " (unexpected character '%c')", *ptr); |
| 748 | return -1; |
| 749 | } |
| 750 | |
| 751 | if (curpx->tcp_req.inspect_delay) { |
| 752 | snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'", |
| 753 | args[0], args[1], proxy_type_str(proxy), curpx->id); |
| 754 | return 1; |
| 755 | } |
| 756 | curpx->tcp_req.inspect_delay = val; |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | if (!strcmp(args[1], "content")) { |
| 761 | int action; |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 762 | int warn = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 763 | int pol = ACL_COND_NONE; |
| 764 | struct acl_cond *cond; |
| 765 | struct tcp_rule *rule; |
| 766 | |
| 767 | if (curpx == defpx) { |
| 768 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 769 | args[0], args[1]); |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | if (!strcmp(args[2], "accept")) |
| 774 | action = TCP_ACT_ACCEPT; |
| 775 | else if (!strcmp(args[2], "reject")) |
| 776 | action = TCP_ACT_REJECT; |
| 777 | else { |
| 778 | retlen = snprintf(err, errlen, |
| 779 | "'%s %s' expects 'accept' or 'reject', in %s '%s' (was '%s')", |
| 780 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[2]); |
| 781 | return -1; |
| 782 | } |
| 783 | |
| 784 | pol = ACL_COND_NONE; |
| 785 | cond = NULL; |
| 786 | |
Willy Tarreau | ef6494c | 2010-01-28 17:12:36 +0100 | [diff] [blame] | 787 | if (strcmp(args[3], "if") == 0 || strcmp(args[3], "unless") == 0) { |
| 788 | if ((cond = build_acl_cond(NULL, 0, curpx, (const char **)args+3)) == NULL) { |
| 789 | retlen = snprintf(err, errlen, |
| 790 | "error detected in %s '%s' while parsing '%s' condition", |
| 791 | proxy_type_str(curpx), curpx->id, args[3]); |
| 792 | return -1; |
| 793 | } |
| 794 | } |
| 795 | else if (*args[3]) { |
Willy Tarreau | 606ad73 | 2009-07-14 21:17:05 +0200 | [diff] [blame] | 796 | retlen = snprintf(err, errlen, |
| 797 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')", |
| 798 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[3]); |
| 799 | return -1; |
| 800 | } |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 801 | |
Willy Tarreau | 1a21194 | 2009-07-14 13:53:17 +0200 | [diff] [blame] | 802 | if (cond && (cond->requires & ACL_USE_RTR_ANY)) { |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 803 | struct acl *acl; |
| 804 | const char *name; |
| 805 | |
Willy Tarreau | 1a21194 | 2009-07-14 13:53:17 +0200 | [diff] [blame] | 806 | acl = cond_find_require(cond, ACL_USE_RTR_ANY); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 807 | name = acl ? acl->name : "(unknown)"; |
| 808 | |
| 809 | retlen = snprintf(err, errlen, |
Willy Tarreau | 1a21194 | 2009-07-14 13:53:17 +0200 | [diff] [blame] | 810 | "acl '%s' involves some response-only criteria which will be ignored.", |
| 811 | name); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 812 | warn++; |
| 813 | } |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 814 | rule = (struct tcp_rule *)calloc(1, sizeof(*rule)); |
| 815 | rule->cond = cond; |
| 816 | rule->action = action; |
| 817 | LIST_INIT(&rule->list); |
| 818 | LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 819 | return warn; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | snprintf(err, errlen, "unknown argument '%s' after '%s' in %s '%s'", |
| 823 | args[1], args[0], proxy_type_str(proxy), curpx->id); |
| 824 | return -1; |
| 825 | } |
| 826 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 827 | |
| 828 | /************************************************************************/ |
| 829 | /* All supported ACL keywords must be declared here. */ |
| 830 | /************************************************************************/ |
| 831 | |
| 832 | /* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */ |
| 833 | static int |
| 834 | acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir, |
| 835 | struct acl_expr *expr, struct acl_test *test) |
| 836 | { |
| 837 | test->i = l4->cli_addr.ss_family; |
| 838 | if (test->i == AF_INET) |
| 839 | test->ptr = (void *)&((struct sockaddr_in *)&l4->cli_addr)->sin_addr; |
| 840 | else |
| 841 | test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_addr; |
| 842 | test->flags = ACL_TEST_F_READ_ONLY; |
| 843 | return 1; |
| 844 | } |
| 845 | |
| 846 | /* extract the connection's source address */ |
| 847 | static int |
| 848 | pattern_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir, |
| 849 | const char *arg, int arg_len, union pattern_data *data) |
| 850 | { |
| 851 | data->ip.s_addr = ((struct sockaddr_in *)&l4->cli_addr)->sin_addr.s_addr; |
| 852 | return 1; |
| 853 | } |
| 854 | |
| 855 | |
| 856 | /* set test->i to the connection's source port */ |
| 857 | static int |
| 858 | acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 859 | struct acl_expr *expr, struct acl_test *test) |
| 860 | { |
| 861 | if (l4->cli_addr.ss_family == AF_INET) |
| 862 | test->i = ntohs(((struct sockaddr_in *)&l4->cli_addr)->sin_port); |
| 863 | else |
| 864 | test->i = ntohs(((struct sockaddr_in6 *)(&l4->cli_addr))->sin6_port); |
| 865 | test->flags = 0; |
| 866 | return 1; |
| 867 | } |
| 868 | |
| 869 | |
| 870 | /* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */ |
| 871 | static int |
| 872 | acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir, |
| 873 | struct acl_expr *expr, struct acl_test *test) |
| 874 | { |
| 875 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 876 | get_frt_addr(l4); |
| 877 | |
| 878 | test->i = l4->frt_addr.ss_family; |
| 879 | if (test->i == AF_INET) |
| 880 | test->ptr = (void *)&((struct sockaddr_in *)&l4->frt_addr)->sin_addr; |
| 881 | else |
| 882 | test->ptr = (void *)&((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_addr; |
| 883 | test->flags = ACL_TEST_F_READ_ONLY; |
| 884 | return 1; |
| 885 | } |
| 886 | |
| 887 | |
| 888 | /* extract the connection's destination address */ |
| 889 | static int |
| 890 | pattern_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir, |
| 891 | const char *arg, int arg_len, union pattern_data *data) |
| 892 | { |
| 893 | data->ip.s_addr = ((struct sockaddr_in *)&l4->frt_addr)->sin_addr.s_addr; |
| 894 | return 1; |
| 895 | } |
| 896 | |
| 897 | /* set test->i to the frontend connexion's destination port */ |
| 898 | static int |
| 899 | acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 900 | struct acl_expr *expr, struct acl_test *test) |
| 901 | { |
| 902 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 903 | get_frt_addr(l4); |
| 904 | |
| 905 | if (l4->frt_addr.ss_family == AF_INET) |
| 906 | test->i = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port); |
| 907 | else |
| 908 | test->i = ntohs(((struct sockaddr_in6 *)(&l4->frt_addr))->sin6_port); |
| 909 | test->flags = 0; |
| 910 | return 1; |
| 911 | } |
| 912 | |
| 913 | static int |
| 914 | pattern_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 915 | const char *arg, int arg_len, union pattern_data *data) |
| 916 | |
| 917 | { |
| 918 | data->integer = ntohs(((struct sockaddr_in *)&l4->frt_addr)->sin_port); |
| 919 | return 1; |
| 920 | } |
| 921 | |
| 922 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 923 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 924 | { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req }, |
| 925 | { 0, NULL, NULL }, |
| 926 | }}; |
| 927 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 928 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 929 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 930 | { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT }, |
| 931 | { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP }, |
| 932 | { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP }, |
| 933 | { "dst_port", acl_parse_int, acl_fetch_dport, acl_match_int, ACL_USE_TCP_PERMANENT }, |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 934 | { NULL, NULL, NULL, NULL }, |
| 935 | }}; |
| 936 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 937 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 938 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
| 939 | { "src", pattern_fetch_src, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
| 940 | { "dst", pattern_fetch_dst, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
| 941 | { "dst_port", pattern_fetch_dport, PATTERN_TYPE_INTEGER, PATTERN_FETCH_REQ }, |
| 942 | { NULL, NULL, 0, 0 }, |
| 943 | }}; |
| 944 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 945 | __attribute__((constructor)) |
| 946 | static void __tcp_protocol_init(void) |
| 947 | { |
| 948 | protocol_register(&proto_tcpv4); |
| 949 | protocol_register(&proto_tcpv6); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 950 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 951 | cfg_register_keywords(&cfg_kws); |
| 952 | acl_register_keywords(&acl_kws); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | |
| 956 | /* |
| 957 | * Local variables: |
| 958 | * c-indent-level: 8 |
| 959 | * c-basic-offset: 8 |
| 960 | * End: |
| 961 | */ |