File: | build-scan/../src/basic/socket-util.c |
Warning: | line 140, column 50 Potential leak of memory pointed to by 'n' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |||
2 | ||||
3 | #include <arpa/inet.h> | |||
4 | #include <errno(*__errno_location ()).h> | |||
5 | #include <limits.h> | |||
6 | #include <net/if.h> | |||
7 | #include <netdb.h> | |||
8 | #include <netinet/ip.h> | |||
9 | #include <poll.h> | |||
10 | #include <stddef.h> | |||
11 | #include <stdint.h> | |||
12 | #include <stdio.h> | |||
13 | #include <stdlib.h> | |||
14 | #include <string.h> | |||
15 | #include <unistd.h> | |||
16 | ||||
17 | #include "alloc-util.h" | |||
18 | #include "fd-util.h" | |||
19 | #include "fileio.h" | |||
20 | #include "format-util.h" | |||
21 | #include "log.h" | |||
22 | #include "macro.h" | |||
23 | #include "missing.h" | |||
24 | #include "parse-util.h" | |||
25 | #include "path-util.h" | |||
26 | #include "process-util.h" | |||
27 | #include "socket-util.h" | |||
28 | #include "string-table.h" | |||
29 | #include "string-util.h" | |||
30 | #include "strv.h" | |||
31 | #include "user-util.h" | |||
32 | #include "utf8.h" | |||
33 | #include "util.h" | |||
34 | ||||
35 | #if ENABLE_IDN1 | |||
36 | # define IDN_FLAGS32 NI_IDN32 | |||
37 | #else | |||
38 | # define IDN_FLAGS32 0 | |||
39 | #endif | |||
40 | ||||
41 | static const char* const socket_address_type_table[] = { | |||
42 | [SOCK_STREAMSOCK_STREAM] = "Stream", | |||
43 | [SOCK_DGRAMSOCK_DGRAM] = "Datagram", | |||
44 | [SOCK_RAWSOCK_RAW] = "Raw", | |||
45 | [SOCK_RDMSOCK_RDM] = "ReliableDatagram", | |||
46 | [SOCK_SEQPACKETSOCK_SEQPACKET] = "SequentialPacket", | |||
47 | [SOCK_DCCPSOCK_DCCP] = "DatagramCongestionControl", | |||
48 | }; | |||
49 | ||||
50 | DEFINE_STRING_TABLE_LOOKUP(socket_address_type, int)const char *socket_address_type_to_string(int i) { if (i < 0 || i >= (int) __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p (typeof(socket_address_type_table), typeof(&*(socket_address_type_table ))), sizeof(socket_address_type_table)/sizeof((socket_address_type_table )[0]), ((void)0)))) return ((void*)0); return socket_address_type_table [i]; } int socket_address_type_from_string(const char *s) { return (int) string_table_lookup(socket_address_type_table, __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (socket_address_type_table), typeof(&*(socket_address_type_table ))), sizeof(socket_address_type_table)/sizeof((socket_address_type_table )[0]), ((void)0))), s); }; | |||
51 | ||||
52 | int socket_address_parse(SocketAddress *a, const char *s) { | |||
53 | _cleanup_free___attribute__((cleanup(freep))) char *n = NULL((void*)0); | |||
54 | char *e; | |||
55 | int r; | |||
56 | ||||
57 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 57, __PRETTY_FUNCTION__ ); } while (0); | |||
58 | assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("s"), "../src/basic/socket-util.c", 58, __PRETTY_FUNCTION__ ); } while (0); | |||
59 | ||||
60 | zero(*a)(({ size_t _l_ = (sizeof(*a)); void *_x_ = (&(*a)); _l_ == 0 ? _x_ : memset(_x_, 0, _l_); })); | |||
61 | a->type = SOCK_STREAMSOCK_STREAM; | |||
62 | ||||
63 | if (*s == '[') { | |||
64 | uint16_t port; | |||
65 | ||||
66 | /* IPv6 in [x:.....:z]:p notation */ | |||
67 | ||||
68 | e = strchr(s+1, ']'); | |||
69 | if (!e) | |||
70 | return -EINVAL22; | |||
71 | ||||
72 | n = strndup(s+1, e-s-1); | |||
73 | if (!n) | |||
74 | return -ENOMEM12; | |||
75 | ||||
76 | errno(*__errno_location ()) = 0; | |||
77 | if (inet_pton(AF_INET610, n, &a->sockaddr.in6.sin6_addr) <= 0) | |||
78 | return errno(*__errno_location ()) > 0 ? -errno(*__errno_location ()) : -EINVAL22; | |||
79 | ||||
80 | e++; | |||
81 | if (*e != ':') | |||
82 | return -EINVAL22; | |||
83 | ||||
84 | e++; | |||
85 | r = parse_ip_port(e, &port); | |||
86 | if (r < 0) | |||
87 | return r; | |||
88 | ||||
89 | a->sockaddr.in6.sin6_family = AF_INET610; | |||
90 | a->sockaddr.in6.sin6_port = htobe16(port)__bswap_16 (port); | |||
91 | a->size = sizeof(struct sockaddr_in6); | |||
92 | ||||
93 | } else if (*s == '/') { | |||
94 | /* AF_UNIX socket */ | |||
95 | ||||
96 | size_t l; | |||
97 | ||||
98 | l = strlen(s); | |||
99 | if (l >= sizeof(a->sockaddr.un.sun_path)) | |||
100 | return -EINVAL22; | |||
101 | ||||
102 | a->sockaddr.un.sun_family = AF_UNIX1; | |||
103 | memcpy(a->sockaddr.un.sun_path, s, l); | |||
104 | a->size = offsetof(struct sockaddr_un, sun_path)__builtin_offsetof(struct sockaddr_un, sun_path) + l + 1; | |||
105 | ||||
106 | } else if (*s == '@') { | |||
107 | /* Abstract AF_UNIX socket */ | |||
108 | size_t l; | |||
109 | ||||
110 | l = strlen(s+1); | |||
111 | if (l >= sizeof(a->sockaddr.un.sun_path) - 1) | |||
112 | return -EINVAL22; | |||
113 | ||||
114 | a->sockaddr.un.sun_family = AF_UNIX1; | |||
115 | memcpy(a->sockaddr.un.sun_path+1, s+1, l); | |||
116 | a->size = offsetof(struct sockaddr_un, sun_path)__builtin_offsetof(struct sockaddr_un, sun_path) + 1 + l; | |||
117 | ||||
118 | } else if (startswith(s, "vsock:")) { | |||
119 | /* AF_VSOCK socket in vsock:cid:port notation */ | |||
120 | const char *cid_start = s + STRLEN("vsock:")(sizeof("""vsock:""") - 1); | |||
121 | unsigned port; | |||
122 | ||||
123 | e = strchr(cid_start, ':'); | |||
124 | if (!e) | |||
125 | return -EINVAL22; | |||
126 | ||||
127 | r = safe_atou(e+1, &port); | |||
128 | if (r < 0) | |||
129 | return r; | |||
130 | ||||
131 | n = strndup(cid_start, e - cid_start); | |||
132 | if (!n) | |||
133 | return -ENOMEM12; | |||
134 | ||||
135 | if (!isempty(n)) { | |||
136 | r = safe_atou(n, &a->sockaddr.vm.svm_cid); | |||
137 | if (r < 0) | |||
138 | return r; | |||
139 | } else | |||
140 | a->sockaddr.vm.svm_cid = VMADDR_CID_ANY-1U; | |||
| ||||
141 | ||||
142 | a->sockaddr.vm.svm_family = AF_VSOCK40; | |||
143 | a->sockaddr.vm.svm_port = port; | |||
144 | a->size = sizeof(struct sockaddr_vm); | |||
145 | ||||
146 | } else { | |||
147 | uint16_t port; | |||
148 | ||||
149 | e = strchr(s, ':'); | |||
150 | if (e) { | |||
151 | r = parse_ip_port(e + 1, &port); | |||
152 | if (r < 0) | |||
153 | return r; | |||
154 | ||||
155 | n = strndup(s, e-s); | |||
156 | if (!n) | |||
157 | return -ENOMEM12; | |||
158 | ||||
159 | /* IPv4 in w.x.y.z:p notation? */ | |||
160 | r = inet_pton(AF_INET2, n, &a->sockaddr.in.sin_addr); | |||
161 | if (r < 0) | |||
162 | return -errno(*__errno_location ()); | |||
163 | ||||
164 | if (r > 0) { | |||
165 | /* Gotcha, it's a traditional IPv4 address */ | |||
166 | a->sockaddr.in.sin_family = AF_INET2; | |||
167 | a->sockaddr.in.sin_port = htobe16(port)__bswap_16 (port); | |||
168 | a->size = sizeof(struct sockaddr_in); | |||
169 | } else { | |||
170 | unsigned idx; | |||
171 | ||||
172 | if (strlen(n) > IF_NAMESIZE16-1) | |||
173 | return -EINVAL22; | |||
174 | ||||
175 | /* Uh, our last resort, an interface name */ | |||
176 | idx = if_nametoindex(n); | |||
177 | if (idx == 0) | |||
178 | return -EINVAL22; | |||
179 | ||||
180 | a->sockaddr.in6.sin6_family = AF_INET610; | |||
181 | a->sockaddr.in6.sin6_port = htobe16(port)__bswap_16 (port); | |||
182 | a->sockaddr.in6.sin6_scope_id = idx; | |||
183 | a->sockaddr.in6.sin6_addr = in6addr_any; | |||
184 | a->size = sizeof(struct sockaddr_in6); | |||
185 | } | |||
186 | } else { | |||
187 | ||||
188 | /* Just a port */ | |||
189 | r = parse_ip_port(s, &port); | |||
190 | if (r < 0) | |||
191 | return r; | |||
192 | ||||
193 | if (socket_ipv6_is_supported()) { | |||
194 | a->sockaddr.in6.sin6_family = AF_INET610; | |||
195 | a->sockaddr.in6.sin6_port = htobe16(port)__bswap_16 (port); | |||
196 | a->sockaddr.in6.sin6_addr = in6addr_any; | |||
197 | a->size = sizeof(struct sockaddr_in6); | |||
198 | } else { | |||
199 | a->sockaddr.in.sin_family = AF_INET2; | |||
200 | a->sockaddr.in.sin_port = htobe16(port)__bswap_16 (port); | |||
201 | a->sockaddr.in.sin_addr.s_addr = INADDR_ANY((in_addr_t) 0x00000000); | |||
202 | a->size = sizeof(struct sockaddr_in); | |||
203 | } | |||
204 | } | |||
205 | } | |||
206 | ||||
207 | return 0; | |||
208 | } | |||
209 | ||||
210 | int socket_address_parse_and_warn(SocketAddress *a, const char *s) { | |||
211 | SocketAddress b; | |||
212 | int r; | |||
213 | ||||
214 | /* Similar to socket_address_parse() but warns for IPv6 sockets when we don't support them. */ | |||
215 | ||||
216 | r = socket_address_parse(&b, s); | |||
| ||||
217 | if (r < 0) | |||
218 | return r; | |||
219 | ||||
220 | if (!socket_ipv6_is_supported() && b.sockaddr.sa.sa_family == AF_INET610) { | |||
221 | log_warning("Binding to IPv6 address not available since kernel does not support IPv6.")({ int _level = (((4))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/basic/socket-util.c", 221, __func__, "Binding to IPv6 address not available since kernel does not support IPv6." ) : -abs(_e); }); | |||
222 | return -EAFNOSUPPORT97; | |||
223 | } | |||
224 | ||||
225 | *a = b; | |||
226 | return 0; | |||
227 | } | |||
228 | ||||
229 | int socket_address_parse_netlink(SocketAddress *a, const char *s) { | |||
230 | int family; | |||
231 | unsigned group = 0; | |||
232 | _cleanup_free___attribute__((cleanup(freep))) char *sfamily = NULL((void*)0); | |||
233 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 233, __PRETTY_FUNCTION__); } while (0); | |||
234 | assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("s"), "../src/basic/socket-util.c", 234, __PRETTY_FUNCTION__); } while (0); | |||
235 | ||||
236 | zero(*a)(({ size_t _l_ = (sizeof(*a)); void *_x_ = (&(*a)); _l_ == 0 ? _x_ : memset(_x_, 0, _l_); })); | |||
237 | a->type = SOCK_RAWSOCK_RAW; | |||
238 | ||||
239 | errno(*__errno_location ()) = 0; | |||
240 | if (sscanf(s, "%ms %u", &sfamily, &group) < 1) | |||
241 | return errno(*__errno_location ()) > 0 ? -errno(*__errno_location ()) : -EINVAL22; | |||
242 | ||||
243 | family = netlink_family_from_string(sfamily); | |||
244 | if (family < 0) | |||
245 | return -EINVAL22; | |||
246 | ||||
247 | a->sockaddr.nl.nl_family = AF_NETLINK16; | |||
248 | a->sockaddr.nl.nl_groups = group; | |||
249 | ||||
250 | a->type = SOCK_RAWSOCK_RAW; | |||
251 | a->size = sizeof(struct sockaddr_nl); | |||
252 | a->protocol = family; | |||
253 | ||||
254 | return 0; | |||
255 | } | |||
256 | ||||
257 | int socket_address_verify(const SocketAddress *a) { | |||
258 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 258, __PRETTY_FUNCTION__); } while (0); | |||
259 | ||||
260 | switch (socket_address_family(a)((a)->sockaddr.sa.sa_family)) { | |||
261 | ||||
262 | case AF_INET2: | |||
263 | if (a->size != sizeof(struct sockaddr_in)) | |||
264 | return -EINVAL22; | |||
265 | ||||
266 | if (a->sockaddr.in.sin_port == 0) | |||
267 | return -EINVAL22; | |||
268 | ||||
269 | if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){SOCK_STREAM, SOCK_DGRAM})/sizeof(int)]; switch (a->type) { case SOCK_STREAM: case SOCK_DGRAM: _found = 1; break; default: break; } _found; })) | |||
270 | return -EINVAL22; | |||
271 | ||||
272 | return 0; | |||
273 | ||||
274 | case AF_INET610: | |||
275 | if (a->size != sizeof(struct sockaddr_in6)) | |||
276 | return -EINVAL22; | |||
277 | ||||
278 | if (a->sockaddr.in6.sin6_port == 0) | |||
279 | return -EINVAL22; | |||
280 | ||||
281 | if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){SOCK_STREAM, SOCK_DGRAM})/sizeof(int)]; switch (a->type) { case SOCK_STREAM: case SOCK_DGRAM: _found = 1; break; default: break; } _found; })) | |||
282 | return -EINVAL22; | |||
283 | ||||
284 | return 0; | |||
285 | ||||
286 | case AF_UNIX1: | |||
287 | if (a->size < offsetof(struct sockaddr_un, sun_path)__builtin_offsetof(struct sockaddr_un, sun_path)) | |||
288 | return -EINVAL22; | |||
289 | ||||
290 | if (a->size > offsetof(struct sockaddr_un, sun_path)__builtin_offsetof(struct sockaddr_un, sun_path)) { | |||
291 | ||||
292 | if (a->sockaddr.un.sun_path[0] != 0) { | |||
293 | char *e; | |||
294 | ||||
295 | /* path */ | |||
296 | e = memchr(a->sockaddr.un.sun_path, 0, sizeof(a->sockaddr.un.sun_path)); | |||
297 | if (!e) | |||
298 | return -EINVAL22; | |||
299 | ||||
300 | if (a->size != offsetof(struct sockaddr_un, sun_path)__builtin_offsetof(struct sockaddr_un, sun_path) + (e - a->sockaddr.un.sun_path) + 1) | |||
301 | return -EINVAL22; | |||
302 | } | |||
303 | } | |||
304 | ||||
305 | if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET} )/sizeof(int)]; switch(a->type) { case SOCK_STREAM: case SOCK_DGRAM : case SOCK_SEQPACKET: _found = 1; break; default: break; } _found ; })) | |||
306 | return -EINVAL22; | |||
307 | ||||
308 | return 0; | |||
309 | ||||
310 | case AF_NETLINK16: | |||
311 | ||||
312 | if (a->size != sizeof(struct sockaddr_nl)) | |||
313 | return -EINVAL22; | |||
314 | ||||
315 | if (!IN_SET(a->type, SOCK_RAW, SOCK_DGRAM)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){SOCK_RAW, SOCK_DGRAM})/sizeof(int)]; switch (a->type) { case SOCK_RAW: case SOCK_DGRAM: _found = 1; break ; default: break; } _found; })) | |||
316 | return -EINVAL22; | |||
317 | ||||
318 | return 0; | |||
319 | ||||
320 | case AF_VSOCK40: | |||
321 | if (a->size != sizeof(struct sockaddr_vm)) | |||
322 | return -EINVAL22; | |||
323 | ||||
324 | if (!IN_SET(a->type, SOCK_STREAM, SOCK_DGRAM)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){SOCK_STREAM, SOCK_DGRAM})/sizeof(int)]; switch (a->type) { case SOCK_STREAM: case SOCK_DGRAM: _found = 1; break; default: break; } _found; })) | |||
325 | return -EINVAL22; | |||
326 | ||||
327 | return 0; | |||
328 | ||||
329 | default: | |||
330 | return -EAFNOSUPPORT97; | |||
331 | } | |||
332 | } | |||
333 | ||||
334 | int socket_address_print(const SocketAddress *a, char **ret) { | |||
335 | int r; | |||
336 | ||||
337 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 337, __PRETTY_FUNCTION__); } while (0); | |||
338 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/socket-util.c", 338 , __PRETTY_FUNCTION__); } while (0); | |||
339 | ||||
340 | r = socket_address_verify(a); | |||
341 | if (r < 0) | |||
342 | return r; | |||
343 | ||||
344 | if (socket_address_family(a)((a)->sockaddr.sa.sa_family) == AF_NETLINK16) { | |||
345 | _cleanup_free___attribute__((cleanup(freep))) char *sfamily = NULL((void*)0); | |||
346 | ||||
347 | r = netlink_family_to_string_alloc(a->protocol, &sfamily); | |||
348 | if (r < 0) | |||
349 | return r; | |||
350 | ||||
351 | r = asprintf(ret, "%s %u", sfamily, a->sockaddr.nl.nl_groups); | |||
352 | if (r < 0) | |||
353 | return -ENOMEM12; | |||
354 | ||||
355 | return 0; | |||
356 | } | |||
357 | ||||
358 | return sockaddr_pretty(&a->sockaddr.sa, a->size, false0, true1, ret); | |||
359 | } | |||
360 | ||||
361 | bool_Bool socket_address_can_accept(const SocketAddress *a) { | |||
362 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 362, __PRETTY_FUNCTION__); } while (0); | |||
363 | ||||
364 | return | |||
365 | IN_SET(a->type, SOCK_STREAM, SOCK_SEQPACKET)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){SOCK_STREAM, SOCK_SEQPACKET})/sizeof(int )]; switch(a->type) { case SOCK_STREAM: case SOCK_SEQPACKET : _found = 1; break; default: break; } _found; }); | |||
366 | } | |||
367 | ||||
368 | bool_Bool socket_address_equal(const SocketAddress *a, const SocketAddress *b) { | |||
369 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 369, __PRETTY_FUNCTION__); } while (0); | |||
370 | assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("b"), "../src/basic/socket-util.c", 370, __PRETTY_FUNCTION__); } while (0); | |||
371 | ||||
372 | /* Invalid addresses are unequal to all */ | |||
373 | if (socket_address_verify(a) < 0 || | |||
374 | socket_address_verify(b) < 0) | |||
375 | return false0; | |||
376 | ||||
377 | if (a->type != b->type) | |||
378 | return false0; | |||
379 | ||||
380 | if (socket_address_family(a)((a)->sockaddr.sa.sa_family) != socket_address_family(b)((b)->sockaddr.sa.sa_family)) | |||
381 | return false0; | |||
382 | ||||
383 | switch (socket_address_family(a)((a)->sockaddr.sa.sa_family)) { | |||
384 | ||||
385 | case AF_INET2: | |||
386 | if (a->sockaddr.in.sin_addr.s_addr != b->sockaddr.in.sin_addr.s_addr) | |||
387 | return false0; | |||
388 | ||||
389 | if (a->sockaddr.in.sin_port != b->sockaddr.in.sin_port) | |||
390 | return false0; | |||
391 | ||||
392 | break; | |||
393 | ||||
394 | case AF_INET610: | |||
395 | if (memcmp(&a->sockaddr.in6.sin6_addr, &b->sockaddr.in6.sin6_addr, sizeof(a->sockaddr.in6.sin6_addr)) != 0) | |||
396 | return false0; | |||
397 | ||||
398 | if (a->sockaddr.in6.sin6_port != b->sockaddr.in6.sin6_port) | |||
399 | return false0; | |||
400 | ||||
401 | break; | |||
402 | ||||
403 | case AF_UNIX1: | |||
404 | if (a->size <= offsetof(struct sockaddr_un, sun_path)__builtin_offsetof(struct sockaddr_un, sun_path) || | |||
405 | b->size <= offsetof(struct sockaddr_un, sun_path)__builtin_offsetof(struct sockaddr_un, sun_path)) | |||
406 | return false0; | |||
407 | ||||
408 | if ((a->sockaddr.un.sun_path[0] == 0) != (b->sockaddr.un.sun_path[0] == 0)) | |||
409 | return false0; | |||
410 | ||||
411 | if (a->sockaddr.un.sun_path[0]) { | |||
412 | if (!path_equal_or_files_same(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, 0)) | |||
413 | return false0; | |||
414 | } else { | |||
415 | if (a->size != b->size) | |||
416 | return false0; | |||
417 | ||||
418 | if (memcmp(a->sockaddr.un.sun_path, b->sockaddr.un.sun_path, a->size) != 0) | |||
419 | return false0; | |||
420 | } | |||
421 | ||||
422 | break; | |||
423 | ||||
424 | case AF_NETLINK16: | |||
425 | if (a->protocol != b->protocol) | |||
426 | return false0; | |||
427 | ||||
428 | if (a->sockaddr.nl.nl_groups != b->sockaddr.nl.nl_groups) | |||
429 | return false0; | |||
430 | ||||
431 | break; | |||
432 | ||||
433 | case AF_VSOCK40: | |||
434 | if (a->sockaddr.vm.svm_cid != b->sockaddr.vm.svm_cid) | |||
435 | return false0; | |||
436 | ||||
437 | if (a->sockaddr.vm.svm_port != b->sockaddr.vm.svm_port) | |||
438 | return false0; | |||
439 | ||||
440 | break; | |||
441 | ||||
442 | default: | |||
443 | /* Cannot compare, so we assume the addresses are different */ | |||
444 | return false0; | |||
445 | } | |||
446 | ||||
447 | return true1; | |||
448 | } | |||
449 | ||||
450 | bool_Bool socket_address_is(const SocketAddress *a, const char *s, int type) { | |||
451 | struct SocketAddress b; | |||
452 | ||||
453 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 453, __PRETTY_FUNCTION__); } while (0); | |||
454 | assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("s"), "../src/basic/socket-util.c", 454, __PRETTY_FUNCTION__); } while (0); | |||
455 | ||||
456 | if (socket_address_parse(&b, s) < 0) | |||
457 | return false0; | |||
458 | ||||
459 | b.type = type; | |||
460 | ||||
461 | return socket_address_equal(a, &b); | |||
462 | } | |||
463 | ||||
464 | bool_Bool socket_address_is_netlink(const SocketAddress *a, const char *s) { | |||
465 | struct SocketAddress b; | |||
466 | ||||
467 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 467, __PRETTY_FUNCTION__); } while (0); | |||
468 | assert(s)do { if ((__builtin_expect(!!(!(s)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("s"), "../src/basic/socket-util.c", 468, __PRETTY_FUNCTION__); } while (0); | |||
469 | ||||
470 | if (socket_address_parse_netlink(&b, s) < 0) | |||
471 | return false0; | |||
472 | ||||
473 | return socket_address_equal(a, &b); | |||
474 | } | |||
475 | ||||
476 | const char* socket_address_get_path(const SocketAddress *a) { | |||
477 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 477, __PRETTY_FUNCTION__); } while (0); | |||
478 | ||||
479 | if (socket_address_family(a)((a)->sockaddr.sa.sa_family) != AF_UNIX1) | |||
480 | return NULL((void*)0); | |||
481 | ||||
482 | if (a->sockaddr.un.sun_path[0] == 0) | |||
483 | return NULL((void*)0); | |||
484 | ||||
485 | return a->sockaddr.un.sun_path; | |||
486 | } | |||
487 | ||||
488 | bool_Bool socket_ipv6_is_supported(void) { | |||
489 | if (access("/proc/net/if_inet6", F_OK0) != 0) | |||
490 | return false0; | |||
491 | ||||
492 | return true1; | |||
493 | } | |||
494 | ||||
495 | bool_Bool socket_address_matches_fd(const SocketAddress *a, int fd) { | |||
496 | SocketAddress b; | |||
497 | socklen_t solen; | |||
498 | ||||
499 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 499, __PRETTY_FUNCTION__); } while (0); | |||
500 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/basic/socket-util.c" , 500, __PRETTY_FUNCTION__); } while (0); | |||
501 | ||||
502 | b.size = sizeof(b.sockaddr); | |||
503 | if (getsockname(fd, &b.sockaddr.sa, &b.size) < 0) | |||
504 | return false0; | |||
505 | ||||
506 | if (b.sockaddr.sa.sa_family != a->sockaddr.sa.sa_family) | |||
507 | return false0; | |||
508 | ||||
509 | solen = sizeof(b.type); | |||
510 | if (getsockopt(fd, SOL_SOCKET1, SO_TYPE3, &b.type, &solen) < 0) | |||
511 | return false0; | |||
512 | ||||
513 | if (b.type != a->type) | |||
514 | return false0; | |||
515 | ||||
516 | if (a->protocol != 0) { | |||
517 | solen = sizeof(b.protocol); | |||
518 | if (getsockopt(fd, SOL_SOCKET1, SO_PROTOCOL38, &b.protocol, &solen) < 0) | |||
519 | return false0; | |||
520 | ||||
521 | if (b.protocol != a->protocol) | |||
522 | return false0; | |||
523 | } | |||
524 | ||||
525 | return socket_address_equal(a, &b); | |||
526 | } | |||
527 | ||||
528 | int sockaddr_port(const struct sockaddr *_sa, unsigned *ret_port) { | |||
529 | union sockaddr_union *sa = (union sockaddr_union*) _sa; | |||
530 | ||||
531 | /* Note, this returns the port as 'unsigned' rather than 'uint16_t', as AF_VSOCK knows larger ports */ | |||
532 | ||||
533 | assert(sa)do { if ((__builtin_expect(!!(!(sa)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("sa"), "../src/basic/socket-util.c", 533 , __PRETTY_FUNCTION__); } while (0); | |||
534 | ||||
535 | switch (sa->sa.sa_family) { | |||
536 | ||||
537 | case AF_INET2: | |||
538 | *ret_port = be16toh(sa->in.sin_port)__bswap_16 (sa->in.sin_port); | |||
539 | return 0; | |||
540 | ||||
541 | case AF_INET610: | |||
542 | *ret_port = be16toh(sa->in6.sin6_port)__bswap_16 (sa->in6.sin6_port); | |||
543 | return 0; | |||
544 | ||||
545 | case AF_VSOCK40: | |||
546 | *ret_port = sa->vm.svm_port; | |||
547 | return 0; | |||
548 | ||||
549 | default: | |||
550 | return -EAFNOSUPPORT97; | |||
551 | } | |||
552 | } | |||
553 | ||||
554 | int sockaddr_pretty(const struct sockaddr *_sa, socklen_t salen, bool_Bool translate_ipv6, bool_Bool include_port, char **ret) { | |||
555 | union sockaddr_union *sa = (union sockaddr_union*) _sa; | |||
556 | char *p; | |||
557 | int r; | |||
558 | ||||
559 | assert(sa)do { if ((__builtin_expect(!!(!(sa)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("sa"), "../src/basic/socket-util.c", 559 , __PRETTY_FUNCTION__); } while (0); | |||
560 | assert(salen >= sizeof(sa->sa.sa_family))do { if ((__builtin_expect(!!(!(salen >= sizeof(sa->sa. sa_family))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ( "salen >= sizeof(sa->sa.sa_family)"), "../src/basic/socket-util.c" , 560, __PRETTY_FUNCTION__); } while (0); | |||
561 | ||||
562 | switch (sa->sa.sa_family) { | |||
563 | ||||
564 | case AF_INET2: { | |||
565 | uint32_t a; | |||
566 | ||||
567 | a = be32toh(sa->in.sin_addr.s_addr)__bswap_32 (sa->in.sin_addr.s_addr); | |||
568 | ||||
569 | if (include_port) | |||
570 | r = asprintf(&p, | |||
571 | "%u.%u.%u.%u:%u", | |||
572 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF, | |||
573 | be16toh(sa->in.sin_port)__bswap_16 (sa->in.sin_port)); | |||
574 | else | |||
575 | r = asprintf(&p, | |||
576 | "%u.%u.%u.%u", | |||
577 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF); | |||
578 | if (r < 0) | |||
579 | return -ENOMEM12; | |||
580 | break; | |||
581 | } | |||
582 | ||||
583 | case AF_INET610: { | |||
584 | static const unsigned char ipv4_prefix[] = { | |||
585 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF | |||
586 | }; | |||
587 | ||||
588 | if (translate_ipv6 && | |||
589 | memcmp(&sa->in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) { | |||
590 | const uint8_t *a = sa->in6.sin6_addr.s6_addr__in6_u.__u6_addr8+12; | |||
591 | if (include_port) | |||
592 | r = asprintf(&p, | |||
593 | "%u.%u.%u.%u:%u", | |||
594 | a[0], a[1], a[2], a[3], | |||
595 | be16toh(sa->in6.sin6_port)__bswap_16 (sa->in6.sin6_port)); | |||
596 | else | |||
597 | r = asprintf(&p, | |||
598 | "%u.%u.%u.%u", | |||
599 | a[0], a[1], a[2], a[3]); | |||
600 | if (r < 0) | |||
601 | return -ENOMEM12; | |||
602 | } else { | |||
603 | char a[INET6_ADDRSTRLEN46]; | |||
604 | ||||
605 | inet_ntop(AF_INET610, &sa->in6.sin6_addr, a, sizeof(a)); | |||
606 | ||||
607 | if (include_port) { | |||
608 | r = asprintf(&p, | |||
609 | "[%s]:%u", | |||
610 | a, | |||
611 | be16toh(sa->in6.sin6_port)__bswap_16 (sa->in6.sin6_port)); | |||
612 | if (r < 0) | |||
613 | return -ENOMEM12; | |||
614 | } else { | |||
615 | p = strdup(a); | |||
616 | if (!p) | |||
617 | return -ENOMEM12; | |||
618 | } | |||
619 | } | |||
620 | ||||
621 | break; | |||
622 | } | |||
623 | ||||
624 | case AF_UNIX1: | |||
625 | if (salen <= offsetof(struct sockaddr_un, sun_path)__builtin_offsetof(struct sockaddr_un, sun_path)) { | |||
626 | p = strdup("<unnamed>"); | |||
627 | if (!p) | |||
628 | return -ENOMEM12; | |||
629 | ||||
630 | } else if (sa->un.sun_path[0] == 0) { | |||
631 | /* abstract */ | |||
632 | ||||
633 | /* FIXME: We assume we can print the | |||
634 | * socket path here and that it hasn't | |||
635 | * more than one NUL byte. That is | |||
636 | * actually an invalid assumption */ | |||
637 | ||||
638 | p = new(char, sizeof(sa->un.sun_path)+1)((char*) malloc_multiply(sizeof(char), (sizeof(sa->un.sun_path )+1))); | |||
639 | if (!p) | |||
640 | return -ENOMEM12; | |||
641 | ||||
642 | p[0] = '@'; | |||
643 | memcpy(p+1, sa->un.sun_path+1, sizeof(sa->un.sun_path)-1); | |||
644 | p[sizeof(sa->un.sun_path)] = 0; | |||
645 | ||||
646 | } else { | |||
647 | p = strndup(sa->un.sun_path, sizeof(sa->un.sun_path)); | |||
648 | if (!p) | |||
649 | return -ENOMEM12; | |||
650 | } | |||
651 | ||||
652 | break; | |||
653 | ||||
654 | case AF_VSOCK40: | |||
655 | if (include_port) | |||
656 | r = asprintf(&p, | |||
657 | "vsock:%u:%u", | |||
658 | sa->vm.svm_cid, | |||
659 | sa->vm.svm_port); | |||
660 | else | |||
661 | r = asprintf(&p, "vsock:%u", sa->vm.svm_cid); | |||
662 | if (r < 0) | |||
663 | return -ENOMEM12; | |||
664 | break; | |||
665 | ||||
666 | default: | |||
667 | return -EOPNOTSUPP95; | |||
668 | } | |||
669 | ||||
670 | *ret = p; | |||
671 | return 0; | |||
672 | } | |||
673 | ||||
674 | int getpeername_pretty(int fd, bool_Bool include_port, char **ret) { | |||
675 | union sockaddr_union sa; | |||
676 | socklen_t salen = sizeof(sa); | |||
677 | int r; | |||
678 | ||||
679 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/basic/socket-util.c" , 679, __PRETTY_FUNCTION__); } while (0); | |||
680 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/socket-util.c", 680 , __PRETTY_FUNCTION__); } while (0); | |||
681 | ||||
682 | if (getpeername(fd, &sa.sa, &salen) < 0) | |||
683 | return -errno(*__errno_location ()); | |||
684 | ||||
685 | if (sa.sa.sa_family == AF_UNIX1) { | |||
686 | struct ucred ucred = {}; | |||
687 | ||||
688 | /* UNIX connection sockets are anonymous, so let's use | |||
689 | * PID/UID as pretty credentials instead */ | |||
690 | ||||
691 | r = getpeercred(fd, &ucred); | |||
692 | if (r < 0) | |||
693 | return r; | |||
694 | ||||
695 | if (asprintf(ret, "PID "PID_FMT"%" "i""/UID "UID_FMT"%" "u", ucred.pid, ucred.uid) < 0) | |||
696 | return -ENOMEM12; | |||
697 | ||||
698 | return 0; | |||
699 | } | |||
700 | ||||
701 | /* For remote sockets we translate IPv6 addresses back to IPv4 | |||
702 | * if applicable, since that's nicer. */ | |||
703 | ||||
704 | return sockaddr_pretty(&sa.sa, salen, true1, include_port, ret); | |||
705 | } | |||
706 | ||||
707 | int getsockname_pretty(int fd, char **ret) { | |||
708 | union sockaddr_union sa; | |||
709 | socklen_t salen = sizeof(sa); | |||
710 | ||||
711 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/basic/socket-util.c" , 711, __PRETTY_FUNCTION__); } while (0); | |||
712 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/socket-util.c", 712 , __PRETTY_FUNCTION__); } while (0); | |||
713 | ||||
714 | if (getsockname(fd, &sa.sa, &salen) < 0) | |||
715 | return -errno(*__errno_location ()); | |||
716 | ||||
717 | /* For local sockets we do not translate IPv6 addresses back | |||
718 | * to IPv6 if applicable, since this is usually used for | |||
719 | * listening sockets where the difference between IPv4 and | |||
720 | * IPv6 matters. */ | |||
721 | ||||
722 | return sockaddr_pretty(&sa.sa, salen, false0, true1, ret); | |||
723 | } | |||
724 | ||||
725 | int socknameinfo_pretty(union sockaddr_union *sa, socklen_t salen, char **_ret) { | |||
726 | int r; | |||
727 | char host[NI_MAXHOST1025], *ret; | |||
728 | ||||
729 | assert(_ret)do { if ((__builtin_expect(!!(!(_ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("_ret"), "../src/basic/socket-util.c", 729 , __PRETTY_FUNCTION__); } while (0); | |||
730 | ||||
731 | r = getnameinfo(&sa->sa, salen, host, sizeof(host), NULL((void*)0), 0, IDN_FLAGS32); | |||
732 | if (r != 0) { | |||
733 | int saved_errno = errno(*__errno_location ()); | |||
734 | ||||
735 | r = sockaddr_pretty(&sa->sa, salen, true1, true1, &ret); | |||
736 | if (r < 0) | |||
737 | return r; | |||
738 | ||||
739 | log_debug_errno(saved_errno, "getnameinfo(%s) failed: %m", ret)({ int _level = ((7)), _e = ((saved_errno)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/basic/socket-util.c", 739, __func__, "getnameinfo(%s) failed: %m" , ret) : -abs(_e); }); | |||
740 | } else { | |||
741 | ret = strdup(host); | |||
742 | if (!ret) | |||
743 | return -ENOMEM12; | |||
744 | } | |||
745 | ||||
746 | *_ret = ret; | |||
747 | return 0; | |||
748 | } | |||
749 | ||||
750 | int socket_address_unlink(SocketAddress *a) { | |||
751 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 751, __PRETTY_FUNCTION__); } while (0); | |||
752 | ||||
753 | if (socket_address_family(a)((a)->sockaddr.sa.sa_family) != AF_UNIX1) | |||
754 | return 0; | |||
755 | ||||
756 | if (a->sockaddr.un.sun_path[0] == 0) | |||
757 | return 0; | |||
758 | ||||
759 | if (unlink(a->sockaddr.un.sun_path) < 0) | |||
760 | return -errno(*__errno_location ()); | |||
761 | ||||
762 | return 1; | |||
763 | } | |||
764 | ||||
765 | static const char* const netlink_family_table[] = { | |||
766 | [NETLINK_ROUTE0] = "route", | |||
767 | [NETLINK_FIREWALL3] = "firewall", | |||
768 | [NETLINK_INET_DIAG4] = "inet-diag", | |||
769 | [NETLINK_NFLOG5] = "nflog", | |||
770 | [NETLINK_XFRM6] = "xfrm", | |||
771 | [NETLINK_SELINUX7] = "selinux", | |||
772 | [NETLINK_ISCSI8] = "iscsi", | |||
773 | [NETLINK_AUDIT9] = "audit", | |||
774 | [NETLINK_FIB_LOOKUP10] = "fib-lookup", | |||
775 | [NETLINK_CONNECTOR11] = "connector", | |||
776 | [NETLINK_NETFILTER12] = "netfilter", | |||
777 | [NETLINK_IP6_FW13] = "ip6-fw", | |||
778 | [NETLINK_DNRTMSG14] = "dnrtmsg", | |||
779 | [NETLINK_KOBJECT_UEVENT15] = "kobject-uevent", | |||
780 | [NETLINK_GENERIC16] = "generic", | |||
781 | [NETLINK_SCSITRANSPORT18] = "scsitransport", | |||
782 | [NETLINK_ECRYPTFS19] = "ecryptfs", | |||
783 | [NETLINK_RDMA20] = "rdma", | |||
784 | }; | |||
785 | ||||
786 | DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(netlink_family, int, INT_MAX)int netlink_family_to_string_alloc(int i, char **str) { char * s; if (i < 0 || i > 2147483647) return -34; if (i < ( int) __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p (typeof(netlink_family_table), typeof(&*(netlink_family_table ))), sizeof(netlink_family_table)/sizeof((netlink_family_table )[0]), ((void)0)))) { s = strdup(netlink_family_table[i]); if (!s) return -12; } else { if (asprintf(&s, "%i", i) < 0) return -12; } *str = s; return 0; } int netlink_family_from_string (const char *s) { int i; unsigned u = 0; if (!s) return (int) -1; for (i = 0; i < (int) __extension__ (__builtin_choose_expr ( !__builtin_types_compatible_p(typeof(netlink_family_table), typeof(&*(netlink_family_table))), sizeof(netlink_family_table )/sizeof((netlink_family_table)[0]), ((void)0))); i++) if (streq_ptr (netlink_family_table[i], s)) return i; if (safe_atou(s, & u) >= 0 && u <= 2147483647) return (int) u; return (int) -1; }; | |||
787 | ||||
788 | static const char* const socket_address_bind_ipv6_only_table[_SOCKET_ADDRESS_BIND_IPV6_ONLY_MAX] = { | |||
789 | [SOCKET_ADDRESS_DEFAULT] = "default", | |||
790 | [SOCKET_ADDRESS_BOTH] = "both", | |||
791 | [SOCKET_ADDRESS_IPV6_ONLY] = "ipv6-only" | |||
792 | }; | |||
793 | ||||
794 | DEFINE_STRING_TABLE_LOOKUP(socket_address_bind_ipv6_only, SocketAddressBindIPv6Only)const char *socket_address_bind_ipv6_only_to_string(SocketAddressBindIPv6Only i) { if (i < 0 || i >= (SocketAddressBindIPv6Only) __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (socket_address_bind_ipv6_only_table), typeof(&*(socket_address_bind_ipv6_only_table ))), sizeof(socket_address_bind_ipv6_only_table)/sizeof((socket_address_bind_ipv6_only_table )[0]), ((void)0)))) return ((void*)0); return socket_address_bind_ipv6_only_table [i]; } SocketAddressBindIPv6Only socket_address_bind_ipv6_only_from_string (const char *s) { return (SocketAddressBindIPv6Only) string_table_lookup (socket_address_bind_ipv6_only_table, __extension__ (__builtin_choose_expr ( !__builtin_types_compatible_p(typeof(socket_address_bind_ipv6_only_table ), typeof(&*(socket_address_bind_ipv6_only_table))), sizeof (socket_address_bind_ipv6_only_table)/sizeof((socket_address_bind_ipv6_only_table )[0]), ((void)0))), s); }; | |||
795 | ||||
796 | SocketAddressBindIPv6Only socket_address_bind_ipv6_only_or_bool_from_string(const char *n) { | |||
797 | int r; | |||
798 | ||||
799 | r = parse_boolean(n); | |||
800 | if (r > 0) | |||
801 | return SOCKET_ADDRESS_IPV6_ONLY; | |||
802 | if (r == 0) | |||
803 | return SOCKET_ADDRESS_BOTH; | |||
804 | ||||
805 | return socket_address_bind_ipv6_only_from_string(n); | |||
806 | } | |||
807 | ||||
808 | bool_Bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b) { | |||
809 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/basic/socket-util.c", 809, __PRETTY_FUNCTION__); } while (0); | |||
810 | assert(b)do { if ((__builtin_expect(!!(!(b)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("b"), "../src/basic/socket-util.c", 810, __PRETTY_FUNCTION__); } while (0); | |||
811 | ||||
812 | if (a->sa.sa_family != b->sa.sa_family) | |||
813 | return false0; | |||
814 | ||||
815 | if (a->sa.sa_family == AF_INET2) | |||
816 | return a->in.sin_addr.s_addr == b->in.sin_addr.s_addr; | |||
817 | ||||
818 | if (a->sa.sa_family == AF_INET610) | |||
819 | return memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a->in6.sin6_addr)) == 0; | |||
820 | ||||
821 | if (a->sa.sa_family == AF_VSOCK40) | |||
822 | return a->vm.svm_cid == b->vm.svm_cid; | |||
823 | ||||
824 | return false0; | |||
825 | } | |||
826 | ||||
827 | int fd_inc_sndbuf(int fd, size_t n) { | |||
828 | int r, value; | |||
829 | socklen_t l = sizeof(value); | |||
830 | ||||
831 | r = getsockopt(fd, SOL_SOCKET1, SO_SNDBUF7, &value, &l); | |||
832 | if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) | |||
833 | return 0; | |||
834 | ||||
835 | /* If we have the privileges we will ignore the kernel limit. */ | |||
836 | ||||
837 | value = (int) n; | |||
838 | if (setsockopt(fd, SOL_SOCKET1, SO_SNDBUFFORCE32, &value, sizeof(value)) < 0) | |||
839 | if (setsockopt(fd, SOL_SOCKET1, SO_SNDBUF7, &value, sizeof(value)) < 0) | |||
840 | return -errno(*__errno_location ()); | |||
841 | ||||
842 | return 1; | |||
843 | } | |||
844 | ||||
845 | int fd_inc_rcvbuf(int fd, size_t n) { | |||
846 | int r, value; | |||
847 | socklen_t l = sizeof(value); | |||
848 | ||||
849 | r = getsockopt(fd, SOL_SOCKET1, SO_RCVBUF8, &value, &l); | |||
850 | if (r >= 0 && l == sizeof(value) && (size_t) value >= n*2) | |||
851 | return 0; | |||
852 | ||||
853 | /* If we have the privileges we will ignore the kernel limit. */ | |||
854 | ||||
855 | value = (int) n; | |||
856 | if (setsockopt(fd, SOL_SOCKET1, SO_RCVBUFFORCE33, &value, sizeof(value)) < 0) | |||
857 | if (setsockopt(fd, SOL_SOCKET1, SO_RCVBUF8, &value, sizeof(value)) < 0) | |||
858 | return -errno(*__errno_location ()); | |||
859 | return 1; | |||
860 | } | |||
861 | ||||
862 | static const char* const ip_tos_table[] = { | |||
863 | [IPTOS_LOWDELAY0x10] = "low-delay", | |||
864 | [IPTOS_THROUGHPUT0x08] = "throughput", | |||
865 | [IPTOS_RELIABILITY0x04] = "reliability", | |||
866 | [IPTOS_LOWCOST0x02] = "low-cost", | |||
867 | }; | |||
868 | ||||
869 | DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(ip_tos, int, 0xff)int ip_tos_to_string_alloc(int i, char **str) { char *s; if ( i < 0 || i > 0xff) return -34; if (i < (int) __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (ip_tos_table), typeof(&*(ip_tos_table))), sizeof(ip_tos_table )/sizeof((ip_tos_table)[0]), ((void)0)))) { s = strdup(ip_tos_table [i]); if (!s) return -12; } else { if (asprintf(&s, "%i", i) < 0) return -12; } *str = s; return 0; } int ip_tos_from_string (const char *s) { int i; unsigned u = 0; if (!s) return (int) -1; for (i = 0; i < (int) __extension__ (__builtin_choose_expr ( !__builtin_types_compatible_p(typeof(ip_tos_table), typeof( &*(ip_tos_table))), sizeof(ip_tos_table)/sizeof((ip_tos_table )[0]), ((void)0))); i++) if (streq_ptr(ip_tos_table[i], s)) return i; if (safe_atou(s, &u) >= 0 && u <= 0xff) return (int) u; return (int) -1; }; | |||
870 | ||||
871 | bool_Bool ifname_valid(const char *p) { | |||
872 | bool_Bool numeric = true1; | |||
873 | ||||
874 | /* Checks whether a network interface name is valid. This is inspired by dev_valid_name() in the kernel sources | |||
875 | * but slightly stricter, as we only allow non-control, non-space ASCII characters in the interface name. We | |||
876 | * also don't permit names that only container numbers, to avoid confusion with numeric interface indexes. */ | |||
877 | ||||
878 | if (isempty(p)) | |||
879 | return false0; | |||
880 | ||||
881 | if (strlen(p) >= IFNAMSIZ16) | |||
882 | return false0; | |||
883 | ||||
884 | if (dot_or_dot_dot(p)) | |||
885 | return false0; | |||
886 | ||||
887 | while (*p) { | |||
888 | if ((unsigned char) *p >= 127U) | |||
889 | return false0; | |||
890 | ||||
891 | if ((unsigned char) *p <= 32U) | |||
892 | return false0; | |||
893 | ||||
894 | if (IN_SET(*p, ':', '/')({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){':', '/'})/sizeof(int)]; switch(*p) { case ':': case '/': _found = 1; break; default: break; } _found; } )) | |||
895 | return false0; | |||
896 | ||||
897 | numeric = numeric && (*p >= '0' && *p <= '9'); | |||
898 | p++; | |||
899 | } | |||
900 | ||||
901 | if (numeric) | |||
902 | return false0; | |||
903 | ||||
904 | return true1; | |||
905 | } | |||
906 | ||||
907 | bool_Bool address_label_valid(const char *p) { | |||
908 | ||||
909 | if (isempty(p)) | |||
910 | return false0; | |||
911 | ||||
912 | if (strlen(p) >= IFNAMSIZ16) | |||
913 | return false0; | |||
914 | ||||
915 | while (*p) { | |||
916 | if ((uint8_t) *p >= 127U) | |||
917 | return false0; | |||
918 | ||||
919 | if ((uint8_t) *p <= 31U) | |||
920 | return false0; | |||
921 | p++; | |||
922 | } | |||
923 | ||||
924 | return true1; | |||
925 | } | |||
926 | ||||
927 | int getpeercred(int fd, struct ucred *ucred) { | |||
928 | socklen_t n = sizeof(struct ucred); | |||
929 | struct ucred u; | |||
930 | int r; | |||
931 | ||||
932 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/basic/socket-util.c" , 932, __PRETTY_FUNCTION__); } while (0); | |||
933 | assert(ucred)do { if ((__builtin_expect(!!(!(ucred)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ucred"), "../src/basic/socket-util.c", 933 , __PRETTY_FUNCTION__); } while (0); | |||
934 | ||||
935 | r = getsockopt(fd, SOL_SOCKET1, SO_PEERCRED17, &u, &n); | |||
936 | if (r < 0) | |||
937 | return -errno(*__errno_location ()); | |||
938 | ||||
939 | if (n != sizeof(struct ucred)) | |||
940 | return -EIO5; | |||
941 | ||||
942 | /* Check if the data is actually useful and not suppressed due to namespacing issues */ | |||
943 | if (!pid_is_valid(u.pid)) | |||
944 | return -ENODATA61; | |||
945 | ||||
946 | /* Note that we don't check UID/GID here, as namespace translation works differently there: instead of | |||
947 | * receiving in "invalid" user/group we get the overflow UID/GID. */ | |||
948 | ||||
949 | *ucred = u; | |||
950 | return 0; | |||
951 | } | |||
952 | ||||
953 | int getpeersec(int fd, char **ret) { | |||
954 | _cleanup_free___attribute__((cleanup(freep))) char *s = NULL((void*)0); | |||
955 | socklen_t n = 64; | |||
956 | ||||
957 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/basic/socket-util.c" , 957, __PRETTY_FUNCTION__); } while (0); | |||
958 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/socket-util.c", 958 , __PRETTY_FUNCTION__); } while (0); | |||
959 | ||||
960 | for (;;) { | |||
961 | s = new0(char, n+1)((char*) calloc((n+1), sizeof(char))); | |||
962 | if (!s) | |||
963 | return -ENOMEM12; | |||
964 | ||||
965 | if (getsockopt(fd, SOL_SOCKET1, SO_PEERSEC31, s, &n) >= 0) | |||
966 | break; | |||
967 | ||||
968 | if (errno(*__errno_location ()) != ERANGE34) | |||
969 | return -errno(*__errno_location ()); | |||
970 | ||||
971 | s = mfree(s); | |||
972 | } | |||
973 | ||||
974 | if (isempty(s)) | |||
975 | return -EOPNOTSUPP95; | |||
976 | ||||
977 | *ret = TAKE_PTR(s)({ typeof(s) _ptr_ = (s); (s) = ((void*)0); _ptr_; }); | |||
978 | ||||
979 | return 0; | |||
980 | } | |||
981 | ||||
982 | int getpeergroups(int fd, gid_t **ret) { | |||
983 | socklen_t n = sizeof(gid_t) * 64; | |||
984 | _cleanup_free___attribute__((cleanup(freep))) gid_t *d = NULL((void*)0); | |||
985 | ||||
986 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/basic/socket-util.c" , 986, __PRETTY_FUNCTION__); } while (0); | |||
987 | assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret"), "../src/basic/socket-util.c", 987 , __PRETTY_FUNCTION__); } while (0); | |||
988 | ||||
989 | for (;;) { | |||
990 | d = malloc(n); | |||
991 | if (!d) | |||
992 | return -ENOMEM12; | |||
993 | ||||
994 | if (getsockopt(fd, SOL_SOCKET1, SO_PEERGROUPS59, d, &n) >= 0) | |||
995 | break; | |||
996 | ||||
997 | if (errno(*__errno_location ()) != ERANGE34) | |||
998 | return -errno(*__errno_location ()); | |||
999 | ||||
1000 | d = mfree(d); | |||
1001 | } | |||
1002 | ||||
1003 | assert_se(n % sizeof(gid_t) == 0)do { if ((__builtin_expect(!!(!(n % sizeof(gid_t) == 0)),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("n % sizeof(gid_t) == 0" ), "../src/basic/socket-util.c", 1003, __PRETTY_FUNCTION__); } while (0); | |||
1004 | n /= sizeof(gid_t); | |||
1005 | ||||
1006 | if ((socklen_t) (int) n != n) | |||
1007 | return -E2BIG7; | |||
1008 | ||||
1009 | *ret = TAKE_PTR(d)({ typeof(d) _ptr_ = (d); (d) = ((void*)0); _ptr_; }); | |||
1010 | ||||
1011 | return (int) n; | |||
1012 | } | |||
1013 | ||||
1014 | ssize_t send_one_fd_iov_sa( | |||
1015 | int transport_fd, | |||
1016 | int fd, | |||
1017 | struct iovec *iov, size_t iovlen, | |||
1018 | const struct sockaddr *sa, socklen_t len, | |||
1019 | int flags) { | |||
1020 | ||||
1021 | union { | |||
1022 | struct cmsghdr cmsghdr; | |||
1023 | uint8_t buf[CMSG_SPACE(sizeof(int))((((sizeof(int)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t ) - 1) & (size_t) ~(sizeof (size_t) - 1)))]; | |||
1024 | } control = {}; | |||
1025 | struct msghdr mh = { | |||
1026 | .msg_name = (struct sockaddr*) sa, | |||
1027 | .msg_namelen = len, | |||
1028 | .msg_iov = iov, | |||
1029 | .msg_iovlen = iovlen, | |||
1030 | }; | |||
1031 | ssize_t k; | |||
1032 | ||||
1033 | assert(transport_fd >= 0)do { if ((__builtin_expect(!!(!(transport_fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("transport_fd >= 0"), "../src/basic/socket-util.c" , 1033, __PRETTY_FUNCTION__); } while (0); | |||
1034 | ||||
1035 | /* | |||
1036 | * We need either an FD or data to send. | |||
1037 | * If there's nothing, return an error. | |||
1038 | */ | |||
1039 | if (fd < 0 && !iov) | |||
1040 | return -EINVAL22; | |||
1041 | ||||
1042 | if (fd >= 0) { | |||
1043 | struct cmsghdr *cmsg; | |||
1044 | ||||
1045 | mh.msg_control = &control; | |||
1046 | mh.msg_controllen = sizeof(control); | |||
1047 | ||||
1048 | cmsg = CMSG_FIRSTHDR(&mh)((size_t) (&mh)->msg_controllen >= sizeof (struct cmsghdr ) ? (struct cmsghdr *) (&mh)->msg_control : (struct cmsghdr *) 0); | |||
1049 | cmsg->cmsg_level = SOL_SOCKET1; | |||
1050 | cmsg->cmsg_type = SCM_RIGHTSSCM_RIGHTS; | |||
1051 | cmsg->cmsg_len = CMSG_LEN(sizeof(int))((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t ) ~(sizeof (size_t) - 1)) + (sizeof(int))); | |||
1052 | memcpy(CMSG_DATA(cmsg)((cmsg)->__cmsg_data), &fd, sizeof(int)); | |||
1053 | ||||
1054 | mh.msg_controllen = CMSG_SPACE(sizeof(int))((((sizeof(int)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t ) - 1) & (size_t) ~(sizeof (size_t) - 1))); | |||
1055 | } | |||
1056 | k = sendmsg(transport_fd, &mh, MSG_NOSIGNALMSG_NOSIGNAL | flags); | |||
1057 | if (k < 0) | |||
1058 | return (ssize_t) -errno(*__errno_location ()); | |||
1059 | ||||
1060 | return k; | |||
1061 | } | |||
1062 | ||||
1063 | int send_one_fd_sa( | |||
1064 | int transport_fd, | |||
1065 | int fd, | |||
1066 | const struct sockaddr *sa, socklen_t len, | |||
1067 | int flags) { | |||
1068 | ||||
1069 | assert(fd >= 0)do { if ((__builtin_expect(!!(!(fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("fd >= 0"), "../src/basic/socket-util.c" , 1069, __PRETTY_FUNCTION__); } while (0); | |||
1070 | ||||
1071 | return (int) send_one_fd_iov_sa(transport_fd, fd, NULL((void*)0), 0, sa, len, flags); | |||
1072 | } | |||
1073 | ||||
1074 | ssize_t receive_one_fd_iov( | |||
1075 | int transport_fd, | |||
1076 | struct iovec *iov, size_t iovlen, | |||
1077 | int flags, | |||
1078 | int *ret_fd) { | |||
1079 | ||||
1080 | union { | |||
1081 | struct cmsghdr cmsghdr; | |||
1082 | uint8_t buf[CMSG_SPACE(sizeof(int))((((sizeof(int)) + sizeof (size_t) - 1) & (size_t) ~(sizeof (size_t) - 1)) + (((sizeof (struct cmsghdr)) + sizeof (size_t ) - 1) & (size_t) ~(sizeof (size_t) - 1)))]; | |||
1083 | } control = {}; | |||
1084 | struct msghdr mh = { | |||
1085 | .msg_control = &control, | |||
1086 | .msg_controllen = sizeof(control), | |||
1087 | .msg_iov = iov, | |||
1088 | .msg_iovlen = iovlen, | |||
1089 | }; | |||
1090 | struct cmsghdr *cmsg, *found = NULL((void*)0); | |||
1091 | ssize_t k; | |||
1092 | ||||
1093 | assert(transport_fd >= 0)do { if ((__builtin_expect(!!(!(transport_fd >= 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("transport_fd >= 0"), "../src/basic/socket-util.c" , 1093, __PRETTY_FUNCTION__); } while (0); | |||
1094 | assert(ret_fd)do { if ((__builtin_expect(!!(!(ret_fd)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("ret_fd"), "../src/basic/socket-util.c", 1094, __PRETTY_FUNCTION__); } while (0); | |||
1095 | ||||
1096 | /* | |||
1097 | * Receive a single FD via @transport_fd. We don't care for | |||
1098 | * the transport-type. We retrieve a single FD at most, so for | |||
1099 | * packet-based transports, the caller must ensure to send | |||
1100 | * only a single FD per packet. This is best used in | |||
1101 | * combination with send_one_fd(). | |||
1102 | */ | |||
1103 | ||||
1104 | k = recvmsg(transport_fd, &mh, MSG_CMSG_CLOEXECMSG_CMSG_CLOEXEC | flags); | |||
1105 | if (k < 0) | |||
1106 | return (ssize_t) -errno(*__errno_location ()); | |||
1107 | ||||
1108 | CMSG_FOREACH(cmsg, &mh)for ((cmsg) = ((size_t) (&mh)->msg_controllen >= sizeof (struct cmsghdr) ? (struct cmsghdr *) (&mh)->msg_control : (struct cmsghdr *) 0); (cmsg); (cmsg) = __cmsg_nxthdr ((& mh), (cmsg))) { | |||
1109 | if (cmsg->cmsg_level == SOL_SOCKET1 && | |||
1110 | cmsg->cmsg_type == SCM_RIGHTSSCM_RIGHTS && | |||
1111 | cmsg->cmsg_len == CMSG_LEN(sizeof(int))((((sizeof (struct cmsghdr)) + sizeof (size_t) - 1) & (size_t ) ~(sizeof (size_t) - 1)) + (sizeof(int)))) { | |||
1112 | assert(!found)do { if ((__builtin_expect(!!(!(!found)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("!found"), "../src/basic/socket-util.c", 1112, __PRETTY_FUNCTION__); } while (0); | |||
1113 | found = cmsg; | |||
1114 | break; | |||
1115 | } | |||
1116 | } | |||
1117 | ||||
1118 | if (!found) | |||
1119 | cmsg_close_all(&mh); | |||
1120 | ||||
1121 | /* If didn't receive an FD or any data, return an error. */ | |||
1122 | if (k == 0 && !found) | |||
1123 | return -EIO5; | |||
1124 | ||||
1125 | if (found) | |||
1126 | *ret_fd = *(int*) CMSG_DATA(found)((found)->__cmsg_data); | |||
1127 | else | |||
1128 | *ret_fd = -1; | |||
1129 | ||||
1130 | return k; | |||
1131 | } | |||
1132 | ||||
1133 | int receive_one_fd(int transport_fd, int flags) { | |||
1134 | int fd; | |||
1135 | ssize_t k; | |||
1136 | ||||
1137 | k = receive_one_fd_iov(transport_fd, NULL((void*)0), 0, flags, &fd); | |||
1138 | if (k == 0) | |||
1139 | return fd; | |||
1140 | ||||
1141 | /* k must be negative, since receive_one_fd_iov() only returns | |||
1142 | * a positive value if data was received through the iov. */ | |||
1143 | assert(k < 0)do { if ((__builtin_expect(!!(!(k < 0)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("k < 0"), "../src/basic/socket-util.c" , 1143, __PRETTY_FUNCTION__); } while (0); | |||
1144 | return (int) k; | |||
1145 | } | |||
1146 | ||||
1147 | ssize_t next_datagram_size_fd(int fd) { | |||
1148 | ssize_t l; | |||
1149 | int k; | |||
1150 | ||||
1151 | /* This is a bit like FIONREAD/SIOCINQ, however a bit more powerful. The difference being: recv(MSG_PEEK) will | |||
1152 | * actually cause the next datagram in the queue to be validated regarding checksums, which FIONREAD doesn't | |||
1153 | * do. This difference is actually of major importance as we need to be sure that the size returned here | |||
1154 | * actually matches what we will read with recvmsg() next, as otherwise we might end up allocating a buffer of | |||
1155 | * the wrong size. */ | |||
1156 | ||||
1157 | l = recv(fd, NULL((void*)0), 0, MSG_PEEKMSG_PEEK|MSG_TRUNCMSG_TRUNC); | |||
1158 | if (l < 0) { | |||
1159 | if (IN_SET(errno, EOPNOTSUPP, EFAULT)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){95, 14})/sizeof(int)]; switch((*__errno_location ())) { case 95: case 14: _found = 1; break; default: break; } _found; })) | |||
1160 | goto fallback; | |||
1161 | ||||
1162 | return -errno(*__errno_location ()); | |||
1163 | } | |||
1164 | if (l == 0) | |||
1165 | goto fallback; | |||
1166 | ||||
1167 | return l; | |||
1168 | ||||
1169 | fallback: | |||
1170 | k = 0; | |||
1171 | ||||
1172 | /* Some sockets (AF_PACKET) do not support null-sized recv() with MSG_TRUNC set, let's fall back to FIONREAD | |||
1173 | * for them. Checksums don't matter for raw sockets anyway, hence this should be fine. */ | |||
1174 | ||||
1175 | if (ioctl(fd, FIONREAD0x541B, &k) < 0) | |||
1176 | return -errno(*__errno_location ()); | |||
1177 | ||||
1178 | return (ssize_t) k; | |||
1179 | } | |||
1180 | ||||
1181 | int flush_accept(int fd) { | |||
1182 | ||||
1183 | struct pollfd pollfd = { | |||
1184 | .fd = fd, | |||
1185 | .events = POLLIN0x001, | |||
1186 | }; | |||
1187 | int r; | |||
1188 | ||||
1189 | /* Similar to flush_fd() but flushes all incoming connection by accepting them and immediately closing them. */ | |||
1190 | ||||
1191 | for (;;) { | |||
1192 | int cfd; | |||
1193 | ||||
1194 | r = poll(&pollfd, 1, 0); | |||
1195 | if (r < 0) { | |||
1196 | if (errno(*__errno_location ()) == EINTR4) | |||
1197 | continue; | |||
1198 | ||||
1199 | return -errno(*__errno_location ()); | |||
1200 | ||||
1201 | } else if (r == 0) | |||
1202 | return 0; | |||
1203 | ||||
1204 | cfd = accept4(fd, NULL((void*)0), NULL((void*)0), SOCK_NONBLOCKSOCK_NONBLOCK|SOCK_CLOEXECSOCK_CLOEXEC); | |||
1205 | if (cfd < 0) { | |||
1206 | if (errno(*__errno_location ()) == EINTR4) | |||
1207 | continue; | |||
1208 | ||||
1209 | if (errno(*__errno_location ()) == EAGAIN11) | |||
1210 | return 0; | |||
1211 | ||||
1212 | return -errno(*__errno_location ()); | |||
1213 | } | |||
1214 | ||||
1215 | close(cfd); | |||
1216 | } | |||
1217 | } | |||
1218 | ||||
1219 | struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length) { | |||
1220 | struct cmsghdr *cmsg; | |||
1221 | ||||
1222 | assert(mh)do { if ((__builtin_expect(!!(!(mh)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("mh"), "../src/basic/socket-util.c", 1222 , __PRETTY_FUNCTION__); } while (0); | |||
1223 | ||||
1224 | CMSG_FOREACH(cmsg, mh)for ((cmsg) = ((size_t) (mh)->msg_controllen >= sizeof ( struct cmsghdr) ? (struct cmsghdr *) (mh)->msg_control : ( struct cmsghdr *) 0); (cmsg); (cmsg) = __cmsg_nxthdr ((mh), ( cmsg))) | |||
1225 | if (cmsg->cmsg_level == level && | |||
1226 | cmsg->cmsg_type == type && | |||
1227 | (length == (socklen_t) -1 || length == cmsg->cmsg_len)) | |||
1228 | return cmsg; | |||
1229 | ||||
1230 | return NULL((void*)0); | |||
1231 | } | |||
1232 | ||||
1233 | int socket_ioctl_fd(void) { | |||
1234 | int fd; | |||
1235 | ||||
1236 | /* Create a socket to invoke the various network interface ioctl()s on. Traditionally only AF_INET was good for | |||
1237 | * that. Since kernel 4.6 AF_NETLINK works for this too. We first try to use AF_INET hence, but if that's not | |||
1238 | * available (for example, because it is made unavailable via SECCOMP or such), we'll fall back to the more | |||
1239 | * generic AF_NETLINK. */ | |||
1240 | ||||
1241 | fd = socket(AF_INET2, SOCK_DGRAMSOCK_DGRAM|SOCK_CLOEXECSOCK_CLOEXEC, 0); | |||
1242 | if (fd < 0) | |||
1243 | fd = socket(AF_NETLINK16, SOCK_RAWSOCK_RAW|SOCK_CLOEXECSOCK_CLOEXEC, NETLINK_GENERIC16); | |||
1244 | if (fd < 0) | |||
1245 | return -errno(*__errno_location ()); | |||
1246 | ||||
1247 | return fd; | |||
1248 | } | |||
1249 | ||||
1250 | int socket_pass_pktinfo(int fd, bool_Bool b) { | |||
1251 | int af; | |||
1252 | socklen_t sl = sizeof(af); | |||
1253 | ||||
1254 | if (getsockopt(fd, SOL_SOCKET1, SO_DOMAIN39, &af, &sl) < 0) | |||
1255 | return -errno(*__errno_location ()); | |||
1256 | ||||
1257 | switch (af) { | |||
1258 | ||||
1259 | case AF_INET2: | |||
1260 | return setsockopt_int(fd, IPPROTO_IPIPPROTO_IP, IP_PKTINFO8, b); | |||
1261 | ||||
1262 | case AF_INET610: | |||
1263 | return setsockopt_int(fd, IPPROTO_IPV6IPPROTO_IPV6, IPV6_RECVPKTINFO49, b); | |||
1264 | ||||
1265 | case AF_NETLINK16: | |||
1266 | return setsockopt_int(fd, SOL_NETLINK270, NETLINK_PKTINFO3, b); | |||
1267 | ||||
1268 | default: | |||
1269 | return -EAFNOSUPPORT97; | |||
1270 | } | |||
1271 | } |