xref: /openbsd-src/usr.sbin/nsd/util/proxy_protocol.c (revision b71395ea3d4830c6fd338870b804059761b8292d)
1*b71395eaSflorian /*
2*b71395eaSflorian  * util/proxy_protocol.c - event notification
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 functions.
40*b71395eaSflorian  */
41*b71395eaSflorian #include "util/proxy_protocol.h"
42*b71395eaSflorian 
43*b71395eaSflorian /**
44*b71395eaSflorian  * Internal struct initialized with function pointers for writing uint16 and
45*b71395eaSflorian  * uint32.
46*b71395eaSflorian  */
47*b71395eaSflorian struct proxy_protocol_data {
48*b71395eaSflorian 	void (*write_uint16)(void* buf, uint16_t data);
49*b71395eaSflorian 	void (*write_uint32)(void* buf, uint32_t data);
50*b71395eaSflorian };
51*b71395eaSflorian struct proxy_protocol_data pp_data;
52*b71395eaSflorian 
53*b71395eaSflorian /**
54*b71395eaSflorian  * Internal lookup table; could be further generic like sldns_lookup_table
55*b71395eaSflorian  * for all the future generic stuff.
56*b71395eaSflorian  */
57*b71395eaSflorian struct proxy_protocol_lookup_table {
58*b71395eaSflorian 	int id;
59*b71395eaSflorian 	const char *text;
60*b71395eaSflorian };
61*b71395eaSflorian 
62*b71395eaSflorian /**
63*b71395eaSflorian  * Internal parsing error text; could be exposed with pp_lookup_error.
64*b71395eaSflorian  */
65*b71395eaSflorian static struct proxy_protocol_lookup_table pp_parse_errors_data[] = {
66*b71395eaSflorian 	{ PP_PARSE_NOERROR, "no parse error" },
67*b71395eaSflorian 	{ PP_PARSE_SIZE, "not enough space for header" },
68*b71395eaSflorian 	{ PP_PARSE_WRONG_HEADERv2, "could not match PROXYv2 header" },
69*b71395eaSflorian 	{ PP_PARSE_UNKNOWN_CMD, "unknown command" },
70*b71395eaSflorian 	{ PP_PARSE_UNKNOWN_FAM_PROT, "unknown family and protocol" },
71*b71395eaSflorian };
72*b71395eaSflorian 
73*b71395eaSflorian void
pp_init(void (* write_uint16)(void * buf,uint16_t data),void (* write_uint32)(void * buf,uint32_t data))74*b71395eaSflorian pp_init(void (*write_uint16)(void* buf, uint16_t data),
75*b71395eaSflorian 	void (*write_uint32)(void* buf, uint32_t data)) {
76*b71395eaSflorian 	pp_data.write_uint16 = write_uint16;
77*b71395eaSflorian 	pp_data.write_uint32 = write_uint32;
78*b71395eaSflorian }
79*b71395eaSflorian 
80*b71395eaSflorian const char*
pp_lookup_error(enum pp_parse_errors error)81*b71395eaSflorian pp_lookup_error(enum pp_parse_errors error) {
82*b71395eaSflorian 	return pp_parse_errors_data[error].text;
83*b71395eaSflorian }
84*b71395eaSflorian 
85*b71395eaSflorian size_t
pp2_write_to_buf(uint8_t * buf,size_t buflen,struct sockaddr_storage * src,int stream)86*b71395eaSflorian pp2_write_to_buf(uint8_t* buf, size_t buflen,
87*b71395eaSflorian #ifdef INET6
88*b71395eaSflorian 	struct sockaddr_storage* src,
89*b71395eaSflorian #else
90*b71395eaSflorian 	struct sockaddr_in* src,
91*b71395eaSflorian #endif
92*b71395eaSflorian 	int stream)
93*b71395eaSflorian {
94*b71395eaSflorian 	int af;
95*b71395eaSflorian 	size_t expected_size;
96*b71395eaSflorian 	if(!src) return 0;
97*b71395eaSflorian 	af = (int)((struct sockaddr_in*)src)->sin_family;
98*b71395eaSflorian 	expected_size = PP2_HEADER_SIZE + (af==AF_INET?12:36);
99*b71395eaSflorian 	if(buflen < expected_size) {
100*b71395eaSflorian 		return 0;
101*b71395eaSflorian 	}
102*b71395eaSflorian 	/* sig */
103*b71395eaSflorian 	memcpy(buf, PP2_SIG, PP2_SIG_LEN);
104*b71395eaSflorian 	buf += PP2_SIG_LEN;
105*b71395eaSflorian 	/* version and command */
106*b71395eaSflorian 	*buf = (PP2_VERSION << 4) | PP2_CMD_PROXY;
107*b71395eaSflorian 	buf++;
108*b71395eaSflorian 	switch(af) {
109*b71395eaSflorian 	case AF_INET:
110*b71395eaSflorian 		/* family and protocol */
111*b71395eaSflorian 		*buf = (PP2_AF_INET<<4) |
112*b71395eaSflorian 			(stream?PP2_PROT_STREAM:PP2_PROT_DGRAM);
113*b71395eaSflorian 		buf++;
114*b71395eaSflorian 		/* length */
115*b71395eaSflorian 		(*pp_data.write_uint16)(buf, 12);
116*b71395eaSflorian 		buf += 2;
117*b71395eaSflorian 		/* src addr */
118*b71395eaSflorian 		memcpy(buf,
119*b71395eaSflorian 			&((struct sockaddr_in*)src)->sin_addr.s_addr, 4);
120*b71395eaSflorian 		buf += 4;
121*b71395eaSflorian 		/* dst addr */
122*b71395eaSflorian 		(*pp_data.write_uint32)(buf, 0);
123*b71395eaSflorian 		buf += 4;
124*b71395eaSflorian 		/* src port */
125*b71395eaSflorian 		memcpy(buf,
126*b71395eaSflorian 			&((struct sockaddr_in*)src)->sin_port, 2);
127*b71395eaSflorian 		buf += 2;
128*b71395eaSflorian 		/* dst addr */
129*b71395eaSflorian 		/* dst port */
130*b71395eaSflorian 		(*pp_data.write_uint16)(buf, 12);
131*b71395eaSflorian 		break;
132*b71395eaSflorian #ifdef INET6
133*b71395eaSflorian 	case AF_INET6:
134*b71395eaSflorian 		/* family and protocol */
135*b71395eaSflorian 		*buf = (PP2_AF_INET6<<4) |
136*b71395eaSflorian 			(stream?PP2_PROT_STREAM:PP2_PROT_DGRAM);
137*b71395eaSflorian 		buf++;
138*b71395eaSflorian 		/* length */
139*b71395eaSflorian 		(*pp_data.write_uint16)(buf, 36);
140*b71395eaSflorian 		buf += 2;
141*b71395eaSflorian 		/* src addr */
142*b71395eaSflorian 		memcpy(buf,
143*b71395eaSflorian 			&((struct sockaddr_in6*)src)->sin6_addr, 16);
144*b71395eaSflorian 		buf += 16;
145*b71395eaSflorian 		/* dst addr */
146*b71395eaSflorian 		memset(buf, 0, 16);
147*b71395eaSflorian 		buf += 16;
148*b71395eaSflorian 		/* src port */
149*b71395eaSflorian 		memcpy(buf, &((struct sockaddr_in6*)src)->sin6_port, 2);
150*b71395eaSflorian 		buf += 2;
151*b71395eaSflorian 		/* dst port */
152*b71395eaSflorian 		(*pp_data.write_uint16)(buf, 0);
153*b71395eaSflorian 		break;
154*b71395eaSflorian #endif /* INET6 */
155*b71395eaSflorian 	case AF_UNIX:
156*b71395eaSflorian 		/* fallthrough */
157*b71395eaSflorian 	default:
158*b71395eaSflorian 		return 0;
159*b71395eaSflorian 	}
160*b71395eaSflorian 	return expected_size;
161*b71395eaSflorian }
162*b71395eaSflorian 
163*b71395eaSflorian int
pp2_read_header(uint8_t * buf,size_t buflen)164*b71395eaSflorian pp2_read_header(uint8_t* buf, size_t buflen)
165*b71395eaSflorian {
166*b71395eaSflorian 	size_t size;
167*b71395eaSflorian 	struct pp2_header* header = (struct pp2_header*)buf;
168*b71395eaSflorian 	/* Try to fail all the unsupported cases first. */
169*b71395eaSflorian 	if(buflen < PP2_HEADER_SIZE) {
170*b71395eaSflorian 		return PP_PARSE_SIZE;
171*b71395eaSflorian 	}
172*b71395eaSflorian 	/* Check for PROXYv2 header */
173*b71395eaSflorian 	if(memcmp(header, PP2_SIG, PP2_SIG_LEN) != 0 ||
174*b71395eaSflorian 		((header->ver_cmd & 0xF0)>>4) != PP2_VERSION) {
175*b71395eaSflorian 		return PP_PARSE_WRONG_HEADERv2;
176*b71395eaSflorian 	}
177*b71395eaSflorian 	/* Check the length */
178*b71395eaSflorian 	size = PP2_HEADER_SIZE + ntohs(header->len);
179*b71395eaSflorian 	if(buflen < size) {
180*b71395eaSflorian 		return PP_PARSE_SIZE;
181*b71395eaSflorian 	}
182*b71395eaSflorian 	/* Check for supported commands */
183*b71395eaSflorian 	if((header->ver_cmd & 0xF) != PP2_CMD_LOCAL &&
184*b71395eaSflorian 		(header->ver_cmd & 0xF) != PP2_CMD_PROXY) {
185*b71395eaSflorian 		return PP_PARSE_UNKNOWN_CMD;
186*b71395eaSflorian 	}
187*b71395eaSflorian 	/* Check for supported family and protocol */
188*b71395eaSflorian 	if(header->fam_prot != PP2_UNSPEC_UNSPEC &&
189*b71395eaSflorian 		header->fam_prot != PP2_INET_STREAM &&
190*b71395eaSflorian 		header->fam_prot != PP2_INET_DGRAM &&
191*b71395eaSflorian 		header->fam_prot != PP2_INET6_STREAM &&
192*b71395eaSflorian 		header->fam_prot != PP2_INET6_DGRAM &&
193*b71395eaSflorian 		header->fam_prot != PP2_UNIX_STREAM &&
194*b71395eaSflorian 		header->fam_prot != PP2_UNIX_DGRAM) {
195*b71395eaSflorian 		return PP_PARSE_UNKNOWN_FAM_PROT;
196*b71395eaSflorian 	}
197*b71395eaSflorian 	/* We have a correct header */
198*b71395eaSflorian 	return PP_PARSE_NOERROR;
199*b71395eaSflorian }
200