1 /* 2 * util/proxy_protocol.h - PROXY protocol 3 * 4 * Copyright (c) 2022, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 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. 18 * 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. 22 * 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. 34 */ 35 36 /** 37 * \file 38 * 39 * This file contains PROXY protocol structs and functions. 40 * Only v2 is supported. TLVs are not currently supported. 41 */ 42 #ifndef PROXY_PROTOCOL_H 43 #define PROXY_PROTOCOL_H 44 45 #include "sldns/sbuffer.h" 46 47 /** PROXYv2 minimum header size */ 48 #define PP2_HEADER_SIZE 16 49 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 53 54 /** PROXYv2 version */ 55 #define PP2_VERSION 0x2 56 57 /** 58 * PROXYv2 command. 59 */ 60 enum pp2_command { 61 PP2_CMD_LOCAL = 0x0, 62 PP2_CMD_PROXY = 0x1 63 }; 64 65 /** 66 * PROXYv2 address family. 67 */ 68 enum pp2_af { 69 PP2_AF_UNSPEC = 0x0, 70 PP2_AF_INET = 0x1, 71 PP2_AF_INET6 = 0x2, 72 PP2_AF_UNIX = 0x3 73 }; 74 75 /** 76 * PROXYv2 protocol. 77 */ 78 enum pp2_protocol { 79 PP2_PROT_UNSPEC = 0x0, 80 PP2_PROT_STREAM = 0x1, 81 PP2_PROT_DGRAM = 0x2 82 }; 83 84 /** 85 * PROXYv2 header. 86 */ 87 struct pp2_header { 88 uint8_t sig[PP2_SIG_LEN]; 89 uint8_t ver_cmd; 90 uint8_t fam_prot; 91 uint16_t len; 92 union { 93 struct { /* for TCP/UDP over IPv4, len = 12 */ 94 uint32_t src_addr; 95 uint32_t dst_addr; 96 uint16_t src_port; 97 uint16_t dst_port; 98 } addr4; 99 struct { /* for TCP/UDP over IPv6, len = 36 */ 100 uint8_t src_addr[16]; 101 uint8_t dst_addr[16]; 102 uint16_t src_port; 103 uint16_t dst_port; 104 } addr6; 105 struct { /* for AF_UNIX sockets, len = 216 */ 106 uint8_t src_addr[108]; 107 uint8_t dst_addr[108]; 108 } addru; 109 } addr; 110 }; 111 112 /** 113 * Write a PROXYv2 header at the current position of the buffer. 114 * @param buf: the buffer to write to. 115 * @param src: the source address. 116 * @param stream: if the protocol is stream or datagram. 117 * @return 1 on success, 0 on failure. 118 */ 119 int pp2_write_to_buf(struct sldns_buffer* buf, struct sockaddr_storage* src, 120 int stream); 121 122 /** 123 * Read a PROXYv2 header from the current position of the buffer. 124 * It does initial validation and returns a pointer to the buffer position on 125 * success. 126 * @param buf: the buffer to read from. 127 * @return the pointer to the buffer position on success, NULL on error. 128 */ 129 struct pp2_header* pp2_read_header(struct sldns_buffer* buf); 130 131 #endif /* PROXY_PROTOCOL_H */ 132