]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
42f4e3c4 | 3 | #include <arpa/inet.h> |
07630cea | 4 | #include <errno.h> |
11c3a366 | 5 | #include <limits.h> |
542563ba | 6 | #include <net/if.h> |
b31f535c | 7 | #include <netdb.h> |
2583fbea | 8 | #include <netinet/ip.h> |
60d9771c | 9 | #include <poll.h> |
07630cea | 10 | #include <stddef.h> |
11c3a366 | 11 | #include <stdint.h> |
07630cea | 12 | #include <stdio.h> |
11c3a366 | 13 | #include <stdlib.h> |
f5947a5e | 14 | #include <sys/ioctl.h> |
07630cea | 15 | #include <unistd.h> |
4252696a | 16 | #include <linux/if.h> |
42f4e3c4 | 17 | |
b5efdb8a | 18 | #include "alloc-util.h" |
4ff9bc2e | 19 | #include "errno-util.h" |
15dca371 | 20 | #include "escape.h" |
2583fbea | 21 | #include "fd-util.h" |
07630cea | 22 | #include "fileio.h" |
f97b34a6 | 23 | #include "format-util.h" |
0f2d351f | 24 | #include "io-util.h" |
93cc7779 | 25 | #include "log.h" |
0a970718 | 26 | #include "memory-util.h" |
6bedfcbb | 27 | #include "parse-util.h" |
9eb977db | 28 | #include "path-util.h" |
dccca82b | 29 | #include "process-util.h" |
2583fbea | 30 | #include "socket-util.h" |
8b43440b | 31 | #include "string-table.h" |
07630cea | 32 | #include "string-util.h" |
ef76dff2 | 33 | #include "strv.h" |
83e03c4f | 34 | #include "sysctl-util.h" |
ee104e11 | 35 | #include "user-util.h" |
ef76dff2 | 36 | #include "utf8.h" |
42f4e3c4 | 37 | |
349cc4a5 | 38 | #if ENABLE_IDN |
cadc80b8 | 39 | # define IDN_FLAGS NI_IDN |
6326a143 WB |
40 | #else |
41 | # define IDN_FLAGS 0 | |
42 | #endif | |
43 | ||
398ce0bc | 44 | static const char* const socket_address_type_table[] = { |
955bb7fa ZJS |
45 | [SOCK_STREAM] = "Stream", |
46 | [SOCK_DGRAM] = "Datagram", | |
47 | [SOCK_RAW] = "Raw", | |
48 | [SOCK_RDM] = "ReliableDatagram", | |
398ce0bc | 49 | [SOCK_SEQPACKET] = "SequentialPacket", |
955bb7fa | 50 | [SOCK_DCCP] = "DatagramCongestionControl", |
398ce0bc YW |
51 | }; |
52 | ||
53 | DEFINE_STRING_TABLE_LOOKUP(socket_address_type, int); | |
54 | ||
15dca371 | 55 | int socket_address_verify(const SocketAddress *a, bool strict) { |
42f4e3c4 LP |
56 | assert(a); |
57 | ||
15dca371 ZJS |
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 | ||
542563ba | 61 | switch (socket_address_family(a)) { |
42f4e3c4 | 62 | |
7a22745a LP |
63 | case AF_INET: |
64 | if (a->size != sizeof(struct sockaddr_in)) | |
65 | return -EINVAL; | |
42f4e3c4 | 66 | |
4d49b48c | 67 | if (a->sockaddr.in.sin_port == 0) |
7a22745a | 68 | return -EINVAL; |
42f4e3c4 | 69 | |
44ab2347 | 70 | if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM)) |
7a22745a | 71 | return -EINVAL; |
42f4e3c4 | 72 | |
7a22745a LP |
73 | return 0; |
74 | ||
75 | case AF_INET6: | |
76 | if (a->size != sizeof(struct sockaddr_in6)) | |
77 | return -EINVAL; | |
42f4e3c4 | 78 | |
7a22745a LP |
79 | if (a->sockaddr.in6.sin6_port == 0) |
80 | return -EINVAL; | |
42f4e3c4 | 81 | |
44ab2347 | 82 | if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM)) |
7a22745a | 83 | return -EINVAL; |
42f4e3c4 | 84 | |
7a22745a | 85 | return 0; |
42f4e3c4 | 86 | |
7a22745a LP |
87 | case AF_UNIX: |
88 | if (a->size < offsetof(struct sockaddr_un, sun_path)) | |
89 | return -EINVAL; | |
15dca371 ZJS |
90 | if (a->size > sizeof(struct sockaddr_un) + !strict) |
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. */ | |
8e8132c6 | 93 | return -EINVAL; |
42f4e3c4 | 94 | |
8e8132c6 | 95 | if (a->size > offsetof(struct sockaddr_un, sun_path) && |
15dca371 ZJS |
96 | a->sockaddr.un.sun_path[0] != 0 && |
97 | strict) { | |
98 | /* Only validate file system sockets here, and only in strict mode */ | |
8e8132c6 | 99 | const char *e; |
7a22745a | 100 | |
8e8132c6 LP |
101 | e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path)); |
102 | if (e) { | |
15dca371 | 103 | /* If there's an embedded NUL byte, make sure the size of the socket address matches it */ |
7a22745a LP |
104 | if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1) |
105 | return -EINVAL; | |
8e8132c6 | 106 | } else { |
d7b34e38 | 107 | /* If there's no embedded NUL byte, then the size needs to match the whole |
8e8132c6 LP |
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 | |
7802194a | 110 | * size if the path is non NUL terminated.) */ |
8e8132c6 LP |
111 | if (!IN_SET(a->size, sizeof(a->sockaddr.un.sun_path), sizeof(a->sockaddr.un.sun_path)+1)) |
112 | return -EINVAL; | |
42f4e3c4 | 113 | } |
7a22745a | 114 | } |
42f4e3c4 | 115 | |
44ab2347 | 116 | if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET)) |
7a22745a | 117 | return -EINVAL; |
42f4e3c4 | 118 | |
7a22745a LP |
119 | return 0; |
120 | ||
121 | case AF_NETLINK: | |
122 | ||
123 | if (a->size != sizeof(struct sockaddr_nl)) | |
124 | return -EINVAL; | |
125 | ||
44ab2347 | 126 | if (!IN_SET(a->type, 0, SOCK_RAW, SOCK_DGRAM)) |
7a22745a LP |
127 | return -EINVAL; |
128 | ||
129 | return 0; | |
130 | ||
0fc0f14b SH |
131 | case AF_VSOCK: |
132 | if (a->size != sizeof(struct sockaddr_vm)) | |
133 | return -EINVAL; | |
134 | ||
44ab2347 | 135 | if (!IN_SET(a->type, 0, SOCK_STREAM, SOCK_DGRAM)) |
0fc0f14b SH |
136 | return -EINVAL; |
137 | ||
138 | return 0; | |
139 | ||
7a22745a LP |
140 | default: |
141 | return -EAFNOSUPPORT; | |
42f4e3c4 LP |
142 | } |
143 | } | |
144 | ||
4d49b48c | 145 | int socket_address_print(const SocketAddress *a, char **ret) { |
42f4e3c4 | 146 | int r; |
4d49b48c | 147 | |
42f4e3c4 | 148 | assert(a); |
4d49b48c | 149 | assert(ret); |
42f4e3c4 | 150 | |
15dca371 ZJS |
151 | r = socket_address_verify(a, false); /* We do non-strict validation, because we want to be |
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. */ | |
4d49b48c | 155 | if (r < 0) |
42f4e3c4 LP |
156 | return r; |
157 | ||
4d49b48c | 158 | if (socket_address_family(a) == AF_NETLINK) { |
7fd1b19b | 159 | _cleanup_free_ char *sfamily = NULL; |
7a22745a | 160 | |
f8b69d1d | 161 | r = netlink_family_to_string_alloc(a->protocol, &sfamily); |
7a22745a | 162 | if (r < 0) |
f8b69d1d | 163 | return r; |
4d49b48c LP |
164 | |
165 | r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups); | |
8520cfa5 MS |
166 | if (r < 0) |
167 | return -ENOMEM; | |
7a22745a LP |
168 | |
169 | return 0; | |
170 | } | |
171 | ||
3b1c5241 | 172 | return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret); |
42f4e3c4 LP |
173 | } |
174 | ||
4f2d528d LP |
175 | bool socket_address_can_accept(const SocketAddress *a) { |
176 | assert(a); | |
177 | ||
178 | return | |
3742095b | 179 | IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET); |
4f2d528d | 180 | } |
a16e1123 LP |
181 | |
182 | bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) { | |
183 | assert(a); | |
184 | assert(b); | |
185 | ||
186 | /* Invalid addresses are unequal to all */ | |
15dca371 ZJS |
187 | if (socket_address_verify(a, false) < 0 || |
188 | socket_address_verify(b, false) < 0) | |
a16e1123 LP |
189 | return false; |
190 | ||
191 | if (a->type != b->type) | |
192 | return false; | |
193 | ||
a16e1123 LP |
194 | if (socket_address_family(a) != socket_address_family(b)) |
195 | return false; | |
196 | ||
197 | switch (socket_address_family(a)) { | |
198 | ||
199 | case AF_INET: | |
4d49b48c | 200 | if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr) |
a16e1123 LP |
201 | return false; |
202 | ||
4d49b48c | 203 | if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port) |
a16e1123 LP |
204 | return false; |
205 | ||
206 | break; | |
207 | ||
208 | case AF_INET6: | |
209 | if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0) | |
210 | return false; | |
211 | ||
212 | if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port) | |
213 | return false; | |
214 | ||
215 | break; | |
216 | ||
217 | case AF_UNIX: | |
710708a5 MS |
218 | if (a->size <= offsetof(struct sockaddr_un, sun_path) || |
219 | b->size <= offsetof(struct sockaddr_un, sun_path)) | |
220 | return false; | |
221 | ||
a16e1123 LP |
222 | if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0)) |
223 | return false; | |
224 | ||
225 | if (a->sockaddr.un.sun_path[0]) { | |
563e6846 | 226 | if (!path_equal_or_inode_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0)) |
a16e1123 LP |
227 | return false; |
228 | } else { | |
c78e47a6 MS |
229 | if (a->size != b->size) |
230 | return false; | |
231 | ||
b12c1e7c | 232 | if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0) |
a16e1123 LP |
233 | return false; |
234 | } | |
235 | ||
236 | break; | |
237 | ||
7a22745a | 238 | case AF_NETLINK: |
7a22745a LP |
239 | if (a->protocol != b->protocol) |
240 | return false; | |
241 | ||
242 | if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups) | |
243 | return false; | |
244 | ||
245 | break; | |
246 | ||
0fc0f14b SH |
247 | case AF_VSOCK: |
248 | if (a->sockaddr.vm.svm_cid != b->sockaddr.vm.svm_cid) | |
249 | return false; | |
250 | ||
251 | if (a->sockaddr.vm.svm_port != b->sockaddr.vm.svm_port) | |
252 | return false; | |
253 | ||
254 | break; | |
255 | ||
a16e1123 LP |
256 | default: |
257 | /* Cannot compare, so we assume the addresses are different */ | |
258 | return false; | |
259 | } | |
260 | ||
261 | return true; | |
262 | } | |
263 | ||
a57f7e2c | 264 | const char* socket_address_get_path(const SocketAddress *a) { |
6e2ef85b LP |
265 | assert(a); |
266 | ||
267 | if (socket_address_family(a) != AF_UNIX) | |
a57f7e2c | 268 | return NULL; |
6e2ef85b LP |
269 | |
270 | if (a->sockaddr.un.sun_path[0] == 0) | |
a57f7e2c | 271 | return NULL; |
a16e1123 | 272 | |
48e6a2dc LP |
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); | |
a57f7e2c | 277 | return a->sockaddr.un.sun_path; |
a16e1123 | 278 | } |
c0120d99 | 279 | |
5bfcc1c6 | 280 | bool socket_ipv6_is_supported(void) { |
571ec995 | 281 | static int cached = -1; |
f89f1e8f | 282 | |
571ec995 LP |
283 | if (cached < 0) { |
284 | ||
285 | if (access("/proc/net/if_inet6", F_OK) < 0) { | |
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; | |
295 | } | |
296 | ||
297 | return cached; | |
5bfcc1c6 FF |
298 | } |
299 | ||
83e03c4f | 300 | bool socket_ipv6_is_enabled(void) { |
f96f5d54 | 301 | _cleanup_free_ char *v = NULL; |
83e03c4f LP |
302 | int r; |
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()) | |
90ab5042 | 308 | return false; |
f89f1e8f | 309 | |
83e03c4f LP |
310 | r = sysctl_read_ip_property(AF_INET6, "all", "disable_ipv6", &v); |
311 | if (r < 0) { | |
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); | |
317 | if (r < 0) { | |
318 | log_debug_errno(r, "Failed to pare 'net.ipv6.conf.all.disable_ipv6' sysctl: %m"); | |
319 | return true; | |
320 | } | |
321 | ||
322 | return !r; | |
5bfcc1c6 FF |
323 | } |
324 | ||
01e10de3 | 325 | bool socket_address_matches_fd(const SocketAddress *a, int fd) { |
dbafedac MS |
326 | SocketAddress b; |
327 | socklen_t solen; | |
01e10de3 LP |
328 | |
329 | assert(a); | |
330 | assert(fd >= 0); | |
331 | ||
dbafedac MS |
332 | b.size = sizeof(b.sockaddr); |
333 | if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0) | |
01e10de3 LP |
334 | return false; |
335 | ||
dbafedac | 336 | if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family) |
01e10de3 LP |
337 | return false; |
338 | ||
dbafedac MS |
339 | solen = sizeof(b.type); |
340 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0) | |
01e10de3 LP |
341 | return false; |
342 | ||
dbafedac | 343 | if (b.type != a->type) |
01e10de3 LP |
344 | return false; |
345 | ||
346 | if (a->protocol != 0) { | |
dbafedac MS |
347 | solen = sizeof(b.protocol); |
348 | if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0) | |
01e10de3 LP |
349 | return false; |
350 | ||
dbafedac | 351 | if (b.protocol != a->protocol) |
01e10de3 LP |
352 | return false; |
353 | } | |
354 | ||
02233928 | 355 | return socket_address_equal(a, &b); |
01e10de3 LP |
356 | } |
357 | ||
f6aac5bf | 358 | int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) { |
31325971 | 359 | const union sockaddr_union *sa = (const union sockaddr_union*) _sa; |
3b1c5241 | 360 | |
f6aac5bf LP |
361 | /* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */ |
362 | ||
3b1c5241 SL |
363 | assert(sa); |
364 | ||
0fc0f14b | 365 | switch (sa->sa.sa_family) { |
f6aac5bf | 366 | |
0fc0f14b | 367 | case AF_INET: |
f6aac5bf | 368 | *ret_port = be16toh(sa->in.sin_port); |
0fc0f14b | 369 | return 0; |
3b1c5241 | 370 | |
0fc0f14b | 371 | case AF_INET6: |
f6aac5bf | 372 | *ret_port = be16toh(sa->in6.sin6_port); |
0fc0f14b SH |
373 | return 0; |
374 | ||
375 | case AF_VSOCK: | |
f6aac5bf | 376 | *ret_port = sa->vm.svm_port; |
0fc0f14b SH |
377 | return 0; |
378 | ||
379 | default: | |
380 | return -EAFNOSUPPORT; | |
381 | } | |
3b1c5241 SL |
382 | } |
383 | ||
31325971 LP |
384 | const union in_addr_union *sockaddr_in_addr(const struct sockaddr *_sa) { |
385 | const union sockaddr_union *sa = (const union sockaddr_union*) _sa; | |
386 | ||
387 | if (!sa) | |
388 | return NULL; | |
389 | ||
390 | switch (sa->sa.sa_family) { | |
391 | ||
392 | case AF_INET: | |
393 | return (const union in_addr_union*) &sa->in.sin_addr; | |
394 | ||
395 | case AF_INET6: | |
396 | return (const union in_addr_union*) &sa->in6.sin6_addr; | |
397 | ||
398 | default: | |
399 | return NULL; | |
400 | } | |
401 | } | |
402 | ||
c1b91f06 LP |
403 | int sockaddr_set_in_addr( |
404 | union sockaddr_union *u, | |
405 | int family, | |
406 | const union in_addr_union *a, | |
407 | uint16_t port) { | |
408 | ||
409 | assert(u); | |
410 | assert(a); | |
411 | ||
412 | switch (family) { | |
413 | ||
414 | case AF_INET: | |
415 | u->in = (struct sockaddr_in) { | |
416 | .sin_family = AF_INET, | |
417 | .sin_addr = a->in, | |
418 | .sin_port = htobe16(port), | |
419 | }; | |
420 | ||
421 | return 0; | |
422 | ||
423 | case AF_INET6: | |
424 | u->in6 = (struct sockaddr_in6) { | |
425 | .sin6_family = AF_INET6, | |
426 | .sin6_addr = a->in6, | |
427 | .sin6_port = htobe16(port), | |
428 | }; | |
429 | ||
430 | return 0; | |
431 | ||
432 | default: | |
433 | return -EAFNOSUPPORT; | |
434 | ||
435 | } | |
436 | } | |
437 | ||
836f9cfe LP |
438 | int sockaddr_pretty( |
439 | const struct sockaddr *_sa, | |
440 | socklen_t salen, | |
441 | bool translate_ipv6, | |
442 | bool include_port, | |
443 | char **ret) { | |
444 | ||
4d49b48c | 445 | union sockaddr_union *sa = (union sockaddr_union*) _sa; |
8569a776 | 446 | char *p; |
fc25ad25 | 447 | int r; |
8569a776 | 448 | |
4d49b48c LP |
449 | assert(sa); |
450 | assert(salen >= sizeof(sa->sa.sa_family)); | |
8569a776 | 451 | |
4d49b48c | 452 | switch (sa->sa.sa_family) { |
8569a776 LP |
453 | |
454 | case AF_INET: { | |
455 | uint32_t a; | |
456 | ||
8e38570e | 457 | a = be32toh(sa->in.sin_addr.s_addr); |
8569a776 | 458 | |
fc25ad25 ZJS |
459 | if (include_port) |
460 | r = asprintf(&p, | |
3b1c5241 SL |
461 | "%u.%u.%u.%u:%u", |
462 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF, | |
8e38570e | 463 | be16toh(sa->in.sin_port)); |
fc25ad25 ZJS |
464 | else |
465 | r = asprintf(&p, | |
3b1c5241 | 466 | "%u.%u.%u.%u", |
fc25ad25 ZJS |
467 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF); |
468 | if (r < 0) | |
469 | return -ENOMEM; | |
8569a776 LP |
470 | break; |
471 | } | |
472 | ||
473 | case AF_INET6: { | |
474 | static const unsigned char ipv4_prefix[] = { | |
475 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF | |
476 | }; | |
477 | ||
fc25ad25 ZJS |
478 | if (translate_ipv6 && |
479 | memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) { | |
4d49b48c | 480 | const uint8_t *a = sa->in6.sin6_addr.s6_addr+12; |
fc25ad25 ZJS |
481 | if (include_port) |
482 | r = asprintf(&p, | |
3b1c5241 SL |
483 | "%u.%u.%u.%u:%u", |
484 | a[0], a[1], a[2], a[3], | |
8e38570e | 485 | be16toh(sa->in6.sin6_port)); |
fc25ad25 ZJS |
486 | else |
487 | r = asprintf(&p, | |
3b1c5241 | 488 | "%u.%u.%u.%u", |
fc25ad25 ZJS |
489 | a[0], a[1], a[2], a[3]); |
490 | if (r < 0) | |
491 | return -ENOMEM; | |
8569a776 | 492 | } else { |
071e522e | 493 | const char *a = IN6_ADDR_TO_STRING(&sa->in6.sin6_addr); |
3b1c5241 SL |
494 | |
495 | if (include_port) { | |
01afd0f7 | 496 | if (asprintf(&p, |
b16d17a6 | 497 | "[%s]:%u%s%s", |
3b1c5241 | 498 | a, |
b16d17a6 ZJS |
499 | be16toh(sa->in6.sin6_port), |
500 | sa->in6.sin6_scope_id != 0 ? "%" : "", | |
01afd0f7 | 501 | FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX)) < 0) |
3b1c5241 SL |
502 | return -ENOMEM; |
503 | } else { | |
01afd0f7 YW |
504 | if (sa->in6.sin6_scope_id != 0) |
505 | p = strjoin(a, "%", FORMAT_IFNAME_FULL(sa->in6.sin6_scope_id, FORMAT_IFNAME_IFINDEX)); | |
506 | else | |
507 | p = strdup(a); | |
3b1c5241 SL |
508 | if (!p) |
509 | return -ENOMEM; | |
510 | } | |
8569a776 LP |
511 | } |
512 | ||
513 | break; | |
514 | } | |
515 | ||
4d49b48c | 516 | case AF_UNIX: |
15dca371 | 517 | if (salen <= offsetof(struct sockaddr_un, sun_path) || |
994b9d4e | 518 | (sa->un.sun_path[0] == 0 && salen == offsetof(struct sockaddr_un, sun_path) + 1)) |
15dca371 | 519 | /* The name must have at least one character (and the leading NUL does not count) */ |
4d49b48c | 520 | p = strdup("<unnamed>"); |
994b9d4e | 521 | else { |
085b39e9 LP |
522 | /* Note that we calculate the path pointer here through the .un_buffer[] field, in order to |
523 | * outtrick bounds checking tools such as ubsan, which are too smart for their own good: on | |
524 | * Linux the kernel may return sun_path[] data one byte longer than the declared size of the | |
525 | * field. */ | |
526 | char *path = (char*) sa->un_buffer + offsetof(struct sockaddr_un, sun_path); | |
15dca371 | 527 | size_t path_len = salen - offsetof(struct sockaddr_un, sun_path); |
8569a776 | 528 | |
085b39e9 | 529 | if (path[0] == 0) { |
15dca371 ZJS |
530 | /* Abstract socket. When parsing address information from, we |
531 | * explicitly reject overly long paths and paths with embedded NULs. | |
532 | * But we might get such a socket from the outside. Let's return | |
533 | * something meaningful and printable in this case. */ | |
4d49b48c | 534 | |
15dca371 | 535 | _cleanup_free_ char *e = NULL; |
4d49b48c | 536 | |
085b39e9 | 537 | e = cescape_length(path + 1, path_len - 1); |
15dca371 ZJS |
538 | if (!e) |
539 | return -ENOMEM; | |
4d49b48c | 540 | |
15dca371 ZJS |
541 | p = strjoin("@", e); |
542 | } else { | |
085b39e9 | 543 | if (path[path_len - 1] == '\0') |
15dca371 ZJS |
544 | /* We expect a terminating NUL and don't print it */ |
545 | path_len --; | |
546 | ||
085b39e9 | 547 | p = cescape_length(path, path_len); |
15dca371 | 548 | } |
4d49b48c | 549 | } |
994b9d4e LP |
550 | if (!p) |
551 | return -ENOMEM; | |
8569a776 LP |
552 | |
553 | break; | |
8569a776 | 554 | |
0fc0f14b | 555 | case AF_VSOCK: |
3a484991 ZJS |
556 | if (include_port) { |
557 | if (sa->vm.svm_cid == VMADDR_CID_ANY) | |
558 | r = asprintf(&p, "vsock::%u", sa->vm.svm_port); | |
559 | else | |
560 | r = asprintf(&p, "vsock:%u:%u", sa->vm.svm_cid, sa->vm.svm_port); | |
561 | } else | |
0fc0f14b SH |
562 | r = asprintf(&p, "vsock:%u", sa->vm.svm_cid); |
563 | if (r < 0) | |
564 | return -ENOMEM; | |
565 | break; | |
566 | ||
8569a776 | 567 | default: |
15411c0c | 568 | return -EOPNOTSUPP; |
8569a776 LP |
569 | } |
570 | ||
571 | *ret = p; | |
572 | return 0; | |
573 | } | |
574 | ||
366b7db4 | 575 | int getpeername_pretty(int fd, bool include_port, char **ret) { |
4d49b48c | 576 | union sockaddr_union sa; |
b31f535c | 577 | socklen_t salen = sizeof(sa); |
eff05270 | 578 | int r; |
4d49b48c LP |
579 | |
580 | assert(fd >= 0); | |
581 | assert(ret); | |
582 | ||
4d49b48c LP |
583 | if (getpeername(fd, &sa.sa, &salen) < 0) |
584 | return -errno; | |
585 | ||
586 | if (sa.sa.sa_family == AF_UNIX) { | |
a995ce47 | 587 | struct ucred ucred = UCRED_INVALID; |
4d49b48c LP |
588 | |
589 | /* UNIX connection sockets are anonymous, so let's use | |
590 | * PID/UID as pretty credentials instead */ | |
591 | ||
eff05270 LP |
592 | r = getpeercred(fd, &ucred); |
593 | if (r < 0) | |
594 | return r; | |
4d49b48c | 595 | |
de0671ee | 596 | if (asprintf(ret, "PID "PID_FMT"/UID "UID_FMT, ucred.pid, ucred.uid) < 0) |
4d49b48c LP |
597 | return -ENOMEM; |
598 | ||
599 | return 0; | |
600 | } | |
601 | ||
602 | /* For remote sockets we translate IPv6 addresses back to IPv4 | |
603 | * if applicable, since that's nicer. */ | |
604 | ||
366b7db4 | 605 | return sockaddr_pretty(&sa.sa, salen, true, include_port, ret); |
4d49b48c LP |
606 | } |
607 | ||
608 | int getsockname_pretty(int fd, char **ret) { | |
609 | union sockaddr_union sa; | |
b31f535c | 610 | socklen_t salen = sizeof(sa); |
4d49b48c LP |
611 | |
612 | assert(fd >= 0); | |
613 | assert(ret); | |
614 | ||
4d49b48c LP |
615 | if (getsockname(fd, &sa.sa, &salen) < 0) |
616 | return -errno; | |
617 | ||
618 | /* For local sockets we do not translate IPv6 addresses back | |
619 | * to IPv6 if applicable, since this is usually used for | |
620 | * listening sockets where the difference between IPv4 and | |
621 | * IPv6 matters. */ | |
622 | ||
3b1c5241 | 623 | return sockaddr_pretty(&sa.sa, salen, false, true, ret); |
4d49b48c LP |
624 | } |
625 | ||
b31f535c ZJS |
626 | int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret) { |
627 | int r; | |
628 | char host[NI_MAXHOST], *ret; | |
629 | ||
630 | assert(_ret); | |
631 | ||
6326a143 | 632 | r = getnameinfo(&sa->sa, salen, host, sizeof(host), NULL, 0, IDN_FLAGS); |
b31f535c | 633 | if (r != 0) { |
b31f535c ZJS |
634 | int saved_errno = errno; |
635 | ||
3b1c5241 | 636 | r = sockaddr_pretty(&sa->sa, salen, true, true, &ret); |
f647962d | 637 | if (r < 0) |
1938ac51 | 638 | return r; |
b31f535c | 639 | |
279d3c9c | 640 | log_debug_errno(saved_errno, "getnameinfo(%s) failed: %m", ret); |
cb651834 ZJS |
641 | } else { |
642 | ret = strdup(host); | |
643 | if (!ret) | |
1938ac51 | 644 | return -ENOMEM; |
cb651834 | 645 | } |
b31f535c ZJS |
646 | |
647 | *_ret = ret; | |
648 | return 0; | |
649 | } | |
650 | ||
7a22745a | 651 | static const char* const netlink_family_table[] = { |
0d7e34e3 ZJS |
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", | |
7a22745a | 665 | [NETLINK_KOBJECT_UEVENT] = "kobject-uevent", |
0d7e34e3 ZJS |
666 | [NETLINK_GENERIC] = "generic", |
667 | [NETLINK_SCSITRANSPORT] = "scsitransport", | |
668 | [NETLINK_ECRYPTFS] = "ecryptfs", | |
669 | [NETLINK_RDMA] = "rdma", | |
7a22745a LP |
670 | }; |
671 | ||
f8b69d1d | 672 | DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX); |
7a22745a | 673 | |
c0120d99 LP |
674 | static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = { |
675 | [SOCKET_ADDRESS_DEFAULT] = "default", | |
676 | [SOCKET_ADDRESS_BOTH] = "both", | |
677 | [SOCKET_ADDRESS_IPV6_ONLY] = "ipv6-only" | |
678 | }; | |
679 | ||
680 | DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only, SocketAddressBindIPv6Only); | |
f01e5736 | 681 | |
b54e98ef | 682 | SocketAddressBindIPv6Only socket_address_bind_ipv6_only_or_bool_from_string(const char *n) { |
6f90844f YW |
683 | int r; |
684 | ||
685 | r = parse_boolean(n); | |
686 | if (r > 0) | |
687 | return SOCKET_ADDRESS_IPV6_ONLY; | |
688 | if (r == 0) | |
689 | return SOCKET_ADDRESS_BOTH; | |
690 | ||
691 | return socket_address_bind_ipv6_only_from_string(n); | |
692 | } | |
693 | ||
f01e5736 LP |
694 | bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) { |
695 | assert(a); | |
696 | assert(b); | |
697 | ||
698 | if (a->sa.sa_family != b->sa.sa_family) | |
699 | return false; | |
700 | ||
701 | if (a->sa.sa_family == AF_INET) | |
702 | return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr; | |
703 | ||
704 | if (a->sa.sa_family == AF_INET6) | |
705 | return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0; | |
706 | ||
0fc0f14b SH |
707 | if (a->sa.sa_family == AF_VSOCK) |
708 | return a->vm.svm_cid == b->vm.svm_cid; | |
709 | ||
f01e5736 LP |
710 | return false; |
711 | } | |
2583fbea | 712 | |
d9d9b2a0 | 713 | int fd_set_sndbuf(int fd, size_t n, bool increase) { |
2583fbea LP |
714 | int r, value; |
715 | socklen_t l = sizeof(value); | |
716 | ||
1263c85e YW |
717 | if (n > INT_MAX) |
718 | return -ERANGE; | |
719 | ||
2583fbea | 720 | r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l); |
d9d9b2a0 | 721 | if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2) |
2583fbea LP |
722 | return 0; |
723 | ||
b92f3507 YW |
724 | /* First, try to set the buffer size with SO_SNDBUF. */ |
725 | r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, n); | |
726 | if (r < 0) | |
727 | return r; | |
2583fbea | 728 | |
b92f3507 YW |
729 | /* SO_SNDBUF above may set to the kernel limit, instead of the requested size. |
730 | * So, we need to check the actual buffer size here. */ | |
67f5ae2d | 731 | l = sizeof(value); |
b92f3507 | 732 | r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l); |
d9d9b2a0 | 733 | if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2) |
b92f3507 YW |
734 | return 1; |
735 | ||
736 | /* If we have the privileges we will ignore the kernel limit. */ | |
737 | r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, n); | |
738 | if (r < 0) | |
739 | return r; | |
2583fbea LP |
740 | |
741 | return 1; | |
742 | } | |
743 | ||
d9d9b2a0 | 744 | int fd_set_rcvbuf(int fd, size_t n, bool increase) { |
2583fbea LP |
745 | int r, value; |
746 | socklen_t l = sizeof(value); | |
747 | ||
1263c85e YW |
748 | if (n > INT_MAX) |
749 | return -ERANGE; | |
750 | ||
2583fbea | 751 | r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l); |
d9d9b2a0 | 752 | if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2) |
2583fbea LP |
753 | return 0; |
754 | ||
b92f3507 YW |
755 | /* First, try to set the buffer size with SO_RCVBUF. */ |
756 | r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, n); | |
757 | if (r < 0) | |
758 | return r; | |
2583fbea | 759 | |
b92f3507 YW |
760 | /* SO_RCVBUF above may set to the kernel limit, instead of the requested size. |
761 | * So, we need to check the actual buffer size here. */ | |
67f5ae2d | 762 | l = sizeof(value); |
b92f3507 | 763 | r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l); |
d9d9b2a0 | 764 | if (r >= 0 && l == sizeof(value) && increase ? (size_t) value >= n*2 : (size_t) value == n*2) |
b92f3507 YW |
765 | return 1; |
766 | ||
767 | /* If we have the privileges we will ignore the kernel limit. */ | |
768 | r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, n); | |
769 | if (r < 0) | |
770 | return r; | |
9e5b6496 | 771 | |
2583fbea LP |
772 | return 1; |
773 | } | |
774 | ||
775 | static const char* const ip_tos_table[] = { | |
0d7e34e3 ZJS |
776 | [IPTOS_LOWDELAY] = "low-delay", |
777 | [IPTOS_THROUGHPUT] = "throughput", | |
2583fbea | 778 | [IPTOS_RELIABILITY] = "reliability", |
0d7e34e3 | 779 | [IPTOS_LOWCOST] = "low-cost", |
2583fbea LP |
780 | }; |
781 | ||
782 | DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff); | |
783 | ||
5a3586db YW |
784 | bool ifname_valid_char(char a) { |
785 | if ((unsigned char) a >= 127U) | |
786 | return false; | |
787 | ||
788 | if ((unsigned char) a <= 32U) | |
789 | return false; | |
790 | ||
791 | if (IN_SET(a, | |
792 | ':', /* colons are used by the legacy "alias" interface logic */ | |
793 | '/', /* slashes cannot work, since we need to use network interfaces in sysfs paths, and in paths slashes are separators */ | |
794 | '%')) /* %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 */ | |
795 | return false; | |
796 | ||
797 | return true; | |
798 | } | |
799 | ||
2313524a | 800 | bool ifname_valid_full(const char *p, IfnameValidFlags flags) { |
ef76dff2 LP |
801 | bool numeric = true; |
802 | ||
803 | /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources | |
804 | * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We | |
805 | * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */ | |
806 | ||
2313524a ZJS |
807 | assert(!(flags & ~_IFNAME_VALID_ALL)); |
808 | ||
ef76dff2 LP |
809 | if (isempty(p)) |
810 | return false; | |
811 | ||
ee29bd5d LP |
812 | /* A valid ifindex? If so, it's valid iff IFNAME_VALID_NUMERIC is set */ |
813 | if (parse_ifindex(p) >= 0) | |
814 | return flags & IFNAME_VALID_NUMERIC; | |
815 | ||
2313524a | 816 | if (flags & IFNAME_VALID_ALTERNATIVE) { |
4252696a YW |
817 | if (strlen(p) >= ALTIFNAMSIZ) |
818 | return false; | |
819 | } else { | |
820 | if (strlen(p) >= IFNAMSIZ) | |
821 | return false; | |
822 | } | |
ef76dff2 | 823 | |
49bfc877 | 824 | if (dot_or_dot_dot(p)) |
ef76dff2 LP |
825 | return false; |
826 | ||
07a7441a LP |
827 | /* Let's refuse "all" and "default" as interface name, to avoid collisions with the special sysctl |
828 | * directories /proc/sys/net/{ipv4,ipv6}/conf/{all,default} */ | |
6aebfec3 | 829 | if (!FLAGS_SET(flags, IFNAME_VALID_SPECIAL) && STR_IN_SET(p, "all", "default")) |
07a7441a LP |
830 | return false; |
831 | ||
2313524a | 832 | for (const char *t = p; *t; t++) { |
5a3586db | 833 | if (!ifname_valid_char(*t)) |
ef76dff2 LP |
834 | return false; |
835 | ||
ff25d338 | 836 | numeric = numeric && ascii_isdigit(*t); |
ef76dff2 LP |
837 | } |
838 | ||
ee29bd5d LP |
839 | /* It's fully numeric but didn't parse as valid ifindex above? if so, it must be too large or zero or |
840 | * so, let's refuse that. */ | |
841 | if (numeric) | |
842 | return false; | |
ef76dff2 LP |
843 | |
844 | return true; | |
845 | } | |
846 | ||
26808948 SS |
847 | bool address_label_valid(const char *p) { |
848 | ||
849 | if (isempty(p)) | |
850 | return false; | |
851 | ||
852 | if (strlen(p) >= IFNAMSIZ) | |
853 | return false; | |
854 | ||
855 | while (*p) { | |
856 | if ((uint8_t) *p >= 127U) | |
857 | return false; | |
858 | ||
859 | if ((uint8_t) *p <= 31U) | |
860 | return false; | |
861 | p++; | |
862 | } | |
863 | ||
864 | return true; | |
865 | } | |
866 | ||
2583fbea LP |
867 | int getpeercred(int fd, struct ucred *ucred) { |
868 | socklen_t n = sizeof(struct ucred); | |
869 | struct ucred u; | |
870 | int r; | |
871 | ||
872 | assert(fd >= 0); | |
873 | assert(ucred); | |
874 | ||
875 | r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n); | |
876 | if (r < 0) | |
877 | return -errno; | |
878 | ||
879 | if (n != sizeof(struct ucred)) | |
880 | return -EIO; | |
881 | ||
bbcc701e LP |
882 | /* Check if the data is actually useful and not suppressed due to namespacing issues */ |
883 | if (!pid_is_valid(u.pid)) | |
2583fbea LP |
884 | return -ENODATA; |
885 | ||
bbcc701e LP |
886 | /* Note that we don't check UID/GID here, as namespace translation works differently there: instead of |
887 | * receiving in "invalid" user/group we get the overflow UID/GID. */ | |
888 | ||
2583fbea LP |
889 | *ucred = u; |
890 | return 0; | |
891 | } | |
892 | ||
893 | int getpeersec(int fd, char **ret) { | |
217d8967 | 894 | _cleanup_free_ char *s = NULL; |
2583fbea | 895 | socklen_t n = 64; |
2583fbea LP |
896 | |
897 | assert(fd >= 0); | |
898 | assert(ret); | |
899 | ||
217d8967 LP |
900 | for (;;) { |
901 | s = new0(char, n+1); | |
902 | if (!s) | |
903 | return -ENOMEM; | |
2583fbea | 904 | |
217d8967 LP |
905 | if (getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n) >= 0) |
906 | break; | |
2583fbea LP |
907 | |
908 | if (errno != ERANGE) | |
909 | return -errno; | |
910 | ||
217d8967 | 911 | s = mfree(s); |
2583fbea LP |
912 | } |
913 | ||
217d8967 | 914 | if (isempty(s)) |
2583fbea | 915 | return -EOPNOTSUPP; |
2583fbea | 916 | |
ae2a15bc | 917 | *ret = TAKE_PTR(s); |
217d8967 | 918 | |
2583fbea LP |
919 | return 0; |
920 | } | |
921 | ||
43f2c88d LP |
922 | int getpeergroups(int fd, gid_t **ret) { |
923 | socklen_t n = sizeof(gid_t) * 64; | |
924 | _cleanup_free_ gid_t *d = NULL; | |
925 | ||
75f40779 | 926 | assert(fd >= 0); |
43f2c88d LP |
927 | assert(ret); |
928 | ||
929 | for (;;) { | |
930 | d = malloc(n); | |
931 | if (!d) | |
932 | return -ENOMEM; | |
933 | ||
934 | if (getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, d, &n) >= 0) | |
935 | break; | |
936 | ||
937 | if (errno != ERANGE) | |
938 | return -errno; | |
939 | ||
940 | d = mfree(d); | |
941 | } | |
942 | ||
943 | assert_se(n % sizeof(gid_t) == 0); | |
944 | n /= sizeof(gid_t); | |
945 | ||
946 | if ((socklen_t) (int) n != n) | |
947 | return -E2BIG; | |
948 | ||
1cc6c93a | 949 | *ret = TAKE_PTR(d); |
43f2c88d LP |
950 | |
951 | return (int) n; | |
952 | } | |
953 | ||
d34673ec | 954 | ssize_t send_one_fd_iov_sa( |
726f4c47 ZJS |
955 | int transport_fd, |
956 | int fd, | |
f621b8d7 | 957 | const struct iovec *iov, size_t iovlen, |
726f4c47 ZJS |
958 | const struct sockaddr *sa, socklen_t len, |
959 | int flags) { | |
960 | ||
fb29cdbe | 961 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control = {}; |
2583fbea | 962 | struct msghdr mh = { |
726f4c47 ZJS |
963 | .msg_name = (struct sockaddr*) sa, |
964 | .msg_namelen = len, | |
f621b8d7 | 965 | .msg_iov = (struct iovec *)iov, |
d34673ec | 966 | .msg_iovlen = iovlen, |
2583fbea | 967 | }; |
d34673ec | 968 | ssize_t k; |
2583fbea LP |
969 | |
970 | assert(transport_fd >= 0); | |
2583fbea | 971 | |
d34673ec FB |
972 | /* |
973 | * We need either an FD or data to send. | |
974 | * If there's nothing, return an error. | |
975 | */ | |
976 | if (fd < 0 && !iov) | |
977 | return -EINVAL; | |
2583fbea | 978 | |
d34673ec FB |
979 | if (fd >= 0) { |
980 | struct cmsghdr *cmsg; | |
2583fbea | 981 | |
d34673ec FB |
982 | mh.msg_control = &control; |
983 | mh.msg_controllen = sizeof(control); | |
984 | ||
985 | cmsg = CMSG_FIRSTHDR(&mh); | |
986 | cmsg->cmsg_level = SOL_SOCKET; | |
987 | cmsg->cmsg_type = SCM_RIGHTS; | |
988 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | |
989 | memcpy(CMSG_DATA(cmsg), &fd, sizeof(int)); | |
d34673ec FB |
990 | } |
991 | k = sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags); | |
992 | if (k < 0) | |
993 | return (ssize_t) -errno; | |
994 | ||
995 | return k; | |
2583fbea LP |
996 | } |
997 | ||
d34673ec FB |
998 | int send_one_fd_sa( |
999 | int transport_fd, | |
1000 | int fd, | |
1001 | const struct sockaddr *sa, socklen_t len, | |
1002 | int flags) { | |
1003 | ||
1004 | assert(fd >= 0); | |
1005 | ||
1006 | return (int) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, sa, len, flags); | |
1007 | } | |
1008 | ||
1009 | ssize_t receive_one_fd_iov( | |
1010 | int transport_fd, | |
1011 | struct iovec *iov, size_t iovlen, | |
1012 | int flags, | |
1013 | int *ret_fd) { | |
1014 | ||
fb29cdbe | 1015 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(int))) control; |
2583fbea LP |
1016 | struct msghdr mh = { |
1017 | .msg_control = &control, | |
1018 | .msg_controllen = sizeof(control), | |
d34673ec FB |
1019 | .msg_iov = iov, |
1020 | .msg_iovlen = iovlen, | |
2583fbea | 1021 | }; |
dac556fa | 1022 | struct cmsghdr *found; |
d34673ec | 1023 | ssize_t k; |
2583fbea LP |
1024 | |
1025 | assert(transport_fd >= 0); | |
d34673ec | 1026 | assert(ret_fd); |
2583fbea LP |
1027 | |
1028 | /* | |
1029 | * Receive a single FD via @transport_fd. We don't care for | |
1030 | * the transport-type. We retrieve a single FD at most, so for | |
1031 | * packet-based transports, the caller must ensure to send | |
1032 | * only a single FD per packet. This is best used in | |
1033 | * combination with send_one_fd(). | |
1034 | */ | |
1035 | ||
3691bcf3 | 1036 | k = recvmsg_safe(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags); |
d34673ec | 1037 | if (k < 0) |
3691bcf3 | 1038 | return k; |
2583fbea | 1039 | |
dac556fa | 1040 | found = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, CMSG_LEN(sizeof(int))); |
3691bcf3 | 1041 | if (!found) { |
2583fbea | 1042 | cmsg_close_all(&mh); |
d34673ec | 1043 | |
3691bcf3 LP |
1044 | /* If didn't receive an FD or any data, return an error. */ |
1045 | if (k == 0) | |
1046 | return -EIO; | |
1047 | } | |
2583fbea | 1048 | |
d34673ec | 1049 | if (found) |
b1d02191 | 1050 | *ret_fd = *CMSG_TYPED_DATA(found, int); |
d34673ec | 1051 | else |
254d1313 | 1052 | *ret_fd = -EBADF; |
d34673ec FB |
1053 | |
1054 | return k; | |
1055 | } | |
1056 | ||
1057 | int receive_one_fd(int transport_fd, int flags) { | |
1058 | int fd; | |
1059 | ssize_t k; | |
1060 | ||
1061 | k = receive_one_fd_iov(transport_fd, NULL, 0, flags, &fd); | |
1062 | if (k == 0) | |
1063 | return fd; | |
1064 | ||
1065 | /* k must be negative, since receive_one_fd_iov() only returns | |
1066 | * a positive value if data was received through the iov. */ | |
1067 | assert(k < 0); | |
1068 | return (int) k; | |
2583fbea | 1069 | } |
4edc2c9b LP |
1070 | |
1071 | ssize_t next_datagram_size_fd(int fd) { | |
1072 | ssize_t l; | |
1073 | int k; | |
1074 | ||
1075 | /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will | |
96d49011 | 1076 | * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD doesn't |
4edc2c9b LP |
1077 | * do. This difference is actually of major importance as we need to be sure that the size returned here |
1078 | * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of | |
1079 | * the wrong size. */ | |
1080 | ||
1081 | l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC); | |
1082 | if (l < 0) { | |
3742095b | 1083 | if (IN_SET(errno, EOPNOTSUPP, EFAULT)) |
4edc2c9b LP |
1084 | goto fallback; |
1085 | ||
1086 | return -errno; | |
1087 | } | |
1088 | if (l == 0) | |
1089 | goto fallback; | |
1090 | ||
1091 | return l; | |
1092 | ||
1093 | fallback: | |
1094 | k = 0; | |
1095 | ||
1096 | /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD | |
1097 | * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */ | |
1098 | ||
1099 | if (ioctl(fd, FIONREAD, &k) < 0) | |
1100 | return -errno; | |
1101 | ||
1102 | return (ssize_t) k; | |
1103 | } | |
60d9771c | 1104 | |
67962036 ZJS |
1105 | /* Put a limit on how many times will attempt to call accept4(). We loop |
1106 | * only on "transient" errors, but let's make sure we don't loop forever. */ | |
1107 | #define MAX_FLUSH_ITERATIONS 1024 | |
1108 | ||
60d9771c LP |
1109 | int flush_accept(int fd) { |
1110 | ||
f3d75364 LP |
1111 | int r, b; |
1112 | socklen_t l = sizeof(b); | |
1113 | ||
644cb4cc ZJS |
1114 | /* Similar to flush_fd() but flushes all incoming connections by accepting and immediately closing |
1115 | * them. */ | |
f3d75364 LP |
1116 | |
1117 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &b, &l) < 0) | |
1118 | return -errno; | |
60d9771c | 1119 | |
f3d75364 | 1120 | assert(l == sizeof(b)); |
644cb4cc ZJS |
1121 | if (!b) /* Let's check if this socket accepts connections before calling accept(). accept4() can |
1122 | * return EOPNOTSUPP if the fd is not a listening socket, which we should treat as a fatal | |
1123 | * error, or in case the incoming TCP connection triggered a network issue, which we want to | |
1124 | * treat as a transient error. Thus, let's rule out the first reason for EOPNOTSUPP early, so | |
1125 | * we can loop safely on transient errors below. */ | |
f3d75364 | 1126 | return -ENOTTY; |
60d9771c | 1127 | |
67962036 | 1128 | for (unsigned iteration = 0;; iteration++) { |
60d9771c LP |
1129 | int cfd; |
1130 | ||
0f2d351f | 1131 | r = fd_wait_for_event(fd, POLLIN, 0); |
60d9771c | 1132 | if (r < 0) { |
0f2d351f | 1133 | if (r == -EINTR) |
60d9771c LP |
1134 | continue; |
1135 | ||
0f2d351f | 1136 | return r; |
4ff9bc2e LP |
1137 | } |
1138 | if (r == 0) | |
60d9771c LP |
1139 | return 0; |
1140 | ||
67962036 ZJS |
1141 | if (iteration >= MAX_FLUSH_ITERATIONS) |
1142 | return log_debug_errno(SYNTHETIC_ERRNO(EBUSY), | |
1143 | "Failed to flush connections within " STRINGIFY(MAX_FLUSH_ITERATIONS) " iterations."); | |
1144 | ||
60d9771c LP |
1145 | cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); |
1146 | if (cfd < 0) { | |
60d9771c LP |
1147 | if (errno == EAGAIN) |
1148 | return 0; | |
1149 | ||
4ff9bc2e LP |
1150 | if (ERRNO_IS_ACCEPT_AGAIN(errno)) |
1151 | continue; | |
1152 | ||
60d9771c LP |
1153 | return -errno; |
1154 | } | |
1155 | ||
4ff9bc2e | 1156 | safe_close(cfd); |
60d9771c LP |
1157 | } |
1158 | } | |
29206d46 LP |
1159 | |
1160 | struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length) { | |
1161 | struct cmsghdr *cmsg; | |
1162 | ||
1163 | assert(mh); | |
1164 | ||
1165 | CMSG_FOREACH(cmsg, mh) | |
1166 | if (cmsg->cmsg_level == level && | |
1167 | cmsg->cmsg_type == type && | |
1168 | (length == (socklen_t) -1 || length == cmsg->cmsg_len)) | |
1169 | return cmsg; | |
1170 | ||
1171 | return NULL; | |
1172 | } | |
429b4350 | 1173 | |
4836f4c6 YW |
1174 | void* cmsg_find_and_copy_data(struct msghdr *mh, int level, int type, void *buf, size_t buf_len) { |
1175 | struct cmsghdr *cmsg; | |
1176 | ||
1177 | assert(mh); | |
1178 | assert(buf); | |
1179 | assert(buf_len > 0); | |
1180 | ||
1181 | /* This is similar to cmsg_find_data(), but copy the found data to buf. This should be typically used | |
da890466 | 1182 | * when reading possibly unaligned data such as timestamp, as time_t is 64-bit and size_t is 32-bit on |
4836f4c6 YW |
1183 | * RISCV32. See issue #27241. */ |
1184 | ||
1185 | cmsg = cmsg_find(mh, level, type, CMSG_LEN(buf_len)); | |
1186 | if (!cmsg) | |
1187 | return NULL; | |
1188 | ||
1189 | return memcpy_safe(buf, CMSG_DATA(cmsg), buf_len); | |
1190 | } | |
1191 | ||
429b4350 LP |
1192 | int socket_ioctl_fd(void) { |
1193 | int fd; | |
1194 | ||
1195 | /* Create a socket to invoke the various network interface ioctl()s on. Traditionally only AF_INET was good for | |
1196 | * that. Since kernel 4.6 AF_NETLINK works for this too. We first try to use AF_INET hence, but if that's not | |
1197 | * available (for example, because it is made unavailable via SECCOMP or such), we'll fall back to the more | |
1198 | * generic AF_NETLINK. */ | |
1199 | ||
1200 | fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0); | |
1201 | if (fd < 0) | |
1202 | fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_GENERIC); | |
1203 | if (fd < 0) | |
1204 | return -errno; | |
1205 | ||
1206 | return fd; | |
1207 | } | |
9f20fc28 LP |
1208 | |
1209 | int sockaddr_un_unlink(const struct sockaddr_un *sa) { | |
1210 | const char *p, * nul; | |
1211 | ||
1212 | assert(sa); | |
1213 | ||
1214 | if (sa->sun_family != AF_UNIX) | |
1215 | return -EPROTOTYPE; | |
1216 | ||
1217 | if (sa->sun_path[0] == 0) /* Nothing to do for abstract sockets */ | |
1218 | return 0; | |
1219 | ||
1220 | /* The path in .sun_path is not necessarily NUL terminated. Let's fix that. */ | |
1221 | nul = memchr(sa->sun_path, 0, sizeof(sa->sun_path)); | |
1222 | if (nul) | |
1223 | p = sa->sun_path; | |
1224 | else | |
1225 | p = memdupa_suffix0(sa->sun_path, sizeof(sa->sun_path)); | |
1226 | ||
1227 | if (unlink(p) < 0) | |
1228 | return -errno; | |
1229 | ||
1230 | return 1; | |
1231 | } | |
5cf91ea9 LP |
1232 | |
1233 | int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path) { | |
1234 | size_t l; | |
1235 | ||
1236 | assert(ret); | |
1237 | assert(path); | |
1238 | ||
1239 | /* Initialize ret->sun_path from the specified argument. This will interpret paths starting with '@' as | |
1240 | * abstract namespace sockets, and those starting with '/' as regular filesystem sockets. It won't accept | |
1241 | * anything else (i.e. no relative paths), to avoid ambiguities. Note that this function cannot be used to | |
1242 | * reference paths in the abstract namespace that include NUL bytes in the name. */ | |
1243 | ||
1244 | l = strlen(path); | |
c097bf1f | 1245 | if (l < 2) |
5cf91ea9 LP |
1246 | return -EINVAL; |
1247 | if (!IN_SET(path[0], '/', '@')) | |
1248 | return -EINVAL; | |
5cf91ea9 LP |
1249 | |
1250 | /* Don't allow paths larger than the space in sockaddr_un. Note that we are a tiny bit more restrictive than | |
1251 | * the kernel is: we insist on NUL termination (both for abstract namespace and regular file system socket | |
1252 | * addresses!), which the kernel doesn't. We do this to reduce chance of incompatibility with other apps that | |
7802194a | 1253 | * do not expect non-NUL terminated file system path. */ |
5cf91ea9 | 1254 | if (l+1 > sizeof(ret->sun_path)) |
dfa2b389 LP |
1255 | return path[0] == '@' ? -EINVAL : -ENAMETOOLONG; /* return a recognizable error if this is |
1256 | * too long to fit into a sockaddr_un, but | |
1257 | * is a file system path, and thus might be | |
1258 | * connectible via O_PATH indirection. */ | |
5cf91ea9 LP |
1259 | |
1260 | *ret = (struct sockaddr_un) { | |
1261 | .sun_family = AF_UNIX, | |
1262 | }; | |
1263 | ||
1264 | if (path[0] == '@') { | |
1265 | /* Abstract namespace socket */ | |
1266 | memcpy(ret->sun_path + 1, path + 1, l); /* copy *with* trailing NUL byte */ | |
1267 | return (int) (offsetof(struct sockaddr_un, sun_path) + l); /* 🔥 *don't* 🔥 include trailing NUL in size */ | |
1268 | ||
1269 | } else { | |
1270 | assert(path[0] == '/'); | |
1271 | ||
1272 | /* File system socket */ | |
1273 | memcpy(ret->sun_path, path, l + 1); /* copy *with* trailing NUL byte */ | |
1274 | return (int) (offsetof(struct sockaddr_un, sun_path) + l + 1); /* include trailing NUL in size */ | |
1275 | } | |
1276 | } | |
5d594d01 LP |
1277 | |
1278 | int socket_bind_to_ifname(int fd, const char *ifname) { | |
1279 | assert(fd >= 0); | |
1280 | ||
1281 | /* Call with NULL to drop binding */ | |
1282 | ||
7c248223 | 1283 | return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifname, strlen_ptr(ifname))); |
5d594d01 LP |
1284 | } |
1285 | ||
1286 | int socket_bind_to_ifindex(int fd, int ifindex) { | |
01afd0f7 | 1287 | char ifname[IF_NAMESIZE]; |
5e958e1d | 1288 | int r; |
5d594d01 LP |
1289 | |
1290 | assert(fd >= 0); | |
1291 | ||
7c248223 | 1292 | if (ifindex <= 0) |
5d594d01 | 1293 | /* Drop binding */ |
7c248223 | 1294 | return RET_NERRNO(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, NULL, 0)); |
5d594d01 | 1295 | |
5e958e1d YW |
1296 | r = setsockopt_int(fd, SOL_SOCKET, SO_BINDTOIFINDEX, ifindex); |
1297 | if (r != -ENOPROTOOPT) | |
1298 | return r; | |
5d594d01 LP |
1299 | |
1300 | /* Fall back to SO_BINDTODEVICE on kernels < 5.0 which didn't have SO_BINDTOIFINDEX */ | |
01afd0f7 YW |
1301 | r = format_ifname(ifindex, ifname); |
1302 | if (r < 0) | |
1303 | return r; | |
5d594d01 LP |
1304 | |
1305 | return socket_bind_to_ifname(fd, ifname); | |
1306 | } | |
47eae6ce LP |
1307 | |
1308 | ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags) { | |
1309 | ssize_t n; | |
1310 | ||
1311 | /* A wrapper around recvmsg() that checks for MSG_CTRUNC, and turns it into an error, in a reasonably | |
1312 | * safe way, closing any SCM_RIGHTS fds in the error path. | |
1313 | * | |
1314 | * Note that unlike our usual coding style this might modify *msg on failure. */ | |
1315 | ||
1316 | n = recvmsg(sockfd, msg, flags); | |
1317 | if (n < 0) | |
1318 | return -errno; | |
1319 | ||
1320 | if (FLAGS_SET(msg->msg_flags, MSG_CTRUNC)) { | |
1321 | cmsg_close_all(msg); | |
1322 | return -EXFULL; /* a recognizable error code */ | |
1323 | } | |
1324 | ||
1325 | return n; | |
35a3eb9b LP |
1326 | } |
1327 | ||
5f64d2bf | 1328 | int socket_get_family(int fd) { |
35a3eb9b LP |
1329 | int af; |
1330 | socklen_t sl = sizeof(af); | |
1331 | ||
1332 | if (getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &af, &sl) < 0) | |
1333 | return -errno; | |
1334 | ||
5d0fe423 LP |
1335 | if (sl != sizeof(af)) |
1336 | return -EINVAL; | |
1337 | ||
1338 | return af; | |
1339 | } | |
1340 | ||
1341 | int socket_set_recvpktinfo(int fd, int af, bool b) { | |
5d0fe423 LP |
1342 | |
1343 | if (af == AF_UNSPEC) { | |
5f64d2bf LP |
1344 | af = socket_get_family(fd); |
1345 | if (af < 0) | |
1346 | return af; | |
5d0fe423 LP |
1347 | } |
1348 | ||
35a3eb9b LP |
1349 | switch (af) { |
1350 | ||
1351 | case AF_INET: | |
1352 | return setsockopt_int(fd, IPPROTO_IP, IP_PKTINFO, b); | |
47eae6ce | 1353 | |
35a3eb9b LP |
1354 | case AF_INET6: |
1355 | return setsockopt_int(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, b); | |
1356 | ||
1357 | case AF_NETLINK: | |
1358 | return setsockopt_int(fd, SOL_NETLINK, NETLINK_PKTINFO, b); | |
1359 | ||
2d6d4136 LP |
1360 | case AF_PACKET: |
1361 | return setsockopt_int(fd, SOL_PACKET, PACKET_AUXDATA, b); | |
1362 | ||
35a3eb9b LP |
1363 | default: |
1364 | return -EAFNOSUPPORT; | |
1365 | } | |
47eae6ce | 1366 | } |
5d0fe423 | 1367 | |
5d0fe423 LP |
1368 | int socket_set_unicast_if(int fd, int af, int ifi) { |
1369 | be32_t ifindex_be = htobe32(ifi); | |
5d0fe423 LP |
1370 | |
1371 | if (af == AF_UNSPEC) { | |
5f64d2bf LP |
1372 | af = socket_get_family(fd); |
1373 | if (af < 0) | |
1374 | return af; | |
5d0fe423 LP |
1375 | } |
1376 | ||
1377 | switch (af) { | |
1378 | ||
1379 | case AF_INET: | |
7c248223 | 1380 | return RET_NERRNO(setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex_be, sizeof(ifindex_be))); |
5d0fe423 LP |
1381 | |
1382 | case AF_INET6: | |
7c248223 | 1383 | return RET_NERRNO(setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex_be, sizeof(ifindex_be))); |
5d0fe423 LP |
1384 | |
1385 | default: | |
1386 | return -EAFNOSUPPORT; | |
1387 | } | |
1388 | } | |
1389 | ||
402506ce | 1390 | int socket_set_option(int fd, int af, int opt_ipv4, int opt_ipv6, int val) { |
5d0fe423 | 1391 | if (af == AF_UNSPEC) { |
5f64d2bf LP |
1392 | af = socket_get_family(fd); |
1393 | if (af < 0) | |
1394 | return af; | |
5d0fe423 LP |
1395 | } |
1396 | ||
1397 | switch (af) { | |
1398 | ||
1399 | case AF_INET: | |
402506ce | 1400 | return setsockopt_int(fd, IPPROTO_IP, opt_ipv4, val); |
5d0fe423 LP |
1401 | |
1402 | case AF_INET6: | |
402506ce | 1403 | return setsockopt_int(fd, IPPROTO_IPV6, opt_ipv6, val); |
5d0fe423 LP |
1404 | |
1405 | default: | |
1406 | return -EAFNOSUPPORT; | |
1407 | } | |
1408 | } | |
52975f86 LP |
1409 | |
1410 | int socket_get_mtu(int fd, int af, size_t *ret) { | |
1411 | int mtu, r; | |
1412 | ||
1413 | if (af == AF_UNSPEC) { | |
5f64d2bf LP |
1414 | af = socket_get_family(fd); |
1415 | if (af < 0) | |
1416 | return af; | |
52975f86 LP |
1417 | } |
1418 | ||
1419 | switch (af) { | |
1420 | ||
1421 | case AF_INET: | |
1422 | r = getsockopt_int(fd, IPPROTO_IP, IP_MTU, &mtu); | |
1423 | break; | |
1424 | ||
1425 | case AF_INET6: | |
1426 | r = getsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, &mtu); | |
1427 | break; | |
1428 | ||
1429 | default: | |
1430 | return -EAFNOSUPPORT; | |
1431 | } | |
1432 | ||
1433 | if (r < 0) | |
1434 | return r; | |
1435 | if (mtu <= 0) | |
1436 | return -EINVAL; | |
1437 | ||
1438 | *ret = (size_t) mtu; | |
1439 | return 0; | |
1440 | } | |
2679aee4 | 1441 | |
9a603dc2 | 1442 | static int connect_unix_path_simple(int fd, const char *path) { |
2679aee4 LP |
1443 | union sockaddr_union sa = { |
1444 | .un.sun_family = AF_UNIX, | |
1445 | }; | |
9a603dc2 | 1446 | size_t l; |
2679aee4 LP |
1447 | |
1448 | assert(fd >= 0); | |
2679aee4 LP |
1449 | assert(path); |
1450 | ||
9a603dc2 LP |
1451 | l = strlen(path); |
1452 | assert(l > 0); | |
1453 | assert(l < sizeof(sa.un.sun_path)); | |
1454 | ||
1455 | memcpy(sa.un.sun_path, path, l + 1); | |
1456 | return RET_NERRNO(connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + l + 1)); | |
1457 | } | |
1458 | ||
1459 | static int connect_unix_inode(int fd, int inode_fd) { | |
1460 | assert(fd >= 0); | |
1461 | assert(inode_fd >= 0); | |
1462 | ||
1463 | return connect_unix_path_simple(fd, FORMAT_PROC_FD_PATH(inode_fd)); | |
1464 | } | |
1465 | ||
1466 | int connect_unix_path(int fd, int dir_fd, const char *path) { | |
1467 | _cleanup_close_ int inode_fd = -EBADF; | |
1468 | ||
1469 | assert(fd >= 0); | |
1470 | assert(dir_fd == AT_FDCWD || dir_fd >= 0); | |
1471 | ||
2679aee4 LP |
1472 | /* Connects to the specified AF_UNIX socket in the file system. Works around the 108 byte size limit |
1473 | * in sockaddr_un, by going via O_PATH if needed. This hence works for any kind of path. */ | |
1474 | ||
9a603dc2 LP |
1475 | if (!path) |
1476 | return connect_unix_inode(fd, dir_fd); /* If no path is specified, then dir_fd refers to the socket inode to connect to. */ | |
2679aee4 LP |
1477 | |
1478 | /* Refuse zero length path early, to make sure AF_UNIX stack won't mistake this for an abstract | |
1479 | * namespace path, since first char is NUL */ | |
9a603dc2 | 1480 | if (isempty(path)) |
2679aee4 LP |
1481 | return -EINVAL; |
1482 | ||
9a603dc2 LP |
1483 | /* Shortcut for the simple case */ |
1484 | if (dir_fd == AT_FDCWD && strlen(path) < sizeof_field(struct sockaddr_un, sun_path)) | |
1485 | return connect_unix_path_simple(fd, path); | |
2679aee4 | 1486 | |
9a603dc2 LP |
1487 | /* If dir_fd is specified, then we need to go the indirect O_PATH route, because connectat() does not |
1488 | * exist. If the path is too long, we also need to take the indirect route, since we can't fit this | |
1489 | * into a sockaddr_un directly. */ | |
2679aee4 | 1490 | |
9a603dc2 LP |
1491 | inode_fd = openat(dir_fd, path, O_PATH|O_CLOEXEC); |
1492 | if (inode_fd < 0) | |
1493 | return -errno; | |
2679aee4 | 1494 | |
9a603dc2 | 1495 | return connect_unix_inode(fd, inode_fd); |
2679aee4 | 1496 | } |
747b5d96 LB |
1497 | |
1498 | int socket_address_parse_unix(SocketAddress *ret_address, const char *s) { | |
1499 | struct sockaddr_un un; | |
1500 | int r; | |
1501 | ||
1502 | assert(ret_address); | |
1503 | assert(s); | |
1504 | ||
1505 | if (!IN_SET(*s, '/', '@')) | |
1506 | return -EPROTO; | |
1507 | ||
1508 | r = sockaddr_un_set_path(&un, s); | |
1509 | if (r < 0) | |
1510 | return r; | |
1511 | ||
1512 | *ret_address = (SocketAddress) { | |
1513 | .sockaddr.un = un, | |
1514 | .size = r, | |
1515 | }; | |
1516 | ||
1517 | return 0; | |
1518 | } | |
1519 | ||
1520 | int socket_address_parse_vsock(SocketAddress *ret_address, const char *s) { | |
1521 | /* AF_VSOCK socket in vsock:cid:port notation */ | |
1522 | _cleanup_free_ char *n = NULL; | |
1523 | char *e, *cid_start; | |
1524 | unsigned port, cid; | |
c31984e3 | 1525 | int type, r; |
747b5d96 LB |
1526 | |
1527 | assert(ret_address); | |
1528 | assert(s); | |
1529 | ||
c31984e3 DDM |
1530 | if ((cid_start = startswith(s, "vsock:"))) |
1531 | type = 0; | |
1532 | else if ((cid_start = startswith(s, "vsock-dgram:"))) | |
1533 | type = SOCK_DGRAM; | |
1534 | else if ((cid_start = startswith(s, "vsock-seqpacket:"))) | |
1535 | type = SOCK_SEQPACKET; | |
1536 | else if ((cid_start = startswith(s, "vsock-stream:"))) | |
1537 | type = SOCK_STREAM; | |
1538 | else | |
747b5d96 LB |
1539 | return -EPROTO; |
1540 | ||
1541 | e = strchr(cid_start, ':'); | |
1542 | if (!e) | |
1543 | return -EINVAL; | |
1544 | ||
1545 | r = safe_atou(e+1, &port); | |
1546 | if (r < 0) | |
1547 | return r; | |
1548 | ||
1549 | n = strndup(cid_start, e - cid_start); | |
1550 | if (!n) | |
1551 | return -ENOMEM; | |
1552 | ||
1553 | if (isempty(n)) | |
1554 | cid = VMADDR_CID_ANY; | |
1555 | else { | |
1556 | r = safe_atou(n, &cid); | |
1557 | if (r < 0) | |
1558 | return r; | |
1559 | } | |
1560 | ||
1561 | *ret_address = (SocketAddress) { | |
1562 | .sockaddr.vm = { | |
1563 | .svm_cid = cid, | |
1564 | .svm_family = AF_VSOCK, | |
1565 | .svm_port = port, | |
1566 | }, | |
c31984e3 | 1567 | .type = type, |
747b5d96 LB |
1568 | .size = sizeof(struct sockaddr_vm), |
1569 | }; | |
1570 | ||
1571 | return 0; | |
1572 | } |