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