varnish-cache/lib/libvarnish/vsa.c
0
/*-
1
 * Copyright (c) 2013-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
 * Struct sockaddr_* is not even close to a convenient API.
30
 *
31
 * These functions try to mitigate the madness, at the cost of actually
32
 * knowing something about address families.
33
 */
34
35
#include "config.h"
36
37
#include <string.h>
38
#include <stdint.h>
39
#include <stdlib.h>
40
#include <sys/socket.h>
41
#include <sys/un.h>
42
#include <sys/types.h>
43
#include <netinet/in.h>
44
45
#include "vdef.h"
46
#include "vas.h"
47
#include "vsa.h"
48
#include "miniobj.h"
49
50
/*
51
 * Struct sockaddr{|_in|_in6|_storage} is absolutely the worst data
52
 * structure I have ever seen gold-plated in international standards.
53
 *
54
 * Network addresses have multiple different forms, many fewer today
55
 * than in last century, but imagine that in addition to IPv4 and IPv6
56
 * we had 40 other protocols.  Actually, you don't need to imagine that
57
 * just count the AF_* macros in /usr/include/sys/socket.h.
58
 *
59
 * So what do we pass the kernel API for an address to bind(2), connect(2) &
60
 * listen(2) etc. etc ?
61
 *
62
 * We could define a struct which is big enough to hold any and all
63
 * of these addresses.  That would make it a fixed size argument.
64
 * obviously the struct would have to be something like:
65
 *      struct bla {
66
 *              int family;
67
 *              char address[MAX_ADDR_LEN];
68
 *      }
69
 * and MAX_ADDR_LEN would have to be quite large, 128 byte or so.
70
 *
71
 * Back in last century that was TOTALLY unacceptable waste of space.
72
 *
73
 * The way which was chosen instead, was to make a "generic" address,
74
 * and have per protocol "specific" addresses, and pass the length
75
 * argument explicitly to the KPI functions.
76
 *
77
 * The generic address was called "struct sockaddr", and the specific
78
 * were called "struct sockaddr_${whatever}".  All of these must have
79
 * a "family" field as first element, so the kernel can figure out
80
 * which protocol it is.
81
 *
82
 * The generic struct sockaddr was made big enough for all protocols
83
 * supported in the kernel, so it would have different sizes depending
84
 * on your machine and kernel configuration.
85
 *
86
 * However, that allowed you to write protocol-agnostic programs, by
87
 * using "struct sockaddr" throughout, and relying on libray APIs for
88
 * things like name to address (and vice versa) resolution, and since
89
 * nobody were in the business of shipping random UNIX binaries around
90
 * the lack of binary portability didn't matter.
91
 *
92
 * Along the way the BSD people figured out that it was a bother
93
 * to carry the length argument separately, and added that to the
94
 * format of sockaddr, but other groups found this unclean, as
95
 * the length was already an explicit parameter.
96
 *
97
 * The net result of this is that your "portable" code, must take
98
 * care to handle the "sa_len" member on kernels which have it,
99
 * while still tracking the separate length argument for all other
100
 * kernels.
101
 *
102
 * Needless to say, there were no neat #define to tell you which
103
 * was which, so each programmer found a different heuristic to
104
 * decide, often not understanding it fully, which caused the kind
105
 * of portability issues which lead to the autocrap tools.
106
 *
107
 * Then all the other protocols died, we were left with IP and
108
 * life were good, the dot-com madness multiplied the IT-business
109
 * by a factor 1000, by making any high-school student who had
110
 * programmed PERL for 6 weeks a "senior web-programmer".
111
 *
112
 * Next IPv6 happened, in a rush even, (no seriously, I'm not kidding!),
113
 * and since IPv6 addresses were HUGE, like 16 bytes HUGE, the generic
114
 * struct sockaddr was not increased in size.
115
 *
116
 * At least "not yet", because it would break all the shitty code written
117
 * by the dot-com generation.
118
 *
119
 * Nobody used IPv6 anyway so that didn't matter that much.
120
 *
121
 * Then people actually started using IPv6 and its struct sockaddr_in6,
122
 * and realized that all the code which used "struct sockaddr" to allocate
123
 * space at compile time were broken.
124
 *
125
 * Some people took to using sockaddr_in6, since that was known to
126
 * be big enough for both IPv4 and IPv6, but "purist" found that
127
 * ugly and "prone to future trouble".
128
 *
129
 * So instead they came up with a "clean solution":  The added
130
 * "struct sockaddr_storage" which is defined to be "Large enough
131
 * to accommodate all supported protocol-specific address structures".
132
 *
133
 * Since we cannot possibly know what zany protocols will exist in
134
 * the future, and since some people think that we will add future
135
 * protocols, while retaining ABI compatibility, (totally overlooking
136
 * the fact that no code for name-resolution supports that) it is
137
 * usually defined so it can cope with 128 byte addresses.
138
 *
139
 * Does that ring a bell ?
140
 *
141
 * Only, not quite:  Remember that all APIs require you to track
142
 * the address and the length separately, so you only get the
143
 * size of the specific protocols sockaddr_${whatever} from API
144
 * functions, not a full sockaddr_storage, and besides the
145
 * prototype for the KPI is still "struct sockaddr *", so you
146
 * cannot gain C type-safety back by using sockaddr_storage
147
 * as the "generic network address" type.
148
 *
149
 * So we have come full circle, while causing maximum havoc along
150
 * the way and for the forseeable future.
151
 *
152
 * Do I need to tell you that static code analysis tools have a
153
 * really hard time coping with this, and that they give a lot of
154
 * false negatives which confuse people ?
155
 *
156
 * I have decided to try to contain this crap in this single
157
 * source-file, with only minimum leakage into the rest of Varnish,
158
 * which will only know of pointers to "struct suckaddr", the naming
159
 * of which is my of the historical narrative above.
160
 *
161
 * And you don't need to take my word for this, you can see it all
162
 * in various #include files on your own system.   If you are on
163
 * a Solaris derivative, don't miss the beautiful horror hidden in the
164
 * variant definition of IPv6 addresses between kernel and userland.
165
 *
166
 */
167
168
struct suckaddr {
169
        unsigned                        magic;
170
#define SUCKADDR_MAGIC                  0x4b1e9335
171
        union {
172
                struct sockaddr         sa;
173
                struct sockaddr_in      sa4;
174
                struct sockaddr_in6     sa6;
175
        } u;
176
};
177
178
const size_t vsa_suckaddr_len = sizeof(struct suckaddr);
179
180
/*
181
 * Bogus IPv4 address 0.0.0.0:0 to be used for VCL *.ip variables when the
182
 * "real" address is not IP (such as UDS addresses).
183
 */
184
static struct suckaddr bogo_ip_vsa;
185
const struct suckaddr *bogo_ip = &bogo_ip_vsa;
186
/* same in IPv6 */
187
static struct suckaddr bogo_ip6_vsa;
188
const struct suckaddr *bogo_ip6 = &bogo_ip6_vsa;
189
190
void
191 24975
VSA_Init(void)
192
{
193 24975
        AN(VSA_BuildFAP(&bogo_ip_vsa, PF_INET, NULL, 0, NULL, 0));
194 24975
        AN(VSA_BuildFAP(&bogo_ip6_vsa, PF_INET6, NULL, 0, NULL, 0));
195 24975
}
196
197
/*
198
 * This VRT interface is for the VCC generated ACL code, which needs
199
 * to know the address family and a pointer to the actual address.
200
 */
201
202
int
203 2607012
VSA_GetPtr(const struct suckaddr *sua, const unsigned char ** dst)
204
{
205
206 2607012
        AN(dst);
207 2607012
        if (sua == NULL)
208 0
                return (-1);
209 2607012
        CHECK_OBJ_NOTNULL(sua, SUCKADDR_MAGIC);
210
211 2607012
        switch (sua->u.sa.sa_family) {
212
        case PF_INET:
213 5454
                assert(sua->u.sa.sa_family == sua->u.sa4.sin_family);
214 5454
                *dst = (const unsigned char *)&sua->u.sa4.sin_addr;
215 5454
                return (sua->u.sa4.sin_family);
216
        case PF_INET6:
217 2601558
                assert(sua->u.sa.sa_family == sua->u.sa6.sin6_family);
218 2601558
                *dst = (const unsigned char *)&sua->u.sa6.sin6_addr;
219 2601558
                return (sua->u.sa6.sin6_family);
220
        default:
221 0
                *dst = NULL;
222 0
                return (-1);
223
        }
224 2607012
}
225
226
/*
227
 * Return the size of a struct sockaddr in a struck suckaddr
228
 * or 0 if unknown family
229
 */
230
static inline
231 4268302
socklen_t sua_len(const struct sockaddr *sa)
232
{
233
234 4268302
        switch (sa->sa_family) {
235
        case PF_INET:
236 1507525
                return (sizeof(struct sockaddr_in));
237
        case PF_INET6:
238 2760750
                return (sizeof(struct sockaddr_in6));
239
        case AF_UNIX:
240 27
                return (sizeof(struct sockaddr_un));
241
        default:
242 0
                return (0);
243
        }
244 4268302
}
245
246
/*
247
 * Malloc a suckaddr from a sockaddr of some kind.
248
 */
249
250
const struct suckaddr *
251 288333
VSA_Malloc(const void *s, unsigned  sal)
252
{
253
254 288333
        return (VSA_Build(NULL, s, sal));
255
}
256
257
/* 'd' SHALL point to vsa_suckaddr_len aligned bytes of storage
258
 *
259
 * fam: address family
260
 * a / al : address and length
261
 * p / pl : port and length
262
 *
263
 * NULL or 0 length argument are ignored.
264
 * argument of the wrong length are an error (NULL return value, EINVAL)
265
 */
266
const struct suckaddr *
267 51138
VSA_BuildFAP(void *d, sa_family_t fam, const void *a, unsigned al,
268
            const void *p, unsigned pl)
269
{
270
        struct sockaddr_in sin4;
271
        struct sockaddr_in6 sin6;
272
273 51138
        switch (fam) {
274
        case PF_INET:
275 25893
                memset(&sin4, 0, sizeof sin4);
276 25893
                sin4.sin_family = fam;
277 25893
                if (a != NULL && al > 0) {
278 918
                        if (al != sizeof(sin4.sin_addr))
279 0
                                break;
280 918
                        memcpy(&sin4.sin_addr, a, al);
281 918
                }
282 25893
                if (p != NULL && pl > 0) {
283 756
                        if (pl != sizeof(sin4.sin_port))
284 0
                                break;
285 756
                        memcpy(&sin4.sin_port, p, pl);
286 756
                }
287 25893
                return (VSA_Build(d, &sin4, sizeof sin4));
288
        case PF_INET6:
289 25245
                memset(&sin6, 0, sizeof sin6);
290 25245
                sin6.sin6_family = fam;
291 25245
                if (a != NULL && al > 0) {
292 270
                        if (al != sizeof(sin6.sin6_addr))
293 0
                                break;
294 270
                        memcpy(&sin6.sin6_addr, a, al);
295 270
                }
296 25245
                if (p != NULL && pl > 0) {
297 270
                        if (pl != sizeof(sin6.sin6_port))
298 0
                                break;
299 270
                        memcpy(&sin6.sin6_port, p, pl);
300 270
                }
301 25245
                return (VSA_Build(d, &sin6, sizeof sin6));
302
        default:
303 0
                errno = EAFNOSUPPORT;
304 0
                return (NULL);
305
        }
306 0
        errno = EINVAL;
307 0
        return (NULL);
308 51138
}
309
310
const struct suckaddr *
311 391060
VSA_Build(void *d, const void *s, unsigned sal)
312
{
313
        struct suckaddr *sua;
314 391060
        const struct sockaddr *sa = s;
315
        unsigned l;     // for flexelint
316
317 391060
        AN(s);
318 391060
        l = sua_len(sa);
319 391060
        if (l == 0 || l != sal)
320 34
                return (NULL);
321
322 391030
        if (d == NULL) {
323 288332
                d = malloc(vsa_suckaddr_len);
324 288332
                AN(d);
325 288332
        }
326
327 391030
        sua = d;
328
329 391030
        INIT_OBJ(sua, SUCKADDR_MAGIC);
330 391030
        switch (l) {
331
        case sizeof sua->u.sa4:
332 313216
                memcpy(&sua->u.sa4, s, l);
333 313216
                break;
334
        case sizeof sua->u.sa6:
335 77814
                memcpy(&sua->u.sa6, s, l);
336 77814
                break;
337
        default:
338 0
                WRONG("VSA protocol vs. size");
339 0
        }
340
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
341 391030
        sua->u.sa.sa_len = (unsigned char)l;
342
#endif
343 391030
        return (sua);
344 391064
}
345
346
const void *
347 1006586
VSA_Get_Sockaddr(const struct suckaddr *sua, socklen_t *slp)
348
{
349
        socklen_t sl;
350
351 1006586
        CHECK_OBJ_NOTNULL(sua, SUCKADDR_MAGIC);
352 1006586
        AN(slp);
353 1006586
        sl = sua_len(&sua->u.sa);
354 1006586
        if (sl == 0)
355 0
                return (NULL);
356 1006586
        *slp = sl;
357 1006586
        return (&sua->u.sa);
358 1006586
}
359
360
int
361 337794
VSA_Get_Proto(const struct suckaddr *sua)
362
{
363
364 337794
        CHECK_OBJ_NOTNULL(sua, SUCKADDR_MAGIC);
365 337794
        return (sua->u.sa.sa_family);
366
}
367
368
int
369 2870659
VSA_Sane(const struct suckaddr *sua)
370
{
371 2870659
        return (VALID_OBJ(sua, SUCKADDR_MAGIC) && sua_len(&sua->u.sa) != 0);
372
}
373
374
int
375 4830
VSA_Compare(const struct suckaddr *sua1, const struct suckaddr *sua2)
376
{
377
378 4830
        CHECK_OBJ_NOTNULL(sua1, SUCKADDR_MAGIC);
379 4830
        CHECK_OBJ_NOTNULL(sua2, SUCKADDR_MAGIC);
380 4830
        return (memcmp(sua1, sua2, vsa_suckaddr_len));
381
}
382
383
int
384 432
VSA_Compare_IP(const struct suckaddr *sua1, const struct suckaddr *sua2)
385
{
386
387 432
        assert(VSA_Sane(sua1));
388 432
        assert(VSA_Sane(sua2));
389
390 432
        if (sua1->u.sa.sa_family != sua2->u.sa.sa_family)
391 0
                return (-1);
392
393 432
        switch (sua1->u.sa.sa_family) {
394
        case PF_INET:
395 864
                return (memcmp(&sua1->u.sa4.sin_addr,
396 432
                    &sua2->u.sa4.sin_addr, sizeof(struct in_addr)));
397
        case PF_INET6:
398 0
                return (memcmp(&sua1->u.sa6.sin6_addr,
399 0
                    &sua2->u.sa6.sin6_addr, sizeof(struct in6_addr)));
400
        default:
401 0
                WRONG("Just plain insane");
402 0
        }
403 0
        NEEDLESS(return (-1));
404 432
}
405
406
const struct suckaddr *
407 82917
VSA_Clone(const struct suckaddr *sua)
408
{
409
        struct suckaddr *sua2;
410
411 82917
        assert(VSA_Sane(sua));
412 82917
        sua2 = calloc(1, vsa_suckaddr_len);
413 82917
        XXXAN(sua2);
414 82917
        memcpy(sua2, sua, vsa_suckaddr_len);
415 82917
        return (sua2);
416
}
417
418
unsigned
419 26244
VSA_Port(const struct suckaddr *sua)
420
{
421
422 26244
        CHECK_OBJ_NOTNULL(sua, SUCKADDR_MAGIC);
423 26244
        switch (sua->u.sa.sa_family) {
424
        case PF_INET:
425 25191
                return (ntohs(sua->u.sa4.sin_port));
426
        case PF_INET6:
427 1053
                return (ntohs(sua->u.sa6.sin6_port));
428
        default:
429 0
                return (0);
430
        }
431 26244
}
432
433
#define VSA_getname(which)                              \
434
const struct suckaddr *                                 \
435
VSA_get ## which ## name(int fd, void *d, size_t l)     \
436
{                                                       \
437
        struct suckaddr *sua;                           \
438
        socklen_t sl;                                   \
439
        int r;                                          \
440
                                                        \
441
        AN(d);                                          \
442
        if (l != vsa_suckaddr_len) {                    \
443
                errno = EINVAL;                         \
444
                return (NULL);                          \
445
        }                                               \
446
                                                        \
447
        sua = d;                                        \
448
                                                        \
449
        INIT_OBJ(sua, SUCKADDR_MAGIC);                  \
450
        sl = sizeof(sua->u);                            \
451
        r = get ## which ## name(fd, &sua->u.sa, &sl);  \
452
                                                        \
453
        return (r == 0 ? sua : NULL);                   \
454
}                                                       \
455
456 391751
VSA_getname(sock)
457 200711
VSA_getname(peer)
458
#undef VSA_getname
459
460
void
461 346810
VSA_free(const struct suckaddr **vsap)
462
{
463
        const struct suckaddr *vsa;
464
465 346810
        TAKE_OBJ_NOTNULL(vsa, vsap, SUCKADDR_MAGIC);
466 346810
        free(TRUST_ME(vsa));
467 346810
}
468