| | varnish-cache/bin/varnishtest/vtc_proxy.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2015 Varnish Software AS |
2 |
|
* All rights reserved. |
3 |
|
* |
4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
5 |
|
* |
6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
7 |
|
* |
8 |
|
* Redistribution and use in source and binary forms, with or without |
9 |
|
* modification, are permitted provided that the following conditions |
10 |
|
* are met: |
11 |
|
* 1. Redistributions of source code must retain the above copyright |
12 |
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
|
* notice, this list of conditions and the following disclaimer in the |
15 |
|
* documentation and/or other materials provided with the distribution. |
16 |
|
* |
17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 |
|
* SUCH DAMAGE. |
28 |
|
*/ |
29 |
|
|
30 |
|
#include "config.h" |
31 |
|
|
32 |
|
#include <stdlib.h> |
33 |
|
#include <string.h> |
34 |
|
#include <sys/socket.h> |
35 |
|
|
36 |
|
#include <netinet/in.h> |
37 |
|
|
38 |
|
#include <unistd.h> |
39 |
|
|
40 |
|
#include "vtc.h" |
41 |
|
|
42 |
|
#include "vend.h" |
43 |
|
#include "vsa.h" |
44 |
|
#include "vtcp.h" |
45 |
|
|
46 |
|
static const char vpx1_sig[] = {'P', 'R', 'O', 'X', 'Y'}; |
47 |
|
static const char vpx2_sig[] = { |
48 |
|
'\r', '\n', '\r', '\n', '\0', '\r', '\n', |
49 |
|
'Q', 'U', 'I', 'T', '\n', |
50 |
|
}; |
51 |
|
|
52 |
|
//lint -esym(750, PP2_*) |
53 |
|
#define PP2_TYPE_ALPN 0x01 |
54 |
|
#define PP2_TYPE_AUTHORITY 0x02 |
55 |
|
#define PP2_TYPE_CRC32C 0x03 |
56 |
|
#define PP2_TYPE_NOOP 0x04 |
57 |
|
#define PP2_TYPE_UNIQUE_ID 0x05 |
58 |
|
#define PP2_TYPE_SSL 0x20 |
59 |
|
#define PP2_SUBTYPE_SSL_VERSION 0x21 |
60 |
|
#define PP2_SUBTYPE_SSL_CN 0x22 |
61 |
|
#define PP2_SUBTYPE_SSL_CIPHER 0x23 |
62 |
|
#define PP2_SUBTYPE_SSL_SIG_ALG 0x24 |
63 |
|
#define PP2_SUBTYPE_SSL_KEY_ALG 0x25 |
64 |
|
#define PP2_SUBTYPE_SSL_MAX 0x25 |
65 |
|
#define PP2_TYPE_NETNS 0x30 |
66 |
|
|
67 |
|
struct pp2_type { |
68 |
|
const char * name; |
69 |
|
uint8_t type; |
70 |
|
}; |
71 |
|
|
72 |
|
/* sorted ! */ |
73 |
|
static const struct pp2_type pp2_types[] = { |
74 |
|
{"alpn", PP2_TYPE_ALPN}, |
75 |
|
{"authority", PP2_TYPE_AUTHORITY}, |
76 |
|
{"crc32c", PP2_TYPE_CRC32C}, |
77 |
|
{"netns", PP2_TYPE_NETNS}, |
78 |
|
{"noop", PP2_TYPE_NOOP}, |
79 |
|
{"unique_id", PP2_TYPE_UNIQUE_ID} |
80 |
|
}; |
81 |
|
|
82 |
|
static int |
83 |
6 |
pp2cmp(const void *va, const void *vb) |
84 |
|
{ |
85 |
6 |
const struct pp2_type *a = va; |
86 |
6 |
const struct pp2_type *b = vb; |
87 |
6 |
return (strcmp(a->name, b->name)); |
88 |
|
} |
89 |
|
|
90 |
|
void |
91 |
2 |
vtc_proxy_tlv(struct vtclog *vl, struct vsb *vsb, const char *kva) |
92 |
|
{ |
93 |
|
struct pp2_type *pp2, needle; |
94 |
2 |
char *save = NULL, *kv; |
95 |
|
struct vsb *vsb2; |
96 |
|
const char *p; |
97 |
|
uint16_t le; |
98 |
|
ssize_t sz; |
99 |
|
|
100 |
2 |
kv = strdup(kva); |
101 |
2 |
AN(kv); |
102 |
|
|
103 |
2 |
p = strtok_r(kv, "=", &save); |
104 |
2 |
AN(p); |
105 |
2 |
if (p[0] == '0' && p[1] == 'x') { |
106 |
0 |
p += 2; |
107 |
0 |
vsb2 = vtc_hex_to_bin(vl, p); |
108 |
0 |
AN(vsb2); |
109 |
0 |
if (VSB_len(vsb2) != 1) |
110 |
0 |
vtc_fatal(vl, "tlv hex type has wrong length"); |
111 |
0 |
VSB_bcat(vsb, VSB_data(vsb2), 1); |
112 |
0 |
VSB_destroy(&vsb2); |
113 |
0 |
} |
114 |
|
else { |
115 |
2 |
needle = (typeof(needle)){p, 0}; |
116 |
2 |
pp2 = bsearch(&needle, pp2_types, sizeof pp2_types / sizeof pp2_types[0], |
117 |
|
sizeof pp2_types[0], pp2cmp); |
118 |
2 |
if (pp2 == NULL) |
119 |
0 |
vtc_fatal(vl, "tlv type %s not found", p); |
120 |
2 |
VSB_putc(vsb, pp2->type); |
121 |
|
} |
122 |
|
|
123 |
2 |
p = strtok_r(NULL, "", &save); |
124 |
2 |
if (p == NULL) |
125 |
0 |
vtc_fatal(vl, "tlv value missing"); |
126 |
2 |
if (p[0] == '0' && p[1] == 'x') |
127 |
0 |
vsb2 = vtc_hex_to_bin(vl, p + 2); |
128 |
|
else { |
129 |
2 |
vsb2 = VSB_new_auto(); |
130 |
2 |
AN(vsb2); |
131 |
2 |
VSB_cat(vsb2, p); |
132 |
2 |
AZ(VSB_finish(vsb2)); |
133 |
|
} |
134 |
2 |
AN(vsb2); |
135 |
2 |
free(kv); |
136 |
|
|
137 |
2 |
sz = VSB_len(vsb2); |
138 |
2 |
assert(sz >= 0); |
139 |
2 |
assert(sz <= UINT16_MAX); |
140 |
|
|
141 |
2 |
vbe16enc(&le, (uint16_t)sz); |
142 |
2 |
assert(sizeof(le) == 2); |
143 |
2 |
VSB_bcat(vsb, &le, 2); |
144 |
2 |
VSB_bcat(vsb, VSB_data(vsb2), sz); |
145 |
2 |
VSB_destroy(&vsb2); |
146 |
2 |
} |
147 |
|
|
148 |
|
static void |
149 |
8 |
vpx_enc_addr(struct vsb *vsb, int proto, const struct suckaddr *s) |
150 |
|
{ |
151 |
|
const struct sockaddr_in *sin4; |
152 |
|
const struct sockaddr_in6 *sin6; |
153 |
|
socklen_t sl; |
154 |
|
|
155 |
8 |
if (proto == PF_INET6) { |
156 |
4 |
sin6 = VSA_Get_Sockaddr(s, &sl); //lint !e826 |
157 |
4 |
AN(sin6); |
158 |
4 |
assert(sl >= sizeof(*sin6)); |
159 |
4 |
VSB_bcat(vsb, &sin6->sin6_addr, sizeof(sin6->sin6_addr)); |
160 |
4 |
} else { |
161 |
4 |
sin4 = VSA_Get_Sockaddr(s, &sl); //lint !e826 |
162 |
4 |
AN(sin4); |
163 |
4 |
assert(sl >= sizeof(*sin4)); |
164 |
4 |
VSB_bcat(vsb, &sin4->sin_addr, sizeof(sin4->sin_addr)); |
165 |
|
} |
166 |
8 |
} |
167 |
|
|
168 |
|
static void |
169 |
8 |
vpx_enc_port(struct vsb *vsb, const struct suckaddr *s) |
170 |
|
{ |
171 |
|
uint8_t b[2]; |
172 |
|
|
173 |
8 |
vbe16enc(b, (uint16_t)VSA_Port(s)); |
174 |
8 |
VSB_bcat(vsb, b, sizeof(b)); |
175 |
8 |
} |
176 |
|
|
177 |
|
int |
178 |
12 |
vtc_send_proxy(int fd, int version, const struct suckaddr *sac, |
179 |
|
const struct suckaddr *sas, struct vsb *tlv) |
180 |
|
{ |
181 |
|
struct vsb *vsb; |
182 |
|
char hc[VTCP_ADDRBUFSIZE]; |
183 |
|
char pc[VTCP_PORTBUFSIZE]; |
184 |
|
char hs[VTCP_ADDRBUFSIZE]; |
185 |
|
char ps[VTCP_PORTBUFSIZE]; |
186 |
|
uint16_t le, l; |
187 |
|
int i; |
188 |
|
int proto; |
189 |
|
|
190 |
12 |
AN(sac); |
191 |
12 |
AN(sas); |
192 |
|
|
193 |
12 |
assert(version == 1 || version == 2); |
194 |
12 |
vsb = VSB_new_auto(); |
195 |
12 |
AN(vsb); |
196 |
|
|
197 |
12 |
proto = VSA_Get_Proto(sas); |
198 |
12 |
assert(proto == PF_INET6 || proto == PF_INET); |
199 |
|
|
200 |
12 |
if (tlv == NULL) |
201 |
0 |
l = 0; |
202 |
|
else |
203 |
12 |
l = VSB_len(tlv); |
204 |
|
|
205 |
12 |
assert(l <= UINT16_MAX - 0x24); |
206 |
|
|
207 |
12 |
if (version == 1) { |
208 |
8 |
VSB_bcat(vsb, vpx1_sig, sizeof(vpx1_sig)); |
209 |
8 |
if (proto == PF_INET6) |
210 |
4 |
VSB_cat(vsb, " TCP6 "); |
211 |
4 |
else if (proto == PF_INET) |
212 |
4 |
VSB_cat(vsb, " TCP4 "); |
213 |
8 |
VTCP_name(sac, hc, sizeof(hc), pc, sizeof(pc)); |
214 |
8 |
VTCP_name(sas, hs, sizeof(hs), ps, sizeof(ps)); |
215 |
8 |
VSB_printf(vsb, "%s %s %s %s\r\n", hc, hs, pc, ps); |
216 |
12 |
} else if (version == 2) { |
217 |
4 |
VSB_bcat(vsb, vpx2_sig, sizeof(vpx2_sig)); |
218 |
4 |
VSB_putc(vsb, 0x21); |
219 |
4 |
if (proto == PF_INET6) { |
220 |
2 |
VSB_putc(vsb, 0x21); |
221 |
2 |
l += 0x24; |
222 |
4 |
} else if (proto == PF_INET) { |
223 |
2 |
VSB_putc(vsb, 0x11); |
224 |
2 |
l += 0x0c; |
225 |
2 |
} else |
226 |
0 |
WRONG("proto"); |
227 |
|
|
228 |
4 |
vbe16enc(&le, l); |
229 |
4 |
assert(sizeof(le) == 2); |
230 |
4 |
VSB_bcat(vsb, &le, 2); |
231 |
4 |
vpx_enc_addr(vsb, proto, sac); |
232 |
4 |
vpx_enc_addr(vsb, proto, sas); |
233 |
4 |
vpx_enc_port(vsb, sac); |
234 |
4 |
vpx_enc_port(vsb, sas); |
235 |
4 |
} else |
236 |
0 |
WRONG("Wrong proxy version"); |
237 |
|
|
238 |
12 |
AZ(VSB_finish(vsb)); |
239 |
12 |
i = VSB_tofile(vsb, fd); |
240 |
12 |
VSB_destroy(&vsb); |
241 |
12 |
if (i != 0 && tlv != NULL) |
242 |
0 |
VSB_destroy(&tlv); |
243 |
12 |
if (i != 0 || tlv == NULL) |
244 |
0 |
return (i); |
245 |
|
|
246 |
12 |
i = VSB_tofile(tlv, fd); |
247 |
12 |
VSB_destroy(&tlv); |
248 |
12 |
return (i); |
249 |
12 |
} |