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