You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

506 lines
18 KiB

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