blob: 06b99dcddc806bf51a5ed6849130aac1cc05d04d [file] [log] [blame]
Andriy Palamarchukceae85b2017-01-24 13:48:27 -050012017/01/30 Willy Tarreau
Willy Tarreaua3393952014-05-10 15:16:43 +02002 HAProxy Technologies
Willy Tarreau7f898512011-03-20 11:32:40 +01003 The PROXY protocol
Willy Tarreau332d7b02012-11-19 11:27:29 +01004 Versions 1 & 2
Willy Tarreau7f898512011-03-20 11:32:40 +01005
6Abstract
7
8 The PROXY protocol provides a convenient way to safely transport connection
9 information such as a client's address across multiple layers of NAT or TCP
10 proxies. It is designed to require little changes to existing components and
11 to limit the performance impact caused by the processing of the transported
12 information.
13
14
15Revision history
16
17 2010/10/29 - first version
18 2011/03/20 - update: implementation and security considerations
Willy Tarreau332d7b02012-11-19 11:27:29 +010019 2012/06/21 - add support for binary format
20 2012/11/19 - final review and fixes
David Safb76832014-05-08 23:42:08 -040021 2014/05/18 - modify and extend PROXY protocol version 2
Willy Tarreau7a6f1342014-06-14 11:45:09 +020022 2014/06/11 - fix example code to consider ver+cmd merge
23 2014/06/14 - fix v2 header check in example code, and update Forwarded spec
Willy Tarreau7b7011c2015-05-02 15:13:07 +020024 2014/07/12 - update list of implementations (add Squid)
25 2015/05/02 - update list of implementations and format of the TLV add-ons
Andriy Palamarchukceae85b2017-01-24 13:48:27 -050026 2017/01/30 - added the checksum TLV and reserved TLV type ranges, clarified
27 wording
Willy Tarreau7f898512011-03-20 11:32:40 +010028
29
301. Background
Willy Tarreau640cf222010-10-29 21:46:16 +020031
32Relaying TCP connections through proxies generally involves a loss of the
33original TCP connection parameters such as source and destination addresses,
34ports, and so on. Some protocols make it a little bit easier to transfer such
Willy Tarreau332d7b02012-11-19 11:27:29 +010035information. For SMTP, Postfix authors have proposed the XCLIENT protocol [1]
Willy Tarreau7a6f1342014-06-14 11:45:09 +020036which received broad adoption and is particularly suited to mail exchanges.
37For HTTP, there is the "Forwarded" extension [2], which aims at replacing the
38omnipresent "X-Forwarded-For" header which carries information about the
39original source address, and the less common X-Original-To which carries
40information about the destination address.
Willy Tarreau640cf222010-10-29 21:46:16 +020041
42However, both mechanisms require a knowledge of the underlying protocol to be
43implemented in intermediaries.
44
45Then comes a new class of products which we'll call "dumb proxies", not because
46they don't do anything, but because they're processing protocol-agnostic data.
Willy Tarreau332d7b02012-11-19 11:27:29 +010047Both Stunnel[3] and Stud[4] are examples of such "dumb proxies". They talk raw
48TCP on one side, and raw SSL on the other one, and do that reliably, without
Willy Tarreau7a6f1342014-06-14 11:45:09 +020049any knowledge of what protocol is transported on top of the connection. Haproxy
50running in pure TCP mode obviously falls into that category as well.
Willy Tarreau640cf222010-10-29 21:46:16 +020051
52The problem with such a proxy when it is combined with another one such as
Willy Tarreau7a6f1342014-06-14 11:45:09 +020053haproxy, is to adapt it to talk the higher level protocol. A patch is available
Willy Tarreau332d7b02012-11-19 11:27:29 +010054for Stunnel to make it capable of inserting an X-Forwarded-For header in the
55first HTTP request of each incoming connection. Haproxy is able not to add
56another one when the connection comes from Stunnel, so that it's possible to
57hide it from the servers.
Willy Tarreau640cf222010-10-29 21:46:16 +020058
59The typical architecture becomes the following one :
60
61
62 +--------+ HTTP :80 +----------+
63 | client | --------------------------------> | |
64 | | | haproxy, |
65 +--------+ +---------+ | 1 or 2 |
66 / / HTTPS | stunnel | HTTP :81 | listening|
67 <________/ ---------> | (server | ---------> | ports |
68 | mode) | | |
69 +---------+ +----------+
70
71
72The problem appears when haproxy runs with keep-alive on the side towards the
73client. The Stunnel patch will only add the X-Forwarded-For header to the first
74request of each connection and all subsequent requests will not have it. One
75solution could be to improve the patch to make it support keep-alive and parse
76all forwarded data, whether they're announced with a Content-Length or with a
77Transfer-Encoding, taking care of special methods such as HEAD which announce
Andriy Palamarchukf1eae4e2017-01-24 13:34:08 -050078data without transferring them, etc... In fact, it would require implementing a
Willy Tarreau640cf222010-10-29 21:46:16 +020079full HTTP stack in Stunnel. It would then become a lot more complex, a lot less
80reliable and would not anymore be the "dumb proxy" that fits every purposes.
81
82In practice, we don't need to add a header for each request because we'll emit
83the exact same information every time : the information related to the client
84side connection. We could then cache that information in haproxy and use it for
85every other request. But that becomes dangerous and is still limited to HTTP
86only.
87
Willy Tarreau332d7b02012-11-19 11:27:29 +010088Another approach consists in prepending each connection with a header reporting
89the characteristics of the other side's connection. This method is simpler to
Willy Tarreau640cf222010-10-29 21:46:16 +020090implement, does not require any protocol-specific knowledge on either side, and
Willy Tarreau332d7b02012-11-19 11:27:29 +010091completely fits the purpose since what is desired precisely is to know the
92other side's connection endpoints. It is easy to perform for the sender (just
93send a short header once the connection is established) and to parse for the
94receiver (simply perform one read() on the incoming connection to fill in
95addresses after an accept). The protocol used to carry connection information
96across proxies was thus called the PROXY protocol.
Willy Tarreau640cf222010-10-29 21:46:16 +020097
Willy Tarreau7f898512011-03-20 11:32:40 +010098
Willy Tarreau332d7b02012-11-19 11:27:29 +0100992. The PROXY protocol header
Willy Tarreau7f898512011-03-20 11:32:40 +0100100
Willy Tarreau332d7b02012-11-19 11:27:29 +0100101This document uses a few terms that are worth explaining here :
102 - "connection initiator" is the party requesting a new connection
103 - "connection target" is the party accepting a connection request
104 - "client" is the party for which a connection was requested
105 - "server" is the party to which the client desired to connect
106 - "proxy" is the party intercepting and relaying the connection
107 from the client to the server.
108 - "sender" is the party sending data over a connection.
109 - "receiver" is the party receiving data from the sender.
110 - "header" or "PROXY protocol header" is the block of connection information
111 the connection initiator prepends at the beginning of a connection, which
112 makes it the sender from the protocol point of view.
113
114The PROXY protocol's goal is to fill the server's internal structures with the
115information collected by the proxy that the server would have been able to get
116by itself if the client was connecting directly to the server instead of via a
117proxy. The information carried by the protocol are the ones the server would
118get using getsockname() and getpeername() :
119 - address family (AF_INET for IPv4, AF_INET6 for IPv6, AF_UNIX)
120 - socket protocol (SOCK_STREAM for TCP, SOCK_DGRAM for UDP)
Willy Tarreau640cf222010-10-29 21:46:16 +0200121 - layer 3 source and destination addresses
122 - layer 4 source and destination ports if any
123
124Unlike the XCLIENT protocol, the PROXY protocol was designed with limited
Willy Tarreau332d7b02012-11-19 11:27:29 +0100125extensibility in order to help the receiver parse it very fast. Version 1 was
126focused on keeping it human-readable for better debugging possibilities, which
127is always desirable for early adoption when few implementations exist. Version
1282 adds support for a binary encoding of the header which is much more efficient
129to produce and to parse, especially when dealing with IPv6 addresses that are
130expensive to emit in ASCII form and to parse.
131
132In both cases, the protocol simply consists in an easily parsable header placed
133by the connection initiator at the beginning of each connection. The protocol
134is intentionally stateless in that it does not expect the sender to wait for
135the receiver before sending the header, nor the receiver to send anything back.
136
137This specification supports two header formats, a human-readable format which
138is the only format supported in version 1 of the protocol, and a binary format
139which is only supported in version 2. Both formats were designed to ensure that
140the header cannot be confused with common higher level protocols such as HTTP,
141SSL/TLS, FTP or SMTP, and that both formats are easily distinguishable one from
142each other for the receiver.
143
144Version 1 senders MAY only produce the human-readable header format. Version 2
145senders MAY only produce the binary header format. Version 1 receivers MUST at
146least implement the human-readable header format. Version 2 receivers MUST at
147least implement the binary header format, and it is recommended that they also
148implement the human-readable header format for better interoperability and ease
149of upgrade when facing version 1 senders.
150
151Both formats are designed to fit in the smallest TCP segment that any TCP/IP
152host is required to support (576 - 40 = 536 bytes). This ensures that the whole
153header will always be delivered at once when the socket buffers are still empty
154at the beginning of a connection. The sender must always ensure that the header
155is sent at once, so that the transport layer maintains atomicity along the path
156to the receiver. The receiver may be tolerant to partial headers or may simply
157drop the connection when receiving a partial header. Recommendation is to be
158tolerant, but implementation constraints may not always easily permit this. It
159is important to note that nothing forces any intermediary to forward the whole
160header at once, because TCP is a streaming protocol which may be processed one
161byte at a time if desired, causing the header to be fragmented when reaching
162the receiver. But due to the places where such a protocol is used, the above
163simplification generally is acceptable because the risk of crossing such a
164device handling one byte at a time is close to zero.
165
166The receiver MUST NOT start processing the connection before it receives a
167complete and valid PROXY protocol header. This is particularly important for
168protocols where the receiver is expected to speak first (eg: SMTP, FTP or SSH).
169The receiver may apply a short timeout and decide to abort the connection if
170the protocol header is not seen within a few seconds (at least 3 seconds to
171cover a TCP retransmit).
172
173The receiver MUST be configured to only receive the protocol described in this
174specification and MUST not try to guess whether the protocol header is present
175or not. This means that the protocol explicitly prevents port sharing between
176public and private access. Otherwise it would open a major security breach by
177allowing untrusted parties to spoof their connection addresses. The receiver
178SHOULD ensure proper access filtering so that only trusted proxies are allowed
179to use this protocol.
180
181Some proxies are smart enough to understand transported protocols and to reuse
182idle server connections for multiple messages. This typically happens in HTTP
183where requests from multiple clients may be sent over the same connection. Such
184proxies MUST NOT implement this protocol on multiplexed connections because the
185receiver would use the address advertised in the PROXY header as the address of
186all forwarded requests's senders. In fact, such proxies are not dumb proxies,
187and since they do have a complete understanding of the transported protocol,
188they MUST use the facilities provided by this protocol to present the client's
189address.
190
191
1922.1. Human-readable header format (Version 1)
193
194This is the format specified in version 1 of the protocol. It consists in one
195line of ASCII text matching exactly the following block, sent immediately and
196at once upon the connection establishment and prepended before any data flowing
197from the sender to the receiver :
Willy Tarreau640cf222010-10-29 21:46:16 +0200198
199 - a string identifying the protocol : "PROXY" ( \x50 \x52 \x4F \x58 \x59 )
Willy Tarreau332d7b02012-11-19 11:27:29 +0100200 Seeing this string indicates that this is version 1 of the protocol.
Willy Tarreau640cf222010-10-29 21:46:16 +0200201
202 - exactly one space : " " ( \x20 )
203
Willy Tarreau332d7b02012-11-19 11:27:29 +0100204 - a string indicating the proxied INET protocol and family. As of version 1,
Willy Tarreau640cf222010-10-29 21:46:16 +0200205 only "TCP4" ( \x54 \x43 \x50 \x34 ) for TCP over IPv4, and "TCP6"
Willy Tarreau332d7b02012-11-19 11:27:29 +0100206 ( \x54 \x43 \x50 \x36 ) for TCP over IPv6 are allowed. Other, unsupported,
207 or unknown protocols must be reported with the name "UNKNOWN" ( \x55 \x4E
208 \x4B \x4E \x4F \x57 \x4E ). For "UNKNOWN", the rest of the line before the
209 CRLF may be omitted by the sender, and the receiver must ignore anything
210 presented before the CRLF is found. Note that an earlier version of this
211 specification suggested to use this when sending health checks, but this
212 causes issues with servers that reject the "UNKNOWN" keyword. Thus is it
213 now recommended not to send "UNKNOWN" when the connection is expected to
214 be accepted, but only when it is not possible to correctly fill the PROXY
215 line.
Willy Tarreau640cf222010-10-29 21:46:16 +0200216
217 - exactly one space : " " ( \x20 )
218
219 - the layer 3 source address in its canonical format. IPv4 addresses must be
220 indicated as a series of exactly 4 integers in the range [0..255] inclusive
221 written in decimal representation separated by exactly one dot between each
222 other. Heading zeroes are not permitted in front of numbers in order to
223 avoid any possible confusion with octal numbers. IPv6 addresses must be
224 indicated as series of 4 hexadecimal digits (upper or lower case) delimited
225 by colons between each other, with the acceptance of one double colon
226 sequence to replace the largest acceptable range of consecutive zeroes. The
227 total number of decoded bits must exactly be 128. The advertised protocol
228 family dictates what format to use.
229
230 - exactly one space : " " ( \x20 )
231
232 - the layer 3 destination address in its canonical format. It is the same
233 format as the layer 3 source address and matches the same family.
234
235 - exactly one space : " " ( \x20 )
236
237 - the TCP source port represented as a decimal integer in the range
238 [0..65535] inclusive. Heading zeroes are not permitted in front of numbers
239 in order to avoid any possible confusion with octal numbers.
240
241 - exactly one space : " " ( \x20 )
242
243 - the TCP destination port represented as a decimal integer in the range
244 [0..65535] inclusive. Heading zeroes are not permitted in front of numbers
245 in order to avoid any possible confusion with octal numbers.
246
247 - the CRLF sequence ( \x0D \x0A )
248
Willy Tarreau332d7b02012-11-19 11:27:29 +0100249
250The maximum line lengths the receiver must support including the CRLF are :
251 - TCP/IPv4 :
252 "PROXY TCP4 255.255.255.255 255.255.255.255 65535 65535\r\n"
253 => 5 + 1 + 4 + 1 + 15 + 1 + 15 + 1 + 5 + 1 + 5 + 2 = 56 chars
254
255 - TCP/IPv6 :
256 "PROXY TCP6 ffff:f...f:ffff ffff:f...f:ffff 65535 65535\r\n"
257 => 5 + 1 + 4 + 1 + 39 + 1 + 39 + 1 + 5 + 1 + 5 + 2 = 104 chars
258
259 - unknown connection (short form) :
260 "PROXY UNKNOWN\r\n"
261 => 5 + 1 + 7 + 2 = 15 chars
262
263 - worst case (optional fields set to 0xff) :
264 "PROXY UNKNOWN ffff:f...f:ffff ffff:f...f:ffff 65535 65535\r\n"
265 => 5 + 1 + 7 + 1 + 39 + 1 + 39 + 1 + 5 + 1 + 5 + 2 = 107 chars
266
267So a 108-byte buffer is always enough to store all the line and a trailing zero
268for string processing.
269
270The receiver must wait for the CRLF sequence before starting to decode the
271addresses in order to ensure they are complete and properly parsed. If the CRLF
272sequence is not found in the first 107 characters, the receiver should declare
273the line invalid. A receiver may reject an incomplete line which does not
274contain the CRLF sequence in the first atomic read operation. The receiver must
275not tolerate a single CR or LF character to end the line when a complete CRLF
276sequence is expected.
277
278Any sequence which does not exactly match the protocol must be discarded and
279cause the receiver to abort the connection. It is recommended to abort the
280connection as soon as possible so that the sender gets a chance to notice the
281anomaly and log it.
Willy Tarreau640cf222010-10-29 21:46:16 +0200282
283If the announced transport protocol is "UNKNOWN", then the receiver knows that
Willy Tarreau332d7b02012-11-19 11:27:29 +0100284the sender speaks the correct PROXY protocol with the appropriate version, and
285SHOULD accept the connection and use the real connection's parameters as if
286there were no PROXY protocol header on the wire. However, senders SHOULD not
287use the "UNKNOWN" protocol when they are the initiators of outgoing connections
288because some receivers may reject them. When a load balancing proxy has to send
289health checks to a server, it SHOULD build a valid PROXY line which it will
290fill with a getsockname()/getpeername() pair indicating the addresses used. It
291is important to understand that doing so is not appropriate when some source
292address translation is performed between the sender and the receiver.
Willy Tarreau640cf222010-10-29 21:46:16 +0200293
294An example of such a line before an HTTP request would look like this (CR
295marked as "\r" and LF marked as "\n") :
296
297 PROXY TCP4 192.168.0.1 192.168.0.11 56324 443\r\n
298 GET / HTTP/1.1\r\n
299 Host: 192.168.0.11\r\n
300 \r\n
301
Willy Tarreau332d7b02012-11-19 11:27:29 +0100302For the sender, the header line is easy to put into the output buffers once the
303connection is established. Note that since the line is always shorter than an
304MSS, the sender is guaranteed to always be able to emit it at once and should
305not even bother handling partial sends. For the receiver, once the header is
306parsed, it is easy to skip it from the input buffers. Please consult section 9
307for implementation suggestions.
308
309
3102.2. Binary header format (version 2)
311
312Producing human-readable IPv6 addresses and parsing them is very inefficient,
313due to the multiple possible representation formats and the handling of compact
314address format. It was also not possible to specify address families outside
315IPv4/IPv6 nor non-TCP protocols. Another drawback of the human-readable format
316is the fact that implementations need to parse all characters to find the
317trailing CRLF, which makes it harder to read only the exact bytes count. Last,
318the UNKNOWN address type has not always been accepted by servers as a valid
319protocol because of its imprecise meaning.
320
321Version 2 of the protocol thus introduces a new binary format which remains
322distinguishable from version 1 and from other commonly used protocols. It was
323specially designed in order to be incompatible with a wide range of protocols
324and to be rejected by a number of common implementations of these protocols
325when unexpectedly presented (please see section 7). Also for better processing
326efficiency, IPv4 and IPv6 addresses are respectively aligned on 4 and 16 bytes
327boundaries.
328
329The binary header format starts with a constant 12 bytes block containing the
330protocol signature :
331
332 \x0D \x0A \x0D \x0A \x00 \x0D \x0A \x51 \x55 \x49 \x54 \x0A
333
334Note that this block contains a null byte at the 5th position, so it must not
335be handled as a null-terminated string.
336
David Safb76832014-05-08 23:42:08 -0400337The next byte (the 13th one) is the protocol version and command.
Willy Tarreau332d7b02012-11-19 11:27:29 +0100338
David Safb76832014-05-08 23:42:08 -0400339The highest four bits contains the version. As of this specification, it must
340always be sent as \x2 and the receiver must only accept this value.
341
342The lowest four bits represents the command :
343 - \x0 : LOCAL : the connection was established on purpose by the proxy
Willy Tarreau332d7b02012-11-19 11:27:29 +0100344 without being relayed. The connection endpoints are the sender and the
345 receiver. Such connections exist when the proxy sends health-checks to the
346 server. The receiver must accept this connection as valid and must use the
347 real connection endpoints and discard the protocol block including the
348 family which is ignored.
349
David Safb76832014-05-08 23:42:08 -0400350 - \x1 : PROXY : the connection was established on behalf of another node,
Willy Tarreau332d7b02012-11-19 11:27:29 +0100351 and reflects the original connection endpoints. The receiver must then use
352 the information provided in the protocol block to get original the address.
353
354 - other values are unassigned and must not be emitted by senders. Receivers
355 must drop connections presenting unexpected values here.
356
David Safb76832014-05-08 23:42:08 -0400357The 14th byte contains the transport protocol and address family. The highest 4
Willy Tarreau332d7b02012-11-19 11:27:29 +0100358bits contain the address family, the lowest 4 bits contain the protocol.
359
360The address family maps to the original socket family without necessarily
361matching the values internally used by the system. It may be one of :
362
363 - 0x0 : AF_UNSPEC : the connection is forwarded for an unknown, unspecified
364 or unsupported protocol. The sender should use this family when sending
365 LOCAL commands or when dealing with unsupported protocol families. The
366 receiver is free to accept the connection anyway and use the real endpoint
367 addresses or to reject it. The receiver should ignore address information.
368
369 - 0x1 : AF_INET : the forwarded connection uses the AF_INET address family
370 (IPv4). The addresses are exactly 4 bytes each in network byte order,
371 followed by transport protocol information (typically ports).
372
373 - 0x2 : AF_INET6 : the forwarded connection uses the AF_INET6 address family
374 (IPv6). The addresses are exactly 16 bytes each in network byte order,
375 followed by transport protocol information (typically ports).
376
377 - 0x3 : AF_UNIX : the forwarded connection uses the AF_UNIX address family
378 (UNIX). The addresses are exactly 108 bytes each.
379
380 - other values are unspecified and must not be emitted in version 2 of this
381 protocol and must be rejected as invalid by receivers.
382
Andriy Palamarchukf1eae4e2017-01-24 13:34:08 -0500383The transport protocol is specified in the lowest 4 bits of the 14th byte :
Willy Tarreau332d7b02012-11-19 11:27:29 +0100384
385 - 0x0 : UNSPEC : the connection is forwarded for an unknown, unspecified
386 or unsupported protocol. The sender should use this family when sending
387 LOCAL commands or when dealing with unsupported protocol families. The
388 receiver is free to accept the connection anyway and use the real endpoint
389 addresses or to reject it. The receiver should ignore address information.
390
391 - 0x1 : STREAM : the forwarded connection uses a SOCK_STREAM protocol (eg:
392 TCP or UNIX_STREAM). When used with AF_INET/AF_INET6 (TCP), the addresses
393 are followed by the source and destination ports represented on 2 bytes
394 each in network byte order.
395
396 - 0x2 : DGRAM : the forwarded connection uses a SOCK_DGRAM protocol (eg:
397 UDP or UNIX_DGRAM). When used with AF_INET/AF_INET6 (UDP), the addresses
398 are followed by the source and destination ports represented on 2 bytes
399 each in network byte order.
400
401 - other values are unspecified and must not be emitted in version 2 of this
402 protocol and must be rejected as invalid by receivers.
403
404In practice, the following protocol bytes are expected :
405
406 - \x00 : UNSPEC : the connection is forwarded for an unknown, unspecified
407 or unsupported protocol. The sender should use this family when sending
408 LOCAL commands or when dealing with unsupported protocol families. When
409 used with a LOCAL command, the receiver must accept the connection and
410 ignore any address information. For other commands, the receiver is free
411 to accept the connection anyway and use the real endpoints addresses or to
412 reject the connection. The receiver should ignore address information.
413
414 - \x11 : TCP over IPv4 : the forwarded connection uses TCP over the AF_INET
415 protocol family. Address length is 2*4 + 2*2 = 12 bytes.
416
417 - \x12 : UDP over IPv4 : the forwarded connection uses UDP over the AF_INET
418 protocol family. Address length is 2*4 + 2*2 = 12 bytes.
419
420 - \x21 : TCP over IPv6 : the forwarded connection uses TCP over the AF_INET6
421 protocol family. Address length is 2*16 + 2*2 = 36 bytes.
422
423 - \x22 : UDP over IPv6 : the forwarded connection uses UDP over the AF_INET6
424 protocol family. Address length is 2*16 + 2*2 = 36 bytes.
425
426 - \x31 : UNIX stream : the forwarded connection uses SOCK_STREAM over the
427 AF_UNIX protocol family. Address length is 2*108 = 216 bytes.
428
429 - \x32 : UNIX datagram : the forwarded connection uses SOCK_DGRAM over the
430 AF_UNIX protocol family. Address length is 2*108 = 216 bytes.
431
432
Andriy Palamarchukceae85b2017-01-24 13:48:27 -0500433Only the UNSPEC protocol byte (\x00) is mandatory to implement on the receiver.
434A receiver is not required to implement other ones, provided that it
435automatically falls back to the UNSPEC mode for the valid combinations above
436that it does not support.
Willy Tarreau640cf222010-10-29 21:46:16 +0200437
Andriy Palamarchukf1eae4e2017-01-24 13:34:08 -0500438The 15th and 16th bytes is the address length in bytes in network endian order.
David Safb76832014-05-08 23:42:08 -0400439It is used so that the receiver knows how many address bytes to skip even when
440it does not implement the presented protocol. Thus the length of the protocol
441header in bytes is always exactly 16 + this value. When a sender presents a
Willy Tarreau332d7b02012-11-19 11:27:29 +0100442LOCAL connection, it should not present any address so it sets this field to
443zero. Receivers MUST always consider this field to skip the appropriate number
444of bytes and must not assume zero is presented for LOCAL connections. When a
445receiver accepts an incoming connection showing an UNSPEC address family or
446protocol, it may or may not decide to log the address information if present.
447
448So the 16-byte version 2 header can be described this way :
449
450 struct proxy_hdr_v2 {
451 uint8_t sig[12]; /* hex 0D 0A 0D 0A 00 0D 0A 51 55 49 54 0A */
Willy Tarreau0f6093a2014-06-11 21:21:26 +0200452 uint8_t ver_cmd; /* protocol version and command */
Willy Tarreau332d7b02012-11-19 11:27:29 +0100453 uint8_t fam; /* protocol family and address */
David Safb76832014-05-08 23:42:08 -0400454 uint16_t len; /* number of following bytes part of the header */
Willy Tarreau332d7b02012-11-19 11:27:29 +0100455 };
456
457Starting from the 17th byte, addresses are presented in network byte order.
458The address order is always the same :
459 - source layer 3 address in network byte order
460 - destination layer 3 address in network byte order
461 - source layer 4 address if any, in network byte order (port)
462 - destination layer 4 address if any, in network byte order (port)
463
464The address block may directly be sent from or received into the following
465union which makes it easy to cast from/to the relevant socket native structs
466depending on the address type :
467
468 union proxy_addr {
469 struct { /* for TCP/UDP over IPv4, len = 12 */
470 uint32_t src_addr;
471 uint32_t dst_addr;
472 uint16_t src_port;
473 uint16_t dst_port;
474 } ipv4_addr;
475 struct { /* for TCP/UDP over IPv6, len = 36 */
476 uint8_t src_addr[16];
477 uint8_t dst_addr[16];
478 uint16_t src_port;
479 uint16_t dst_port;
480 } ipv6_addr;
481 struct { /* for AF_UNIX sockets, len = 216 */
482 uint8_t src_addr[108];
483 uint8_t dst_addr[108];
484 } unix_addr;
485 };
486
487The sender must ensure that all the protocol header is sent at once. This block
488is always smaller than an MSS, so there is no reason for it to be segmented at
489the beginning of the connection. The receiver should also process the header
490at once. The receiver must not start to parse an address before the whole
491address block is received. The receiver must also reject incoming connections
492containing partial protocol headers.
493
494A receiver may be configured to support both version 1 and version 2 of the
495protocol. Identifying the protocol version is easy :
496
497 - if the incoming byte count is 16 or above and the 13 first bytes match
498 the protocol signature block followed by the protocol version 2 :
499
500 \x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A\x02
501
502 - otherwise, if the incoming byte count is 8 or above, and the 5 first
503 characters match the ASCII representation of "PROXY" then the protocol
504 must be parsed as version 1 :
505
506 \x50\x52\x4F\x58\x59
507
508 - otherwise the protocol is not covered by this specification and the
509 connection must be dropped.
510
David Safb76832014-05-08 23:42:08 -0400511If the length specified in the PROXY protocol header indicates that additional
512bytes are part of the header beyond the address information, a receiver may
513choose to skip over and ignore those bytes, or attempt to interpret those
514bytes.
515
516The information in those bytes will be arranged in Type-Length-Value (TLV
517vectors) in the following format. The first byte is the Type of the vector.
518The second two bytes represent the length in bytes of the value (not included
519the Type and Length bytes), and following the length field is the number of
520bytes specified by the length.
521
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200522 struct pp2_tlv {
David Safb76832014-05-08 23:42:08 -0400523 uint8_t type;
524 uint8_t length_hi;
525 uint8_t length_lo;
526 uint8_t value[0];
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200527 };
David Safb76832014-05-08 23:42:08 -0400528
Andriy Palamarchukceae85b2017-01-24 13:48:27 -0500529A receiver may choose to skip over and ignore the TLVs he is not interested in
530or he does not understand. Senders can generate the TLVs only for
531the information they choose to publish.
532
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200533The following types have already been registered for the <type> field :
534
Nikos Mavrogiannopoulosf1650a82015-08-24 15:53:18 +0200535 #define PP2_TYPE_ALPN 0x01
536 #define PP2_TYPE_AUTHORITY 0x02
Andriy Palamarchukceae85b2017-01-24 13:48:27 -0500537 #define PP2_TYPE_CHECKSUM 0x03
Nikos Mavrogiannopoulosf1650a82015-08-24 15:53:18 +0200538 #define PP2_TYPE_SSL 0x20
539 #define PP2_SUBTYPE_SSL_VERSION 0x21
540 #define PP2_SUBTYPE_SSL_CN 0x22
541 #define PP2_TYPE_NETNS 0x30
542
Andriy Palamarchukceae85b2017-01-24 13:48:27 -0500543
5442.2.1. The PP2_TYPE_CHECKSUM type
545
546The value of the type PP2_TYPE_CHECKSUM is a 32-bit number storing the CRC32c
547checksum of the PROXY protocol header.
548
549When the checksum is supported by the sender after constructing the header
550the sender MUST:
551
552 - initialize the checksum field to '0's.
553
554 - calculate the CRC32c checksum of the PROXY header as described in RFC4960,
555 Appendix B [8].
Nikos Mavrogiannopoulosf1650a82015-08-24 15:53:18 +0200556
Andriy Palamarchukceae85b2017-01-24 13:48:27 -0500557 - put the resultant value into the checksum field, and leave the rest of
558 the bits unchanged.
559
560If the checksum is provided as part of the PROXY header and the checksum
561functionality is supported by the receiver, the receiver MUST:
562
563 - store the received CRC32c checksum value aside.
564
565 - replace the 32 bits of the checksum field in the received PROXY header with
566 all '0's and calculate a CRC32c checksum value of the whole PROXY header.
567
568 - verify that the calculated CRC32c checksum is the same as the received
569 CRC32c checksum. If it is not, the receiver MUST treat the TCP connection
570 providing the header as invalid.
571
572The default procedure for handling an invalid TCP connection is to abort it.
573
574
5752.2.2. The PP2_TYPE_SSL type and subtypes
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200576
577For the type PP2_TYPE_SSL, the value is itselv a defined like this :
578
579 struct pp2_tlv_ssl {
580 uint8_t client;
581 uint32_t verify;
582 struct pp2_tlv sub_tlv[0];
583 };
584
Nikos Mavrogiannopoulosf1650a82015-08-24 15:53:18 +0200585The <verify> field will be zero if the client presented a certificate
586and it was successfully verified, and non-zero otherwise.
587
588The <client> field is made of a bit field from the following values,
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200589indicating which element is present :
590
591 #define PP2_CLIENT_SSL 0x01
592 #define PP2_CLIENT_CERT_CONN 0x02
593 #define PP2_CLIENT_CERT_SESS 0x04
594
Nikos Mavrogiannopoulosf1650a82015-08-24 15:53:18 +0200595Note, that each of these elements may lead to extra data being appended to
596this TLV using a second level of TLV encapsulation. It is thus possible to
597find multiple TLV values after this field. The total length of the pp2_tlv_ssl
598TLV will reflect this.
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200599
Nikos Mavrogiannopoulosf1650a82015-08-24 15:53:18 +0200600The PP2_CLIENT_SSL flag indicates that the client connected over SSL/TLS. When
601this field is present, the string representation of the TLS version is appended
602at the end of the field in the TLV format using the type PP2_SUBTYPE_SSL_VERSION.
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200603
604PP2_CLIENT_CERT_CONN indicates that the client provided a certificate over the
605current connection. PP2_CLIENT_CERT_SESS indicates that the client provided a
Nikos Mavrogiannopoulosf1650a82015-08-24 15:53:18 +0200606certificate at least once over the TLS session this connection belongs to.
607
608In all cases, the string representation (in UTF8) of the Common Name field
609(OID: 2.5.4.3) of the client certificate's DistinguishedName, is appended
610using the TLV format and the type PP2_SUBTYPE_SSL_CN.
611
612
Andriy Palamarchukceae85b2017-01-24 13:48:27 -05006132.2.3. The PP2_TYPE_NETNS type
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200614
615The type PP2_TYPE_NETNS defines the value as the string representation of the
616namespace's name.
617
Andriy Palamarchukceae85b2017-01-24 13:48:27 -0500618
6192.2.4. Reserved type ranges
620
621The following range of 16 type values is reserved for application-specific
622data and will be never used by the PROXY Protocol. If you need more values
623consider extending the range with a type field in your TLVs.
624
625 #define PP2_TYPE_MIN_CUSTOM 0xE0
626 #define PP2_TYPE_MAX_CUSTOM 0xEF
627
628This range of 8 values is reserved for temporary experimental use by
629application developers and protocol designers. The values from the range will
630never be used by the PROXY protocol and should not be used by production
631functionality.
632
633 #define PP2_TYPE_MIN_EXPERIMENT 0xF0
634 #define PP2_TYPE_MAX_EXPERIMENT 0xF7
635
636The following range of 8 values is reserved for future use, potentially to
637extend the protocol with multibyte type values.
638
639 #define PP2_TYPE_MIN_FUTURE 0xF8
640 #define PP2_TYPE_MAX_FUTURE 0xFF
641
Willy Tarreau7f898512011-03-20 11:32:40 +0100642
6433. Implementations
644
Willy Tarreau332d7b02012-11-19 11:27:29 +0100645Haproxy 1.5 implements version 1 of the PROXY protocol on both sides :
Willy Tarreau7f898512011-03-20 11:32:40 +0100646 - the listening sockets accept the protocol when the "accept-proxy" setting
647 is passed to the "bind" keyword. Connections accepted on such listeners
648 will behave just as if the source really was the one advertised in the
649 protocol. This is true for logging, ACLs, content filtering, transparent
650 proxying, etc...
651
652 - the protocol may be used to connect to servers if the "send-proxy" setting
653 is present on the "server" line. It is enabled on a per-server basis, so it
654 is possible to have it enabled for remote servers only and still have local
655 ones behave differently. If the incoming connection was accepted with the
656 "accept-proxy", then the relayed information is the one advertised in this
657 connection's PROXY line.
658
David Safb76832014-05-08 23:42:08 -0400659 - Haproxy 1.5 also implements version 2 of the PROXY protocol as a sender. In
660 addition, a TLV with limited, optional, SSL information has been added.
661
Willy Tarreau332d7b02012-11-19 11:27:29 +0100662Stunnel added support for version 1 of the protocol for outgoing connections in
663version 4.45.
Willy Tarreau7f898512011-03-20 11:32:40 +0100664
Willy Tarreau332d7b02012-11-19 11:27:29 +0100665Stud added support for version 1 of the protocol for outgoing connections on
6662011/06/29.
667
668Postfix added support for version 1 of the protocol for incoming connections
669in smtpd and postscreen in version 2.10.
670
671A patch is available for Stud[5] to implement version 1 of the protocol on
672incoming connections.
673
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200674Support for versions 1 and 2 of the protocol was added to Varnish 4.1 [6].
Willy Tarreau332d7b02012-11-19 11:27:29 +0100675
Todd Lyonsd1dcea02014-06-03 13:29:33 -0700676Exim added support for version 1 and version 2 of the protocol for incoming
677connections on 2014/05/13, and will be released as part of version 4.83.
678
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200679Squid added support for versions 1 and 2 of the protocol in version 3.5 [7].
680
681Jetty 9.3.0 supports protocol version 1.
682
Willy Tarreau332d7b02012-11-19 11:27:29 +0100683The protocol is simple enough that it is expected that other implementations
684will appear, especially in environments such as SMTP, IMAP, FTP, RDP where the
Willy Tarreau7f898512011-03-20 11:32:40 +0100685client's address is an important piece of information for the server and some
Willy Tarreau332d7b02012-11-19 11:27:29 +0100686intermediaries. In fact, several proprietary deployments have already done so
687on FTP and SMTP servers.
Willy Tarreau7f898512011-03-20 11:32:40 +0100688
689Proxy developers are encouraged to implement this protocol, because it will
690make their products much more transparent in complex infrastructures, and will
691get rid of a number of issues related to logging and access control.
692
Willy Tarreau332d7b02012-11-19 11:27:29 +0100693
6944. Architectural benefits
6954.1. Multiple layers
696
697Using the PROXY protocol instead of transparent proxy provides several benefits
698in multiple-layer infrastructures. The first immediate benefit is that it
699becomes possible to chain multiple layers of proxies and always present the
700original IP address. for instance, let's consider the following 2-layer proxy
701architecture :
702
703 Internet
704 ,---. | client to PX1:
705 ( X ) | native protocol
706 `---' |
707 | V
708 +--+--+ +-----+
709 | FW1 |------| PX1 |
710 +--+--+ +-----+ | PX1 to PX2: PROXY + native
711 | V
712 +--+--+ +-----+
713 | FW2 |------| PX2 |
714 +--+--+ +-----+ | PX2 to SRV: PROXY + native
715 | V
716 +--+--+
717 | SRV |
718 +-----+
Willy Tarreau7f898512011-03-20 11:32:40 +0100719
Willy Tarreau332d7b02012-11-19 11:27:29 +0100720Firewall FW1 receives traffic from internet-based clients and forwards it to
721reverse-proxy PX1. PX1 adds a PROXY header then forwards to PX2 via FW2. PX2
722is configured to read the PROXY header and to emit it on output. It then joins
723the origin server SRV and presents the original client's address there. Since
724all TCP connections endpoints are real machines and are not spoofed, there is
725no issue for the return traffic to pass via the firewalls and reverse proxies.
726Using transparent proxy, this would be quite difficult because the firewalls
727would have to deal with the client's address coming from the proxies in the DMZ
728and would have to correctly route the return traffic there instead of using the
729default route.
Willy Tarreau7f898512011-03-20 11:32:40 +0100730
Willy Tarreau332d7b02012-11-19 11:27:29 +0100731
7324.2. IPv4 and IPv6 integration
733
734The protocol also eases IPv4 and IPv6 integration : if only the first layer
735(FW1 and PX1) is IPv6-capable, it is still possible to present the original
Andriy Palamarchukf1eae4e2017-01-24 13:34:08 -0500736client's IPv6 address to the target server even though the whole chain is only
Willy Tarreau332d7b02012-11-19 11:27:29 +0100737connected via IPv4.
738
739
7404.3. Multiple return paths
741
742When transparent proxy is used, it is not possible to run multiple proxies
743because the return traffic would follow the default route instead of finding
744the proper proxy. Some tricks are sometimes possible using multiple server
745addresses and policy routing but these are very limited.
746
747Using the PROXY protocol, this problem disappears as the servers don't need
748to route to the client, just to the proxy that forwarded the connection. So
749it is perfectly possible to run a proxy farm in front of a very large server
750farm and have it working effortless, even when dealing with multiple sites.
751
752This is particularly important in Cloud-like environments where there is little
753choice of binding to random addresses and where the lower processing power per
754node generally requires multiple front nodes.
755
756The example below illustrates the following case : virtualized infrastructures
757are deployed in 3 datacenters (DC1..DC3). Each DC uses its own VIP which is
758handled by the hosting provider's layer 3 load balancer. This load balancer
759routes the traffic to a farm of layer 7 SSL/cache offloaders which load balance
760among their local servers. The VIPs are advertised by geolocalised DNS so that
761clients generally stick to a given DC. Since clients are not guaranteed to
762stick to one DC, the L7 load balancing proxies have to know the other DCs'
763servers that may be reached via the hosting provider's LAN or via the internet.
764The L7 proxies use the PROXY protocol to join the servers behind them, so that
765even inter-DC traffic can forward the original client's address and the return
766path is unambiguous. This would not be possible using transparent proxy because
767most often the L7 proxies would not be able to spoof an address, and this would
768never work between datacenters.
769
770 Internet
771
772 DC1 DC2 DC3
773 ,---. ,---. ,---.
774 ( X ) ( X ) ( X )
775 `---' `---' `---'
776 | +-------+ | +-------+ | +-------+
777 +----| L3 LB | +----| L3 LB | +----| L3 LB |
778 | +-------+ | +-------+ | +-------+
779 ------+------- ~ ~ ~ ------+------- ~ ~ ~ ------+-------
780 ||||| |||| ||||| |||| ||||| ||||
781 50 SRV 4 PX 50 SRV 4 PX 50 SRV 4 PX
782
783
7845. Security considerations
785
786Version 1 of the protocol header (the human-readable format) was designed so as
787to be distinguishable from HTTP. It will not parse as a valid HTTP request and
788an HTTP request will not parse as a valid proxy request. Version 2 add to use a
789non-parsable binary signature to make many products fail on this block. The
790signature was designed to cause immediate failure on HTTP, SSL/TLS, SMTP, FTP,
791and POP. It also causes aborts on LDAP and RDP servers (see section 6). That
792makes it easier to enforce its use under certain connections and at the same
793time, it ensures that improperly configured servers are quickly detected.
794
Willy Tarreau7f898512011-03-20 11:32:40 +0100795Implementers should be very careful about not trying to automatically detect
Willy Tarreau332d7b02012-11-19 11:27:29 +0100796whether they have to decode the header or not, but rather they must only rely
797on a configuration parameter. Indeed, if the opportunity is left to a normal
798client to use the protocol, he will be able to hide his activities or make them
799appear as coming from someone else. However, accepting the header only from a
800number of known sources should be safe.
801
802
8036. Validation
Willy Tarreau7f898512011-03-20 11:32:40 +0100804
Willy Tarreau332d7b02012-11-19 11:27:29 +0100805The version 2 protocol signature has been sent to a wide variety of protocols
806and implementations including old ones. The following protocol and products
Andriy Palamarchukf1eae4e2017-01-24 13:34:08 -0500807have been tested to ensure the best possible behavior when the signature was
Willy Tarreau332d7b02012-11-19 11:27:29 +0100808presented, even with minimal implementations :
Willy Tarreau7f898512011-03-20 11:32:40 +0100809
Willy Tarreau332d7b02012-11-19 11:27:29 +0100810 - HTTP :
811 - Apache 1.3.33 : connection abort => pass/optimal
812 - Nginx 0.7.69 : 400 Bad Request + abort => pass/optimal
813 - lighttpd 1.4.20 : 400 Bad Request + abort => pass/optimal
814 - thttpd 2.20c : 400 Bad Request + abort => pass/optimal
815 - mini-httpd-1.19 : 400 Bad Request + abort => pass/optimal
816 - haproxy 1.4.21 : 400 Bad Request + abort => pass/optimal
Willy Tarreau9e138202014-07-12 17:31:07 +0200817 - Squid 3 : 400 Bad Request + abort => pass/optimal
Willy Tarreau332d7b02012-11-19 11:27:29 +0100818 - SSL :
819 - stud 0.3.47 : connection abort => pass/optimal
820 - stunnel 4.45 : connection abort => pass/optimal
821 - nginx 0.7.69 : 400 Bad Request + abort => pass/optimal
822 - FTP :
823 - Pure-ftpd 1.0.20 : 3*500 then 221 Goodbye => pass/optimal
824 - vsftpd 2.0.1 : 3*530 then 221 Goodbye => pass/optimal
825 - SMTP :
826 - postfix 2.3 : 3*500 + 221 Bye => pass/optimal
827 - exim 4.69 : 554 + connection abort => pass/optimal
828 - POP :
829 - dovecot 1.0.10 : 3*ERR + Logout => pass/optimal
830 - IMAP :
831 - dovecot 1.0.10 : 5*ERR + hang => pass/non-optimal
832 - LDAP :
833 - openldap 2.3 : abort => pass/optimal
834 - SSH :
835 - openssh 3.9p1 : abort => pass/optimal
836 - RDP :
837 - Windows XP SP3 : abort => pass/optimal
838
839This means that most protocols and implementations will not be confused by an
840incoming connection exhibiting the protocol signature, which avoids issues when
841facing misconfigurations.
842
843
8447. Future developments
Willy Tarreau640cf222010-10-29 21:46:16 +0200845
846It is possible that the protocol may slightly evolve to present other
847information such as the incoming network interface, or the origin addresses in
848case of network address translation happening before the first proxy, but this
Willy Tarreau332d7b02012-11-19 11:27:29 +0100849is not identified as a requirement right now. Some deep thinking has been spent
Andriy Palamarchukf1eae4e2017-01-24 13:34:08 -0500850on this and it appears that trying to add a few more information open a Pandora
Willy Tarreau332d7b02012-11-19 11:27:29 +0100851box with many information from MAC addresses to SSL client certificates, which
852would make the protocol much more complex. So at this point it is not planned.
853Suggestions on improvements are welcome.
Willy Tarreau7f898512011-03-20 11:32:40 +0100854
855
Willy Tarreau332d7b02012-11-19 11:27:29 +01008568. Contacts and links
Willy Tarreau7f898512011-03-20 11:32:40 +0100857
858Please use w@1wt.eu to send any comments to the author.
859
Willy Tarreau332d7b02012-11-19 11:27:29 +0100860The following links were referenced in the document.
861
862[1] http://www.postfix.org/XCLIENT_README.html
Willy Tarreau7a6f1342014-06-14 11:45:09 +0200863[2] http://tools.ietf.org/html/rfc7239
Willy Tarreau332d7b02012-11-19 11:27:29 +0100864[3] http://www.stunnel.org/
865[4] https://github.com/bumptech/stud
866[5] https://github.com/bumptech/stud/pull/81
Willy Tarreau7b7011c2015-05-02 15:13:07 +0200867[6] https://www.varnish-cache.org/docs/trunk/phk/ssl_again.html
868[7] http://wiki.squid-cache.org/Squid-3.5
Andriy Palamarchukceae85b2017-01-24 13:48:27 -0500869[8] https://tools.ietf.org/html/rfc4960#appendix-B
Willy Tarreau332d7b02012-11-19 11:27:29 +0100870
871
8729. Sample code
873
874The code below is an example of how a receiver may deal with both versions of
875the protocol header for TCP over IPv4 or IPv6. The function is supposed to be
876called upon a read event. Addresses may be directly copied into their final
877memory location since they're transported in network byte order. The sending
878side is even simpler and can easily be deduced from this sample code.
879
880 struct sockaddr_storage from; /* already filled by accept() */
881 struct sockaddr_storage to; /* already filled by getsockname() */
Willy Tarreau01320c92014-06-14 08:36:29 +0200882 const char v2sig[12] = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";
Willy Tarreau332d7b02012-11-19 11:27:29 +0100883
884 /* returns 0 if needs to poll, <0 upon error or >0 if it did the job */
885 int read_evt(int fd)
886 {
887 union {
888 struct {
889 char line[108];
890 } v1;
891 struct {
892 uint8_t sig[12];
Willy Tarreau0f6093a2014-06-11 21:21:26 +0200893 uint8_t ver_cmd;
Willy Tarreau332d7b02012-11-19 11:27:29 +0100894 uint8_t fam;
Willy Tarreau0f6093a2014-06-11 21:21:26 +0200895 uint16_t len;
Willy Tarreau332d7b02012-11-19 11:27:29 +0100896 union {
897 struct { /* for TCP/UDP over IPv4, len = 12 */
898 uint32_t src_addr;
899 uint32_t dst_addr;
900 uint16_t src_port;
901 uint16_t dst_port;
902 } ip4;
903 struct { /* for TCP/UDP over IPv6, len = 36 */
904 uint8_t src_addr[16];
905 uint8_t dst_addr[16];
906 uint16_t src_port;
907 uint16_t dst_port;
908 } ip6;
909 struct { /* for AF_UNIX sockets, len = 216 */
910 uint8_t src_addr[108];
911 uint8_t dst_addr[108];
912 } unx;
913 } addr;
914 } v2;
915 } hdr;
916
917 int size, ret;
918
919 do {
920 ret = recv(fd, &hdr, sizeof(hdr), MSG_PEEK);
921 } while (ret == -1 && errno == EINTR);
922
923 if (ret == -1)
924 return (errno == EAGAIN) ? 0 : -1;
925
Willy Tarreau01320c92014-06-14 08:36:29 +0200926 if (ret >= 16 && memcmp(&hdr.v2, v2sig, 12) == 0 &&
927 (hdr.v2.ver_cmd & 0xF0) == 0x20) {
Willy Tarreau332d7b02012-11-19 11:27:29 +0100928 size = 16 + hdr.v2.len;
929 if (ret < size)
930 return -1; /* truncated or too large header */
931
Willy Tarreau0f6093a2014-06-11 21:21:26 +0200932 switch (hdr.v2.ver_cmd & 0xF) {
Willy Tarreau332d7b02012-11-19 11:27:29 +0100933 case 0x01: /* PROXY command */
934 switch (hdr.v2.fam) {
935 case 0x11: /* TCPv4 */
936 ((struct sockaddr_in *)&from)->sin_family = AF_INET;
937 ((struct sockaddr_in *)&from)->sin_addr.s_addr =
938 hdr.v2.addr.ip4.src_addr;
939 ((struct sockaddr_in *)&from)->sin_port =
940 hdr.v2.addr.ip4.src_port;
941 ((struct sockaddr_in *)&to)->sin_family = AF_INET;
942 ((struct sockaddr_in *)&to)->sin_addr.s_addr =
943 hdr.v2.addr.ip4.dst_addr;
944 ((struct sockaddr_in *)&to)->sin_port =
945 hdr.v2.addr.ip4.dst_port;
946 goto done;
947 case 0x21: /* TCPv6 */
948 ((struct sockaddr_in6 *)&from)->sin6_family = AF_INET6;
949 memcpy(&((struct sockaddr_in6 *)&from)->sin6_addr,
950 hdr.v2.addr.ip6.src_addr, 16);
951 ((struct sockaddr_in6 *)&from)->sin6_port =
952 hdr.v2.addr.ip6.src_port;
953 ((struct sockaddr_in6 *)&to)->sin6_family = AF_INET6;
954 memcpy(&((struct sockaddr_in6 *)&to)->sin6_addr,
955 hdr.v2.addr.ip6.dst_addr, 16);
956 ((struct sockaddr_in6 *)&to)->sin6_port =
957 hdr.v2.addr.ip6.dst_port;
958 goto done;
959 }
960 /* unsupported protocol, keep local connection address */
961 break;
962 case 0x00: /* LOCAL command */
963 /* keep local connection address for LOCAL */
964 break;
965 default:
966 return -1; /* not a supported command */
967 }
968 }
969 else if (ret >= 8 && memcmp(hdr.v1.line, "PROXY", 5) == 0) {
970 char *end = memchr(hdr.v1.line, '\r', ret - 1);
971 if (!end || end[1] != '\n')
972 return -1; /* partial or invalid header */
973 *end = '\0'; /* terminate the string to ease parsing */
974 size = end + 2 - hdr.v1.line; /* skip header + CRLF */
975 /* parse the V1 header using favorite address parsers like inet_pton.
976 * return -1 upon error, or simply fall through to accept.
977 */
978 }
979 else {
980 /* Wrong protocol */
981 return -1;
982 }
983
984 done:
985 /* we need to consume the appropriate amount of data from the socket */
986 do {
987 ret = recv(fd, &hdr, size, 0);
988 } while (ret == -1 && errno == EINTR);
989 return (ret >= 0) ? 1 : -1;
990 }