blob: 78180807690978248377a3493384b6a9f83bead4 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau03fa5df2010-05-24 21:02:37 +02002 * Frontend variables and functions.
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 *
Willy Tarreau03fa5df2010-05-24 21:02:37 +02004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreau2dd0d472006-06-29 17:53:05 +020017#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
Willy Tarreau48a7e722010-12-24 15:26:39 +010023#include <netinet/tcp.h>
24
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/compat.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020026#include <common/config.h>
Willy Tarreau8b0cbf92010-10-15 23:23:19 +020027#include <common/debug.h>
28#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020029#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Willy Tarreau8797c062007-05-07 00:55:35 +020033#include <proto/acl.h>
Willy Tarreau54469402006-07-29 16:59:06 +020034#include <proto/buffers.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020036#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020037#include <proto/log.h>
Willy Tarreaue5f20dc2006-12-03 15:21:35 +010038#include <proto/hdr_idx.h>
Willy Tarreau9650f372009-08-16 14:02:45 +020039#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010041#include <proto/proxy.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020042#include <proto/session.h>
Willy Tarreaudded32d2008-11-30 19:48:07 +010043#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/stream_sock.h>
45#include <proto/task.h>
46
47
Willy Tarreau14c8aac2007-05-08 19:46:30 +020048/* Retrieves the original destination address used by the client, and sets the
49 * SN_FRT_ADDR_SET flag.
50 */
51void get_frt_addr(struct session *s)
52{
53 socklen_t namelen = sizeof(s->frt_addr);
54
Willy Tarreau7e5067d2008-12-07 16:27:56 +010055 if (get_original_dst(s->si[0].fd, (struct sockaddr_in *)&s->frt_addr, &namelen) == -1)
56 getsockname(s->si[0].fd, (struct sockaddr *)&s->frt_addr, &namelen);
Willy Tarreau14c8aac2007-05-08 19:46:30 +020057 s->flags |= SN_FRT_ADDR_SET;
58}
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
Willy Tarreau81f9aa32010-06-01 17:45:26 +020060/* Finish a session accept() for a proxy (TCP or HTTP). It returns a negative
Willy Tarreauabe8ea52010-11-11 10:56:04 +010061 * value in case of a critical failure which must cause the listener to be
62 * disabled, a positive value in case of success, or zero if it is a success
63 * but the session must be closed ASAP (eg: monitoring).
Willy Tarreaubaaee002006-06-26 02:48:02 +020064 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020065int frontend_accept(struct session *s)
Willy Tarreaueb472682010-05-28 18:46:57 +020066{
Willy Tarreau81f9aa32010-06-01 17:45:26 +020067 int cfd = s->si[0].fd;
Emeric Brunb982a3d2010-01-04 15:45:53 +010068
Willy Tarreaueb472682010-05-28 18:46:57 +020069 tv_zero(&s->logs.tv_request);
70 s->logs.t_queue = -1;
71 s->logs.t_connect = -1;
72 s->logs.t_data = -1;
73 s->logs.t_close = 0;
74 s->logs.bytes_in = s->logs.bytes_out = 0;
75 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
76 s->logs.srv_queue_size = 0; /* we will get this number soon */
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
Willy Tarreau35a09942010-06-01 17:12:40 +020078 s->data_state = DATA_ST_INIT;
Willy Tarreaueb472682010-05-28 18:46:57 +020079 s->data_source = DATA_SRC_NONE;
Willy Tarreaua5555ec2008-11-30 19:02:32 +010080
Willy Tarreau35a09942010-06-01 17:12:40 +020081 /* FIXME: the logs are horribly complicated now, because they are
82 * defined in <p>, <p>, and later <be> and <be>.
Willy Tarreaueb472682010-05-28 18:46:57 +020083 */
Willy Tarreau35a09942010-06-01 17:12:40 +020084 if (s->logs.logwait & LW_REQ)
85 s->do_log = http_sess_log;
86 else
87 s->do_log = tcp_sess_log;
88
89 /* default error reporting function, may be changed by analysers */
90 s->srv_error = default_srv_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +020091
Willy Tarreauf67c9782010-05-23 22:59:00 +020092 /* Adjust some socket options */
Willy Tarreau9c3bc222010-12-24 14:49:37 +010093 if (s->listener->addr.ss_family == AF_INET || s->listener->addr.ss_family == AF_INET6) {
94 if (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
95 (char *) &one, sizeof(one)) == -1)
96 goto out_return;
Willy Tarreauf67c9782010-05-23 22:59:00 +020097
Willy Tarreau9c3bc222010-12-24 14:49:37 +010098 if (s->fe->options & PR_O_TCP_CLI_KA)
99 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE,
100 (char *) &one, sizeof(one));
Willy Tarreauf67c9782010-05-23 22:59:00 +0200101
Willy Tarreau9c3bc222010-12-24 14:49:37 +0100102 if (s->fe->options & PR_O_TCP_NOLING)
103 setsockopt(cfd, SOL_SOCKET, SO_LINGER,
104 (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau48a7e722010-12-24 15:26:39 +0100105#if defined(TCP_MAXSEG)
106 if (s->listener->maxseg < 0) {
107 /* we just want to reduce the current MSS by that value */
108 int mss;
Willy Tarreau7d286a02011-01-05 15:42:54 +0100109 socklen_t mss_len = sizeof(mss);
Willy Tarreau48a7e722010-12-24 15:26:39 +0100110 if (getsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &mss_len) == 0) {
111 mss += s->listener->maxseg; /* remember, it's < 0 */
112 setsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG, &mss, sizeof(mss));
113 }
114 }
115#endif
Willy Tarreau9c3bc222010-12-24 14:49:37 +0100116 }
Willy Tarreauf67c9782010-05-23 22:59:00 +0200117
118 if (global.tune.client_sndbuf)
119 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
120
121 if (global.tune.client_rcvbuf)
122 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
123
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200124 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200125 /* the captures are only used in HTTP frontends */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200126 if (unlikely(s->fe->nb_req_cap > 0 &&
127 (s->txn.req.cap = pool_alloc2(s->fe->req_cap_pool)) == NULL))
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100128 goto out_return; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200130 if (unlikely(s->fe->nb_rsp_cap > 0 &&
131 (s->txn.rsp.cap = pool_alloc2(s->fe->rsp_cap_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200132 goto out_free_reqcap; /* no memory */
Willy Tarreaueb472682010-05-28 18:46:57 +0200133 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200135 if (s->fe->acl_requires & ACL_USE_L7_ANY) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200136 /* we have to allocate header indexes only if we know
137 * that we may make use of them. This of course includes
138 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100139 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200140 s->txn.hdr_idx.size = MAX_HTTP_HDR;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100141
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200142 if (unlikely((s->txn.hdr_idx.v = pool_alloc2(s->fe->hdr_idx_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200143 goto out_free_rspcap; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100144
Willy Tarreaueb472682010-05-28 18:46:57 +0200145 /* and now initialize the HTTP transaction state */
146 http_init_txn(s);
147 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100148
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200149 if ((s->fe->mode == PR_MODE_TCP || s->fe->mode == PR_MODE_HTTP)
150 && (s->fe->logfac1 >= 0 || s->fe->logfac2 >= 0)) {
151 if (likely(s->fe->to_log)) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200152 /* we have the client ip */
153 if (s->logs.logwait & LW_CLIP)
154 if (!(s->logs.logwait &= ~LW_CLIP))
155 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200156 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200157 else if (s->cli_addr.ss_family == AF_INET) {
158 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200159
Willy Tarreaueb472682010-05-28 18:46:57 +0200160 if (!(s->flags & SN_FRT_ADDR_SET))
161 get_frt_addr(s);
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200162
Willy Tarreaueb472682010-05-28 18:46:57 +0200163 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr,
164 sn, sizeof(sn)) &&
165 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
166 pn, sizeof(pn))) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200167 send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreaueb472682010-05-28 18:46:57 +0200168 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port),
169 sn, ntohs(((struct sockaddr_in *)&s->frt_addr)->sin_port),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200170 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171 }
172 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200173 else if (s->cli_addr.ss_family == AF_INET6) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200174 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200175
176 if (!(s->flags & SN_FRT_ADDR_SET))
177 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178
Willy Tarreaueb472682010-05-28 18:46:57 +0200179 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr,
180 sn, sizeof(sn)) &&
181 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr,
182 pn, sizeof(pn))) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200183 send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreaueb472682010-05-28 18:46:57 +0200184 pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
185 sn, ntohs(((struct sockaddr_in6 *)&s->frt_addr)->sin6_port),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200186 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200188 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200189 else {
190 /* UNIX socket, only the destination is known */
191 send_log(s->fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
192 s->listener->luid,
193 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
194 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200195 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200197 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200198 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199
Willy Tarreaueb472682010-05-28 18:46:57 +0200200 if (!(s->flags & SN_FRT_ADDR_SET))
201 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200202
Willy Tarreaueb472682010-05-28 18:46:57 +0200203 if (s->cli_addr.ss_family == AF_INET) {
204 char pn[INET_ADDRSTRLEN];
205 inet_ntop(AF_INET,
206 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
207 pn, sizeof(pn));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200208
Willy Tarreaueb472682010-05-28 18:46:57 +0200209 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200210 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreaueb472682010-05-28 18:46:57 +0200211 pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port));
212 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200213 else if (s->cli_addr.ss_family == AF_INET6) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200214 char pn[INET6_ADDRSTRLEN];
215 inet_ntop(AF_INET6,
216 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
217 pn, sizeof(pn));
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200218
Willy Tarreaueb472682010-05-28 18:46:57 +0200219 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200220 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreaueb472682010-05-28 18:46:57 +0200221 pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port));
222 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200223 else {
224 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [unix:%d]\n",
225 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
226 s->listener->luid);
227 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200228
Willy Tarreaueb472682010-05-28 18:46:57 +0200229 write(1, trash, len);
230 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200232 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreaueb472682010-05-28 18:46:57 +0200233 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200234
Willy Tarreaueb472682010-05-28 18:46:57 +0200235 /* note: this should not happen anymore since there's always at least the switching rules */
236 if (!s->req->analysers) {
237 buffer_auto_connect(s->req); /* don't wait to establish connection */
238 buffer_auto_close(s->req); /* let the producer forward close requests */
239 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200240
Willy Tarreaueb472682010-05-28 18:46:57 +0200241 s->req->rto = s->fe->timeout.client;
Willy Tarreaueb472682010-05-28 18:46:57 +0200242 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243
Willy Tarreaueb472682010-05-28 18:46:57 +0200244 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200245 if (s->fe->options & PR_O_TCP_NOLING)
Willy Tarreaueb472682010-05-28 18:46:57 +0200246 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200247
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200248 if (unlikely((s->fe->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
249 (s->fe->mode == PR_MODE_HEALTH && (s->fe->options & PR_O_HTTP_CHK)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200250 /* Either we got a request from a monitoring system on an HTTP instance,
251 * or we're in health check mode with the 'httpchk' option enabled. In
252 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
253 */
254 struct chunk msg;
255 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
256 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
257 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200258 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200259 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200260 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200261 else if (unlikely(s->fe->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
Willy Tarreaueb472682010-05-28 18:46:57 +0200262 struct chunk msg;
263 chunk_initstr(&msg, "OK\n");
264 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
265 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200266 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200267 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200268 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200269 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200270 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100271
272 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200273 out_free_rspcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200274 pool_free2(s->fe->rsp_cap_pool, s->txn.rsp.cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200275 out_free_reqcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200276 pool_free2(s->fe->req_cap_pool, s->txn.req.cap);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100277 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200278 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200279}
280
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200281/* This analyser tries to fetch a line from the request buffer which looks like :
282 *
283 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
284 *
285 * There must be exactly one space between each field. Fields are :
Emeric Brun861ccff2010-10-29 12:03:03 +0200286 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200287 * - SRC3 : layer 3 (eg: IP) source address in standard text form
288 * - DST3 : layer 3 (eg: IP) destination address in standard text form
289 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
290 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
291 *
292 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
293 *
294 * Once the data is fetched, the values are set in the session's field and data
295 * are removed from the buffer. The function returns zero if it needs to wait
296 * for more data (max: timeout_client), or 1 if it has finished and removed itself.
297 */
298int frontend_decode_proxy_request(struct session *s, struct buffer *req, int an_bit)
299{
300 char *line = req->data;
301 char *end = req->data + req->l;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200302 int len;
303
304 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
305 now_ms, __FUNCTION__,
306 s,
307 req,
308 req->rex, req->wex,
309 req->flags,
310 req->l,
311 req->analysers);
312
313 if (req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT))
314 goto fail;
315
Emeric Brunf4711a32010-10-29 15:16:55 +0200316 len = MIN(req->l, 6);
317 if (!len)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200318 goto missing;
319
Emeric Brunf4711a32010-10-29 15:16:55 +0200320 /* Decode a possible proxy request, fail early if it does not match */
321 if (strncmp(line, "PROXY ", len) != 0)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200322 goto fail;
Emeric Brunf4711a32010-10-29 15:16:55 +0200323
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200324 line += 6;
Emeric Brunf4711a32010-10-29 15:16:55 +0200325 if (req->l < 18) /* shortest possible line */
326 goto missing;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200327
Emeric Brun861ccff2010-10-29 12:03:03 +0200328 if (!memcmp(line, "TCP4 ", 5) != 0) {
329 u32 src3, dst3, sport, dport;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200330
Emeric Brun861ccff2010-10-29 12:03:03 +0200331 line += 5;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200332
Emeric Brun861ccff2010-10-29 12:03:03 +0200333 src3 = inetaddr_host_lim_ret(line, end, &line);
334 if (line == end)
335 goto missing;
336 if (*line++ != ' ')
337 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200338
Emeric Brun861ccff2010-10-29 12:03:03 +0200339 dst3 = inetaddr_host_lim_ret(line, end, &line);
340 if (line == end)
341 goto missing;
342 if (*line++ != ' ')
343 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200344
Emeric Brun861ccff2010-10-29 12:03:03 +0200345 sport = read_uint((const char **)&line, end);
346 if (line == end)
347 goto missing;
348 if (*line++ != ' ')
349 goto fail;
350
351 dport = read_uint((const char **)&line, end);
352 if (line > end - 2)
353 goto missing;
354 if (*line++ != '\r')
355 goto fail;
356 if (*line++ != '\n')
357 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200358
Emeric Brun861ccff2010-10-29 12:03:03 +0200359 /* update the session's addresses and mark them set */
360 ((struct sockaddr_in *)&s->cli_addr)->sin_family = AF_INET;
361 ((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr = htonl(src3);
362 ((struct sockaddr_in *)&s->cli_addr)->sin_port = htons(sport);
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200363
Emeric Brun861ccff2010-10-29 12:03:03 +0200364 ((struct sockaddr_in *)&s->frt_addr)->sin_family = AF_INET;
365 ((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr = htonl(dst3);
366 ((struct sockaddr_in *)&s->frt_addr)->sin_port = htons(dport);
367 s->flags |= SN_FRT_ADDR_SET;
368
369 }
370 else if (!memcmp(line, "TCP6 ", 5) != 0) {
371 u32 sport, dport;
372 char *src_s;
373 char *dst_s, *sport_s, *dport_s;
374 struct in6_addr src3, dst3;
375
376 line+=5;
377
378 src_s = line;
379 dst_s = sport_s = dport_s = NULL;
380 while (1) {
381 if (line > end - 2) {
382 goto missing;
383 }
384 else if (*line == '\r') {
385 *line = 0;
386 line++;
387 if (*line++ != '\n')
388 goto fail;
389 break;
390 }
391
392 if (*line == ' ') {
393 *line = 0;
394 if (!dst_s)
395 dst_s = line+1;
396 else if (!sport_s)
397 sport_s = line+1;
398 else if (!dport_s)
399 dport_s = line+1;
400 }
401 line++;
402 }
403
404 if (!dst_s || !sport_s || !dport_s)
405 goto fail;
406
407 sport = read_uint((const char **)&sport_s,dport_s-1);
408 if ( *sport_s != 0 )
409 goto fail;
410
411 dport = read_uint((const char **)&dport_s,line-2);
412 if ( *dport_s != 0 )
413 goto fail;
414
415 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
416 goto fail;
417
418 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
419 goto fail;
420
421 /* update the session's addresses and mark them set */
422 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_family = AF_INET6;
423 memcpy(&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, &src3, sizeof(struct in6_addr));
424 ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port = htons(sport);
425
426 ((struct sockaddr_in6 *)&s->frt_addr)->sin6_family = AF_INET6;
427 memcpy(&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr, &dst3, sizeof(struct in6_addr));
428 ((struct sockaddr_in6 *)&s->frt_addr)->sin6_port = htons(dport);
429 s->flags |= SN_FRT_ADDR_SET;
430 }
431 else {
432 goto fail;
433 }
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200434
435 /* remove the PROXY line from the request */
436 len = line - req->data;
437 buffer_replace2(req, req->data, line, NULL, 0);
438 req->total -= len; /* don't count the header line */
439
440 req->analysers &= ~an_bit;
441 return 1;
442
443 missing:
444 if (!(req->flags & (BF_SHUTR|BF_FULL))) {
445 buffer_dont_connect(s->req);
446 return 0;
447 }
448 /* missing data and buffer is either full or shutdown => fail */
449
450 fail:
451 buffer_abort(req);
452 buffer_abort(s->rep);
453 req->analysers = 0;
454
455 s->fe->counters.failed_req++;
456 if (s->listener->counters)
457 s->listener->counters->failed_req++;
458
459 if (!(s->flags & SN_ERR_MASK))
460 s->flags |= SN_ERR_PRXCOND;
461 if (!(s->flags & SN_FINST_MASK))
462 s->flags |= SN_FINST_R;
463 return 0;
464}
465
Willy Tarreau645513a2010-05-24 20:55:15 +0200466/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200467static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200468acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
469 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200470
Willy Tarreau662b2d82007-05-08 19:56:15 +0200471 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200472
Willy Tarreau645513a2010-05-24 20:55:15 +0200473 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200474
Emeric Brun5d16eda2010-01-04 15:47:45 +0100475 return 1;
476}
477
Willy Tarreau645513a2010-05-24 20:55:15 +0200478/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200479static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200480acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
481 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200482{
Willy Tarreau645513a2010-05-24 20:55:15 +0200483 test->flags = ACL_TEST_F_VOL_TEST;
484 if (expr->arg_len) {
485 /* another proxy was designated, we must look for it */
486 for (px = proxy; px; px = px->next)
487 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
488 break;
489 }
490 if (!px)
491 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100492
Willy Tarreau645513a2010-05-24 20:55:15 +0200493 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100494 return 1;
495}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100496
Willy Tarreau645513a2010-05-24 20:55:15 +0200497/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200498static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200499acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
500 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200501{
Willy Tarreau645513a2010-05-24 20:55:15 +0200502 test->flags = ACL_TEST_F_VOL_TEST;
503 if (expr->arg_len) {
504 /* another proxy was designated, we must look for it */
505 for (px = proxy; px; px = px->next)
506 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
507 break;
508 }
509 if (!px)
510 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100511
Willy Tarreau645513a2010-05-24 20:55:15 +0200512 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100513 return 1;
514}
515
Willy Tarreau8797c062007-05-07 00:55:35 +0200516
517/* Note: must not be declared <const> as its list will be overwritten */
518static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200519 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
520 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
521 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200522 { NULL, NULL, NULL, NULL },
523}};
524
525
526__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200527static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200528{
529 acl_register_keywords(&acl_kws);
530}
531
532
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533/*
534 * Local variables:
535 * c-indent-level: 8
536 * c-basic-offset: 8
537 * End:
538 */