blob: 12dc2c8a0ea0b49b63193512b05f9a03944b9cad [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 Tarreaua73fcaf2011-03-20 10:15:22 +01004 * Copyright 2000-2011 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{
Willy Tarreau6471afb2011-09-23 10:54:59 +020053 socklen_t namelen = sizeof(s->si[0].addr.to);
Willy Tarreau14c8aac2007-05-08 19:46:30 +020054
Willy Tarreau6471afb2011-09-23 10:54:59 +020055 if (get_original_dst(s->si[0].fd, (struct sockaddr_in *)&s->si[0].addr.to, &namelen) == -1)
56 getsockname(s->si[0].fd, (struct sockaddr *)&s->si[0].addr.to, &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 /* FIXME: the logs are horribly complicated now, because they are
79 * defined in <p>, <p>, and later <be> and <be>.
Willy Tarreaueb472682010-05-28 18:46:57 +020080 */
William Lallemandbddd4fd2012-02-27 11:23:10 +010081 s->do_log = sess_log;
Willy Tarreau35a09942010-06-01 17:12:40 +020082
83 /* default error reporting function, may be changed by analysers */
84 s->srv_error = default_srv_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +020085
Willy Tarreauf67c9782010-05-23 22:59:00 +020086 /* Adjust some socket options */
Willy Tarreau9c3bc222010-12-24 14:49:37 +010087 if (s->listener->addr.ss_family == AF_INET || s->listener->addr.ss_family == AF_INET6) {
88 if (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
89 (char *) &one, sizeof(one)) == -1)
90 goto out_return;
Willy Tarreauf67c9782010-05-23 22:59:00 +020091
Willy Tarreau9c3bc222010-12-24 14:49:37 +010092 if (s->fe->options & PR_O_TCP_CLI_KA)
93 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE,
94 (char *) &one, sizeof(one));
Willy Tarreauf67c9782010-05-23 22:59:00 +020095
Willy Tarreau9c3bc222010-12-24 14:49:37 +010096 if (s->fe->options & PR_O_TCP_NOLING)
97 setsockopt(cfd, SOL_SOCKET, SO_LINGER,
98 (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau48a7e722010-12-24 15:26:39 +010099#if defined(TCP_MAXSEG)
100 if (s->listener->maxseg < 0) {
101 /* we just want to reduce the current MSS by that value */
102 int mss;
Willy Tarreau7d286a02011-01-05 15:42:54 +0100103 socklen_t mss_len = sizeof(mss);
Willy Tarreau48a7e722010-12-24 15:26:39 +0100104 if (getsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &mss_len) == 0) {
105 mss += s->listener->maxseg; /* remember, it's < 0 */
106 setsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG, &mss, sizeof(mss));
107 }
108 }
109#endif
Willy Tarreau9c3bc222010-12-24 14:49:37 +0100110 }
Willy Tarreauf67c9782010-05-23 22:59:00 +0200111
112 if (global.tune.client_sndbuf)
113 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
114
115 if (global.tune.client_rcvbuf)
116 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
117
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200118 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200119 /* the captures are only used in HTTP frontends */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200120 if (unlikely(s->fe->nb_req_cap > 0 &&
121 (s->txn.req.cap = pool_alloc2(s->fe->req_cap_pool)) == NULL))
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100122 goto out_return; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200124 if (unlikely(s->fe->nb_rsp_cap > 0 &&
125 (s->txn.rsp.cap = pool_alloc2(s->fe->rsp_cap_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200126 goto out_free_reqcap; /* no memory */
Willy Tarreaueb472682010-05-28 18:46:57 +0200127 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200129 if (s->fe->acl_requires & ACL_USE_L7_ANY) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200130 /* we have to allocate header indexes only if we know
131 * that we may make use of them. This of course includes
132 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100133 */
Willy Tarreauac1932d2011-10-24 19:14:41 +0200134 s->txn.hdr_idx.size = global.tune.max_http_hdr;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100135
Willy Tarreau34eb6712011-10-24 18:15:04 +0200136 if (unlikely((s->txn.hdr_idx.v = pool_alloc2(pool2_hdr_idx)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200137 goto out_free_rspcap; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100138
Willy Tarreaueb472682010-05-28 18:46:57 +0200139 /* and now initialize the HTTP transaction state */
140 http_init_txn(s);
141 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100142
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200143 if ((s->fe->mode == PR_MODE_TCP || s->fe->mode == PR_MODE_HTTP)
William Lallemand0f99e342011-10-12 17:50:54 +0200144 && (!LIST_ISEMPTY(&s->fe->logsrvs))) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200145 if (likely(s->fe->to_log)) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200146 /* we have the client ip */
147 if (s->logs.logwait & LW_CLIP)
148 if (!(s->logs.logwait &= ~LW_CLIP))
149 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200150 }
Willy Tarreau631f01c2011-09-05 00:36:48 +0200151 else {
Willy Tarreaueb472682010-05-28 18:46:57 +0200152 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200153
154 if (!(s->flags & SN_FRT_ADDR_SET))
155 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156
Willy Tarreau6471afb2011-09-23 10:54:59 +0200157 switch (addr_to_str(&s->req->prod->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200158 case AF_INET:
159 case AF_INET6:
Willy Tarreau6471afb2011-09-23 10:54:59 +0200160 addr_to_str(&s->req->prod->addr.to, sn, sizeof(sn));
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200161 send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +0200162 pn, get_host_port(&s->req->prod->addr.from),
163 sn, get_host_port(&s->req->prod->addr.to),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200164 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreau631f01c2011-09-05 00:36:48 +0200165 break;
166 case AF_UNIX:
167 /* UNIX socket, only the destination is known */
168 send_log(s->fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
169 s->listener->luid,
170 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
171 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200173 }
174 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200176 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200177 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub0f75322011-09-09 11:21:06 +0200178 int len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179
Willy Tarreaueb472682010-05-28 18:46:57 +0200180 if (!(s->flags & SN_FRT_ADDR_SET))
181 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182
Willy Tarreau6471afb2011-09-23 10:54:59 +0200183 switch (addr_to_str(&s->req->prod->addr.from, pn, sizeof(pn))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200184 case AF_INET:
185 case AF_INET6:
Willy Tarreaueb472682010-05-28 18:46:57 +0200186 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200187 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreau6471afb2011-09-23 10:54:59 +0200188 pn, get_host_port(&s->req->prod->addr.from));
Willy Tarreau631f01c2011-09-05 00:36:48 +0200189 break;
190 case AF_UNIX:
191 /* UNIX socket, only the destination is known */
Emeric Brunab844ea2010-10-22 16:33:18 +0200192 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [unix:%d]\n",
193 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
194 s->listener->luid);
Willy Tarreau631f01c2011-09-05 00:36:48 +0200195 break;
Emeric Brunab844ea2010-10-22 16:33:18 +0200196 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200197
Willy Tarreaueb472682010-05-28 18:46:57 +0200198 write(1, trash, len);
199 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200200
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200201 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreaueb472682010-05-28 18:46:57 +0200202 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203
Willy Tarreaueb472682010-05-28 18:46:57 +0200204 /* note: this should not happen anymore since there's always at least the switching rules */
205 if (!s->req->analysers) {
206 buffer_auto_connect(s->req); /* don't wait to establish connection */
207 buffer_auto_close(s->req); /* let the producer forward close requests */
208 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200209
Willy Tarreaueb472682010-05-28 18:46:57 +0200210 s->req->rto = s->fe->timeout.client;
Willy Tarreaueb472682010-05-28 18:46:57 +0200211 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212
Willy Tarreaueb472682010-05-28 18:46:57 +0200213 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200214 if (s->fe->options & PR_O_TCP_NOLING)
Willy Tarreaueb472682010-05-28 18:46:57 +0200215 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200216
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200217 if (unlikely((s->fe->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
Willy Tarreau1620ec32011-08-06 17:05:02 +0200218 (s->fe->mode == PR_MODE_HEALTH && ((s->fe->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200219 /* Either we got a request from a monitoring system on an HTTP instance,
220 * or we're in health check mode with the 'httpchk' option enabled. In
221 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
222 */
223 struct chunk msg;
224 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
225 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
226 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200227 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200228 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200229 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200230 else if (unlikely(s->fe->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
Willy Tarreaueb472682010-05-28 18:46:57 +0200231 struct chunk msg;
232 chunk_initstr(&msg, "OK\n");
233 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
234 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200235 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200236 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200237 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200238 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200239 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100240
241 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200242 out_free_rspcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200243 pool_free2(s->fe->rsp_cap_pool, s->txn.rsp.cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200244 out_free_reqcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200245 pool_free2(s->fe->req_cap_pool, s->txn.req.cap);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100246 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200247 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200248}
249
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200250/* This analyser tries to fetch a line from the request buffer which looks like :
251 *
252 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
253 *
254 * There must be exactly one space between each field. Fields are :
Emeric Brun861ccff2010-10-29 12:03:03 +0200255 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200256 * - SRC3 : layer 3 (eg: IP) source address in standard text form
257 * - DST3 : layer 3 (eg: IP) destination address in standard text form
258 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
259 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
260 *
261 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
262 *
263 * Once the data is fetched, the values are set in the session's field and data
264 * are removed from the buffer. The function returns zero if it needs to wait
265 * for more data (max: timeout_client), or 1 if it has finished and removed itself.
266 */
267int frontend_decode_proxy_request(struct session *s, struct buffer *req, int an_bit)
268{
269 char *line = req->data;
270 char *end = req->data + req->l;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200271 int len;
272
273 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
274 now_ms, __FUNCTION__,
275 s,
276 req,
277 req->rex, req->wex,
278 req->flags,
279 req->l,
280 req->analysers);
281
282 if (req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT))
283 goto fail;
284
Emeric Brunf4711a32010-10-29 15:16:55 +0200285 len = MIN(req->l, 6);
286 if (!len)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200287 goto missing;
288
Emeric Brunf4711a32010-10-29 15:16:55 +0200289 /* Decode a possible proxy request, fail early if it does not match */
290 if (strncmp(line, "PROXY ", len) != 0)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200291 goto fail;
Emeric Brunf4711a32010-10-29 15:16:55 +0200292
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200293 line += 6;
Emeric Brunf4711a32010-10-29 15:16:55 +0200294 if (req->l < 18) /* shortest possible line */
295 goto missing;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200296
Emeric Brun861ccff2010-10-29 12:03:03 +0200297 if (!memcmp(line, "TCP4 ", 5) != 0) {
298 u32 src3, dst3, sport, dport;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200299
Emeric Brun861ccff2010-10-29 12:03:03 +0200300 line += 5;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200301
Emeric Brun861ccff2010-10-29 12:03:03 +0200302 src3 = inetaddr_host_lim_ret(line, end, &line);
303 if (line == end)
304 goto missing;
305 if (*line++ != ' ')
306 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200307
Emeric Brun861ccff2010-10-29 12:03:03 +0200308 dst3 = inetaddr_host_lim_ret(line, end, &line);
309 if (line == end)
310 goto missing;
311 if (*line++ != ' ')
312 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200313
Emeric Brun861ccff2010-10-29 12:03:03 +0200314 sport = read_uint((const char **)&line, end);
315 if (line == end)
316 goto missing;
317 if (*line++ != ' ')
318 goto fail;
319
320 dport = read_uint((const char **)&line, end);
321 if (line > end - 2)
322 goto missing;
323 if (*line++ != '\r')
324 goto fail;
325 if (*line++ != '\n')
326 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200327
Emeric Brun861ccff2010-10-29 12:03:03 +0200328 /* update the session's addresses and mark them set */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200329 ((struct sockaddr_in *)&s->si[0].addr.from)->sin_family = AF_INET;
330 ((struct sockaddr_in *)&s->si[0].addr.from)->sin_addr.s_addr = htonl(src3);
331 ((struct sockaddr_in *)&s->si[0].addr.from)->sin_port = htons(sport);
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200332
Willy Tarreau6471afb2011-09-23 10:54:59 +0200333 ((struct sockaddr_in *)&s->si[0].addr.to)->sin_family = AF_INET;
334 ((struct sockaddr_in *)&s->si[0].addr.to)->sin_addr.s_addr = htonl(dst3);
335 ((struct sockaddr_in *)&s->si[0].addr.to)->sin_port = htons(dport);
Emeric Brun861ccff2010-10-29 12:03:03 +0200336 s->flags |= SN_FRT_ADDR_SET;
337
338 }
339 else if (!memcmp(line, "TCP6 ", 5) != 0) {
340 u32 sport, dport;
341 char *src_s;
342 char *dst_s, *sport_s, *dport_s;
343 struct in6_addr src3, dst3;
344
345 line+=5;
346
347 src_s = line;
348 dst_s = sport_s = dport_s = NULL;
349 while (1) {
350 if (line > end - 2) {
351 goto missing;
352 }
353 else if (*line == '\r') {
354 *line = 0;
355 line++;
356 if (*line++ != '\n')
357 goto fail;
358 break;
359 }
360
361 if (*line == ' ') {
362 *line = 0;
363 if (!dst_s)
364 dst_s = line+1;
365 else if (!sport_s)
366 sport_s = line+1;
367 else if (!dport_s)
368 dport_s = line+1;
369 }
370 line++;
371 }
372
373 if (!dst_s || !sport_s || !dport_s)
374 goto fail;
375
376 sport = read_uint((const char **)&sport_s,dport_s-1);
377 if ( *sport_s != 0 )
378 goto fail;
379
380 dport = read_uint((const char **)&dport_s,line-2);
381 if ( *dport_s != 0 )
382 goto fail;
383
384 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
385 goto fail;
386
387 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
388 goto fail;
389
390 /* update the session's addresses and mark them set */
Willy Tarreau6471afb2011-09-23 10:54:59 +0200391 ((struct sockaddr_in6 *)&s->si[0].addr.from)->sin6_family = AF_INET6;
392 memcpy(&((struct sockaddr_in6 *)&s->si[0].addr.from)->sin6_addr, &src3, sizeof(struct in6_addr));
393 ((struct sockaddr_in6 *)&s->si[0].addr.from)->sin6_port = htons(sport);
Emeric Brun861ccff2010-10-29 12:03:03 +0200394
Willy Tarreau6471afb2011-09-23 10:54:59 +0200395 ((struct sockaddr_in6 *)&s->si[0].addr.to)->sin6_family = AF_INET6;
396 memcpy(&((struct sockaddr_in6 *)&s->si[0].addr.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
397 ((struct sockaddr_in6 *)&s->si[0].addr.to)->sin6_port = htons(dport);
Emeric Brun861ccff2010-10-29 12:03:03 +0200398 s->flags |= SN_FRT_ADDR_SET;
399 }
400 else {
401 goto fail;
402 }
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200403
404 /* remove the PROXY line from the request */
405 len = line - req->data;
406 buffer_replace2(req, req->data, line, NULL, 0);
407 req->total -= len; /* don't count the header line */
408
409 req->analysers &= ~an_bit;
410 return 1;
411
412 missing:
413 if (!(req->flags & (BF_SHUTR|BF_FULL))) {
414 buffer_dont_connect(s->req);
415 return 0;
416 }
417 /* missing data and buffer is either full or shutdown => fail */
418
419 fail:
420 buffer_abort(req);
421 buffer_abort(s->rep);
422 req->analysers = 0;
423
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100424 s->fe->fe_counters.failed_req++;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200425 if (s->listener->counters)
426 s->listener->counters->failed_req++;
427
428 if (!(s->flags & SN_ERR_MASK))
429 s->flags |= SN_ERR_PRXCOND;
430 if (!(s->flags & SN_FINST_MASK))
431 s->flags |= SN_FINST_R;
432 return 0;
433}
434
Willy Tarreaua73fcaf2011-03-20 10:15:22 +0100435/* Makes a PROXY protocol line from the two addresses. The output is sent to
436 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
437 * It returns the number of bytes composing this line (including the trailing
438 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
439 * TCP6 and "UNKNOWN" formats.
440 */
441int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
442{
443 int ret = 0;
444
445 if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
446 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
447 if (ret >= buf_len)
448 return 0;
449
450 /* IPv4 src */
451 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
452 return 0;
453
454 ret += strlen(buf + ret);
455 if (ret >= buf_len)
456 return 0;
457
458 buf[ret++] = ' ';
459
460 /* IPv4 dst */
461 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
462 return 0;
463
464 ret += strlen(buf + ret);
465 if (ret >= buf_len)
466 return 0;
467
468 /* source and destination ports */
469 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
470 ntohs(((struct sockaddr_in *)src)->sin_port),
471 ntohs(((struct sockaddr_in *)dst)->sin_port));
472 if (ret >= buf_len)
473 return 0;
474 }
475 else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
476 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
477 if (ret >= buf_len)
478 return 0;
479
480 /* IPv6 src */
481 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
482 return 0;
483
484 ret += strlen(buf + ret);
485 if (ret >= buf_len)
486 return 0;
487
488 buf[ret++] = ' ';
489
490 /* IPv6 dst */
491 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
492 return 0;
493
494 ret += strlen(buf + ret);
495 if (ret >= buf_len)
496 return 0;
497
498 /* source and destination ports */
499 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
500 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
501 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
502 if (ret >= buf_len)
503 return 0;
504 }
505 else {
506 /* unknown family combination */
507 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
508 if (ret >= buf_len)
509 return 0;
510 }
511 return ret;
512}
513
Willy Tarreaua5e37562011-12-16 17:06:15 +0100514/* set temp integer to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200515static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200516acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
517 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200518
Willy Tarreau662b2d82007-05-08 19:56:15 +0200519 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreaua5e37562011-12-16 17:06:15 +0100520 temp_pattern.data.integer = l4->fe->uuid;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100521 return 1;
522}
523
Willy Tarreaua5e37562011-12-16 17:06:15 +0100524/* set temp integer to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200525static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200526acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
527 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200528{
Willy Tarreau645513a2010-05-24 20:55:15 +0200529 test->flags = ACL_TEST_F_VOL_TEST;
530 if (expr->arg_len) {
531 /* another proxy was designated, we must look for it */
532 for (px = proxy; px; px = px->next)
533 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
534 break;
535 }
536 if (!px)
537 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100538
Willy Tarreaua5e37562011-12-16 17:06:15 +0100539 temp_pattern.data.integer = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100540 return 1;
541}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100542
Willy Tarreaua5e37562011-12-16 17:06:15 +0100543/* set temp integer to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200544static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200545acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
546 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200547{
Willy Tarreau645513a2010-05-24 20:55:15 +0200548 test->flags = ACL_TEST_F_VOL_TEST;
549 if (expr->arg_len) {
550 /* another proxy was designated, we must look for it */
551 for (px = proxy; px; px = px->next)
552 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
553 break;
554 }
555 if (!px)
556 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100557
Willy Tarreaua5e37562011-12-16 17:06:15 +0100558 temp_pattern.data.integer = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100559 return 1;
560}
561
Willy Tarreau8797c062007-05-07 00:55:35 +0200562
563/* Note: must not be declared <const> as its list will be overwritten */
564static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200565 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
566 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
567 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200568 { NULL, NULL, NULL, NULL },
569}};
570
571
572__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200573static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200574{
575 acl_register_keywords(&acl_kws);
576}
577
578
Willy Tarreaubaaee002006-06-26 02:48:02 +0200579/*
580 * Local variables:
581 * c-indent-level: 8
582 * c-basic-offset: 8
583 * End:
584 */