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 | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 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 | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 49 | #include <proto/session.h> |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 50 | #include <proto/stick_table.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 51 | #include <proto/stream_sock.h> |
Willy Tarreau | a975b8f | 2010-06-05 19:13:27 +0200 | [diff] [blame] | 52 | #include <proto/task.h> |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 53 | #include <proto/buffers.h> |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 54 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 55 | #ifdef CONFIG_HAP_CTTPROXY |
| 56 | #include <import/ip_tproxy.h> |
| 57 | #endif |
| 58 | |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 59 | static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen); |
| 60 | static int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 61 | |
| 62 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 63 | static struct protocol proto_tcpv4 = { |
| 64 | .name = "tcpv4", |
| 65 | .sock_domain = AF_INET, |
| 66 | .sock_type = SOCK_STREAM, |
| 67 | .sock_prot = IPPROTO_TCP, |
| 68 | .sock_family = AF_INET, |
| 69 | .sock_addrlen = sizeof(struct sockaddr_in), |
| 70 | .l3_addrlen = 32/8, |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 71 | .accept = &stream_sock_accept, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 72 | .read = &stream_sock_read, |
| 73 | .write = &stream_sock_write, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 74 | .bind = tcp_bind_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 75 | .bind_all = tcp_bind_listeners, |
| 76 | .unbind_all = unbind_all_listeners, |
| 77 | .enable_all = enable_all_listeners, |
| 78 | .listeners = LIST_HEAD_INIT(proto_tcpv4.listeners), |
| 79 | .nb_listeners = 0, |
| 80 | }; |
| 81 | |
| 82 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 83 | static struct protocol proto_tcpv6 = { |
| 84 | .name = "tcpv6", |
| 85 | .sock_domain = AF_INET6, |
| 86 | .sock_type = SOCK_STREAM, |
| 87 | .sock_prot = IPPROTO_TCP, |
| 88 | .sock_family = AF_INET6, |
| 89 | .sock_addrlen = sizeof(struct sockaddr_in6), |
| 90 | .l3_addrlen = 128/8, |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 91 | .accept = &stream_sock_accept, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 92 | .read = &stream_sock_read, |
| 93 | .write = &stream_sock_write, |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 94 | .bind = tcp_bind_listener, |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 95 | .bind_all = tcp_bind_listeners, |
| 96 | .unbind_all = unbind_all_listeners, |
| 97 | .enable_all = enable_all_listeners, |
| 98 | .listeners = LIST_HEAD_INIT(proto_tcpv6.listeners), |
| 99 | .nb_listeners = 0, |
| 100 | }; |
| 101 | |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 102 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 103 | /* Binds ipv4/ipv6 address <local> to socket <fd>, unless <flags> is set, in which |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 104 | * case we try to bind <remote>. <flags> is a 2-bit field consisting of : |
| 105 | * - 0 : ignore remote address (may even be a NULL pointer) |
| 106 | * - 1 : use provided address |
| 107 | * - 2 : use provided port |
| 108 | * - 3 : use both |
| 109 | * |
| 110 | * The function supports multiple foreign binding methods : |
| 111 | * - linux_tproxy: we directly bind to the foreign address |
| 112 | * - cttproxy: we bind to a local address then nat. |
| 113 | * The second one can be used as a fallback for the first one. |
| 114 | * This function returns 0 when everything's OK, 1 if it could not bind, to the |
| 115 | * local address, 2 if it could not bind to the foreign address. |
| 116 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 117 | int tcp_bind_socket(int fd, int flags, struct sockaddr_storage *local, struct sockaddr_storage *remote) |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 118 | { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 119 | struct sockaddr_storage bind_addr; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 120 | int foreign_ok = 0; |
| 121 | int ret; |
| 122 | |
| 123 | #ifdef CONFIG_HAP_LINUX_TPROXY |
| 124 | static int ip_transp_working = 1; |
| 125 | if (flags && ip_transp_working) { |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 126 | if (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == 0 |
| 127 | || setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == 0) |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 128 | foreign_ok = 1; |
| 129 | else |
| 130 | ip_transp_working = 0; |
| 131 | } |
| 132 | #endif |
| 133 | if (flags) { |
| 134 | memset(&bind_addr, 0, sizeof(bind_addr)); |
Willy Tarreau | 96dd079 | 2011-04-19 07:20:57 +0200 | [diff] [blame] | 135 | bind_addr.ss_family = remote->ss_family; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 136 | switch (remote->ss_family) { |
| 137 | case AF_INET: |
| 138 | if (flags & 1) |
| 139 | ((struct sockaddr_in *)&bind_addr)->sin_addr = ((struct sockaddr_in *)remote)->sin_addr; |
| 140 | if (flags & 2) |
| 141 | ((struct sockaddr_in *)&bind_addr)->sin_port = ((struct sockaddr_in *)remote)->sin_port; |
| 142 | break; |
| 143 | case AF_INET6: |
| 144 | if (flags & 1) |
| 145 | ((struct sockaddr_in6 *)&bind_addr)->sin6_addr = ((struct sockaddr_in6 *)remote)->sin6_addr; |
| 146 | if (flags & 2) |
| 147 | ((struct sockaddr_in6 *)&bind_addr)->sin6_port = ((struct sockaddr_in6 *)remote)->sin6_port; |
| 148 | break; |
| 149 | } |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 152 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 153 | if (foreign_ok) { |
Willy Tarreau | 1b4b7ce | 2011-04-05 16:56:50 +0200 | [diff] [blame] | 154 | ret = bind(fd, (struct sockaddr *)&bind_addr, get_addr_len(&bind_addr)); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 155 | if (ret < 0) |
| 156 | return 2; |
| 157 | } |
| 158 | else { |
Willy Tarreau | 1b4b7ce | 2011-04-05 16:56:50 +0200 | [diff] [blame] | 159 | ret = bind(fd, (struct sockaddr *)local, get_addr_len(local)); |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 160 | if (ret < 0) |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | if (!flags) |
| 165 | return 0; |
| 166 | |
| 167 | #ifdef CONFIG_HAP_CTTPROXY |
Willy Tarreau | 6f831b4 | 2011-03-20 14:03:54 +0100 | [diff] [blame] | 168 | if (!foreign_ok && remote->ss_family == AF_INET) { |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 169 | struct in_tproxy itp1, itp2; |
| 170 | memset(&itp1, 0, sizeof(itp1)); |
| 171 | |
| 172 | itp1.op = TPROXY_ASSIGN; |
Willy Tarreau | 6f831b4 | 2011-03-20 14:03:54 +0100 | [diff] [blame] | 173 | itp1.v.addr.faddr = ((struct sockaddr_in *)&bind_addr)->sin_addr; |
| 174 | itp1.v.addr.fport = ((struct sockaddr_in *)&bind_addr)->sin_port; |
Willy Tarreau | e8c66af | 2008-01-13 18:40:14 +0100 | [diff] [blame] | 175 | |
| 176 | /* set connect flag on socket */ |
| 177 | itp2.op = TPROXY_FLAGS; |
| 178 | itp2.v.flags = ITP_CONNECT | ITP_ONCE; |
| 179 | |
| 180 | if (setsockopt(fd, SOL_IP, IP_TPROXY, &itp1, sizeof(itp1)) != -1 && |
| 181 | setsockopt(fd, SOL_IP, IP_TPROXY, &itp2, sizeof(itp2)) != -1) { |
| 182 | foreign_ok = 1; |
| 183 | } |
| 184 | } |
| 185 | #endif |
| 186 | if (!foreign_ok) |
| 187 | /* we could not bind to a foreign address */ |
| 188 | return 2; |
| 189 | |
| 190 | return 0; |
| 191 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 192 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 193 | |
| 194 | /* |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 195 | * This function initiates a connection to the target assigned to this session |
| 196 | * (si->{target,addr.s.to}). A source address may be pointed to by si->addr.s.from |
| 197 | * in case of transparent proxying. Normal source bind addresses are still |
| 198 | * determined locally (due to the possible need of a source port). |
| 199 | * si->target may point either to a valid server or to a backend, depending |
| 200 | * on si->target.type. Only TARG_TYPE_PROXY and TARG_TYPE_SERVER are supported. |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 201 | * |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 202 | * It can return one of : |
| 203 | * - SN_ERR_NONE if everything's OK |
| 204 | * - SN_ERR_SRVTO if there are no more servers |
| 205 | * - SN_ERR_SRVCL if the connection was refused by the server |
| 206 | * - SN_ERR_PRXCOND if the connection has been limited by the proxy (maxconn) |
| 207 | * - SN_ERR_RESOURCE if a system resource is lacking (eg: fd limits, ports, ...) |
| 208 | * - SN_ERR_INTERNAL for any other purely internal errors |
| 209 | * Additionnally, in the case of SN_ERR_RESOURCE, an emergency log will be emitted. |
| 210 | */ |
Willy Tarreau | f153686 | 2011-03-03 18:27:32 +0100 | [diff] [blame] | 211 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 212 | int tcp_connect_server(struct stream_interface *si) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 213 | { |
| 214 | int fd; |
Willy Tarreau | ac82540 | 2011-03-04 22:04:29 +0100 | [diff] [blame] | 215 | struct server *srv; |
| 216 | struct proxy *be; |
| 217 | |
| 218 | switch (si->target.type) { |
| 219 | case TARG_TYPE_PROXY: |
| 220 | be = si->target.ptr.p; |
| 221 | srv = NULL; |
| 222 | break; |
| 223 | case TARG_TYPE_SERVER: |
| 224 | srv = si->target.ptr.s; |
| 225 | be = srv->proxy; |
| 226 | break; |
| 227 | default: |
| 228 | return SN_ERR_INTERNAL; |
| 229 | } |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 230 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 231 | if ((fd = si->fd = socket(si->addr.s.to.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 232 | qfprintf(stderr, "Cannot get a server socket.\n"); |
| 233 | |
| 234 | if (errno == ENFILE) |
| 235 | send_log(be, LOG_EMERG, |
| 236 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 237 | be->id, maxfd); |
| 238 | else if (errno == EMFILE) |
| 239 | send_log(be, LOG_EMERG, |
| 240 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 241 | be->id, maxfd); |
| 242 | else if (errno == ENOBUFS || errno == ENOMEM) |
| 243 | send_log(be, LOG_EMERG, |
| 244 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 245 | be->id, maxfd); |
| 246 | /* this is a resource error */ |
| 247 | return SN_ERR_RESOURCE; |
| 248 | } |
| 249 | |
| 250 | if (fd >= global.maxsock) { |
| 251 | /* do not log anything there, it's a normal condition when this option |
| 252 | * is used to serialize connections to a server ! |
| 253 | */ |
| 254 | Alert("socket(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 255 | close(fd); |
| 256 | return SN_ERR_PRXCOND; /* it is a configuration limit */ |
| 257 | } |
| 258 | |
| 259 | if ((fcntl(fd, F_SETFL, O_NONBLOCK)==-1) || |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 260 | (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)) == -1)) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 261 | qfprintf(stderr,"Cannot set client socket to non blocking mode.\n"); |
| 262 | close(fd); |
| 263 | return SN_ERR_INTERNAL; |
| 264 | } |
| 265 | |
| 266 | if (be->options & PR_O_TCP_SRV_KA) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 267 | setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 268 | |
| 269 | if (be->options & PR_O_TCP_NOLING) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 270 | setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 271 | |
| 272 | /* allow specific binding : |
| 273 | * - server-specific at first |
| 274 | * - proxy-specific next |
| 275 | */ |
| 276 | if (srv != NULL && srv->state & SRV_BIND_SRC) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 277 | int ret, flags = 0; |
| 278 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 279 | switch (srv->state & SRV_TPROXY_MASK) { |
| 280 | case SRV_TPROXY_ADDR: |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 281 | case SRV_TPROXY_CLI: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 282 | flags = 3; |
| 283 | break; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 284 | case SRV_TPROXY_CIP: |
Willy Tarreau | 090466c | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 285 | case SRV_TPROXY_DYN: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 286 | flags = 1; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 287 | break; |
| 288 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 289 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 290 | #ifdef SO_BINDTODEVICE |
| 291 | /* Note: this might fail if not CAP_NET_RAW */ |
| 292 | if (srv->iface_name) |
| 293 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, srv->iface_name, srv->iface_len + 1); |
| 294 | #endif |
| 295 | |
| 296 | if (srv->sport_range) { |
| 297 | int attempts = 10; /* should be more than enough to find a spare port */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 298 | struct sockaddr_storage src; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 299 | |
| 300 | ret = 1; |
| 301 | src = srv->source_addr; |
| 302 | |
| 303 | do { |
| 304 | /* note: in case of retry, we may have to release a previously |
| 305 | * allocated port, hence this loop's construct. |
| 306 | */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 307 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 308 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 309 | |
| 310 | if (!attempts) |
| 311 | break; |
| 312 | attempts--; |
| 313 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 314 | fdinfo[fd].local_port = port_range_alloc_port(srv->sport_range); |
| 315 | if (!fdinfo[fd].local_port) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 316 | break; |
| 317 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 318 | fdinfo[fd].port_range = srv->sport_range; |
Willy Tarreau | 86ad42c | 2011-08-27 12:29:07 +0200 | [diff] [blame] | 319 | set_host_port(&src, fdinfo[fd].local_port); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 320 | |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 321 | ret = tcp_bind_socket(fd, flags, &src, &si->addr.s.from); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 322 | } while (ret != 0); /* binding NOK */ |
| 323 | } |
| 324 | else { |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 325 | ret = tcp_bind_socket(fd, flags, &srv->source_addr, &si->addr.s.from); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | if (ret) { |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 329 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 330 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 331 | close(fd); |
| 332 | |
| 333 | if (ret == 1) { |
| 334 | Alert("Cannot bind to source address before connect() for server %s/%s. Aborting.\n", |
| 335 | be->id, srv->id); |
| 336 | send_log(be, LOG_EMERG, |
| 337 | "Cannot bind to source address before connect() for server %s/%s.\n", |
| 338 | be->id, srv->id); |
| 339 | } else { |
| 340 | Alert("Cannot bind to tproxy source address before connect() for server %s/%s. Aborting.\n", |
| 341 | be->id, srv->id); |
| 342 | send_log(be, LOG_EMERG, |
| 343 | "Cannot bind to tproxy source address before connect() for server %s/%s.\n", |
| 344 | be->id, srv->id); |
| 345 | } |
| 346 | return SN_ERR_RESOURCE; |
| 347 | } |
| 348 | } |
| 349 | else if (be->options & PR_O_BIND_SRC) { |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 350 | int ret, flags = 0; |
| 351 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 352 | switch (be->options & PR_O_TPXY_MASK) { |
| 353 | case PR_O_TPXY_ADDR: |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 354 | case PR_O_TPXY_CLI: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 355 | flags = 3; |
| 356 | break; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 357 | case PR_O_TPXY_CIP: |
Willy Tarreau | 090466c | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 358 | case PR_O_TPXY_DYN: |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 359 | flags = 1; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 360 | break; |
| 361 | } |
Willy Tarreau | b1d6774 | 2010-03-29 19:36:59 +0200 | [diff] [blame] | 362 | |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 363 | #ifdef SO_BINDTODEVICE |
| 364 | /* Note: this might fail if not CAP_NET_RAW */ |
| 365 | if (be->iface_name) |
| 366 | setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, be->iface_name, be->iface_len + 1); |
| 367 | #endif |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 368 | ret = tcp_bind_socket(fd, flags, &be->source_addr, &si->addr.s.from); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 369 | if (ret) { |
| 370 | close(fd); |
| 371 | if (ret == 1) { |
| 372 | Alert("Cannot bind to source address before connect() for proxy %s. Aborting.\n", |
| 373 | be->id); |
| 374 | send_log(be, LOG_EMERG, |
| 375 | "Cannot bind to source address before connect() for proxy %s.\n", |
| 376 | be->id); |
| 377 | } else { |
| 378 | Alert("Cannot bind to tproxy source address before connect() for proxy %s. Aborting.\n", |
| 379 | be->id); |
| 380 | send_log(be, LOG_EMERG, |
| 381 | "Cannot bind to tproxy source address before connect() for proxy %s.\n", |
| 382 | be->id); |
| 383 | } |
| 384 | return SN_ERR_RESOURCE; |
| 385 | } |
| 386 | } |
| 387 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 388 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 389 | /* disabling tcp quick ack now allows the first request to leave the |
| 390 | * machine with the first ACK. We only do this if there are pending |
| 391 | * data in the buffer. |
| 392 | */ |
| 393 | if ((be->options2 & PR_O2_SMARTCON) && si->ob->send_max) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 394 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero)); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 395 | #endif |
| 396 | |
Willy Tarreau | e803de2 | 2010-01-21 17:43:04 +0100 | [diff] [blame] | 397 | if (global.tune.server_sndbuf) |
| 398 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &global.tune.server_sndbuf, sizeof(global.tune.server_sndbuf)); |
| 399 | |
| 400 | if (global.tune.server_rcvbuf) |
| 401 | setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &global.tune.server_rcvbuf, sizeof(global.tune.server_rcvbuf)); |
| 402 | |
Willy Tarreau | 1b4b7ce | 2011-04-05 16:56:50 +0200 | [diff] [blame] | 403 | if ((connect(fd, (struct sockaddr *)&si->addr.s.to, get_addr_len(&si->addr.s.to)) == -1) && |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 404 | (errno != EINPROGRESS) && (errno != EALREADY) && (errno != EISCONN)) { |
| 405 | |
| 406 | if (errno == EAGAIN || errno == EADDRINUSE) { |
| 407 | char *msg; |
| 408 | if (errno == EAGAIN) /* no free ports left, try again later */ |
| 409 | msg = "no free ports"; |
| 410 | else |
| 411 | msg = "local address already in use"; |
| 412 | |
| 413 | qfprintf(stderr,"Cannot connect: %s.\n",msg); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 414 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 415 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 416 | close(fd); |
| 417 | send_log(be, LOG_EMERG, |
| 418 | "Connect() failed for server %s/%s: %s.\n", |
| 419 | be->id, srv->id, msg); |
| 420 | return SN_ERR_RESOURCE; |
| 421 | } else if (errno == ETIMEDOUT) { |
| 422 | //qfprintf(stderr,"Connect(): ETIMEDOUT"); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 423 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 424 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 425 | close(fd); |
| 426 | return SN_ERR_SRVTO; |
| 427 | } else { |
| 428 | // (errno == ECONNREFUSED || errno == ENETUNREACH || errno == EACCES || errno == EPERM) |
| 429 | //qfprintf(stderr,"Connect(): %d", errno); |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 430 | port_range_release_port(fdinfo[fd].port_range, fdinfo[fd].local_port); |
| 431 | fdinfo[fd].port_range = NULL; |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 432 | close(fd); |
| 433 | return SN_ERR_SRVCL; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | fdtab[fd].owner = si; |
| 438 | fdtab[fd].state = FD_STCONN; /* connection in progress */ |
| 439 | fdtab[fd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY; |
| 440 | fdtab[fd].cb[DIR_RD].f = &stream_sock_read; |
| 441 | fdtab[fd].cb[DIR_RD].b = si->ib; |
| 442 | fdtab[fd].cb[DIR_WR].f = &stream_sock_write; |
| 443 | fdtab[fd].cb[DIR_WR].b = si->ob; |
| 444 | |
Willy Tarreau | f153686 | 2011-03-03 18:27:32 +0100 | [diff] [blame] | 445 | fdinfo[fd].peeraddr = (struct sockaddr *)&si->addr.s.to; |
Willy Tarreau | 1b4b7ce | 2011-04-05 16:56:50 +0200 | [diff] [blame] | 446 | fdinfo[fd].peerlen = get_addr_len(&si->addr.s.to); |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 447 | |
| 448 | fd_insert(fd); |
| 449 | EV_FD_SET(fd, DIR_WR); /* for connect status */ |
| 450 | |
| 451 | si->state = SI_ST_CON; |
| 452 | si->flags |= SI_FL_CAP_SPLTCP; /* TCP supports splicing */ |
| 453 | si->exp = tick_add_ifset(now_ms, be->timeout.connect); |
| 454 | |
| 455 | return SN_ERR_NONE; /* connection is OK */ |
| 456 | } |
| 457 | |
| 458 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 459 | /* This function tries to bind a TCPv4/v6 listener. It may return a warning or |
| 460 | * an error message in <err> if the message is at most <errlen> bytes long |
| 461 | * (including '\0'). The return value is composed from ERR_ABORT, ERR_WARN, |
| 462 | * ERR_ALERT, ERR_RETRYABLE and ERR_FATAL. ERR_NONE indicates that everything |
| 463 | * was alright and that no message was returned. ERR_RETRYABLE means that an |
| 464 | * error occurred but that it may vanish after a retry (eg: port in use), and |
| 465 | * ERR_FATAL indicates a non-fixable error.ERR_WARN and ERR_ALERT do not alter |
| 466 | * the meaning of the error, but just indicate that a message is present which |
| 467 | * should be displayed with the respective level. Last, ERR_ABORT indicates |
| 468 | * that it's pointless to try to start other listeners. No error message is |
| 469 | * returned if errlen is NULL. |
| 470 | */ |
| 471 | int tcp_bind_listener(struct listener *listener, char *errmsg, int errlen) |
| 472 | { |
| 473 | __label__ tcp_return, tcp_close_return; |
| 474 | int fd, err; |
| 475 | const char *msg = NULL; |
| 476 | |
| 477 | /* ensure we never return garbage */ |
| 478 | if (errmsg && errlen) |
| 479 | *errmsg = 0; |
| 480 | |
| 481 | if (listener->state != LI_ASSIGNED) |
| 482 | return ERR_NONE; /* already bound */ |
| 483 | |
| 484 | err = ERR_NONE; |
| 485 | |
| 486 | if ((fd = socket(listener->addr.ss_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { |
| 487 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 488 | msg = "cannot create listening socket"; |
| 489 | goto tcp_return; |
| 490 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 491 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 492 | if (fd >= global.maxsock) { |
| 493 | err |= ERR_FATAL | ERR_ABORT | ERR_ALERT; |
| 494 | msg = "not enough free sockets (raise '-n' parameter)"; |
| 495 | goto tcp_close_return; |
| 496 | } |
| 497 | |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 498 | if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 499 | err |= ERR_FATAL | ERR_ALERT; |
| 500 | msg = "cannot make socket non-blocking"; |
| 501 | goto tcp_close_return; |
| 502 | } |
| 503 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 504 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 505 | /* not fatal but should be reported */ |
| 506 | msg = "cannot do so_reuseaddr"; |
| 507 | err |= ERR_ALERT; |
| 508 | } |
| 509 | |
| 510 | if (listener->options & LI_O_NOLINGER) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 511 | setsockopt(fd, SOL_SOCKET, SO_LINGER, &nolinger, sizeof(struct linger)); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 512 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 513 | #ifdef SO_REUSEPORT |
| 514 | /* OpenBSD supports this. As it's present in old libc versions of Linux, |
| 515 | * it might return an error that we will silently ignore. |
| 516 | */ |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 517 | setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 518 | #endif |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 519 | #ifdef CONFIG_HAP_LINUX_TPROXY |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 520 | if ((listener->options & LI_O_FOREIGN) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 521 | && (setsockopt(fd, SOL_IP, IP_TRANSPARENT, &one, sizeof(one)) == -1) |
| 522 | && (setsockopt(fd, SOL_IP, IP_FREEBIND, &one, sizeof(one)) == -1)) { |
Willy Tarreau | b1e52e8 | 2008-01-13 14:49:51 +0100 | [diff] [blame] | 523 | msg = "cannot make listening socket transparent"; |
| 524 | err |= ERR_ALERT; |
| 525 | } |
| 526 | #endif |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 527 | #ifdef SO_BINDTODEVICE |
| 528 | /* Note: this might fail if not CAP_NET_RAW */ |
| 529 | if (listener->interface) { |
| 530 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, |
Willy Tarreau | 604e830 | 2009-03-06 00:48:23 +0100 | [diff] [blame] | 531 | listener->interface, strlen(listener->interface) + 1) == -1) { |
Willy Tarreau | 5e6e204 | 2009-02-04 17:19:29 +0100 | [diff] [blame] | 532 | msg = "cannot bind listener to device"; |
| 533 | err |= ERR_WARN; |
| 534 | } |
| 535 | } |
| 536 | #endif |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 537 | #if defined(TCP_MAXSEG) |
Willy Tarreau | 48a7e72 | 2010-12-24 15:26:39 +0100 | [diff] [blame] | 538 | if (listener->maxseg > 0) { |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 539 | if (setsockopt(fd, IPPROTO_TCP, TCP_MAXSEG, |
Willy Tarreau | be1b918 | 2009-06-14 18:48:19 +0200 | [diff] [blame] | 540 | &listener->maxseg, sizeof(listener->maxseg)) == -1) { |
| 541 | msg = "cannot set MSS"; |
| 542 | err |= ERR_WARN; |
| 543 | } |
| 544 | } |
| 545 | #endif |
Willy Tarreau | cb6cd43 | 2009-10-13 07:34:14 +0200 | [diff] [blame] | 546 | #if defined(TCP_DEFER_ACCEPT) |
| 547 | if (listener->options & LI_O_DEF_ACCEPT) { |
| 548 | /* defer accept by up to one second */ |
| 549 | int accept_delay = 1; |
| 550 | if (setsockopt(fd, IPPROTO_TCP, TCP_DEFER_ACCEPT, &accept_delay, sizeof(accept_delay)) == -1) { |
| 551 | msg = "cannot enable DEFER_ACCEPT"; |
| 552 | err |= ERR_WARN; |
| 553 | } |
| 554 | } |
| 555 | #endif |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 556 | if (bind(fd, (struct sockaddr *)&listener->addr, listener->proto->sock_addrlen) == -1) { |
| 557 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 558 | msg = "cannot bind socket"; |
| 559 | goto tcp_close_return; |
| 560 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 561 | |
Willy Tarreau | c73ce2b | 2008-01-06 10:55:10 +0100 | [diff] [blame] | 562 | if (listen(fd, listener->backlog ? listener->backlog : listener->maxconn) == -1) { |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 563 | err |= ERR_RETRYABLE | ERR_ALERT; |
| 564 | msg = "cannot listen to socket"; |
| 565 | goto tcp_close_return; |
| 566 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 567 | |
Dmitry Sivachenko | caf5898 | 2009-08-24 15:11:06 +0400 | [diff] [blame] | 568 | #if defined(TCP_QUICKACK) |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 569 | if (listener->options & LI_O_NOQUICKACK) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 570 | setsockopt(fd, IPPROTO_TCP, TCP_QUICKACK, &zero, sizeof(zero)); |
Willy Tarreau | 9ea05a7 | 2009-06-14 12:07:01 +0200 | [diff] [blame] | 571 | #endif |
| 572 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 573 | /* the socket is ready */ |
| 574 | listener->fd = fd; |
| 575 | listener->state = LI_LISTEN; |
| 576 | |
Willy Tarreau | eabf313 | 2008-08-29 23:36:51 +0200 | [diff] [blame] | 577 | fdtab[fd].owner = listener; /* reference the listener instead of a task */ |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 578 | fdtab[fd].state = FD_STLISTEN; |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 579 | fdtab[fd].flags = FD_FL_TCP | ((listener->options & LI_O_NOLINGER) ? FD_FL_TCP_NOLING : 0); |
| 580 | fdtab[fd].cb[DIR_RD].f = listener->proto->accept; |
| 581 | fdtab[fd].cb[DIR_WR].f = NULL; /* never called */ |
| 582 | fdtab[fd].cb[DIR_RD].b = fdtab[fd].cb[DIR_WR].b = NULL; |
Willy Tarreau | 5d707e1 | 2009-06-28 11:09:07 +0200 | [diff] [blame] | 583 | |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 584 | fdinfo[fd].peeraddr = NULL; |
| 585 | fdinfo[fd].peerlen = 0; |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 586 | fd_insert(fd); |
| 587 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 588 | tcp_return: |
Cyril Bonté | 43ba1b3 | 2010-11-01 19:26:01 +0100 | [diff] [blame] | 589 | if (msg && errlen) { |
| 590 | char pn[INET6_ADDRSTRLEN]; |
| 591 | |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 592 | addr_to_str(&listener->addr, pn, sizeof(pn)); |
| 593 | snprintf(errmsg, errlen, "%s [%s:%d]", msg, pn, get_host_port(&listener->addr)); |
Cyril Bonté | 43ba1b3 | 2010-11-01 19:26:01 +0100 | [diff] [blame] | 594 | } |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 595 | return err; |
| 596 | |
| 597 | tcp_close_return: |
| 598 | close(fd); |
| 599 | goto tcp_return; |
| 600 | } |
| 601 | |
| 602 | /* This function creates all TCP sockets bound to the protocol entry <proto>. |
| 603 | * It is intended to be used as the protocol's bind_all() function. |
| 604 | * The sockets will be registered but not added to any fd_set, in order not to |
| 605 | * loose them across the fork(). A call to enable_all_listeners() is needed |
| 606 | * to complete initialization. The return value is composed from ERR_*. |
| 607 | */ |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 608 | static int tcp_bind_listeners(struct protocol *proto, char *errmsg, int errlen) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 609 | { |
| 610 | struct listener *listener; |
| 611 | int err = ERR_NONE; |
| 612 | |
| 613 | list_for_each_entry(listener, &proto->listeners, proto_list) { |
Emeric Brun | cf20bf1 | 2010-10-22 16:06:11 +0200 | [diff] [blame] | 614 | err |= tcp_bind_listener(listener, errmsg, errlen); |
| 615 | if (err & ERR_ABORT) |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 616 | break; |
| 617 | } |
| 618 | |
| 619 | return err; |
| 620 | } |
| 621 | |
| 622 | /* Add listener to the list of tcpv4 listeners. The listener's state |
| 623 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 624 | * listeners is updated. This is the function to use to add a new listener. |
| 625 | */ |
| 626 | void tcpv4_add_listener(struct listener *listener) |
| 627 | { |
| 628 | if (listener->state != LI_INIT) |
| 629 | return; |
| 630 | listener->state = LI_ASSIGNED; |
| 631 | listener->proto = &proto_tcpv4; |
| 632 | LIST_ADDQ(&proto_tcpv4.listeners, &listener->proto_list); |
| 633 | proto_tcpv4.nb_listeners++; |
| 634 | } |
| 635 | |
| 636 | /* Add listener to the list of tcpv4 listeners. The listener's state |
| 637 | * is automatically updated from LI_INIT to LI_ASSIGNED. The number of |
| 638 | * listeners is updated. This is the function to use to add a new listener. |
| 639 | */ |
| 640 | void tcpv6_add_listener(struct listener *listener) |
| 641 | { |
| 642 | if (listener->state != LI_INIT) |
| 643 | return; |
| 644 | listener->state = LI_ASSIGNED; |
| 645 | listener->proto = &proto_tcpv6; |
| 646 | LIST_ADDQ(&proto_tcpv6.listeners, &listener->proto_list); |
| 647 | proto_tcpv6.nb_listeners++; |
| 648 | } |
| 649 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 650 | /* This function performs the TCP request analysis on the current request. It |
| 651 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 652 | * needs more data, encounters an error, or wants to immediately abort the |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 653 | * request. It relies on buffers flags, and updates s->req->analysers. The |
| 654 | * function may be called for frontend rules and backend rules. It only relies |
| 655 | * on the backend pointer so this works for both cases. |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 656 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 657 | 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] | 658 | { |
| 659 | struct tcp_rule *rule; |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 660 | struct stksess *ts; |
| 661 | struct stktable *t; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 662 | int partial; |
| 663 | |
| 664 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 665 | now_ms, __FUNCTION__, |
| 666 | s, |
| 667 | req, |
| 668 | req->rex, req->wex, |
| 669 | req->flags, |
| 670 | req->l, |
| 671 | req->analysers); |
| 672 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 673 | /* We don't know whether we have enough data, so must proceed |
| 674 | * this way : |
| 675 | * - iterate through all rules in their declaration order |
| 676 | * - if one rule returns MISS, it means the inspect delay is |
| 677 | * not over yet, then return immediately, otherwise consider |
| 678 | * it as a non-match. |
| 679 | * - if one rule returns OK, then return OK |
| 680 | * - if one rule returns KO, then return KO |
| 681 | */ |
| 682 | |
Willy Tarreau | b824b00 | 2010-09-29 16:36:16 +0200 | [diff] [blame] | 683 | if (req->flags & (BF_SHUTR|BF_FULL) || !s->be->tcp_req.inspect_delay || tick_is_expired(req->analyse_exp, now_ms)) |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 684 | partial = 0; |
| 685 | else |
| 686 | partial = ACL_PARTIAL; |
| 687 | |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 688 | list_for_each_entry(rule, &s->be->tcp_req.inspect_rules, list) { |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 689 | int ret = ACL_PAT_PASS; |
| 690 | |
| 691 | if (rule->cond) { |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 692 | ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_REQ | partial); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 693 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 694 | buffer_dont_connect(req); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 695 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | fb35620 | 2010-08-03 14:02:05 +0200 | [diff] [blame] | 696 | if (!tick_isset(req->analyse_exp) && s->be->tcp_req.inspect_delay) |
| 697 | req->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_req.inspect_delay); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | ret = acl_pass(ret); |
| 702 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 703 | ret = !ret; |
| 704 | } |
| 705 | |
| 706 | if (ret) { |
| 707 | /* we have a matching rule. */ |
| 708 | if (rule->action == TCP_ACT_REJECT) { |
| 709 | buffer_abort(req); |
| 710 | buffer_abort(s->rep); |
| 711 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 712 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 713 | s->be->be_counters.denied_req++; |
| 714 | s->fe->fe_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 715 | if (s->listener->counters) |
Willy Tarreau | 23968d8 | 2010-05-23 23:50:44 +0200 | [diff] [blame] | 716 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 717 | |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 718 | if (!(s->flags & SN_ERR_MASK)) |
| 719 | s->flags |= SN_ERR_PRXCOND; |
| 720 | if (!(s->flags & SN_FINST_MASK)) |
| 721 | s->flags |= SN_FINST_R; |
| 722 | return 0; |
| 723 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 724 | else if (rule->action == TCP_ACT_TRK_SC1) { |
| 725 | if (!s->stkctr1_entry) { |
| 726 | /* only the first valid track-sc1 directive applies. |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 727 | * Also, note that right now we can only track SRC so we |
| 728 | * don't check how to get the key, but later we may need |
| 729 | * to consider rule->act_prm->trk_ctr.type. |
| 730 | */ |
| 731 | t = rule->act_prm.trk_ctr.table.t; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 732 | ts = stktable_get_entry(t, tcp_src_to_stktable_key(s)); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 733 | if (ts) { |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 734 | session_track_stkctr1(s, t, ts); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 735 | if (s->fe != s->be) |
| 736 | s->flags |= SN_BE_TRACK_SC1; |
| 737 | } |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 738 | } |
| 739 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 740 | else if (rule->action == TCP_ACT_TRK_SC2) { |
| 741 | if (!s->stkctr2_entry) { |
| 742 | /* only the first valid track-sc2 directive applies. |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 743 | * Also, note that right now we can only track SRC so we |
| 744 | * don't check how to get the key, but later we may need |
| 745 | * to consider rule->act_prm->trk_ctr.type. |
| 746 | */ |
| 747 | t = rule->act_prm.trk_ctr.table.t; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 748 | ts = stktable_get_entry(t, tcp_src_to_stktable_key(s)); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 749 | if (ts) { |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 750 | session_track_stkctr2(s, t, ts); |
Willy Tarreau | 0a4838c | 2010-08-06 20:11:05 +0200 | [diff] [blame] | 751 | if (s->fe != s->be) |
| 752 | s->flags |= SN_BE_TRACK_SC2; |
| 753 | } |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | else { |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 757 | /* otherwise accept */ |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 758 | break; |
| 759 | } |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | /* if we get there, it means we have no rule which matches, or |
| 764 | * we have an explicit accept, so we apply the default accept. |
| 765 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 766 | req->analysers &= ~an_bit; |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 767 | req->analyse_exp = TICK_ETERNITY; |
| 768 | return 1; |
| 769 | } |
| 770 | |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 771 | /* This function performs the TCP response analysis on the current response. It |
| 772 | * returns 1 if the processing can continue on next analysers, or zero if it |
| 773 | * needs more data, encounters an error, or wants to immediately abort the |
| 774 | * response. It relies on buffers flags, and updates s->rep->analysers. The |
| 775 | * function may be called for backend rules. |
| 776 | */ |
| 777 | int tcp_inspect_response(struct session *s, struct buffer *rep, int an_bit) |
| 778 | { |
| 779 | struct tcp_rule *rule; |
| 780 | int partial; |
| 781 | |
| 782 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 783 | now_ms, __FUNCTION__, |
| 784 | s, |
| 785 | rep, |
| 786 | rep->rex, rep->wex, |
| 787 | rep->flags, |
| 788 | rep->l, |
| 789 | rep->analysers); |
| 790 | |
| 791 | /* We don't know whether we have enough data, so must proceed |
| 792 | * this way : |
| 793 | * - iterate through all rules in their declaration order |
| 794 | * - if one rule returns MISS, it means the inspect delay is |
| 795 | * not over yet, then return immediately, otherwise consider |
| 796 | * it as a non-match. |
| 797 | * - if one rule returns OK, then return OK |
| 798 | * - if one rule returns KO, then return KO |
| 799 | */ |
| 800 | |
| 801 | if (rep->flags & BF_SHUTR || tick_is_expired(rep->analyse_exp, now_ms)) |
| 802 | partial = 0; |
| 803 | else |
| 804 | partial = ACL_PARTIAL; |
| 805 | |
| 806 | list_for_each_entry(rule, &s->be->tcp_rep.inspect_rules, list) { |
| 807 | int ret = ACL_PAT_PASS; |
| 808 | |
| 809 | if (rule->cond) { |
| 810 | ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, ACL_DIR_RTR | partial); |
| 811 | if (ret == ACL_PAT_MISS) { |
| 812 | /* just set the analyser timeout once at the beginning of the response */ |
| 813 | if (!tick_isset(rep->analyse_exp) && s->be->tcp_rep.inspect_delay) |
| 814 | rep->analyse_exp = tick_add_ifset(now_ms, s->be->tcp_rep.inspect_delay); |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | ret = acl_pass(ret); |
| 819 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 820 | ret = !ret; |
| 821 | } |
| 822 | |
| 823 | if (ret) { |
| 824 | /* we have a matching rule. */ |
| 825 | if (rule->action == TCP_ACT_REJECT) { |
| 826 | buffer_abort(rep); |
| 827 | buffer_abort(s->req); |
| 828 | rep->analysers = 0; |
| 829 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 830 | s->be->be_counters.denied_resp++; |
| 831 | s->fe->fe_counters.denied_resp++; |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 832 | if (s->listener->counters) |
| 833 | s->listener->counters->denied_resp++; |
| 834 | |
| 835 | if (!(s->flags & SN_ERR_MASK)) |
| 836 | s->flags |= SN_ERR_PRXCOND; |
| 837 | if (!(s->flags & SN_FINST_MASK)) |
| 838 | s->flags |= SN_FINST_D; |
| 839 | return 0; |
| 840 | } |
| 841 | else { |
| 842 | /* otherwise accept */ |
| 843 | break; |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* if we get there, it means we have no rule which matches, or |
| 849 | * we have an explicit accept, so we apply the default accept. |
| 850 | */ |
| 851 | rep->analysers &= ~an_bit; |
| 852 | rep->analyse_exp = TICK_ETERNITY; |
| 853 | return 1; |
| 854 | } |
| 855 | |
| 856 | |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 857 | /* This function performs the TCP layer4 analysis on the current request. It |
| 858 | * returns 0 if a reject rule matches, otherwise 1 if either an accept rule |
| 859 | * matches or if no more rule matches. It can only use rules which don't need |
| 860 | * any data. |
| 861 | */ |
| 862 | int tcp_exec_req_rules(struct session *s) |
| 863 | { |
| 864 | struct tcp_rule *rule; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 865 | struct stksess *ts; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 866 | struct stktable *t = NULL; |
| 867 | int result = 1; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 868 | int ret; |
| 869 | |
| 870 | list_for_each_entry(rule, &s->fe->tcp_req.l4_rules, list) { |
| 871 | ret = ACL_PAT_PASS; |
| 872 | |
| 873 | if (rule->cond) { |
| 874 | ret = acl_exec_cond(rule->cond, s->fe, s, NULL, ACL_DIR_REQ); |
| 875 | ret = acl_pass(ret); |
| 876 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 877 | ret = !ret; |
| 878 | } |
| 879 | |
| 880 | if (ret) { |
| 881 | /* we have a matching rule. */ |
| 882 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 883 | s->fe->fe_counters.denied_conn++; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 884 | if (s->listener->counters) |
Willy Tarreau | 2799e98 | 2010-06-05 15:43:21 +0200 | [diff] [blame] | 885 | s->listener->counters->denied_conn++; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 886 | |
| 887 | if (!(s->flags & SN_ERR_MASK)) |
| 888 | s->flags |= SN_ERR_PRXCOND; |
| 889 | if (!(s->flags & SN_FINST_MASK)) |
| 890 | s->flags |= SN_FINST_R; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 891 | result = 0; |
| 892 | break; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 893 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 894 | else if (rule->action == TCP_ACT_TRK_SC1) { |
| 895 | if (!s->stkctr1_entry) { |
| 896 | /* only the first valid track-sc1 directive applies. |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 897 | * Also, note that right now we can only track SRC so we |
| 898 | * don't check how to get the key, but later we may need |
| 899 | * to consider rule->act_prm->trk_ctr.type. |
| 900 | */ |
| 901 | t = rule->act_prm.trk_ctr.table.t; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 902 | ts = stktable_get_entry(t, tcp_src_to_stktable_key(s)); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 903 | if (ts) |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 904 | session_track_stkctr1(s, t, ts); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 905 | } |
| 906 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 907 | else if (rule->action == TCP_ACT_TRK_SC2) { |
| 908 | if (!s->stkctr2_entry) { |
| 909 | /* only the first valid track-sc2 directive applies. |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 910 | * Also, note that right now we can only track SRC so we |
| 911 | * don't check how to get the key, but later we may need |
| 912 | * to consider rule->act_prm->trk_ctr.type. |
| 913 | */ |
| 914 | t = rule->act_prm.trk_ctr.table.t; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 915 | ts = stktable_get_entry(t, tcp_src_to_stktable_key(s)); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 916 | if (ts) |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 917 | session_track_stkctr2(s, t, ts); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 918 | } |
| 919 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 920 | else { |
| 921 | /* otherwise it's an accept */ |
| 922 | break; |
| 923 | } |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 924 | } |
| 925 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 926 | return result; |
Willy Tarreau | a5c0ab2 | 2010-05-31 10:30:33 +0200 | [diff] [blame] | 927 | } |
| 928 | |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 929 | /* Parse a tcp-response rule. Return a negative value in case of failure */ |
| 930 | static int tcp_parse_response_rule(char **args, int arg, int section_type, |
| 931 | struct proxy *curpx, struct proxy *defpx, |
| 932 | struct tcp_rule *rule, char *err, int errlen) |
| 933 | { |
| 934 | if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) { |
| 935 | snprintf(err, errlen, "%s %s is only allowed in 'backend' sections", |
| 936 | args[0], args[1]); |
| 937 | return -1; |
| 938 | } |
| 939 | |
| 940 | if (strcmp(args[arg], "accept") == 0) { |
| 941 | arg++; |
| 942 | rule->action = TCP_ACT_ACCEPT; |
| 943 | } |
| 944 | else if (strcmp(args[arg], "reject") == 0) { |
| 945 | arg++; |
| 946 | rule->action = TCP_ACT_REJECT; |
| 947 | } |
| 948 | else { |
| 949 | snprintf(err, errlen, |
| 950 | "'%s %s' expects 'accept' or 'reject' in %s '%s' (was '%s')", |
| 951 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]); |
| 952 | return -1; |
| 953 | } |
| 954 | |
| 955 | if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) { |
| 956 | if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) { |
| 957 | snprintf(err, errlen, |
| 958 | "error detected in %s '%s' while parsing '%s' condition", |
| 959 | proxy_type_str(curpx), curpx->id, args[arg]); |
| 960 | return -1; |
| 961 | } |
| 962 | } |
| 963 | else if (*args[arg]) { |
| 964 | snprintf(err, errlen, |
| 965 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')", |
| 966 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]); |
| 967 | return -1; |
| 968 | } |
| 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | |
| 973 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 974 | /* Parse a tcp-request rule. Return a negative value in case of failure */ |
| 975 | static int tcp_parse_request_rule(char **args, int arg, int section_type, |
| 976 | struct proxy *curpx, struct proxy *defpx, |
| 977 | struct tcp_rule *rule, char *err, int errlen) |
| 978 | { |
| 979 | if (curpx == defpx) { |
| 980 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 981 | args[0], args[1]); |
| 982 | return -1; |
| 983 | } |
| 984 | |
| 985 | if (!strcmp(args[arg], "accept")) { |
| 986 | arg++; |
| 987 | rule->action = TCP_ACT_ACCEPT; |
| 988 | } |
| 989 | else if (!strcmp(args[arg], "reject")) { |
| 990 | arg++; |
| 991 | rule->action = TCP_ACT_REJECT; |
| 992 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 993 | else if (strcmp(args[arg], "track-sc1") == 0) { |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 994 | int ret; |
| 995 | |
| 996 | arg++; |
| 997 | ret = parse_track_counters(args, &arg, section_type, curpx, |
| 998 | &rule->act_prm.trk_ctr, defpx, err, errlen); |
| 999 | |
| 1000 | if (ret < 0) /* nb: warnings are not handled yet */ |
| 1001 | return -1; |
| 1002 | |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 1003 | rule->action = TCP_ACT_TRK_SC1; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1004 | } |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 1005 | else if (strcmp(args[arg], "track-sc2") == 0) { |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1006 | int ret; |
| 1007 | |
| 1008 | arg++; |
| 1009 | ret = parse_track_counters(args, &arg, section_type, curpx, |
| 1010 | &rule->act_prm.trk_ctr, defpx, err, errlen); |
| 1011 | |
| 1012 | if (ret < 0) /* nb: warnings are not handled yet */ |
| 1013 | return -1; |
| 1014 | |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 1015 | rule->action = TCP_ACT_TRK_SC2; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1016 | } |
| 1017 | else { |
| 1018 | snprintf(err, errlen, |
Willy Tarreau | 5612328 | 2010-08-06 19:06:56 +0200 | [diff] [blame] | 1019 | "'%s %s' expects 'accept', 'reject', 'track-sc1' " |
| 1020 | "or 'track-sc2' in %s '%s' (was '%s')", |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1021 | args[0], args[1], proxy_type_str(curpx), curpx->id, args[arg]); |
| 1022 | return -1; |
| 1023 | } |
| 1024 | |
| 1025 | if (strcmp(args[arg], "if") == 0 || strcmp(args[arg], "unless") == 0) { |
| 1026 | if ((rule->cond = build_acl_cond(NULL, 0, curpx, (const char **)args+arg)) == NULL) { |
| 1027 | snprintf(err, errlen, |
| 1028 | "error detected in %s '%s' while parsing '%s' condition", |
| 1029 | proxy_type_str(curpx), curpx->id, args[arg]); |
| 1030 | return -1; |
| 1031 | } |
| 1032 | } |
| 1033 | else if (*args[arg]) { |
| 1034 | snprintf(err, errlen, |
| 1035 | "'%s %s %s' only accepts 'if' or 'unless', in %s '%s' (was '%s')", |
| 1036 | args[0], args[1], args[2], proxy_type_str(curpx), curpx->id, args[arg]); |
| 1037 | return -1; |
| 1038 | } |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1042 | /* This function should be called to parse a line starting with the "tcp-response" |
| 1043 | * keyword. |
| 1044 | */ |
| 1045 | static int tcp_parse_tcp_rep(char **args, int section_type, struct proxy *curpx, |
| 1046 | struct proxy *defpx, char *err, int errlen) |
| 1047 | { |
| 1048 | const char *ptr = NULL; |
| 1049 | unsigned int val; |
| 1050 | int retlen; |
| 1051 | int warn = 0; |
| 1052 | int arg; |
| 1053 | struct tcp_rule *rule; |
| 1054 | |
| 1055 | if (!*args[1]) { |
| 1056 | snprintf(err, errlen, "missing argument for '%s' in %s '%s'", |
| 1057 | args[0], proxy_type_str(curpx), curpx->id); |
| 1058 | return -1; |
| 1059 | } |
| 1060 | |
| 1061 | if (strcmp(args[1], "inspect-delay") == 0) { |
| 1062 | if (curpx == defpx || !(curpx->cap & PR_CAP_BE)) { |
| 1063 | snprintf(err, errlen, "%s %s is only allowed in 'backend' sections", |
| 1064 | args[0], args[1]); |
| 1065 | return -1; |
| 1066 | } |
| 1067 | |
| 1068 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
| 1069 | retlen = snprintf(err, errlen, |
| 1070 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
| 1071 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1072 | if (ptr && retlen < errlen) |
| 1073 | retlen += snprintf(err + retlen, errlen - retlen, |
| 1074 | " (unexpected character '%c')", *ptr); |
| 1075 | return -1; |
| 1076 | } |
| 1077 | |
| 1078 | if (curpx->tcp_rep.inspect_delay) { |
| 1079 | snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'", |
| 1080 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
| 1081 | return 1; |
| 1082 | } |
| 1083 | curpx->tcp_rep.inspect_delay = val; |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1087 | rule = calloc(1, sizeof(*rule)); |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1088 | LIST_INIT(&rule->list); |
| 1089 | arg = 1; |
| 1090 | |
| 1091 | if (strcmp(args[1], "content") == 0) { |
| 1092 | arg++; |
| 1093 | if (tcp_parse_response_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0) |
| 1094 | goto error; |
| 1095 | |
| 1096 | if (rule->cond && (rule->cond->requires & ACL_USE_L6REQ_VOLATILE)) { |
| 1097 | struct acl *acl; |
| 1098 | const char *name; |
| 1099 | |
| 1100 | acl = cond_find_require(rule->cond, ACL_USE_L6REQ_VOLATILE); |
| 1101 | name = acl ? acl->name : "(unknown)"; |
| 1102 | |
| 1103 | retlen = snprintf(err, errlen, |
| 1104 | "acl '%s' involves some request-only criteria which will be ignored.", |
| 1105 | name); |
| 1106 | warn++; |
| 1107 | } |
| 1108 | |
| 1109 | LIST_ADDQ(&curpx->tcp_rep.inspect_rules, &rule->list); |
| 1110 | } |
| 1111 | else { |
| 1112 | retlen = snprintf(err, errlen, |
| 1113 | "'%s' expects 'inspect-delay' or 'content' in %s '%s' (was '%s')", |
| 1114 | args[0], proxy_type_str(curpx), curpx->id, args[1]); |
| 1115 | goto error; |
| 1116 | } |
| 1117 | |
| 1118 | return warn; |
| 1119 | error: |
| 1120 | free(rule); |
| 1121 | return -1; |
| 1122 | } |
| 1123 | |
| 1124 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1125 | /* This function should be called to parse a line starting with the "tcp-request" |
| 1126 | * keyword. |
| 1127 | */ |
| 1128 | static int tcp_parse_tcp_req(char **args, int section_type, struct proxy *curpx, |
| 1129 | struct proxy *defpx, char *err, int errlen) |
| 1130 | { |
| 1131 | const char *ptr = NULL; |
Willy Tarreau | c7e961e | 2008-08-17 17:13:47 +0200 | [diff] [blame] | 1132 | unsigned int val; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1133 | int retlen; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1134 | int warn = 0; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1135 | int arg; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1136 | struct tcp_rule *rule; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1137 | |
| 1138 | if (!*args[1]) { |
| 1139 | snprintf(err, errlen, "missing argument for '%s' in %s '%s'", |
Willy Tarreau | f535683 | 2010-06-14 18:40:26 +0200 | [diff] [blame] | 1140 | args[0], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1141 | return -1; |
| 1142 | } |
| 1143 | |
| 1144 | if (!strcmp(args[1], "inspect-delay")) { |
| 1145 | if (curpx == defpx) { |
| 1146 | snprintf(err, errlen, "%s %s is not allowed in 'defaults' sections", |
| 1147 | args[0], args[1]); |
| 1148 | return -1; |
| 1149 | } |
| 1150 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1151 | if (!*args[2] || (ptr = parse_time_err(args[2], &val, TIME_UNIT_MS))) { |
| 1152 | retlen = snprintf(err, errlen, |
| 1153 | "'%s %s' expects a positive delay in milliseconds, in %s '%s'", |
Willy Tarreau | f535683 | 2010-06-14 18:40:26 +0200 | [diff] [blame] | 1154 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1155 | if (ptr && retlen < errlen) |
| 1156 | retlen += snprintf(err+retlen, errlen - retlen, |
| 1157 | " (unexpected character '%c')", *ptr); |
| 1158 | return -1; |
| 1159 | } |
| 1160 | |
| 1161 | if (curpx->tcp_req.inspect_delay) { |
| 1162 | snprintf(err, errlen, "ignoring %s %s (was already defined) in %s '%s'", |
Willy Tarreau | f535683 | 2010-06-14 18:40:26 +0200 | [diff] [blame] | 1163 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1164 | return 1; |
| 1165 | } |
| 1166 | curpx->tcp_req.inspect_delay = val; |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1170 | rule = calloc(1, sizeof(*rule)); |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 1171 | LIST_INIT(&rule->list); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1172 | arg = 1; |
| 1173 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1174 | if (strcmp(args[1], "content") == 0) { |
Willy Tarreau | d1f9652 | 2010-08-03 19:34:32 +0200 | [diff] [blame] | 1175 | arg++; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1176 | if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0) |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1177 | goto error; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1178 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1179 | if (rule->cond && (rule->cond->requires & ACL_USE_RTR_ANY)) { |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1180 | struct acl *acl; |
| 1181 | const char *name; |
| 1182 | |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1183 | acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1184 | name = acl ? acl->name : "(unknown)"; |
| 1185 | |
| 1186 | retlen = snprintf(err, errlen, |
Willy Tarreau | 1a21194 | 2009-07-14 13:53:17 +0200 | [diff] [blame] | 1187 | "acl '%s' involves some response-only criteria which will be ignored.", |
| 1188 | name); |
Willy Tarreau | dd64f8d | 2008-07-27 22:02:32 +0200 | [diff] [blame] | 1189 | warn++; |
| 1190 | } |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 1191 | LIST_ADDQ(&curpx->tcp_req.inspect_rules, &rule->list); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1192 | } |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1193 | else if (strcmp(args[1], "connection") == 0) { |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1194 | arg++; |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1195 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1196 | if (!(curpx->cap & PR_CAP_FE)) { |
| 1197 | snprintf(err, errlen, "%s %s is not allowed because %s %s is not a frontend", |
| 1198 | args[0], args[1], proxy_type_str(curpx), curpx->id); |
Simon Horman | 6c54d8b | 2011-07-15 13:14:06 +0900 | [diff] [blame] | 1199 | goto error; |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1200 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1201 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1202 | if (tcp_parse_request_rule(args, arg, section_type, curpx, defpx, rule, err, errlen) < 0) |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1203 | goto error; |
| 1204 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1205 | if (rule->cond && (rule->cond->requires & (ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY))) { |
| 1206 | struct acl *acl; |
| 1207 | const char *name; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 1208 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1209 | acl = cond_find_require(rule->cond, ACL_USE_RTR_ANY|ACL_USE_L6_ANY|ACL_USE_L7_ANY); |
| 1210 | name = acl ? acl->name : "(unknown)"; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 1211 | |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1212 | if (acl->requires & (ACL_USE_L6_ANY|ACL_USE_L7_ANY)) { |
| 1213 | retlen = snprintf(err, errlen, |
| 1214 | "'%s %s' may not reference acl '%s' which makes use of " |
| 1215 | "payload in %s '%s'. Please use '%s content' for this.", |
| 1216 | args[0], args[1], name, proxy_type_str(curpx), curpx->id, args[0]); |
| 1217 | goto error; |
| 1218 | } |
| 1219 | if (acl->requires & ACL_USE_RTR_ANY) |
| 1220 | retlen = snprintf(err, errlen, |
| 1221 | "acl '%s' involves some response-only criteria which will be ignored.", |
| 1222 | name); |
| 1223 | warn++; |
| 1224 | } |
Willy Tarreau | fb024dc | 2010-08-20 13:35:41 +0200 | [diff] [blame] | 1225 | LIST_ADDQ(&curpx->tcp_req.l4_rules, &rule->list); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 1226 | } |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1227 | else { |
| 1228 | retlen = snprintf(err, errlen, |
Willy Tarreau | 68c03ab | 2010-08-06 15:08:45 +0200 | [diff] [blame] | 1229 | "'%s' expects 'inspect-delay', 'connection', or 'content' in %s '%s' (was '%s')", |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1230 | args[0], proxy_type_str(curpx), curpx->id, args[1]); |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1231 | goto error; |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1232 | } |
| 1233 | |
Willy Tarreau | 1a68794 | 2010-05-23 22:40:30 +0200 | [diff] [blame] | 1234 | return warn; |
Willy Tarreau | 6a984fa | 2010-06-14 16:44:27 +0200 | [diff] [blame] | 1235 | error: |
| 1236 | free(rule); |
| 1237 | return -1; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1238 | } |
| 1239 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1240 | |
| 1241 | /************************************************************************/ |
| 1242 | /* All supported ACL keywords must be declared here. */ |
| 1243 | /************************************************************************/ |
| 1244 | |
| 1245 | /* set test->ptr to point to the source IPv4/IPv6 address and test->i to the family */ |
| 1246 | static int |
| 1247 | acl_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1248 | struct acl_expr *expr, struct acl_test *test) |
| 1249 | { |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1250 | test->i = l4->si[0].addr.c.from.ss_family; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1251 | if (test->i == AF_INET) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1252 | test->ptr = (char *)&((struct sockaddr_in *)&l4->si[0].addr.c.from)->sin_addr; |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1253 | else if (test->i == AF_INET6) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1254 | test->ptr = (char *)&((struct sockaddr_in6 *)(&l4->si[0].addr.c.from))->sin6_addr; |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1255 | else |
| 1256 | return 0; |
| 1257 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1258 | test->flags = ACL_TEST_F_READ_ONLY; |
| 1259 | return 1; |
| 1260 | } |
| 1261 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 1262 | /* extract the connection's source ipv4 address */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1263 | static int |
| 1264 | pattern_fetch_src(struct proxy *px, struct session *l4, void *l7, int dir, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1265 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1266 | { |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1267 | if (l4->si[0].addr.c.from.ss_family != AF_INET ) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1268 | return 0; |
| 1269 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1270 | data->ip.s_addr = ((struct sockaddr_in *)&l4->si[0].addr.c.from)->sin_addr.s_addr; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1271 | return 1; |
| 1272 | } |
| 1273 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 1274 | /* extract the connection's source ipv6 address */ |
| 1275 | static int |
| 1276 | pattern_fetch_src6(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1277 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 1278 | { |
| 1279 | if (l4->si[0].addr.c.from.ss_family != AF_INET6) |
| 1280 | return 0; |
| 1281 | |
| 1282 | memcpy(data->ipv6.s6_addr, ((struct sockaddr_in6 *)&l4->si[0].addr.c.from)->sin6_addr.s6_addr, sizeof(data->ipv6.s6_addr)); |
| 1283 | return 1; |
| 1284 | } |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1285 | |
| 1286 | /* set test->i to the connection's source port */ |
| 1287 | static int |
| 1288 | acl_fetch_sport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1289 | struct acl_expr *expr, struct acl_test *test) |
| 1290 | { |
Willy Tarreau | 86ad42c | 2011-08-27 12:29:07 +0200 | [diff] [blame] | 1291 | if (!(test->i = get_host_port(&l4->si[0].addr.c.from))) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1292 | return 0; |
| 1293 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1294 | test->flags = 0; |
| 1295 | return 1; |
| 1296 | } |
| 1297 | |
| 1298 | |
| 1299 | /* set test->ptr to point to the frontend's IPv4/IPv6 address and test->i to the family */ |
| 1300 | static int |
| 1301 | acl_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1302 | struct acl_expr *expr, struct acl_test *test) |
| 1303 | { |
| 1304 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 1305 | get_frt_addr(l4); |
| 1306 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1307 | test->i = l4->si[0].addr.c.to.ss_family; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1308 | if (test->i == AF_INET) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1309 | test->ptr = (char *)&((struct sockaddr_in *)&l4->si[0].addr.c.to)->sin_addr; |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1310 | else if (test->i == AF_INET6) |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1311 | test->ptr = (char *)&((struct sockaddr_in6 *)(&l4->si[0].addr.c.to))->sin6_addr; |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1312 | else |
| 1313 | return 0; |
| 1314 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1315 | test->flags = ACL_TEST_F_READ_ONLY; |
| 1316 | return 1; |
| 1317 | } |
| 1318 | |
| 1319 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 1320 | /* extract the connection's destination ipv4 address */ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1321 | static int |
| 1322 | pattern_fetch_dst(struct proxy *px, struct session *l4, void *l7, int dir, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1323 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1324 | { |
emeric | 8aa6b37 | 2010-10-22 17:06:26 +0200 | [diff] [blame] | 1325 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 1326 | get_frt_addr(l4); |
| 1327 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1328 | if (l4->si[0].addr.c.to.ss_family != AF_INET) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1329 | return 0; |
| 1330 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1331 | data->ip.s_addr = ((struct sockaddr_in *)&l4->si[0].addr.c.to)->sin_addr.s_addr; |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1332 | return 1; |
| 1333 | } |
| 1334 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 1335 | /* extract the connection's destination ipv6 address */ |
| 1336 | static int |
| 1337 | pattern_fetch_dst6(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1338 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 1339 | { |
| 1340 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 1341 | get_frt_addr(l4); |
| 1342 | |
| 1343 | if (l4->si[0].addr.c.to.ss_family != AF_INET6) |
| 1344 | return 0; |
| 1345 | |
| 1346 | memcpy(data->ipv6.s6_addr, ((struct sockaddr_in6 *)&l4->si[0].addr.c.to)->sin6_addr.s6_addr, sizeof(data->ipv6.s6_addr)); |
| 1347 | return 1; |
| 1348 | } |
| 1349 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1350 | /* set test->i to the frontend connexion's destination port */ |
| 1351 | static int |
| 1352 | acl_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1353 | struct acl_expr *expr, struct acl_test *test) |
| 1354 | { |
| 1355 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 1356 | get_frt_addr(l4); |
| 1357 | |
Willy Tarreau | 86ad42c | 2011-08-27 12:29:07 +0200 | [diff] [blame] | 1358 | if (!(test->i = get_host_port(&l4->si[0].addr.c.to))) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1359 | return 0; |
| 1360 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1361 | test->flags = 0; |
| 1362 | return 1; |
| 1363 | } |
| 1364 | |
| 1365 | static int |
| 1366 | pattern_fetch_dport(struct proxy *px, struct session *l4, void *l7, int dir, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1367 | const struct pattern_arg *arg, int i, union pattern_data *data) |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1368 | { |
emeric | 8aa6b37 | 2010-10-22 17:06:26 +0200 | [diff] [blame] | 1369 | if (!(l4->flags & SN_FRT_ADDR_SET)) |
| 1370 | get_frt_addr(l4); |
| 1371 | |
Willy Tarreau | 86ad42c | 2011-08-27 12:29:07 +0200 | [diff] [blame] | 1372 | if (!(data->integer = get_host_port(&l4->si[0].addr.c.to))) |
Emeric Brun | f769f51 | 2010-10-22 17:14:01 +0200 | [diff] [blame] | 1373 | return 0; |
| 1374 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1375 | return 1; |
| 1376 | } |
| 1377 | |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1378 | static int |
| 1379 | pattern_arg_fetch_payloadlv(const char *arg, struct pattern_arg **arg_p, int *arg_i) |
| 1380 | { |
| 1381 | int member = 0; |
| 1382 | int len_offset = 0; |
| 1383 | int len_size = 0; |
| 1384 | int buf_offset = 0; |
| 1385 | int relative = 0; |
| 1386 | int arg_len = strlen(arg); |
| 1387 | int i; |
| 1388 | |
| 1389 | for (i = 0; i < arg_len; i++) { |
| 1390 | if (arg[i] == ',') { |
| 1391 | member++; |
| 1392 | } else if (member == 0) { |
| 1393 | if (arg[i] < '0' || arg[i] > '9') |
| 1394 | return 0; |
| 1395 | |
| 1396 | len_offset = 10 * len_offset + arg[i] - '0'; |
| 1397 | } else if (member == 1) { |
| 1398 | if (arg[i] < '0' || arg[i] > '9') |
| 1399 | return 0; |
| 1400 | |
| 1401 | len_size = 10 * len_size + arg[i] - '0'; |
| 1402 | } else if (member == 2) { |
| 1403 | if (!relative && !buf_offset && arg[i] == '+') { |
| 1404 | relative = 1; |
| 1405 | continue; |
| 1406 | } else if (!relative && !buf_offset && arg[i] == '-') { |
| 1407 | relative = 2; |
| 1408 | continue; |
| 1409 | } else if (arg[i] < '0' || arg[i] > '9') |
| 1410 | return 0; |
| 1411 | |
| 1412 | buf_offset = 10 * buf_offset + arg[i] - '0'; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | if (member < 1) |
| 1417 | return 0; |
| 1418 | |
| 1419 | if (!len_size) |
| 1420 | return 0; |
| 1421 | |
| 1422 | if (member == 1) { |
| 1423 | buf_offset = len_offset + len_size; |
| 1424 | } |
| 1425 | else if (relative == 1) { |
| 1426 | buf_offset = len_offset + len_size + buf_offset; |
| 1427 | } |
| 1428 | else if (relative == 2) { |
| 1429 | if (len_offset + len_size < buf_offset) |
| 1430 | return 0; |
| 1431 | |
| 1432 | buf_offset = len_offset + len_size - buf_offset; |
| 1433 | } |
| 1434 | |
| 1435 | *arg_i = 3; |
| 1436 | *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg)); |
| 1437 | (*arg_p)[0].type = PATTERN_ARG_TYPE_INTEGER; |
| 1438 | (*arg_p)[0].data.integer = len_offset; |
| 1439 | (*arg_p)[1].type = PATTERN_ARG_TYPE_INTEGER; |
| 1440 | (*arg_p)[1].data.integer = len_size; |
| 1441 | (*arg_p)[2].type = PATTERN_ARG_TYPE_INTEGER; |
| 1442 | (*arg_p)[2].data.integer = buf_offset; |
| 1443 | |
| 1444 | return 1; |
| 1445 | } |
| 1446 | |
| 1447 | static int |
| 1448 | pattern_fetch_payloadlv(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1449 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 1450 | { |
| 1451 | int len_offset = arg_p[0].data.integer; |
| 1452 | int len_size = arg_p[1].data.integer; |
| 1453 | int buf_offset = arg_p[2].data.integer; |
| 1454 | int buf_size = 0; |
| 1455 | struct buffer *b; |
| 1456 | int i; |
| 1457 | |
| 1458 | /* Format is (len offset, len size, buf offset) or (len offset, len size) */ |
| 1459 | /* by default buf offset == len offset + len size */ |
| 1460 | /* buf offset could be absolute or relative to len offset + len size if prefixed by + or - */ |
| 1461 | |
| 1462 | if (!l4) |
| 1463 | return 0; |
| 1464 | |
| 1465 | b = (dir & PATTERN_FETCH_RTR) ? l4->rep : l4->req; |
| 1466 | |
| 1467 | if (!b || !b->l) |
| 1468 | return 0; |
| 1469 | |
| 1470 | if (len_offset + len_size > b->l) |
| 1471 | return 0; |
| 1472 | |
| 1473 | for (i = 0; i < len_size; i++) { |
| 1474 | buf_size = (buf_size << 8) + ((unsigned char *)b->w)[i + len_offset]; |
| 1475 | } |
| 1476 | |
| 1477 | if (!buf_size) |
| 1478 | return 0; |
| 1479 | |
| 1480 | if (buf_offset + buf_size > b->l) |
| 1481 | return 0; |
| 1482 | |
| 1483 | /* init chunk as read only */ |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1484 | chunk_initlen(&data->str, b->w + buf_offset, 0, buf_size); |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1485 | |
| 1486 | return 1; |
| 1487 | } |
| 1488 | |
| 1489 | static int |
| 1490 | pattern_arg_fetch_payload (const char *arg, struct pattern_arg **arg_p, int *arg_i) |
| 1491 | { |
| 1492 | int member = 0; |
| 1493 | int buf_offset = 0; |
| 1494 | int buf_size = 0; |
| 1495 | int arg_len = strlen(arg); |
| 1496 | int i; |
| 1497 | |
| 1498 | for (i = 0 ; i < arg_len ; i++) { |
| 1499 | if (arg[i] == ',') { |
| 1500 | member++; |
| 1501 | } else if (member == 0) { |
| 1502 | if (arg[i] < '0' || arg[i] > '9') |
| 1503 | return 0; |
| 1504 | |
| 1505 | buf_offset = 10 * buf_offset + arg[i] - '0'; |
| 1506 | } else if (member == 1) { |
| 1507 | if (arg[i] < '0' || arg[i] > '9') |
| 1508 | return 0; |
| 1509 | |
| 1510 | buf_size = 10 * buf_size + arg[i] - '0'; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | if (!buf_size) |
| 1515 | return 0; |
| 1516 | |
| 1517 | *arg_i = 2; |
| 1518 | *arg_p = calloc(1, *arg_i*sizeof(struct pattern_arg)); |
| 1519 | (*arg_p)[0].type = PATTERN_ARG_TYPE_INTEGER; |
| 1520 | (*arg_p)[0].data.integer = buf_offset; |
| 1521 | (*arg_p)[1].type = PATTERN_ARG_TYPE_INTEGER; |
| 1522 | (*arg_p)[1].data.integer = buf_size; |
| 1523 | |
| 1524 | return 1; |
| 1525 | } |
| 1526 | |
| 1527 | static int |
| 1528 | pattern_fetch_payload(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1529 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 1530 | { |
| 1531 | int buf_offset = arg_p[0].data.integer; |
| 1532 | int buf_size = arg_p[1].data.integer; |
| 1533 | struct buffer *b; |
| 1534 | |
| 1535 | if (!l4) |
| 1536 | return 0; |
| 1537 | |
| 1538 | b = (dir & PATTERN_FETCH_RTR) ? l4->rep : l4->req; |
| 1539 | |
| 1540 | if (!b || !b->l) |
| 1541 | return 0; |
| 1542 | |
| 1543 | if (buf_offset + buf_size > b->l) |
| 1544 | return 0; |
| 1545 | |
| 1546 | /* init chunk as read only */ |
Simon Horman | de072bd | 2011-06-24 15:11:37 +0900 | [diff] [blame] | 1547 | chunk_initlen(&data->str, b->w + buf_offset, 0, buf_size); |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1548 | |
| 1549 | return 1; |
| 1550 | } |
| 1551 | |
Simon Horman | ab814e0 | 2011-06-24 14:50:20 +0900 | [diff] [blame] | 1552 | static int |
| 1553 | pattern_fetch_rdp_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 1554 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 1555 | { |
| 1556 | int ret; |
| 1557 | struct acl_expr expr; |
| 1558 | struct acl_test test; |
| 1559 | |
| 1560 | if (!l4) |
| 1561 | return 0; |
| 1562 | |
| 1563 | memset(&expr, 0, sizeof(expr)); |
| 1564 | memset(&test, 0, sizeof(test)); |
| 1565 | |
| 1566 | expr.arg.str = arg_p[0].data.str.str; |
| 1567 | expr.arg_len = arg_p[0].data.str.len; |
| 1568 | |
| 1569 | ret = acl_fetch_rdp_cookie(px, l4, NULL, ACL_DIR_REQ, &expr, &test); |
| 1570 | if (ret == 0 || (test.flags & ACL_TEST_F_MAY_CHANGE) || test.len == 0) |
| 1571 | return 0; |
| 1572 | |
| 1573 | /* init chunk as read only */ |
| 1574 | chunk_initlen(&data->str, test.ptr, 0, test.len); |
| 1575 | return 1; |
| 1576 | } |
| 1577 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1578 | static struct cfg_kw_list cfg_kws = {{ },{ |
| 1579 | { CFG_LISTEN, "tcp-request", tcp_parse_tcp_req }, |
Emeric Brun | 97679e7 | 2010-09-23 17:56:44 +0200 | [diff] [blame] | 1580 | { CFG_LISTEN, "tcp-response", tcp_parse_tcp_rep }, |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1581 | { 0, NULL, NULL }, |
| 1582 | }}; |
| 1583 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1584 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1585 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1586 | { "src_port", acl_parse_int, acl_fetch_sport, acl_match_int, ACL_USE_TCP_PERMANENT }, |
| 1587 | { "src", acl_parse_ip, acl_fetch_src, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP }, |
| 1588 | { "dst", acl_parse_ip, acl_fetch_dst, acl_match_ip, ACL_USE_TCP4_PERMANENT|ACL_MAY_LOOKUP }, |
| 1589 | { "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] | 1590 | { NULL, NULL, NULL, NULL }, |
| 1591 | }}; |
| 1592 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1593 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1594 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1595 | { "src", pattern_fetch_src, NULL, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 1596 | { "src6", pattern_fetch_src6, NULL, PATTERN_TYPE_IPV6, PATTERN_FETCH_REQ }, |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1597 | { "dst", pattern_fetch_dst, NULL, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 1598 | { "dst6", pattern_fetch_dst6, NULL, PATTERN_TYPE_IPV6, PATTERN_FETCH_REQ }, |
Emeric | f2d7cae | 2010-11-05 18:13:50 +0100 | [diff] [blame] | 1599 | { "dst_port", pattern_fetch_dport, NULL, PATTERN_TYPE_INTEGER, PATTERN_FETCH_REQ }, |
| 1600 | { "payload", pattern_fetch_payload, pattern_arg_fetch_payload, PATTERN_TYPE_CONSTDATA, PATTERN_FETCH_REQ|PATTERN_FETCH_RTR }, |
| 1601 | { "payload_lv", pattern_fetch_payloadlv, pattern_arg_fetch_payloadlv, PATTERN_TYPE_CONSTDATA, PATTERN_FETCH_REQ|PATTERN_FETCH_RTR }, |
Simon Horman | ab814e0 | 2011-06-24 14:50:20 +0900 | [diff] [blame] | 1602 | { "rdp_cookie", pattern_fetch_rdp_cookie, pattern_arg_str, PATTERN_TYPE_CONSTSTRING, PATTERN_FETCH_REQ }, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1603 | { NULL, NULL, NULL, 0, 0 }, |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1604 | }}; |
| 1605 | |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1606 | __attribute__((constructor)) |
| 1607 | static void __tcp_protocol_init(void) |
| 1608 | { |
| 1609 | protocol_register(&proto_tcpv4); |
| 1610 | protocol_register(&proto_tcpv6); |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 1611 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1612 | cfg_register_keywords(&cfg_kws); |
| 1613 | acl_register_keywords(&acl_kws); |
Willy Tarreau | e6b9894 | 2007-10-29 01:09:36 +0100 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | |
| 1617 | /* |
| 1618 | * Local variables: |
| 1619 | * c-indent-level: 8 |
| 1620 | * c-basic-offset: 8 |
| 1621 | * End: |
| 1622 | */ |