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

systemd / systemd / 25705508282

11 May 2026 11:07PM UTC coverage: 72.65% (+0.1%) from 72.511%
25705508282

push

github

bluca
firstboot,sysinstall,hostnamed: always show FANCY_NAME=

This makes sure that whenever we want to show the OS name we can show
the fancy name. Thus this moves the escaping/validation of the fancy
name out of hostnamed into generic code, and then makes use of it in
sysinstall,firstboot,prompt-util.

17 of 36 new or added lines in 6 files covered. (47.22%)

2673 existing lines in 72 files now uncovered.

327104 of 450245 relevant lines covered (72.65%)

1200575.51 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) {
20,115✔
56
        assert(a);
20,115✔
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)) {
20,115✔
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:
19,365✔
88
                if (a->size < offsetof(struct sockaddr_un, sun_path))
19,365✔
89
                        return -EINVAL;
90
                if (a->size > sizeof(struct sockaddr_un) + !strict)
19,365✔
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) &&
19,365✔
96
                    a->sockaddr.un.sun_path[0] != 0 &&
19,365✔
97
                    strict) {
98
                        /* Only validate file system sockets here, and only in strict mode */
99
                        const char *e;
3,806✔
100

101
                        e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path));
3,806✔
102
                        if (e) {
3,806✔
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,806✔
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))
19,365✔
117
                        return -EINVAL;
×
118

119
                return 0;
120

121
        case AF_NETLINK:
604✔
122

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

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

129
                return 0;
130

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

135
                if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM))
69✔
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,398✔
146
        int r;
5,398✔
147

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

151
        r = socket_address_verify(a, false); /* We do non-strict validation, because we want to be
5,398✔
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,398✔
156
                return r;
157

158
        if (socket_address_family(a) == AF_NETLINK) {
5,398✔
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,193✔
173
}
174

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

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

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

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

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

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

197
        switch (socket_address_family(a)) {
5,390✔
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,197✔
218
                if (a->size <= offsetof(struct sockaddr_un, sun_path) ||
5,197✔
219
                    b->size <= offsetof(struct sockaddr_un, sun_path))
5,197✔
220
                        return false;
221

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

225
                if (a->sockaddr.un.sun_path[0]) {
4,972✔
226
                        if (!path_equal_or_inode_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0))
4,933✔
227
                                return false;
333✔
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) {
25,158✔
265
        assert(a);
25,158✔
266

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

270
        if (a->sockaddr.un.sun_path[0] == 0)
24,377✔
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);
24,336✔
277
        return a->sockaddr.un.sun_path;
24,336✔
278
}
279

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

283
        if (cached < 0) {
156,232✔
284

285
                if (access("/proc/net/if_inet6", F_OK) < 0) {
1,132✔
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,132✔
295
        }
296

297
        return cached;
156,232✔
298
}
299

300
bool socket_ipv6_is_enabled(void) {
113,018✔
301
        _cleanup_free_ char *v = NULL;
113,018✔
302
        int r;
113,018✔
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())
113,018✔
308
                return false;
309

310
        r = sysctl_read_ip_property(AF_INET6, "all", "disable_ipv6", &v);
113,018✔
311
        if (r < 0) {
113,018✔
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);
113,018✔
317
        if (r < 0) {
113,018✔
318
                log_debug_errno(r, "Failed to pare 'net.ipv6.conf.all.disable_ipv6' sysctl: %m");
113,018✔
319
                return true;
320
        }
321

322
        return !r;
113,018✔
323
}
324

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

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

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

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

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

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

346
        if (a->protocol != 0)  {
558✔
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);
558✔
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) {
319✔
386
        const union sockaddr_union *sa = (const union sockaddr_union*) _sa;
319✔
387

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

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

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

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

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

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

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

413
        switch (family) {
1,127✔
414

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

422
                return 0;
1,034✔
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(
12,770✔
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;
12,770✔
447
        char *p;
12,770✔
448
        int r;
12,770✔
449

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

454
        switch (sa->sa.sa_family) {
12,770✔
455

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

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

461
                if (include_port)
80✔
462
                        r = asprintf(&p,
80✔
463
                                     "%u.%u.%u.%u:%u",
464
                                     a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
465
                                     be16toh(sa->in.sin_port));
80✔
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)
80✔
471
                        return -ENOMEM;
12,770✔
472
                break;
473
        }
474

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

480
                if (translate_ipv6 &&
20✔
481
                    memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
10✔
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);
20✔
496

497
                        if (include_port) {
20✔
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)
2✔
507
                                        p = strjoin(a, "%", FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX));
×
508
                                else
509
                                        p = strdup(a);
2✔
510
                                if (!p)
2✔
511
                                        return -ENOMEM;
512
                        }
513
                }
514

515
                break;
516
        }
517

518
        case AF_UNIX:
12,648✔
519
                if (salen <= offsetof(struct sockaddr_un, sun_path) ||
12,648✔
520
                    (sa->un.sun_path[0] == 0 && salen == offsetof(struct sockaddr_un, sun_path) + 1))
12,487✔
521
                        /* The name must have at least one character (and the leading NUL does not count) */
522
                        p = strdup("<unnamed>");
163✔
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,485✔
529
                        size_t path_len = salen - offsetof(struct sockaddr_un, sun_path);
12,485✔
530

531
                        if (path[0] == 0) {
12,485✔
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,439✔
546
                                        /* We expect a terminating NUL and don't print it */
547
                                        path_len--;
12,438✔
548

549
                                p = cescape_length(path, path_len);
12,439✔
550
                        }
551
                }
552
                if (!p)
12,648✔
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;
12,770✔
574
        return 0;
12,770✔
575
}
576

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

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

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

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

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

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

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

601
                return 0;
161✔
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);
2✔
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);
961✔
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) {
413,353✔
694
        int r, value;
413,353✔
695
        socklen_t l = sizeof(value);
413,353✔
696

697
        if (n > INT_MAX)
413,353✔
698
                return -ERANGE;
413,353✔
699

700
        r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
413,353✔
701
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
413,353✔
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);
413,353✔
706
        if (r < 0)
413,353✔
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);
413,353✔
712
        r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l);
413,353✔
713
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
413,353✔
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);
412,531✔
718
        if (r < 0)
412,531✔
719
                return r;
31,342✔
720

721
        return 1;
722
}
723

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

728
        if (n > INT_MAX)
31,930✔
729
                return -ERANGE;
31,930✔
730

731
        r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
31,930✔
732
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
31,930✔
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);
31,929✔
737
        if (r < 0)
31,929✔
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);
31,929✔
743
        r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l);
31,929✔
744
        if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2)
31,929✔
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);
31,107✔
749
        if (r < 0)
31,107✔
750
                return r;
5,491✔
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) {
497,590✔
765
        if ((unsigned char) a >= 127U)
497,590✔
766
                return false;
767

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

771
        if (IN_SET(a,
497,586✔
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) {
142,565✔
781
        bool numeric = true;
142,565✔
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));
142,565✔
788

789
        if (isempty(p))
142,565✔
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)
137,783✔
794
                return flags & IFNAME_VALID_NUMERIC;
57✔
795

796
        if (flags & IFNAME_VALID_ALTERNATIVE) {
137,726✔
797
                if (strlen(p) >= ALTIFNAMSIZ)
13,458✔
798
                        return false;
799
        } else {
800
                if (strlen(p) >= IFNAMSIZ)
124,268✔
801
                        return false;
802
        }
803

804
        if (dot_or_dot_dot(p))
137,719✔
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"))
137,717✔
810
                return false;
×
811

812
        for (const char *t = p; *t; t++) {
628,145✔
813
                if (!ifname_valid_char(*t))
490,442✔
814
                        return false;
815

816
                numeric = numeric && ascii_isdigit(*t);
628,135✔
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)
137,703✔
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,855✔
850
        socklen_t n = sizeof(struct ucred);
87,855✔
851
        struct ucred u;
87,855✔
852

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

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

859
        if (n != sizeof(struct ucred))
87,855✔
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,855✔
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,855✔
870
        return 0;
87,855✔
871
}
872

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

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

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

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

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

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

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

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

901
        return 0;
9,079✔
902
}
903

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

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

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

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

920
                if (getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, d, &n) >= 0)
19,748✔
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,748✔
930
        n /= sizeof(gid_t);
19,748✔
931

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

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

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

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

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

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

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

952
        return pidfd;
20,506✔
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,030✔
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,030✔
985
        struct msghdr mh = {
1,030✔
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,030✔
992

993
        assert(transport_fd >= 0);
1,030✔
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,030✔
1000
                return -EINVAL;
1,030✔
1001

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

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

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

1018
        return k;
1019
}
1020

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

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

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

1032
ssize_t receive_one_fd_iov(
6,944✔
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,944✔
1039
        struct msghdr mh = {
6,944✔
1040
                .msg_control = &control,
1041
                .msg_controllen = sizeof(control),
1042
                .msg_iov = iov,
1043
                .msg_iovlen = iovlen,
1044
        };
1045
        struct cmsghdr *found;
6,944✔
1046
        ssize_t k;
6,944✔
1047

1048
        assert(transport_fd >= 0);
6,944✔
1049
        assert(ret_fd);
6,944✔
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,944✔
1060
        if (k < 0)
6,944✔
1061
                return k;
6,944✔
1062

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

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

1072
        if (found)
4,694✔
1073
                *ret_fd = *CMSG_TYPED_DATA(found, int);
4,691✔
1074
        else
1075
                *ret_fd = -EBADF;
3✔
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) {
209,841✔
1095
        ssize_t l;
209,841✔
1096
        int k;
209,841✔
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);
209,841✔
1105
        if (l < 0) {
209,841✔
1106
                if (IN_SET(errno, EOPNOTSUPP, EFAULT))
662✔
1107
                        goto fallback;
×
1108

1109
                return -errno;
662✔
1110
        }
1111
        if (l == 0)
209,179✔
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) {
47✔
1133

1134
        int r, b;
47✔
1135
        socklen_t l = sizeof(b);
47✔
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)
47✔
1141
                return -errno;
×
1142

1143
        assert(l == sizeof(b));
47✔
1144
        if (!b) /* Let's check if this socket accepts connections before calling accept(). accept4() can
47✔
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++) {
3✔
1152
                int cfd;
42✔
1153

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

1160
                if (iteration >= MAX_FLUSH_ITERATIONS)
3✔
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);
3✔
1165
                if (cfd < 0) {
3✔
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);
3✔
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) {
404,706✔
1226
        struct cmsghdr *cmsg;
404,706✔
1227

1228
        assert(mh);
404,706✔
1229

1230
        CMSG_FOREACH(cmsg, mh)
405,344✔
1231
                if (cmsg->cmsg_level == level &&
368,376✔
1232
                    cmsg->cmsg_type == type &&
368,057✔
1233
                    (length == (socklen_t) -1 || length == cmsg->cmsg_len))
367,200✔
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) {
354✔
1240
        struct cmsghdr *cmsg;
354✔
1241

1242
        assert(mh);
354✔
1243
        assert(buf);
354✔
1244
        assert(buf_len > 0);
354✔
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));
354✔
1251
        if (!cmsg)
354✔
1252
                return NULL;
1253

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

1257
size_t sockaddr_ll_len(const struct sockaddr_ll *sa) {
398✔
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);
398✔
1263

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

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

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

1274
size_t sockaddr_un_len(const struct sockaddr_un *sa) {
2,637✔
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,637✔
1278

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

1285
size_t sockaddr_len(const union sockaddr_union *sa) {
434✔
1286
        switch (sa->sa.sa_family) {
434✔
1287
        case AF_INET:
1288
                return sizeof(struct sockaddr_in);
1289
        case AF_INET6:
136✔
1290
                return sizeof(struct sockaddr_in6);
136✔
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,875✔
1305
        int fd;
4,875✔
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,875✔
1313
        if (fd < 0)
4,875✔
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,786✔
1322
        const char *p, * nul;
1,786✔
1323

1324
        assert(sa);
1,786✔
1325

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

1329
        if (sa->sun_path[0] == 0) /* Nothing to do for abstract sockets */
1,786✔
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,786✔
1334
        if (nul)
1,786✔
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,786✔
1340
                return -errno;
1,358✔
1341

1342
        return 1;
1343
}
1344

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

1348
        assert(ret);
403,367✔
1349
        assert(path);
403,367✔
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);
403,367✔
1357
        if (l < 2)
403,367✔
1358
                return -EINVAL;
1359
        if (!IN_SET(path[0], '/', '@'))
403,366✔
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))
403,366✔
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) {
403,364✔
1373
                .sun_family = AF_UNIX,
1374
        };
1375

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

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

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

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

1394
        assert(fd >= 0);
12,950✔
1395
        assert(ret);
12,950✔
1396

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

1402
        *ret = v;
12,894✔
1403
        return 0;
12,894✔
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) {
823✔
1415
        assert(fd >= 0);
823✔
1416

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

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

1424
int socket_autobind(int fd, char **ret_name) {
541✔
1425
        _cleanup_free_ char *name = NULL;
541✔
1426
        uint64_t random;
541✔
1427
        int r;
541✔
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);
541✔
1433

1434
        random = random_u64();
541✔
1435

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

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

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

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

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

1454
ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags) {
1,148,634✔
1455
        ssize_t n;
1,148,634✔
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,148,634✔
1463
        assert(msg);
1,148,634✔
1464

1465
        n = recvmsg(sockfd, msg, flags);
1,148,634✔
1466
        if (n < 0)
1,148,634✔
1467
                return -errno;
59,309✔
1468

1469
        if (FLAGS_SET(msg->msg_flags, MSG_CTRUNC) ||
1,089,325✔
1470
            (!FLAGS_SET(flags, MSG_PEEK) && FLAGS_SET(msg->msg_flags, MSG_TRUNC))) {
1,089,325✔
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) {
35,153✔
1479
        int af;
35,153✔
1480
        socklen_t sl = sizeof(af);
35,153✔
1481

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

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

1488
        return af;
35,153✔
1489
}
1490

1491
int socket_set_recvpktinfo(int fd, int af, bool b) {
18,138✔
1492

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

1499
        switch (af) {
18,138✔
1500

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

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

1507
        case AF_NETLINK:
37✔
1508
                return setsockopt_int(fd, SOL_NETLINK, NETLINK_PKTINFO, b);
37✔
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) {
129✔
1519
        be32_t ifindex_be = htobe32(ifi);
129✔
1520

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

1527
        switch (af) {
129✔
1528

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

1532
        case AF_INET6:
36✔
1533
                return RET_NERRNO(setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be)));
36✔
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) {
36,032✔
1541
        if (af == AF_UNSPEC) {
36,032✔
1542
                af = socket_get_family(fd);
×
1543
                if (af < 0)
×
1544
                        return af;
1545
        }
1546

1547
        switch (af) {
36,032✔
1548

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

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

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

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

1563
        assert(ret);
758✔
1564

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

1571
        switch (af) {
758✔
1572

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

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

1581
        default:
1582
                return -EAFNOSUPPORT;
1583
        }
1584

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

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

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

1600
        assert(fd >= 0);
182,930✔
1601
        assert(path);
182,930✔
1602

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

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

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

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

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

1621
        assert(fd >= 0);
182,930✔
1622
        assert(IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT) || dir_fd >= 0);
182,930✔
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) {
182,930✔
1628
                if (dir_fd < 0)
3✔
1629
                        return -EISDIR;
1630

1631
                return connect_unix_inode(fd, dir_fd); /* If no path is specified, then dir_fd refers to the socket inode to connect to. */
3✔
1632
        }
1633

1634
        /* Refuse zero length path early, to make sure AF_UNIX stack won't mistake this for an abstract
1635
         * namespace path, since first char is NUL */
1636
        if (isempty(path))
365,857✔
1637
                return -EINVAL;
1638

1639
        /* Shortcut for the simple case */
1640
        if (dir_fd == AT_FDCWD && strlen(path) < sizeof_field(struct sockaddr_un, sun_path))
182,927✔
1641
                return connect_unix_path_simple(fd, path);
182,866✔
1642

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

1647
        if (dir_fd == XAT_FDROOT) {
61✔
1648
                _cleanup_free_ char *j = strjoin("/", path);
2✔
1649
                if (!j)
1✔
UNCOV
1650
                        return -ENOMEM;
×
1651

1652
                inode_fd = open(j, O_PATH|O_CLOEXEC);
1✔
1653
        } else
1654
                inode_fd = openat(dir_fd, path, O_PATH|O_CLOEXEC);
60✔
1655
        if (inode_fd < 0)
61✔
UNCOV
1656
                return -errno;
×
1657

1658
        return connect_unix_inode(fd, inode_fd);
61✔
1659
}
1660

1661
int socket_address_parse_unix(SocketAddress *ret_address, const char *s) {
223,056✔
1662
        struct sockaddr_un un;
223,056✔
1663
        int r;
223,056✔
1664

1665
        assert(ret_address);
223,056✔
1666
        assert(s);
223,056✔
1667

1668
        if (!IN_SET(*s, '/', '@'))
223,056✔
1669
                return -EPROTO;
223,056✔
1670

1671
        r = sockaddr_un_set_path(&un, s);
222,558✔
1672
        if (r < 0)
222,558✔
1673
                return r;
1674

1675
        *ret_address = (SocketAddress) {
222,556✔
1676
                .sockaddr.un = un,
1677
                .size = r,
1678
        };
1679

1680
        return 0;
222,556✔
1681
}
1682

1683
int socket_address_equal_unix(const char *a, const char *b) {
1,020✔
1684
        SocketAddress socket_a, socket_b;
1,020✔
1685
        int r;
1,020✔
1686

1687
        assert(a);
1,020✔
1688
        assert(b);
1,020✔
1689

1690
        r = socket_address_parse_unix(&socket_a, a);
1,020✔
1691
        if (r < 0)
1,020✔
1692
                return r;
1,020✔
1693

1694
        r = socket_address_parse_unix(&socket_b, b);
1,020✔
1695
        if (r < 0)
1,020✔
1696
                return r;
1697

1698
        return sockaddr_equal(&socket_a.sockaddr, &socket_b.sockaddr);
1,020✔
1699
}
1700

1701
int vsock_parse_port(const char *s, unsigned *ret) {
421✔
1702
        int r;
421✔
1703

1704
        assert(ret);
421✔
1705

1706
        if (!s)
421✔
1707
                return -EINVAL;
421✔
1708

1709
        unsigned u;
421✔
1710
        r = safe_atou(s, &u);
421✔
1711
        if (r < 0)
421✔
1712
                return r;
1713

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

1717
        if (u == VMADDR_PORT_ANY)
418✔
1718
                return -EINVAL;
1719

1720
        *ret = u;
418✔
1721
        return 0;
418✔
1722
}
1723

1724
int vsock_parse_cid(const char *s, unsigned *ret) {
380✔
1725
        assert(ret);
380✔
1726

1727
        if (!s)
380✔
1728
                return -EINVAL;
1729

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

1733
        if (streq(s, "hypervisor"))
380✔
UNCOV
1734
                *ret = VMADDR_CID_HYPERVISOR;
×
1735
        else if (streq(s, "local"))
380✔
UNCOV
1736
                *ret = VMADDR_CID_LOCAL;
×
1737
        else if (streq(s, "host"))
380✔
UNCOV
1738
                *ret = VMADDR_CID_HOST;
×
1739
        else
1740
                return safe_atou(s, ret);
380✔
1741

1742
        return 0;
1743
}
1744

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

1752
        assert(ret_address);
498✔
1753
        assert(s);
498✔
1754

1755
        if ((cid_start = startswith(s, "vsock:")))
498✔
1756
                type = 0;
1757
        else if ((cid_start = startswith(s, "vsock-dgram:")))
445✔
1758
                type = SOCK_DGRAM;
1759
        else if ((cid_start = startswith(s, "vsock-seqpacket:")))
445✔
1760
                type = SOCK_SEQPACKET;
1761
        else if ((cid_start = startswith(s, "vsock-stream:")))
445✔
1762
                type = SOCK_STREAM;
1763
        else
1764
                return -EPROTO;
1765

1766
        e = strchr(cid_start, ':');
423✔
1767
        if (!e)
423✔
1768
                return -EINVAL;
1769

1770
        r = vsock_parse_port(e+1, &port);
421✔
1771
        if (r < 0)
421✔
1772
                return r;
1773

1774
        n = strndup(cid_start, e - cid_start);
418✔
1775
        if (!n)
418✔
1776
                return -ENOMEM;
1777

1778
        if (isempty(n))
418✔
1779
                cid = VMADDR_CID_ANY;
38✔
1780
        else {
1781
                r = vsock_parse_cid(n, &cid);
380✔
1782
                if (r < 0)
380✔
1783
                        return r;
1784
        }
1785

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

1796
        return 0;
415✔
1797
}
1798

1799
int vsock_get_local_cid(unsigned *ret) {
85✔
1800
        _cleanup_close_ int vsock_fd = -EBADF;
85✔
1801

1802
        vsock_fd = open("/dev/vsock", O_RDONLY|O_CLOEXEC);
85✔
1803
        if (vsock_fd < 0)
85✔
1804
                return log_debug_errno(errno, "Failed to open %s: %m", "/dev/vsock");
16✔
1805

1806
        unsigned tmp;
69✔
1807
        if (ioctl(vsock_fd, IOCTL_VM_SOCKETS_GET_LOCAL_CID, &tmp) < 0)
69✔
UNCOV
1808
                return log_debug_errno(errno, "Failed to query local AF_VSOCK CID: %m");
×
1809
        log_debug("Local AF_VSOCK CID: %u", tmp);
69✔
1810

1811
        /* If ret == NULL, we're just want to check if AF_VSOCK is available, so accept
1812
         * any address. Otherwise, filter out special addresses that are cannot be used
1813
         * to identify _this_ machine from the outside. */
1814
        if (ret && IN_SET(tmp, VMADDR_CID_LOCAL, VMADDR_CID_HOST, VMADDR_CID_ANY))
69✔
UNCOV
1815
                return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL),
×
1816
                                       "IOCTL_VM_SOCKETS_GET_LOCAL_CID returned special value (%u), ignoring.", tmp);
1817

1818
        if (ret)
66✔
1819
                *ret = tmp;
66✔
1820
        return 0;
1821
}
1822

1823
int netlink_socket_get_multicast_groups(int fd, size_t *ret_len, uint32_t **ret_groups) {
12,138✔
1824
        _cleanup_free_ uint32_t *groups = NULL;
24,276✔
1825
        socklen_t len = 0, old_len;
12,138✔
1826

1827
        assert(fd >= 0);
12,138✔
1828

1829
        if (getsockopt(fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, NULL, &len) < 0)
12,138✔
UNCOV
1830
                return -errno;
×
1831

1832
        if (len == 0)
12,138✔
1833
                goto finalize;
11,689✔
1834

1835
        groups = new0(uint32_t, len);
449✔
1836
        if (!groups)
449✔
1837
                return -ENOMEM;
1838

1839
        old_len = len;
449✔
1840

1841
        if (getsockopt(fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, groups, &len) < 0)
449✔
UNCOV
1842
                return -errno;
×
1843

1844
        if (old_len != len)
449✔
1845
                return -EIO;
1846

1847
finalize:
449✔
1848
        if (ret_len)
12,138✔
1849
                *ret_len = len;
12,138✔
1850
        if (ret_groups)
12,138✔
1851
                *ret_groups = TAKE_PTR(groups);
12,138✔
1852

1853
        return 0;
1854
}
1855

1856
int socket_get_cookie(int fd, uint64_t *ret) {
517✔
1857
        assert(fd >= 0);
517✔
1858

1859
        uint64_t cookie = 0;
517✔
1860
        socklen_t cookie_len = sizeof(cookie);
517✔
1861
        if (getsockopt(fd, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len) < 0)
517✔
UNCOV
1862
                return -errno;
×
1863

1864
        assert(cookie_len == sizeof(cookie));
517✔
1865
        if (ret)
517✔
1866
                *ret = cookie;
517✔
1867

1868
        return 0;
1869
}
1870

1871
void cmsg_close_all(struct msghdr *mh) {
103,900✔
1872
        assert(mh);
103,900✔
1873

1874
        struct cmsghdr *cmsg;
103,900✔
1875
        CMSG_FOREACH(cmsg, mh) {
243,604✔
1876
                if (cmsg->cmsg_level != SOL_SOCKET)
69,852✔
UNCOV
1877
                        continue;
×
1878

1879
                if (cmsg->cmsg_type == SCM_RIGHTS)
69,852✔
UNCOV
1880
                        close_many(CMSG_TYPED_DATA(cmsg, int),
×
UNCOV
1881
                                   (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int));
×
1882
                else if (cmsg->cmsg_type == SCM_PIDFD) {
69,852✔
UNCOV
1883
                        assert(cmsg->cmsg_len == CMSG_LEN(sizeof(int)));
×
UNCOV
1884
                        safe_close(*CMSG_TYPED_DATA(cmsg, int));
×
1885
                }
1886
        }
1887
}
103,900✔
1888

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

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

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

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

1909
        case IPTOS_PREC_ROUTINE:         /* 0x00 (CS0) - Routine. Best effort traffic. */
2✔
1910
        default:
1911
                return TC_PRIO_BESTEFFORT;
2✔
1912
        }
1913
}
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