Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 2 | * Frontend variables and functions. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | * |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [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 <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 23 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 24 | #include <common/config.h> |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 25 | #include <common/debug.h> |
| 26 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 27 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 31 | #include <proto/acl.h> |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 32 | #include <proto/buffers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 34 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | #include <proto/log.h> |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 36 | #include <proto/hdr_idx.h> |
Willy Tarreau | 9650f37 | 2009-08-16 14:02:45 +0200 | [diff] [blame] | 37 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 39 | #include <proto/proxy.h> |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 40 | #include <proto/session.h> |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 41 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <proto/stream_sock.h> |
| 43 | #include <proto/task.h> |
| 44 | |
| 45 | |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 46 | /* Retrieves the original destination address used by the client, and sets the |
| 47 | * SN_FRT_ADDR_SET flag. |
| 48 | */ |
| 49 | void get_frt_addr(struct session *s) |
| 50 | { |
| 51 | socklen_t namelen = sizeof(s->frt_addr); |
| 52 | |
Willy Tarreau | 7e5067d | 2008-12-07 16:27:56 +0100 | [diff] [blame] | 53 | if (get_original_dst(s->si[0].fd, (struct sockaddr_in *)&s->frt_addr, &namelen) == -1) |
| 54 | getsockname(s->si[0].fd, (struct sockaddr *)&s->frt_addr, &namelen); |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 55 | s->flags |= SN_FRT_ADDR_SET; |
| 56 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 58 | /* Finish a session accept() for a proxy (TCP or HTTP). It returns a negative |
Willy Tarreau | abe8ea5 | 2010-11-11 10:56:04 +0100 | [diff] [blame] | 59 | * value in case of a critical failure which must cause the listener to be |
| 60 | * disabled, a positive value in case of success, or zero if it is a success |
| 61 | * but the session must be closed ASAP (eg: monitoring). |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | */ |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 63 | int frontend_accept(struct session *s) |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 64 | { |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 65 | int cfd = s->si[0].fd; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 66 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 67 | tv_zero(&s->logs.tv_request); |
| 68 | s->logs.t_queue = -1; |
| 69 | s->logs.t_connect = -1; |
| 70 | s->logs.t_data = -1; |
| 71 | s->logs.t_close = 0; |
| 72 | s->logs.bytes_in = s->logs.bytes_out = 0; |
| 73 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 74 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | |
Willy Tarreau | 35a0994 | 2010-06-01 17:12:40 +0200 | [diff] [blame] | 76 | s->data_state = DATA_ST_INIT; |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 77 | s->data_source = DATA_SRC_NONE; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 78 | |
Willy Tarreau | 35a0994 | 2010-06-01 17:12:40 +0200 | [diff] [blame] | 79 | /* FIXME: the logs are horribly complicated now, because they are |
| 80 | * defined in <p>, <p>, and later <be> and <be>. |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 81 | */ |
Willy Tarreau | 35a0994 | 2010-06-01 17:12:40 +0200 | [diff] [blame] | 82 | if (s->logs.logwait & LW_REQ) |
| 83 | s->do_log = http_sess_log; |
| 84 | else |
| 85 | s->do_log = tcp_sess_log; |
| 86 | |
| 87 | /* default error reporting function, may be changed by analysers */ |
| 88 | s->srv_error = default_srv_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 89 | |
Willy Tarreau | f67c978 | 2010-05-23 22:59:00 +0200 | [diff] [blame] | 90 | /* Adjust some socket options */ |
Willy Tarreau | fffe132 | 2010-11-11 09:48:16 +0100 | [diff] [blame] | 91 | if ((s->listener->addr.ss_family != AF_UNIX) && |
| 92 | setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)) == -1) |
Willy Tarreau | abe8ea5 | 2010-11-11 10:56:04 +0100 | [diff] [blame] | 93 | goto out_return; |
Willy Tarreau | f67c978 | 2010-05-23 22:59:00 +0200 | [diff] [blame] | 94 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 95 | if (s->fe->options & PR_O_TCP_CLI_KA) |
Willy Tarreau | f67c978 | 2010-05-23 22:59:00 +0200 | [diff] [blame] | 96 | setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 97 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 98 | if (s->fe->options & PR_O_TCP_NOLING) |
Willy Tarreau | f67c978 | 2010-05-23 22:59:00 +0200 | [diff] [blame] | 99 | setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger)); |
| 100 | |
| 101 | if (global.tune.client_sndbuf) |
| 102 | setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf)); |
| 103 | |
| 104 | if (global.tune.client_rcvbuf) |
| 105 | setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf)); |
| 106 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 107 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 108 | /* the captures are only used in HTTP frontends */ |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 109 | if (unlikely(s->fe->nb_req_cap > 0 && |
| 110 | (s->txn.req.cap = pool_alloc2(s->fe->req_cap_pool)) == NULL)) |
Willy Tarreau | abe8ea5 | 2010-11-11 10:56:04 +0100 | [diff] [blame] | 111 | goto out_return; /* no memory */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 113 | if (unlikely(s->fe->nb_rsp_cap > 0 && |
| 114 | (s->txn.rsp.cap = pool_alloc2(s->fe->rsp_cap_pool)) == NULL)) |
Willy Tarreau | 35a0994 | 2010-06-01 17:12:40 +0200 | [diff] [blame] | 115 | goto out_free_reqcap; /* no memory */ |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 116 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 118 | if (s->fe->acl_requires & ACL_USE_L7_ANY) { |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 119 | /* we have to allocate header indexes only if we know |
| 120 | * that we may make use of them. This of course includes |
| 121 | * (mode == PR_MODE_HTTP). |
Willy Tarreau | 042cc79 | 2007-03-19 16:20:06 +0100 | [diff] [blame] | 122 | */ |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 123 | s->txn.hdr_idx.size = MAX_HTTP_HDR; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 124 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 125 | if (unlikely((s->txn.hdr_idx.v = pool_alloc2(s->fe->hdr_idx_pool)) == NULL)) |
Willy Tarreau | 35a0994 | 2010-06-01 17:12:40 +0200 | [diff] [blame] | 126 | goto out_free_rspcap; /* no memory */ |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 127 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 128 | /* and now initialize the HTTP transaction state */ |
| 129 | http_init_txn(s); |
| 130 | } |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 131 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 132 | if ((s->fe->mode == PR_MODE_TCP || s->fe->mode == PR_MODE_HTTP) |
| 133 | && (s->fe->logfac1 >= 0 || s->fe->logfac2 >= 0)) { |
| 134 | if (likely(s->fe->to_log)) { |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 135 | /* we have the client ip */ |
| 136 | if (s->logs.logwait & LW_CLIP) |
| 137 | if (!(s->logs.logwait &= ~LW_CLIP)) |
| 138 | s->do_log(s); |
Willy Tarreau | a3445fc | 2010-05-20 16:17:07 +0200 | [diff] [blame] | 139 | } |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 140 | else if (s->cli_addr.ss_family == AF_INET) { |
| 141 | char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN]; |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 142 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 143 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 144 | get_frt_addr(s); |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 145 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 146 | if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr, |
| 147 | sn, sizeof(sn)) && |
| 148 | inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 149 | pn, sizeof(pn))) { |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 150 | send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 151 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port), |
| 152 | sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port), |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 153 | s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 154 | } |
| 155 | } |
Emeric Brun | ab844ea | 2010-10-22 16:33:18 +0200 | [diff] [blame] | 156 | else if (s->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 157 | char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN]; |
Willy Tarreau | 14c8aac | 2007-05-08 19:46:30 +0200 | [diff] [blame] | 158 | |
| 159 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 160 | get_frt_addr(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 161 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 162 | if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr, |
| 163 | sn, sizeof(sn)) && |
| 164 | inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, |
| 165 | pn, sizeof(pn))) { |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 166 | send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 167 | pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 168 | sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port), |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 169 | s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | } |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 171 | } |
Emeric Brun | ab844ea | 2010-10-22 16:33:18 +0200 | [diff] [blame] | 172 | else { |
| 173 | /* UNIX socket, only the destination is known */ |
| 174 | send_log(s->fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n", |
| 175 | s->listener->luid, |
| 176 | s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 177 | } |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 178 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 179 | |
Willy Tarreau | 2281b7f | 2010-05-28 19:29:49 +0200 | [diff] [blame] | 180 | if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 181 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 182 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 183 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 184 | get_frt_addr(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 185 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 186 | if (s->cli_addr.ss_family == AF_INET) { |
| 187 | char pn[INET_ADDRSTRLEN]; |
| 188 | inet_ntop(AF_INET, |
| 189 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 190 | pn, sizeof(pn)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 191 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 192 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 193 | s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd, |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 194 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port)); |
| 195 | } |
Emeric Brun | ab844ea | 2010-10-22 16:33:18 +0200 | [diff] [blame] | 196 | else if (s->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 197 | char pn[INET6_ADDRSTRLEN]; |
| 198 | inet_ntop(AF_INET6, |
| 199 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 200 | pn, sizeof(pn)); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 201 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 202 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 203 | s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd, |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 204 | pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port)); |
| 205 | } |
Emeric Brun | ab844ea | 2010-10-22 16:33:18 +0200 | [diff] [blame] | 206 | else { |
| 207 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [unix:%d]\n", |
| 208 | s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd, |
| 209 | s->listener->luid); |
| 210 | } |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 211 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 212 | write(1, trash, len); |
| 213 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 214 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 215 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 216 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 217 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 218 | /* note: this should not happen anymore since there's always at least the switching rules */ |
| 219 | if (!s->req->analysers) { |
| 220 | buffer_auto_connect(s->req); /* don't wait to establish connection */ |
| 221 | buffer_auto_close(s->req); /* let the producer forward close requests */ |
| 222 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 223 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 224 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 225 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 226 | |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 227 | fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 228 | if (s->fe->options & PR_O_TCP_NOLING) |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 229 | fdtab[cfd].flags |= FD_FL_TCP_NOLING; |
Willy Tarreau | 6e6fb2b | 2009-08-16 18:20:44 +0200 | [diff] [blame] | 230 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 231 | if (unlikely((s->fe->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) || |
| 232 | (s->fe->mode == PR_MODE_HEALTH && (s->fe->options & PR_O_HTTP_CHK)))) { |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 233 | /* Either we got a request from a monitoring system on an HTTP instance, |
| 234 | * or we're in health check mode with the 'httpchk' option enabled. In |
| 235 | * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. |
| 236 | */ |
| 237 | struct chunk msg; |
| 238 | chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n"); |
| 239 | stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */ |
| 240 | s->req->analysers = 0; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 241 | s->task->expire = s->rep->wex; |
Willy Tarreau | 35a0994 | 2010-06-01 17:12:40 +0200 | [diff] [blame] | 242 | EV_FD_CLR(cfd, DIR_RD); |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 243 | } |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 244 | else if (unlikely(s->fe->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */ |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 245 | struct chunk msg; |
| 246 | chunk_initstr(&msg, "OK\n"); |
| 247 | stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */ |
| 248 | s->req->analysers = 0; |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 249 | s->task->expire = s->rep->wex; |
Willy Tarreau | 35a0994 | 2010-06-01 17:12:40 +0200 | [diff] [blame] | 250 | EV_FD_CLR(cfd, DIR_RD); |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 251 | } |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 252 | /* everything's OK, let's go on */ |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 253 | return 1; |
Willy Tarreau | 8ced9a4 | 2007-11-04 17:51:50 +0100 | [diff] [blame] | 254 | |
| 255 | /* Error unrolling */ |
Willy Tarreau | 35a0994 | 2010-06-01 17:12:40 +0200 | [diff] [blame] | 256 | out_free_rspcap: |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 257 | pool_free2(s->fe->rsp_cap_pool, s->txn.rsp.cap); |
Willy Tarreau | 35a0994 | 2010-06-01 17:12:40 +0200 | [diff] [blame] | 258 | out_free_reqcap: |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 259 | pool_free2(s->fe->req_cap_pool, s->txn.req.cap); |
Willy Tarreau | abe8ea5 | 2010-11-11 10:56:04 +0100 | [diff] [blame] | 260 | out_return: |
Willy Tarreau | eb47268 | 2010-05-28 18:46:57 +0200 | [diff] [blame] | 261 | return -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 264 | /* This analyser tries to fetch a line from the request buffer which looks like : |
| 265 | * |
| 266 | * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n" |
| 267 | * |
| 268 | * There must be exactly one space between each field. Fields are : |
Emeric Brun | 861ccff | 2010-10-29 12:03:03 +0200 | [diff] [blame] | 269 | * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6". |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 270 | * - SRC3 : layer 3 (eg: IP) source address in standard text form |
| 271 | * - DST3 : layer 3 (eg: IP) destination address in standard text form |
| 272 | * - SRC4 : layer 4 (eg: TCP port) source address in standard text form |
| 273 | * - DST4 : layer 4 (eg: TCP port) destination address in standard text form |
| 274 | * |
| 275 | * This line MUST be at the beginning of the buffer and MUST NOT wrap. |
| 276 | * |
| 277 | * Once the data is fetched, the values are set in the session's field and data |
| 278 | * are removed from the buffer. The function returns zero if it needs to wait |
| 279 | * for more data (max: timeout_client), or 1 if it has finished and removed itself. |
| 280 | */ |
| 281 | int frontend_decode_proxy_request(struct session *s, struct buffer *req, int an_bit) |
| 282 | { |
| 283 | char *line = req->data; |
| 284 | char *end = req->data + req->l; |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 285 | int len; |
| 286 | |
| 287 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 288 | now_ms, __FUNCTION__, |
| 289 | s, |
| 290 | req, |
| 291 | req->rex, req->wex, |
| 292 | req->flags, |
| 293 | req->l, |
| 294 | req->analysers); |
| 295 | |
| 296 | if (req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT)) |
| 297 | goto fail; |
| 298 | |
Emeric Brun | f4711a3 | 2010-10-29 15:16:55 +0200 | [diff] [blame] | 299 | len = MIN(req->l, 6); |
| 300 | if (!len) |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 301 | goto missing; |
| 302 | |
Emeric Brun | f4711a3 | 2010-10-29 15:16:55 +0200 | [diff] [blame] | 303 | /* Decode a possible proxy request, fail early if it does not match */ |
| 304 | if (strncmp(line, "PROXY ", len) != 0) |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 305 | goto fail; |
Emeric Brun | f4711a3 | 2010-10-29 15:16:55 +0200 | [diff] [blame] | 306 | |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 307 | line += 6; |
Emeric Brun | f4711a3 | 2010-10-29 15:16:55 +0200 | [diff] [blame] | 308 | if (req->l < 18) /* shortest possible line */ |
| 309 | goto missing; |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 310 | |
Emeric Brun | 861ccff | 2010-10-29 12:03:03 +0200 | [diff] [blame] | 311 | if (!memcmp(line, "TCP4 ", 5) != 0) { |
| 312 | u32 src3, dst3, sport, dport; |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 313 | |
Emeric Brun | 861ccff | 2010-10-29 12:03:03 +0200 | [diff] [blame] | 314 | line += 5; |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 315 | |
Emeric Brun | 861ccff | 2010-10-29 12:03:03 +0200 | [diff] [blame] | 316 | src3 = inetaddr_host_lim_ret(line, end, &line); |
| 317 | if (line == end) |
| 318 | goto missing; |
| 319 | if (*line++ != ' ') |
| 320 | goto fail; |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 321 | |
Emeric Brun | 861ccff | 2010-10-29 12:03:03 +0200 | [diff] [blame] | 322 | dst3 = inetaddr_host_lim_ret(line, end, &line); |
| 323 | if (line == end) |
| 324 | goto missing; |
| 325 | if (*line++ != ' ') |
| 326 | goto fail; |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 327 | |
Emeric Brun | 861ccff | 2010-10-29 12:03:03 +0200 | [diff] [blame] | 328 | sport = read_uint((const char **)&line, end); |
| 329 | if (line == end) |
| 330 | goto missing; |
| 331 | if (*line++ != ' ') |
| 332 | goto fail; |
| 333 | |
| 334 | dport = read_uint((const char **)&line, end); |
| 335 | if (line > end - 2) |
| 336 | goto missing; |
| 337 | if (*line++ != '\r') |
| 338 | goto fail; |
| 339 | if (*line++ != '\n') |
| 340 | goto fail; |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 341 | |
Emeric Brun | 861ccff | 2010-10-29 12:03:03 +0200 | [diff] [blame] | 342 | /* update the session's addresses and mark them set */ |
| 343 | ((struct sockaddr_in *)&s->cli_addr)->sin_family = AF_INET; |
| 344 | ((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr = htonl(src3); |
| 345 | ((struct sockaddr_in *)&s->cli_addr)->sin_port = htons(sport); |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 346 | |
Emeric Brun | 861ccff | 2010-10-29 12:03:03 +0200 | [diff] [blame] | 347 | ((struct sockaddr_in *)&s->frt_addr)->sin_family = AF_INET; |
| 348 | ((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr = htonl(dst3); |
| 349 | ((struct sockaddr_in *)&s->frt_addr)->sin_port = htons(dport); |
| 350 | s->flags |= SN_FRT_ADDR_SET; |
| 351 | |
| 352 | } |
| 353 | else if (!memcmp(line, "TCP6 ", 5) != 0) { |
| 354 | u32 sport, dport; |
| 355 | char *src_s; |
| 356 | char *dst_s, *sport_s, *dport_s; |
| 357 | struct in6_addr src3, dst3; |
| 358 | |
| 359 | line+=5; |
| 360 | |
| 361 | src_s = line; |
| 362 | dst_s = sport_s = dport_s = NULL; |
| 363 | while (1) { |
| 364 | if (line > end - 2) { |
| 365 | goto missing; |
| 366 | } |
| 367 | else if (*line == '\r') { |
| 368 | *line = 0; |
| 369 | line++; |
| 370 | if (*line++ != '\n') |
| 371 | goto fail; |
| 372 | break; |
| 373 | } |
| 374 | |
| 375 | if (*line == ' ') { |
| 376 | *line = 0; |
| 377 | if (!dst_s) |
| 378 | dst_s = line+1; |
| 379 | else if (!sport_s) |
| 380 | sport_s = line+1; |
| 381 | else if (!dport_s) |
| 382 | dport_s = line+1; |
| 383 | } |
| 384 | line++; |
| 385 | } |
| 386 | |
| 387 | if (!dst_s || !sport_s || !dport_s) |
| 388 | goto fail; |
| 389 | |
| 390 | sport = read_uint((const char **)&sport_s,dport_s-1); |
| 391 | if ( *sport_s != 0 ) |
| 392 | goto fail; |
| 393 | |
| 394 | dport = read_uint((const char **)&dport_s,line-2); |
| 395 | if ( *dport_s != 0 ) |
| 396 | goto fail; |
| 397 | |
| 398 | if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1) |
| 399 | goto fail; |
| 400 | |
| 401 | if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1) |
| 402 | goto fail; |
| 403 | |
| 404 | /* update the session's addresses and mark them set */ |
| 405 | ((struct sockaddr_in6 *)&s->cli_addr)->sin6_family = AF_INET6; |
| 406 | memcpy(&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, &src3, sizeof(struct in6_addr)); |
| 407 | ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port = htons(sport); |
| 408 | |
| 409 | ((struct sockaddr_in6 *)&s->frt_addr)->sin6_family = AF_INET6; |
| 410 | memcpy(&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr, &dst3, sizeof(struct in6_addr)); |
| 411 | ((struct sockaddr_in6 *)&s->frt_addr)->sin6_port = htons(dport); |
| 412 | s->flags |= SN_FRT_ADDR_SET; |
| 413 | } |
| 414 | else { |
| 415 | goto fail; |
| 416 | } |
Willy Tarreau | 8b0cbf9 | 2010-10-15 23:23:19 +0200 | [diff] [blame] | 417 | |
| 418 | /* remove the PROXY line from the request */ |
| 419 | len = line - req->data; |
| 420 | buffer_replace2(req, req->data, line, NULL, 0); |
| 421 | req->total -= len; /* don't count the header line */ |
| 422 | |
| 423 | req->analysers &= ~an_bit; |
| 424 | return 1; |
| 425 | |
| 426 | missing: |
| 427 | if (!(req->flags & (BF_SHUTR|BF_FULL))) { |
| 428 | buffer_dont_connect(s->req); |
| 429 | return 0; |
| 430 | } |
| 431 | /* missing data and buffer is either full or shutdown => fail */ |
| 432 | |
| 433 | fail: |
| 434 | buffer_abort(req); |
| 435 | buffer_abort(s->rep); |
| 436 | req->analysers = 0; |
| 437 | |
| 438 | s->fe->counters.failed_req++; |
| 439 | if (s->listener->counters) |
| 440 | s->listener->counters->failed_req++; |
| 441 | |
| 442 | if (!(s->flags & SN_ERR_MASK)) |
| 443 | s->flags |= SN_ERR_PRXCOND; |
| 444 | if (!(s->flags & SN_FINST_MASK)) |
| 445 | s->flags |= SN_FINST_R; |
| 446 | return 0; |
| 447 | } |
| 448 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 449 | /* set test->i to the id of the frontend */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 450 | static int |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 451 | acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir, |
| 452 | struct acl_expr *expr, struct acl_test *test) { |
Willy Tarreau | 662b2d8 | 2007-05-08 19:56:15 +0200 | [diff] [blame] | 453 | |
Willy Tarreau | 662b2d8 | 2007-05-08 19:56:15 +0200 | [diff] [blame] | 454 | test->flags = ACL_TEST_F_READ_ONLY; |
Willy Tarreau | 662b2d8 | 2007-05-08 19:56:15 +0200 | [diff] [blame] | 455 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 456 | test->i = l4->fe->uuid; |
Willy Tarreau | 662b2d8 | 2007-05-08 19:56:15 +0200 | [diff] [blame] | 457 | |
Emeric Brun | 5d16eda | 2010-01-04 15:47:45 +0100 | [diff] [blame] | 458 | return 1; |
| 459 | } |
| 460 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 461 | /* set test->i to the number of connections per second reaching the frontend */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 462 | static int |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 463 | acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir, |
| 464 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 662b2d8 | 2007-05-08 19:56:15 +0200 | [diff] [blame] | 465 | { |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 466 | test->flags = ACL_TEST_F_VOL_TEST; |
| 467 | if (expr->arg_len) { |
| 468 | /* another proxy was designated, we must look for it */ |
| 469 | for (px = proxy; px; px = px->next) |
| 470 | if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str)) |
| 471 | break; |
| 472 | } |
| 473 | if (!px) |
| 474 | return 0; |
Emeric Brun | 5d16eda | 2010-01-04 15:47:45 +0100 | [diff] [blame] | 475 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 476 | test->i = read_freq_ctr(&px->fe_sess_per_sec); |
Emeric Brun | 5d16eda | 2010-01-04 15:47:45 +0100 | [diff] [blame] | 477 | return 1; |
| 478 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 479 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 480 | /* set test->i to the number of concurrent connections on the frontend */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 481 | static int |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 482 | acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir, |
| 483 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 484 | { |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 485 | test->flags = ACL_TEST_F_VOL_TEST; |
| 486 | if (expr->arg_len) { |
| 487 | /* another proxy was designated, we must look for it */ |
| 488 | for (px = proxy; px; px = px->next) |
| 489 | if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str)) |
| 490 | break; |
| 491 | } |
| 492 | if (!px) |
| 493 | return 0; |
Krzysztof Piotr Oledzki | 346f76d | 2010-01-12 21:59:30 +0100 | [diff] [blame] | 494 | |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 495 | test->i = px->feconn; |
Krzysztof Piotr Oledzki | 346f76d | 2010-01-12 21:59:30 +0100 | [diff] [blame] | 496 | return 1; |
| 497 | } |
| 498 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 499 | |
| 500 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 501 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 645513a | 2010-05-24 20:55:15 +0200 | [diff] [blame] | 502 | { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING }, |
| 503 | { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING }, |
| 504 | { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 505 | { NULL, NULL, NULL, NULL }, |
| 506 | }}; |
| 507 | |
| 508 | |
| 509 | __attribute__((constructor)) |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 510 | static void __frontend_init(void) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 511 | { |
| 512 | acl_register_keywords(&acl_kws); |
| 513 | } |
| 514 | |
| 515 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 516 | /* |
| 517 | * Local variables: |
| 518 | * c-indent-level: 8 |
| 519 | * c-basic-offset: 8 |
| 520 | * End: |
| 521 | */ |