]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/socket-netlink.c
Merge pull request #16491 from keszybz/udev-logging
[thirdparty/systemd.git] / src / shared / socket-netlink.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <arpa/inet.h>
4 #include <errno.h>
5 #include <net/if.h>
6 #include <string.h>
7
8 #include "alloc-util.h"
9 #include "errno-util.h"
10 #include "extract-word.h"
11 #include "log.h"
12 #include "memory-util.h"
13 #include "netlink-util.h"
14 #include "parse-util.h"
15 #include "socket-netlink.h"
16 #include "socket-util.h"
17 #include "string-util.h"
18
19 int resolve_ifname(sd_netlink **rtnl, const char *name) {
20 int r;
21
22 /* Like if_nametoindex, but resolves "alternative names" too. */
23
24 assert(name);
25
26 r = if_nametoindex(name);
27 if (r > 0)
28 return r;
29
30 return rtnl_resolve_link_alternative_name(rtnl, name);
31 }
32
33 int resolve_interface(sd_netlink **rtnl, const char *name) {
34 int r;
35
36 /* Like resolve_ifname, but resolves interface numbers too. */
37
38 assert(name);
39
40 r = parse_ifindex(name);
41 if (r > 0)
42 return r;
43 assert(r < 0);
44
45 return resolve_ifname(rtnl, name);
46 }
47
48 int resolve_interface_or_warn(sd_netlink **rtnl, const char *name) {
49 int r;
50
51 r = resolve_interface(rtnl, name);
52 if (r < 0)
53 return log_error_errno(r, "Failed to resolve interface \"%s\": %m", name);
54 return r;
55 }
56
57 int socket_address_parse(SocketAddress *a, const char *s) {
58 _cleanup_free_ char *n = NULL;
59 char *e;
60 int r;
61
62 assert(a);
63 assert(s);
64
65 *a = (SocketAddress) {
66 .type = SOCK_STREAM,
67 };
68
69 if (*s == '[') {
70 uint16_t port;
71
72 /* IPv6 in [x:.....:z]:p notation */
73
74 e = strchr(s+1, ']');
75 if (!e)
76 return -EINVAL;
77
78 n = strndup(s+1, e-s-1);
79 if (!n)
80 return -ENOMEM;
81
82 errno = 0;
83 if (inet_pton(AF_INET6, n, &a->sockaddr.in6.sin6_addr) <= 0)
84 return errno_or_else(EINVAL);
85
86 e++;
87 if (*e != ':')
88 return -EINVAL;
89
90 e++;
91 r = parse_ip_port(e, &port);
92 if (r < 0)
93 return r;
94
95 a->sockaddr.in6.sin6_family = AF_INET6;
96 a->sockaddr.in6.sin6_port = htobe16(port);
97 a->size = sizeof(struct sockaddr_in6);
98
99 } else if (*s == '/') {
100 /* AF_UNIX socket */
101
102 size_t l;
103
104 l = strlen(s);
105 if (l >= sizeof(a->sockaddr.un.sun_path)) /* Note that we refuse non-NUL-terminated sockets when
106 * parsing (the kernel itself is less strict here in what it
107 * accepts) */
108 return -EINVAL;
109
110 a->sockaddr.un.sun_family = AF_UNIX;
111 memcpy(a->sockaddr.un.sun_path, s, l);
112 a->size = offsetof(struct sockaddr_un, sun_path) + l + 1;
113
114 } else if (*s == '@') {
115 /* Abstract AF_UNIX socket */
116 size_t l;
117
118 l = strlen(s+1);
119 if (l >= sizeof(a->sockaddr.un.sun_path) - 1) /* Note that we refuse non-NUL-terminated sockets here
120 * when parsing, even though abstract namespace sockets
121 * explicitly allow embedded NUL bytes and don't consider
122 * them special. But it's simply annoying to debug such
123 * sockets. */
124 return -EINVAL;
125
126 a->sockaddr.un.sun_family = AF_UNIX;
127 memcpy(a->sockaddr.un.sun_path+1, s+1, l);
128 a->size = offsetof(struct sockaddr_un, sun_path) + 1 + l;
129
130 } else if (startswith(s, "vsock:")) {
131 /* AF_VSOCK socket in vsock:cid:port notation */
132 const char *cid_start = s + STRLEN("vsock:");
133 unsigned port;
134
135 e = strchr(cid_start, ':');
136 if (!e)
137 return -EINVAL;
138
139 r = safe_atou(e+1, &port);
140 if (r < 0)
141 return r;
142
143 n = strndup(cid_start, e - cid_start);
144 if (!n)
145 return -ENOMEM;
146
147 if (!isempty(n)) {
148 r = safe_atou(n, &a->sockaddr.vm.svm_cid);
149 if (r < 0)
150 return r;
151 } else
152 a->sockaddr.vm.svm_cid = VMADDR_CID_ANY;
153
154 a->sockaddr.vm.svm_family = AF_VSOCK;
155 a->sockaddr.vm.svm_port = port;
156 a->size = sizeof(struct sockaddr_vm);
157
158 } else {
159 uint16_t port;
160
161 e = strchr(s, ':');
162 if (e) {
163 r = parse_ip_port(e + 1, &port);
164 if (r < 0)
165 return r;
166
167 n = strndup(s, e-s);
168 if (!n)
169 return -ENOMEM;
170
171 /* IPv4 in w.x.y.z:p notation? */
172 r = inet_pton(AF_INET, n, &a->sockaddr.in.sin_addr);
173 if (r < 0)
174 return -errno;
175
176 if (r > 0) {
177 /* Gotcha, it's a traditional IPv4 address */
178 a->sockaddr.in.sin_family = AF_INET;
179 a->sockaddr.in.sin_port = htobe16(port);
180 a->size = sizeof(struct sockaddr_in);
181 } else {
182 int idx;
183
184 /* Uh, our last resort, an interface name */
185 idx = resolve_ifname(NULL, n);
186 if (idx < 0)
187 return idx;
188
189 a->sockaddr.in6.sin6_family = AF_INET6;
190 a->sockaddr.in6.sin6_port = htobe16(port);
191 a->sockaddr.in6.sin6_scope_id = idx;
192 a->sockaddr.in6.sin6_addr = in6addr_any;
193 a->size = sizeof(struct sockaddr_in6);
194 }
195 } else {
196
197 /* Just a port */
198 r = parse_ip_port(s, &port);
199 if (r < 0)
200 return r;
201
202 if (socket_ipv6_is_supported()) {
203 a->sockaddr.in6.sin6_family = AF_INET6;
204 a->sockaddr.in6.sin6_port = htobe16(port);
205 a->sockaddr.in6.sin6_addr = in6addr_any;
206 a->size = sizeof(struct sockaddr_in6);
207 } else {
208 a->sockaddr.in.sin_family = AF_INET;
209 a->sockaddr.in.sin_port = htobe16(port);
210 a->sockaddr.in.sin_addr.s_addr = INADDR_ANY;
211 a->size = sizeof(struct sockaddr_in);
212 }
213 }
214 }
215
216 return 0;
217 }
218
219 int socket_address_parse_and_warn(SocketAddress *a, const char *s) {
220 SocketAddress b;
221 int r;
222
223 /* Similar to socket_address_parse() but warns for IPv6 sockets when we don't support them. */
224
225 r = socket_address_parse(&b, s);
226 if (r < 0)
227 return r;
228
229 if (!socket_ipv6_is_supported() && b.sockaddr.sa.sa_family == AF_INET6) {
230 log_warning("Binding to IPv6 address not available since kernel does not support IPv6.");
231 return -EAFNOSUPPORT;
232 }
233
234 *a = b;
235 return 0;
236 }
237
238 int socket_address_parse_netlink(SocketAddress *a, const char *s) {
239 _cleanup_free_ char *word = NULL;
240 unsigned group = 0;
241 int family, r;
242
243 assert(a);
244 assert(s);
245
246 *a = (SocketAddress) {
247 .type = SOCK_RAW,
248 };
249
250 r = extract_first_word(&s, &word, NULL, 0);
251 if (r < 0)
252 return r;
253 if (r == 0)
254 return -EINVAL;
255
256 family = netlink_family_from_string(word);
257 if (family < 0)
258 return -EINVAL;
259
260 if (!isempty(s)) {
261 r = safe_atou(s, &group);
262 if (r < 0)
263 return r;
264 }
265
266 a->sockaddr.nl.nl_family = AF_NETLINK;
267 a->sockaddr.nl.nl_groups = group;
268
269 a->type = SOCK_RAW;
270 a->size = sizeof(struct sockaddr_nl);
271 a->protocol = family;
272
273 return 0;
274 }
275
276 bool socket_address_is(const SocketAddress *a, const char *s, int type) {
277 struct SocketAddress b;
278
279 assert(a);
280 assert(s);
281
282 if (socket_address_parse(&b, s) < 0)
283 return false;
284
285 b.type = type;
286
287 return socket_address_equal(a, &b);
288 }
289
290 bool socket_address_is_netlink(const SocketAddress *a, const char *s) {
291 struct SocketAddress b;
292
293 assert(a);
294 assert(s);
295
296 if (socket_address_parse_netlink(&b, s) < 0)
297 return false;
298
299 return socket_address_equal(a, &b);
300 }
301
302 int make_socket_fd(int log_level, const char* address, int type, int flags) {
303 SocketAddress a;
304 int fd, r;
305
306 r = socket_address_parse(&a, address);
307 if (r < 0)
308 return log_error_errno(r, "Failed to parse socket address \"%s\": %m", address);
309
310 a.type = type;
311
312 fd = socket_address_listen(&a, type | flags, SOMAXCONN, SOCKET_ADDRESS_DEFAULT,
313 NULL, false, false, false, 0755, 0644, NULL);
314 if (fd < 0 || log_get_max_level() >= log_level) {
315 _cleanup_free_ char *p = NULL;
316
317 r = socket_address_print(&a, &p);
318 if (r < 0)
319 return log_error_errno(r, "socket_address_print(): %m");
320
321 if (fd < 0)
322 log_error_errno(fd, "Failed to listen on %s: %m", p);
323 else
324 log_full(log_level, "Listening on %s", p);
325 }
326
327 return fd;
328 }
329
330 int in_addr_ifindex_from_string_auto(const char *s, int *family, union in_addr_union *ret_addr, int *ret_ifindex) {
331 _cleanup_free_ char *buf = NULL;
332 const char *suffix;
333 int r, ifindex = 0;
334
335 assert(s);
336 assert(family);
337 assert(ret_addr);
338
339 /* Similar to in_addr_from_string_auto() but also parses an optionally appended IPv6 zone suffix ("scope id")
340 * if one is found. */
341
342 suffix = strchr(s, '%');
343 if (suffix) {
344 if (ret_ifindex) {
345 /* If we shall return the interface index, try to parse it */
346 ifindex = resolve_interface(NULL, suffix + 1);
347 if (ifindex < 0)
348 return ifindex;
349 }
350
351 s = buf = strndup(s, suffix - s);
352 if (!buf)
353 return -ENOMEM;
354 }
355
356 r = in_addr_from_string_auto(s, family, ret_addr);
357 if (r < 0)
358 return r;
359
360 if (ret_ifindex)
361 *ret_ifindex = ifindex;
362
363 return r;
364 }
365
366 int in_addr_ifindex_name_from_string_auto(const char *s, int *family, union in_addr_union *ret, int *ifindex, char **server_name) {
367 _cleanup_free_ char *buf = NULL, *name = NULL;
368 const char *m;
369 int r;
370
371 assert(s);
372
373 m = strchr(s, '#');
374 if (m) {
375 name = strdup(m+1);
376 if (!name)
377 return -ENOMEM;
378
379 buf = strndup(s, m - s);
380 if (!buf)
381 return -ENOMEM;
382
383 s = buf;
384 }
385
386 r = in_addr_ifindex_from_string_auto(s, family, ret, ifindex);
387 if (r < 0)
388 return r;
389
390 if (server_name)
391 *server_name = TAKE_PTR(name);
392
393 return r;
394 }