15c45b740Sflorian /* 25c45b740Sflorian * util/proxy_protocol.h - PROXY protocol 35c45b740Sflorian * 45c45b740Sflorian * Copyright (c) 2022, NLnet Labs. All rights reserved. 55c45b740Sflorian * 65c45b740Sflorian * This software is open source. 75c45b740Sflorian * 85c45b740Sflorian * Redistribution and use in source and binary forms, with or without 95c45b740Sflorian * modification, are permitted provided that the following conditions 105c45b740Sflorian * are met: 115c45b740Sflorian * 125c45b740Sflorian * Redistributions of source code must retain the above copyright notice, 135c45b740Sflorian * this list of conditions and the following disclaimer. 145c45b740Sflorian * 155c45b740Sflorian * Redistributions in binary form must reproduce the above copyright notice, 165c45b740Sflorian * this list of conditions and the following disclaimer in the documentation 175c45b740Sflorian * and/or other materials provided with the distribution. 185c45b740Sflorian * 195c45b740Sflorian * Neither the name of the NLNET LABS nor the names of its contributors may 205c45b740Sflorian * be used to endorse or promote products derived from this software without 215c45b740Sflorian * specific prior written permission. 225c45b740Sflorian * 235c45b740Sflorian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 245c45b740Sflorian * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 255c45b740Sflorian * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 265c45b740Sflorian * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 275c45b740Sflorian * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 285c45b740Sflorian * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 295c45b740Sflorian * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 305c45b740Sflorian * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 315c45b740Sflorian * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 325c45b740Sflorian * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 335c45b740Sflorian * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 345c45b740Sflorian */ 355c45b740Sflorian 365c45b740Sflorian /** 375c45b740Sflorian * \file 385c45b740Sflorian * 395c45b740Sflorian * This file contains PROXY protocol structs and functions. 405c45b740Sflorian * Only v2 is supported. TLVs are not currently supported. 415c45b740Sflorian */ 425c45b740Sflorian #ifndef PROXY_PROTOCOL_H 435c45b740Sflorian #define PROXY_PROTOCOL_H 445c45b740Sflorian 45*54cc57acSflorian #include "config.h" 465c45b740Sflorian 475c45b740Sflorian /** PROXYv2 minimum header size */ 485c45b740Sflorian #define PP2_HEADER_SIZE 16 495c45b740Sflorian 505c45b740Sflorian /** PROXYv2 header signature */ 515c45b740Sflorian #define PP2_SIG "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A" 525c45b740Sflorian #define PP2_SIG_LEN 12 535c45b740Sflorian 54*54cc57acSflorian /** PROXYv2 version (protocol value) */ 555c45b740Sflorian #define PP2_VERSION 0x2 565c45b740Sflorian 575c45b740Sflorian /** 58*54cc57acSflorian * PROXYv2 command (protocol value). 595c45b740Sflorian */ 605c45b740Sflorian enum pp2_command { 615c45b740Sflorian PP2_CMD_LOCAL = 0x0, 625c45b740Sflorian PP2_CMD_PROXY = 0x1 635c45b740Sflorian }; 645c45b740Sflorian 655c45b740Sflorian /** 66*54cc57acSflorian * PROXYv2 address family (protocol value). 675c45b740Sflorian */ 685c45b740Sflorian enum pp2_af { 695c45b740Sflorian PP2_AF_UNSPEC = 0x0, 705c45b740Sflorian PP2_AF_INET = 0x1, 715c45b740Sflorian PP2_AF_INET6 = 0x2, 725c45b740Sflorian PP2_AF_UNIX = 0x3 735c45b740Sflorian }; 745c45b740Sflorian 755c45b740Sflorian /** 76*54cc57acSflorian * PROXYv2 protocol (protocol value). 775c45b740Sflorian */ 785c45b740Sflorian enum pp2_protocol { 795c45b740Sflorian PP2_PROT_UNSPEC = 0x0, 805c45b740Sflorian PP2_PROT_STREAM = 0x1, 815c45b740Sflorian PP2_PROT_DGRAM = 0x2 825c45b740Sflorian }; 835c45b740Sflorian 845c45b740Sflorian /** 85*54cc57acSflorian * Expected combinations of address family and protocol values used in checks. 86*54cc57acSflorian */ 87*54cc57acSflorian enum pp2_af_protocol_combination { 88*54cc57acSflorian PP2_UNSPEC_UNSPEC = (PP2_AF_UNSPEC<<4)|PP2_PROT_UNSPEC, 89*54cc57acSflorian PP2_INET_STREAM = (PP2_AF_INET<<4)|PP2_PROT_STREAM, 90*54cc57acSflorian PP2_INET_DGRAM = (PP2_AF_INET<<4)|PP2_PROT_DGRAM, 91*54cc57acSflorian PP2_INET6_STREAM = (PP2_AF_INET6<<4)|PP2_PROT_STREAM, 92*54cc57acSflorian PP2_INET6_DGRAM = (PP2_AF_INET6<<4)|PP2_PROT_DGRAM, 93*54cc57acSflorian PP2_UNIX_STREAM = (PP2_AF_UNIX<<4)|PP2_PROT_STREAM, 94*54cc57acSflorian PP2_UNIX_DGRAM = (PP2_AF_UNIX<<4)|PP2_PROT_DGRAM 95*54cc57acSflorian }; 96*54cc57acSflorian 97*54cc57acSflorian /** 985c45b740Sflorian * PROXYv2 header. 995c45b740Sflorian */ 1005c45b740Sflorian struct pp2_header { 1015c45b740Sflorian uint8_t sig[PP2_SIG_LEN]; 1025c45b740Sflorian uint8_t ver_cmd; 1035c45b740Sflorian uint8_t fam_prot; 1045c45b740Sflorian uint16_t len; 1055c45b740Sflorian union { 1065c45b740Sflorian struct { /* for TCP/UDP over IPv4, len = 12 */ 1075c45b740Sflorian uint32_t src_addr; 1085c45b740Sflorian uint32_t dst_addr; 1095c45b740Sflorian uint16_t src_port; 1105c45b740Sflorian uint16_t dst_port; 1115c45b740Sflorian } addr4; 1125c45b740Sflorian struct { /* for TCP/UDP over IPv6, len = 36 */ 1135c45b740Sflorian uint8_t src_addr[16]; 1145c45b740Sflorian uint8_t dst_addr[16]; 1155c45b740Sflorian uint16_t src_port; 1165c45b740Sflorian uint16_t dst_port; 1175c45b740Sflorian } addr6; 1185c45b740Sflorian struct { /* for AF_UNIX sockets, len = 216 */ 1195c45b740Sflorian uint8_t src_addr[108]; 1205c45b740Sflorian uint8_t dst_addr[108]; 1215c45b740Sflorian } addru; 1225c45b740Sflorian } addr; 1235c45b740Sflorian }; 1245c45b740Sflorian 1255c45b740Sflorian /** 126*54cc57acSflorian * PROXY parse errors. 127*54cc57acSflorian */ 128*54cc57acSflorian enum pp_parse_errors { 129*54cc57acSflorian PP_PARSE_NOERROR = 0, 130*54cc57acSflorian PP_PARSE_SIZE, 131*54cc57acSflorian PP_PARSE_WRONG_HEADERv2, 132*54cc57acSflorian PP_PARSE_UNKNOWN_CMD, 133*54cc57acSflorian PP_PARSE_UNKNOWN_FAM_PROT, 134*54cc57acSflorian }; 135*54cc57acSflorian 136*54cc57acSflorian /** 137*54cc57acSflorian * Initialize the internal proxy structure. 138*54cc57acSflorian * @param write_uint16: pointer to a function that can write uint16. 139*54cc57acSflorian * @param write_uint32: pointer to a function that can write uint32. 140*54cc57acSflorian */ 141*54cc57acSflorian void pp_init(void (*write_uint16)(void* buf, uint16_t data), 142*54cc57acSflorian void (*write_uint32)(void* buf, uint32_t data)); 143*54cc57acSflorian 144*54cc57acSflorian /** 145*54cc57acSflorian * Lookup the parsing error description. 146*54cc57acSflorian * @param error: parsing error from pp2_read_header. 147*54cc57acSflorian * @return the description. 148*54cc57acSflorian */ 149*54cc57acSflorian const char* pp_lookup_error(enum pp_parse_errors error); 150*54cc57acSflorian 151*54cc57acSflorian /** 1525c45b740Sflorian * Write a PROXYv2 header at the current position of the buffer. 153*54cc57acSflorian * @param buf: pointer to the buffer to write data to. 154*54cc57acSflorian * @param buflen: available size on the buffer. 1555c45b740Sflorian * @param src: the source address. 1565c45b740Sflorian * @param stream: if the protocol is stream or datagram. 1575c45b740Sflorian * @return 1 on success, 0 on failure. 1585c45b740Sflorian */ 159*54cc57acSflorian size_t pp2_write_to_buf(uint8_t* buf, size_t buflen, 160*54cc57acSflorian #ifdef INET6 161*54cc57acSflorian struct sockaddr_storage* src, 162*54cc57acSflorian #else 163*54cc57acSflorian struct sockaddr_in* src, 164*54cc57acSflorian #endif 1655c45b740Sflorian int stream); 1665c45b740Sflorian 1675c45b740Sflorian /** 1685c45b740Sflorian * Read a PROXYv2 header from the current position of the buffer. 1695c45b740Sflorian * It does initial validation and returns a pointer to the buffer position on 1705c45b740Sflorian * success. 171*54cc57acSflorian * @param buf: pointer to the buffer data to read from. 172*54cc57acSflorian * @param buflen: available size on the buffer. 173*54cc57acSflorian * @return parsing error, 0 on success. 1745c45b740Sflorian */ 175*54cc57acSflorian int pp2_read_header(uint8_t* buf, size_t buflen); 1765c45b740Sflorian 1775c45b740Sflorian #endif /* PROXY_PROTOCOL_H */ 178