package: haproxy
[packages.git] / net / haproxy / patches / 001-haproxy-1.4.x-sendproxy.patch
1 --- a/doc/configuration.txt
2 +++ b/doc/configuration.txt
3 @@ -1339,6 +1339,7 @@ bind [<address>]:<port_range> [, ...] tr
4  bind [<address>]:<port_range> [, ...] id <id>
5  bind [<address>]:<port_range> [, ...] name <name>
6  bind [<address>]:<port_range> [, ...] defer-accept
7 +bind [<address>]:<port_range> [, ...] accept-proxy
8    Define one or several listening addresses and/or ports in a frontend.
9    May be used in sections :   defaults | frontend | listen | backend
10                                    no   |    yes   |   yes  |   no
11 @@ -1419,6 +1420,19 @@ bind [<address>]:<port_range> [, ...] de
12                    with front firewalls which would see an established
13                    connection while the proxy will only see it in SYN_RECV.
14  
15 +    accept-proxy  is an optional keyword which enforces use of the PROXY
16 +                  protocol over any connection accepted by this listener. The
17 +                  PROXY protocol dictates the layer 3/4 addresses of the
18 +                  incoming connection to be used everywhere an address is used,
19 +                  with the only exception of "tcp-request connection" rules
20 +                  which will only see the real connection address. Logs will
21 +                  reflect the addresses indicated in the protocol, unless it is
22 +                  violated, in which case the real address will still be used.
23 +                  This keyword combined with support from external components
24 +                  can be used as an efficient and reliable alternative to the
25 +                  X-Forwarded-For mechanism which is not always reliable and
26 +                  not even always usable.
27 +
28    It is possible to specify a list of address:port combinations delimited by
29    commas. The frontend will then listen on all of these addresses. There is no
30    fixed limit to the number of addresses and ports which can be listened on in
31 @@ -1429,8 +1443,10 @@ bind [<address>]:<port_range> [, ...] de
32          listen http_proxy
33              bind :80,:443
34              bind 10.0.0.1:10080,10.0.0.1:10443
35 +            bind 127.0.0.1:8443 accept-proxy
36  
37 -  See also : "source".
38 +  See also : "source", "option forwardfor" and the PROXY protocol
39 +             documentation.
40  
41  
42  bind-process [ all | odd | even | <number 1-32> ] ...
43 @@ -7231,7 +7247,9 @@ marked with a star ('*') after the field
44  
45  Detailed fields description :
46    - "client_ip" is the IP address of the client which initiated the TCP
47 -    connection to haproxy.
48 +    connection to haproxy. Note that when the connection is accepted on a
49 +    socket configured with "accept-proxy" and the PROXY protocol is correctly
50 +    used, then the logs will reflect the forwarded connection's information.
51  
52    - "client_port" is the TCP port of the client which initiated the connection.
53  
54 @@ -7404,7 +7422,9 @@ with a star ('*') after the field name b
55  
56  Detailed fields description :
57    - "client_ip" is the IP address of the client which initiated the TCP
58 -    connection to haproxy.
59 +    connection to haproxy. Note that when the connection is accepted on a
60 +    socket configured with "accept-proxy" and the PROXY protocol is correctly
61 +    used, then the logs will reflect the forwarded connection's information.
62  
63    - "client_port" is the TCP port of the client which initiated the connection.
64  
65 --- a/include/common/standard.h
66 +++ b/include/common/standard.h
67 @@ -269,6 +269,28 @@ static inline unsigned int __strl2uic(co
68         return i;
69  }
70  
71 +/* This function reads an unsigned integer from the string pointed to by <s>
72 + * and returns it. The <s> pointer is adjusted to point to the first unread
73 + * char. The function automatically stops at <end>.
74 + */
75 +static inline unsigned int __read_uint(const char **s, const char *end)
76 +{
77 +       const char *ptr = *s;
78 +       unsigned int i = 0;
79 +       unsigned int j, k;
80 +
81 +       while (ptr < end) {
82 +               j = *ptr - '0';
83 +               k = i * 10;
84 +               if (j > 9)
85 +                       break;
86 +               i = k + j;
87 +               ptr++;
88 +       }
89 +       *s = ptr;
90 +       return i;
91 +}
92 +
93  extern unsigned int str2ui(const char *s);
94  extern unsigned int str2uic(const char *s);
95  extern unsigned int strl2ui(const char *s, int len);
96 @@ -276,6 +298,7 @@ extern unsigned int strl2uic(const char 
97  extern int strl2ic(const char *s, int len);
98  extern int strl2irc(const char *s, int len, int *ret);
99  extern int strl2llrc(const char *s, int len, long long *ret);
100 +extern unsigned int read_uint(const char **s, const char *end);
101  unsigned int inetaddr_host(const char *text);
102  unsigned int inetaddr_host_lim(const char *text, const char *stop);
103  unsigned int inetaddr_host_lim_ret(const char *text, char *stop, const char **ret);
104 --- a/include/proto/client.h
105 +++ b/include/proto/client.h
106 @@ -25,6 +25,7 @@
107  #include <common/config.h>
108  #include <types/session.h>
109  
110 +int frontend_decode_proxy_request(struct session *s, struct buffer *req, int an_bit);
111  void get_frt_addr(struct session *s);
112  int event_accept(int fd);
113  
114 --- a/include/types/buffers.h
115 +++ b/include/types/buffers.h
116 @@ -135,16 +135,16 @@
117   * The field is blanked by buffer_init() and only by analysers themselves
118   * afterwards.
119   */
120 -#define AN_REQ_INSPECT          0x00000001  /* inspect request contents */
121 -#define AN_REQ_WAIT_HTTP        0x00000002  /* wait for an HTTP request */
122 -#define AN_REQ_HTTP_PROCESS_FE  0x00000004  /* process the frontend's HTTP part */
123 -#define AN_REQ_SWITCHING_RULES  0x00000008  /* apply the switching rules */
124 -#define AN_REQ_HTTP_PROCESS_BE  0x00000010  /* process the backend's HTTP part */
125 -#define AN_REQ_HTTP_INNER       0x00000020  /* inner processing of HTTP request */
126 -#define AN_REQ_HTTP_TARPIT      0x00000040  /* wait for end of HTTP tarpit */
127 -#define AN_REQ_HTTP_BODY        0x00000080  /* inspect HTTP request body */
128 -#define AN_REQ_STICKING_RULES   0x00000100  /* table persistence matching */
129 -/* unused: 0x200 */
130 +#define AN_REQ_DECODE_PROXY     0x00000001  /* take the proxied address from a 'PROXY' line */
131 +#define AN_REQ_INSPECT          0x00000002  /* inspect request contents */
132 +#define AN_REQ_WAIT_HTTP        0x00000004  /* wait for an HTTP request */
133 +#define AN_REQ_HTTP_PROCESS_FE  0x00000008  /* process the frontend's HTTP part */
134 +#define AN_REQ_SWITCHING_RULES  0x00000010  /* apply the switching rules */
135 +#define AN_REQ_HTTP_PROCESS_BE  0x00000020  /* process the backend's HTTP part */
136 +#define AN_REQ_HTTP_INNER       0x00000040  /* inner processing of HTTP request */
137 +#define AN_REQ_HTTP_TARPIT      0x00000080  /* wait for end of HTTP tarpit */
138 +#define AN_REQ_HTTP_BODY        0x00000100  /* inspect HTTP request body */
139 +#define AN_REQ_STICKING_RULES   0x00000200  /* table persistence matching */
140  #define AN_REQ_PRST_RDP_COOKIE  0x00000400  /* persistence on rdp cookie */
141  #define AN_REQ_HTTP_XFER_BODY   0x00000800  /* forward request body */
142  
143 --- a/include/types/protocols.h
144 +++ b/include/types/protocols.h
145 @@ -72,6 +72,7 @@
146  #define LI_O_FOREIGN   0x0002  /* permit listening on foreing addresses */
147  #define LI_O_NOQUICKACK        0x0004  /* disable quick ack of immediate data (linux) */
148  #define LI_O_DEF_ACCEPT        0x0008  /* wait up to 1 second for data before accepting */
149 +#define LI_O_ACC_PROXY  0x0010  /* find the proxied address in the first request line */
150  
151  /* The listener will be directly referenced by the fdtab[] which holds its
152   * socket. The listener provides the protocol-specific accept() function to
153 --- a/src/cfgparse.c
154 +++ b/src/cfgparse.c
155 @@ -1467,6 +1467,16 @@ int cfg_parse_listen(const char *file, i
156  #endif
157                         }
158  
159 +                       if (!strcmp(args[cur_arg], "accept-proxy")) { /* expect a 'PROXY' line first */
160 +                               struct listener *l;
161 +
162 +                               for (l = curproxy->listen; l != last_listen; l = l->next)
163 +                                       l->options |= LI_O_ACC_PROXY;
164 +
165 +                               cur_arg ++;
166 +                               continue;
167 +                       }
168 +
169                         if (!strcmp(args[cur_arg], "name")) {
170                                 struct listener *l;
171  
172 @@ -1519,7 +1529,7 @@ int cfg_parse_listen(const char *file, i
173                                 continue;
174                         }
175  
176 -                       Alert("parsing [%s:%d] : '%s' only supports the 'transparent', 'defer-accept', 'name', 'id', 'mss' and 'interface' options.\n",
177 +                       Alert("parsing [%s:%d] : '%s' only supports the 'transparent', 'accept-proxy', 'defer-accept', 'name', 'id', 'mss' and 'interface' options.\n",
178                               file, linenum, args[0]);
179                         err_code |= ERR_ALERT | ERR_FATAL;
180                         goto out;
181 @@ -5732,6 +5742,9 @@ out_uri_auth_compat:
182                         listener->handler = process_session;
183                         listener->analysers |= curproxy->fe_req_ana;
184  
185 +                       if (listener->options & LI_O_ACC_PROXY)
186 +                               listener->analysers |= AN_REQ_DECODE_PROXY;
187 +
188                         /* smart accept mode is automatic in HTTP mode */
189                         if ((curproxy->options2 & PR_O2_SMARTACC) ||
190                             (curproxy->mode == PR_MODE_HTTP &&
191 --- a/src/client.c
192 +++ b/src/client.c
193 @@ -22,6 +22,7 @@
194  
195  #include <common/compat.h>
196  #include <common/config.h>
197 +#include <common/debug.h>
198  #include <common/time.h>
199  
200  #include <types/global.h>
201 @@ -43,6 +44,191 @@
202  #include <proto/task.h>
203  
204  
205 +/* This analyser tries to fetch a line from the request buffer which looks like :
206 + *
207 + *   "PROXY" <SP> PROTO <SP> SRC3 <SP> DST3 <SP> SRC4 <SP> <DST4> "\r\n"
208 + *
209 + * There must be exactly one space between each field. Fields are :
210 + *  - PROTO : layer 4 protocol, which must be "TCP4" or "TCP6".
211 + *  - SRC3  : layer 3 (eg: IP) source address in standard text form
212 + *  - DST3  : layer 3 (eg: IP) destination address in standard text form
213 + *  - SRC4  : layer 4 (eg: TCP port) source address in standard text form
214 + *  - DST4  : layer 4 (eg: TCP port) destination address in standard text form
215 + *
216 + * This line MUST be at the beginning of the buffer and MUST NOT wrap.
217 + *
218 + * Once the data is fetched, the values are set in the session's field and data
219 + * are removed from the buffer. The function returns zero if it needs to wait
220 + * for more data (max: timeout_client), or 1 if it has finished and removed itself.
221 + */
222 +int frontend_decode_proxy_request(struct session *s, struct buffer *req, int an_bit)
223 +{
224 +       char *line = req->data;
225 +       char *end = req->data + req->l;
226 +       int len;
227 +
228 +       DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
229 +               now_ms, __FUNCTION__,
230 +               s,
231 +               req,
232 +               req->rex, req->wex,
233 +               req->flags,
234 +               req->l,
235 +               req->analysers);
236 +
237 +       if (req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT))
238 +               goto fail;
239 +
240 +       len = MIN(req->l, 6);
241 +       if (!len)
242 +               goto missing;
243 +
244 +       /* Decode a possible proxy request, fail early if it does not match */
245 +       if (strncmp(line, "PROXY ", len) != 0)
246 +               goto fail;
247 +
248 +       line += 6;
249 +       if (req->l < 18) /* shortest possible line */
250 +               goto missing;
251 +
252 +       if (!memcmp(line, "TCP4 ", 5) != 0) {
253 +               u32 src3, dst3, sport, dport;
254 +
255 +               line += 5;
256 +
257 +               src3 = inetaddr_host_lim_ret(line, end, &line);
258 +               if (line == end)
259 +                       goto missing;
260 +               if (*line++ != ' ')
261 +                       goto fail;
262 +
263 +               dst3 = inetaddr_host_lim_ret(line, end, &line);
264 +               if (line == end)
265 +                       goto missing;
266 +               if (*line++ != ' ')
267 +                       goto fail;
268 +
269 +               sport = read_uint((const char **)&line, end);
270 +               if (line == end)
271 +                       goto missing;
272 +               if (*line++ != ' ')
273 +                       goto fail;
274 +
275 +               dport = read_uint((const char **)&line, end);
276 +               if (line > end - 2)
277 +                       goto missing;
278 +               if (*line++ != '\r')
279 +                       goto fail;
280 +               if (*line++ != '\n')
281 +                       goto fail;
282 +
283 +               /* update the session's addresses and mark them set */
284 +               ((struct sockaddr_in *)&s->cli_addr)->sin_family      = AF_INET;
285 +               ((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr = htonl(src3);
286 +               ((struct sockaddr_in *)&s->cli_addr)->sin_port        = htons(sport);
287 +
288 +               ((struct sockaddr_in *)&s->frt_addr)->sin_family      = AF_INET;
289 +               ((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr = htonl(dst3);
290 +               ((struct sockaddr_in *)&s->frt_addr)->sin_port        = htons(dport);
291 +               s->flags |= SN_FRT_ADDR_SET;
292 +
293 +       }
294 +       else if (!memcmp(line, "TCP6 ", 5) != 0) {
295 +               u32 sport, dport;
296 +               char *src_s;
297 +               char *dst_s, *sport_s, *dport_s;
298 +               struct in6_addr src3, dst3;
299 +
300 +               line+=5;
301 +
302 +               src_s = line;
303 +               dst_s = sport_s = dport_s = NULL;
304 +               while (1) {
305 +                       if (line > end - 2) {
306 +                               goto missing;
307 +                       }
308 +                       else if (*line == '\r') {
309 +                               *line = 0;
310 +                               line++;
311 +                               if (*line++ != '\n')
312 +                                       goto fail;
313 +                               break;
314 +                       }
315 +
316 +                       if (*line == ' ') {
317 +                               *line = 0;
318 +                               if (!dst_s)
319 +                                       dst_s = line+1;
320 +                               else if (!sport_s)
321 +                                       sport_s = line+1;
322 +                               else if (!dport_s)
323 +                                       dport_s = line+1;
324 +                       }
325 +                       line++;
326 +               }
327 +
328 +               if (!dst_s || !sport_s || !dport_s)
329 +                       goto fail;
330 +
331 +               sport = read_uint((const char **)&sport_s,dport_s-1);
332 +               if ( *sport_s != 0 )
333 +                       goto fail;
334 +
335 +               dport = read_uint((const char **)&dport_s,line-2);
336 +               if ( *dport_s != 0 )
337 +                       goto fail;
338 +
339 +               if (inet_pton(AF_INET6, src_s, (void *)&src3) != 1)
340 +                       goto fail;
341 +
342 +               if (inet_pton(AF_INET6, dst_s, (void *)&dst3) != 1)
343 +                       goto fail;
344 +
345 +               /* update the session's addresses and mark them set */
346 +               ((struct sockaddr_in6 *)&s->cli_addr)->sin6_family      = AF_INET6;
347 +               memcpy(&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, &src3, sizeof(struct in6_addr));
348 +               ((struct sockaddr_in6 *)&s->cli_addr)->sin6_port        = htons(sport);
349 +
350 +               ((struct sockaddr_in6 *)&s->frt_addr)->sin6_family      = AF_INET6;
351 +               memcpy(&((struct sockaddr_in6 *)&s->frt_addr)->sin6_addr, &dst3, sizeof(struct in6_addr));
352 +               ((struct sockaddr_in6 *)&s->frt_addr)->sin6_port        = htons(dport);
353 +               s->flags |= SN_FRT_ADDR_SET;
354 +       }
355 +       else {
356 +               goto fail;
357 +       }
358 +
359 +       /* remove the PROXY line from the request */
360 +       len = line - req->data;
361 +       buffer_replace2(req, req->data, line, NULL, 0);
362 +       req->total -= len; /* don't count the header line */
363 +
364 +       req->analysers &= ~an_bit;
365 +       return 1;
366 +
367 + missing:
368 +       if (!(req->flags & (BF_SHUTR|BF_FULL))) {
369 +               buffer_dont_connect(s->req);
370 +               return 0;
371 +       }
372 +       /* missing data and buffer is either full or shutdown => fail */
373 +
374 + fail:
375 +       buffer_abort(req);
376 +       buffer_abort(s->rep);
377 +       req->analysers = 0;
378 +
379 +       s->fe->counters.failed_req++;
380 +       if (s->listener->counters)
381 +               s->listener->counters->failed_req++;
382 +
383 +       if (!(s->flags & SN_ERR_MASK))
384 +               s->flags |= SN_ERR_PRXCOND;
385 +       if (!(s->flags & SN_FINST_MASK))
386 +               s->flags |= SN_FINST_R;
387 +       return 0;
388 +}
389 +
390  /* Retrieves the original destination address used by the client, and sets the
391   * SN_FRT_ADDR_SET flag.
392   */
393 --- a/src/proto_http.c
394 +++ b/src/proto_http.c
395 @@ -4127,7 +4127,8 @@ void http_end_txn_clean_session(struct s
396         if (s->rep->lr >= s->rep->data + s->rep->size)
397                 s->rep->lr -= s->req->size;
398  
399 -       s->req->analysers |= s->fe->fe_req_ana;
400 +       s->req->analysers = s->fe->fe_req_ana;
401 +       s->req->analysers &= ~AN_REQ_DECODE_PROXY;
402         s->rep->analysers = 0;
403  
404         http_silent_debug(__LINE__, s);
405 @@ -7670,7 +7671,6 @@ void http_reset_txn(struct session *s)
406         http_init_txn(s);
407  
408         s->be = s->fe;
409 -       s->req->analysers = s->listener->analysers;
410         s->logs.logwait = s->fe->to_log;
411         s->srv = s->prev_srv = s->srv_conn = NULL;
412         /* re-init store persistence */
413 --- a/src/session.c
414 +++ b/src/session.c
415 @@ -1055,6 +1055,12 @@ resync_stream_interface:
416                         while (ana_list && max_loops--) {
417                                 /* Warning! ensure that analysers are always placed in ascending order! */
418  
419 +                               if (ana_list & AN_REQ_DECODE_PROXY) {
420 +                                       if (!frontend_decode_proxy_request(s, s->req, AN_REQ_DECODE_PROXY))
421 +                                               break;
422 +                                       UPDATE_ANALYSERS(s->req->analysers, ana_list, ana_back, AN_REQ_DECODE_PROXY);
423 +                               }
424 +
425                                 if (ana_list & AN_REQ_INSPECT) {
426                                         if (!tcp_inspect_request(s, s->req, AN_REQ_INSPECT))
427                                                 break;
428 --- a/src/standard.c
429 +++ b/src/standard.c
430 @@ -569,6 +569,11 @@ unsigned int strl2uic(const char *s, int
431         return __strl2uic(s, len);
432  }
433  
434 +unsigned int read_uint(const char **s, const char *end)
435 +{
436 +       return __read_uint(s, end);
437 +}
438 +
439  /* This one is 7 times faster than strtol() on athlon with checks.
440   * It returns the value of the number composed of all valid digits read,
441   * and can process negative numbers too.