• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14458263136

14 Apr 2025 06:41PM UTC coverage: 72.031% (+0.001%) from 72.03%
14458263136

push

github

yuwata
test: drop error conditions for old kernels (<3.2)

Now our baseline on the kernel is 5.4.

2 of 4 new or added lines in 1 file covered. (50.0%)

1428 existing lines in 44 files now uncovered.

297292 of 412726 relevant lines covered (72.03%)

683119.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.07
/src/basic/socket-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
/* Make sure the net/if.h header is included before any linux/ one */
4
#include <net/if.h>
5
#include <arpa/inet.h>
6
#include <errno.h>
7
#include <limits.h>
8
#include <netdb.h>
9
#include <netinet/ip.h>
10
#include <poll.h>
11
#include <stddef.h>
12
#include <stdint.h>
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <sys/ioctl.h>
16
#include <unistd.h>
17
#include <linux/if.h>
18

19
#include "alloc-util.h"
20
#include "errno-util.h"
21
#include "escape.h"
22
#include "fd-util.h"
23
#include "fileio.h"
24
#include "format-ifname.h"
25
#include "io-util.h"
26
#include "log.h"
27
#include "memory-util.h"
28
#include "parse-util.h"
29
#include "path-util.h"
30
#include "process-util.h"
31
#include "random-util.h"
32
#include "socket-util.h"
33
#include "string-table.h"
34
#include "string-util.h"
35
#include "strv.h"
36
#include "sysctl-util.h"
37
#include "user-util.h"
38
#include "utf8.h"
39

40
#if ENABLE_IDN
41
#  define IDN_FLAGS NI_IDN
42
#else
43
#  define IDN_FLAGS 0
44
#endif
45

46
/* From the kernel's include/net/scm.h */
47
#ifndef SCM_MAX_FD
48
#  define SCM_MAX_FD 253
49
#endif
50

51
static const char* const socket_address_type_table[] = {
52
        [SOCK_STREAM] =    "Stream",
53
        [SOCK_DGRAM] =     "Datagram",
54
        [SOCK_RAW] =       "Raw",
55
        [SOCK_RDM] =       "ReliableDatagram",
56
        [SOCK_SEQPACKET] = "SequentialPacket",
57
        [SOCK_DCCP] =      "DatagramCongestionControl",
58
};
59

60
DEFINE_STRING_TABLE_LOOKUP(socket_address_type, int);
×
61

62
int socket_address_verify(const SocketAddress *a, bool strict) {
7,518✔
63
        assert(a);
7,518✔
64

65
        /* With 'strict' we enforce additional sanity constraints which are not set by the standard,
66
         * but should only apply to sockets we create ourselves. */
67

68
        switch (socket_address_family(a)) {
7,518✔
69

70
        case AF_INET:
25✔
71
                if (a->size != sizeof(struct sockaddr_in))
25✔
72
                        return -EINVAL;
73

74
                if (a->sockaddr.in.sin_port == 0)
25✔
75
                        return -EINVAL;
76

77
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
25✔
78
                        return -EINVAL;
1✔
79

80
                return 0;
81

82
        case AF_INET6:
16✔
83
                if (a->size != sizeof(struct sockaddr_in6))
16✔
84
                        return -EINVAL;
85

86
                if (a->sockaddr.in6.sin6_port == 0)
16✔
87
                        return -EINVAL;
88

89
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
16✔
90
                        return -EINVAL;
×
91

92
                return 0;
93

94
        case AF_UNIX:
7,049✔
95
                if (a->size < offsetof(struct sockaddr_un, sun_path))
7,049✔
96
                        return -EINVAL;
97
                if (a->size > sizeof(struct sockaddr_un) + !strict)
7,049✔
98
                        /* If !strict, allow one extra byte, since getsockname() on Linux will append
99
                         * a NUL byte if we have path sockets that are above sun_path's full size. */
100
                        return -EINVAL;
101

102
                if (a->size > offsetof(struct sockaddr_un, sun_path) &&
7,049✔
103
                    a->sockaddr.un.sun_path[0] != 0 &&
7,049✔
104
                    strict) {
105
                        /* Only validate file system sockets here, and only in strict mode */
106
                        const char *e;
2,207✔
107

108
                        e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path));
2,207✔
109
                        if (e) {
2,207✔
110
                                /* If there's an embedded NUL byte, make sure the size of the socket address matches it */
111
                                if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1)
2,207✔
112
                                        return -EINVAL;
113
                        } else {
114
                                /* If there's no embedded NUL byte, then the size needs to match the whole
115
                                 * structure or the structure with one extra NUL byte suffixed. (Yeah, Linux is awful,
116
                                 * and considers both equivalent: getsockname() even extends sockaddr_un beyond its
117
                                 * size if the path is non NUL terminated.) */
118
                                if (!IN_SET(a->size, sizeof(a->sockaddr.un.sun_path), sizeof(a->sockaddr.un.sun_path)+1))
×
119
                                        return -EINVAL;
120
                        }
121
                }
122

123
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET))
7,049✔
124
                        return -EINVAL;
×
125

126
                return 0;
127

128
        case AF_NETLINK:
343✔
129

130
                if (a->size != sizeof(struct sockaddr_nl))
343✔
131
                        return -EINVAL;
132

133
                if (!IN_SET(a->type, 0, SOCK_RAW, SOCK_DGRAM))
343✔
134
                        return -EINVAL;
×
135

136
                return 0;
137

138
        case AF_VSOCK:
68✔
139
                if (a->size != sizeof(struct sockaddr_vm))
68✔
140
                        return -EINVAL;
141

142
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
68✔
143
                        return -EINVAL;
×
144

145
                return 0;
146

147
        default:
148
                return -EAFNOSUPPORT;
149
        }
150
}
151

152
int socket_address_print(const SocketAddress *a, char **ret) {
1,509✔
153
        int r;
1,509✔
154

155
        assert(a);
1,509✔
156
        assert(ret);
1,509✔
157

158
        r = socket_address_verify(a, false); /* We do non-strict validation, because we want to be
1,509✔
159
                                              * able to pretty-print any socket the kernel considers
160
                                              * valid. We still need to do validation to know if we
161
                                              * can meaningfully print the address. */
162
        if (r < 0)
1,509✔
163
                return r;
164

165
        if (socket_address_family(a) == AF_NETLINK) {
1,509✔
166
                _cleanup_free_ char *sfamily = NULL;
113✔
167

168
                r = netlink_family_to_string_alloc(a->protocol, &sfamily);
113✔
169
                if (r < 0)
113✔
170
                        return r;
171

172
                r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
113✔
173
                if (r < 0)
113✔
174
                        return -ENOMEM;
175

176
                return 0;
113✔
177
        }
178

179
        return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret);
1,396✔
180
}
181

182
bool socket_address_can_accept(const SocketAddress *a) {
4,632✔
183
        assert(a);
4,632✔
184

185
        return
4,632✔
186
                IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET);
4,632✔
187
}
188

189
bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
1,853✔
190
        assert(a);
1,853✔
191
        assert(b);
1,853✔
192

193
        /* Invalid addresses are unequal to all */
194
        if (socket_address_verify(a, false) < 0 ||
3,706✔
195
            socket_address_verify(b, false) < 0)
1,853✔
196
                return false;
197

198
        if (a->type != b->type)
1,852✔
199
                return false;
200

201
        if (socket_address_family(a) != socket_address_family(b))
1,851✔
202
                return false;
203

204
        switch (socket_address_family(a)) {
1,849✔
205

206
        case AF_INET:
6✔
207
                if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr)
6✔
208
                        return false;
209

210
                if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port)
5✔
211
                        return false;
1✔
212

213
                break;
214

215
        case AF_INET6:
2✔
216
                if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0)
2✔
217
                        return false;
218

219
                if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port)
2✔
220
                        return false;
×
221

222
                break;
223

224
        case AF_UNIX:
1,737✔
225
                if (a->size <= offsetof(struct sockaddr_un, sun_path) ||
1,737✔
226
                    b->size <= offsetof(struct sockaddr_un, sun_path))
1,737✔
227
                        return false;
228

229
                if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0))
1,737✔
230
                        return false;
231

232
                if (a->sockaddr.un.sun_path[0]) {
1,472✔
233
                        if (!path_equal_or_inode_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
1,470✔
234
                                return false;
437✔
235
                } else {
236
                        if (a->size != b->size)
2✔
237
                                return false;
238

239
                        if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
2✔
240
                                return false;
×
241
                }
242

243
                break;
244

245
        case AF_NETLINK:
85✔
246
                if (a->protocol != b->protocol)
85✔
247
                        return false;
248

249
                if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups)
85✔
250
                        return false;
1✔
251

252
                break;
253

254
        case AF_VSOCK:
19✔
255
                if (a->sockaddr.vm.svm_cid != b->sockaddr.vm.svm_cid)
19✔
256
                        return false;
257

258
                if (a->sockaddr.vm.svm_port != b->sockaddr.vm.svm_port)
18✔
259
                        return false;
1✔
260

261
                break;
262

263
        default:
264
                /* Cannot compare, so we assume the addresses are different */
265
                return false;
266
        }
267

268
        return true;
269
}
270

271
const char* socket_address_get_path(const SocketAddress *a) {
10,132✔
272
        assert(a);
10,132✔
273

274
        if (socket_address_family(a) != AF_UNIX)
10,132✔
275
                return NULL;
276

277
        if (a->sockaddr.un.sun_path[0] == 0)
9,652✔
278
                return NULL;
279

280
        /* Note that this is only safe because we know that there's an extra NUL byte after the sockaddr_un
281
         * structure. On Linux AF_UNIX file system socket addresses don't have to be NUL terminated if they take up the
282
         * full sun_path space. */
283
        assert_cc(sizeof(union sockaddr_union) >= sizeof(struct sockaddr_un)+1);
9,651✔
284
        return a->sockaddr.un.sun_path;
9,651✔
285
}
286

287
bool socket_ipv6_is_supported(void) {
139,039✔
288
        static int cached = -1;
139,039✔
289

290
        if (cached < 0) {
139,039✔
291

292
                if (access("/proc/net/if_inet6", F_OK) < 0) {
1,125✔
293

294
                        if (errno != ENOENT) {
×
295
                                log_debug_errno(errno, "Unexpected error when checking whether /proc/net/if_inet6 exists: %m");
×
296
                                return false;
×
297
                        }
298

299
                        cached = false;
×
300
                } else
301
                        cached = true;
1,125✔
302
        }
303

304
        return cached;
139,039✔
305
}
306

307
bool socket_ipv6_is_enabled(void) {
99,825✔
308
        _cleanup_free_ char *v = NULL;
99,825✔
309
        int r;
99,825✔
310

311
        /* Much like socket_ipv6_is_supported(), but also checks that the sysctl that disables IPv6 on all
312
         * interfaces isn't turned on */
313

314
        if (!socket_ipv6_is_supported())
99,825✔
315
                return false;
316

317
        r = sysctl_read_ip_property(AF_INET6, "all", "disable_ipv6", &v);
99,825✔
318
        if (r < 0) {
99,825✔
319
                log_debug_errno(r, "Unexpected error reading 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
×
320
                return true;
×
321
        }
322

323
        r = parse_boolean(v);
99,825✔
324
        if (r < 0) {
99,825✔
325
                log_debug_errno(r, "Failed to pare 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
×
326
                return true;
×
327
        }
328

329
        return !r;
99,825✔
330
}
331

332
bool socket_address_matches_fd(const SocketAddress *a, int fd) {
946✔
333
        SocketAddress b;
946✔
334
        socklen_t solen;
946✔
335

336
        assert(a);
946✔
337
        assert(fd >= 0);
946✔
338

339
        b.size = sizeof(b.sockaddr);
946✔
340
        if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0)
946✔
341
                return false;
946✔
342

343
        if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family)
946✔
344
                return false;
345

346
        solen = sizeof(b.type);
829✔
347
        if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0)
829✔
348
                return false;
349

350
        if (b.type != a->type)
829✔
351
                return false;
352

353
        if (a->protocol != 0)  {
702✔
354
                solen = sizeof(b.protocol);
×
355
                if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0)
×
356
                        return false;
357

358
                if (b.protocol != a->protocol)
×
359
                        return false;
360
        }
361

362
        return socket_address_equal(a, &b);
702✔
363
}
364

365
int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) {
×
366
        const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
×
367

368
        /* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */
369

370
        assert(sa);
×
371

372
        switch (sa->sa.sa_family) {
×
373

374
        case AF_INET:
×
375
                *ret_port = be16toh(sa->in.sin_port);
×
376
                return 0;
×
377

378
        case AF_INET6:
×
379
                *ret_port = be16toh(sa->in6.sin6_port);
×
380
                return 0;
×
381

382
        case AF_VSOCK:
×
383
                *ret_port = sa->vm.svm_port;
×
384
                return 0;
×
385

386
        default:
387
                return -EAFNOSUPPORT;
388
        }
389
}
390

391
const union in_addr_union *sockaddr_in_addr(const struct sockaddr *_sa) {
411✔
392
        const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
411✔
393

394
        if (!sa)
411✔
395
                return NULL;
396

397
        switch (sa->sa.sa_family) {
411✔
398

399
        case AF_INET:
315✔
400
                return (const union in_addr_union*) &sa->in.sin_addr;
315✔
401

402
        case AF_INET6:
96✔
403
                return (const union in_addr_union*) &sa->in6.sin6_addr;
96✔
404

405
        default:
406
                return NULL;
407
        }
408
}
409

410
int sockaddr_set_in_addr(
673✔
411
                union sockaddr_union *u,
412
                int family,
413
                const union in_addr_union *a,
414
                uint16_t port) {
415

416
        assert(u);
673✔
417
        assert(a);
673✔
418

419
        switch (family) {
673✔
420

421
        case AF_INET:
670✔
422
                u->in = (struct sockaddr_in) {
670✔
423
                        .sin_family = AF_INET,
424
                        .sin_addr = a->in,
670✔
425
                        .sin_port = htobe16(port),
670✔
426
                };
427

428
                return 0;
670✔
429

430
        case AF_INET6:
3✔
431
                u->in6 = (struct sockaddr_in6) {
3✔
432
                        .sin6_family = AF_INET6,
433
                        .sin6_addr = a->in6,
3✔
434
                        .sin6_port = htobe16(port),
3✔
435
                };
436

437
                return 0;
3✔
438

439
        default:
440
                return -EAFNOSUPPORT;
441

442
        }
443
}
444

445
int sockaddr_pretty(
9,602✔
446
                const struct sockaddr *_sa,
447
                socklen_t salen,
448
                bool translate_ipv6,
449
                bool include_port,
450
                char **ret) {
451

452
        union sockaddr_union *sa = (union sockaddr_union*) _sa;
9,602✔
453
        char *p;
9,602✔
454
        int r;
9,602✔
455

456
        assert(sa);
9,602✔
457
        assert(salen >= sizeof(sa->sa.sa_family));
9,602✔
458
        assert(ret);
9,602✔
459

460
        switch (sa->sa.sa_family) {
9,602✔
461

462
        case AF_INET: {
129✔
463
                uint32_t a;
129✔
464

465
                a = be32toh(sa->in.sin_addr.s_addr);
129✔
466

467
                if (include_port)
129✔
468
                        r = asprintf(&p,
129✔
469
                                     "%u.%u.%u.%u:%u",
470
                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
471
                                     be16toh(sa->in.sin_port));
129✔
472
                else
473
                        r = asprintf(&p,
×
474
                                     "%u.%u.%u.%u",
475
                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF);
476
                if (r < 0)
129✔
477
                        return -ENOMEM;
9,602✔
478
                break;
479
        }
480

481
        case AF_INET6: {
18✔
482
                static const unsigned char ipv4_prefix[] = {
18✔
483
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
484
                };
485

486
                if (translate_ipv6 &&
18✔
487
                    memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
10✔
488
                        const uint8_t *a = sa->in6.sin6_addr.s6_addr+12;
×
489
                        if (include_port)
×
490
                                r = asprintf(&p,
×
491
                                             "%u.%u.%u.%u:%u",
492
                                             a[0], a[1], a[2], a[3],
×
493
                                             be16toh(sa->in6.sin6_port));
×
494
                        else
495
                                r = asprintf(&p,
×
496
                                             "%u.%u.%u.%u",
497
                                             a[0], a[1], a[2], a[3]);
×
498
                        if (r < 0)
×
499
                                return -ENOMEM;
500
                } else {
501
                        const char *a = IN6_ADDR_TO_STRING(&sa->in6.sin6_addr);
18✔
502

503
                        if (include_port) {
18✔
504
                                if (asprintf(&p,
16✔
505
                                             "[%s]:%u%s%s",
506
                                             a,
507
                                             be16toh(sa->in6.sin6_port),
16✔
508
                                             sa->in6.sin6_scope_id != 0 ? "%" : "",
16✔
509
                                             FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX)) < 0)
16✔
510
                                        return -ENOMEM;
×
511
                        } else {
512
                                if (sa->in6.sin6_scope_id != 0)
2✔
513
                                        p = strjoin(a, "%", FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX));
×
514
                                else
515
                                        p = strdup(a);
2✔
516
                                if (!p)
2✔
517
                                        return -ENOMEM;
518
                        }
519
                }
520

521
                break;
522
        }
523

524
        case AF_UNIX:
9,433✔
525
                if (salen <= offsetof(struct sockaddr_un, sun_path) ||
9,433✔
526
                    (sa->un.sun_path[0] == 0 && salen == offsetof(struct sockaddr_un, sun_path) + 1))
9,431✔
527
                        /* The name must have at least one character (and the leading NUL does not count) */
528
                        p = strdup("<unnamed>");
4✔
529
                else {
530
                        /* Note that we calculate the path pointer here through the .un_buffer[] field, in order to
531
                         * outtrick bounds checking tools such as ubsan, which are too smart for their own good: on
532
                         * Linux the kernel may return sun_path[] data one byte longer than the declared size of the
533
                         * field. */
534
                        char *path = (char*) sa->un_buffer + offsetof(struct sockaddr_un, sun_path);
9,429✔
535
                        size_t path_len = salen - offsetof(struct sockaddr_un, sun_path);
9,429✔
536

537
                        if (path[0] == 0) {
9,429✔
538
                                /* Abstract socket. When parsing address information from, we
539
                                 * explicitly reject overly long paths and paths with embedded NULs.
540
                                 * But we might get such a socket from the outside. Let's return
541
                                 * something meaningful and printable in this case. */
542

543
                                _cleanup_free_ char *e = NULL;
8✔
544

545
                                e = cescape_length(path + 1, path_len - 1);
8✔
546
                                if (!e)
8✔
547
                                        return -ENOMEM;
×
548

549
                                p = strjoin("@", e);
8✔
550
                        } else {
551
                                if (path[path_len - 1] == '\0')
9,421✔
552
                                        /* We expect a terminating NUL and don't print it */
553
                                        path_len--;
9,420✔
554

555
                                p = cescape_length(path, path_len);
9,421✔
556
                        }
557
                }
558
                if (!p)
9,433✔
559
                        return -ENOMEM;
560

561
                break;
562

563
        case AF_VSOCK:
22✔
564
                if (include_port) {
22✔
565
                        if (sa->vm.svm_cid == VMADDR_CID_ANY)
22✔
566
                                r = asprintf(&p, "vsock::%u", sa->vm.svm_port);
19✔
567
                        else
568
                                r = asprintf(&p, "vsock:%u:%u", sa->vm.svm_cid, sa->vm.svm_port);
3✔
569
                } else
570
                        r = asprintf(&p, "vsock:%u", sa->vm.svm_cid);
×
571
                if (r < 0)
22✔
572
                        return -ENOMEM;
573
                break;
574

575
        default:
576
                return -EOPNOTSUPP;
577
        }
578

579
        *ret = p;
9,602✔
580
        return 0;
9,602✔
581
}
582

583
int getpeername_pretty(int fd, bool include_port, char **ret) {
4✔
584
        union sockaddr_union sa;
4✔
585
        socklen_t salen = sizeof(sa);
4✔
586
        int r;
4✔
587

588
        assert(fd >= 0);
4✔
589
        assert(ret);
4✔
590

591
        if (getpeername(fd, &sa.sa, &salen) < 0)
4✔
592
                return -errno;
×
593

594
        if (sa.sa.sa_family == AF_UNIX) {
4✔
595
                struct ucred ucred = UCRED_INVALID;
2✔
596

597
                /* UNIX connection sockets are anonymous, so let's use
598
                 * PID/UID as pretty credentials instead */
599

600
                r = getpeercred(fd, &ucred);
2✔
601
                if (r < 0)
2✔
602
                        return r;
2✔
603

604
                if (asprintf(ret, "PID "PID_FMT"/UID "UID_FMT, ucred.pid, ucred.uid) < 0)
2✔
605
                        return -ENOMEM;
606

607
                return 0;
2✔
608
        }
609

610
        /* For remote sockets we translate IPv6 addresses back to IPv4
611
         * if applicable, since that's nicer. */
612

613
        return sockaddr_pretty(&sa.sa, salen, true, include_port, ret);
2✔
614
}
615

616
int getsockname_pretty(int fd, char **ret) {
1✔
617
        union sockaddr_union sa;
1✔
618
        socklen_t salen = sizeof(sa);
1✔
619

620
        assert(fd >= 0);
1✔
621
        assert(ret);
1✔
622

623
        if (getsockname(fd, &sa.sa, &salen) < 0)
1✔
624
                return -errno;
×
625

626
        /* For local sockets we do not translate IPv6 addresses back
627
         * to IPv6 if applicable, since this is usually used for
628
         * listening sockets where the difference between IPv4 and
629
         * IPv6 matters. */
630

631
        return sockaddr_pretty(&sa.sa, salen, false, true, ret);
1✔
632
}
633

634
int socknameinfo_pretty(const struct sockaddr *sa, socklen_t salen, char **ret) {
×
635
        char host[NI_MAXHOST];
×
636
        int r;
×
637

638
        assert(sa);
×
639
        assert(salen >= sizeof(sa_family_t));
×
640
        assert(ret);
×
641

642
        r = getnameinfo(sa, salen, host, sizeof(host), /* service= */ NULL, /* service_len= */ 0, IDN_FLAGS);
×
643
        if (r != 0) {
×
644
                if (r == EAI_MEMORY)
×
645
                        return log_oom_debug();
×
646
                if (r == EAI_SYSTEM)
×
647
                        log_debug_errno(errno, "getnameinfo() failed, ignoring: %m");
×
648
                else
649
                        log_debug("getnameinfo() failed, ignoring: %s", gai_strerror(r));
×
650

651
                return sockaddr_pretty(sa, salen, /* translate_ipv6= */ true, /* include_port= */ true, ret);
×
652
        }
653

654
        return strdup_to(ret, host);
×
655
}
656

657
static const char* const netlink_family_table[] = {
658
        [NETLINK_ROUTE]          = "route",
659
        [NETLINK_FIREWALL]       = "firewall",
660
        [NETLINK_INET_DIAG]      = "inet-diag",
661
        [NETLINK_NFLOG]          = "nflog",
662
        [NETLINK_XFRM]           = "xfrm",
663
        [NETLINK_SELINUX]        = "selinux",
664
        [NETLINK_ISCSI]          = "iscsi",
665
        [NETLINK_AUDIT]          = "audit",
666
        [NETLINK_FIB_LOOKUP]     = "fib-lookup",
667
        [NETLINK_CONNECTOR]      = "connector",
668
        [NETLINK_NETFILTER]      = "netfilter",
669
        [NETLINK_IP6_FW]         = "ip6-fw",
670
        [NETLINK_DNRTMSG]        = "dnrtmsg",
671
        [NETLINK_KOBJECT_UEVENT] = "kobject-uevent",
672
        [NETLINK_GENERIC]        = "generic",
673
        [NETLINK_SCSITRANSPORT]  = "scsitransport",
674
        [NETLINK_ECRYPTFS]       = "ecryptfs",
675
        [NETLINK_RDMA]           = "rdma",
676
};
677

678
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX);
530✔
679

680
static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = {
681
        [SOCKET_ADDRESS_DEFAULT] = "default",
682
        [SOCKET_ADDRESS_BOTH] = "both",
683
        [SOCKET_ADDRESS_IPV6_ONLY] = "ipv6-only"
684
};
685

686
DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only, SocketAddressBindIPv6Only);
154✔
687

688
SocketAddressBindIPv6Only socket_address_bind_ipv6_only_or_bool_from_string(const char *n) {
×
689
        int r;
×
690

691
        r = parse_boolean(n);
×
692
        if (r > 0)
×
693
                return SOCKET_ADDRESS_IPV6_ONLY;
694
        if (r == 0)
×
695
                return SOCKET_ADDRESS_BOTH;
696

697
        return socket_address_bind_ipv6_only_from_string(n);
×
698
}
699

700
bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) {
7✔
701
        assert(a);
7✔
702
        assert(b);
7✔
703

704
        if (a->sa.sa_family != b->sa.sa_family)
7✔
705
                return false;
706

707
        if (a->sa.sa_family == AF_INET)
6✔
708
                return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr;
4✔
709

710
        if (a->sa.sa_family == AF_INET6)
2✔
711
                return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0;
1✔
712

713
        if (a->sa.sa_family == AF_VSOCK)
1✔
714
                return a->vm.svm_cid == b->vm.svm_cid;
1✔
715

716
        return false;
717
}
718

719
int fd_set_sndbuf(int fd, size_t n, bool increase) {
405,787✔
720
        int r, value;
405,787✔
721
        socklen_t l = sizeof(value);
405,787✔
722

723
        if (n > INT_MAX)
405,787✔
724
                return -ERANGE;
405,787✔
725

726
        r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
405,787✔
727
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
405,787✔
728
                return 0;
729

730
        /* First, try to set the buffer size with SO_SNDBUF. */
731
        r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, n);
405,787✔
732
        if (r < 0)
405,787✔
733
                return r;
734

735
        /* SO_SNDBUF above may set to the kernel limit, instead of the requested size.
736
         * So, we need to check the actual buffer size here. */
737
        l = sizeof(value);
405,787✔
738
        r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
405,787✔
739
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
405,787✔
740
                return 1;
741

742
        /* If we have the privileges we will ignore the kernel limit. */
743
        r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, n);
405,787✔
744
        if (r < 0)
405,787✔
745
                return r;
33,813✔
746

747
        return 1;
748
}
749

750
int fd_set_rcvbuf(int fd, size_t n, bool increase) {
32,889✔
751
        int r, value;
32,889✔
752
        socklen_t l = sizeof(value);
32,889✔
753

754
        if (n > INT_MAX)
32,889✔
755
                return -ERANGE;
32,889✔
756

757
        r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
32,889✔
758
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
32,889✔
759
                return 0;
760

761
        /* First, try to set the buffer size with SO_RCVBUF. */
762
        r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, n);
32,888✔
763
        if (r < 0)
32,888✔
764
                return r;
765

766
        /* SO_RCVBUF above may set to the kernel limit, instead of the requested size.
767
         * So, we need to check the actual buffer size here. */
768
        l = sizeof(value);
32,888✔
769
        r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
32,888✔
770
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
32,888✔
771
                return 1;
772

773
        /* If we have the privileges we will ignore the kernel limit. */
774
        r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, n);
32,888✔
775
        if (r < 0)
32,888✔
776
                return r;
7,241✔
777

778
        return 1;
779
}
780

781
static const char* const ip_tos_table[] = {
782
        [IPTOS_LOWDELAY]    = "low-delay",
783
        [IPTOS_THROUGHPUT]  = "throughput",
784
        [IPTOS_RELIABILITY] = "reliability",
785
        [IPTOS_LOWCOST]     = "low-cost",
786
};
787

788
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
×
789

790
bool ifname_valid_char(char a) {
438,666✔
791
        if ((unsigned char) a >= 127U)
438,666✔
792
                return false;
793

794
        if ((unsigned char) a <= 32U)
438,666✔
795
                return false;
796

797
        if (IN_SET(a,
438,662✔
798
                   ':',  /* colons are used by the legacy "alias" interface logic */
799
                   '/',  /* slashes cannot work, since we need to use network interfaces in sysfs paths, and in paths slashes are separators */
800
                   '%')) /* %d is used in the kernel's weird foo%d format string naming feature which we really really don't want to ever run into by accident */
801
                return false;
10✔
802

803
        return true;
804
}
805

806
bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
125,898✔
807
        bool numeric = true;
125,898✔
808

809
        /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources
810
         * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We
811
         * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */
812

813
        assert(!(flags & ~_IFNAME_VALID_ALL));
125,898✔
814

815
        if (isempty(p))
125,898✔
816
                return false;
817

818
        /* A valid ifindex? If so, it's valid iff IFNAME_VALID_NUMERIC is set */
819
        if (parse_ifindex(p) >= 0)
121,435✔
820
                return flags & IFNAME_VALID_NUMERIC;
57✔
821

822
        if (flags & IFNAME_VALID_ALTERNATIVE) {
121,378✔
823
                if (strlen(p) >= ALTIFNAMSIZ)
11,701✔
824
                        return false;
825
        } else {
826
                if (strlen(p) >= IFNAMSIZ)
109,677✔
827
                        return false;
828
        }
829

830
        if (dot_or_dot_dot(p))
121,371✔
831
                return false;
832

833
        /* Let's refuse "all" and "default" as interface name, to avoid collisions with the special sysctl
834
         * directories /proc/sys/net/{ipv4,ipv6}/conf/{all,default} */
835
        if (!FLAGS_SET(flags, IFNAME_VALID_SPECIAL) && STR_IN_SET(p, "all", "default"))
121,369✔
836
                return false;
×
837

838
        for (const char *t = p; *t; t++) {
553,075✔
839
                if (!ifname_valid_char(*t))
431,720✔
840
                        return false;
841

842
                numeric = numeric && ascii_isdigit(*t);
553,065✔
843
        }
844

845
        /* It's fully numeric but didn't parse as valid ifindex above? if so, it must be too large or zero or
846
         * so, let's refuse that. */
847
        if (numeric)
121,355✔
848
                return false;
2✔
849

850
        return true;
851
}
852

853
bool address_label_valid(const char *p) {
44✔
854

855
        if (isempty(p))
44✔
856
                return false;
857

858
        if (strlen(p) >= IFNAMSIZ)
44✔
859
                return false;
860

861
        while (*p) {
376✔
862
                if ((uint8_t) *p >= 127U)
332✔
863
                        return false;
864

865
                if ((uint8_t) *p <= 31U)
332✔
866
                        return false;
867
                p++;
332✔
868
        }
869

870
        return true;
871
}
872

873
int getpeercred(int fd, struct ucred *ucred) {
65,100✔
874
        socklen_t n = sizeof(struct ucred);
65,100✔
875
        struct ucred u;
65,100✔
876

877
        assert(fd >= 0);
65,100✔
878
        assert(ucred);
65,100✔
879

880
        if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n) < 0)
65,100✔
881
                return -errno;
×
882

883
        if (n != sizeof(struct ucred))
65,100✔
884
                return -EIO;
885

886
        /* Check if the data is actually useful and not suppressed due to namespacing issues */
887
        if (!pid_is_valid(u.pid))
65,100✔
888
                return -ENODATA;
889

890
        /* Note that we don't check UID/GID here, as namespace translation works differently there: instead of
891
         * receiving in "invalid" user/group we get the overflow UID/GID. */
892

893
        *ucred = u;
65,100✔
894
        return 0;
65,100✔
895
}
896

897
int getpeersec(int fd, char **ret) {
19,851✔
898
        _cleanup_free_ char *s = NULL;
39,702✔
899
        socklen_t n = 64;
19,851✔
900

901
        assert(fd >= 0);
19,851✔
902
        assert(ret);
19,851✔
903

904
        for (;;) {
19,851✔
905
                s = new0(char, n+1);
19,851✔
906
                if (!s)
19,851✔
907
                        return -ENOMEM;
908

909
                if (getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n) >= 0) {
19,851✔
910
                        s[n] = 0;
6,991✔
911
                        break;
6,991✔
912
                }
913

914
                if (errno != ERANGE)
12,860✔
915
                        return -errno;
12,860✔
916

917
                s = mfree(s);
×
918
        }
919

920
        if (isempty(s))
6,991✔
921
                return -EOPNOTSUPP;
922

923
        *ret = TAKE_PTR(s);
6,991✔
924

925
        return 0;
6,991✔
926
}
927

928
int getpeergroups(int fd, gid_t **ret) {
19,852✔
929
        socklen_t n = sizeof(gid_t) * 64U;
19,852✔
930
        _cleanup_free_ gid_t *d = NULL;
19,852✔
931

932
        assert(fd >= 0);
19,852✔
933
        assert(ret);
19,852✔
934

935
        long ngroups_max = sysconf(_SC_NGROUPS_MAX);
19,852✔
936
        if (ngroups_max > 0)
19,852✔
937
                n = MAX(n, sizeof(gid_t) * (socklen_t) ngroups_max);
19,852✔
938

939
        for (;;) {
19,852✔
940
                d = malloc(n);
19,852✔
941
                if (!d)
19,852✔
942
                        return -ENOMEM;
943

944
                if (getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, d, &n) >= 0)
19,852✔
945
                        break;
946

947
                if (errno != ERANGE)
×
948
                        return -errno;
×
949

950
                d = mfree(d);
×
951
        }
952

953
        assert_se(n % sizeof(gid_t) == 0);
19,852✔
954
        n /= sizeof(gid_t);
19,852✔
955

956
        if (n > INT_MAX)
19,852✔
957
                return -E2BIG;
958

959
        *ret = TAKE_PTR(d);
19,852✔
960

961
        return (int) n;
19,852✔
962
}
963

964
int getpeerpidfd(int fd) {
20,125✔
965
        socklen_t n = sizeof(int);
20,125✔
966
        int pidfd = -EBADF;
20,125✔
967

968
        assert(fd >= 0);
20,125✔
969

970
        if (getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD, &pidfd, &n) < 0)
20,125✔
971
                return -errno;
2✔
972

973
        if (n != sizeof(int))
20,123✔
974
                return -EIO;
975

976
        return pidfd;
20,123✔
977
}
978

979
int getpeerpidref(int fd, PidRef *ret) {
4✔
980
        int r;
4✔
981

982
        assert(fd >= 0);
4✔
983
        assert(ret);
4✔
984

985
        int pidfd = getpeerpidfd(fd);
4✔
986
        if (pidfd < 0) {
4✔
987
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(pidfd))
×
988
                        return pidfd;
×
989

990
                struct ucred ucred;
×
991
                r = getpeercred(fd, &ucred);
×
992
                if (r < 0)
×
993
                        return r;
994

995
                return pidref_set_pid(ret, ucred.pid);
×
996
        }
997

998
        return pidref_set_pidfd_consume(ret, pidfd);
4✔
999
}
1000

1001
ssize_t send_many_fds_iov_sa(
1✔
1002
                int transport_fd,
1003
                int *fds_array, size_t n_fds_array,
1004
                const struct iovec *iov, size_t iovlen,
1005
                const struct sockaddr *sa, socklen_t len,
1006
                int flags) {
1007

1008
        _cleanup_free_ struct cmsghdr *cmsg = NULL;
1✔
1009
        struct msghdr mh = {
1✔
1010
                .msg_name = (struct sockaddr*) sa,
1011
                .msg_namelen = len,
1012
                .msg_iov = (struct iovec *)iov,
1013
                .msg_iovlen = iovlen,
1014
        };
1015
        ssize_t k;
1✔
1016

1017
        assert(transport_fd >= 0);
1✔
1018
        assert(fds_array || n_fds_array == 0);
1✔
1019

1020
        /* The kernel will reject sending more than SCM_MAX_FD FDs at once */
1021
        if (n_fds_array > SCM_MAX_FD)
1✔
1022
                return -E2BIG;
1023

1024
        /* We need either an FD array or data to send. If there's nothing, return an error. */
1025
        if (n_fds_array == 0 && !iov)
1✔
1026
                return -EINVAL;
1027

1028
        if (n_fds_array > 0) {
1✔
1029
                mh.msg_controllen = CMSG_SPACE(sizeof(int) * n_fds_array);
1✔
1030
                mh.msg_control = cmsg = malloc(mh.msg_controllen);
1✔
1031
                if (!cmsg)
1✔
1032
                        return -ENOMEM;
1033

1034
                *cmsg = (struct cmsghdr) {
1✔
1035
                        .cmsg_len = CMSG_LEN(sizeof(int) * n_fds_array),
1✔
1036
                        .cmsg_level = SOL_SOCKET,
1037
                        .cmsg_type = SCM_RIGHTS,
1038
                };
1039
                memcpy(CMSG_DATA(cmsg), fds_array, sizeof(int) * n_fds_array);
1✔
1040
        }
1041
        k = sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags);
1✔
1042
        if (k < 0)
1✔
1043
                return (ssize_t) -errno;
×
1044

1045
        return k;
1046
}
1047

1048
ssize_t send_one_fd_iov_sa(
741✔
1049
                int transport_fd,
1050
                int fd,
1051
                const struct iovec *iov, size_t iovlen,
1052
                const struct sockaddr *sa, socklen_t len,
1053
                int flags) {
1054

1055
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control = {};
741✔
1056
        struct msghdr mh = {
741✔
1057
                .msg_name = (struct sockaddr*) sa,
1058
                .msg_namelen = len,
1059
                .msg_iov = (struct iovec *)iov,
1060
                .msg_iovlen = iovlen,
1061
        };
1062
        ssize_t k;
741✔
1063

1064
        assert(transport_fd >= 0);
741✔
1065

1066
        /*
1067
         * We need either an FD or data to send.
1068
         * If there's nothing, return an error.
1069
         */
1070
        if (fd < 0 && !iov)
741✔
1071
                return -EINVAL;
741✔
1072

1073
        if (fd >= 0) {
740✔
1074
                struct cmsghdr *cmsg;
738✔
1075

1076
                mh.msg_control = &control;
738✔
1077
                mh.msg_controllen = sizeof(control);
738✔
1078

1079
                cmsg = CMSG_FIRSTHDR(&mh);
738✔
1080
                cmsg->cmsg_level = SOL_SOCKET;
738✔
1081
                cmsg->cmsg_type = SCM_RIGHTS;
738✔
1082
                cmsg->cmsg_len = CMSG_LEN(sizeof(int));
738✔
1083
                memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
738✔
1084
        }
1085
        k = sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags);
740✔
1086
        if (k < 0)
740✔
1087
                return (ssize_t) -errno;
×
1088

1089
        return k;
1090
}
1091

1092
int send_one_fd_sa(
6✔
1093
                int transport_fd,
1094
                int fd,
1095
                const struct sockaddr *sa, socklen_t len,
1096
                int flags) {
1097

1098
        assert(fd >= 0);
6✔
1099

1100
        return (int) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, sa, len, flags);
6✔
1101
}
1102

1103
ssize_t receive_many_fds_iov(
1✔
1104
                int transport_fd,
1105
                struct iovec *iov, size_t iovlen,
1106
                int **ret_fds_array, size_t *ret_n_fds_array,
1107
                int flags) {
1108

1109
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int) * SCM_MAX_FD)) control;
1✔
1110
        struct msghdr mh = {
1✔
1111
                .msg_control = &control,
1112
                .msg_controllen = sizeof(control),
1113
                .msg_iov = iov,
1114
                .msg_iovlen = iovlen,
1115
        };
1116
        _cleanup_free_ int *fds_array = NULL;
1✔
1117
        size_t n_fds_array = 0;
1✔
1118
        struct cmsghdr *cmsg;
1✔
1119
        ssize_t k;
1✔
1120

1121
        assert(transport_fd >= 0);
1✔
1122
        assert(ret_fds_array);
1✔
1123
        assert(ret_n_fds_array);
1✔
1124

1125
        /*
1126
         * Receive many FDs via @transport_fd. We don't care for the transport-type. We retrieve all the FDs
1127
         * at once. This is best used in combination with send_many_fds().
1128
         */
1129

1130
        k = recvmsg_safe(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags);
1✔
1131
        if (k < 0)
1✔
1132
                return k;
1133

1134
        CMSG_FOREACH(cmsg, &mh)
4✔
1135
                if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
1✔
1136
                        size_t n = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
1✔
1137

1138
                        if (!GREEDY_REALLOC_APPEND(fds_array, n_fds_array, CMSG_TYPED_DATA(cmsg, int), n)) {
1✔
1139
                                cmsg_close_all(&mh);
×
1140
                                return -ENOMEM;
1141
                        }
1142
                }
1143

1144
        if (n_fds_array == 0) {
1✔
1145
                cmsg_close_all(&mh);
×
1146

1147
                /* If didn't receive an FD or any data, return an error. */
1148
                if (k == 0)
×
1149
                        return -EIO;
1150
        }
1151

1152
        *ret_fds_array = TAKE_PTR(fds_array);
1✔
1153
        *ret_n_fds_array = n_fds_array;
1✔
1154

1155
        return k;
1✔
1156
}
1157

1158
int receive_many_fds(int transport_fd, int **ret_fds_array, size_t *ret_n_fds_array, int flags) {
×
1159
        ssize_t k;
×
1160

1161
        k = receive_many_fds_iov(transport_fd, NULL, 0, ret_fds_array, ret_n_fds_array, flags);
×
1162
        if (k == 0)
×
1163
                return 0;
1164

1165
        /* k must be negative, since receive_many_fds_iov() only returns a positive value if data was received
1166
         * through the iov. */
1167
        assert(k < 0);
×
1168
        return (int) k;
×
1169
}
1170

1171
ssize_t receive_one_fd_iov(
5,101✔
1172
                int transport_fd,
1173
                struct iovec *iov, size_t iovlen,
1174
                int flags,
1175
                int *ret_fd) {
1176

1177
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control;
5,101✔
1178
        struct msghdr mh = {
5,101✔
1179
                .msg_control = &control,
1180
                .msg_controllen = sizeof(control),
1181
                .msg_iov = iov,
1182
                .msg_iovlen = iovlen,
1183
        };
1184
        struct cmsghdr *found;
5,101✔
1185
        ssize_t k;
5,101✔
1186

1187
        assert(transport_fd >= 0);
5,101✔
1188
        assert(ret_fd);
5,101✔
1189

1190
        /*
1191
         * Receive a single FD via @transport_fd. We don't care for
1192
         * the transport-type. We retrieve a single FD at most, so for
1193
         * packet-based transports, the caller must ensure to send
1194
         * only a single FD per packet.  This is best used in
1195
         * combination with send_one_fd().
1196
         */
1197

1198
        k = recvmsg_safe(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags);
5,101✔
1199
        if (k < 0)
5,101✔
1200
                return k;
5,101✔
1201

1202
        found = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, CMSG_LEN(sizeof(int)));
4,920✔
1203
        if (!found) {
4,920✔
1204
                cmsg_close_all(&mh);
1,542✔
1205

1206
                /* If didn't receive an FD or any data, return an error. */
1207
                if (k == 0)
1,542✔
1208
                        return -EIO;
1209
        }
1210

1211
        if (found)
3,379✔
1212
                *ret_fd = *CMSG_TYPED_DATA(found, int);
3,378✔
1213
        else
1214
                *ret_fd = -EBADF;
1✔
1215

1216
        return k;
1217
}
1218

1219
int receive_one_fd(int transport_fd, int flags) {
587✔
1220
        int fd;
587✔
1221
        ssize_t k;
587✔
1222

1223
        k = receive_one_fd_iov(transport_fd, NULL, 0, flags, &fd);
587✔
1224
        if (k == 0)
587✔
1225
                return fd;
519✔
1226

1227
        /* k must be negative, since receive_one_fd_iov() only returns
1228
         * a positive value if data was received through the iov. */
1229
        assert(k < 0);
68✔
1230
        return (int) k;
68✔
1231
}
1232

1233
ssize_t next_datagram_size_fd(int fd) {
198,091✔
1234
        ssize_t l;
198,091✔
1235
        int k;
198,091✔
1236

1237
        /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will
1238
         * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD doesn't
1239
         * do. This difference is actually of major importance as we need to be sure that the size returned here
1240
         * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of
1241
         * the wrong size. */
1242

1243
        l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC);
198,091✔
1244
        if (l < 0) {
198,091✔
1245
                if (IN_SET(errno, EOPNOTSUPP, EFAULT))
551✔
1246
                        goto fallback;
×
1247

1248
                return -errno;
551✔
1249
        }
1250
        if (l == 0)
197,540✔
1251
                goto fallback;
65✔
1252

1253
        return l;
1254

1255
fallback:
65✔
1256
        k = 0;
65✔
1257

1258
        /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD
1259
         * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */
1260

1261
        if (ioctl(fd, FIONREAD, &k) < 0)
65✔
1262
                return -errno;
×
1263

1264
        return (ssize_t) k;
65✔
1265
}
1266

1267
/* Put a limit on how many times will attempt to call accept4(). We loop
1268
 * only on "transient" errors, but let's make sure we don't loop forever. */
1269
#define MAX_FLUSH_ITERATIONS 1024
1270

1271
int flush_accept(int fd) {
16✔
1272

1273
        int r, b;
16✔
1274
        socklen_t l = sizeof(b);
16✔
1275

1276
        /* Similar to flush_fd() but flushes all incoming connections by accepting and immediately closing
1277
         * them. */
1278

1279
        if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &b, &l) < 0)
16✔
1280
                return -errno;
×
1281

1282
        assert(l == sizeof(b));
16✔
1283
        if (!b) /* Let's check if this socket accepts connections before calling accept(). accept4() can
16✔
1284
                 * return EOPNOTSUPP if the fd is not a listening socket, which we should treat as a fatal
1285
                 * error, or in case the incoming TCP connection triggered a network issue, which we want to
1286
                 * treat as a transient error. Thus, let's rule out the first reason for EOPNOTSUPP early, so
1287
                 * we can loop safely on transient errors below. */
1288
                return -ENOTTY;
1289

1290
        for (unsigned iteration = 0;; iteration++) {
2✔
1291
                int cfd;
6✔
1292

1293
                r = fd_wait_for_event(fd, POLLIN, 0);
6✔
1294
                if (r < 0) {
6✔
1295
                        if (r == -EINTR)
×
1296
                                continue;
×
1297

1298
                        return r;
1299
                }
1300
                if (r == 0)
6✔
1301
                        return 0;
1302

1303
                if (iteration >= MAX_FLUSH_ITERATIONS)
2✔
1304
                        return log_debug_errno(SYNTHETIC_ERRNO(EBUSY),
×
1305
                                               "Failed to flush connections within " STRINGIFY(MAX_FLUSH_ITERATIONS) " iterations.");
1306

1307
                cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
2✔
1308
                if (cfd < 0) {
2✔
1309
                        if (errno == EAGAIN)
×
1310
                                return 0;
1311

1312
                        if (ERRNO_IS_ACCEPT_AGAIN(errno))
×
1313
                                continue;
×
1314

1315
                        return -errno;
×
1316
                }
1317

1318
                safe_close(cfd);
2✔
1319
        }
1320
}
1321

1322
struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length) {
364,200✔
1323
        struct cmsghdr *cmsg;
364,200✔
1324

1325
        assert(mh);
364,200✔
1326

1327
        CMSG_FOREACH(cmsg, mh)
728,936✔
1328
                if (cmsg->cmsg_level == level &&
342,817✔
1329
                    cmsg->cmsg_type == type &&
342,549✔
1330
                    (length == (socklen_t) -1 || length == cmsg->cmsg_len))
341,780✔
1331
                        return cmsg;
1332

1333
        return NULL;
1334
}
1335

1336
void* cmsg_find_and_copy_data(struct msghdr *mh, int level, int type, void *buf, size_t buf_len) {
299✔
1337
        struct cmsghdr *cmsg;
299✔
1338

1339
        assert(mh);
299✔
1340
        assert(buf);
299✔
1341
        assert(buf_len > 0);
299✔
1342

1343
        /* This is similar to cmsg_find_data(), but copy the found data to buf. This should be typically used
1344
         * when reading possibly unaligned data such as timestamp, as time_t is 64-bit and size_t is 32-bit on
1345
         * RISCV32. See issue #27241. */
1346

1347
        cmsg = cmsg_find(mh, level, type, CMSG_LEN(buf_len));
299✔
1348
        if (!cmsg)
299✔
1349
                return NULL;
1350

1351
        return memcpy_safe(buf, CMSG_DATA(cmsg), buf_len);
294✔
1352
}
1353

1354
int socket_ioctl_fd(void) {
4,322✔
1355
        int fd;
4,322✔
1356

1357
        /* Create a socket to invoke the various network interface ioctl()s on. Traditionally only AF_INET was good for
1358
         * that. Since kernel 4.6 AF_NETLINK works for this too. We first try to use AF_INET hence, but if that's not
1359
         * available (for example, because it is made unavailable via SECCOMP or such), we'll fall back to the more
1360
         * generic AF_NETLINK. */
1361

1362
        fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
4,322✔
1363
        if (fd < 0)
4,322✔
1364
                fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_GENERIC);
×
1365
        if (fd < 0)
×
1366
                return -errno;
×
1367

1368
        return fd;
1369
}
1370

1371
int sockaddr_un_unlink(const struct sockaddr_un *sa) {
1,779✔
1372
        const char *p, * nul;
1,779✔
1373

1374
        assert(sa);
1,779✔
1375

1376
        if (sa->sun_family != AF_UNIX)
1,779✔
1377
                return -EPROTOTYPE;
1378

1379
        if (sa->sun_path[0] == 0) /* Nothing to do for abstract sockets */
1,779✔
1380
                return 0;
1381

1382
        /* The path in .sun_path is not necessarily NUL terminated. Let's fix that. */
1383
        nul = memchr(sa->sun_path, 0, sizeof(sa->sun_path));
1,779✔
1384
        if (nul)
1,779✔
1385
                p = sa->sun_path;
1386
        else
1387
                p = memdupa_suffix0(sa->sun_path, sizeof(sa->sun_path));
×
1388

1389
        if (unlink(p) < 0)
1,779✔
1390
                return -errno;
1,539✔
1391

1392
        return 1;
1393
}
1394

1395
int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path) {
363,970✔
1396
        size_t l;
363,970✔
1397

1398
        assert(ret);
363,970✔
1399
        assert(path);
363,970✔
1400

1401
        /* Initialize ret->sun_path from the specified argument. This will interpret paths starting with '@' as
1402
         * abstract namespace sockets, and those starting with '/' as regular filesystem sockets. It won't accept
1403
         * anything else (i.e. no relative paths), to avoid ambiguities. Note that this function cannot be used to
1404
         * reference paths in the abstract namespace that include NUL bytes in the name. */
1405

1406
        l = strlen(path);
363,970✔
1407
        if (l < 2)
363,970✔
1408
                return -EINVAL;
1409
        if (!IN_SET(path[0], '/', '@'))
363,969✔
1410
                return -EINVAL;
1411

1412
        /* Don't allow paths larger than the space in sockaddr_un. Note that we are a tiny bit more restrictive than
1413
         * the kernel is: we insist on NUL termination (both for abstract namespace and regular file system socket
1414
         * addresses!), which the kernel doesn't. We do this to reduce chance of incompatibility with other apps that
1415
         * do not expect non-NUL terminated file system path. */
1416
        if (l+1 > sizeof(ret->sun_path))
363,969✔
1417
                return path[0] == '@' ? -EINVAL : -ENAMETOOLONG; /* return a recognizable error if this is
2✔
1418
                                                                  * too long to fit into a sockaddr_un, but
1419
                                                                  * is a file system path, and thus might be
1420
                                                                  * connectible via O_PATH indirection. */
1421

1422
        *ret = (struct sockaddr_un) {
363,967✔
1423
                .sun_family = AF_UNIX,
1424
        };
1425

1426
        if (path[0] == '@') {
363,967✔
1427
                /* Abstract namespace socket */
1428
                memcpy(ret->sun_path + 1, path + 1, l); /* copy *with* trailing NUL byte */
209,159✔
1429
                return (int) (offsetof(struct sockaddr_un, sun_path) + l); /* 🔥 *don't* 🔥 include trailing NUL in size */
209,159✔
1430

1431
        } else {
1432
                assert(path[0] == '/');
154,808✔
1433

1434
                /* File system socket */
1435
                memcpy(ret->sun_path, path, l + 1); /* copy *with* trailing NUL byte */
154,808✔
1436
                return (int) (offsetof(struct sockaddr_un, sun_path) + l + 1); /* include trailing NUL in size */
154,808✔
1437
        }
1438
}
1439

1440
int socket_bind_to_ifname(int fd, const char *ifname) {
1✔
1441
        assert(fd >= 0);
1✔
1442

1443
        /* Call with NULL to drop binding */
1444

1445
        return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen_ptr(ifname)));
2✔
1446
}
1447

1448
int socket_bind_to_ifindex(int fd, int ifindex) {
713✔
1449
        assert(fd >= 0);
713✔
1450

1451
        if (ifindex <= 0)
713✔
1452
                /* Drop binding */
1453
                return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, NULL, 0));
60✔
1454

1455
        return setsockopt_int(fd, SOL_SOCKET, SO_BINDTOIFINDEX, ifindex);
653✔
1456
}
1457

1458
int socket_autobind(int fd, char **ret_name) {
238✔
1459
        _cleanup_free_ char *name = NULL;
238✔
1460
        uint64_t random;
238✔
1461
        int r;
238✔
1462

1463
        /* Generate a random abstract socket name and bind fd to it. This is modeled after the kernel
1464
         * "autobind" feature, but uses 64-bit random number internally. */
1465

1466
        assert(fd >= 0);
238✔
1467
        assert(ret_name);
238✔
1468

1469
        random = random_u64();
238✔
1470

1471
        if (asprintf(&name, "@%" PRIu64, random) < 0)
238✔
1472
                return -ENOMEM;
1473

1474
        union sockaddr_union sa;
238✔
1475
        assert_cc(DECIMAL_STR_MAX(uint64_t) < sizeof(sa.un.sun_path));
238✔
1476

1477
        r = sockaddr_un_set_path(&sa.un, name);
238✔
1478
        if (r < 0)
238✔
1479
                return r;
1480

1481
        if (bind(fd, &sa.sa, r) < 0)
238✔
1482
                return -errno;
×
1483

1484
        *ret_name = TAKE_PTR(name);
238✔
1485
        return 0;
238✔
1486
}
1487

1488
ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags) {
2,836,390✔
1489
        ssize_t n;
2,836,390✔
1490

1491
        /* A wrapper around recvmsg() that checks for MSG_CTRUNC and MSG_TRUNC, and turns them into an error,
1492
         * in a reasonably safe way, closing any received fds in the error path.
1493
         *
1494
         * Note that unlike our usual coding style this might modify *msg on failure. */
1495

1496
        assert(sockfd >= 0);
2,836,390✔
1497
        assert(msg);
2,836,390✔
1498

1499
        n = recvmsg(sockfd, msg, flags);
2,836,390✔
1500
        if (n < 0)
2,836,390✔
1501
                return -errno;
268,451✔
1502

1503
        if (FLAGS_SET(msg->msg_flags, MSG_CTRUNC) ||
2,567,939✔
1504
            (!FLAGS_SET(flags, MSG_PEEK) && FLAGS_SET(msg->msg_flags, MSG_TRUNC))) {
2,567,939✔
1505
                cmsg_close_all(msg);
×
1506
                return FLAGS_SET(msg->msg_flags, MSG_CTRUNC) ? -ECHRNG : -EXFULL;
×
1507
        }
1508

1509
        return n;
1510
}
1511

1512
int socket_get_family(int fd) {
18,838✔
1513
        int af;
18,838✔
1514
        socklen_t sl = sizeof(af);
18,838✔
1515

1516
        if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &sl) < 0)
18,838✔
1517
                return -errno;
×
1518

1519
        if (sl != sizeof(af))
18,838✔
1520
                return -EINVAL;
1521

1522
        return af;
18,838✔
1523
}
1524

1525
int socket_set_recvpktinfo(int fd, int af, bool b) {
13,340✔
1526

1527
        if (af == AF_UNSPEC) {
13,340✔
1528
                af = socket_get_family(fd);
×
1529
                if (af < 0)
×
1530
                        return af;
1531
        }
1532

1533
        switch (af) {
13,340✔
1534

1535
        case AF_INET:
7,008✔
1536
                return setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, b);
7,008✔
1537

1538
        case AF_INET6:
6,299✔
1539
                return setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, b);
6,299✔
1540

1541
        case AF_NETLINK:
33✔
1542
                return setsockopt_int(fd, SOL_NETLINK, NETLINK_PKTINFO, b);
33✔
1543

1544
        case AF_PACKET:
×
1545
                return setsockopt_int(fd, SOL_PACKET, PACKET_AUXDATA, b);
×
1546

1547
        default:
1548
                return -EAFNOSUPPORT;
1549
        }
1550
}
1551

1552
int socket_set_unicast_if(int fd, int af, int ifi) {
130✔
1553
        be32_t ifindex_be = htobe32(ifi);
130✔
1554

1555
        if (af == AF_UNSPEC) {
130✔
1556
                af = socket_get_family(fd);
×
1557
                if (af < 0)
×
1558
                        return af;
130✔
1559
        }
1560

1561
        switch (af) {
130✔
1562

1563
        case AF_INET:
98✔
1564
                return RET_NERRNO(setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
98✔
1565

1566
        case AF_INET6:
32✔
1567
                return RET_NERRNO(setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
32✔
1568

1569
        default:
1570
                return -EAFNOSUPPORT;
1571
        }
1572
}
1573

1574
int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val) {
26,497✔
1575
        if (af == AF_UNSPEC) {
26,497✔
1576
                af = socket_get_family(fd);
×
1577
                if (af < 0)
×
1578
                        return af;
1579
        }
1580

1581
        switch (af) {
26,497✔
1582

1583
        case AF_INET:
13,891✔
1584
                return setsockopt_int(fd, IPPROTO_IP, opt_ipv4, val);
13,891✔
1585

1586
        case AF_INET6:
12,606✔
1587
                return setsockopt_int(fd, IPPROTO_IPV6, opt_ipv6, val);
12,606✔
1588

1589
        default:
1590
                return -EAFNOSUPPORT;
1591
        }
1592
}
1593

1594
int socket_get_mtu(int fd, int af, size_t *ret) {
678✔
1595
        int mtu, r;
678✔
1596

1597
        if (af == AF_UNSPEC) {
678✔
1598
                af = socket_get_family(fd);
×
1599
                if (af < 0)
×
1600
                        return af;
678✔
1601
        }
1602

1603
        switch (af) {
678✔
1604

1605
        case AF_INET:
320✔
1606
                r = getsockopt_int(fd, IPPROTO_IP, IP_MTU, &mtu);
320✔
1607
                break;
1608

1609
        case AF_INET6:
358✔
1610
                r = getsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, &mtu);
358✔
1611
                break;
1612

1613
        default:
1614
                return -EAFNOSUPPORT;
1615
        }
1616

1617
        if (r < 0)
678✔
1618
                return r;
1619
        if (mtu <= 0)
618✔
1620
                return -EINVAL;
1621

1622
        *ret = (size_t) mtu;
618✔
1623
        return 0;
618✔
1624
}
1625

1626
static int connect_unix_path_simple(int fd, const char *path) {
170,731✔
1627
        union sockaddr_union sa = {
170,731✔
1628
                .un.sun_family = AF_UNIX,
1629
        };
1630
        size_t l;
170,731✔
1631

1632
        assert(fd >= 0);
170,731✔
1633
        assert(path);
170,731✔
1634

1635
        l = strlen(path);
170,731✔
1636
        assert(l > 0);
170,731✔
1637
        assert(l < sizeof(sa.un.sun_path));
170,731✔
1638

1639
        memcpy(sa.un.sun_path, path, l + 1);
170,731✔
1640
        return RET_NERRNO(connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + l + 1));
170,731✔
1641
}
1642

1643
static int connect_unix_inode(int fd, int inode_fd) {
14✔
1644
        assert(fd >= 0);
14✔
1645
        assert(inode_fd >= 0);
14✔
1646

1647
        return connect_unix_path_simple(fd, FORMAT_PROC_FD_PATH(inode_fd));
14✔
1648
}
1649

1650
int connect_unix_path(int fd, int dir_fd, const char *path) {
170,731✔
1651
        _cleanup_close_ int inode_fd = -EBADF;
170,731✔
1652

1653
        assert(fd >= 0);
170,731✔
1654
        assert(dir_fd == AT_FDCWD || dir_fd >= 0);
170,731✔
1655

1656
        /* Connects to the specified AF_UNIX socket in the file system. Works around the 108 byte size limit
1657
         * in sockaddr_un, by going via O_PATH if needed. This hence works for any kind of path. */
1658

1659
        if (!path)
170,731✔
1660
                return connect_unix_inode(fd, dir_fd); /* If no path is specified, then dir_fd refers to the socket inode to connect to. */
1✔
1661

1662
        /* Refuse zero length path early, to make sure AF_UNIX stack won't mistake this for an abstract
1663
         * namespace path, since first char is NUL */
1664
        if (isempty(path))
341,461✔
1665
                return -EINVAL;
1666

1667
        /* Shortcut for the simple case */
1668
        if (dir_fd == AT_FDCWD && strlen(path) < sizeof_field(struct sockaddr_un, sun_path))
170,730✔
1669
                return connect_unix_path_simple(fd, path);
170,717✔
1670

1671
        /* If dir_fd is specified, then we need to go the indirect O_PATH route, because connectat() does not
1672
         * exist. If the path is too long, we also need to take the indirect route, since we can't fit this
1673
         * into a sockaddr_un directly. */
1674

1675
        inode_fd = openat(dir_fd, path, O_PATH|O_CLOEXEC);
13✔
1676
        if (inode_fd < 0)
13✔
1677
                return -errno;
×
1678

1679
        return connect_unix_inode(fd, inode_fd);
13✔
1680
}
1681

1682
int socket_address_parse_unix(SocketAddress *ret_address, const char *s) {
213,286✔
1683
        struct sockaddr_un un;
213,286✔
1684
        int r;
213,286✔
1685

1686
        assert(ret_address);
213,286✔
1687
        assert(s);
213,286✔
1688

1689
        if (!IN_SET(*s, '/', '@'))
213,286✔
1690
                return -EPROTO;
213,286✔
1691

1692
        r = sockaddr_un_set_path(&un, s);
212,847✔
1693
        if (r < 0)
212,847✔
1694
                return r;
1695

1696
        *ret_address = (SocketAddress) {
212,845✔
1697
                .sockaddr.un = un,
1698
                .size = r,
1699
        };
1700

1701
        return 0;
212,845✔
1702
}
1703

1704
int vsock_parse_port(const char *s, unsigned *ret) {
371✔
1705
        int r;
371✔
1706

1707
        assert(ret);
371✔
1708

1709
        if (!s)
371✔
1710
                return -EINVAL;
371✔
1711

1712
        unsigned u;
371✔
1713
        r = safe_atou(s, &u);
371✔
1714
        if (r < 0)
371✔
1715
                return r;
1716

1717
        /* Port 0 is apparently valid and not special in AF_VSOCK (unlike on IP). But VMADDR_PORT_ANY
1718
         * (UINT32_MAX) is. Hence refuse that. */
1719

1720
        if (u == VMADDR_PORT_ANY)
368✔
1721
                return -EINVAL;
1722

1723
        *ret = u;
368✔
1724
        return 0;
368✔
1725
}
1726

1727
int vsock_parse_cid(const char *s, unsigned *ret) {
331✔
1728
        assert(ret);
331✔
1729

1730
        if (!s)
331✔
1731
                return -EINVAL;
1732

1733
        /* Parsed an AF_VSOCK "CID". This is a 32bit entity, and the usual type is "unsigned". We recognize
1734
         * the three special CIDs as strings, and otherwise parse the numeric CIDs. */
1735

1736
        if (streq(s, "hypervisor"))
331✔
1737
                *ret = VMADDR_CID_HYPERVISOR;
×
1738
        else if (streq(s, "local"))
331✔
1739
                *ret = VMADDR_CID_LOCAL;
×
1740
        else if (streq(s, "host"))
331✔
1741
                *ret = VMADDR_CID_HOST;
×
1742
        else
1743
                return safe_atou(s, ret);
331✔
1744

1745
        return 0;
1746
}
1747

1748
int socket_address_parse_vsock(SocketAddress *ret_address, const char *s) {
439✔
1749
        /* AF_VSOCK socket in vsock:cid:port notation */
1750
        _cleanup_free_ char *n = NULL;
439✔
1751
        char *e, *cid_start;
439✔
1752
        unsigned port, cid;
439✔
1753
        int type, r;
439✔
1754

1755
        assert(ret_address);
439✔
1756
        assert(s);
439✔
1757

1758
        if ((cid_start = startswith(s, "vsock:")))
439✔
1759
                type = 0;
1760
        else if ((cid_start = startswith(s, "vsock-dgram:")))
387✔
1761
                type = SOCK_DGRAM;
1762
        else if ((cid_start = startswith(s, "vsock-seqpacket:")))
387✔
1763
                type = SOCK_SEQPACKET;
1764
        else if ((cid_start = startswith(s, "vsock-stream:")))
387✔
1765
                type = SOCK_STREAM;
1766
        else
1767
                return -EPROTO;
1768

1769
        e = strchr(cid_start, ':');
373✔
1770
        if (!e)
373✔
1771
                return -EINVAL;
1772

1773
        r = vsock_parse_port(e+1, &port);
371✔
1774
        if (r < 0)
371✔
1775
                return r;
1776

1777
        n = strndup(cid_start, e - cid_start);
368✔
1778
        if (!n)
368✔
1779
                return -ENOMEM;
1780

1781
        if (isempty(n))
368✔
1782
                cid = VMADDR_CID_ANY;
37✔
1783
        else {
1784
                r = vsock_parse_cid(n, &cid);
331✔
1785
                if (r < 0)
331✔
1786
                        return r;
1787
        }
1788

1789
        *ret_address = (SocketAddress) {
365✔
1790
                .sockaddr.vm = {
1791
                        .svm_family = AF_VSOCK,
1792
                        .svm_cid = cid,
1793
                        .svm_port = port,
1794
                },
1795
                .type = type,
1796
                .size = sizeof(struct sockaddr_vm),
1797
        };
1798

1799
        return 0;
365✔
1800
}
1801

1802
int vsock_get_local_cid(unsigned *ret) {
16✔
1803
        _cleanup_close_ int vsock_fd = -EBADF;
16✔
1804

1805
        vsock_fd = open("/dev/vsock", O_RDONLY|O_CLOEXEC);
16✔
1806
        if (vsock_fd < 0)
16✔
1807
                return log_debug_errno(errno, "Failed to open /dev/vsock: %m");
13✔
1808

1809
        unsigned tmp;
3✔
1810
        if (ioctl(vsock_fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, ret ?: &tmp) < 0)
6✔
1811
                return log_debug_errno(errno, "Failed to query local AF_VSOCK CID: %m");
×
1812

1813
        return 0;
1814
}
1815

1816
int netlink_socket_get_multicast_groups(int fd, size_t *ret_len, uint32_t **ret_groups) {
12,011✔
1817
        _cleanup_free_ uint32_t *groups = NULL;
24,022✔
1818
        socklen_t len = 0, old_len;
12,011✔
1819

1820
        assert(fd >= 0);
12,011✔
1821

1822
        if (getsockopt(fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, NULL, &len) < 0)
12,011✔
UNCOV
1823
                return -errno;
×
1824

1825
        if (len == 0)
12,011✔
1826
                goto finalize;
11,594✔
1827

1828
        groups = new0(uint32_t, len);
417✔
1829
        if (!groups)
417✔
1830
                return -ENOMEM;
1831

1832
        old_len = len;
417✔
1833

1834
        if (getsockopt(fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, groups, &len) < 0)
417✔
UNCOV
1835
                return -errno;
×
1836

1837
        if (old_len != len)
417✔
1838
                return -EIO;
1839

1840
finalize:
417✔
1841
        if (ret_len)
12,011✔
1842
                *ret_len = len;
12,011✔
1843
        if (ret_groups)
12,011✔
1844
                *ret_groups = TAKE_PTR(groups);
12,011✔
1845

1846
        return 0;
1847
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc