1*b71395eaSflorian /* 2*b71395eaSflorian * util/proxy_protocol.h - PROXY protocol 3*b71395eaSflorian * 4*b71395eaSflorian * Copyright (c) 2022, NLnet Labs. All rights reserved. 5*b71395eaSflorian * 6*b71395eaSflorian * This software is open source. 7*b71395eaSflorian * 8*b71395eaSflorian * Redistribution and use in source and binary forms, with or without 9*b71395eaSflorian * modification, are permitted provided that the following conditions 10*b71395eaSflorian * are met: 11*b71395eaSflorian * 12*b71395eaSflorian * Redistributions of source code must retain the above copyright notice, 13*b71395eaSflorian * this list of conditions and the following disclaimer. 14*b71395eaSflorian * 15*b71395eaSflorian * Redistributions in binary form must reproduce the above copyright notice, 16*b71395eaSflorian * this list of conditions and the following disclaimer in the documentation 17*b71395eaSflorian * and/or other materials provided with the distribution. 18*b71395eaSflorian * 19*b71395eaSflorian * Neither the name of the NLNET LABS nor the names of its contributors may 20*b71395eaSflorian * be used to endorse or promote products derived from this software without 21*b71395eaSflorian * specific prior written permission. 22*b71395eaSflorian * 23*b71395eaSflorian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24*b71395eaSflorian * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25*b71395eaSflorian * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26*b71395eaSflorian * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27*b71395eaSflorian * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28*b71395eaSflorian * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29*b71395eaSflorian * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30*b71395eaSflorian * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31*b71395eaSflorian * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32*b71395eaSflorian * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33*b71395eaSflorian * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34*b71395eaSflorian */ 35*b71395eaSflorian 36*b71395eaSflorian /** 37*b71395eaSflorian * \file 38*b71395eaSflorian * 39*b71395eaSflorian * This file contains PROXY protocol structs and functions. 40*b71395eaSflorian * Only v2 is supported. TLVs are not currently supported. 41*b71395eaSflorian */ 42*b71395eaSflorian #ifndef PROXY_PROTOCOL_H 43*b71395eaSflorian #define PROXY_PROTOCOL_H 44*b71395eaSflorian 45*b71395eaSflorian #include "config.h" 46*b71395eaSflorian 47*b71395eaSflorian /** PROXYv2 minimum header size */ 48*b71395eaSflorian #define PP2_HEADER_SIZE 16 49*b71395eaSflorian 50*b71395eaSflorian /** PROXYv2 header signature */ 51*b71395eaSflorian #define PP2_SIG "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A" 52*b71395eaSflorian #define PP2_SIG_LEN 12 53*b71395eaSflorian 54*b71395eaSflorian /** PROXYv2 version (protocol value) */ 55*b71395eaSflorian #define PP2_VERSION 0x2 56*b71395eaSflorian 57*b71395eaSflorian /** 58*b71395eaSflorian * PROXYv2 command (protocol value). 59*b71395eaSflorian */ 60*b71395eaSflorian enum pp2_command { 61*b71395eaSflorian PP2_CMD_LOCAL = 0x0, 62*b71395eaSflorian PP2_CMD_PROXY = 0x1 63*b71395eaSflorian }; 64*b71395eaSflorian 65*b71395eaSflorian /** 66*b71395eaSflorian * PROXYv2 address family (protocol value). 67*b71395eaSflorian */ 68*b71395eaSflorian enum pp2_af { 69*b71395eaSflorian PP2_AF_UNSPEC = 0x0, 70*b71395eaSflorian PP2_AF_INET = 0x1, 71*b71395eaSflorian PP2_AF_INET6 = 0x2, 72*b71395eaSflorian PP2_AF_UNIX = 0x3 73*b71395eaSflorian }; 74*b71395eaSflorian 75*b71395eaSflorian /** 76*b71395eaSflorian * PROXYv2 protocol (protocol value). 77*b71395eaSflorian */ 78*b71395eaSflorian enum pp2_protocol { 79*b71395eaSflorian PP2_PROT_UNSPEC = 0x0, 80*b71395eaSflorian PP2_PROT_STREAM = 0x1, 81*b71395eaSflorian PP2_PROT_DGRAM = 0x2 82*b71395eaSflorian }; 83*b71395eaSflorian 84*b71395eaSflorian /** 85*b71395eaSflorian * Expected combinations of address family and protocol values used in checks. 86*b71395eaSflorian */ 87*b71395eaSflorian enum pp2_af_protocol_combination { 88*b71395eaSflorian PP2_UNSPEC_UNSPEC = (PP2_AF_UNSPEC<<4)|PP2_PROT_UNSPEC, 89*b71395eaSflorian PP2_INET_STREAM = (PP2_AF_INET<<4)|PP2_PROT_STREAM, 90*b71395eaSflorian PP2_INET_DGRAM = (PP2_AF_INET<<4)|PP2_PROT_DGRAM, 91*b71395eaSflorian PP2_INET6_STREAM = (PP2_AF_INET6<<4)|PP2_PROT_STREAM, 92*b71395eaSflorian PP2_INET6_DGRAM = (PP2_AF_INET6<<4)|PP2_PROT_DGRAM, 93*b71395eaSflorian PP2_UNIX_STREAM = (PP2_AF_UNIX<<4)|PP2_PROT_STREAM, 94*b71395eaSflorian PP2_UNIX_DGRAM = (PP2_AF_UNIX<<4)|PP2_PROT_DGRAM 95*b71395eaSflorian }; 96*b71395eaSflorian 97*b71395eaSflorian /** 98*b71395eaSflorian * PROXYv2 header. 99*b71395eaSflorian */ 100*b71395eaSflorian struct pp2_header { 101*b71395eaSflorian uint8_t sig[PP2_SIG_LEN]; 102*b71395eaSflorian uint8_t ver_cmd; 103*b71395eaSflorian uint8_t fam_prot; 104*b71395eaSflorian uint16_t len; 105*b71395eaSflorian union { 106*b71395eaSflorian struct { /* for TCP/UDP over IPv4, len = 12 */ 107*b71395eaSflorian uint32_t src_addr; 108*b71395eaSflorian uint32_t dst_addr; 109*b71395eaSflorian uint16_t src_port; 110*b71395eaSflorian uint16_t dst_port; 111*b71395eaSflorian } addr4; 112*b71395eaSflorian struct { /* for TCP/UDP over IPv6, len = 36 */ 113*b71395eaSflorian uint8_t src_addr[16]; 114*b71395eaSflorian uint8_t dst_addr[16]; 115*b71395eaSflorian uint16_t src_port; 116*b71395eaSflorian uint16_t dst_port; 117*b71395eaSflorian } addr6; 118*b71395eaSflorian struct { /* for AF_UNIX sockets, len = 216 */ 119*b71395eaSflorian uint8_t src_addr[108]; 120*b71395eaSflorian uint8_t dst_addr[108]; 121*b71395eaSflorian } addru; 122*b71395eaSflorian } addr; 123*b71395eaSflorian }; 124*b71395eaSflorian 125*b71395eaSflorian /** 126*b71395eaSflorian * PROXY parse errors. 127*b71395eaSflorian */ 128*b71395eaSflorian enum pp_parse_errors { 129*b71395eaSflorian PP_PARSE_NOERROR = 0, 130*b71395eaSflorian PP_PARSE_SIZE, 131*b71395eaSflorian PP_PARSE_WRONG_HEADERv2, 132*b71395eaSflorian PP_PARSE_UNKNOWN_CMD, 133*b71395eaSflorian PP_PARSE_UNKNOWN_FAM_PROT, 134*b71395eaSflorian }; 135*b71395eaSflorian 136*b71395eaSflorian /** 137*b71395eaSflorian * Initialize the internal proxy structure. 138*b71395eaSflorian * @param write_uint16: pointer to a function that can write uint16. 139*b71395eaSflorian * @param write_uint32: pointer to a function that can write uint32. 140*b71395eaSflorian */ 141*b71395eaSflorian void pp_init(void (*write_uint16)(void* buf, uint16_t data), 142*b71395eaSflorian void (*write_uint32)(void* buf, uint32_t data)); 143*b71395eaSflorian 144*b71395eaSflorian /** 145*b71395eaSflorian * Lookup the parsing error description. 146*b71395eaSflorian * @param error: parsing error from pp2_read_header. 147*b71395eaSflorian * @return the description. 148*b71395eaSflorian */ 149*b71395eaSflorian const char* pp_lookup_error(enum pp_parse_errors error); 150*b71395eaSflorian 151*b71395eaSflorian /** 152*b71395eaSflorian * Write a PROXYv2 header at the current position of the buffer. 153*b71395eaSflorian * @param buf: pointer to the buffer to write data to. 154*b71395eaSflorian * @param buflen: available size on the buffer. 155*b71395eaSflorian * @param src: the source address. 156*b71395eaSflorian * @param stream: if the protocol is stream or datagram. 157*b71395eaSflorian * @return 1 on success, 0 on failure. 158*b71395eaSflorian */ 159*b71395eaSflorian size_t pp2_write_to_buf(uint8_t* buf, size_t buflen, 160*b71395eaSflorian #ifdef INET6 161*b71395eaSflorian struct sockaddr_storage* src, 162*b71395eaSflorian #else 163*b71395eaSflorian struct sockaddr_in* src, 164*b71395eaSflorian #endif 165*b71395eaSflorian int stream); 166*b71395eaSflorian 167*b71395eaSflorian /** 168*b71395eaSflorian * Read a PROXYv2 header from the current position of the buffer. 169*b71395eaSflorian * It does initial validation and returns a pointer to the buffer position on 170*b71395eaSflorian * success. 171*b71395eaSflorian * @param buf: pointer to the buffer data to read from. 172*b71395eaSflorian * @param buflen: available size on the buffer. 173*b71395eaSflorian * @return parsing error, 0 on success. 174*b71395eaSflorian */ 175*b71395eaSflorian int pp2_read_header(uint8_t* buf, size_t buflen); 176*b71395eaSflorian 177*b71395eaSflorian #endif /* PROXY_PROTOCOL_H */ 178