]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
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> |
07630cea | 14 | #include <string.h> |
07630cea | 15 | #include <unistd.h> |
42f4e3c4 | 16 | |
b5efdb8a | 17 | #include "alloc-util.h" |
15dca371 | 18 | #include "escape.h" |
2583fbea | 19 | #include "fd-util.h" |
07630cea | 20 | #include "fileio.h" |
f97b34a6 | 21 | #include "format-util.h" |
93cc7779 | 22 | #include "log.h" |
42f4e3c4 | 23 | #include "macro.h" |
07630cea | 24 | #include "missing.h" |
6bedfcbb | 25 | #include "parse-util.h" |
9eb977db | 26 | #include "path-util.h" |
dccca82b | 27 | #include "process-util.h" |
2583fbea | 28 | #include "socket-util.h" |
8b43440b | 29 | #include "string-table.h" |
07630cea | 30 | #include "string-util.h" |
ef76dff2 | 31 | #include "strv.h" |
ee104e11 | 32 | #include "user-util.h" |
ef76dff2 | 33 | #include "utf8.h" |
2eec67ac | 34 | #include "util.h" |
42f4e3c4 | 35 | |
349cc4a5 | 36 | #if ENABLE_IDN |
cadc80b8 | 37 | # define IDN_FLAGS NI_IDN |
6326a143 WB |
38 | #else |
39 | # define IDN_FLAGS 0 | |
40 | #endif | |
41 | ||
398ce0bc YW |
42 | static const char* const socket_address_type_table[] = { |
43 | [SOCK_STREAM] = "Stream", | |
44 | [SOCK_DGRAM] = "Datagram", | |
45 | [SOCK_RAW] = "Raw", | |
46 | [SOCK_RDM] = "ReliableDatagram", | |
47 | [SOCK_SEQPACKET] = "SequentialPacket", | |
48 | [SOCK_DCCP] = "DatagramCongestionControl", | |
49 | }; | |
50 | ||
51 | DEFINE_STRING_TABLE_LOOKUP(socket_address_type, int); | |
52 | ||
542563ba | 53 | int socket_address_parse(SocketAddress *a, const char *s) { |
8d30fcb9 YW |
54 | _cleanup_free_ char *n = NULL; |
55 | char *e; | |
4d49b48c | 56 | int r; |
42f4e3c4 LP |
57 | |
58 | assert(a); | |
59 | assert(s); | |
60 | ||
9ec30b27 LP |
61 | *a = (SocketAddress) { |
62 | .type = SOCK_STREAM, | |
63 | }; | |
42f4e3c4 LP |
64 | |
65 | if (*s == '[') { | |
89220e2f LP |
66 | uint16_t port; |
67 | ||
42f4e3c4 LP |
68 | /* IPv6 in [x:.....:z]:p notation */ |
69 | ||
4d49b48c LP |
70 | e = strchr(s+1, ']'); |
71 | if (!e) | |
42f4e3c4 LP |
72 | return -EINVAL; |
73 | ||
8d30fcb9 YW |
74 | n = strndup(s+1, e-s-1); |
75 | if (!n) | |
76 | return -ENOMEM; | |
42f4e3c4 LP |
77 | |
78 | errno = 0; | |
4d49b48c | 79 | if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0) |
8333c77e | 80 | return errno > 0 ? -errno : -EINVAL; |
42f4e3c4 LP |
81 | |
82 | e++; | |
83 | if (*e != ':') | |
84 | return -EINVAL; | |
85 | ||
86 | e++; | |
89220e2f | 87 | r = parse_ip_port(e, &port); |
4d49b48c | 88 | if (r < 0) |
42f4e3c4 LP |
89 | return r; |
90 | ||
42f4e3c4 | 91 | a->sockaddr.in6.sin6_family = AF_INET6; |
89220e2f | 92 | a->sockaddr.in6.sin6_port = htobe16(port); |
42f4e3c4 | 93 | a->size = sizeof(struct sockaddr_in6); |
42f4e3c4 LP |
94 | |
95 | } else if (*s == '/') { | |
96 | /* AF_UNIX socket */ | |
97 | ||
98 | size_t l; | |
99 | ||
100 | l = strlen(s); | |
057e8580 LP |
101 | if (l >= sizeof(a->sockaddr.un.sun_path)) /* Note that we refuse non-NUL-terminated sockets when |
102 | * parsing (the kernel itself is less strict here in what it | |
103 | * accepts) */ | |
42f4e3c4 LP |
104 | return -EINVAL; |
105 | ||
106 | a->sockaddr.un.sun_family = AF_UNIX; | |
107 | memcpy(a->sockaddr.un.sun_path, s, l); | |
0e098b15 | 108 | a->size = offsetof(struct sockaddr_un, sun_path) + l + 1; |
42f4e3c4 | 109 | |
1c24e7bd | 110 | } else if (*s == '@') { |
42f4e3c4 LP |
111 | /* Abstract AF_UNIX socket */ |
112 | size_t l; | |
113 | ||
114 | l = strlen(s+1); | |
15dca371 | 115 | if (l >= sizeof(a->sockaddr.un.sun_path) - 1) /* Note that we refuse non-NUL-terminated sockets here |
057e8580 LP |
116 | * when parsing, even though abstract namespace sockets |
117 | * explicitly allow embedded NUL bytes and don't consider | |
118 | * them special. But it's simply annoying to debug such | |
119 | * sockets. */ | |
42f4e3c4 LP |
120 | return -EINVAL; |
121 | ||
122 | a->sockaddr.un.sun_family = AF_UNIX; | |
123 | memcpy(a->sockaddr.un.sun_path+1, s+1, l); | |
0e098b15 | 124 | a->size = offsetof(struct sockaddr_un, sun_path) + 1 + l; |
42f4e3c4 | 125 | |
0fc0f14b SH |
126 | } else if (startswith(s, "vsock:")) { |
127 | /* AF_VSOCK socket in vsock:cid:port notation */ | |
fbd0b64f | 128 | const char *cid_start = s + STRLEN("vsock:"); |
89220e2f | 129 | unsigned port; |
0fc0f14b SH |
130 | |
131 | e = strchr(cid_start, ':'); | |
132 | if (!e) | |
133 | return -EINVAL; | |
134 | ||
89220e2f | 135 | r = safe_atou(e+1, &port); |
0fc0f14b SH |
136 | if (r < 0) |
137 | return r; | |
138 | ||
8d30fcb9 YW |
139 | n = strndup(cid_start, e - cid_start); |
140 | if (!n) | |
141 | return -ENOMEM; | |
142 | ||
0fc0f14b SH |
143 | if (!isempty(n)) { |
144 | r = safe_atou(n, &a->sockaddr.vm.svm_cid); | |
145 | if (r < 0) | |
146 | return r; | |
147 | } else | |
148 | a->sockaddr.vm.svm_cid = VMADDR_CID_ANY; | |
149 | ||
150 | a->sockaddr.vm.svm_family = AF_VSOCK; | |
89220e2f | 151 | a->sockaddr.vm.svm_port = port; |
0fc0f14b SH |
152 | a->size = sizeof(struct sockaddr_vm); |
153 | ||
42f4e3c4 | 154 | } else { |
89220e2f LP |
155 | uint16_t port; |
156 | ||
4d49b48c LP |
157 | e = strchr(s, ':'); |
158 | if (e) { | |
89220e2f | 159 | r = parse_ip_port(e + 1, &port); |
4d49b48c | 160 | if (r < 0) |
542563ba LP |
161 | return r; |
162 | ||
8d30fcb9 YW |
163 | n = strndup(s, e-s); |
164 | if (!n) | |
165 | return -ENOMEM; | |
42f4e3c4 | 166 | |
542563ba | 167 | /* IPv4 in w.x.y.z:p notation? */ |
4d49b48c LP |
168 | r = inet_pton(AF_INET, n, &a->sockaddr.in.sin_addr); |
169 | if (r < 0) | |
542563ba | 170 | return -errno; |
42f4e3c4 | 171 | |
542563ba LP |
172 | if (r > 0) { |
173 | /* Gotcha, it's a traditional IPv4 address */ | |
4d49b48c | 174 | a->sockaddr.in.sin_family = AF_INET; |
89220e2f | 175 | a->sockaddr.in.sin_port = htobe16(port); |
542563ba LP |
176 | a->size = sizeof(struct sockaddr_in); |
177 | } else { | |
178 | unsigned idx; | |
42f4e3c4 | 179 | |
4d49b48c | 180 | if (strlen(n) > IF_NAMESIZE-1) |
acbb0225 | 181 | return -EINVAL; |
acbb0225 | 182 | |
542563ba LP |
183 | /* Uh, our last resort, an interface name */ |
184 | idx = if_nametoindex(n); | |
83c60c9f | 185 | if (idx == 0) |
542563ba | 186 | return -EINVAL; |
42f4e3c4 | 187 | |
542563ba | 188 | a->sockaddr.in6.sin6_family = AF_INET6; |
89220e2f | 189 | a->sockaddr.in6.sin6_port = htobe16(port); |
542563ba | 190 | a->sockaddr.in6.sin6_scope_id = idx; |
83c60c9f | 191 | a->sockaddr.in6.sin6_addr = in6addr_any; |
542563ba LP |
192 | a->size = sizeof(struct sockaddr_in6); |
193 | } | |
42f4e3c4 LP |
194 | } else { |
195 | ||
196 | /* Just a port */ | |
89220e2f | 197 | r = parse_ip_port(s, &port); |
5198dabc | 198 | if (r < 0) |
42f4e3c4 LP |
199 | return r; |
200 | ||
5bfcc1c6 FF |
201 | if (socket_ipv6_is_supported()) { |
202 | a->sockaddr.in6.sin6_family = AF_INET6; | |
89220e2f | 203 | a->sockaddr.in6.sin6_port = htobe16(port); |
5bfcc1c6 FF |
204 | a->sockaddr.in6.sin6_addr = in6addr_any; |
205 | a->size = sizeof(struct sockaddr_in6); | |
206 | } else { | |
4d49b48c | 207 | a->sockaddr.in.sin_family = AF_INET; |
89220e2f | 208 | a->sockaddr.in.sin_port = htobe16(port); |
4d49b48c | 209 | a->sockaddr.in.sin_addr.s_addr = INADDR_ANY; |
5bfcc1c6 FF |
210 | a->size = sizeof(struct sockaddr_in); |
211 | } | |
42f4e3c4 LP |
212 | } |
213 | } | |
214 | ||
215 | return 0; | |
216 | } | |
217 | ||
7693146d LP |
218 | int socket_address_parse_and_warn(SocketAddress *a, const char *s) { |
219 | SocketAddress b; | |
220 | int r; | |
221 | ||
222 | /* Similar to socket_address_parse() but warns for IPv6 sockets when we don't support them. */ | |
223 | ||
224 | r = socket_address_parse(&b, s); | |
225 | if (r < 0) | |
226 | return r; | |
227 | ||
228 | if (!socket_ipv6_is_supported() && b.sockaddr.sa.sa_family == AF_INET6) { | |
229 | log_warning("Binding to IPv6 address not available since kernel does not support IPv6."); | |
230 | return -EAFNOSUPPORT; | |
231 | } | |
232 | ||
233 | *a = b; | |
234 | return 0; | |
235 | } | |
236 | ||
7a22745a | 237 | int socket_address_parse_netlink(SocketAddress *a, const char *s) { |
528a74a1 | 238 | _cleanup_free_ char *word = NULL; |
7a22745a | 239 | unsigned group = 0; |
528a74a1 YW |
240 | int family, r; |
241 | ||
7a22745a LP |
242 | assert(a); |
243 | assert(s); | |
244 | ||
245 | zero(*a); | |
246 | a->type = SOCK_RAW; | |
247 | ||
528a74a1 YW |
248 | r = extract_first_word(&s, &word, NULL, 0); |
249 | if (r < 0) | |
250 | return r; | |
251 | if (r == 0) | |
252 | return -EINVAL; | |
7a22745a | 253 | |
528a74a1 | 254 | family = netlink_family_from_string(word); |
f8b69d1d MS |
255 | if (family < 0) |
256 | return -EINVAL; | |
7a22745a | 257 | |
528a74a1 YW |
258 | if (!isempty(s)) { |
259 | r = safe_atou(s, &group); | |
260 | if (r < 0) | |
261 | return r; | |
262 | } | |
263 | ||
7a22745a LP |
264 | a->sockaddr.nl.nl_family = AF_NETLINK; |
265 | a->sockaddr.nl.nl_groups = group; | |
266 | ||
267 | a->type = SOCK_RAW; | |
268 | a->size = sizeof(struct sockaddr_nl); | |
269 | a->protocol = family; | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
15dca371 | 274 | int socket_address_verify(const SocketAddress *a, bool strict) { |
42f4e3c4 LP |
275 | assert(a); |
276 | ||
15dca371 ZJS |
277 | /* With 'strict' we enforce additional sanity constraints which are not set by the standard, |
278 | * but should only apply to sockets we create ourselves. */ | |
279 | ||
542563ba | 280 | switch (socket_address_family(a)) { |
42f4e3c4 | 281 | |
7a22745a LP |
282 | case AF_INET: |
283 | if (a->size != sizeof(struct sockaddr_in)) | |
284 | return -EINVAL; | |
42f4e3c4 | 285 | |
4d49b48c | 286 | if (a->sockaddr.in.sin_port == 0) |
7a22745a | 287 | return -EINVAL; |
42f4e3c4 | 288 | |
ec2ce0c5 | 289 | if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM)) |
7a22745a | 290 | return -EINVAL; |
42f4e3c4 | 291 | |
7a22745a LP |
292 | return 0; |
293 | ||
294 | case AF_INET6: | |
295 | if (a->size != sizeof(struct sockaddr_in6)) | |
296 | return -EINVAL; | |
42f4e3c4 | 297 | |
7a22745a LP |
298 | if (a->sockaddr.in6.sin6_port == 0) |
299 | return -EINVAL; | |
42f4e3c4 | 300 | |
ec2ce0c5 | 301 | if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM)) |
7a22745a | 302 | return -EINVAL; |
42f4e3c4 | 303 | |
7a22745a | 304 | return 0; |
42f4e3c4 | 305 | |
7a22745a LP |
306 | case AF_UNIX: |
307 | if (a->size < offsetof(struct sockaddr_un, sun_path)) | |
308 | return -EINVAL; | |
15dca371 ZJS |
309 | if (a->size > sizeof(struct sockaddr_un) + !strict) |
310 | /* If !strict, allow one extra byte, since getsockname() on Linux will append | |
311 | * a NUL byte if we have path sockets that are above sun_path's full size. */ | |
8e8132c6 | 312 | return -EINVAL; |
42f4e3c4 | 313 | |
8e8132c6 | 314 | if (a->size > offsetof(struct sockaddr_un, sun_path) && |
15dca371 ZJS |
315 | a->sockaddr.un.sun_path[0] != 0 && |
316 | strict) { | |
317 | /* Only validate file system sockets here, and only in strict mode */ | |
8e8132c6 | 318 | const char *e; |
7a22745a | 319 | |
8e8132c6 LP |
320 | e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path)); |
321 | if (e) { | |
15dca371 | 322 | /* If there's an embedded NUL byte, make sure the size of the socket address matches it */ |
7a22745a LP |
323 | if (a->size != offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1) |
324 | return -EINVAL; | |
8e8132c6 LP |
325 | } else { |
326 | /* If there's no embedded NUL byte, then then the size needs to match the whole | |
327 | * structure or the structure with one extra NUL byte suffixed. (Yeah, Linux is awful, | |
328 | * and considers both equivalent: getsockname() even extends sockaddr_un beyond its | |
329 | * size if the path is non NUL terminated.)*/ | |
330 | if (!IN_SET(a->size, sizeof(a->sockaddr.un.sun_path), sizeof(a->sockaddr.un.sun_path)+1)) | |
331 | return -EINVAL; | |
42f4e3c4 | 332 | } |
7a22745a | 333 | } |
42f4e3c4 | 334 | |
ec2ce0c5 | 335 | if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET)) |
7a22745a | 336 | return -EINVAL; |
42f4e3c4 | 337 | |
7a22745a LP |
338 | return 0; |
339 | ||
340 | case AF_NETLINK: | |
341 | ||
342 | if (a->size != sizeof(struct sockaddr_nl)) | |
343 | return -EINVAL; | |
344 | ||
ec2ce0c5 | 345 | if (!IN_SET(a->type, SOCK_RAW, SOCK_DGRAM)) |
7a22745a LP |
346 | return -EINVAL; |
347 | ||
348 | return 0; | |
349 | ||
0fc0f14b SH |
350 | case AF_VSOCK: |
351 | if (a->size != sizeof(struct sockaddr_vm)) | |
352 | return -EINVAL; | |
353 | ||
ec2ce0c5 | 354 | if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM)) |
0fc0f14b SH |
355 | return -EINVAL; |
356 | ||
357 | return 0; | |
358 | ||
7a22745a LP |
359 | default: |
360 | return -EAFNOSUPPORT; | |
42f4e3c4 LP |
361 | } |
362 | } | |
363 | ||
4d49b48c | 364 | int socket_address_print(const SocketAddress *a, char **ret) { |
42f4e3c4 | 365 | int r; |
4d49b48c | 366 | |
42f4e3c4 | 367 | assert(a); |
4d49b48c | 368 | assert(ret); |
42f4e3c4 | 369 | |
15dca371 ZJS |
370 | r = socket_address_verify(a, false); /* We do non-strict validation, because we want to be |
371 | * able to pretty-print any socket the kernel considers | |
372 | * valid. We still need to do validation to know if we | |
373 | * can meaningfully print the address. */ | |
4d49b48c | 374 | if (r < 0) |
42f4e3c4 LP |
375 | return r; |
376 | ||
4d49b48c | 377 | if (socket_address_family(a) == AF_NETLINK) { |
7fd1b19b | 378 | _cleanup_free_ char *sfamily = NULL; |
7a22745a | 379 | |
f8b69d1d | 380 | r = netlink_family_to_string_alloc(a->protocol, &sfamily); |
7a22745a | 381 | if (r < 0) |
f8b69d1d | 382 | return r; |
4d49b48c LP |
383 | |
384 | r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups); | |
8520cfa5 MS |
385 | if (r < 0) |
386 | return -ENOMEM; | |
7a22745a LP |
387 | |
388 | return 0; | |
389 | } | |
390 | ||
3b1c5241 | 391 | return sockaddr_pretty(&a->sockaddr.sa, a->size, false, true, ret); |
42f4e3c4 LP |
392 | } |
393 | ||
4f2d528d LP |
394 | bool socket_address_can_accept(const SocketAddress *a) { |
395 | assert(a); | |
396 | ||
397 | return | |
3742095b | 398 | IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET); |
4f2d528d | 399 | } |
a16e1123 LP |
400 | |
401 | bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) { | |
402 | assert(a); | |
403 | assert(b); | |
404 | ||
405 | /* Invalid addresses are unequal to all */ | |
15dca371 ZJS |
406 | if (socket_address_verify(a, false) < 0 || |
407 | socket_address_verify(b, false) < 0) | |
a16e1123 LP |
408 | return false; |
409 | ||
410 | if (a->type != b->type) | |
411 | return false; | |
412 | ||
a16e1123 LP |
413 | if (socket_address_family(a) != socket_address_family(b)) |
414 | return false; | |
415 | ||
416 | switch (socket_address_family(a)) { | |
417 | ||
418 | case AF_INET: | |
4d49b48c | 419 | if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr) |
a16e1123 LP |
420 | return false; |
421 | ||
4d49b48c | 422 | if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port) |
a16e1123 LP |
423 | return false; |
424 | ||
425 | break; | |
426 | ||
427 | case AF_INET6: | |
428 | if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0) | |
429 | return false; | |
430 | ||
431 | if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port) | |
432 | return false; | |
433 | ||
434 | break; | |
435 | ||
436 | case AF_UNIX: | |
710708a5 MS |
437 | if (a->size <= offsetof(struct sockaddr_un, sun_path) || |
438 | b->size <= offsetof(struct sockaddr_un, sun_path)) | |
439 | return false; | |
440 | ||
a16e1123 LP |
441 | if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0)) |
442 | return false; | |
443 | ||
444 | if (a->sockaddr.un.sun_path[0]) { | |
e3f791a2 | 445 | if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0)) |
a16e1123 LP |
446 | return false; |
447 | } else { | |
c78e47a6 MS |
448 | if (a->size != b->size) |
449 | return false; | |
450 | ||
b12c1e7c | 451 | if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0) |
a16e1123 LP |
452 | return false; |
453 | } | |
454 | ||
455 | break; | |
456 | ||
7a22745a | 457 | case AF_NETLINK: |
7a22745a LP |
458 | if (a->protocol != b->protocol) |
459 | return false; | |
460 | ||
461 | if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups) | |
462 | return false; | |
463 | ||
464 | break; | |
465 | ||
0fc0f14b SH |
466 | case AF_VSOCK: |
467 | if (a->sockaddr.vm.svm_cid != b->sockaddr.vm.svm_cid) | |
468 | return false; | |
469 | ||
470 | if (a->sockaddr.vm.svm_port != b->sockaddr.vm.svm_port) | |
471 | return false; | |
472 | ||
473 | break; | |
474 | ||
a16e1123 LP |
475 | default: |
476 | /* Cannot compare, so we assume the addresses are different */ | |
477 | return false; | |
478 | } | |
479 | ||
480 | return true; | |
481 | } | |
482 | ||
27ca8d7a | 483 | bool socket_address_is(const SocketAddress *a, const char *s, int type) { |
a16e1123 LP |
484 | struct SocketAddress b; |
485 | ||
486 | assert(a); | |
487 | assert(s); | |
488 | ||
489 | if (socket_address_parse(&b, s) < 0) | |
490 | return false; | |
491 | ||
27ca8d7a LP |
492 | b.type = type; |
493 | ||
a16e1123 | 494 | return socket_address_equal(a, &b); |
6e2ef85b LP |
495 | } |
496 | ||
7a22745a LP |
497 | bool socket_address_is_netlink(const SocketAddress *a, const char *s) { |
498 | struct SocketAddress b; | |
499 | ||
500 | assert(a); | |
501 | assert(s); | |
502 | ||
503 | if (socket_address_parse_netlink(&b, s) < 0) | |
504 | return false; | |
505 | ||
506 | return socket_address_equal(a, &b); | |
507 | } | |
508 | ||
a57f7e2c | 509 | const char* socket_address_get_path(const SocketAddress *a) { |
6e2ef85b LP |
510 | assert(a); |
511 | ||
512 | if (socket_address_family(a) != AF_UNIX) | |
a57f7e2c | 513 | return NULL; |
6e2ef85b LP |
514 | |
515 | if (a->sockaddr.un.sun_path[0] == 0) | |
a57f7e2c | 516 | return NULL; |
a16e1123 | 517 | |
48e6a2dc LP |
518 | /* Note that this is only safe because we know that there's an extra NUL byte after the sockaddr_un |
519 | * structure. On Linux AF_UNIX file system socket addresses don't have to be NUL terminated if they take up the | |
520 | * full sun_path space. */ | |
521 | assert_cc(sizeof(union sockaddr_union) >= sizeof(struct sockaddr_un)+1); | |
a57f7e2c | 522 | return a->sockaddr.un.sun_path; |
a16e1123 | 523 | } |
c0120d99 | 524 | |
5bfcc1c6 | 525 | bool socket_ipv6_is_supported(void) { |
629abfc2 | 526 | if (access("/proc/net/if_inet6", F_OK) != 0) |
90ab5042 | 527 | return false; |
f89f1e8f | 528 | |
7377964d | 529 | return true; |
5bfcc1c6 FF |
530 | } |
531 | ||
01e10de3 | 532 | bool socket_address_matches_fd(const SocketAddress *a, int fd) { |
dbafedac MS |
533 | SocketAddress b; |
534 | socklen_t solen; | |
01e10de3 LP |
535 | |
536 | assert(a); | |
537 | assert(fd >= 0); | |
538 | ||
dbafedac MS |
539 | b.size = sizeof(b.sockaddr); |
540 | if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0) | |
01e10de3 LP |
541 | return false; |
542 | ||
dbafedac | 543 | if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family) |
01e10de3 LP |
544 | return false; |
545 | ||
dbafedac MS |
546 | solen = sizeof(b.type); |
547 | if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &b.type, &solen) < 0) | |
01e10de3 LP |
548 | return false; |
549 | ||
dbafedac | 550 | if (b.type != a->type) |
01e10de3 LP |
551 | return false; |
552 | ||
553 | if (a->protocol != 0) { | |
dbafedac MS |
554 | solen = sizeof(b.protocol); |
555 | if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &b.protocol, &solen) < 0) | |
01e10de3 LP |
556 | return false; |
557 | ||
dbafedac | 558 | if (b.protocol != a->protocol) |
01e10de3 LP |
559 | return false; |
560 | } | |
561 | ||
02233928 | 562 | return socket_address_equal(a, &b); |
01e10de3 LP |
563 | } |
564 | ||
f6aac5bf | 565 | int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) { |
3b1c5241 SL |
566 | union sockaddr_union *sa = (union sockaddr_union*) _sa; |
567 | ||
f6aac5bf LP |
568 | /* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */ |
569 | ||
3b1c5241 SL |
570 | assert(sa); |
571 | ||
0fc0f14b | 572 | switch (sa->sa.sa_family) { |
f6aac5bf | 573 | |
0fc0f14b | 574 | case AF_INET: |
f6aac5bf | 575 | *ret_port = be16toh(sa->in.sin_port); |
0fc0f14b | 576 | return 0; |
3b1c5241 | 577 | |
0fc0f14b | 578 | case AF_INET6: |
f6aac5bf | 579 | *ret_port = be16toh(sa->in6.sin6_port); |
0fc0f14b SH |
580 | return 0; |
581 | ||
582 | case AF_VSOCK: | |
f6aac5bf | 583 | *ret_port = sa->vm.svm_port; |
0fc0f14b SH |
584 | return 0; |
585 | ||
586 | default: | |
587 | return -EAFNOSUPPORT; | |
588 | } | |
3b1c5241 SL |
589 | } |
590 | ||
836f9cfe LP |
591 | int sockaddr_pretty( |
592 | const struct sockaddr *_sa, | |
593 | socklen_t salen, | |
594 | bool translate_ipv6, | |
595 | bool include_port, | |
596 | char **ret) { | |
597 | ||
4d49b48c | 598 | union sockaddr_union *sa = (union sockaddr_union*) _sa; |
8569a776 | 599 | char *p; |
fc25ad25 | 600 | int r; |
8569a776 | 601 | |
4d49b48c LP |
602 | assert(sa); |
603 | assert(salen >= sizeof(sa->sa.sa_family)); | |
8569a776 | 604 | |
4d49b48c | 605 | switch (sa->sa.sa_family) { |
8569a776 LP |
606 | |
607 | case AF_INET: { | |
608 | uint32_t a; | |
609 | ||
8e38570e | 610 | a = be32toh(sa->in.sin_addr.s_addr); |
8569a776 | 611 | |
fc25ad25 ZJS |
612 | if (include_port) |
613 | r = asprintf(&p, | |
3b1c5241 SL |
614 | "%u.%u.%u.%u:%u", |
615 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF, | |
8e38570e | 616 | be16toh(sa->in.sin_port)); |
fc25ad25 ZJS |
617 | else |
618 | r = asprintf(&p, | |
3b1c5241 | 619 | "%u.%u.%u.%u", |
fc25ad25 ZJS |
620 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF); |
621 | if (r < 0) | |
622 | return -ENOMEM; | |
8569a776 LP |
623 | break; |
624 | } | |
625 | ||
626 | case AF_INET6: { | |
627 | static const unsigned char ipv4_prefix[] = { | |
628 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF | |
629 | }; | |
630 | ||
fc25ad25 ZJS |
631 | if (translate_ipv6 && |
632 | memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) { | |
4d49b48c | 633 | const uint8_t *a = sa->in6.sin6_addr.s6_addr+12; |
fc25ad25 ZJS |
634 | if (include_port) |
635 | r = asprintf(&p, | |
3b1c5241 SL |
636 | "%u.%u.%u.%u:%u", |
637 | a[0], a[1], a[2], a[3], | |
8e38570e | 638 | be16toh(sa->in6.sin6_port)); |
fc25ad25 ZJS |
639 | else |
640 | r = asprintf(&p, | |
3b1c5241 | 641 | "%u.%u.%u.%u", |
fc25ad25 ZJS |
642 | a[0], a[1], a[2], a[3]); |
643 | if (r < 0) | |
644 | return -ENOMEM; | |
8569a776 LP |
645 | } else { |
646 | char a[INET6_ADDRSTRLEN]; | |
647 | ||
3b1c5241 SL |
648 | inet_ntop(AF_INET6, &sa->in6.sin6_addr, a, sizeof(a)); |
649 | ||
650 | if (include_port) { | |
fc25ad25 | 651 | r = asprintf(&p, |
3b1c5241 SL |
652 | "[%s]:%u", |
653 | a, | |
8e38570e | 654 | be16toh(sa->in6.sin6_port)); |
fc25ad25 | 655 | if (r < 0) |
3b1c5241 SL |
656 | return -ENOMEM; |
657 | } else { | |
658 | p = strdup(a); | |
659 | if (!p) | |
660 | return -ENOMEM; | |
661 | } | |
8569a776 LP |
662 | } |
663 | ||
664 | break; | |
665 | } | |
666 | ||
4d49b48c | 667 | case AF_UNIX: |
15dca371 | 668 | if (salen <= offsetof(struct sockaddr_un, sun_path) || |
994b9d4e | 669 | (sa->un.sun_path[0] == 0 && salen == offsetof(struct sockaddr_un, sun_path) + 1)) |
15dca371 | 670 | /* The name must have at least one character (and the leading NUL does not count) */ |
4d49b48c | 671 | p = strdup("<unnamed>"); |
994b9d4e | 672 | else { |
085b39e9 LP |
673 | /* Note that we calculate the path pointer here through the .un_buffer[] field, in order to |
674 | * outtrick bounds checking tools such as ubsan, which are too smart for their own good: on | |
675 | * Linux the kernel may return sun_path[] data one byte longer than the declared size of the | |
676 | * field. */ | |
677 | char *path = (char*) sa->un_buffer + offsetof(struct sockaddr_un, sun_path); | |
15dca371 | 678 | size_t path_len = salen - offsetof(struct sockaddr_un, sun_path); |
8569a776 | 679 | |
085b39e9 | 680 | if (path[0] == 0) { |
15dca371 ZJS |
681 | /* Abstract socket. When parsing address information from, we |
682 | * explicitly reject overly long paths and paths with embedded NULs. | |
683 | * But we might get such a socket from the outside. Let's return | |
684 | * something meaningful and printable in this case. */ | |
4d49b48c | 685 | |
15dca371 | 686 | _cleanup_free_ char *e = NULL; |
4d49b48c | 687 | |
085b39e9 | 688 | e = cescape_length(path + 1, path_len - 1); |
15dca371 ZJS |
689 | if (!e) |
690 | return -ENOMEM; | |
4d49b48c | 691 | |
15dca371 ZJS |
692 | p = strjoin("@", e); |
693 | } else { | |
085b39e9 | 694 | if (path[path_len - 1] == '\0') |
15dca371 ZJS |
695 | /* We expect a terminating NUL and don't print it */ |
696 | path_len --; | |
697 | ||
085b39e9 | 698 | p = cescape_length(path, path_len); |
15dca371 | 699 | } |
4d49b48c | 700 | } |
994b9d4e LP |
701 | if (!p) |
702 | return -ENOMEM; | |
8569a776 LP |
703 | |
704 | break; | |
8569a776 | 705 | |
0fc0f14b | 706 | case AF_VSOCK: |
3a484991 ZJS |
707 | if (include_port) { |
708 | if (sa->vm.svm_cid == VMADDR_CID_ANY) | |
709 | r = asprintf(&p, "vsock::%u", sa->vm.svm_port); | |
710 | else | |
711 | r = asprintf(&p, "vsock:%u:%u", sa->vm.svm_cid, sa->vm.svm_port); | |
712 | } else | |
0fc0f14b SH |
713 | r = asprintf(&p, "vsock:%u", sa->vm.svm_cid); |
714 | if (r < 0) | |
715 | return -ENOMEM; | |
716 | break; | |
717 | ||
8569a776 | 718 | default: |
15411c0c | 719 | return -EOPNOTSUPP; |
8569a776 LP |
720 | } |
721 | ||
722 | *ret = p; | |
723 | return 0; | |
724 | } | |
725 | ||
366b7db4 | 726 | int getpeername_pretty(int fd, bool include_port, char **ret) { |
4d49b48c | 727 | union sockaddr_union sa; |
b31f535c | 728 | socklen_t salen = sizeof(sa); |
eff05270 | 729 | int r; |
4d49b48c LP |
730 | |
731 | assert(fd >= 0); | |
732 | assert(ret); | |
733 | ||
4d49b48c LP |
734 | if (getpeername(fd, &sa.sa, &salen) < 0) |
735 | return -errno; | |
736 | ||
737 | if (sa.sa.sa_family == AF_UNIX) { | |
39883f62 | 738 | struct ucred ucred = {}; |
4d49b48c LP |
739 | |
740 | /* UNIX connection sockets are anonymous, so let's use | |
741 | * PID/UID as pretty credentials instead */ | |
742 | ||
eff05270 LP |
743 | r = getpeercred(fd, &ucred); |
744 | if (r < 0) | |
745 | return r; | |
4d49b48c | 746 | |
de0671ee | 747 | if (asprintf(ret, "PID "PID_FMT"/UID "UID_FMT, ucred.pid, ucred.uid) < 0) |
4d49b48c LP |
748 | return -ENOMEM; |
749 | ||
750 | return 0; | |
751 | } | |
752 | ||
753 | /* For remote sockets we translate IPv6 addresses back to IPv4 | |
754 | * if applicable, since that's nicer. */ | |
755 | ||
366b7db4 | 756 | return sockaddr_pretty(&sa.sa, salen, true, include_port, ret); |
4d49b48c LP |
757 | } |
758 | ||
759 | int getsockname_pretty(int fd, char **ret) { | |
760 | union sockaddr_union sa; | |
b31f535c | 761 | socklen_t salen = sizeof(sa); |
4d49b48c LP |
762 | |
763 | assert(fd >= 0); | |
764 | assert(ret); | |
765 | ||
4d49b48c LP |
766 | if (getsockname(fd, &sa.sa, &salen) < 0) |
767 | return -errno; | |
768 | ||
769 | /* For local sockets we do not translate IPv6 addresses back | |
770 | * to IPv6 if applicable, since this is usually used for | |
771 | * listening sockets where the difference between IPv4 and | |
772 | * IPv6 matters. */ | |
773 | ||
3b1c5241 | 774 | return sockaddr_pretty(&sa.sa, salen, false, true, ret); |
4d49b48c LP |
775 | } |
776 | ||
b31f535c ZJS |
777 | int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret) { |
778 | int r; | |
779 | char host[NI_MAXHOST], *ret; | |
780 | ||
781 | assert(_ret); | |
782 | ||
6326a143 | 783 | r = getnameinfo(&sa->sa, salen, host, sizeof(host), NULL, 0, IDN_FLAGS); |
b31f535c | 784 | if (r != 0) { |
b31f535c ZJS |
785 | int saved_errno = errno; |
786 | ||
3b1c5241 | 787 | r = sockaddr_pretty(&sa->sa, salen, true, true, &ret); |
f647962d | 788 | if (r < 0) |
1938ac51 | 789 | return r; |
b31f535c | 790 | |
279d3c9c | 791 | log_debug_errno(saved_errno, "getnameinfo(%s) failed: %m", ret); |
cb651834 ZJS |
792 | } else { |
793 | ret = strdup(host); | |
794 | if (!ret) | |
1938ac51 | 795 | return -ENOMEM; |
cb651834 | 796 | } |
b31f535c ZJS |
797 | |
798 | *_ret = ret; | |
799 | return 0; | |
800 | } | |
801 | ||
7a22745a LP |
802 | static const char* const netlink_family_table[] = { |
803 | [NETLINK_ROUTE] = "route", | |
804 | [NETLINK_FIREWALL] = "firewall", | |
805 | [NETLINK_INET_DIAG] = "inet-diag", | |
806 | [NETLINK_NFLOG] = "nflog", | |
807 | [NETLINK_XFRM] = "xfrm", | |
808 | [NETLINK_SELINUX] = "selinux", | |
809 | [NETLINK_ISCSI] = "iscsi", | |
810 | [NETLINK_AUDIT] = "audit", | |
811 | [NETLINK_FIB_LOOKUP] = "fib-lookup", | |
812 | [NETLINK_CONNECTOR] = "connector", | |
813 | [NETLINK_NETFILTER] = "netfilter", | |
814 | [NETLINK_IP6_FW] = "ip6-fw", | |
815 | [NETLINK_DNRTMSG] = "dnrtmsg", | |
816 | [NETLINK_KOBJECT_UEVENT] = "kobject-uevent", | |
817 | [NETLINK_GENERIC] = "generic", | |
818 | [NETLINK_SCSITRANSPORT] = "scsitransport", | |
5570d7f9 JG |
819 | [NETLINK_ECRYPTFS] = "ecryptfs", |
820 | [NETLINK_RDMA] = "rdma", | |
7a22745a LP |
821 | }; |
822 | ||
f8b69d1d | 823 | DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX); |
7a22745a | 824 | |
c0120d99 LP |
825 | static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = { |
826 | [SOCKET_ADDRESS_DEFAULT] = "default", | |
827 | [SOCKET_ADDRESS_BOTH] = "both", | |
828 | [SOCKET_ADDRESS_IPV6_ONLY] = "ipv6-only" | |
829 | }; | |
830 | ||
831 | DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only, SocketAddressBindIPv6Only); | |
f01e5736 | 832 | |
b54e98ef | 833 | SocketAddressBindIPv6Only socket_address_bind_ipv6_only_or_bool_from_string(const char *n) { |
6f90844f YW |
834 | int r; |
835 | ||
836 | r = parse_boolean(n); | |
837 | if (r > 0) | |
838 | return SOCKET_ADDRESS_IPV6_ONLY; | |
839 | if (r == 0) | |
840 | return SOCKET_ADDRESS_BOTH; | |
841 | ||
842 | return socket_address_bind_ipv6_only_from_string(n); | |
843 | } | |
844 | ||
f01e5736 LP |
845 | bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) { |
846 | assert(a); | |
847 | assert(b); | |
848 | ||
849 | if (a->sa.sa_family != b->sa.sa_family) | |
850 | return false; | |
851 | ||
852 | if (a->sa.sa_family == AF_INET) | |
853 | return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr; | |
854 | ||
855 | if (a->sa.sa_family == AF_INET6) | |
856 | return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0; | |
857 | ||
0fc0f14b SH |
858 | if (a->sa.sa_family == AF_VSOCK) |
859 | return a->vm.svm_cid == b->vm.svm_cid; | |
860 | ||
f01e5736 LP |
861 | return false; |
862 | } | |
2583fbea LP |
863 | |
864 | int fd_inc_sndbuf(int fd, size_t n) { | |
865 | int r, value; | |
866 | socklen_t l = sizeof(value); | |
867 | ||
868 | r = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &value, &l); | |
869 | if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) | |
870 | return 0; | |
871 | ||
872 | /* If we have the privileges we will ignore the kernel limit. */ | |
873 | ||
9e5b6496 YW |
874 | if (setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, n) < 0) { |
875 | r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, n); | |
876 | if (r < 0) | |
877 | return r; | |
878 | } | |
2583fbea LP |
879 | |
880 | return 1; | |
881 | } | |
882 | ||
883 | int fd_inc_rcvbuf(int fd, size_t n) { | |
884 | int r, value; | |
885 | socklen_t l = sizeof(value); | |
886 | ||
887 | r = getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &value, &l); | |
888 | if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) | |
889 | return 0; | |
890 | ||
891 | /* If we have the privileges we will ignore the kernel limit. */ | |
892 | ||
9e5b6496 YW |
893 | if (setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, n) < 0) { |
894 | r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, n); | |
895 | if (r < 0) | |
896 | return r; | |
897 | } | |
898 | ||
2583fbea LP |
899 | return 1; |
900 | } | |
901 | ||
902 | static const char* const ip_tos_table[] = { | |
903 | [IPTOS_LOWDELAY] = "low-delay", | |
904 | [IPTOS_THROUGHPUT] = "throughput", | |
905 | [IPTOS_RELIABILITY] = "reliability", | |
906 | [IPTOS_LOWCOST] = "low-cost", | |
907 | }; | |
908 | ||
909 | DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff); | |
910 | ||
ef76dff2 LP |
911 | bool ifname_valid(const char *p) { |
912 | bool numeric = true; | |
913 | ||
914 | /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources | |
915 | * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We | |
916 | * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */ | |
917 | ||
918 | if (isempty(p)) | |
919 | return false; | |
920 | ||
921 | if (strlen(p) >= IFNAMSIZ) | |
922 | return false; | |
923 | ||
49bfc877 | 924 | if (dot_or_dot_dot(p)) |
ef76dff2 LP |
925 | return false; |
926 | ||
927 | while (*p) { | |
928 | if ((unsigned char) *p >= 127U) | |
929 | return false; | |
930 | ||
931 | if ((unsigned char) *p <= 32U) | |
932 | return false; | |
933 | ||
4c701096 | 934 | if (IN_SET(*p, ':', '/')) |
ef76dff2 LP |
935 | return false; |
936 | ||
937 | numeric = numeric && (*p >= '0' && *p <= '9'); | |
938 | p++; | |
939 | } | |
940 | ||
941 | if (numeric) | |
942 | return false; | |
943 | ||
944 | return true; | |
945 | } | |
946 | ||
26808948 SS |
947 | bool address_label_valid(const char *p) { |
948 | ||
949 | if (isempty(p)) | |
950 | return false; | |
951 | ||
952 | if (strlen(p) >= IFNAMSIZ) | |
953 | return false; | |
954 | ||
955 | while (*p) { | |
956 | if ((uint8_t) *p >= 127U) | |
957 | return false; | |
958 | ||
959 | if ((uint8_t) *p <= 31U) | |
960 | return false; | |
961 | p++; | |
962 | } | |
963 | ||
964 | return true; | |
965 | } | |
966 | ||
2583fbea LP |
967 | int getpeercred(int fd, struct ucred *ucred) { |
968 | socklen_t n = sizeof(struct ucred); | |
969 | struct ucred u; | |
970 | int r; | |
971 | ||
972 | assert(fd >= 0); | |
973 | assert(ucred); | |
974 | ||
975 | r = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &u, &n); | |
976 | if (r < 0) | |
977 | return -errno; | |
978 | ||
979 | if (n != sizeof(struct ucred)) | |
980 | return -EIO; | |
981 | ||
bbcc701e LP |
982 | /* Check if the data is actually useful and not suppressed due to namespacing issues */ |
983 | if (!pid_is_valid(u.pid)) | |
2583fbea LP |
984 | return -ENODATA; |
985 | ||
bbcc701e LP |
986 | /* Note that we don't check UID/GID here, as namespace translation works differently there: instead of |
987 | * receiving in "invalid" user/group we get the overflow UID/GID. */ | |
988 | ||
2583fbea LP |
989 | *ucred = u; |
990 | return 0; | |
991 | } | |
992 | ||
993 | int getpeersec(int fd, char **ret) { | |
217d8967 | 994 | _cleanup_free_ char *s = NULL; |
2583fbea | 995 | socklen_t n = 64; |
2583fbea LP |
996 | |
997 | assert(fd >= 0); | |
998 | assert(ret); | |
999 | ||
217d8967 LP |
1000 | for (;;) { |
1001 | s = new0(char, n+1); | |
1002 | if (!s) | |
1003 | return -ENOMEM; | |
2583fbea | 1004 | |
217d8967 LP |
1005 | if (getsockopt(fd, SOL_SOCKET, SO_PEERSEC, s, &n) >= 0) |
1006 | break; | |
2583fbea LP |
1007 | |
1008 | if (errno != ERANGE) | |
1009 | return -errno; | |
1010 | ||
217d8967 | 1011 | s = mfree(s); |
2583fbea LP |
1012 | } |
1013 | ||
217d8967 | 1014 | if (isempty(s)) |
2583fbea | 1015 | return -EOPNOTSUPP; |
2583fbea | 1016 | |
ae2a15bc | 1017 | *ret = TAKE_PTR(s); |
217d8967 | 1018 | |
2583fbea LP |
1019 | return 0; |
1020 | } | |
1021 | ||
43f2c88d LP |
1022 | int getpeergroups(int fd, gid_t **ret) { |
1023 | socklen_t n = sizeof(gid_t) * 64; | |
1024 | _cleanup_free_ gid_t *d = NULL; | |
1025 | ||
75f40779 | 1026 | assert(fd >= 0); |
43f2c88d LP |
1027 | assert(ret); |
1028 | ||
1029 | for (;;) { | |
1030 | d = malloc(n); | |
1031 | if (!d) | |
1032 | return -ENOMEM; | |
1033 | ||
1034 | if (getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, d, &n) >= 0) | |
1035 | break; | |
1036 | ||
1037 | if (errno != ERANGE) | |
1038 | return -errno; | |
1039 | ||
1040 | d = mfree(d); | |
1041 | } | |
1042 | ||
1043 | assert_se(n % sizeof(gid_t) == 0); | |
1044 | n /= sizeof(gid_t); | |
1045 | ||
1046 | if ((socklen_t) (int) n != n) | |
1047 | return -E2BIG; | |
1048 | ||
1cc6c93a | 1049 | *ret = TAKE_PTR(d); |
43f2c88d LP |
1050 | |
1051 | return (int) n; | |
1052 | } | |
1053 | ||
d34673ec | 1054 | ssize_t send_one_fd_iov_sa( |
726f4c47 ZJS |
1055 | int transport_fd, |
1056 | int fd, | |
d34673ec | 1057 | struct iovec *iov, size_t iovlen, |
726f4c47 ZJS |
1058 | const struct sockaddr *sa, socklen_t len, |
1059 | int flags) { | |
1060 | ||
2583fbea LP |
1061 | union { |
1062 | struct cmsghdr cmsghdr; | |
1063 | uint8_t buf[CMSG_SPACE(sizeof(int))]; | |
1064 | } control = {}; | |
1065 | struct msghdr mh = { | |
726f4c47 ZJS |
1066 | .msg_name = (struct sockaddr*) sa, |
1067 | .msg_namelen = len, | |
d34673ec FB |
1068 | .msg_iov = iov, |
1069 | .msg_iovlen = iovlen, | |
2583fbea | 1070 | }; |
d34673ec | 1071 | ssize_t k; |
2583fbea LP |
1072 | |
1073 | assert(transport_fd >= 0); | |
2583fbea | 1074 | |
d34673ec FB |
1075 | /* |
1076 | * We need either an FD or data to send. | |
1077 | * If there's nothing, return an error. | |
1078 | */ | |
1079 | if (fd < 0 && !iov) | |
1080 | return -EINVAL; | |
2583fbea | 1081 | |
d34673ec FB |
1082 | if (fd >= 0) { |
1083 | struct cmsghdr *cmsg; | |
2583fbea | 1084 | |
d34673ec FB |
1085 | mh.msg_control = &control; |
1086 | mh.msg_controllen = sizeof(control); | |
1087 | ||
1088 | cmsg = CMSG_FIRSTHDR(&mh); | |
1089 | cmsg->cmsg_level = SOL_SOCKET; | |
1090 | cmsg->cmsg_type = SCM_RIGHTS; | |
1091 | cmsg->cmsg_len = CMSG_LEN(sizeof(int)); | |
1092 | memcpy(CMSG_DATA(cmsg), &fd, sizeof(int)); | |
1093 | ||
1094 | mh.msg_controllen = CMSG_SPACE(sizeof(int)); | |
1095 | } | |
1096 | k = sendmsg(transport_fd, &mh, MSG_NOSIGNAL | flags); | |
1097 | if (k < 0) | |
1098 | return (ssize_t) -errno; | |
1099 | ||
1100 | return k; | |
2583fbea LP |
1101 | } |
1102 | ||
d34673ec FB |
1103 | int send_one_fd_sa( |
1104 | int transport_fd, | |
1105 | int fd, | |
1106 | const struct sockaddr *sa, socklen_t len, | |
1107 | int flags) { | |
1108 | ||
1109 | assert(fd >= 0); | |
1110 | ||
1111 | return (int) send_one_fd_iov_sa(transport_fd, fd, NULL, 0, sa, len, flags); | |
1112 | } | |
1113 | ||
1114 | ssize_t receive_one_fd_iov( | |
1115 | int transport_fd, | |
1116 | struct iovec *iov, size_t iovlen, | |
1117 | int flags, | |
1118 | int *ret_fd) { | |
1119 | ||
2583fbea LP |
1120 | union { |
1121 | struct cmsghdr cmsghdr; | |
1122 | uint8_t buf[CMSG_SPACE(sizeof(int))]; | |
1123 | } control = {}; | |
1124 | struct msghdr mh = { | |
1125 | .msg_control = &control, | |
1126 | .msg_controllen = sizeof(control), | |
d34673ec FB |
1127 | .msg_iov = iov, |
1128 | .msg_iovlen = iovlen, | |
2583fbea LP |
1129 | }; |
1130 | struct cmsghdr *cmsg, *found = NULL; | |
d34673ec | 1131 | ssize_t k; |
2583fbea LP |
1132 | |
1133 | assert(transport_fd >= 0); | |
d34673ec | 1134 | assert(ret_fd); |
2583fbea LP |
1135 | |
1136 | /* | |
1137 | * Receive a single FD via @transport_fd. We don't care for | |
1138 | * the transport-type. We retrieve a single FD at most, so for | |
1139 | * packet-based transports, the caller must ensure to send | |
1140 | * only a single FD per packet. This is best used in | |
1141 | * combination with send_one_fd(). | |
1142 | */ | |
1143 | ||
d34673ec FB |
1144 | k = recvmsg(transport_fd, &mh, MSG_CMSG_CLOEXEC | flags); |
1145 | if (k < 0) | |
1146 | return (ssize_t) -errno; | |
2583fbea LP |
1147 | |
1148 | CMSG_FOREACH(cmsg, &mh) { | |
1149 | if (cmsg->cmsg_level == SOL_SOCKET && | |
1150 | cmsg->cmsg_type == SCM_RIGHTS && | |
1151 | cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { | |
1152 | assert(!found); | |
1153 | found = cmsg; | |
1154 | break; | |
1155 | } | |
1156 | } | |
1157 | ||
d34673ec | 1158 | if (!found) |
2583fbea | 1159 | cmsg_close_all(&mh); |
d34673ec FB |
1160 | |
1161 | /* If didn't receive an FD or any data, return an error. */ | |
1162 | if (k == 0 && !found) | |
2583fbea | 1163 | return -EIO; |
2583fbea | 1164 | |
d34673ec FB |
1165 | if (found) |
1166 | *ret_fd = *(int*) CMSG_DATA(found); | |
1167 | else | |
1168 | *ret_fd = -1; | |
1169 | ||
1170 | return k; | |
1171 | } | |
1172 | ||
1173 | int receive_one_fd(int transport_fd, int flags) { | |
1174 | int fd; | |
1175 | ssize_t k; | |
1176 | ||
1177 | k = receive_one_fd_iov(transport_fd, NULL, 0, flags, &fd); | |
1178 | if (k == 0) | |
1179 | return fd; | |
1180 | ||
1181 | /* k must be negative, since receive_one_fd_iov() only returns | |
1182 | * a positive value if data was received through the iov. */ | |
1183 | assert(k < 0); | |
1184 | return (int) k; | |
2583fbea | 1185 | } |
4edc2c9b LP |
1186 | |
1187 | ssize_t next_datagram_size_fd(int fd) { | |
1188 | ssize_t l; | |
1189 | int k; | |
1190 | ||
1191 | /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will | |
96d49011 | 1192 | * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD doesn't |
4edc2c9b LP |
1193 | * do. This difference is actually of major importance as we need to be sure that the size returned here |
1194 | * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of | |
1195 | * the wrong size. */ | |
1196 | ||
1197 | l = recv(fd, NULL, 0, MSG_PEEK|MSG_TRUNC); | |
1198 | if (l < 0) { | |
3742095b | 1199 | if (IN_SET(errno, EOPNOTSUPP, EFAULT)) |
4edc2c9b LP |
1200 | goto fallback; |
1201 | ||
1202 | return -errno; | |
1203 | } | |
1204 | if (l == 0) | |
1205 | goto fallback; | |
1206 | ||
1207 | return l; | |
1208 | ||
1209 | fallback: | |
1210 | k = 0; | |
1211 | ||
1212 | /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD | |
1213 | * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */ | |
1214 | ||
1215 | if (ioctl(fd, FIONREAD, &k) < 0) | |
1216 | return -errno; | |
1217 | ||
1218 | return (ssize_t) k; | |
1219 | } | |
60d9771c LP |
1220 | |
1221 | int flush_accept(int fd) { | |
1222 | ||
1223 | struct pollfd pollfd = { | |
1224 | .fd = fd, | |
1225 | .events = POLLIN, | |
1226 | }; | |
1227 | int r; | |
1228 | ||
60d9771c LP |
1229 | /* Similar to flush_fd() but flushes all incoming connection by accepting them and immediately closing them. */ |
1230 | ||
1231 | for (;;) { | |
1232 | int cfd; | |
1233 | ||
1234 | r = poll(&pollfd, 1, 0); | |
1235 | if (r < 0) { | |
1236 | if (errno == EINTR) | |
1237 | continue; | |
1238 | ||
1239 | return -errno; | |
1240 | ||
1241 | } else if (r == 0) | |
1242 | return 0; | |
1243 | ||
1244 | cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); | |
1245 | if (cfd < 0) { | |
1246 | if (errno == EINTR) | |
1247 | continue; | |
1248 | ||
1249 | if (errno == EAGAIN) | |
1250 | return 0; | |
1251 | ||
1252 | return -errno; | |
1253 | } | |
1254 | ||
1255 | close(cfd); | |
1256 | } | |
1257 | } | |
29206d46 LP |
1258 | |
1259 | struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length) { | |
1260 | struct cmsghdr *cmsg; | |
1261 | ||
1262 | assert(mh); | |
1263 | ||
1264 | CMSG_FOREACH(cmsg, mh) | |
1265 | if (cmsg->cmsg_level == level && | |
1266 | cmsg->cmsg_type == type && | |
1267 | (length == (socklen_t) -1 || length == cmsg->cmsg_len)) | |
1268 | return cmsg; | |
1269 | ||
1270 | return NULL; | |
1271 | } | |
429b4350 LP |
1272 | |
1273 | int socket_ioctl_fd(void) { | |
1274 | int fd; | |
1275 | ||
1276 | /* Create a socket to invoke the various network interface ioctl()s on. Traditionally only AF_INET was good for | |
1277 | * that. Since kernel 4.6 AF_NETLINK works for this too. We first try to use AF_INET hence, but if that's not | |
1278 | * available (for example, because it is made unavailable via SECCOMP or such), we'll fall back to the more | |
1279 | * generic AF_NETLINK. */ | |
1280 | ||
1281 | fd = socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC, 0); | |
1282 | if (fd < 0) | |
1283 | fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC, NETLINK_GENERIC); | |
1284 | if (fd < 0) | |
1285 | return -errno; | |
1286 | ||
1287 | return fd; | |
1288 | } | |
9f20fc28 LP |
1289 | |
1290 | int sockaddr_un_unlink(const struct sockaddr_un *sa) { | |
1291 | const char *p, * nul; | |
1292 | ||
1293 | assert(sa); | |
1294 | ||
1295 | if (sa->sun_family != AF_UNIX) | |
1296 | return -EPROTOTYPE; | |
1297 | ||
1298 | if (sa->sun_path[0] == 0) /* Nothing to do for abstract sockets */ | |
1299 | return 0; | |
1300 | ||
1301 | /* The path in .sun_path is not necessarily NUL terminated. Let's fix that. */ | |
1302 | nul = memchr(sa->sun_path, 0, sizeof(sa->sun_path)); | |
1303 | if (nul) | |
1304 | p = sa->sun_path; | |
1305 | else | |
1306 | p = memdupa_suffix0(sa->sun_path, sizeof(sa->sun_path)); | |
1307 | ||
1308 | if (unlink(p) < 0) | |
1309 | return -errno; | |
1310 | ||
1311 | return 1; | |
1312 | } | |
5cf91ea9 LP |
1313 | |
1314 | int sockaddr_un_set_path(struct sockaddr_un *ret, const char *path) { | |
1315 | size_t l; | |
1316 | ||
1317 | assert(ret); | |
1318 | assert(path); | |
1319 | ||
1320 | /* Initialize ret->sun_path from the specified argument. This will interpret paths starting with '@' as | |
1321 | * abstract namespace sockets, and those starting with '/' as regular filesystem sockets. It won't accept | |
1322 | * anything else (i.e. no relative paths), to avoid ambiguities. Note that this function cannot be used to | |
1323 | * reference paths in the abstract namespace that include NUL bytes in the name. */ | |
1324 | ||
1325 | l = strlen(path); | |
1326 | if (l == 0) | |
1327 | return -EINVAL; | |
1328 | if (!IN_SET(path[0], '/', '@')) | |
1329 | return -EINVAL; | |
1330 | if (path[1] == 0) | |
1331 | return -EINVAL; | |
1332 | ||
1333 | /* Don't allow paths larger than the space in sockaddr_un. Note that we are a tiny bit more restrictive than | |
1334 | * the kernel is: we insist on NUL termination (both for abstract namespace and regular file system socket | |
1335 | * addresses!), which the kernel doesn't. We do this to reduce chance of incompatibility with other apps that | |
1336 | * do not expect non-NUL terminated file system path*/ | |
1337 | if (l+1 > sizeof(ret->sun_path)) | |
1338 | return -EINVAL; | |
1339 | ||
1340 | *ret = (struct sockaddr_un) { | |
1341 | .sun_family = AF_UNIX, | |
1342 | }; | |
1343 | ||
1344 | if (path[0] == '@') { | |
1345 | /* Abstract namespace socket */ | |
1346 | memcpy(ret->sun_path + 1, path + 1, l); /* copy *with* trailing NUL byte */ | |
1347 | return (int) (offsetof(struct sockaddr_un, sun_path) + l); /* 🔥 *don't* 🔥 include trailing NUL in size */ | |
1348 | ||
1349 | } else { | |
1350 | assert(path[0] == '/'); | |
1351 | ||
1352 | /* File system socket */ | |
1353 | memcpy(ret->sun_path, path, l + 1); /* copy *with* trailing NUL byte */ | |
1354 | return (int) (offsetof(struct sockaddr_un, sun_path) + l + 1); /* include trailing NUL in size */ | |
1355 | } | |
1356 | } |