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

systemd / systemd / 25351067578

04 May 2026 06:45PM UTC coverage: 72.674% (+0.7%) from 71.943%
25351067578

push

github

keszybz
test: suppress PCR public key auto-loading in TEST-70-TPM2 dditest

The dditest block calls systemd-repart with Encrypt=tpm2 but without
--tpm2-public-key-pcrs=. Since systemd-stub drops
/run/systemd/tpm2-pcr-public-key.pem when booting from a signed UKI
systemd-repart auto-loads it and enrolls a signed PCR policy, and
then systemd-cryptsetup tpm2-device=auto has no matching signature file,
so unlock fails.

--tpm2-public-key= is not enough as the default kicks in then.

Follow-up for cd18656d4

325892 of 448432 relevant lines covered (72.67%)

1195556.07 hits per line

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

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

3
#include <fcntl.h>
4
#include <linux/if.h>
5
#include <linux/if_arp.h>
6
#include <linux/pkt_sched.h>
7
#include <mqueue.h>
8
#include <net/if.h>
9
#include <netdb.h>
10
#include <netinet/ip.h>
11
#include <poll.h>
12
#include <stdio.h>
13
#include <sys/ioctl.h>
14
#include <unistd.h>
15

16
#include "alloc-util.h"
17
#include "errno-util.h"
18
#include "escape.h"
19
#include "fd-util.h"
20
#include "format-ifname.h"
21
#include "format-util.h"
22
#include "in-addr-util.h"
23
#include "io-util.h"
24
#include "log.h"
25
#include "memory-util.h"
26
#include "parse-util.h"
27
#include "path-util.h"
28
#include "pidref.h"
29
#include "process-util.h"
30
#include "random-util.h"
31
#include "socket-util.h"
32
#include "sparse-endian.h"
33
#include "string-table.h"
34
#include "string-util.h"
35
#include "strv.h"
36
#include "sysctl-util.h"
37

38
#if ENABLE_IDN
39
#  define IDN_FLAGS NI_IDN
40
#else
41
#  define IDN_FLAGS 0
42
#endif
43

44
static const char* const socket_address_type_table[] = {
45
        [SOCK_STREAM] =    "Stream",
46
        [SOCK_DGRAM] =     "Datagram",
47
        [SOCK_RAW] =       "Raw",
48
        [SOCK_RDM] =       "ReliableDatagram",
49
        [SOCK_SEQPACKET] = "SequentialPacket",
50
        [SOCK_DCCP] =      "DatagramCongestionControl",
51
};
52

53
DEFINE_STRING_TABLE_LOOKUP(socket_address_type, int);
1✔
54

55
int socket_address_verify(const SocketAddress *a, bool strict) {
19,649✔
56
        assert(a);
19,649✔
57

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

61
        switch (socket_address_family(a)) {
19,649✔
62

63
        case AF_INET:
27✔
64
                if (a->size != sizeof(struct sockaddr_in))
27✔
65
                        return -EINVAL;
66

67
                if (a->sockaddr.in.sin_port == 0)
27✔
68
                        return -EINVAL;
69

70
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
27✔
71
                        return -EINVAL;
1✔
72

73
                return 0;
74

75
        case AF_INET6:
33✔
76
                if (a->size != sizeof(struct sockaddr_in6))
33✔
77
                        return -EINVAL;
78

79
                if (a->sockaddr.in6.sin6_port == 0)
33✔
80
                        return -EINVAL;
81

82
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
33✔
83
                        return -EINVAL;
×
84

85
                return 0;
86

87
        case AF_UNIX:
18,903✔
88
                if (a->size < offsetof(struct sockaddr_un, sun_path))
18,903✔
89
                        return -EINVAL;
90
                if (a->size > sizeof(struct sockaddr_un) + !strict)
18,903✔
91
                        /* If !strict, allow one extra byte, since getsockname() on Linux will append
92
                         * a NUL byte if we have path sockets that are above sun_path's full size. */
93
                        return -EINVAL;
94

95
                if (a->size > offsetof(struct sockaddr_un, sun_path) &&
18,903✔
96
                    a->sockaddr.un.sun_path[0] != 0 &&
18,903✔
97
                    strict) {
98
                        /* Only validate file system sockets here, and only in strict mode */
99
                        const char *e;
3,735✔
100

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

116
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET))
18,903✔
117
                        return -EINVAL;
×
118

119
                return 0;
120

121
        case AF_NETLINK:
601✔
122

123
                if (a->size != sizeof(struct sockaddr_nl))
601✔
124
                        return -EINVAL;
125

126
                if (!IN_SET(a->type, 0, SOCK_RAW, SOCK_DGRAM))
601✔
127
                        return -EINVAL;
×
128

129
                return 0;
130

131
        case AF_VSOCK:
68✔
132
                if (a->size != sizeof(struct sockaddr_vm))
68✔
133
                        return -EINVAL;
134

135
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
68✔
136
                        return -EINVAL;
×
137

138
                return 0;
139

140
        default:
141
                return -EAFNOSUPPORT;
142
        }
143
}
144

145
int socket_address_print(const SocketAddress *a, char **ret) {
5,261✔
146
        int r;
5,261✔
147

148
        assert(a);
5,261✔
149
        assert(ret);
5,261✔
150

151
        r = socket_address_verify(a, false); /* We do non-strict validation, because we want to be
5,261✔
152
                                              * able to pretty-print any socket the kernel considers
153
                                              * valid. We still need to do validation to know if we
154
                                              * can meaningfully print the address. */
155
        if (r < 0)
5,261✔
156
                return r;
157

158
        if (socket_address_family(a) == AF_NETLINK) {
5,261✔
159
                _cleanup_free_ char *sfamily = NULL;
205✔
160

161
                r = netlink_family_to_string_alloc(a->protocol, &sfamily);
205✔
162
                if (r < 0)
205✔
163
                        return r;
164

165
                r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups);
205✔
166
                if (r < 0)
205✔
167
                        return -ENOMEM;
168

169
                return 0;
205✔
170
        }
171

172
        return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret);
5,056✔
173
}
174

175
bool socket_address_can_accept(const SocketAddress *a) {
11,404✔
176
        assert(a);
11,404✔
177

178
        return
11,404✔
179
                IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET);
11,404✔
180
}
181

182
bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) {
5,267✔
183
        assert(a);
5,267✔
184
        assert(b);
5,267✔
185

186
        /* Invalid addresses are unequal to all */
187
        if (socket_address_verify(a, false) < 0 ||
10,534✔
188
            socket_address_verify(b, false) < 0)
5,267✔
189
                return false;
190

191
        if (a->type != b->type)
5,266✔
192
                return false;
193

194
        if (socket_address_family(a) != socket_address_family(b))
5,265✔
195
                return false;
196

197
        switch (socket_address_family(a)) {
5,263✔
198

199
        case AF_INET:
6✔
200
                if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr)
6✔
201
                        return false;
202

203
                if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port)
5✔
204
                        return false;
1✔
205

206
                break;
207

208
        case AF_INET6:
2✔
209
                if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0)
2✔
210
                        return false;
211

212
                if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port)
2✔
213
                        return false;
×
214

215
                break;
216

217
        case AF_UNIX:
5,070✔
218
                if (a->size <= offsetof(struct sockaddr_un, sun_path) ||
5,070✔
219
                    b->size <= offsetof(struct sockaddr_un, sun_path))
5,070✔
220
                        return false;
221

222
                if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0))
5,070✔
223
                        return false;
224

225
                if (a->sockaddr.un.sun_path[0]) {
4,845✔
226
                        if (!path_equal_or_inode_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
4,806✔
227
                                return false;
330✔
228
                } else {
229
                        if (a->size != b->size)
39✔
230
                                return false;
231

232
                        if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0)
39✔
233
                                return false;
×
234
                }
235

236
                break;
237

238
        case AF_NETLINK:
166✔
239
                if (a->protocol != b->protocol)
166✔
240
                        return false;
241

242
                if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups)
166✔
243
                        return false;
1✔
244

245
                break;
246

247
        case AF_VSOCK:
19✔
248
                if (a->sockaddr.vm.svm_cid != b->sockaddr.vm.svm_cid)
19✔
249
                        return false;
250

251
                if (a->sockaddr.vm.svm_port != b->sockaddr.vm.svm_port)
18✔
252
                        return false;
1✔
253

254
                break;
255

256
        default:
257
                /* Cannot compare, so we assume the addresses are different */
258
                return false;
259
        }
260

261
        return true;
262
}
263

264
const char* socket_address_get_path(const SocketAddress *a) {
24,723✔
265
        assert(a);
24,723✔
266

267
        if (socket_address_family(a) != AF_UNIX)
24,723✔
268
                return NULL;
269

270
        if (a->sockaddr.un.sun_path[0] == 0)
23,954✔
271
                return NULL;
272

273
        /* Note that this is only safe because we know that there's an extra NUL byte after the sockaddr_un
274
         * structure. On Linux AF_UNIX file system socket addresses don't have to be NUL terminated if they take up the
275
         * full sun_path space. */
276
        assert_cc(sizeof(union sockaddr_union) >= sizeof(struct sockaddr_un)+1);
23,913✔
277
        return a->sockaddr.un.sun_path;
23,913✔
278
}
279

280
bool socket_ipv6_is_supported(void) {
151,311✔
281
        static int cached = -1;
151,311✔
282

283
        if (cached < 0) {
151,311✔
284

285
                if (access("/proc/net/if_inet6", F_OK) < 0) {
1,129✔
286

287
                        if (errno != ENOENT) {
×
288
                                log_debug_errno(errno, "Unexpected error when checking whether /proc/net/if_inet6 exists: %m");
×
289
                                return false;
290
                        }
291

292
                        cached = false;
×
293
                } else
294
                        cached = true;
1,129✔
295
        }
296

297
        return cached;
151,311✔
298
}
299

300
bool socket_ipv6_is_enabled(void) {
107,606✔
301
        _cleanup_free_ char *v = NULL;
107,606✔
302
        int r;
107,606✔
303

304
        /* Much like socket_ipv6_is_supported(), but also checks that the sysctl that disables IPv6 on all
305
         * interfaces isn't turned on */
306

307
        if (!socket_ipv6_is_supported())
107,606✔
308
                return false;
309

310
        r = sysctl_read_ip_property(AF_INET6, "all", "disable_ipv6", &v);
107,606✔
311
        if (r < 0) {
107,606✔
312
                log_debug_errno(r, "Unexpected error reading 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
×
313
                return true;
314
        }
315

316
        r = parse_boolean(v);
107,606✔
317
        if (r < 0) {
107,606✔
318
                log_debug_errno(r, "Failed to pare 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
107,606✔
319
                return true;
320
        }
321

322
        return !r;
107,606✔
323
}
324

325
bool socket_address_matches_fd(const SocketAddress *a, int fd) {
1,564✔
326
        SocketAddress b;
1,564✔
327
        socklen_t solen;
1,564✔
328

329
        assert(a);
1,564✔
330
        assert(fd >= 0);
1,564✔
331

332
        b.size = sizeof(b.sockaddr);
1,564✔
333
        if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0)
1,564✔
334
                return false;
1,564✔
335

336
        if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family)
724✔
337
                return false;
338

339
        solen = sizeof(b.type);
646✔
340
        if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0)
646✔
341
                return false;
342

343
        if (b.type != a->type)
646✔
344
                return false;
345

346
        if (a->protocol != 0)  {
555✔
347
                solen = sizeof(b.protocol);
×
348
                if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0)
×
349
                        return false;
350

351
                if (b.protocol != a->protocol)
×
352
                        return false;
353
        }
354

355
        return socket_address_equal(a, &b);
555✔
356
}
357

358
int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) {
×
359
        const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
×
360

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

363
        assert(sa);
×
364
        assert(ret_port);
×
365

366
        switch (sa->sa.sa_family) {
×
367

368
        case AF_INET:
×
369
                *ret_port = be16toh(sa->in.sin_port);
×
370
                return 0;
×
371

372
        case AF_INET6:
×
373
                *ret_port = be16toh(sa->in6.sin6_port);
×
374
                return 0;
×
375

376
        case AF_VSOCK:
×
377
                *ret_port = sa->vm.svm_port;
×
378
                return 0;
×
379

380
        default:
381
                return -EAFNOSUPPORT;
382
        }
383
}
384

385
const union in_addr_union *sockaddr_in_addr(const struct sockaddr *_sa) {
346✔
386
        const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
346✔
387

388
        if (!sa)
346✔
389
                return NULL;
390

391
        switch (sa->sa.sa_family) {
346✔
392

393
        case AF_INET:
268✔
394
                return (const union in_addr_union*) &sa->in.sin_addr;
268✔
395

396
        case AF_INET6:
78✔
397
                return (const union in_addr_union*) &sa->in6.sin6_addr;
78✔
398

399
        default:
400
                return NULL;
401
        }
402
}
403

404
int sockaddr_set_in_addr(
1,125✔
405
                union sockaddr_union *u,
406
                int family,
407
                const union in_addr_union *a,
408
                uint16_t port) {
409

410
        assert(u);
1,125✔
411
        assert(a);
1,125✔
412

413
        switch (family) {
1,125✔
414

415
        case AF_INET:
416
                u->in = (struct sockaddr_in) {
1,032✔
417
                        .sin_family = AF_INET,
418
                        .sin_addr = a->in,
1,032✔
419
                        .sin_port = htobe16(port),
1,032✔
420
                };
421

422
                return 0;
1,032✔
423

424
        case AF_INET6:
425
                u->in6 = (struct sockaddr_in6) {
93✔
426
                        .sin6_family = AF_INET6,
427
                        .sin6_addr = a->in6,
93✔
428
                        .sin6_port = htobe16(port),
93✔
429
                };
430

431
                return 0;
93✔
432

433
        default:
434
                return -EAFNOSUPPORT;
435

436
        }
437
}
438

439
int sockaddr_pretty(
13,095✔
440
                const struct sockaddr *_sa,
441
                socklen_t salen,
442
                bool translate_ipv6,
443
                bool include_port,
444
                char **ret) {
445

446
        union sockaddr_union *sa = (union sockaddr_union*) _sa;
13,095✔
447
        char *p;
13,095✔
448
        int r;
13,095✔
449

450
        assert(sa);
13,095✔
451
        assert(salen >= sizeof(sa->sa.sa_family));
13,095✔
452
        assert(ret);
13,095✔
453

454
        switch (sa->sa.sa_family) {
13,095✔
455

456
        case AF_INET: {
93✔
457
                uint32_t a;
93✔
458

459
                a = be32toh(sa->in.sin_addr.s_addr);
93✔
460

461
                if (include_port)
93✔
462
                        r = asprintf(&p,
93✔
463
                                     "%u.%u.%u.%u:%u",
464
                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
465
                                     be16toh(sa->in.sin_port));
93✔
466
                else
467
                        r = asprintf(&p,
×
468
                                     "%u.%u.%u.%u",
469
                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF);
470
                if (r < 0)
93✔
471
                        return -ENOMEM;
13,095✔
472
                break;
473
        }
474

475
        case AF_INET6: {
19✔
476
                static const unsigned char ipv4_prefix[] = {
19✔
477
                        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
478
                };
479

480
                if (translate_ipv6 &&
19✔
481
                    memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
9✔
482
                        const uint8_t *a = sa->in6.sin6_addr.s6_addr+12;
×
483
                        if (include_port)
×
484
                                r = asprintf(&p,
×
485
                                             "%u.%u.%u.%u:%u",
486
                                             a[0], a[1], a[2], a[3],
×
487
                                             be16toh(sa->in6.sin6_port));
×
488
                        else
489
                                r = asprintf(&p,
×
490
                                             "%u.%u.%u.%u",
491
                                             a[0], a[1], a[2], a[3]);
×
492
                        if (r < 0)
×
493
                                return -ENOMEM;
494
                } else {
495
                        const char *a = IN6_ADDR_TO_STRING(&sa->in6.sin6_addr);
19✔
496

497
                        if (include_port) {
19✔
498
                                if (asprintf(&p,
18✔
499
                                             "[%s]:%u%s%s",
500
                                             a,
501
                                             be16toh(sa->in6.sin6_port),
18✔
502
                                             sa->in6.sin6_scope_id != 0 ? "%" : "",
18✔
503
                                             FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX)) < 0)
18✔
504
                                        return -ENOMEM;
×
505
                        } else {
506
                                if (sa->in6.sin6_scope_id != 0)
1✔
507
                                        p = strjoin(a, "%", FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX));
×
508
                                else
509
                                        p = strdup(a);
1✔
510
                                if (!p)
1✔
511
                                        return -ENOMEM;
512
                        }
513
                }
514

515
                break;
516
        }
517

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

531
                        if (path[0] == 0) {
12,786✔
532
                                /* Abstract socket. When parsing address information from, we
533
                                 * explicitly reject overly long paths and paths with embedded NULs.
534
                                 * But we might get such a socket from the outside. Let's return
535
                                 * something meaningful and printable in this case. */
536

537
                                _cleanup_free_ char *e = NULL;
46✔
538

539
                                e = cescape_length(path + 1, path_len - 1);
46✔
540
                                if (!e)
46✔
541
                                        return -ENOMEM;
×
542

543
                                p = strjoin("@", e);
46✔
544
                        } else {
545
                                if (path[path_len - 1] == '\0')
12,740✔
546
                                        /* We expect a terminating NUL and don't print it */
547
                                        path_len--;
12,739✔
548

549
                                p = cescape_length(path, path_len);
12,740✔
550
                        }
551
                }
552
                if (!p)
12,961✔
553
                        return -ENOMEM;
554

555
                break;
556

557
        case AF_VSOCK:
22✔
558
                if (include_port) {
22✔
559
                        if (sa->vm.svm_cid == VMADDR_CID_ANY)
22✔
560
                                r = asprintf(&p, "vsock::%u", sa->vm.svm_port);
19✔
561
                        else
562
                                r = asprintf(&p, "vsock:%u:%u", sa->vm.svm_cid, sa->vm.svm_port);
3✔
563
                } else
564
                        r = asprintf(&p, "vsock:%u", sa->vm.svm_cid);
×
565
                if (r < 0)
22✔
566
                        return -ENOMEM;
567
                break;
568

569
        default:
570
                return -EOPNOTSUPP;
571
        }
572

573
        *ret = p;
13,095✔
574
        return 0;
13,095✔
575
}
576

577
int getpeername_pretty(int fd, bool include_port, char **ret) {
174✔
578
        union sockaddr_union sa;
174✔
579
        socklen_t salen = sizeof(sa);
174✔
580
        int r;
174✔
581

582
        assert(fd >= 0);
174✔
583
        assert(ret);
174✔
584

585
        if (getpeername(fd, &sa.sa, &salen) < 0)
174✔
586
                return -errno;
×
587

588
        if (sa.sa.sa_family == AF_UNIX) {
174✔
589
                struct ucred ucred = UCRED_INVALID;
173✔
590

591
                /* UNIX connection sockets are anonymous, so let's use
592
                 * PID/UID as pretty credentials instead */
593

594
                r = getpeercred(fd, &ucred);
173✔
595
                if (r < 0)
173✔
596
                        return r;
173✔
597

598
                if (asprintf(ret, "PID "PID_FMT"/UID "UID_FMT, ucred.pid, ucred.uid) < 0)
173✔
599
                        return -ENOMEM;
600

601
                return 0;
173✔
602
        }
603

604
        /* For remote sockets we translate IPv6 addresses back to IPv4
605
         * if applicable, since that's nicer. */
606

607
        return sockaddr_pretty(&sa.sa, salen, true, include_port, ret);
1✔
608
}
609

610
int getsockname_pretty(int fd, char **ret) {
4✔
611
        union sockaddr_union sa;
4✔
612
        socklen_t salen = sizeof(sa);
4✔
613

614
        assert(fd >= 0);
4✔
615
        assert(ret);
4✔
616

617
        if (getsockname(fd, &sa.sa, &salen) < 0)
4✔
618
                return -errno;
×
619

620
        /* For local sockets we do not translate IPv6 addresses back
621
         * to IPv6 if applicable, since this is usually used for
622
         * listening sockets where the difference between IPv4 and
623
         * IPv6 matters. */
624

625
        return sockaddr_pretty(&sa.sa, salen, false, true, ret);
4✔
626
}
627

628
int socknameinfo_pretty(const struct sockaddr *sa, socklen_t salen, char **ret) {
×
629
        char host[NI_MAXHOST];
×
630
        int r;
×
631

632
        assert(sa);
×
633
        assert(salen >= sizeof(sa_family_t));
×
634
        assert(ret);
×
635

636
        r = getnameinfo(sa, salen, host, sizeof(host), /* service= */ NULL, /* service_len= */ 0, IDN_FLAGS);
×
637
        if (r != 0) {
×
638
                if (r == EAI_MEMORY)
×
639
                        return log_oom_debug();
×
640
                if (r == EAI_SYSTEM)
×
641
                        log_debug_errno(errno, "getnameinfo() failed, ignoring: %m");
×
642
                else
643
                        log_debug("getnameinfo() failed, ignoring: %s", gai_strerror(r));
×
644

645
                return sockaddr_pretty(sa, salen, /* translate_ipv6= */ true, /* include_port= */ true, ret);
×
646
        }
647

648
        return strdup_to(ret, host);
×
649
}
650

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

672
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX);
958✔
673

674
bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) {
1,027✔
675
        assert(a);
1,027✔
676
        assert(b);
1,027✔
677

678
        if (a->sa.sa_family != b->sa.sa_family)
1,027✔
679
                return false;
680

681
        if (a->sa.sa_family == AF_INET)
1,026✔
682
                return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr;
4✔
683

684
        if (a->sa.sa_family == AF_INET6)
1,022✔
685
                return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0;
1✔
686

687
        if (a->sa.sa_family == AF_VSOCK)
1,021✔
688
                return a->vm.svm_cid == b->vm.svm_cid;
1✔
689

690
        return false;
691
}
692

693
int fd_set_sndbuf(int fd, size_t n, bool increase) {
400,570✔
694
        int r, value;
400,570✔
695
        socklen_t l = sizeof(value);
400,570✔
696

697
        if (n > INT_MAX)
400,570✔
698
                return -ERANGE;
400,570✔
699

700
        r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
400,570✔
701
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
400,570✔
702
                return 0;
703

704
        /* First, try to set the buffer size with SO_SNDBUF. */
705
        r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, n);
400,570✔
706
        if (r < 0)
400,570✔
707
                return r;
708

709
        /* SO_SNDBUF above may set to the kernel limit, instead of the requested size.
710
         * So, we need to check the actual buffer size here. */
711
        l = sizeof(value);
400,570✔
712
        r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
400,570✔
713
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
400,570✔
714
                return 1;
715

716
        /* If we have the privileges we will ignore the kernel limit. */
717
        r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, n);
399,744✔
718
        if (r < 0)
399,744✔
719
                return r;
31,028✔
720

721
        return 1;
722
}
723

724
int fd_set_rcvbuf(int fd, size_t n, bool increase) {
33,070✔
725
        int r, value;
33,070✔
726
        socklen_t l = sizeof(value);
33,070✔
727

728
        if (n > INT_MAX)
33,070✔
729
                return -ERANGE;
33,070✔
730

731
        r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
33,070✔
732
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
33,070✔
733
                return 0;
734

735
        /* First, try to set the buffer size with SO_RCVBUF. */
736
        r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, n);
33,069✔
737
        if (r < 0)
33,069✔
738
                return r;
739

740
        /* SO_RCVBUF above may set to the kernel limit, instead of the requested size.
741
         * So, we need to check the actual buffer size here. */
742
        l = sizeof(value);
33,069✔
743
        r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
33,069✔
744
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
33,069✔
745
                return 1;
746

747
        /* If we have the privileges we will ignore the kernel limit. */
748
        r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, n);
32,243✔
749
        if (r < 0)
32,243✔
750
                return r;
5,758✔
751

752
        return 1;
753
}
754

755
static const char* const ip_tos_table[] = {
756
        [IPTOS_LOWDELAY]    = "low-delay",
757
        [IPTOS_THROUGHPUT]  = "throughput",
758
        [IPTOS_RELIABILITY] = "reliability",
759
        [IPTOS_LOWCOST]     = "low-cost",
760
};
761

762
DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff);
5✔
763

764
bool ifname_valid_char(char a) {
482,923✔
765
        if ((unsigned char) a >= 127U)
482,923✔
766
                return false;
767

768
        if ((unsigned char) a <= 32U)
482,923✔
769
                return false;
770

771
        if (IN_SET(a,
482,919✔
772
                   ':',  /* colons are used by the legacy "alias" interface logic */
773
                   '/',  /* slashes cannot work, since we need to use network interfaces in sysfs paths, and in paths slashes are separators */
774
                   '%')) /* %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 */
775
                return false;
10✔
776

777
        return true;
778
}
779

780
bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
137,539✔
781
        bool numeric = true;
137,539✔
782

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

787
        assert(!(flags & ~_IFNAME_VALID_ALL));
137,539✔
788

789
        if (isempty(p))
137,539✔
790
                return false;
791

792
        /* A valid ifindex? If so, it's valid iff IFNAME_VALID_NUMERIC is set */
793
        if (parse_ifindex(p) >= 0)
132,570✔
794
                return flags & IFNAME_VALID_NUMERIC;
57✔
795

796
        if (flags & IFNAME_VALID_ALTERNATIVE) {
132,513✔
797
                if (strlen(p) >= ALTIFNAMSIZ)
13,525✔
798
                        return false;
799
        } else {
800
                if (strlen(p) >= IFNAMSIZ)
118,988✔
801
                        return false;
802
        }
803

804
        if (dot_or_dot_dot(p))
132,506✔
805
                return false;
806

807
        /* Let's refuse "all" and "default" as interface name, to avoid collisions with the special sysctl
808
         * directories /proc/sys/net/{ipv4,ipv6}/conf/{all,default} */
809
        if (!FLAGS_SET(flags, IFNAME_VALID_SPECIAL) && STR_IN_SET(p, "all", "default"))
132,504✔
810
                return false;
×
811

812
        for (const char *t = p; *t; t++) {
608,026✔
813
                if (!ifname_valid_char(*t))
475,536✔
814
                        return false;
815

816
                numeric = numeric && ascii_isdigit(*t);
608,016✔
817
        }
818

819
        /* It's fully numeric but didn't parse as valid ifindex above? if so, it must be too large or zero or
820
         * so, let's refuse that. */
821
        if (numeric)
132,490✔
822
                return false;
2✔
823

824
        return true;
825
}
826

827
bool address_label_valid(const char *p) {
44✔
828

829
        POINTER_MAY_BE_NULL(p);
44✔
830

831
        if (isempty(p))
44✔
832
                return false;
833

834
        if (strlen(p) >= IFNAMSIZ)
44✔
835
                return false;
836

837
        while (*p) {
376✔
838
                if ((uint8_t) *p >= 127U)
332✔
839
                        return false;
840

841
                if ((uint8_t) *p <= 31U)
332✔
842
                        return false;
843
                p++;
332✔
844
        }
845

846
        return true;
847
}
848

849
int getpeercred(int fd, struct ucred *ucred) {
87,562✔
850
        socklen_t n = sizeof(struct ucred);
87,562✔
851
        struct ucred u;
87,562✔
852

853
        assert(fd >= 0);
87,562✔
854
        assert(ucred);
87,562✔
855

856
        if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n) < 0)
87,562✔
857
                return -errno;
×
858

859
        if (n != sizeof(struct ucred))
87,562✔
860
                return -EIO;
861

862
        /* Check if the data is actually useful and not suppressed due to namespacing issues */
863
        if (!pid_is_valid(u.pid))
87,562✔
864
                return -ENODATA;
865

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

869
        *ucred = u;
87,562✔
870
        return 0;
87,562✔
871
}
872

873
int getpeersec(int fd, char **ret) {
19,730✔
874
        _cleanup_free_ char *s = NULL;
39,460✔
875
        socklen_t n = 64;
19,730✔
876

877
        assert(fd >= 0);
19,730✔
878
        assert(ret);
19,730✔
879

880
        for (;;) {
19,730✔
881
                s = new0(char, n+1);
19,730✔
882
                if (!s)
19,730✔
883
                        return -ENOMEM;
884

885
                if (getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n) >= 0) {
19,730✔
886
                        s[n] = 0;
9,057✔
887
                        break;
9,057✔
888
                }
889

890
                if (errno != ERANGE)
10,673✔
891
                        return -errno;
10,673✔
892

893
                s = mfree(s);
×
894
        }
895

896
        if (isempty(s))
9,057✔
897
                return -EOPNOTSUPP;
898

899
        *ret = TAKE_PTR(s);
9,057✔
900

901
        return 0;
9,057✔
902
}
903

904
int getpeergroups(int fd, gid_t **ret) {
19,731✔
905
        socklen_t n = sizeof(gid_t) * 64U;
19,731✔
906
        _cleanup_free_ gid_t *d = NULL;
19,731✔
907

908
        assert(fd >= 0);
19,731✔
909
        assert(ret);
19,731✔
910

911
        long ngroups_max = sysconf(_SC_NGROUPS_MAX);
19,731✔
912
        if (ngroups_max > 0)
19,731✔
913
                n = MAX(n, sizeof(gid_t) * (socklen_t) ngroups_max);
19,731✔
914

915
        for (;;) {
19,731✔
916
                d = malloc(n);
19,731✔
917
                if (!d)
19,731✔
918
                        return -ENOMEM;
919

920
                if (getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, d, &n) >= 0)
19,731✔
921
                        break;
922

923
                if (errno != ERANGE)
×
924
                        return -errno;
×
925

926
                d = mfree(d);
×
927
        }
928

929
        assert_se(n % sizeof(gid_t) == 0);
19,731✔
930
        n /= sizeof(gid_t);
19,731✔
931

932
        if (n > INT_MAX)
19,731✔
933
                return -E2BIG;
934

935
        *ret = TAKE_PTR(d);
19,731✔
936

937
        return (int) n;
19,731✔
938
}
939

940
int getpeerpidfd(int fd) {
20,498✔
941
        socklen_t n = sizeof(int);
20,498✔
942
        int pidfd = -EBADF;
20,498✔
943

944
        assert(fd >= 0);
20,498✔
945

946
        if (getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD, &pidfd, &n) < 0)
20,498✔
947
                return -errno;
×
948

949
        if (n != sizeof(int))
20,498✔
950
                return -EIO;
951

952
        return pidfd;
20,498✔
953
}
954

955
int getpeerpidref(int fd, PidRef *ret) {
4✔
956
        int r;
4✔
957

958
        assert(fd >= 0);
4✔
959
        assert(ret);
4✔
960

961
        int pidfd = getpeerpidfd(fd);
4✔
962
        if (pidfd < 0) {
4✔
963
                if (!ERRNO_IS_NEG_NOT_SUPPORTED(pidfd))
×
964
                        return pidfd;
×
965

966
                struct ucred ucred;
×
967
                r = getpeercred(fd, &ucred);
×
968
                if (r < 0)
×
969
                        return r;
970

971
                return pidref_set_pid(ret, ucred.pid);
×
972
        }
973

974
        return pidref_set_pidfd_consume(ret, pidfd);
4✔
975
}
976

977
ssize_t send_one_fd_iov_sa(
1,029✔
978
                int transport_fd,
979
                int fd,
980
                const struct iovec *iov, size_t iovlen,
981
                const struct sockaddr *sa, socklen_t len,
982
                int flags) {
983

984
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control = {};
1,029✔
985
        struct msghdr mh = {
1,029✔
986
                .msg_name = (struct sockaddr*) sa,
987
                .msg_namelen = len,
988
                .msg_iov = (struct iovec *)iov,
989
                .msg_iovlen = iovlen,
990
        };
991
        ssize_t k;
1,029✔
992

993
        assert(transport_fd >= 0);
1,029✔
994

995
        /*
996
         * We need either an FD or data to send.
997
         * If there's nothing, return an error.
998
         */
999
        if (fd < 0 && !iov)
1,029✔
1000
                return -EINVAL;
1,029✔
1001

1002
        if (fd >= 0) {
1,028✔
1003
                struct cmsghdr *cmsg;
1,024✔
1004

1005
                mh.msg_control = &control;
1,024✔
1006
                mh.msg_controllen = sizeof(control);
1,024✔
1007

1008
                cmsg = CMSG_FIRSTHDR(&mh);
1,024✔
1009
                cmsg->cmsg_level = SOL_SOCKET;
1,024✔
1010
                cmsg->cmsg_type = SCM_RIGHTS;
1,024✔
1011
                cmsg->cmsg_len = CMSG_LEN(sizeof(int));
1,024✔
1012
                memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
1,024✔
1013
        }
1014
        k = sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags);
1,028✔
1015
        if (k < 0)
1,028✔
1016
                return (ssize_t) -errno;
×
1017

1018
        return k;
1019
}
1020

1021
int send_one_fd_sa(
2✔
1022
                int transport_fd,
1023
                int fd,
1024
                const struct sockaddr *sa, socklen_t len,
1025
                int flags) {
1026

1027
        assert(fd >= 0);
2✔
1028

1029
        return (int) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, sa, len, flags);
2✔
1030
}
1031

1032
ssize_t receive_one_fd_iov(
6,955✔
1033
                int transport_fd,
1034
                struct iovec *iov, size_t iovlen,
1035
                int flags,
1036
                int *ret_fd) {
1037

1038
        CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control;
6,955✔
1039
        struct msghdr mh = {
6,955✔
1040
                .msg_control = &control,
1041
                .msg_controllen = sizeof(control),
1042
                .msg_iov = iov,
1043
                .msg_iovlen = iovlen,
1044
        };
1045
        struct cmsghdr *found;
6,955✔
1046
        ssize_t k;
6,955✔
1047

1048
        assert(transport_fd >= 0);
6,955✔
1049
        assert(ret_fd);
6,955✔
1050

1051
        /*
1052
         * Receive a single FD via @transport_fd. We don't care for
1053
         * the transport-type. We retrieve a single FD at most, so for
1054
         * packet-based transports, the caller must ensure to send
1055
         * only a single FD per packet.  This is best used in
1056
         * combination with send_one_fd().
1057
         */
1058

1059
        k = recvmsg_safe(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags);
6,955✔
1060
        if (k < 0)
6,955✔
1061
                return k;
6,955✔
1062

1063
        found = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, CMSG_LEN(sizeof(int)));
6,780✔
1064
        if (!found) {
6,780✔
1065
                cmsg_close_all(&mh);
2,078✔
1066

1067
                /* If didn't receive an FD or any data, return an error. */
1068
                if (k == 0)
2,078✔
1069
                        return -EIO;
1070
        }
1071

1072
        if (found)
4,706✔
1073
                *ret_fd = *CMSG_TYPED_DATA(found, int);
4,702✔
1074
        else
1075
                *ret_fd = -EBADF;
4✔
1076

1077
        return k;
1078
}
1079

1080
int receive_one_fd(int transport_fd, int flags) {
932✔
1081
        int fd;
932✔
1082
        ssize_t k;
932✔
1083

1084
        k = receive_one_fd_iov(transport_fd, NULL, 0, flags, &fd);
932✔
1085
        if (k == 0)
932✔
1086
                return fd;
848✔
1087

1088
        /* k must be negative, since receive_one_fd_iov() only returns
1089
         * a positive value if data was received through the iov. */
1090
        assert(k < 0);
84✔
1091
        return (int) k;
84✔
1092
}
1093

1094
ssize_t next_datagram_size_fd(int fd) {
200,368✔
1095
        ssize_t l;
200,368✔
1096
        int k;
200,368✔
1097

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

1104
        l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC);
200,368✔
1105
        if (l < 0) {
200,368✔
1106
                if (IN_SET(errno, EOPNOTSUPP, EFAULT))
624✔
1107
                        goto fallback;
×
1108

1109
                return -errno;
624✔
1110
        }
1111
        if (l == 0)
199,744✔
1112
                goto fallback;
75✔
1113

1114
        return l;
1115

1116
fallback:
75✔
1117
        k = 0;
75✔
1118

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

1122
        if (ioctl(fd, FIONREAD, &k) < 0)
75✔
1123
                return -errno;
×
1124

1125
        return (ssize_t) k;
75✔
1126
}
1127

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

1132
int flush_accept(int fd) {
16✔
1133

1134
        int r, b;
16✔
1135
        socklen_t l = sizeof(b);
16✔
1136

1137
        /* Similar to flush_fd() but flushes all incoming connections by accepting and immediately closing
1138
         * them. */
1139

1140
        if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &b, &l) < 0)
16✔
1141
                return -errno;
×
1142

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

1151
        for (unsigned iteration = 0;; iteration++) {
4✔
1152
                int cfd;
12✔
1153

1154
                r = fd_wait_for_event(fd, POLLIN, 0);
12✔
1155
                if (r == -EINTR)
12✔
1156
                        continue;
×
1157
                if (r <= 0)
12✔
1158
                        return r;
1159

1160
                if (iteration >= MAX_FLUSH_ITERATIONS)
4✔
1161
                        return log_debug_errno(SYNTHETIC_ERRNO(EBUSY),
×
1162
                                               "Failed to flush connections within " STRINGIFY(MAX_FLUSH_ITERATIONS) " iterations.");
1163

1164
                cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
4✔
1165
                if (cfd < 0) {
4✔
1166
                        if (errno == EAGAIN)
×
1167
                                return 0;
1168

1169
                        if (ERRNO_IS_ACCEPT_AGAIN(errno))
×
1170
                                continue;
×
1171

1172
                        return -errno;
×
1173
                }
1174

1175
                safe_close(cfd);
4✔
1176
        }
1177
}
1178

1179
ssize_t flush_mqueue(int fd) {
×
1180
        _cleanup_free_ char *buf = NULL;
×
1181
        struct mq_attr attr;
×
1182
        ssize_t count = 0;
×
1183
        int r;
×
1184

1185
        assert(fd >= 0);
×
1186

1187
        /* Similar to flush_fd() but flushes all messages from a POSIX message queue. */
1188

1189
        for (;;) {
×
1190
                ssize_t l;
×
1191

1192
                r = fd_wait_for_event(fd, POLLIN, /* timeout= */ 0);
×
1193
                if (r == -EINTR)
×
1194
                        continue;
×
1195
                if (r < 0)
×
1196
                        return r;
×
1197
                if (r == 0)
×
1198
                        return count;
1199

1200
                if (!buf) {
×
1201
                        /* Buffer must be at least as large as mq_msgsize. */
1202
                        if (mq_getattr(fd, &attr) < 0)
×
1203
                                return -errno;
×
1204

1205
                        buf = malloc(attr.mq_msgsize);
×
1206
                        if (!buf)
×
1207
                                return -ENOMEM;
1208
                }
1209

1210
                l = mq_receive(fd, buf, attr.mq_msgsize, /* msg_prio= */ NULL);
×
1211
                if (l < 0) {
×
1212
                        if (errno == EINTR)
×
1213
                                continue;
×
1214

1215
                        if (errno == EAGAIN)
×
1216
                                return count;
1217

1218
                        return -errno;
×
1219
                }
1220

1221
                count += l;
×
1222
        }
1223
}
1224

1225
struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length) {
395,065✔
1226
        struct cmsghdr *cmsg;
395,065✔
1227

1228
        assert(mh);
395,065✔
1229

1230
        CMSG_FOREACH(cmsg, mh)
395,691✔
1231
                if (cmsg->cmsg_level == level &&
359,105✔
1232
                    cmsg->cmsg_type == type &&
358,792✔
1233
                    (length == (socklen_t) -1 || length == cmsg->cmsg_len))
357,954✔
1234
                        return cmsg;
1235

1236
        return NULL;
1237
}
1238

1239
void* cmsg_find_and_copy_data(struct msghdr *mh, int level, int type, void *buf, size_t buf_len) {
349✔
1240
        struct cmsghdr *cmsg;
349✔
1241

1242
        assert(mh);
349✔
1243
        assert(buf);
349✔
1244
        assert(buf_len > 0);
349✔
1245

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

1250
        cmsg = cmsg_find(mh, level, type, CMSG_LEN(buf_len));
349✔
1251
        if (!cmsg)
349✔
1252
                return NULL;
1253

1254
        return memcpy_safe(buf, CMSG_DATA(cmsg), buf_len);
342✔
1255
}
1256

1257
size_t sockaddr_ll_len(const struct sockaddr_ll *sa) {
406✔
1258
        /* Certain hardware address types (e.g Infiniband) do not fit into sll_addr
1259
         * (8 bytes) and run over the structure. This function returns the correct size that
1260
         * must be passed to kernel. */
1261

1262
        assert(sa->sll_family == AF_PACKET);
406✔
1263

1264
        size_t mac_len = sizeof(sa->sll_addr);
406✔
1265

1266
        if (be16toh(sa->sll_hatype) == ARPHRD_ETHER)
406✔
1267
                mac_len = MAX(mac_len, (size_t) ETH_ALEN);
406✔
1268
        if (be16toh(sa->sll_hatype) == ARPHRD_INFINIBAND)
406✔
1269
                mac_len = MAX(mac_len, (size_t) INFINIBAND_ALEN);
×
1270

1271
        return offsetof(struct sockaddr_ll, sll_addr) + mac_len;
406✔
1272
}
1273

1274
size_t sockaddr_un_len(const struct sockaddr_un *sa) {
2,594✔
1275
        /* Covers only file system and abstract AF_UNIX socket addresses, but not unnamed socket addresses. */
1276

1277
        assert(sa->sun_family == AF_UNIX);
2,594✔
1278

1279
        return offsetof(struct sockaddr_un, sun_path) +
7,782✔
1280
                (sa->sun_path[0] == 0 ?
2,594✔
1281
                        1 + strnlen(sa->sun_path+1, sizeof(sa->sun_path)-1) :
640✔
1282
                        strnlen(sa->sun_path, sizeof(sa->sun_path))+1);
1,954✔
1283
}
1284

1285
size_t sockaddr_len(const union sockaddr_union *sa) {
441✔
1286
        switch (sa->sa.sa_family) {
441✔
1287
        case AF_INET:
1288
                return sizeof(struct sockaddr_in);
1289
        case AF_INET6:
133✔
1290
                return sizeof(struct sockaddr_in6);
133✔
1291
        case AF_UNIX:
3✔
1292
                return sockaddr_un_len(&sa->un);
3✔
1293
        case AF_PACKET:
×
1294
                return sockaddr_ll_len(&sa->ll);
×
1295
        case AF_NETLINK:
×
1296
                return sizeof(struct sockaddr_nl);
×
1297
        case AF_VSOCK:
1298
                return sizeof(struct sockaddr_vm);
1299
        default:
×
1300
                assert_not_reached();
×
1301
        }
1302
}
1303

1304
int socket_ioctl_fd(void) {
4,894✔
1305
        int fd;
4,894✔
1306

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

1312
        fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0);
4,894✔
1313
        if (fd < 0)
4,894✔
1314
                fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_GENERIC);
×
1315
        if (fd < 0)
×
1316
                return -errno;
×
1317

1318
        return fd;
1319
}
1320

1321
int sockaddr_un_unlink(const struct sockaddr_un *sa) {
1,769✔
1322
        const char *p, * nul;
1,769✔
1323

1324
        assert(sa);
1,769✔
1325

1326
        if (sa->sun_family != AF_UNIX)
1,769✔
1327
                return -EPROTOTYPE;
1328

1329
        if (sa->sun_path[0] == 0) /* Nothing to do for abstract sockets */
1,769✔
1330
                return 0;
1331

1332
        /* The path in .sun_path is not necessarily NUL terminated. Let's fix that. */
1333
        nul = memchr(sa->sun_path, 0, sizeof(sa->sun_path));
1,769✔
1334
        if (nul)
1,769✔
1335
                p = sa->sun_path;
1336
        else
1337
                p = memdupa_suffix0(sa->sun_path, sizeof(sa->sun_path));
×
1338

1339
        if (unlink(p) < 0)
1,769✔
1340
                return -errno;
1,349✔
1341

1342
        return 1;
1343
}
1344

1345
int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path) {
391,773✔
1346
        size_t l;
391,773✔
1347

1348
        assert(ret);
391,773✔
1349
        assert(path);
391,773✔
1350

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

1356
        l = strlen(path);
391,773✔
1357
        if (l < 2)
391,773✔
1358
                return -EINVAL;
1359
        if (!IN_SET(path[0], '/', '@'))
391,772✔
1360
                return -EINVAL;
1361

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

1372
        *ret = (struct sockaddr_un) {
391,770✔
1373
                .sun_family = AF_UNIX,
1374
        };
1375

1376
        if (path[0] == '@') {
391,770✔
1377
                /* Abstract namespace socket */
1378
                memcpy(ret->sun_path + 1, path + 1, l); /* copy *with* trailing NUL byte */
193,911✔
1379
                return (int) (offsetof(struct sockaddr_un, sun_path) + l); /* 🔥 *don't* 🔥 include trailing NUL in size */
193,911✔
1380

1381
        } else {
1382
                assert(path[0] == '/');
197,859✔
1383

1384
                /* File system socket */
1385
                memcpy(ret->sun_path, path, l + 1); /* copy *with* trailing NUL byte */
197,859✔
1386
                return (int) (offsetof(struct sockaddr_un, sun_path) + l + 1); /* include trailing NUL in size */
197,859✔
1387
        }
1388
}
1389

1390
int getsockopt_int(int fd, int level, int optname, int *ret) {
13,009✔
1391
        int v;
13,009✔
1392
        socklen_t sl = sizeof(v);
13,009✔
1393

1394
        assert(fd >= 0);
13,009✔
1395
        assert(ret);
13,009✔
1396

1397
        if (getsockopt(fd, level, optname, &v, &sl) < 0)
13,009✔
1398
                return negative_errno();
68✔
1399
        if (sl != sizeof(v))
12,941✔
1400
                return -EIO;
1401

1402
        *ret = v;
12,941✔
1403
        return 0;
12,941✔
1404
}
1405

1406
int socket_bind_to_ifname(int fd, const char *ifname) {
1✔
1407
        assert(fd >= 0);
1✔
1408

1409
        /* Call with NULL to drop binding */
1410

1411
        return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen_ptr(ifname)));
2✔
1412
}
1413

1414
int socket_bind_to_ifindex(int fd, int ifindex) {
842✔
1415
        assert(fd >= 0);
842✔
1416

1417
        if (ifindex <= 0)
842✔
1418
                /* Drop binding */
1419
                return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, NULL, 0));
68✔
1420

1421
        return setsockopt_int(fd, SOL_SOCKET, SO_BINDTOIFINDEX, ifindex);
774✔
1422
}
1423

1424
int socket_autobind(int fd, char **ret_name) {
483✔
1425
        _cleanup_free_ char *name = NULL;
483✔
1426
        uint64_t random;
483✔
1427
        int r;
483✔
1428

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

1432
        assert(fd >= 0);
483✔
1433

1434
        random = random_u64();
483✔
1435

1436
        if (asprintf(&name, "@%" PRIu64, random) < 0)
483✔
1437
                return -ENOMEM;
1438

1439
        union sockaddr_union sa;
483✔
1440
        assert_cc(DECIMAL_STR_MAX(uint64_t) < sizeof(sa.un.sun_path));
483✔
1441

1442
        r = sockaddr_un_set_path(&sa.un, name);
483✔
1443
        if (r < 0)
483✔
1444
                return r;
1445

1446
        if (bind(fd, &sa.sa, r) < 0)
483✔
1447
                return -errno;
×
1448

1449
        if (ret_name)
483✔
1450
                *ret_name = TAKE_PTR(name);
481✔
1451
        return 0;
1452
}
1453

1454
ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags) {
1,122,373✔
1455
        ssize_t n;
1,122,373✔
1456

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

1462
        assert(sockfd >= 0);
1,122,373✔
1463
        assert(msg);
1,122,373✔
1464

1465
        n = recvmsg(sockfd, msg, flags);
1,122,373✔
1466
        if (n < 0)
1,122,373✔
1467
                return -errno;
57,471✔
1468

1469
        if (FLAGS_SET(msg->msg_flags, MSG_CTRUNC) ||
1,064,902✔
1470
            (!FLAGS_SET(flags, MSG_PEEK) && FLAGS_SET(msg->msg_flags, MSG_TRUNC))) {
1,064,902✔
1471
                cmsg_close_all(msg);
×
1472
                return FLAGS_SET(msg->msg_flags, MSG_CTRUNC) ? -ECHRNG : -EXFULL;
×
1473
        }
1474

1475
        return n;
1476
}
1477

1478
int socket_get_family(int fd) {
34,733✔
1479
        int af;
34,733✔
1480
        socklen_t sl = sizeof(af);
34,733✔
1481

1482
        if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &sl) < 0)
34,733✔
1483
                return -errno;
×
1484

1485
        if (sl != sizeof(af))
34,733✔
1486
                return -EINVAL;
1487

1488
        return af;
34,733✔
1489
}
1490

1491
int socket_set_recvpktinfo(int fd, int af, bool b) {
17,279✔
1492

1493
        if (af == AF_UNSPEC) {
17,279✔
1494
                af = socket_get_family(fd);
×
1495
                if (af < 0)
×
1496
                        return af;
1497
        }
1498

1499
        switch (af) {
17,279✔
1500

1501
        case AF_INET:
9,088✔
1502
                return setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, b);
9,088✔
1503

1504
        case AF_INET6:
8,155✔
1505
                return setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, b);
8,155✔
1506

1507
        case AF_NETLINK:
36✔
1508
                return setsockopt_int(fd, SOL_NETLINK, NETLINK_PKTINFO, b);
36✔
1509

1510
        case AF_PACKET:
×
1511
                return setsockopt_int(fd, SOL_PACKET, PACKET_AUXDATA, b);
×
1512

1513
        default:
1514
                return -EAFNOSUPPORT;
1515
        }
1516
}
1517

1518
int socket_set_unicast_if(int fd, int af, int ifi) {
131✔
1519
        be32_t ifindex_be = htobe32(ifi);
131✔
1520

1521
        if (af == AF_UNSPEC) {
131✔
1522
                af = socket_get_family(fd);
1✔
1523
                if (af < 0)
1✔
1524
                        return af;
131✔
1525
        }
1526

1527
        switch (af) {
131✔
1528

1529
        case AF_INET:
87✔
1530
                return RET_NERRNO(setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
87✔
1531

1532
        case AF_INET6:
44✔
1533
                return RET_NERRNO(setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
44✔
1534

1535
        default:
1536
                return -EAFNOSUPPORT;
1537
        }
1538
}
1539

1540
int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val) {
34,316✔
1541
        if (af == AF_UNSPEC) {
34,316✔
1542
                af = socket_get_family(fd);
×
1543
                if (af < 0)
×
1544
                        return af;
1545
        }
1546

1547
        switch (af) {
34,316✔
1548

1549
        case AF_INET:
17,999✔
1550
                return setsockopt_int(fd, IPPROTO_IP, opt_ipv4, val);
17,999✔
1551

1552
        case AF_INET6:
16,317✔
1553
                return setsockopt_int(fd, IPPROTO_IPV6, opt_ipv6, val);
16,317✔
1554

1555
        default:
1556
                return -EAFNOSUPPORT;
1557
        }
1558
}
1559

1560
int socket_get_mtu(int fd, int af, size_t *ret) {
751✔
1561
        int mtu, r;
751✔
1562

1563
        assert(ret);
751✔
1564

1565
        if (af == AF_UNSPEC) {
751✔
1566
                af = socket_get_family(fd);
×
1567
                if (af < 0)
×
1568
                        return af;
751✔
1569
        }
1570

1571
        switch (af) {
751✔
1572

1573
        case AF_INET:
335✔
1574
                r = getsockopt_int(fd, IPPROTO_IP, IP_MTU, &mtu);
335✔
1575
                break;
1576

1577
        case AF_INET6:
416✔
1578
                r = getsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, &mtu);
416✔
1579
                break;
1580

1581
        default:
1582
                return -EAFNOSUPPORT;
1583
        }
1584

1585
        if (r < 0)
751✔
1586
                return r;
1587
        if (mtu <= 0)
683✔
1588
                return -EINVAL;
1589

1590
        *ret = (size_t) mtu;
683✔
1591
        return 0;
683✔
1592
}
1593

1594
static int connect_unix_path_simple(int fd, const char *path) {
180,683✔
1595
        union sockaddr_union sa = {
180,683✔
1596
                .un.sun_family = AF_UNIX,
1597
        };
1598
        size_t l;
180,683✔
1599

1600
        assert(fd >= 0);
180,683✔
1601
        assert(path);
180,683✔
1602

1603
        l = strlen(path);
180,683✔
1604
        assert(l > 0);
180,683✔
1605
        assert(l < sizeof(sa.un.sun_path));
180,683✔
1606

1607
        memcpy(sa.un.sun_path, path, l + 1);
180,683✔
1608
        return RET_NERRNO(connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + l + 1));
180,683✔
1609
}
1610

1611
static int connect_unix_inode(int fd, int inode_fd) {
63✔
1612
        assert(fd >= 0);
63✔
1613
        assert(inode_fd >= 0);
63✔
1614

1615
        return connect_unix_path_simple(fd, FORMAT_PROC_FD_PATH(inode_fd));
63✔
1616
}
1617

1618
int connect_unix_path(int fd, int dir_fd, const char *path) {
180,683✔
1619
        _cleanup_close_ int inode_fd = -EBADF;
180,683✔
1620

1621
        assert(fd >= 0);
180,683✔
1622
        assert(dir_fd == AT_FDCWD || dir_fd >= 0);
180,683✔
1623

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

1627
        if (!path)
180,683✔
1628
                return connect_unix_inode(fd, dir_fd); /* If no path is specified, then dir_fd refers to the socket inode to connect to. */
3✔
1629

1630
        /* Refuse zero length path early, to make sure AF_UNIX stack won't mistake this for an abstract
1631
         * namespace path, since first char is NUL */
1632
        if (isempty(path))
361,363✔
1633
                return -EINVAL;
1634

1635
        /* Shortcut for the simple case */
1636
        if (dir_fd == AT_FDCWD && strlen(path) < sizeof_field(struct sockaddr_un, sun_path))
180,680✔
1637
                return connect_unix_path_simple(fd, path);
180,620✔
1638

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

1643
        inode_fd = openat(dir_fd, path, O_PATH|O_CLOEXEC);
60✔
1644
        if (inode_fd < 0)
60✔
1645
                return -errno;
×
1646

1647
        return connect_unix_inode(fd, inode_fd);
60✔
1648
}
1649

1650
int socket_address_parse_unix(SocketAddress *ret_address, const char *s) {
211,093✔
1651
        struct sockaddr_un un;
211,093✔
1652
        int r;
211,093✔
1653

1654
        assert(ret_address);
211,093✔
1655
        assert(s);
211,093✔
1656

1657
        if (!IN_SET(*s, '/', '@'))
211,093✔
1658
                return -EPROTO;
211,093✔
1659

1660
        r = sockaddr_un_set_path(&un, s);
210,515✔
1661
        if (r < 0)
210,515✔
1662
                return r;
1663

1664
        *ret_address = (SocketAddress) {
210,513✔
1665
                .sockaddr.un = un,
1666
                .size = r,
1667
        };
1668

1669
        return 0;
210,513✔
1670
}
1671

1672
int socket_address_equal_unix(const char *a, const char *b) {
1,020✔
1673
        SocketAddress socket_a, socket_b;
1,020✔
1674
        int r;
1,020✔
1675

1676
        assert(a);
1,020✔
1677
        assert(b);
1,020✔
1678

1679
        r = socket_address_parse_unix(&socket_a, a);
1,020✔
1680
        if (r < 0)
1,020✔
1681
                return r;
1,020✔
1682

1683
        r = socket_address_parse_unix(&socket_b, b);
1,020✔
1684
        if (r < 0)
1,020✔
1685
                return r;
1686

1687
        return sockaddr_equal(&socket_a.sockaddr, &socket_b.sockaddr);
1,020✔
1688
}
1689

1690
int vsock_parse_port(const char *s, unsigned *ret) {
501✔
1691
        int r;
501✔
1692

1693
        assert(ret);
501✔
1694

1695
        if (!s)
501✔
1696
                return -EINVAL;
501✔
1697

1698
        unsigned u;
501✔
1699
        r = safe_atou(s, &u);
501✔
1700
        if (r < 0)
501✔
1701
                return r;
1702

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

1706
        if (u == VMADDR_PORT_ANY)
498✔
1707
                return -EINVAL;
1708

1709
        *ret = u;
498✔
1710
        return 0;
498✔
1711
}
1712

1713
int vsock_parse_cid(const char *s, unsigned *ret) {
461✔
1714
        assert(ret);
461✔
1715

1716
        if (!s)
461✔
1717
                return -EINVAL;
1718

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

1722
        if (streq(s, "hypervisor"))
461✔
1723
                *ret = VMADDR_CID_HYPERVISOR;
×
1724
        else if (streq(s, "local"))
461✔
1725
                *ret = VMADDR_CID_LOCAL;
×
1726
        else if (streq(s, "host"))
461✔
1727
                *ret = VMADDR_CID_HOST;
×
1728
        else
1729
                return safe_atou(s, ret);
461✔
1730

1731
        return 0;
1732
}
1733

1734
int socket_address_parse_vsock(SocketAddress *ret_address, const char *s) {
578✔
1735
        /* AF_VSOCK socket in vsock:cid:port notation */
1736
        _cleanup_free_ char *n = NULL;
578✔
1737
        const char *e, *cid_start;
578✔
1738
        unsigned port, cid;
578✔
1739
        int type, r;
578✔
1740

1741
        assert(ret_address);
578✔
1742
        assert(s);
578✔
1743

1744
        if ((cid_start = startswith(s, "vsock:")))
578✔
1745
                type = 0;
1746
        else if ((cid_start = startswith(s, "vsock-dgram:")))
526✔
1747
                type = SOCK_DGRAM;
1748
        else if ((cid_start = startswith(s, "vsock-seqpacket:")))
526✔
1749
                type = SOCK_SEQPACKET;
1750
        else if ((cid_start = startswith(s, "vsock-stream:")))
526✔
1751
                type = SOCK_STREAM;
1752
        else
1753
                return -EPROTO;
1754

1755
        e = strchr(cid_start, ':');
503✔
1756
        if (!e)
503✔
1757
                return -EINVAL;
1758

1759
        r = vsock_parse_port(e+1, &port);
501✔
1760
        if (r < 0)
501✔
1761
                return r;
1762

1763
        n = strndup(cid_start, e - cid_start);
498✔
1764
        if (!n)
498✔
1765
                return -ENOMEM;
1766

1767
        if (isempty(n))
498✔
1768
                cid = VMADDR_CID_ANY;
37✔
1769
        else {
1770
                r = vsock_parse_cid(n, &cid);
461✔
1771
                if (r < 0)
461✔
1772
                        return r;
1773
        }
1774

1775
        *ret_address = (SocketAddress) {
495✔
1776
                .sockaddr.vm = {
1777
                        .svm_family = AF_VSOCK,
1778
                        .svm_cid = cid,
1779
                        .svm_port = port,
1780
                },
1781
                .type = type,
1782
                .size = sizeof(struct sockaddr_vm),
1783
        };
1784

1785
        return 0;
495✔
1786
}
1787

1788
int vsock_get_local_cid(unsigned *ret) {
83✔
1789
        _cleanup_close_ int vsock_fd = -EBADF;
83✔
1790

1791
        vsock_fd = open("/dev/vsock", O_RDONLY|O_CLOEXEC);
83✔
1792
        if (vsock_fd < 0)
83✔
1793
                return log_debug_errno(errno, "Failed to open %s: %m", "/dev/vsock");
16✔
1794

1795
        unsigned tmp;
67✔
1796
        if (ioctl(vsock_fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, &tmp) < 0)
67✔
1797
                return log_debug_errno(errno, "Failed to query local AF_VSOCK CID: %m");
×
1798
        log_debug("Local AF_VSOCK CID: %u", tmp);
67✔
1799

1800
        /* If ret == NULL, we're just want to check if AF_VSOCK is available, so accept
1801
         * any address. Otherwise, filter out special addresses that are cannot be used
1802
         * to identify _this_ machine from the outside. */
1803
        if (ret && IN_SET(tmp, VMADDR_CID_LOCAL, VMADDR_CID_HOST, VMADDR_CID_ANY))
67✔
1804
                return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL),
×
1805
                                       "IOCTL_VM_SOCKETS_GET_LOCAL_CID returned special value (%u), ignoring.", tmp);
1806

1807
        if (ret)
64✔
1808
                *ret = tmp;
64✔
1809
        return 0;
1810
}
1811

1812
int netlink_socket_get_multicast_groups(int fd, size_t *ret_len, uint32_t **ret_groups) {
12,196✔
1813
        _cleanup_free_ uint32_t *groups = NULL;
24,392✔
1814
        socklen_t len = 0, old_len;
12,196✔
1815

1816
        assert(fd >= 0);
12,196✔
1817

1818
        if (getsockopt(fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, NULL, &len) < 0)
12,196✔
1819
                return -errno;
×
1820

1821
        if (len == 0)
12,196✔
1822
                goto finalize;
11,745✔
1823

1824
        groups = new0(uint32_t, len);
451✔
1825
        if (!groups)
451✔
1826
                return -ENOMEM;
1827

1828
        old_len = len;
451✔
1829

1830
        if (getsockopt(fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, groups, &len) < 0)
451✔
1831
                return -errno;
×
1832

1833
        if (old_len != len)
451✔
1834
                return -EIO;
1835

1836
finalize:
451✔
1837
        if (ret_len)
12,196✔
1838
                *ret_len = len;
12,196✔
1839
        if (ret_groups)
12,196✔
1840
                *ret_groups = TAKE_PTR(groups);
12,196✔
1841

1842
        return 0;
1843
}
1844

1845
int socket_get_cookie(int fd, uint64_t *ret) {
547✔
1846
        assert(fd >= 0);
547✔
1847

1848
        uint64_t cookie = 0;
547✔
1849
        socklen_t cookie_len = sizeof(cookie);
547✔
1850
        if (getsockopt(fd, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len) < 0)
547✔
1851
                return -errno;
×
1852

1853
        assert(cookie_len == sizeof(cookie));
547✔
1854
        if (ret)
547✔
1855
                *ret = cookie;
547✔
1856

1857
        return 0;
1858
}
1859

1860
void cmsg_close_all(struct msghdr *mh) {
103,308✔
1861
        assert(mh);
103,308✔
1862

1863
        struct cmsghdr *cmsg;
103,308✔
1864
        CMSG_FOREACH(cmsg, mh) {
242,688✔
1865
                if (cmsg->cmsg_level != SOL_SOCKET)
69,690✔
1866
                        continue;
×
1867

1868
                if (cmsg->cmsg_type == SCM_RIGHTS)
69,690✔
1869
                        close_many(CMSG_TYPED_DATA(cmsg, int),
×
1870
                                   (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int));
×
1871
                else if (cmsg->cmsg_type == SCM_PIDFD) {
69,690✔
1872
                        assert(cmsg->cmsg_len == CMSG_LEN(sizeof(int)));
×
1873
                        safe_close(*CMSG_TYPED_DATA(cmsg, int));
×
1874
                }
1875
        }
1876
}
103,308✔
1877

1878
int tos_to_priority(uint8_t tos) {
12✔
1879
        /* Map the IP Precedence (top 3 bits of the TOS field) to Linux internal packet priorities
1880
         * (TC_PRIO_*). This exactly mirrors the standard Linux kernel IP precedence-to-priority mapping
1881
         * (rt_tos2priority) to ensure consistent behavior when explicitly setting SO_PRIORITY. */
1882
        switch (IPTOS_PREC(tos)) {
12✔
1883
        case IPTOS_PREC_NETCONTROL:      /* 0xc0 (CS7) - Network Control. Used for infrastructure control (e.g., STP, keepalives). */
1884
        case IPTOS_PREC_INTERNETCONTROL: /* 0xe0 (CS6) - Internetwork Control. Used for routing protocols (e.g., OSPF, BGP) and DHCP. */
1885
                return TC_PRIO_CONTROL;
1886

1887
        case IPTOS_PREC_CRITIC_ECP:      /* 0xa0 (CS5) - Critical. Used for delay-sensitive traffic like Voice over IP (VoIP). */
2✔
1888
        case IPTOS_PREC_FLASHOVERRIDE:   /* 0x80 (CS4) - Flash Override. Used for interactive video and multimedia. */
1889
                return TC_PRIO_INTERACTIVE;
2✔
1890

1891
        case IPTOS_PREC_FLASH:           /* 0x60 (CS3) - Flash. Used for broadcast video and call signaling (e.g., SIP). */
2✔
1892
        case IPTOS_PREC_IMMEDIATE:       /* 0x40 (CS2) - Immediate. Used for OAM (Operations, Administration, and Management) and transactional data. */
1893
                return TC_PRIO_INTERACTIVE_BULK;
2✔
1894

1895
        case IPTOS_PREC_PRIORITY:        /* 0x20 (CS1) - Priority. Used for background traffic and bulk data transfers. */
2✔
1896
                return TC_PRIO_BULK;
2✔
1897

1898
        case IPTOS_PREC_ROUTINE:         /* 0x00 (CS0) - Routine. Best effort traffic. */
2✔
1899
        default:
1900
                return TC_PRIO_BESTEFFORT;
2✔
1901
        }
1902
}
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