varnish-cache/lib/libvarnish/vsa.c
1
/*-
2
 * Copyright (c) 2013-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 *
28
 * Struct sockaddr_* is not even close to a convenient API.
29
 *
30
 * These functions try to mitigate the madness, at the cost of actually
31
 * knowing something about address families.
32
 */
33
34
#include "config.h"
35
36
#include <string.h>
37
#include <stdlib.h>
38
#include <sys/socket.h>
39
#include <sys/types.h>
40
#include <netinet/in.h>
41
#include <netdb.h>
42
43
#include "vdef.h"
44
#include "vas.h"
45
#include "vsa.h"
46
#include "vrt.h"
47
#include "miniobj.h"
48
49
/*
50
 * Struct sockaddr{|_in|_in6|_storage} is absolutely the worst data
51
 * structure I have ever seen gold-plated in international standards.
52
 *
53
 * Network addresses have multiple different forms, many fewer today
54
 * than in last century, but imagine that in addition to IPv4 and IPv6
55
 * we had 40 other protocols.  Actually, you don't need to imagine that
56
 * just count the AF_* macros in /usr/include/sys/socket.h.
57
 *
58
 * So what do we pass the kernel API for an address to bind(2), connect(2) &
59
 * listen(2) etc. etc ?
60
 *
61
 * We could define a struct which is big enough to hold any and all
62
 * of these addresses.  That would make it a fixed size argument.
63
 * obviously the struct would have to be something like:
64
 *      struct bla {
65
 *              int family;
66
 *              char address[MAX_ADDR_LEN];
67
 *      }
68
 * and MAX_ADDR_LEN would have to be quite large, 128 byte or so.
69
 *
70
 * Back in last century that was TOTALLY unacceptable waste of space.
71
 *
72
 * The way which was chosen instead, was to make a "generic" address,
73
 * and have per protocol "specific" addresses, and pass the length
74
 * argument explicitly to the KPI functions.
75
 *
76
 * The generic address was called "struct sockaddr", and the specific
77
 * were called "struct sockaddr_${whatever}".  All of these must have
78
 * a "family" field as first element, so the kernel can figure out
79
 * which protocol it is.
80
 *
81
 * The generic struct sockaddr was made big enough for all protocols
82
 * supported in the kernel, so it would have different sizes depending
83
 * on your machine and kernel configuration.
84
 *
85
 * However, that allowed you to write protocol-agnostic programs, by
86
 * using "struct sockaddr" throughout, and relying on libray APIs for
87
 * things like name to address (and vice versa) resolution, and since
88
 * nobody were in the business of shipping random UNIX binaries around
89
 * the lack of binary portability didn't matter.
90
 *
91
 * Along the way the BSD people figured out that it was a bother
92
 * to carry the length argument separately, and added that to the
93
 * format of sockaddr, but other groups found this unclean, as
94
 * the length was already an explicit parameter.
95
 *
96
 * The net result of this is that your "portable" code, must take
97
 * care to handle the "sa_len" member on kernels which have it,
98
 * while still tracking the separate length argument for all other
99
 * kernels.
100
 *
101
 * Needless to say, there were no neat #define to tell you which
102
 * was which, so each programmer found a different heuristic to
103
 * decide, often not understanding it fully, which caused the kind
104
 * of portability issues which lead to the autocrap tools.
105
 *
106
 * Then all the other protocols died, we were left with IP and
107
 * life were good, the dot-com madness multiplied the IT-business
108
 * by a factor 1000, by making any high-school student who had
109
 * programmed PERL for 6 weeks a "senior web-programmer".
110
 *
111
 * Next IPv6 happened, in a rush even, (no seriously, I'm not kidding!),
112
 * and since IPv6 addresses were HUGE, like 16 bytes HUGE, the generic
113
 * struct sockaddr was not increased in size.
114
 *
115
 * At least "not yet", because it would break all the shitty code written
116
 * by the dot-com generation.
117
 *
118
 * Nobody used IPv6 anyway so that didn't matter that much.
119
 *
120
 * Then people actually started using IPv6 and its struct sockaddr_in6,
121
 * and realized that all the code which used "struct sockaddr" to allocate
122
 * space at compile time were broken.
123
 *
124
 * Some people took to using sockaddr_in6, since that was known to
125
 * be big enough for both IPv4 and IPv6, but "purist" found that
126
 * ugly and "prone to future trouble".
127
 *
128
 * So instead they came up with a "clean solution":  The added
129
 * "struct sockaddr_storage" which is defined to be "Large enough
130
 * to accommodate all supported protocol-specific address structures".
131
 *
132
 * Since we cannot possibly know what zany protocols will exist in
133
 * the future, and since some people think that we will add future
134
 * protocols, while retaining ABI compatibility, (totally overlooking
135
 * the fact that no code for name-resolution supports that) it is
136
 * usually defined so it can cope with 128 byte addresses.
137
 *
138
 * Does that ring a bell ?
139
 *
140
 * Only, not quite:  Remember that all APIs require you to track
141
 * the address and the length separately, so you only get the
142
 * size of the specific protocols sockaddr_${whatever} from API
143
 * functions, not a full sockaddr_storage, and besides the
144
 * prototype for the KPI is still "struct sockaddr *", so you
145
 * cannot gain C type-safety back by using sockaddr_storage
146
 * as the "generic network address" type.
147
 *
148
 * So we have come full circle, while causing maximum havoc along
149
 * the way and for the forseeable future.
150
 *
151
 * Do I need to tell you that static code analysis tools have a
152
 * really hard time coping with this, and that they give a lot of
153
 * false negatives which confuse people ?
154
 *
155
 * I have decided to try to contain this crap in this single
156
 * source-file, with only minimum leakage into the rest of Varnish,
157
 * which will only know of pointers to "struct suckaddr", the naming
158
 * of which is my of the historical narrative above.
159
 *
160
 * And you don't need to take my word for this, you can see it all
161
 * in various #include files on your own system.   If you are on
162
 * a Solaris derivative, don't miss the beautiful horror hidden in the
163
 * variant definition of IPv6 addresses between kernel and userland.
164
 *
165
 */
166
167
struct suckaddr {
168
        unsigned                        magic;
169
#define SUCKADDR_MAGIC                  0x4b1e9335
170
        union {
171
                struct sockaddr         sa;
172
                struct sockaddr_in      sa4;
173
                struct sockaddr_in6     sa6;
174
        };
175
};
176
177
const int vsa_suckaddr_len = sizeof(struct suckaddr);
178
179
/*
180
 * Bogus IPv4 address 0.0.0.0:0 to be used for VCL *.ip variables when the
181
 * "real" address is not IP (such as UDS addresses).
182
 */
183
static struct suckaddr bogo_ip_vsa;
184
const struct suckaddr *bogo_ip = &bogo_ip_vsa;
185
186
void
187 1528
VSA_Init()
188
{
189 1528
        struct addrinfo hints, *res = NULL;
190
191 1528
        memset(&hints, 0, sizeof(hints));
192 1528
        hints.ai_family = AF_INET;
193 1528
        hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
194 1528
        hints.ai_socktype = SOCK_STREAM;
195 1528
        AZ(getaddrinfo("0.0.0.0", "0", &hints, &res));
196 1528
        AN(VSA_Build(&bogo_ip_vsa, res->ai_addr, res->ai_addrlen));
197 1528
        assert(VSA_Sane(bogo_ip));
198 1528
        freeaddrinfo(res);
199 1528
}
200
201
/*
202
 * This VRT interface is for the VCC generated ACL code, which needs
203
 * to know the address family and a pointer to the actual address.
204
 */
205
206
int
207 56
VRT_VSA_GetPtr(const struct suckaddr *sua, const unsigned char ** dst)
208
{
209
210 56
        AN(dst);
211 56
        if (sua == NULL)
212 0
                return (-1);
213 56
        CHECK_OBJ_NOTNULL(sua, SUCKADDR_MAGIC);
214
215 56
        switch (sua->sa.sa_family) {
216
        case PF_INET:
217 52
                assert(sua->sa.sa_family == sua->sa4.sin_family);
218 52
                *dst = (const unsigned char *)&sua->sa4.sin_addr;
219 52
                return (sua->sa4.sin_family);
220
        case PF_INET6:
221 4
                assert(sua->sa.sa_family == sua->sa6.sin6_family);
222 4
                *dst = (const unsigned char *)&sua->sa6.sin6_addr;
223 4
                return (sua->sa6.sin6_family);
224
        default:
225 0
                *dst = NULL;
226 0
                return (-1);
227
        }
228
}
229
230
/*
231
 * Malloc a suckaddr from a sockaddr of some kind.
232
 */
233
234
struct suckaddr *
235 20246
VSA_Malloc(const void *s, unsigned  sal)
236
{
237 20246
        struct suckaddr *sua = NULL;
238 20246
        const struct sockaddr *sa = s;
239 20246
        unsigned l = 0;
240
241 20246
        AN(s);
242 20246
        switch (sa->sa_family) {
243
        case PF_INET:
244 17056
                if (sal == sizeof sua->sa4)
245 17056
                        l = sal;
246 17056
                break;
247
        case PF_INET6:
248 3190
                if (sal == sizeof sua->sa6)
249 3190
                        l = sal;
250 3190
                break;
251
        default:
252 0
                break;
253
        }
254 20246
        if (l != 0) {
255 20246
                ALLOC_OBJ(sua, SUCKADDR_MAGIC);
256 20246
                if (sua != NULL)
257 20246
                        memcpy(&sua->sa, s, l);
258
        }
259 20246
        return (sua);
260
}
261
262
/* 'd' SHALL point to vsa_suckaddr_len aligned bytes of storage */
263
struct suckaddr *
264 7335
VSA_Build(void *d, const void *s, unsigned sal)
265
{
266 7335
        struct suckaddr *sua = d;
267 7335
        const struct sockaddr *sa = s;
268 7335
        unsigned l = 0;
269
270 7335
        AN(d);
271 7335
        AN(s);
272 7335
        switch (sa->sa_family) {
273
        case PF_INET:
274 7293
                if (sal == sizeof sua->sa4)
275 7293
                        l = sal;
276 7293
                break;
277
        case PF_INET6:
278 40
                if (sal == sizeof sua->sa6)
279 40
                        l = sal;
280 40
                break;
281
        default:
282 2
                break;
283
        }
284 7335
        if (l != 0) {
285 7333
                memset(sua, 0, sizeof *sua);
286 7333
                sua->magic = SUCKADDR_MAGIC;
287 7333
                memcpy(&sua->sa, s, l);
288 7333
                return (sua);
289
        }
290 2
        return (NULL);
291
}
292
293
const void *
294 30795
VSA_Get_Sockaddr(const struct suckaddr *sua, socklen_t *sl)
295
{
296
297 30795
        CHECK_OBJ_NOTNULL(sua, SUCKADDR_MAGIC);
298 30795
        AN(sl);
299 30795
        switch (sua->sa.sa_family) {
300
        case PF_INET:
301 27569
                *sl = sizeof sua->sa4;
302 27569
                break;
303
        case PF_INET6:
304 3226
                *sl = sizeof sua->sa6;
305 3226
                break;
306
        default:
307 0
                return (NULL);
308
        }
309 30795
        return (&sua->sa);
310
}
311
312
int
313 14496
VSA_Get_Proto(const struct suckaddr *sua)
314
{
315
316 14496
        CHECK_OBJ_NOTNULL(sua, SUCKADDR_MAGIC);
317 14496
        return (sua->sa.sa_family);
318
}
319
320
int
321 17656
VSA_Sane(const struct suckaddr *sua)
322
{
323 17656
        CHECK_OBJ_NOTNULL(sua, SUCKADDR_MAGIC);
324
325 17656
        switch (sua->sa.sa_family) {
326
        case PF_INET:
327
        case PF_INET6:
328 17656
                return (1);
329
        default:
330 0
                return (0);
331
        }
332
}
333
334
int
335 1166
VSA_Compare(const struct suckaddr *sua1, const struct suckaddr *sua2)
336
{
337
338 1166
        CHECK_OBJ_NOTNULL(sua1, SUCKADDR_MAGIC);
339 1166
        CHECK_OBJ_NOTNULL(sua2, SUCKADDR_MAGIC);
340 1166
        return (memcmp(sua1, sua2, vsa_suckaddr_len));
341
}
342
343
int
344 46
VSA_Compare_IP(const struct suckaddr *sua1, const struct suckaddr *sua2)
345
{
346
347 46
        assert(VSA_Sane(sua1));
348 46
        assert(VSA_Sane(sua2));
349
350 46
        if (sua1->sa.sa_family != sua2->sa.sa_family)
351 0
                return (-1);
352
353 46
        switch (sua1->sa.sa_family) {
354
        case PF_INET:
355 46
                return (memcmp(&sua1->sa4.sin_addr,
356 46
                    &sua2->sa4.sin_addr, sizeof(struct in_addr)));
357
        case PF_INET6:
358 0
                return (memcmp(&sua1->sa6.sin6_addr,
359 0
                    &sua2->sa6.sin6_addr, sizeof(struct in6_addr)));
360
        default:
361 0
                WRONG("Just plain insane");
362
        }
363
        NEEDLESS(return(-1));
364
}
365
366
struct suckaddr *
367 6788
VSA_Clone(const struct suckaddr *sua)
368
{
369
        struct suckaddr *sua2;
370
371 6788
        assert(VSA_Sane(sua));
372 6788
        sua2 = calloc(1, vsa_suckaddr_len);
373 6788
        XXXAN(sua2);
374 6788
        memcpy(sua2, sua, vsa_suckaddr_len);
375 6788
        return (sua2);
376
}
377
378
unsigned
379 1582
VSA_Port(const struct suckaddr *sua)
380
{
381
382 1582
        CHECK_OBJ_NOTNULL(sua, SUCKADDR_MAGIC);
383 1582
        switch (sua->sa.sa_family) {
384
        case PF_INET:
385 1508
                return (ntohs(sua->sa4.sin_port));
386
        case PF_INET6:
387 74
                return (ntohs(sua->sa6.sin6_port));
388
        default:
389 0
                return (0);
390
        }
391
}