xref: /netbsd-src/external/bsd/unbound/dist/util/proxy_protocol.c (revision 91f7d55fb697b5e0475da4718fa34c3a3ebeac85)
1*91f7d55fSchristos /*
2*91f7d55fSchristos  * util/proxy_protocol.c - event notification
3*91f7d55fSchristos  *
4*91f7d55fSchristos  * Copyright (c) 2022, NLnet Labs. All rights reserved.
5*91f7d55fSchristos  *
6*91f7d55fSchristos  * This software is open source.
7*91f7d55fSchristos  *
8*91f7d55fSchristos  * Redistribution and use in source and binary forms, with or without
9*91f7d55fSchristos  * modification, are permitted provided that the following conditions
10*91f7d55fSchristos  * are met:
11*91f7d55fSchristos  *
12*91f7d55fSchristos  * Redistributions of source code must retain the above copyright notice,
13*91f7d55fSchristos  * this list of conditions and the following disclaimer.
14*91f7d55fSchristos  *
15*91f7d55fSchristos  * Redistributions in binary form must reproduce the above copyright notice,
16*91f7d55fSchristos  * this list of conditions and the following disclaimer in the documentation
17*91f7d55fSchristos  * and/or other materials provided with the distribution.
18*91f7d55fSchristos  *
19*91f7d55fSchristos  * Neither the name of the NLNET LABS nor the names of its contributors may
20*91f7d55fSchristos  * be used to endorse or promote products derived from this software without
21*91f7d55fSchristos  * specific prior written permission.
22*91f7d55fSchristos  *
23*91f7d55fSchristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*91f7d55fSchristos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*91f7d55fSchristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*91f7d55fSchristos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*91f7d55fSchristos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*91f7d55fSchristos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*91f7d55fSchristos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*91f7d55fSchristos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*91f7d55fSchristos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*91f7d55fSchristos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*91f7d55fSchristos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*91f7d55fSchristos  */
35*91f7d55fSchristos 
36*91f7d55fSchristos /**
37*91f7d55fSchristos  * \file
38*91f7d55fSchristos  *
39*91f7d55fSchristos  * This file contains PROXY protocol functions.
40*91f7d55fSchristos  */
41*91f7d55fSchristos #include "util/proxy_protocol.h"
42*91f7d55fSchristos 
43*91f7d55fSchristos /**
44*91f7d55fSchristos  * Internal struct initialized with function pointers for writing uint16 and
45*91f7d55fSchristos  * uint32.
46*91f7d55fSchristos  */
47*91f7d55fSchristos struct proxy_protocol_data {
48*91f7d55fSchristos 	void (*write_uint16)(void* buf, uint16_t data);
49*91f7d55fSchristos 	void (*write_uint32)(void* buf, uint32_t data);
50*91f7d55fSchristos };
51*91f7d55fSchristos struct proxy_protocol_data pp_data;
52*91f7d55fSchristos 
53*91f7d55fSchristos /**
54*91f7d55fSchristos  * Internal lookup table; could be further generic like sldns_lookup_table
55*91f7d55fSchristos  * for all the future generic stuff.
56*91f7d55fSchristos  */
57*91f7d55fSchristos struct proxy_protocol_lookup_table {
58*91f7d55fSchristos 	int id;
59*91f7d55fSchristos 	const char *text;
60*91f7d55fSchristos };
61*91f7d55fSchristos 
62*91f7d55fSchristos /**
63*91f7d55fSchristos  * Internal parsing error text; could be exposed with pp_lookup_error.
64*91f7d55fSchristos  */
65*91f7d55fSchristos static struct proxy_protocol_lookup_table pp_parse_errors_data[] = {
66*91f7d55fSchristos 	{ PP_PARSE_NOERROR, "no parse error" },
67*91f7d55fSchristos 	{ PP_PARSE_SIZE, "not enough space for header" },
68*91f7d55fSchristos 	{ PP_PARSE_WRONG_HEADERv2, "could not match PROXYv2 header" },
69*91f7d55fSchristos 	{ PP_PARSE_UNKNOWN_CMD, "unknown command" },
70*91f7d55fSchristos 	{ PP_PARSE_UNKNOWN_FAM_PROT, "unknown family and protocol" },
71*91f7d55fSchristos };
72*91f7d55fSchristos 
73*91f7d55fSchristos void
pp_init(void (* write_uint16)(void * buf,uint16_t data),void (* write_uint32)(void * buf,uint32_t data))74*91f7d55fSchristos pp_init(void (*write_uint16)(void* buf, uint16_t data),
75*91f7d55fSchristos 	void (*write_uint32)(void* buf, uint32_t data)) {
76*91f7d55fSchristos 	pp_data.write_uint16 = write_uint16;
77*91f7d55fSchristos 	pp_data.write_uint32 = write_uint32;
78*91f7d55fSchristos }
79*91f7d55fSchristos 
80*91f7d55fSchristos const char*
pp_lookup_error(enum pp_parse_errors error)81*91f7d55fSchristos pp_lookup_error(enum pp_parse_errors error) {
82*91f7d55fSchristos 	return pp_parse_errors_data[error].text;
83*91f7d55fSchristos }
84*91f7d55fSchristos 
85*91f7d55fSchristos size_t
pp2_write_to_buf(uint8_t * buf,size_t buflen,struct sockaddr_storage * src,int stream)86*91f7d55fSchristos pp2_write_to_buf(uint8_t* buf, size_t buflen,
87*91f7d55fSchristos #ifdef INET6
88*91f7d55fSchristos 	struct sockaddr_storage* src,
89*91f7d55fSchristos #else
90*91f7d55fSchristos 	struct sockaddr_in* src,
91*91f7d55fSchristos #endif
92*91f7d55fSchristos 	int stream)
93*91f7d55fSchristos {
94*91f7d55fSchristos 	int af;
95*91f7d55fSchristos 	size_t expected_size;
96*91f7d55fSchristos 	if(!src) return 0;
97*91f7d55fSchristos 	af = (int)((struct sockaddr_in*)src)->sin_family;
98*91f7d55fSchristos 	expected_size = PP2_HEADER_SIZE + (af==AF_INET?12:36);
99*91f7d55fSchristos 	if(buflen < expected_size) {
100*91f7d55fSchristos 		return 0;
101*91f7d55fSchristos 	}
102*91f7d55fSchristos 	/* sig */
103*91f7d55fSchristos 	memcpy(buf, PP2_SIG, PP2_SIG_LEN);
104*91f7d55fSchristos 	buf += PP2_SIG_LEN;
105*91f7d55fSchristos 	/* version and command */
106*91f7d55fSchristos 	*buf = (PP2_VERSION << 4) | PP2_CMD_PROXY;
107*91f7d55fSchristos 	buf++;
108*91f7d55fSchristos 	switch(af) {
109*91f7d55fSchristos 	case AF_INET:
110*91f7d55fSchristos 		/* family and protocol */
111*91f7d55fSchristos 		*buf = (PP2_AF_INET<<4) |
112*91f7d55fSchristos 			(stream?PP2_PROT_STREAM:PP2_PROT_DGRAM);
113*91f7d55fSchristos 		buf++;
114*91f7d55fSchristos 		/* length */
115*91f7d55fSchristos 		(*pp_data.write_uint16)(buf, 12);
116*91f7d55fSchristos 		buf += 2;
117*91f7d55fSchristos 		/* src addr */
118*91f7d55fSchristos 		memcpy(buf,
119*91f7d55fSchristos 			&((struct sockaddr_in*)src)->sin_addr.s_addr, 4);
120*91f7d55fSchristos 		buf += 4;
121*91f7d55fSchristos 		/* dst addr */
122*91f7d55fSchristos 		(*pp_data.write_uint32)(buf, 0);
123*91f7d55fSchristos 		buf += 4;
124*91f7d55fSchristos 		/* src port */
125*91f7d55fSchristos 		memcpy(buf,
126*91f7d55fSchristos 			&((struct sockaddr_in*)src)->sin_port, 2);
127*91f7d55fSchristos 		buf += 2;
128*91f7d55fSchristos 		/* dst addr */
129*91f7d55fSchristos 		/* dst port */
130*91f7d55fSchristos 		(*pp_data.write_uint16)(buf, 12);
131*91f7d55fSchristos 		break;
132*91f7d55fSchristos #ifdef INET6
133*91f7d55fSchristos 	case AF_INET6:
134*91f7d55fSchristos 		/* family and protocol */
135*91f7d55fSchristos 		*buf = (PP2_AF_INET6<<4) |
136*91f7d55fSchristos 			(stream?PP2_PROT_STREAM:PP2_PROT_DGRAM);
137*91f7d55fSchristos 		buf++;
138*91f7d55fSchristos 		/* length */
139*91f7d55fSchristos 		(*pp_data.write_uint16)(buf, 36);
140*91f7d55fSchristos 		buf += 2;
141*91f7d55fSchristos 		/* src addr */
142*91f7d55fSchristos 		memcpy(buf,
143*91f7d55fSchristos 			&((struct sockaddr_in6*)src)->sin6_addr, 16);
144*91f7d55fSchristos 		buf += 16;
145*91f7d55fSchristos 		/* dst addr */
146*91f7d55fSchristos 		memset(buf, 0, 16);
147*91f7d55fSchristos 		buf += 16;
148*91f7d55fSchristos 		/* src port */
149*91f7d55fSchristos 		memcpy(buf, &((struct sockaddr_in6*)src)->sin6_port, 2);
150*91f7d55fSchristos 		buf += 2;
151*91f7d55fSchristos 		/* dst port */
152*91f7d55fSchristos 		(*pp_data.write_uint16)(buf, 0);
153*91f7d55fSchristos 		break;
154*91f7d55fSchristos #endif /* INET6 */
155*91f7d55fSchristos 	case AF_UNIX:
156*91f7d55fSchristos 		/* fallthrough */
157*91f7d55fSchristos 	default:
158*91f7d55fSchristos 		return 0;
159*91f7d55fSchristos 	}
160*91f7d55fSchristos 	return expected_size;
161*91f7d55fSchristos }
162*91f7d55fSchristos 
163*91f7d55fSchristos int
pp2_read_header(uint8_t * buf,size_t buflen)164*91f7d55fSchristos pp2_read_header(uint8_t* buf, size_t buflen)
165*91f7d55fSchristos {
166*91f7d55fSchristos 	size_t size;
167*91f7d55fSchristos 	struct pp2_header* header = (struct pp2_header*)buf;
168*91f7d55fSchristos 	/* Try to fail all the unsupported cases first. */
169*91f7d55fSchristos 	if(buflen < PP2_HEADER_SIZE) {
170*91f7d55fSchristos 		return PP_PARSE_SIZE;
171*91f7d55fSchristos 	}
172*91f7d55fSchristos 	/* Check for PROXYv2 header */
173*91f7d55fSchristos 	if(memcmp(header, PP2_SIG, PP2_SIG_LEN) != 0 ||
174*91f7d55fSchristos 		((header->ver_cmd & 0xF0)>>4) != PP2_VERSION) {
175*91f7d55fSchristos 		return PP_PARSE_WRONG_HEADERv2;
176*91f7d55fSchristos 	}
177*91f7d55fSchristos 	/* Check the length */
178*91f7d55fSchristos 	size = PP2_HEADER_SIZE + ntohs(header->len);
179*91f7d55fSchristos 	if(buflen < size) {
180*91f7d55fSchristos 		return PP_PARSE_SIZE;
181*91f7d55fSchristos 	}
182*91f7d55fSchristos 	/* Check for supported commands */
183*91f7d55fSchristos 	if((header->ver_cmd & 0xF) != PP2_CMD_LOCAL &&
184*91f7d55fSchristos 		(header->ver_cmd & 0xF) != PP2_CMD_PROXY) {
185*91f7d55fSchristos 		return PP_PARSE_UNKNOWN_CMD;
186*91f7d55fSchristos 	}
187*91f7d55fSchristos 	/* Check for supported family and protocol */
188*91f7d55fSchristos 	if(header->fam_prot != PP2_UNSPEC_UNSPEC &&
189*91f7d55fSchristos 		header->fam_prot != PP2_INET_STREAM &&
190*91f7d55fSchristos 		header->fam_prot != PP2_INET_DGRAM &&
191*91f7d55fSchristos 		header->fam_prot != PP2_INET6_STREAM &&
192*91f7d55fSchristos 		header->fam_prot != PP2_INET6_DGRAM &&
193*91f7d55fSchristos 		header->fam_prot != PP2_UNIX_STREAM &&
194*91f7d55fSchristos 		header->fam_prot != PP2_UNIX_DGRAM) {
195*91f7d55fSchristos 		return PP_PARSE_UNKNOWN_FAM_PROT;
196*91f7d55fSchristos 	}
197*91f7d55fSchristos 	/* We have a correct header */
198*91f7d55fSchristos 	return PP_PARSE_NOERROR;
199*91f7d55fSchristos }
200