blob: 558bb9ec1d98fa5e3753f4f135725de6fbb999ea [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 Tarreau957c0a52011-03-03 17:42:23 +0100154 else if (s->si[0].addr.c.from.ss_family == AF_INET) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200155 char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200156
Willy Tarreaueb472682010-05-28 18:46:57 +0200157 if (!(s->flags & SN_FRT_ADDR_SET))
158 get_frt_addr(s);
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200159
Willy Tarreau957c0a52011-03-03 17:42:23 +0100160 if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->si[0].addr.c.to)->sin_addr,
Willy Tarreaueb472682010-05-28 18:46:57 +0200161 sn, sizeof(sn)) &&
Willy Tarreau957c0a52011-03-03 17:42:23 +0100162 inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_addr,
Willy Tarreaueb472682010-05-28 18:46:57 +0200163 pn, sizeof(pn))) {
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 Tarreau957c0a52011-03-03 17:42:23 +0100165 pn, ntohs(((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_port),
166 sn, ntohs(((struct sockaddr_in *)&s->si[0].addr.c.to)->sin_port),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200167 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168 }
169 }
Willy Tarreau957c0a52011-03-03 17:42:23 +0100170 else if (s->si[0].addr.c.from.ss_family == AF_INET6) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200171 char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN];
Willy Tarreau14c8aac2007-05-08 19:46:30 +0200172
173 if (!(s->flags & SN_FRT_ADDR_SET))
174 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175
Willy Tarreau957c0a52011-03-03 17:42:23 +0100176 if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->si[0].addr.c.to)->sin6_addr,
Willy Tarreaueb472682010-05-28 18:46:57 +0200177 sn, sizeof(sn)) &&
Willy Tarreau957c0a52011-03-03 17:42:23 +0100178 inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_addr,
Willy Tarreaueb472682010-05-28 18:46:57 +0200179 pn, sizeof(pn))) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200180 send_log(s->fe, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n",
Willy Tarreau957c0a52011-03-03 17:42:23 +0100181 pn, ntohs(((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_port),
182 sn, ntohs(((struct sockaddr_in6 *)&s->si[0].addr.c.to)->sin6_port),
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200183 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200185 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200186 else {
187 /* UNIX socket, only the destination is known */
188 send_log(s->fe, LOG_INFO, "Connect to unix:%d (%s/%s)\n",
189 s->listener->luid,
190 s->fe->id, (s->fe->mode == PR_MODE_HTTP) ? "HTTP" : "TCP");
191 }
Willy Tarreaueb472682010-05-28 18:46:57 +0200192 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193
Willy Tarreau2281b7f2010-05-28 19:29:49 +0200194 if (unlikely((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200195 int len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196
Willy Tarreaueb472682010-05-28 18:46:57 +0200197 if (!(s->flags & SN_FRT_ADDR_SET))
198 get_frt_addr(s);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199
Willy Tarreau957c0a52011-03-03 17:42:23 +0100200 if (s->si[0].addr.c.from.ss_family == AF_INET) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200201 char pn[INET_ADDRSTRLEN];
202 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100203 (const void *)&((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_addr,
Willy Tarreaueb472682010-05-28 18:46:57 +0200204 pn, sizeof(pn));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205
Willy Tarreaueb472682010-05-28 18:46:57 +0200206 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200207 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100208 pn, ntohs(((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_port));
Willy Tarreaueb472682010-05-28 18:46:57 +0200209 }
Willy Tarreau957c0a52011-03-03 17:42:23 +0100210 else if (s->si[0].addr.c.from.ss_family == AF_INET6) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200211 char pn[INET6_ADDRSTRLEN];
212 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100213 (const void *)&((struct sockaddr_in6 *)(&s->si[0].addr.c.from))->sin6_addr,
Willy Tarreaueb472682010-05-28 18:46:57 +0200214 pn, sizeof(pn));
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200215
Willy Tarreaueb472682010-05-28 18:46:57 +0200216 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n",
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200217 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100218 pn, ntohs(((struct sockaddr_in6 *)(&s->si[0].addr.c.from))->sin6_port));
Willy Tarreaueb472682010-05-28 18:46:57 +0200219 }
Emeric Brunab844ea2010-10-22 16:33:18 +0200220 else {
221 len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [unix:%d]\n",
222 s->uniq_id, s->fe->id, (unsigned short)s->listener->fd, (unsigned short)cfd,
223 s->listener->luid);
224 }
Willy Tarreau9a2d1542008-08-30 12:31:07 +0200225
Willy Tarreaueb472682010-05-28 18:46:57 +0200226 write(1, trash, len);
227 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200228
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200229 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreaueb472682010-05-28 18:46:57 +0200230 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231
Willy Tarreaueb472682010-05-28 18:46:57 +0200232 /* note: this should not happen anymore since there's always at least the switching rules */
233 if (!s->req->analysers) {
234 buffer_auto_connect(s->req); /* don't wait to establish connection */
235 buffer_auto_close(s->req); /* let the producer forward close requests */
236 }
Willy Tarreaud7971282006-07-29 18:36:34 +0200237
Willy Tarreaueb472682010-05-28 18:46:57 +0200238 s->req->rto = s->fe->timeout.client;
Willy Tarreaueb472682010-05-28 18:46:57 +0200239 s->rep->wto = s->fe->timeout.client;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240
Willy Tarreaueb472682010-05-28 18:46:57 +0200241 fdtab[cfd].flags = FD_FL_TCP | FD_FL_TCP_NODELAY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200242 if (s->fe->options & PR_O_TCP_NOLING)
Willy Tarreaueb472682010-05-28 18:46:57 +0200243 fdtab[cfd].flags |= FD_FL_TCP_NOLING;
Willy Tarreau6e6fb2b2009-08-16 18:20:44 +0200244
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200245 if (unlikely((s->fe->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) ||
246 (s->fe->mode == PR_MODE_HEALTH && (s->fe->options & PR_O_HTTP_CHK)))) {
Willy Tarreaueb472682010-05-28 18:46:57 +0200247 /* Either we got a request from a monitoring system on an HTTP instance,
248 * or we're in health check mode with the 'httpchk' option enabled. In
249 * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit.
250 */
251 struct chunk msg;
252 chunk_initstr(&msg, "HTTP/1.0 200 OK\r\n\r\n");
253 stream_int_retnclose(&s->si[0], &msg); /* forge a 200 response */
254 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200255 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200256 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200257 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200258 else if (unlikely(s->fe->mode == PR_MODE_HEALTH)) { /* health check mode, no client reading */
Willy Tarreaueb472682010-05-28 18:46:57 +0200259 struct chunk msg;
260 chunk_initstr(&msg, "OK\n");
261 stream_int_retnclose(&s->si[0], &msg); /* forge an "OK" response */
262 s->req->analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200263 s->task->expire = s->rep->wex;
Willy Tarreau35a09942010-06-01 17:12:40 +0200264 EV_FD_CLR(cfd, DIR_RD);
Willy Tarreaueb472682010-05-28 18:46:57 +0200265 }
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200266 /* everything's OK, let's go on */
Willy Tarreaueb472682010-05-28 18:46:57 +0200267 return 1;
Willy Tarreau8ced9a42007-11-04 17:51:50 +0100268
269 /* Error unrolling */
Willy Tarreau35a09942010-06-01 17:12:40 +0200270 out_free_rspcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200271 pool_free2(s->fe->rsp_cap_pool, s->txn.rsp.cap);
Willy Tarreau35a09942010-06-01 17:12:40 +0200272 out_free_reqcap:
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200273 pool_free2(s->fe->req_cap_pool, s->txn.req.cap);
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100274 out_return:
Willy Tarreaueb472682010-05-28 18:46:57 +0200275 return -1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200276}
277
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200278/* This analyser tries to fetch a line from the request buffer which looks like :
279 *
280 * "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
281 *
282 * There must be exactly one space between each field. Fields are :
Emeric Brun861ccff2010-10-29 12:03:03 +0200283 * - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200284 * - SRC3 : layer 3 (eg: IP) source address in standard text form
285 * - DST3 : layer 3 (eg: IP) destination address in standard text form
286 * - SRC4 : layer 4 (eg: TCP port) source address in standard text form
287 * - DST4 : layer 4 (eg: TCP port) destination address in standard text form
288 *
289 * This line MUST be at the beginning of the buffer and MUST NOT wrap.
290 *
291 * Once the data is fetched, the values are set in the session's field and data
292 * are removed from the buffer. The function returns zero if it needs to wait
293 * for more data (max: timeout_client), or 1 if it has finished and removed itself.
294 */
295int frontend_decode_proxy_request(struct session *s, struct buffer *req, int an_bit)
296{
297 char *line = req->data;
298 char *end = req->data + req->l;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200299 int len;
300
301 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
302 now_ms, __FUNCTION__,
303 s,
304 req,
305 req->rex, req->wex,
306 req->flags,
307 req->l,
308 req->analysers);
309
310 if (req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT))
311 goto fail;
312
Emeric Brunf4711a32010-10-29 15:16:55 +0200313 len = MIN(req->l, 6);
314 if (!len)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200315 goto missing;
316
Emeric Brunf4711a32010-10-29 15:16:55 +0200317 /* Decode a possible proxy request, fail early if it does not match */
318 if (strncmp(line, "PROXY ", len) != 0)
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200319 goto fail;
Emeric Brunf4711a32010-10-29 15:16:55 +0200320
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200321 line += 6;
Emeric Brunf4711a32010-10-29 15:16:55 +0200322 if (req->l < 18) /* shortest possible line */
323 goto missing;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200324
Emeric Brun861ccff2010-10-29 12:03:03 +0200325 if (!memcmp(line, "TCP4 ", 5) != 0) {
326 u32 src3, dst3, sport, dport;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200327
Emeric Brun861ccff2010-10-29 12:03:03 +0200328 line += 5;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200329
Emeric Brun861ccff2010-10-29 12:03:03 +0200330 src3 = inetaddr_host_lim_ret(line, end, &line);
331 if (line == end)
332 goto missing;
333 if (*line++ != ' ')
334 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200335
Emeric Brun861ccff2010-10-29 12:03:03 +0200336 dst3 = inetaddr_host_lim_ret(line, end, &line);
337 if (line == end)
338 goto missing;
339 if (*line++ != ' ')
340 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200341
Emeric Brun861ccff2010-10-29 12:03:03 +0200342 sport = read_uint((const char **)&line, end);
343 if (line == end)
344 goto missing;
345 if (*line++ != ' ')
346 goto fail;
347
348 dport = read_uint((const char **)&line, end);
349 if (line > end - 2)
350 goto missing;
351 if (*line++ != '\r')
352 goto fail;
353 if (*line++ != '\n')
354 goto fail;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200355
Emeric Brun861ccff2010-10-29 12:03:03 +0200356 /* update the session's addresses and mark them set */
Willy Tarreau957c0a52011-03-03 17:42:23 +0100357 ((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_family = AF_INET;
358 ((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_addr.s_addr = htonl(src3);
359 ((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_port = htons(sport);
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200360
Willy Tarreau957c0a52011-03-03 17:42:23 +0100361 ((struct sockaddr_in *)&s->si[0].addr.c.to)->sin_family = AF_INET;
362 ((struct sockaddr_in *)&s->si[0].addr.c.to)->sin_addr.s_addr = htonl(dst3);
363 ((struct sockaddr_in *)&s->si[0].addr.c.to)->sin_port = htons(dport);
Emeric Brun861ccff2010-10-29 12:03:03 +0200364 s->flags |= SN_FRT_ADDR_SET;
365
366 }
367 else if (!memcmp(line, "TCP6 ", 5) != 0) {
368 u32 sport, dport;
369 char *src_s;
370 char *dst_s, *sport_s, *dport_s;
371 struct in6_addr src3, dst3;
372
373 line+=5;
374
375 src_s = line;
376 dst_s = sport_s = dport_s = NULL;
377 while (1) {
378 if (line > end - 2) {
379 goto missing;
380 }
381 else if (*line == '\r') {
382 *line = 0;
383 line++;
384 if (*line++ != '\n')
385 goto fail;
386 break;
387 }
388
389 if (*line == ' ') {
390 *line = 0;
391 if (!dst_s)
392 dst_s = line+1;
393 else if (!sport_s)
394 sport_s = line+1;
395 else if (!dport_s)
396 dport_s = line+1;
397 }
398 line++;
399 }
400
401 if (!dst_s || !sport_s || !dport_s)
402 goto fail;
403
404 sport = read_uint((const char **)&sport_s,dport_s-1);
405 if ( *sport_s != 0 )
406 goto fail;
407
408 dport = read_uint((const char **)&dport_s,line-2);
409 if ( *dport_s != 0 )
410 goto fail;
411
412 if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
413 goto fail;
414
415 if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
416 goto fail;
417
418 /* update the session's addresses and mark them set */
Willy Tarreau957c0a52011-03-03 17:42:23 +0100419 ((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_family = AF_INET6;
420 memcpy(&((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_addr, &src3, sizeof(struct in6_addr));
421 ((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_port = htons(sport);
Emeric Brun861ccff2010-10-29 12:03:03 +0200422
Willy Tarreau957c0a52011-03-03 17:42:23 +0100423 ((struct sockaddr_in6 *)&s->si[0].addr.c.to)->sin6_family = AF_INET6;
424 memcpy(&((struct sockaddr_in6 *)&s->si[0].addr.c.to)->sin6_addr, &dst3, sizeof(struct in6_addr));
425 ((struct sockaddr_in6 *)&s->si[0].addr.c.to)->sin6_port = htons(dport);
Emeric Brun861ccff2010-10-29 12:03:03 +0200426 s->flags |= SN_FRT_ADDR_SET;
427 }
428 else {
429 goto fail;
430 }
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200431
432 /* remove the PROXY line from the request */
433 len = line - req->data;
434 buffer_replace2(req, req->data, line, NULL, 0);
435 req->total -= len; /* don't count the header line */
436
437 req->analysers &= ~an_bit;
438 return 1;
439
440 missing:
441 if (!(req->flags & (BF_SHUTR|BF_FULL))) {
442 buffer_dont_connect(s->req);
443 return 0;
444 }
445 /* missing data and buffer is either full or shutdown => fail */
446
447 fail:
448 buffer_abort(req);
449 buffer_abort(s->rep);
450 req->analysers = 0;
451
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100452 s->fe->fe_counters.failed_req++;
Willy Tarreau8b0cbf92010-10-15 23:23:19 +0200453 if (s->listener->counters)
454 s->listener->counters->failed_req++;
455
456 if (!(s->flags & SN_ERR_MASK))
457 s->flags |= SN_ERR_PRXCOND;
458 if (!(s->flags & SN_FINST_MASK))
459 s->flags |= SN_FINST_R;
460 return 0;
461}
462
Willy Tarreaua73fcaf2011-03-20 10:15:22 +0100463/* Makes a PROXY protocol line from the two addresses. The output is sent to
464 * buffer <buf> for a maximum size of <buf_len> (including the trailing zero).
465 * It returns the number of bytes composing this line (including the trailing
466 * LF), or zero in case of failure (eg: not enough space). It supports TCP4,
467 * TCP6 and "UNKNOWN" formats.
468 */
469int make_proxy_line(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst)
470{
471 int ret = 0;
472
473 if (src->ss_family == dst->ss_family && src->ss_family == AF_INET) {
474 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP4 ");
475 if (ret >= buf_len)
476 return 0;
477
478 /* IPv4 src */
479 if (!inet_ntop(src->ss_family, &((struct sockaddr_in *)src)->sin_addr, buf + ret, buf_len - ret))
480 return 0;
481
482 ret += strlen(buf + ret);
483 if (ret >= buf_len)
484 return 0;
485
486 buf[ret++] = ' ';
487
488 /* IPv4 dst */
489 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in *)dst)->sin_addr, buf + ret, buf_len - ret))
490 return 0;
491
492 ret += strlen(buf + ret);
493 if (ret >= buf_len)
494 return 0;
495
496 /* source and destination ports */
497 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
498 ntohs(((struct sockaddr_in *)src)->sin_port),
499 ntohs(((struct sockaddr_in *)dst)->sin_port));
500 if (ret >= buf_len)
501 return 0;
502 }
503 else if (src->ss_family == dst->ss_family && src->ss_family == AF_INET6) {
504 ret = snprintf(buf + ret, buf_len - ret, "PROXY TCP6 ");
505 if (ret >= buf_len)
506 return 0;
507
508 /* IPv6 src */
509 if (!inet_ntop(src->ss_family, &((struct sockaddr_in6 *)src)->sin6_addr, buf + ret, buf_len - ret))
510 return 0;
511
512 ret += strlen(buf + ret);
513 if (ret >= buf_len)
514 return 0;
515
516 buf[ret++] = ' ';
517
518 /* IPv6 dst */
519 if (!inet_ntop(dst->ss_family, &((struct sockaddr_in6 *)dst)->sin6_addr, buf + ret, buf_len - ret))
520 return 0;
521
522 ret += strlen(buf + ret);
523 if (ret >= buf_len)
524 return 0;
525
526 /* source and destination ports */
527 ret += snprintf(buf + ret, buf_len - ret, " %u %u\r\n",
528 ntohs(((struct sockaddr_in6 *)src)->sin6_port),
529 ntohs(((struct sockaddr_in6 *)dst)->sin6_port));
530 if (ret >= buf_len)
531 return 0;
532 }
533 else {
534 /* unknown family combination */
535 ret = snprintf(buf, buf_len, "PROXY UNKNOWN\r\n");
536 if (ret >= buf_len)
537 return 0;
538 }
539 return ret;
540}
541
Willy Tarreau645513a2010-05-24 20:55:15 +0200542/* set test->i to the id of the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200543static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200544acl_fetch_fe_id(struct proxy *px, struct session *l4, void *l7, int dir,
545 struct acl_expr *expr, struct acl_test *test) {
Willy Tarreau662b2d82007-05-08 19:56:15 +0200546
Willy Tarreau662b2d82007-05-08 19:56:15 +0200547 test->flags = ACL_TEST_F_READ_ONLY;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200548
Willy Tarreau645513a2010-05-24 20:55:15 +0200549 test->i = l4->fe->uuid;
Willy Tarreau662b2d82007-05-08 19:56:15 +0200550
Emeric Brun5d16eda2010-01-04 15:47:45 +0100551 return 1;
552}
553
Willy Tarreau645513a2010-05-24 20:55:15 +0200554/* set test->i to the number of connections per second reaching the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200555static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200556acl_fetch_fe_sess_rate(struct proxy *px, struct session *l4, void *l7, int dir,
557 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau662b2d82007-05-08 19:56:15 +0200558{
Willy Tarreau645513a2010-05-24 20:55:15 +0200559 test->flags = ACL_TEST_F_VOL_TEST;
560 if (expr->arg_len) {
561 /* another proxy was designated, we must look for it */
562 for (px = proxy; px; px = px->next)
563 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
564 break;
565 }
566 if (!px)
567 return 0;
Emeric Brun5d16eda2010-01-04 15:47:45 +0100568
Willy Tarreau645513a2010-05-24 20:55:15 +0200569 test->i = read_freq_ctr(&px->fe_sess_per_sec);
Emeric Brun5d16eda2010-01-04 15:47:45 +0100570 return 1;
571}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100572
Willy Tarreau645513a2010-05-24 20:55:15 +0200573/* set test->i to the number of concurrent connections on the frontend */
Willy Tarreaud41f8d82007-06-10 10:06:18 +0200574static int
Willy Tarreau645513a2010-05-24 20:55:15 +0200575acl_fetch_fe_conn(struct proxy *px, struct session *l4, void *l7, int dir,
576 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +0200577{
Willy Tarreau645513a2010-05-24 20:55:15 +0200578 test->flags = ACL_TEST_F_VOL_TEST;
579 if (expr->arg_len) {
580 /* another proxy was designated, we must look for it */
581 for (px = proxy; px; px = px->next)
582 if ((px->cap & PR_CAP_FE) && !strcmp(px->id, expr->arg.str))
583 break;
584 }
585 if (!px)
586 return 0;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100587
Willy Tarreau645513a2010-05-24 20:55:15 +0200588 test->i = px->feconn;
Krzysztof Piotr Oledzki346f76d2010-01-12 21:59:30 +0100589 return 1;
590}
591
Willy Tarreau8797c062007-05-07 00:55:35 +0200592
593/* Note: must not be declared <const> as its list will be overwritten */
594static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau645513a2010-05-24 20:55:15 +0200595 { "fe_id", acl_parse_int, acl_fetch_fe_id, acl_match_int, ACL_USE_NOTHING },
596 { "fe_sess_rate", acl_parse_int, acl_fetch_fe_sess_rate, acl_match_int, ACL_USE_NOTHING },
597 { "fe_conn", acl_parse_int, acl_fetch_fe_conn, acl_match_int, ACL_USE_NOTHING },
Willy Tarreau8797c062007-05-07 00:55:35 +0200598 { NULL, NULL, NULL, NULL },
599}};
600
601
602__attribute__((constructor))
Willy Tarreau03fa5df2010-05-24 21:02:37 +0200603static void __frontend_init(void)
Willy Tarreau8797c062007-05-07 00:55:35 +0200604{
605 acl_register_keywords(&acl_kws);
606}
607
608
Willy Tarreaubaaee002006-06-26 02:48:02 +0200609/*
610 * Local variables:
611 * c-indent-level: 8
612 * c-basic-offset: 8
613 * End:
614 */