1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/* Copyright (C) CZ.NIC, z.s.p.o. <knot-resolver@labs.nic.cz>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "daemon/session.h"
#include "daemon/network.h"
#include "daemon/proxyv2.h"
#include "lib/generic/trie.h"
const char PROXY2_SIGNATURE[12] = {
0x0D, 0x0A, 0x0D, 0x0A, 0x00, 0x0D, 0x0A, 0x51, 0x55, 0x49, 0x54, 0x0A
};
#define PROXY2_IP6_ADDR_SIZE 16
#define PROXY2_UNIX_ADDR_SIZE 108
#define TLV_TYPE_SSL 0x20
enum proxy2_family {
PROXY2_AF_UNSPEC = 0x0,
PROXY2_AF_INET = 0x1,
PROXY2_AF_INET6 = 0x2,
PROXY2_AF_UNIX = 0x3
};
enum proxy2_protocol {
PROXY2_PROTOCOL_UNSPEC = 0x0,
PROXY2_PROTOCOL_STREAM = 0x1,
PROXY2_PROTOCOL_DGRAM = 0x2
};
/** PROXYv2 protocol header section */
struct proxy2_header {
uint8_t signature[sizeof(PROXY2_SIGNATURE)];
uint8_t version_command;
uint8_t family_protocol;
uint16_t length; /**< Length of the address section */
};
/** PROXYv2 additional information in Type-Length-Value (TLV) format. */
struct proxy2_tlv {
uint8_t type;
uint8_t length_hi;
uint8_t length_lo;
uint8_t value[];
};
/** PROXYv2 protocol address section */
union proxy2_address {
struct {
uint32_t src_addr;
uint32_t dst_addr;
uint16_t src_port;
uint16_t dst_port;
} ipv4_addr;
struct {
uint8_t src_addr[PROXY2_IP6_ADDR_SIZE];
uint8_t dst_addr[PROXY2_IP6_ADDR_SIZE];
uint16_t src_port;
uint16_t dst_port;
} ipv6_addr;
struct {
uint8_t src_addr[PROXY2_UNIX_ADDR_SIZE];
uint8_t dst_addr[PROXY2_UNIX_ADDR_SIZE];
} unix_addr;
};
/** Gets protocol version from the specified PROXYv2 header. */
static inline unsigned char proxy2_header_version(const struct proxy2_header* h)
{
return (h->version_command & 0xF0) >> 4;
}
/** Gets command from the specified PROXYv2 header. */
static inline enum proxy2_command proxy2_header_command(const struct proxy2_header *h)
{
return h->version_command & 0x0F;
}
/** Gets address family from the specified PROXYv2 header. */
static inline enum proxy2_family proxy2_header_family(const struct proxy2_header *h)
{
return (h->family_protocol & 0xF0) >> 4;
}
/** Gets transport protocol from the specified PROXYv2 header. */
static inline enum proxy2_family proxy2_header_protocol(const struct proxy2_header *h)
{
return h->family_protocol & 0x0F;
}
static inline union proxy2_address *proxy2_get_address(const struct proxy2_header *h)
{
return (union proxy2_address *)((uint8_t *)h + sizeof(struct proxy2_header));
}
static inline struct proxy2_tlv *get_tlvs(const struct proxy2_header *h, size_t addr_len)
{
return (struct proxy2_tlv *)((uint8_t *)proxy2_get_address(h) + addr_len);
}
/** Gets the length of the TLV's `value` attribute. */
static inline uint16_t proxy2_tlv_length(const struct proxy2_tlv *tlv)
{
return ((uint16_t) tlv->length_hi << 16) | tlv->length_lo;
}
static inline bool has_tlv(const struct proxy2_header *h,
const struct proxy2_tlv *tlv)
{
uint64_t addr_length = ntohs(h->length);
ptrdiff_t hdr_len = sizeof(struct proxy2_header) + addr_length;
uint8_t *tlv_hdr_end = (uint8_t *)tlv + sizeof(struct proxy2_tlv);
ptrdiff_t distance = tlv_hdr_end - (uint8_t *)h;
if (hdr_len < distance)
return false;
uint8_t *tlv_end = tlv_hdr_end + proxy2_tlv_length(tlv);
distance = tlv_end - (uint8_t *)h;
return hdr_len >= distance;
}
static inline void next_tlv(struct proxy2_tlv **tlv)
{
uint8_t *next = ((uint8_t *)*tlv + sizeof(struct proxy2_tlv) + proxy2_tlv_length(*tlv));
*tlv = (struct proxy2_tlv *)next;
}
bool proxy_allowed(const struct network *net, const struct sockaddr *saddr)
{
union kr_in_addr addr;
trie_t *trie;
size_t addr_size;
switch (saddr->sa_family) {
case AF_INET:
if (net->proxy_all4)
return true;
trie = net->proxy_addrs4;
addr_size = sizeof(addr.ip4);
addr.ip4 = ((struct sockaddr_in *)saddr)->sin_addr;
break;
case AF_INET6:
if (net->proxy_all6)
return true;
trie = net->proxy_addrs6;
addr_size = sizeof(addr.ip6);
addr.ip6 = ((struct sockaddr_in6 *)saddr)->sin6_addr;
break;
default:
kr_assert(false); // Only IPv4 and IPv6 proxy addresses supported
return false;
}
trie_val_t *val;
int ret = trie_get_leq(trie, (char *)&addr, addr_size, &val);
if (ret != kr_ok() && ret != 1)
return false;
kr_assert(val);
const struct net_proxy_data *found = *val;
kr_assert(found);
return kr_bitcmp((char *)&addr, (char *)&found->addr, found->netmask) == 0;
}
ssize_t proxy_process_header(struct proxy_result *out, struct session *s,
const void *buf, const ssize_t nread)
{
if (!buf)
return kr_error(EINVAL);
const struct proxy2_header *hdr = (struct proxy2_header *)buf;
uint64_t content_length = ntohs(hdr->length);
ssize_t hdr_len = sizeof(struct proxy2_header) + content_length;
/* PROXYv2 requires the header to be received all at once */
if (nread < hdr_len) {
return kr_error(KNOT_EMALF);
}
unsigned char version = proxy2_header_version(hdr);
if (version != 2) {
/* Version MUST be 2 for PROXYv2 protocol */
return kr_error(KNOT_EMALF);
}
enum proxy2_command command = proxy2_header_command(hdr);
if (command == PROXY2_CMD_LOCAL) {
/* Addresses for LOCAL are to be discarded */
*out = (struct proxy_result){ .command = PROXY2_CMD_LOCAL };
goto fill_wirebuf;
}
if (command != PROXY2_CMD_PROXY) {
/* PROXYv2 prohibits values other than LOCAL and PROXY */
return kr_error(KNOT_EMALF);
}
*out = (struct proxy_result){ .command = PROXY2_CMD_PROXY };
/* Parse flags */
enum proxy2_family family = proxy2_header_family(hdr);
switch(family) {
case PROXY2_AF_UNSPEC:
case PROXY2_AF_UNIX:
/* UNIX is unsupported, fall back to UNSPEC */
out->family = AF_UNSPEC;
break;
case PROXY2_AF_INET:
out->family = AF_INET;
break;
case PROXY2_AF_INET6:
out->family = AF_INET6;
break;
default:
/* PROXYv2 prohibits other values */
return kr_error(KNOT_EMALF);
}
enum proxy2_family protocol = proxy2_header_protocol(hdr);
switch (protocol) {
case PROXY2_PROTOCOL_DGRAM:
out->protocol = SOCK_DGRAM;
break;
case PROXY2_PROTOCOL_STREAM:
out->protocol = SOCK_STREAM;
break;
default:
/* PROXYv2 prohibits other values */
return kr_error(KNOT_EMALF);
}
/* Parse addresses */
union proxy2_address* addr = proxy2_get_address(hdr);
size_t addr_length = 0;
switch(out->family) {
case AF_INET:
addr_length = sizeof(addr->ipv4_addr);
if (content_length < addr_length)
return kr_error(KNOT_EMALF);
out->src_addr.ip4 = (struct sockaddr_in){
.sin_family = AF_INET,
.sin_addr = { .s_addr = addr->ipv4_addr.src_addr },
.sin_port = addr->ipv4_addr.src_port,
};
out->dst_addr.ip4 = (struct sockaddr_in){
.sin_family = AF_INET,
.sin_addr = { .s_addr = addr->ipv4_addr.dst_addr },
.sin_port = addr->ipv4_addr.dst_port,
};
break;
case AF_INET6:
addr_length = sizeof(addr->ipv6_addr);
if (content_length < addr_length)
return kr_error(KNOT_EMALF);
out->src_addr.ip6 = (struct sockaddr_in6){
.sin6_family = AF_INET6,
.sin6_port = addr->ipv6_addr.src_port
};
memcpy(
&out->src_addr.ip6.sin6_addr.s6_addr,
&addr->ipv6_addr.src_addr,
sizeof(out->src_addr.ip6.sin6_addr.s6_addr));
out->dst_addr.ip6 = (struct sockaddr_in6){
.sin6_family = AF_INET6,
.sin6_port = addr->ipv6_addr.dst_port
};
memcpy(
&out->dst_addr.ip6.sin6_addr.s6_addr,
&addr->ipv6_addr.dst_addr,
sizeof(out->dst_addr.ip6.sin6_addr.s6_addr));
break;
}
/* Process additional information */
for (struct proxy2_tlv *tlv = get_tlvs(hdr, addr_length); has_tlv(hdr, tlv); next_tlv(&tlv)) {
switch (tlv->type) {
case TLV_TYPE_SSL:
out->has_tls = true;
break;
/* TODO: add more TLV types if needed */
}
}
fill_wirebuf:
return session_wirebuf_trim(s, hdr_len);
}
|