blob: 4ed4862daea4a964069fc84acf62547cd5872df0 [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 Tarreau957c0a52011-03-03 17:42:23 +010053 socklen_t namelen = sizeof(s->si[0].addr.c.to);
Willy Tarreau14c8aac2007-05-08 19:46:30 +020054
Willy Tarreau957c0a52011-03-03 17:42:23 +010055 if (get_original_dst(s->si[0].fd, (struct sockaddr_in *)&s->si[0].addr.c.to, &namelen) == -1)
56 getsockname(s->si[0].fd, (struct sockaddr *)&s->si[0].addr.c.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 */
Willy Tarreau35a09942010-06-01 17:12:40 +020081 if (s->logs.logwait & LW_REQ)
82 s->do_log = http_sess_log;
83 else
84 s->do_log = tcp_sess_log;
85
86 /* default error reporting function, may be changed by analysers */
87 s->srv_error = default_srv_error;
Willy Tarreaubaaee002006-06-26 02:48:02 +020088
Willy Tarreauf67c9782010-05-23 22:59:00 +020089 /* Adjust some socket options */
Willy Tarreau9c3bc222010-12-24 14:49:37 +010090 if (s->listener->addr.ss_family == AF_INET || s->listener->addr.ss_family == AF_INET6) {
91 if (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY,
92 (char *) &one, sizeof(one)) == -1)
93 goto out_return;
Willy Tarreauf67c9782010-05-23 22:59:00 +020094
Willy Tarreau9c3bc222010-12-24 14:49:37 +010095 if (s->fe->options & PR_O_TCP_CLI_KA)
96 setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE,
97 (char *) &one, sizeof(one));
Willy Tarreauf67c9782010-05-23 22:59:00 +020098
Willy Tarreau9c3bc222010-12-24 14:49:37 +010099 if (s->fe->options & PR_O_TCP_NOLING)
100 setsockopt(cfd, SOL_SOCKET, SO_LINGER,
101 (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreau48a7e722010-12-24 15:26:39 +0100102#if defined(TCP_MAXSEG)
103 if (s->listener->maxseg < 0) {
104 /* we just want to reduce the current MSS by that value */
105 int mss;
Willy Tarreau7d286a02011-01-05 15:42:54 +0100106 socklen_t mss_len = sizeof(mss);
Willy Tarreau48a7e722010-12-24 15:26:39 +0100107 if (getsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG, &mss, &mss_len) == 0) {
108 mss += s->listener->maxseg; /* remember, it's < 0 */
109 setsockopt(cfd, IPPROTO_TCP, TCP_MAXSEG, &mss, sizeof(mss));
110 }
111 }
112#endif
Willy Tarreau9c3bc222010-12-24 14:49:37 +0100113 }
Willy Tarreauf67c9782010-05-23 22:59:00 +0200114
115 if (global.tune.client_sndbuf)
116 setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, &global.tune.client_sndbuf, sizeof(global.tune.client_sndbuf));
117
118 if (global.tune.client_rcvbuf)
119 setsockopt(cfd, SOL_SOCKET, SO_RCVBUF, &global.tune.client_rcvbuf, sizeof(global.tune.client_rcvbuf));
120
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200121 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200122 /* the captures are only used in HTTP frontends */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200123 if (unlikely(s->fe->nb_req_cap > 0 &&
124 (s->txn.req.cap = pool_alloc2(s->fe->req_cap_pool)) == NULL))
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100125 goto out_return; /* no memory */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200127 if (unlikely(s->fe->nb_rsp_cap > 0 &&
128 (s->txn.rsp.cap = pool_alloc2(s->fe->rsp_cap_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200129 goto out_free_reqcap; /* no memory */
Willy Tarreaueb472682010-05-28 18:46:57 +0200130 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200132 if (s->fe->acl_requires & ACL_USE_L7_ANY) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200133 /* we have to allocate header indexes only if we know
134 * that we may make use of them. This of course includes
135 * (mode == PR_MODE_HTTP).
Willy Tarreau042cc792007-03-19 16:20:06 +0100136 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200137 s->txn.hdr_idx.size = MAX_HTTP_HDR;
Willy Tarreau45e73e32006-12-17 00:05:15 +0100138
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200139 if (unlikely((s->txn.hdr_idx.v = pool_alloc2(s->fe->hdr_idx_pool)) == NULL))
Willy Tarreau35a09942010-06-01 17:12:40 +0200140 goto out_free_rspcap; /* no memory */
Willy Tarreau45e73e32006-12-17 00:05:15 +0100141
Willy Tarreaueb472682010-05-28 18:46:57 +0200142 /* and now initialize the HTTP transaction state */
143 http_init_txn(s);
144 }
Willy Tarreaue5f20dc2006-12-03 15:21:35 +0100145
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200146 if ((s->fe->mode == PR_MODE_TCP || s->fe->mode == PR_MODE_HTTP)
147 && (s->fe->logfac1 >= 0 || s->fe->logfac2 >= 0)) {
148 if (likely(s->fe->to_log)) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200149 /* we have the client ip */
150 if (s->logs.logwait & LW_CLIP)
151 if (!(s->logs.logwait &= ~LW_CLIP))
152 s->do_log(s);
Willy Tarreaua3445fc2010-05-20 16:17:07 +0200153 }
Willy Tarreau631f01c2011-09-05 00:36:48 +0200154 else {
Willy Tarreaueb472682010-05-28 18:46:57 +0200155 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200156
157 if (!(s->flags & SN_FRT_ADDR_SET))
158 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159
Willy Tarreau631f01c2011-09-05 00:36:48 +0200160 switch (addr_to_str(&s->req->prod->addr.c.from, pn, sizeof(pn))) {
161 case AF_INET:
162 case AF_INET6:
163 addr_to_str(&s->req->prod->addr.c.to, sn, sizeof(sn));
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200164 send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreau631f01c2011-09-05 00:36:48 +0200165 pn, get_host_port(&s->req->prod->addr.c.from),
166 sn, get_host_port(&s->req->prod->addr.c.to),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200167 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreau631f01c2011-09-05 00:36:48 +0200168 break;
169 case AF_UNIX:
170 /* UNIX socket, only the destination is known */
171 send_log(s->fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
172 s->listener->luid,
173 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
174 break;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200176 }
177 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200179 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau631f01c2011-09-05 00:36:48 +0200180 char pn[INET6_ADDRSTRLEN];
Willy Tarreaub0f75322011-09-09 11:21:06 +0200181 int len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182
Willy Tarreaueb472682010-05-28 18:46:57 +0200183 if (!(s->flags & SN_FRT_ADDR_SET))
184 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185
Willy Tarreau631f01c2011-09-05 00:36:48 +0200186 switch (addr_to_str(&s->req->prod->addr.c.from, pn, sizeof(pn))) {
187 case AF_INET:
188 case AF_INET6:
Willy Tarreaueb472682010-05-28 18:46:57 +0200189 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200190 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreau631f01c2011-09-05 00:36:48 +0200191 pn, get_host_port(&s->req->prod->addr.c.from));
192 break;
193 case AF_UNIX:
194 /* UNIX socket, only the destination is known */
Emeric Brunab844ea2010-10-22 16:33:18 +0200195 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [unix:%d]\n",
196 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
197 s->listener->luid);
Willy Tarreau631f01c2011-09-05 00:36:48 +0200198 break;
Emeric Brunab844ea2010-10-22 16:33:18 +0200199 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200200
Willy Tarreaueb472682010-05-28 18:46:57 +0200201 write(1, trash, len);
202 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200204 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreaueb472682010-05-28 18:46:57 +0200205 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206
Willy Tarreaueb472682010-05-28 18:46:57 +0200207 /* note: this should not happen anymore since there's always at least the switching rules */
208 if (!s->req->analysers) {
209 buffer_auto_connect(s->req); /* don't wait to establish connection */
210 buffer_auto_close(s->req); /* let the producer forward close requests */
211 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200212
Willy Tarreaueb472682010-05-28 18:46:57 +0200213 s->req->rto = s->fe->timeout.client;
Willy Tarreaueb472682010-05-28 18:46:57 +0200214 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215
Willy Tarreaueb472682010-05-28 18:46:57 +0200216 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200217 if (s->fe->options & PR_O_TCP_NOLING)
Willy Tarreaueb472682010-05-28 18:46:57 +0200218 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200219
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200220 if (unlikely((s->fe->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
Willy Tarreau1620ec32011-08-06 17:05:02 +0200221 (s->fe->mode == PR_MODE_HEALTH && ((s->fe->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200222 /* Either we got a request from a monitoring system on an HTTP instance,
223 * or we're in health check mode with the 'httpchk' option enabled. In
224 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
225 */
226 struct chunk msg;
227 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
228 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
229 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200230 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200231 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200232 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200233 else if (unlikely(s->fe->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
Willy Tarreaueb472682010-05-28 18:46:57 +0200234 struct chunk msg;
235 chunk_initstr(&msg, "OK\n");
236 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
237 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200238 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200239 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200240 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200241 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200242 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100243
244 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200245 out_free_rspcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200246 pool_free2(s->fe->rsp_cap_pool, s->txn.rsp.cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200247 out_free_reqcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200248 pool_free2(s->fe->req_cap_pool, s->txn.req.cap);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100249 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200250 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251}
252
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200253/* This analyser tries to fetch a line from the request buffer which looks like :
254 *
255 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
256 *
257 * There must be exactly one space between each field. Fields are :
Emeric Brun861ccff2010-10-29 12:03:03 +0200258 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200259 * - SRC3 : layer 3 (eg: IP) source address in standard text form
260 * - DST3 : layer 3 (eg: IP) destination address in standard text form
261 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
262 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
263 *
264 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
265 *
266 * Once the data is fetched, the values are set in the session's field and data
267 * are removed from the buffer. The function returns zero if it needs to wait
268 * for more data (max: timeout_client), or 1 if it has finished and removed itself.
269 */
270int frontend_decode_proxy_request(struct session *s, struct buffer *req, int an_bit)
271{
272 char *line = req->data;
273 char *end = req->data + req->l;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200274 int len;
275
276 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
277 now_ms, __FUNCTION__,
278 s,
279 req,
280 req->rex, req->wex,
281 req->flags,
282 req->l,
283 req->analysers);
284
285 if (req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT))
286 goto fail;
287
Emeric Brunf4711a32010-10-29 15:16:55 +0200288 len = MIN(req->l, 6);
289 if (!len)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200290 goto missing;
291
Emeric Brunf4711a32010-10-29 15:16:55 +0200292 /* Decode a possible proxy request, fail early if it does not match */
293 if (strncmp(line, "PROXY ", len) != 0)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200294 goto fail;
Emeric Brunf4711a32010-10-29 15:16:55 +0200295
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200296 line += 6;
Emeric Brunf4711a32010-10-29 15:16:55 +0200297 if (req->l < 18) /* shortest possible line */
298 goto missing;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200299
Emeric Brun861ccff2010-10-29 12:03:03 +0200300 if (!memcmp(line, "TCP4 ", 5) != 0) {
301 u32 src3, dst3, sport, dport;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200302
Emeric Brun861ccff2010-10-29 12:03:03 +0200303 line += 5;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200304
Emeric Brun861ccff2010-10-29 12:03:03 +0200305 src3 = inetaddr_host_lim_ret(line, end, &line);
306 if (line == end)
307 goto missing;
308 if (*line++ != ' ')
309 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200310
Emeric Brun861ccff2010-10-29 12:03:03 +0200311 dst3 = inetaddr_host_lim_ret(line, end, &line);
312 if (line == end)
313 goto missing;
314 if (*line++ != ' ')
315 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200316
Emeric Brun861ccff2010-10-29 12:03:03 +0200317 sport = read_uint((const char **)&line, end);
318 if (line == end)
319 goto missing;
320 if (*line++ != ' ')
321 goto fail;
322
323 dport = read_uint((const char **)&line, end);
324 if (line > end - 2)
325 goto missing;
326 if (*line++ != '\r')
327 goto fail;
328 if (*line++ != '\n')
329 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200330
Emeric Brun861ccff2010-10-29 12:03:03 +0200331 /* update the session's addresses and mark them set */
Willy Tarreau957c0a52011-03-03 17:42:23 +0100332 ((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_family = AF_INET;
333 ((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_addr.s_addr = htonl(src3);
334 ((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_port = htons(sport);
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200335
Willy Tarreau957c0a52011-03-03 17:42:23 +0100336 ((struct sockaddr_in *)&s->si[0].addr.c.to)->sin_family = AF_INET;
337 ((struct sockaddr_in *)&s->si[0].addr.c.to)->sin_addr.s_addr = htonl(dst3);
338 ((struct sockaddr_in *)&s->si[0].addr.c.to)->sin_port = htons(dport);
Emeric Brun861ccff2010-10-29 12:03:03 +0200339 s->flags |= SN_FRT_ADDR_SET;
340
341 }
342 else if (!memcmp(line, "TCP6 ", 5) != 0) {
343 u32 sport, dport;
344 char *src_s;
345 char *dst_s, *sport_s, *dport_s;
346 struct in6_addr src3, dst3;
347
348 line+=5;
349
350 src_s = line;
351 dst_s = sport_s = dport_s = NULL;
352 while (1) {
353 if (line > end - 2) {
354 goto missing;
355 }
356 else if (*line == '\r') {
357 *line = 0;
358 line++;
359 if (*line++ != '\n')
360 goto fail;
361 break;
362 }
363
364 if (*line == ' ') {
365 *line = 0;
366 if (!dst_s)
367 dst_s = line+1;
368 else if (!sport_s)
369 sport_s = line+1;
370 else if (!dport_s)
371 dport_s = line+1;
372 }
373 line++;
374 }
375
376 if (!dst_s || !sport_s || !dport_s)
377 goto fail;
378
379 sport = read_uint((const char **)&sport_s,dport_s-1);
380 if ( *sport_s != 0 )
381 goto fail;
382
383 dport = read_uint((const char **)&dport_s,line-2);
384 if ( *dport_s != 0 )
385 goto fail;
386
387 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
388 goto fail;
389
390 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
391 goto fail;
392
393 /* update the session's addresses and mark them set */
Willy Tarreau957c0a52011-03-03 17:42:23 +0100394 ((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_family = AF_INET6;
395 memcpy(&((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_addr, &src3, sizeof(struct in6_addr));
396 ((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_port = htons(sport);
Emeric Brun861ccff2010-10-29 12:03:03 +0200397
Willy Tarreau957c0a52011-03-03 17:42:23 +0100398 ((struct sockaddr_in6 *)&s->si[0].addr.c.to)->sin6_family = AF_INET6;
399 memcpy(&((struct sockaddr_in6 *)&s->si[0].addr.c.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
400 ((struct sockaddr_in6 *)&s->si[0].addr.c.to)->sin6_port = htons(dport);
Emeric Brun861ccff2010-10-29 12:03:03 +0200401 s->flags |= SN_FRT_ADDR_SET;
402 }
403 else {
404 goto fail;
405 }
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200406
407 /* remove the PROXY line from the request */
408 len = line - req->data;
409 buffer_replace2(req, req->data, line, NULL, 0);
410 req->total -= len; /* don't count the header line */
411
412 req->analysers &= ~an_bit;
413 return 1;
414
415 missing:
416 if (!(req->flags & (BF_SHUTR|BF_FULL))) {
417 buffer_dont_connect(s->req);
418 return 0;
419 }
420 /* missing data and buffer is either full or shutdown => fail */
421
422 fail:
423 buffer_abort(req);
424 buffer_abort(s->rep);
425 req->analysers = 0;
426
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100427 s->fe->fe_counters.failed_req++;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200428 if (s->listener->counters)
429 s->listener->counters->failed_req++;
430
431 if (!(s->flags & SN_ERR_MASK))
432 s->flags |= SN_ERR_PRXCOND;
433 if (!(s->flags & SN_FINST_MASK))
434 s->flags |= SN_FINST_R;
435 return 0;
436}
437
Willy Tarreaua73fcaf2011-03-20 10:15:22 +0100438/* Makes a PROXY protocol line from the two addresses. The output is sent to
439 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
440 * It returns the number of bytes composing this line (including the trailing
441 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
442 * TCP6 and "UNKNOWN" formats.
443 */
444int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
445{
446 int ret = 0;
447
448 if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
449 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
450 if (ret >= buf_len)
451 return 0;
452
453 /* IPv4 src */
454 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
455 return 0;
456
457 ret += strlen(buf + ret);
458 if (ret >= buf_len)
459 return 0;
460
461 buf[ret++] = ' ';
462
463 /* IPv4 dst */
464 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
465 return 0;
466
467 ret += strlen(buf + ret);
468 if (ret >= buf_len)
469 return 0;
470
471 /* source and destination ports */
472 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
473 ntohs(((struct sockaddr_in *)src)->sin_port),
474 ntohs(((struct sockaddr_in *)dst)->sin_port));
475 if (ret >= buf_len)
476 return 0;
477 }
478 else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
479 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
480 if (ret >= buf_len)
481 return 0;
482
483 /* IPv6 src */
484 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
485 return 0;
486
487 ret += strlen(buf + ret);
488 if (ret >= buf_len)
489 return 0;
490
491 buf[ret++] = ' ';
492
493 /* IPv6 dst */
494 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
495 return 0;
496
497 ret += strlen(buf + ret);
498 if (ret >= buf_len)
499 return 0;
500
501 /* source and destination ports */
502 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
503 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
504 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
505 if (ret >= buf_len)
506 return 0;
507 }
508 else {
509 /* unknown family combination */
510 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
511 if (ret >= buf_len)
512 return 0;
513 }
514 return ret;
515}
516
Willy Tarreau645513a2010-05-24 20:55:15 +0200517/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200518static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200519acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
520 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200521
Willy Tarreau662b2d82007-05-08 19:56:15 +0200522 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200523
Willy Tarreau645513a2010-05-24 20:55:15 +0200524 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200525
Emeric Brun5d16eda2010-01-04 15:47:45 +0100526 return 1;
527}
528
Willy Tarreau645513a2010-05-24 20:55:15 +0200529/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200530static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200531acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
532 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200533{
Willy Tarreau645513a2010-05-24 20:55:15 +0200534 test->flags = ACL_TEST_F_VOL_TEST;
535 if (expr->arg_len) {
536 /* another proxy was designated, we must look for it */
537 for (px = proxy; px; px = px->next)
538 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
539 break;
540 }
541 if (!px)
542 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100543
Willy Tarreau645513a2010-05-24 20:55:15 +0200544 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100545 return 1;
546}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100547
Willy Tarreau645513a2010-05-24 20:55:15 +0200548/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200549static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200550acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
551 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200552{
Willy Tarreau645513a2010-05-24 20:55:15 +0200553 test->flags = ACL_TEST_F_VOL_TEST;
554 if (expr->arg_len) {
555 /* another proxy was designated, we must look for it */
556 for (px = proxy; px; px = px->next)
557 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
558 break;
559 }
560 if (!px)
561 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100562
Willy Tarreau645513a2010-05-24 20:55:15 +0200563 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100564 return 1;
565}
566
Willy Tarreau8797c062007-05-07 00:55:35 +0200567
568/* Note: must not be declared <const> as its list will be overwritten */
569static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200570 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
571 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
572 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200573 { NULL, NULL, NULL, NULL },
574}};
575
576
577__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200578static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200579{
580 acl_register_keywords(&acl_kws);
581}
582
583
Willy Tarreaubaaee002006-06-26 02:48:02 +0200584/*
585 * Local variables:
586 * c-indent-level: 8
587 * c-basic-offset: 8
588 * End:
589 */