2 * util/proxy_protocol.h - PROXY protocol
4 * Copyright (c) 2022, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains PROXY protocol structs and functions.
40 * Only v2 is supported. TLVs are not currently supported.
42 #ifndef PROXY_PROTOCOL_H
43 #define PROXY_PROTOCOL_H
47 /** PROXYv2 minimum header size */
48 #define PP2_HEADER_SIZE 16
50 /** PROXYv2 header signature */
51 #define PP2_SIG "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A"
52 #define PP2_SIG_LEN 12
54 /** PROXYv2 version (protocol value) */
55 #define PP2_VERSION 0x2
58 * PROXYv2 command (protocol value).
66 * PROXYv2 address family (protocol value).
76 * PROXYv2 protocol (protocol value).
79 PP2_PROT_UNSPEC = 0x0,
80 PP2_PROT_STREAM = 0x1,
85 * Expected combinations of address family and protocol values used in checks.
87 enum pp2_af_protocol_combination {
88 PP2_UNSPEC_UNSPEC = (PP2_AF_UNSPEC<<4)|PP2_PROT_UNSPEC,
89 PP2_INET_STREAM = (PP2_AF_INET<<4)|PP2_PROT_STREAM,
90 PP2_INET_DGRAM = (PP2_AF_INET<<4)|PP2_PROT_DGRAM,
91 PP2_INET6_STREAM = (PP2_AF_INET6<<4)|PP2_PROT_STREAM,
92 PP2_INET6_DGRAM = (PP2_AF_INET6<<4)|PP2_PROT_DGRAM,
93 PP2_UNIX_STREAM = (PP2_AF_UNIX<<4)|PP2_PROT_STREAM,
94 PP2_UNIX_DGRAM = (PP2_AF_UNIX<<4)|PP2_PROT_DGRAM
101 uint8_t sig[PP2_SIG_LEN];
106 struct { /* for TCP/UDP over IPv4, len = 12 */
112 struct { /* for TCP/UDP over IPv6, len = 36 */
113 uint8_t src_addr[16];
114 uint8_t dst_addr[16];
118 struct { /* for AF_UNIX sockets, len = 216 */
119 uint8_t src_addr[108];
120 uint8_t dst_addr[108];
126 * PROXY parse errors.
128 enum pp_parse_errors {
129 PP_PARSE_NOERROR = 0,
131 PP_PARSE_WRONG_HEADERv2,
132 PP_PARSE_UNKNOWN_CMD,
133 PP_PARSE_UNKNOWN_FAM_PROT,
137 * Initialize the internal proxy structure.
138 * @param write_uint16: pointer to a function that can write uint16.
139 * @param write_uint32: pointer to a function that can write uint32.
141 void pp_init(void (*write_uint16)(void* buf, uint16_t data),
142 void (*write_uint32)(void* buf, uint32_t data));
145 * Lookup the parsing error description.
146 * @param error: parsing error from pp2_read_header.
147 * @return the description.
149 const char* pp_lookup_error(enum pp_parse_errors error);
152 * Write a PROXYv2 header at the current position of the buffer.
153 * @param buf: pointer to the buffer to write data to.
154 * @param buflen: available size on the buffer.
155 * @param src: the source address.
156 * @param stream: if the protocol is stream or datagram.
157 * @return 1 on success, 0 on failure.
159 size_t pp2_write_to_buf(uint8_t* buf, size_t buflen,
161 struct sockaddr_storage* src,
163 struct sockaddr_in* src,
168 * Read a PROXYv2 header from the current position of the buffer.
169 * It does initial validation and returns a pointer to the buffer position on
171 * @param buf: pointer to the buffer data to read from.
172 * @param buflen: available size on the buffer.
173 * @return parsing error, 0 on success.
175 int pp2_read_header(uint8_t* buf, size_t buflen);
177 #endif /* PROXY_PROTOCOL_H */