]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-manager.c
Merge pull request #12392 from poettering/firstboot-salt
[thirdparty/systemd.git] / src / network / networkd-manager.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <netinet/in.h>
4 #include <sys/socket.h>
5 #include <unistd.h>
6 #include <linux/if.h>
7 #include <linux/fib_rules.h>
8
9 #include "sd-daemon.h"
10 #include "sd-netlink.h"
11
12 #include "alloc-util.h"
13 #include "bus-util.h"
14 #include "conf-parser.h"
15 #include "def.h"
16 #include "device-private.h"
17 #include "device-util.h"
18 #include "dns-domain.h"
19 #include "fd-util.h"
20 #include "fileio.h"
21 #include "local-addresses.h"
22 #include "netlink-util.h"
23 #include "network-internal.h"
24 #include "networkd-manager.h"
25 #include "ordered-set.h"
26 #include "path-util.h"
27 #include "set.h"
28 #include "strv.h"
29 #include "sysctl-util.h"
30 #include "tmpfile-util.h"
31 #include "udev-util.h"
32 #include "virt.h"
33
34 /* use 8 MB for receive socket kernel queue. */
35 #define RCVBUF_SIZE (8*1024*1024)
36
37 static int setup_default_address_pool(Manager *m) {
38 AddressPool *p;
39 int r;
40
41 assert(m);
42
43 /* Add in the well-known private address ranges. */
44
45 r = address_pool_new_from_string(m, &p, AF_INET6, "fd00::", 8);
46 if (r < 0)
47 return r;
48
49 r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
50 if (r < 0)
51 return r;
52
53 r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
54 if (r < 0)
55 return r;
56
57 r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
58 if (r < 0)
59 return r;
60
61 return 0;
62 }
63
64 static int manager_reset_all(Manager *m) {
65 Link *link;
66 Iterator i;
67 int r;
68
69 assert(m);
70
71 HASHMAP_FOREACH(link, m->links, i) {
72 r = link_carrier_reset(link);
73 if (r < 0)
74 log_link_warning_errno(link, r, "Could not reset carrier: %m");
75 }
76
77 return 0;
78 }
79
80 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
81 Manager *m = userdata;
82 int b, r;
83
84 assert(message);
85 assert(m);
86
87 r = sd_bus_message_read(message, "b", &b);
88 if (r < 0) {
89 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
90 return 0;
91 }
92
93 if (b)
94 return 0;
95
96 log_debug("Coming back from suspend, resetting all connections...");
97
98 (void) manager_reset_all(m);
99
100 return 0;
101 }
102
103 static int on_connected(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
104 Manager *m = userdata;
105
106 assert(message);
107 assert(m);
108
109 /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */
110 if (m->dynamic_hostname)
111 (void) manager_set_hostname(m, m->dynamic_hostname);
112 if (m->dynamic_timezone)
113 (void) manager_set_timezone(m, m->dynamic_timezone);
114 if (m->links_requesting_uuid)
115 (void) manager_request_product_uuid(m, NULL);
116
117 return 0;
118 }
119
120 int manager_connect_bus(Manager *m) {
121 int r;
122
123 assert(m);
124
125 if (m->bus)
126 return 0;
127
128 r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-network");
129 if (r < 0)
130 return log_error_errno(r, "Failed to connect to bus: %m");
131
132 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m);
133 if (r < 0)
134 return log_error_errno(r, "Failed to add manager object vtable: %m");
135
136 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m);
137 if (r < 0)
138 return log_error_errno(r, "Failed to add link object vtable: %m");
139
140 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m);
141 if (r < 0)
142 return log_error_errno(r, "Failed to add link enumerator: %m");
143
144 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m);
145 if (r < 0)
146 return log_error_errno(r, "Failed to add network object vtable: %m");
147
148 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m);
149 if (r < 0)
150 return log_error_errno(r, "Failed to add network enumerator: %m");
151
152 r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.network1", 0, NULL, NULL);
153 if (r < 0)
154 return log_error_errno(r, "Failed to request name: %m");
155
156 r = sd_bus_attach_event(m->bus, m->event, 0);
157 if (r < 0)
158 return log_error_errno(r, "Failed to attach bus to event loop: %m");
159
160 r = sd_bus_match_signal_async(
161 m->bus,
162 NULL,
163 "org.freedesktop.DBus.Local",
164 NULL,
165 "org.freedesktop.DBus.Local",
166 "Connected",
167 on_connected, NULL, m);
168 if (r < 0)
169 return log_error_errno(r, "Failed to request match on Connected signal: %m");
170
171 r = sd_bus_match_signal_async(
172 m->bus,
173 NULL,
174 "org.freedesktop.login1",
175 "/org/freedesktop/login1",
176 "org.freedesktop.login1.Manager",
177 "PrepareForSleep",
178 match_prepare_for_sleep, NULL, m);
179 if (r < 0)
180 log_warning_errno(r, "Failed to request match for PrepareForSleep, ignoring: %m");
181
182 return 0;
183 }
184
185 static int manager_udev_process_link(sd_device_monitor *monitor, sd_device *device, void *userdata) {
186 Manager *m = userdata;
187 DeviceAction action;
188 Link *link = NULL;
189 int r, ifindex;
190
191 assert(m);
192 assert(device);
193
194 r = device_get_action(device, &action);
195 if (r < 0) {
196 log_device_debug_errno(device, r, "Failed to get udev action, ignoring device: %m");
197 return 0;
198 }
199
200 if (!IN_SET(action, DEVICE_ACTION_ADD, DEVICE_ACTION_CHANGE, DEVICE_ACTION_MOVE)) {
201 log_device_debug(device, "Ignoring udev %s event for device.", device_action_to_string(action));
202 return 0;
203 }
204
205 r = sd_device_get_ifindex(device, &ifindex);
206 if (r < 0) {
207 log_device_debug_errno(device, r, "Ignoring udev ADD event for device without ifindex or with invalid ifindex: %m");
208 return 0;
209 }
210
211 r = device_is_renaming(device);
212 if (r < 0) {
213 log_device_error_errno(device, r, "Failed to determine the device is renamed or not, ignoring '%s' uevent: %m",
214 device_action_to_string(action));
215 return 0;
216 }
217 if (r > 0) {
218 log_device_debug(device, "Interface is under renaming, wait for the interface to be renamed: %m");
219 return 0;
220 }
221
222 r = link_get(m, ifindex, &link);
223 if (r < 0) {
224 if (r != -ENODEV)
225 log_debug_errno(r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
226 return 0;
227 }
228
229 (void) link_initialized(link, device);
230
231 return 0;
232 }
233
234 static int manager_connect_udev(Manager *m) {
235 int r;
236
237 /* udev does not initialize devices inside containers,
238 * so we rely on them being already initialized before
239 * entering the container */
240 if (detect_container() > 0)
241 return 0;
242
243 r = sd_device_monitor_new(&m->device_monitor);
244 if (r < 0)
245 return log_error_errno(r, "Failed to initialize device monitor: %m");
246
247 r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "net", NULL);
248 if (r < 0)
249 return log_error_errno(r, "Could not add device monitor filter: %m");
250
251 r = sd_device_monitor_attach_event(m->device_monitor, m->event);
252 if (r < 0)
253 return log_error_errno(r, "Failed to attach event to device monitor: %m");
254
255 r = sd_device_monitor_start(m->device_monitor, manager_udev_process_link, m);
256 if (r < 0)
257 return log_error_errno(r, "Failed to start device monitor: %m");
258
259 return 0;
260 }
261
262 int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
263 Manager *m = userdata;
264 Link *link = NULL;
265 uint16_t type;
266 uint32_t ifindex, priority = 0;
267 unsigned char protocol, scope, tos, table, rt_type;
268 int family;
269 unsigned char dst_prefixlen, src_prefixlen;
270 union in_addr_union dst = IN_ADDR_NULL, gw = IN_ADDR_NULL, src = IN_ADDR_NULL, prefsrc = IN_ADDR_NULL;
271 Route *route = NULL;
272 int r;
273
274 assert(rtnl);
275 assert(message);
276 assert(m);
277
278 if (sd_netlink_message_is_error(message)) {
279 r = sd_netlink_message_get_errno(message);
280 if (r < 0)
281 log_warning_errno(r, "rtnl: failed to receive route, ignoring: %m");
282
283 return 0;
284 }
285
286 r = sd_netlink_message_get_type(message, &type);
287 if (r < 0) {
288 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
289 return 0;
290 } else if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) {
291 log_warning("rtnl: received unexpected message type when processing route, ignoring");
292 return 0;
293 }
294
295 r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex);
296 if (r == -ENODATA) {
297 log_debug("rtnl: received route without ifindex, ignoring");
298 return 0;
299 } else if (r < 0) {
300 log_warning_errno(r, "rtnl: could not get ifindex from route, ignoring: %m");
301 return 0;
302 } else if (ifindex <= 0) {
303 log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex);
304 return 0;
305 } else {
306 r = link_get(m, ifindex, &link);
307 if (r < 0 || !link) {
308 /* when enumerating we might be out of sync, but we will
309 * get the route again, so just ignore it */
310 if (!m->enumerating)
311 log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex);
312 return 0;
313 }
314 }
315
316 r = sd_rtnl_message_route_get_family(message, &family);
317 if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) {
318 log_link_warning(link, "rtnl: received address with invalid family, ignoring");
319 return 0;
320 }
321
322 r = sd_rtnl_message_route_get_protocol(message, &protocol);
323 if (r < 0) {
324 log_warning_errno(r, "rtnl: could not get route protocol: %m");
325 return 0;
326 }
327
328 switch (family) {
329 case AF_INET:
330 r = sd_netlink_message_read_in_addr(message, RTA_DST, &dst.in);
331 if (r < 0 && r != -ENODATA) {
332 log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m");
333 return 0;
334 }
335
336 r = sd_netlink_message_read_in_addr(message, RTA_GATEWAY, &gw.in);
337 if (r < 0 && r != -ENODATA) {
338 log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m");
339 return 0;
340 }
341
342 r = sd_netlink_message_read_in_addr(message, RTA_SRC, &src.in);
343 if (r < 0 && r != -ENODATA) {
344 log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m");
345 return 0;
346 }
347
348 r = sd_netlink_message_read_in_addr(message, RTA_PREFSRC, &prefsrc.in);
349 if (r < 0 && r != -ENODATA) {
350 log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m");
351 return 0;
352 }
353
354 break;
355
356 case AF_INET6:
357 r = sd_netlink_message_read_in6_addr(message, RTA_DST, &dst.in6);
358 if (r < 0 && r != -ENODATA) {
359 log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m");
360 return 0;
361 }
362
363 r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &gw.in6);
364 if (r < 0 && r != -ENODATA) {
365 log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m");
366 return 0;
367 }
368
369 r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &src.in6);
370 if (r < 0 && r != -ENODATA) {
371 log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m");
372 return 0;
373 }
374
375 r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &prefsrc.in6);
376 if (r < 0 && r != -ENODATA) {
377 log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m");
378 return 0;
379 }
380
381 break;
382
383 default:
384 assert_not_reached("Received unsupported address family");
385 return 0;
386 }
387
388 r = sd_rtnl_message_route_get_dst_prefixlen(message, &dst_prefixlen);
389 if (r < 0) {
390 log_link_warning_errno(link, r, "rtnl: received route with invalid destination prefixlen, ignoring: %m");
391 return 0;
392 }
393
394 r = sd_rtnl_message_route_get_src_prefixlen(message, &src_prefixlen);
395 if (r < 0) {
396 log_link_warning_errno(link, r, "rtnl: received route with invalid source prefixlen, ignoring: %m");
397 return 0;
398 }
399
400 r = sd_rtnl_message_route_get_scope(message, &scope);
401 if (r < 0) {
402 log_link_warning_errno(link, r, "rtnl: received route with invalid scope, ignoring: %m");
403 return 0;
404 }
405
406 r = sd_rtnl_message_route_get_tos(message, &tos);
407 if (r < 0) {
408 log_link_warning_errno(link, r, "rtnl: received route with invalid tos, ignoring: %m");
409 return 0;
410 }
411
412 r = sd_rtnl_message_route_get_type(message, &rt_type);
413 if (r < 0) {
414 log_link_warning_errno(link, r, "rtnl: received route with invalid type, ignoring: %m");
415 return 0;
416 }
417
418 r = sd_rtnl_message_route_get_table(message, &table);
419 if (r < 0) {
420 log_link_warning_errno(link, r, "rtnl: received route with invalid table, ignoring: %m");
421 return 0;
422 }
423
424 r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &priority);
425 if (r < 0 && r != -ENODATA) {
426 log_link_warning_errno(link, r, "rtnl: received route with invalid priority, ignoring: %m");
427 return 0;
428 }
429
430 (void) route_get(link, family, &dst, dst_prefixlen, tos, priority, table, &route);
431
432 if (DEBUG_LOGGING) {
433 _cleanup_free_ char *buf_dst = NULL, *buf_dst_prefixlen = NULL,
434 *buf_src = NULL, *buf_gw = NULL, *buf_prefsrc = NULL;
435
436 if (!in_addr_is_null(family, &dst)) {
437 (void) in_addr_to_string(family, &dst, &buf_dst);
438 (void) asprintf(&buf_dst_prefixlen, "/%u", dst_prefixlen);
439 }
440 if (!in_addr_is_null(family, &src))
441 (void) in_addr_to_string(family, &src, &buf_src);
442 if (!in_addr_is_null(family, &gw))
443 (void) in_addr_to_string(family, &gw, &buf_gw);
444 if (!in_addr_is_null(family, &prefsrc))
445 (void) in_addr_to_string(family, &prefsrc, &buf_prefsrc);
446
447 log_link_debug(link,
448 "%s route: dst: %s%s, src: %s, gw: %s, prefsrc: %s",
449 type == RTM_DELROUTE ? "Removing" : route ? "Updating" : "Adding",
450 strna(buf_dst), strempty(buf_dst_prefixlen),
451 strna(buf_src), strna(buf_gw), strna(buf_prefsrc));
452 }
453
454 switch (type) {
455 case RTM_NEWROUTE:
456 if (!route) {
457 /* A route appeared that we did not request */
458 r = route_add_foreign(link, family, &dst, dst_prefixlen, tos, priority, table, &route);
459 if (r < 0) {
460 log_link_warning_errno(link, r, "Failed to add route, ignoring: %m");
461 return 0;
462 }
463 }
464
465 route_update(route, &src, src_prefixlen, &gw, &prefsrc, scope, protocol, rt_type);
466
467 break;
468
469 case RTM_DELROUTE:
470 route_free(route);
471 break;
472
473 default:
474 assert_not_reached("Received invalid RTNL message type");
475 }
476
477 return 1;
478 }
479
480 int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
481 _cleanup_free_ char *buf = NULL;
482 Manager *m = userdata;
483 Link *link = NULL;
484 uint16_t type;
485 unsigned char flags, prefixlen, scope;
486 union in_addr_union in_addr = IN_ADDR_NULL;
487 struct ifa_cacheinfo cinfo;
488 Address *address = NULL;
489 char valid_buf[FORMAT_TIMESPAN_MAX];
490 const char *valid_str = NULL;
491 int ifindex, family, r;
492
493 assert(rtnl);
494 assert(message);
495 assert(m);
496
497 if (sd_netlink_message_is_error(message)) {
498 r = sd_netlink_message_get_errno(message);
499 if (r < 0)
500 log_warning_errno(r, "rtnl: failed to receive address, ignoring: %m");
501
502 return 0;
503 }
504
505 r = sd_netlink_message_get_type(message, &type);
506 if (r < 0) {
507 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
508 return 0;
509 } else if (!IN_SET(type, RTM_NEWADDR, RTM_DELADDR)) {
510 log_warning("rtnl: received unexpected message type when processing address, ignoring");
511 return 0;
512 }
513
514 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
515 if (r < 0) {
516 log_warning_errno(r, "rtnl: could not get ifindex from address, ignoring: %m");
517 return 0;
518 } else if (ifindex <= 0) {
519 log_warning("rtnl: received address message with invalid ifindex, ignoring: %d", ifindex);
520 return 0;
521 } else {
522 r = link_get(m, ifindex, &link);
523 if (r < 0 || !link) {
524 /* when enumerating we might be out of sync, but we will
525 * get the address again, so just ignore it */
526 if (!m->enumerating)
527 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex);
528 return 0;
529 }
530 }
531
532 r = sd_rtnl_message_addr_get_family(message, &family);
533 if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) {
534 log_link_warning(link, "rtnl: received address with invalid family, ignoring");
535 return 0;
536 }
537
538 r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen);
539 if (r < 0) {
540 log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m");
541 return 0;
542 }
543
544 r = sd_rtnl_message_addr_get_scope(message, &scope);
545 if (r < 0) {
546 log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m");
547 return 0;
548 }
549
550 r = sd_rtnl_message_addr_get_flags(message, &flags);
551 if (r < 0) {
552 log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m");
553 return 0;
554 }
555
556 switch (family) {
557 case AF_INET:
558 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in);
559 if (r < 0) {
560 log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m");
561 return 0;
562 }
563
564 break;
565
566 case AF_INET6:
567 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6);
568 if (r < 0) {
569 log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m");
570 return 0;
571 }
572
573 break;
574
575 default:
576 assert_not_reached("Received unsupported address family");
577 }
578
579 r = in_addr_to_string(family, &in_addr, &buf);
580 if (r < 0) {
581 log_link_warning_errno(link, r, "Could not print address, ignoring: %m");
582 return 0;
583 }
584
585 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
586 if (r < 0 && r != -ENODATA) {
587 log_link_warning_errno(link, r, "rtnl: cannot get IFA_CACHEINFO attribute, ignoring: %m");
588 return 0;
589 } else if (r >= 0 && cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME)
590 valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
591 cinfo.ifa_valid * USEC_PER_SEC,
592 USEC_PER_SEC);
593
594 (void) address_get(link, family, &in_addr, prefixlen, &address);
595
596 switch (type) {
597 case RTM_NEWADDR:
598 if (address)
599 log_link_debug(link, "Updating address: %s/%u (valid %s%s)", buf, prefixlen,
600 valid_str ? "for " : "forever", strempty(valid_str));
601 else {
602 /* An address appeared that we did not request */
603 r = address_add_foreign(link, family, &in_addr, prefixlen, &address);
604 if (r < 0) {
605 log_link_warning_errno(link, r, "Failed to add address %s/%u, ignoring: %m", buf, prefixlen);
606 return 0;
607 } else
608 log_link_debug(link, "Adding address: %s/%u (valid %s%s)", buf, prefixlen,
609 valid_str ? "for " : "forever", strempty(valid_str));
610 }
611
612 r = address_update(address, flags, scope, &cinfo);
613 if (r < 0) {
614 log_link_warning_errno(link, r, "Failed to update address %s/%u, ignoring: %m", buf, prefixlen);
615 return 0;
616 }
617
618 break;
619
620 case RTM_DELADDR:
621
622 if (address) {
623 log_link_debug(link, "Removing address: %s/%u (valid %s%s)", buf, prefixlen,
624 valid_str ? "for " : "forever", strempty(valid_str));
625 (void) address_drop(address);
626 } else
627 log_link_warning(link, "Removing non-existent address: %s/%u (valid %s%s), ignoring", buf, prefixlen,
628 valid_str ? "for " : "forever", strempty(valid_str));
629
630 break;
631 default:
632 assert_not_reached("Received invalid RTNL message type");
633 }
634
635 return 1;
636 }
637
638 static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
639 Manager *m = userdata;
640 Link *link = NULL;
641 NetDev *netdev = NULL;
642 uint16_t type;
643 const char *name;
644 int r, ifindex;
645
646 assert(rtnl);
647 assert(message);
648 assert(m);
649
650 if (sd_netlink_message_is_error(message)) {
651 r = sd_netlink_message_get_errno(message);
652 if (r < 0)
653 log_warning_errno(r, "rtnl: Could not receive link, ignoring: %m");
654
655 return 0;
656 }
657
658 r = sd_netlink_message_get_type(message, &type);
659 if (r < 0) {
660 log_warning_errno(r, "rtnl: Could not get message type, ignoring: %m");
661 return 0;
662 } else if (!IN_SET(type, RTM_NEWLINK, RTM_DELLINK)) {
663 log_warning("rtnl: Received unexpected message type when processing link, ignoring");
664 return 0;
665 }
666
667 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
668 if (r < 0) {
669 log_warning_errno(r, "rtnl: Could not get ifindex from link, ignoring: %m");
670 return 0;
671 } else if (ifindex <= 0) {
672 log_warning("rtnl: received link message with invalid ifindex %d, ignoring", ifindex);
673 return 0;
674 }
675
676 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
677 if (r < 0) {
678 log_warning_errno(r, "rtnl: Received link message without ifname, ignoring: %m");
679 return 0;
680 }
681
682 (void) link_get(m, ifindex, &link);
683 (void) netdev_get(m, name, &netdev);
684
685 switch (type) {
686 case RTM_NEWLINK:
687 if (!link) {
688 /* link is new, so add it */
689 r = link_add(m, message, &link);
690 if (r < 0) {
691 log_warning_errno(r, "Could not add new link, ignoring: %m");
692 return 0;
693 }
694 }
695
696 if (netdev) {
697 /* netdev exists, so make sure the ifindex matches */
698 r = netdev_set_ifindex(netdev, message);
699 if (r < 0) {
700 log_warning_errno(r, "Could not set ifindex on netdev, ignoring: %m");
701 return 0;
702 }
703 }
704
705 r = link_update(link, message);
706 if (r < 0) {
707 log_warning_errno(r, "Could not update link, ignoring: %m");
708 return 0;
709 }
710
711 break;
712
713 case RTM_DELLINK:
714 link_drop(link);
715 netdev_drop(netdev);
716
717 break;
718
719 default:
720 assert_not_reached("Received invalid RTNL message type.");
721 }
722
723 return 1;
724 }
725
726 int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
727 uint8_t tos = 0, to_prefixlen = 0, from_prefixlen = 0, protocol = 0;
728 struct fib_rule_port_range sport = {}, dport = {};
729 union in_addr_union to = IN_ADDR_NULL, from = IN_ADDR_NULL;
730 RoutingPolicyRule *rule = NULL;
731 uint32_t fwmark = 0, table = 0;
732 const char *iif = NULL, *oif = NULL;
733 Manager *m = userdata;
734 uint16_t type;
735 int family;
736 int r;
737
738 assert(rtnl);
739 assert(message);
740 assert(m);
741
742 if (sd_netlink_message_is_error(message)) {
743 r = sd_netlink_message_get_errno(message);
744 if (r < 0)
745 log_warning_errno(r, "rtnl: failed to receive rule, ignoring: %m");
746
747 return 0;
748 }
749
750 r = sd_netlink_message_get_type(message, &type);
751 if (r < 0) {
752 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
753 return 0;
754 } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) {
755 log_warning("rtnl: received unexpected message type '%u' when processing rule, ignoring", type);
756 return 0;
757 }
758
759 r = sd_rtnl_message_get_family(message, &family);
760 if (r < 0) {
761 log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m");
762 return 0;
763 } else if (!IN_SET(family, AF_INET, AF_INET6)) {
764 log_debug("rtnl: received address with invalid family %u, ignoring", family);
765 return 0;
766 }
767
768 switch (family) {
769 case AF_INET:
770 r = sd_netlink_message_read_in_addr(message, FRA_SRC, &from.in);
771 if (r < 0 && r != -ENODATA) {
772 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
773 return 0;
774 } else if (r >= 0) {
775 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &from_prefixlen);
776 if (r < 0) {
777 log_warning_errno(r, "rtnl: failed to retrieve rule from prefix length, ignoring: %m");
778 return 0;
779 }
780 }
781
782 r = sd_netlink_message_read_in_addr(message, FRA_DST, &to.in);
783 if (r < 0 && r != -ENODATA) {
784 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
785 return 0;
786 } else if (r >= 0) {
787 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &to_prefixlen);
788 if (r < 0) {
789 log_warning_errno(r, "rtnl: failed to retrieve rule to prefix length, ignoring: %m");
790 return 0;
791 }
792 }
793
794 break;
795
796 case AF_INET6:
797 r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &from.in6);
798 if (r < 0 && r != -ENODATA) {
799 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
800 return 0;
801 } else if (r >= 0) {
802 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &from_prefixlen);
803 if (r < 0) {
804 log_warning_errno(r, "rtnl: failed to retrieve rule from prefix length, ignoring: %m");
805 return 0;
806 }
807 }
808
809 r = sd_netlink_message_read_in6_addr(message, FRA_DST, &to.in6);
810 if (r < 0 && r != -ENODATA) {
811 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
812 return 0;
813 } else if (r >= 0) {
814 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &to_prefixlen);
815 if (r < 0) {
816 log_warning_errno(r, "rtnl: failed to retrieve rule to prefix length, ignoring: %m");
817 return 0;
818 }
819 }
820
821 break;
822
823 default:
824 assert_not_reached("Received unsupported address family");
825 }
826
827 if (from_prefixlen == 0 && to_prefixlen == 0)
828 return 0;
829
830 r = sd_netlink_message_read_u32(message, FRA_FWMARK, &fwmark);
831 if (r < 0 && r != -ENODATA) {
832 log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m");
833 return 0;
834 }
835
836 r = sd_netlink_message_read_u32(message, FRA_TABLE, &table);
837 if (r < 0 && r != -ENODATA) {
838 log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m");
839 return 0;
840 }
841
842 r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tos);
843 if (r < 0 && r != -ENODATA) {
844 log_warning_errno(r, "rtnl: could not get ip rule TOS, ignoring: %m");
845 return 0;
846 }
847
848 r = sd_netlink_message_read_string(message, FRA_IIFNAME, &iif);
849 if (r < 0 && r != -ENODATA) {
850 log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m");
851 return 0;
852 }
853
854 r = sd_netlink_message_read_string(message, FRA_OIFNAME, &oif);
855 if (r < 0 && r != -ENODATA) {
856 log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m");
857 return 0;
858 }
859
860 r = sd_netlink_message_read_u8(message, FRA_IP_PROTO, &protocol);
861 if (r < 0 && r != -ENODATA) {
862 log_warning_errno(r, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m");
863 return 0;
864 }
865
866 r = sd_netlink_message_read(message, FRA_SPORT_RANGE, sizeof(sport), (void *) &sport);
867 if (r < 0 && r != -ENODATA) {
868 log_warning_errno(r, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m");
869 return 0;
870 }
871
872 r = sd_netlink_message_read(message, FRA_DPORT_RANGE, sizeof(dport), (void *) &dport);
873 if (r < 0 && r != -ENODATA) {
874 log_warning_errno(r, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m");
875 return 0;
876 }
877
878 (void) routing_policy_rule_get(m, family, &from, from_prefixlen, &to, to_prefixlen, tos, fwmark, table, iif, oif, protocol, &sport, &dport, &rule);
879
880 switch (type) {
881 case RTM_NEWRULE:
882 if (!rule) {
883 r = routing_policy_rule_add_foreign(m, family, &from, from_prefixlen, &to, to_prefixlen, tos, fwmark, table, iif, oif, protocol, &sport, &dport, &rule);
884 if (r < 0) {
885 log_warning_errno(r, "Could not add rule, ignoring: %m");
886 return 0;
887 }
888 }
889 break;
890 case RTM_DELRULE:
891 routing_policy_rule_free(rule);
892
893 break;
894
895 default:
896 assert_not_reached("Received invalid RTNL message type");
897 }
898
899 return 1;
900 }
901
902 static int systemd_netlink_fd(void) {
903 int n, fd, rtnl_fd = -EINVAL;
904
905 n = sd_listen_fds(true);
906 if (n <= 0)
907 return -EINVAL;
908
909 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
910 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
911 if (rtnl_fd >= 0)
912 return -EINVAL;
913
914 rtnl_fd = fd;
915 }
916 }
917
918 return rtnl_fd;
919 }
920
921 static int manager_connect_genl(Manager *m) {
922 int r;
923
924 assert(m);
925
926 r = sd_genl_socket_open(&m->genl);
927 if (r < 0)
928 return r;
929
930 r = sd_netlink_inc_rcvbuf(m->genl, RCVBUF_SIZE);
931 if (r < 0)
932 return r;
933
934 r = sd_netlink_attach_event(m->genl, m->event, 0);
935 if (r < 0)
936 return r;
937
938 return 0;
939 }
940
941 static int manager_connect_rtnl(Manager *m) {
942 int fd, r;
943
944 assert(m);
945
946 fd = systemd_netlink_fd();
947 if (fd < 0)
948 r = sd_netlink_open(&m->rtnl);
949 else
950 r = sd_netlink_open_fd(&m->rtnl, fd);
951 if (r < 0)
952 return r;
953
954 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
955 if (r < 0)
956 return r;
957
958 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
959 if (r < 0)
960 return r;
961
962 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
963 if (r < 0)
964 return r;
965
966 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
967 if (r < 0)
968 return r;
969
970 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
971 if (r < 0)
972 return r;
973
974 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
975 if (r < 0)
976 return r;
977
978 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
979 if (r < 0)
980 return r;
981
982 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
983 if (r < 0)
984 return r;
985
986 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
987 if (r < 0)
988 return r;
989
990 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
991 if (r < 0)
992 return r;
993
994 return 0;
995 }
996
997 static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) {
998 char *p;
999 int r;
1000
1001 assert(s);
1002 assert(address);
1003
1004 r = in_addr_to_string(address->family, &address->address, &p);
1005 if (r < 0)
1006 return r;
1007
1008 r = ordered_set_consume(s, p);
1009 if (r == -EEXIST)
1010 return 0;
1011
1012 return r;
1013 }
1014
1015 static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) {
1016 int r, c = 0;
1017 unsigned i;
1018
1019 assert(s);
1020 assert(addresses || n == 0);
1021
1022 for (i = 0; i < n; i++) {
1023 r = ordered_set_put_in_addr_data(s, addresses+i);
1024 if (r < 0)
1025 return r;
1026
1027 c += r;
1028 }
1029
1030 return c;
1031 }
1032
1033 static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) {
1034 char *p;
1035 int r;
1036
1037 assert(s);
1038 assert(address);
1039
1040 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
1041 if (r < 0)
1042 return r;
1043
1044 r = ordered_set_consume(s, p);
1045 if (r == -EEXIST)
1046 return 0;
1047
1048 return r;
1049 }
1050
1051 static int ordered_set_put_in4_addrv(OrderedSet *s,
1052 const struct in_addr *addresses,
1053 size_t n,
1054 bool (*predicate)(const struct in_addr *addr)) {
1055 int r, c = 0;
1056 size_t i;
1057
1058 assert(s);
1059 assert(n == 0 || addresses);
1060
1061 for (i = 0; i < n; i++) {
1062 if (predicate && !predicate(&addresses[i]))
1063 continue;
1064 r = ordered_set_put_in4_addr(s, addresses+i);
1065 if (r < 0)
1066 return r;
1067
1068 c += r;
1069 }
1070
1071 return c;
1072 }
1073
1074 static int manager_save(Manager *m) {
1075 _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL;
1076 Link *link;
1077 Iterator i;
1078 _cleanup_free_ char *temp_path = NULL;
1079 _cleanup_fclose_ FILE *f = NULL;
1080 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
1081 const char *operstate_str;
1082 int r;
1083
1084 assert(m);
1085 assert(m->state_file);
1086
1087 /* We add all NTP and DNS server to a set, to filter out duplicates */
1088 dns = ordered_set_new(&string_hash_ops);
1089 if (!dns)
1090 return -ENOMEM;
1091
1092 ntp = ordered_set_new(&string_hash_ops);
1093 if (!ntp)
1094 return -ENOMEM;
1095
1096 search_domains = ordered_set_new(&dns_name_hash_ops);
1097 if (!search_domains)
1098 return -ENOMEM;
1099
1100 route_domains = ordered_set_new(&dns_name_hash_ops);
1101 if (!route_domains)
1102 return -ENOMEM;
1103
1104 HASHMAP_FOREACH(link, m->links, i) {
1105 if (link->flags & IFF_LOOPBACK)
1106 continue;
1107
1108 if (link->operstate > operstate)
1109 operstate = link->operstate;
1110
1111 if (!link->network)
1112 continue;
1113
1114 /* First add the static configured entries */
1115 r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns);
1116 if (r < 0)
1117 return r;
1118
1119 r = ordered_set_put_strdupv(ntp, link->network->ntp);
1120 if (r < 0)
1121 return r;
1122
1123 r = ordered_set_put_string_set(search_domains, link->network->search_domains);
1124 if (r < 0)
1125 return r;
1126
1127 r = ordered_set_put_string_set(route_domains, link->network->route_domains);
1128 if (r < 0)
1129 return r;
1130
1131 if (!link->dhcp_lease)
1132 continue;
1133
1134 /* Secondly, add the entries acquired via DHCP */
1135 if (link->network->dhcp_use_dns) {
1136 const struct in_addr *addresses;
1137
1138 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
1139 if (r > 0) {
1140 r = ordered_set_put_in4_addrv(dns, addresses, r, in4_addr_is_non_local);
1141 if (r < 0)
1142 return r;
1143 } else if (r < 0 && r != -ENODATA)
1144 return r;
1145 }
1146
1147 if (link->network->dhcp_use_ntp) {
1148 const struct in_addr *addresses;
1149
1150 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
1151 if (r > 0) {
1152 r = ordered_set_put_in4_addrv(ntp, addresses, r, in4_addr_is_non_local);
1153 if (r < 0)
1154 return r;
1155 } else if (r < 0 && r != -ENODATA)
1156 return r;
1157 }
1158
1159 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
1160 const char *domainname;
1161 char **domains = NULL;
1162
1163 OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains;
1164 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
1165 if (r >= 0) {
1166 r = ordered_set_put_strdup(target_domains, domainname);
1167 if (r < 0)
1168 return r;
1169 } else if (r != -ENODATA)
1170 return r;
1171
1172 r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains);
1173 if (r >= 0) {
1174 r = ordered_set_put_strdupv(target_domains, domains);
1175 if (r < 0)
1176 return r;
1177 } else if (r != -ENODATA)
1178 return r;
1179 }
1180 }
1181
1182 operstate_str = link_operstate_to_string(operstate);
1183 assert(operstate_str);
1184
1185 r = fopen_temporary(m->state_file, &f, &temp_path);
1186 if (r < 0)
1187 return r;
1188
1189 (void) fchmod(fileno(f), 0644);
1190
1191 fprintf(f,
1192 "# This is private data. Do not parse.\n"
1193 "OPER_STATE=%s\n", operstate_str);
1194
1195 ordered_set_print(f, "DNS=", dns);
1196 ordered_set_print(f, "NTP=", ntp);
1197 ordered_set_print(f, "DOMAINS=", search_domains);
1198 ordered_set_print(f, "ROUTE_DOMAINS=", route_domains);
1199
1200 r = routing_policy_serialize_rules(m->rules, f);
1201 if (r < 0)
1202 goto fail;
1203
1204 r = fflush_and_check(f);
1205 if (r < 0)
1206 goto fail;
1207
1208 if (rename(temp_path, m->state_file) < 0) {
1209 r = -errno;
1210 goto fail;
1211 }
1212
1213 if (m->operational_state != operstate) {
1214 m->operational_state = operstate;
1215 r = manager_send_changed(m, "OperationalState", NULL);
1216 if (r < 0)
1217 log_error_errno(r, "Could not emit changed OperationalState: %m");
1218 }
1219
1220 m->dirty = false;
1221
1222 return 0;
1223
1224 fail:
1225 (void) unlink(m->state_file);
1226 (void) unlink(temp_path);
1227
1228 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
1229 }
1230
1231 static int manager_dirty_handler(sd_event_source *s, void *userdata) {
1232 Manager *m = userdata;
1233 Link *link;
1234 Iterator i;
1235
1236 assert(m);
1237
1238 if (m->dirty)
1239 manager_save(m);
1240
1241 SET_FOREACH(link, m->dirty_links, i)
1242 if (link_save(link) >= 0)
1243 link_clean(link);
1244
1245 return 1;
1246 }
1247
1248 Link *manager_dhcp6_prefix_get(Manager *m, struct in6_addr *addr) {
1249 assert_return(m, NULL);
1250 assert_return(addr, NULL);
1251
1252 return hashmap_get(m->dhcp6_prefixes, addr);
1253 }
1254
1255 static int dhcp6_route_add_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
1256 int r;
1257
1258 assert(link);
1259
1260 r = sd_netlink_message_get_errno(m);
1261 if (r < 0 && r != -EEXIST)
1262 log_link_debug_errno(link, r, "Received error adding DHCPv6 Prefix Delegation route: %m");
1263
1264 return 0;
1265 }
1266
1267 int manager_dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) {
1268 _cleanup_free_ struct in6_addr *a = NULL;
1269 _cleanup_free_ char *buf = NULL;
1270 Link *assigned_link;
1271 Route *route;
1272 int r;
1273
1274 assert_return(m, -EINVAL);
1275 assert_return(addr, -EINVAL);
1276
1277 r = route_add(link, AF_INET6, (union in_addr_union *) addr, 64,
1278 0, 0, 0, &route);
1279 if (r < 0)
1280 return r;
1281
1282 r = route_configure(route, link, dhcp6_route_add_handler);
1283 if (r < 0)
1284 return r;
1285
1286 (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
1287 log_link_debug(link, "Adding prefix route %s/64", strnull(buf));
1288
1289 assigned_link = hashmap_get(m->dhcp6_prefixes, addr);
1290 if (assigned_link) {
1291 assert(assigned_link == link);
1292 return 0;
1293 }
1294
1295 a = newdup(struct in6_addr, addr, 1);
1296 if (!a)
1297 return -ENOMEM;
1298
1299 r = hashmap_ensure_allocated(&m->dhcp6_prefixes, &in6_addr_hash_ops);
1300 if (r < 0)
1301 return r;
1302
1303 r = hashmap_put(m->dhcp6_prefixes, a, link);
1304 if (r < 0)
1305 return r;
1306
1307 TAKE_PTR(a);
1308 link_ref(link);
1309 return 0;
1310 }
1311
1312 static int dhcp6_route_remove_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
1313 int r;
1314
1315 assert(link);
1316
1317 r = sd_netlink_message_get_errno(m);
1318 if (r < 0)
1319 log_link_debug_errno(link, r, "Received error on DHCPv6 Prefix Delegation route removal: %m");
1320
1321 return 1;
1322 }
1323
1324 static int manager_dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) {
1325 _cleanup_free_ struct in6_addr *a = NULL;
1326 _cleanup_(link_unrefp) Link *l = NULL;
1327 _cleanup_free_ char *buf = NULL;
1328 Route *route;
1329 int r;
1330
1331 assert_return(m, -EINVAL);
1332 assert_return(addr, -EINVAL);
1333
1334 l = hashmap_remove2(m->dhcp6_prefixes, addr, (void **) &a);
1335 if (!l)
1336 return -EINVAL;
1337
1338 (void) sd_radv_remove_prefix(l->radv, addr, 64);
1339 r = route_get(l, AF_INET6, (union in_addr_union *) addr, 64, 0, 0, 0, &route);
1340 if (r < 0)
1341 return r;
1342
1343 r = route_remove(route, l, dhcp6_route_remove_handler);
1344 if (r < 0)
1345 return r;
1346
1347 (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
1348 log_link_debug(l, "Removing prefix route %s/64", strnull(buf));
1349
1350 return 0;
1351 }
1352
1353 int manager_dhcp6_prefix_remove_all(Manager *m, Link *link) {
1354 struct in6_addr *addr;
1355 Iterator i;
1356 Link *l;
1357
1358 assert_return(m, -EINVAL);
1359 assert_return(link, -EINVAL);
1360
1361 HASHMAP_FOREACH_KEY(l, addr, m->dhcp6_prefixes, i)
1362 if (l == link)
1363 (void) manager_dhcp6_prefix_remove(m, addr);
1364
1365 return 0;
1366 }
1367
1368 int manager_new(Manager **ret) {
1369 _cleanup_(manager_freep) Manager *m = NULL;
1370 int r;
1371
1372 m = new0(Manager, 1);
1373 if (!m)
1374 return -ENOMEM;
1375
1376 m->state_file = strdup("/run/systemd/netif/state");
1377 if (!m->state_file)
1378 return -ENOMEM;
1379
1380 m->sysctl_ipv6_enabled = -1;
1381
1382 r = sd_event_default(&m->event);
1383 if (r < 0)
1384 return r;
1385
1386 (void) sd_event_set_watchdog(m->event, true);
1387 (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
1388 (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
1389
1390 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
1391 if (r < 0)
1392 return r;
1393
1394 r = manager_connect_rtnl(m);
1395 if (r < 0)
1396 return r;
1397
1398 r = manager_connect_genl(m);
1399 if (r < 0)
1400 return r;
1401
1402 r = manager_connect_udev(m);
1403 if (r < 0)
1404 return r;
1405
1406 r = sd_resolve_default(&m->resolve);
1407 if (r < 0)
1408 return r;
1409
1410 r = sd_resolve_attach_event(m->resolve, m->event, 0);
1411 if (r < 0)
1412 return r;
1413
1414 r = setup_default_address_pool(m);
1415 if (r < 0)
1416 return r;
1417
1418 m->duid.type = DUID_TYPE_EN;
1419
1420 (void) routing_policy_load_rules(m->state_file, &m->rules_saved);
1421
1422 *ret = TAKE_PTR(m);
1423
1424 return 0;
1425 }
1426
1427 void manager_free(Manager *m) {
1428 struct in6_addr *a;
1429 AddressPool *pool;
1430 Link *link;
1431
1432 if (!m)
1433 return;
1434
1435 free(m->state_file);
1436
1437 while ((a = hashmap_first_key(m->dhcp6_prefixes)))
1438 (void) manager_dhcp6_prefix_remove(m, a);
1439 hashmap_free(m->dhcp6_prefixes);
1440
1441 while ((link = hashmap_steal_first(m->links))) {
1442 if (link->dhcp6_client)
1443 (void) dhcp6_lease_pd_prefix_lost(link->dhcp6_client, link);
1444
1445 link_stop_clients(link);
1446
1447 link_unref(link);
1448 }
1449
1450 m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref);
1451 m->links_requesting_uuid = set_free_with_destructor(m->links_requesting_uuid, link_unref);
1452 m->links = hashmap_free_with_destructor(m->links, link_unref);
1453
1454 m->duids_requesting_uuid = set_free(m->duids_requesting_uuid);
1455 m->networks = ordered_hashmap_free_with_destructor(m->networks, network_unref);
1456
1457 m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref);
1458
1459 while ((pool = m->address_pools))
1460 address_pool_free(pool);
1461
1462 /* routing_policy_rule_free() access m->rules and m->rules_foreign.
1463 * So, it is necessary to set NULL after the sets are freed. */
1464 m->rules = set_free_with_destructor(m->rules, routing_policy_rule_free);
1465 m->rules_foreign = set_free_with_destructor(m->rules_foreign, routing_policy_rule_free);
1466 set_free_with_destructor(m->rules_saved, routing_policy_rule_free);
1467
1468 sd_netlink_unref(m->rtnl);
1469 sd_netlink_unref(m->genl);
1470 sd_resolve_unref(m->resolve);
1471
1472 sd_event_unref(m->event);
1473
1474 sd_device_monitor_unref(m->device_monitor);
1475
1476 sd_bus_flush_close_unref(m->bus);
1477
1478 free(m->dynamic_timezone);
1479 free(m->dynamic_hostname);
1480
1481 free(m);
1482 }
1483
1484 int manager_start(Manager *m) {
1485 Link *link;
1486 Iterator i;
1487
1488 assert(m);
1489
1490 /* The dirty handler will deal with future serialization, but the first one
1491 must be done explicitly. */
1492
1493 manager_save(m);
1494
1495 HASHMAP_FOREACH(link, m->links, i)
1496 link_save(link);
1497
1498 return 0;
1499 }
1500
1501 int manager_load_config(Manager *m) {
1502 int r;
1503
1504 /* update timestamp */
1505 paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, true);
1506
1507 r = netdev_load(m);
1508 if (r < 0)
1509 return r;
1510
1511 r = network_load(m);
1512 if (r < 0)
1513 return r;
1514
1515 return 0;
1516 }
1517
1518 bool manager_should_reload(Manager *m) {
1519 return paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, false);
1520 }
1521
1522 int manager_rtnl_enumerate_links(Manager *m) {
1523 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1524 sd_netlink_message *link;
1525 int r;
1526
1527 assert(m);
1528 assert(m->rtnl);
1529
1530 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
1531 if (r < 0)
1532 return r;
1533
1534 r = sd_netlink_message_request_dump(req, true);
1535 if (r < 0)
1536 return r;
1537
1538 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1539 if (r < 0)
1540 return r;
1541
1542 for (link = reply; link; link = sd_netlink_message_next(link)) {
1543 int k;
1544
1545 m->enumerating = true;
1546
1547 k = manager_rtnl_process_link(m->rtnl, link, m);
1548 if (k < 0)
1549 r = k;
1550
1551 m->enumerating = false;
1552 }
1553
1554 return r;
1555 }
1556
1557 int manager_rtnl_enumerate_addresses(Manager *m) {
1558 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1559 sd_netlink_message *addr;
1560 int r;
1561
1562 assert(m);
1563 assert(m->rtnl);
1564
1565 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
1566 if (r < 0)
1567 return r;
1568
1569 r = sd_netlink_message_request_dump(req, true);
1570 if (r < 0)
1571 return r;
1572
1573 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1574 if (r < 0)
1575 return r;
1576
1577 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
1578 int k;
1579
1580 m->enumerating = true;
1581
1582 k = manager_rtnl_process_address(m->rtnl, addr, m);
1583 if (k < 0)
1584 r = k;
1585
1586 m->enumerating = false;
1587 }
1588
1589 return r;
1590 }
1591
1592 int manager_rtnl_enumerate_routes(Manager *m) {
1593 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1594 sd_netlink_message *route;
1595 int r;
1596
1597 assert(m);
1598 assert(m->rtnl);
1599
1600 r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0);
1601 if (r < 0)
1602 return r;
1603
1604 r = sd_netlink_message_request_dump(req, true);
1605 if (r < 0)
1606 return r;
1607
1608 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1609 if (r < 0)
1610 return r;
1611
1612 for (route = reply; route; route = sd_netlink_message_next(route)) {
1613 int k;
1614
1615 m->enumerating = true;
1616
1617 k = manager_rtnl_process_route(m->rtnl, route, m);
1618 if (k < 0)
1619 r = k;
1620
1621 m->enumerating = false;
1622 }
1623
1624 return r;
1625 }
1626
1627 int manager_rtnl_enumerate_rules(Manager *m) {
1628 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1629 sd_netlink_message *rule;
1630 int r;
1631
1632 assert(m);
1633 assert(m->rtnl);
1634
1635 r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0);
1636 if (r < 0)
1637 return r;
1638
1639 r = sd_netlink_message_request_dump(req, true);
1640 if (r < 0)
1641 return r;
1642
1643 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1644 if (r < 0) {
1645 if (r == -EOPNOTSUPP) {
1646 log_debug("FIB Rules are not supported by the kernel. Ignoring.");
1647 return 0;
1648 }
1649
1650 return r;
1651 }
1652
1653 for (rule = reply; rule; rule = sd_netlink_message_next(rule)) {
1654 int k;
1655
1656 m->enumerating = true;
1657
1658 k = manager_rtnl_process_rule(m->rtnl, rule, m);
1659 if (k < 0)
1660 r = k;
1661
1662 m->enumerating = false;
1663 }
1664
1665 return r;
1666 }
1667
1668 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
1669 AddressPool *p;
1670 int r;
1671
1672 assert(m);
1673 assert(prefixlen > 0);
1674 assert(found);
1675
1676 LIST_FOREACH(address_pools, p, m->address_pools) {
1677 if (p->family != family)
1678 continue;
1679
1680 r = address_pool_acquire(p, prefixlen, found);
1681 if (r != 0)
1682 return r;
1683 }
1684
1685 return 0;
1686 }
1687
1688 Link* manager_find_uplink(Manager *m, Link *exclude) {
1689 _cleanup_free_ struct local_address *gateways = NULL;
1690 int n, i;
1691
1692 assert(m);
1693
1694 /* Looks for a suitable "uplink", via black magic: an
1695 * interface that is up and where the default route with the
1696 * highest priority points to. */
1697
1698 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
1699 if (n < 0) {
1700 log_warning_errno(n, "Failed to determine list of default gateways: %m");
1701 return NULL;
1702 }
1703
1704 for (i = 0; i < n; i++) {
1705 Link *link;
1706
1707 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
1708 if (!link) {
1709 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
1710 continue;
1711 }
1712
1713 if (link == exclude)
1714 continue;
1715
1716 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
1717 continue;
1718
1719 return link;
1720 }
1721
1722 return NULL;
1723 }
1724
1725 void manager_dirty(Manager *manager) {
1726 assert(manager);
1727
1728 /* the serialized state in /run is no longer up-to-date */
1729 manager->dirty = true;
1730 }
1731
1732 static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
1733 Manager *manager = userdata;
1734 const sd_bus_error *e;
1735
1736 assert(m);
1737 assert(manager);
1738
1739 e = sd_bus_message_get_error(m);
1740 if (e)
1741 log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message);
1742
1743 return 1;
1744 }
1745
1746 int manager_set_hostname(Manager *m, const char *hostname) {
1747 int r;
1748
1749 log_debug("Setting transient hostname: '%s'", strna(hostname));
1750
1751 if (free_and_strdup(&m->dynamic_hostname, hostname) < 0)
1752 return log_oom();
1753
1754 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
1755 log_debug("Not connected to system bus, setting hostname later.");
1756 return 0;
1757 }
1758
1759 r = sd_bus_call_method_async(
1760 m->bus,
1761 NULL,
1762 "org.freedesktop.hostname1",
1763 "/org/freedesktop/hostname1",
1764 "org.freedesktop.hostname1",
1765 "SetHostname",
1766 set_hostname_handler,
1767 m,
1768 "sb",
1769 hostname,
1770 false);
1771
1772 if (r < 0)
1773 return log_error_errno(r, "Could not set transient hostname: %m");
1774
1775 return 0;
1776 }
1777
1778 static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
1779 Manager *manager = userdata;
1780 const sd_bus_error *e;
1781
1782 assert(m);
1783 assert(manager);
1784
1785 e = sd_bus_message_get_error(m);
1786 if (e)
1787 log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message);
1788
1789 return 1;
1790 }
1791
1792 int manager_set_timezone(Manager *m, const char *tz) {
1793 int r;
1794
1795 assert(m);
1796 assert(tz);
1797
1798 log_debug("Setting system timezone: '%s'", tz);
1799 if (free_and_strdup(&m->dynamic_timezone, tz) < 0)
1800 return log_oom();
1801
1802 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
1803 log_debug("Not connected to system bus, setting timezone later.");
1804 return 0;
1805 }
1806
1807 r = sd_bus_call_method_async(
1808 m->bus,
1809 NULL,
1810 "org.freedesktop.timedate1",
1811 "/org/freedesktop/timedate1",
1812 "org.freedesktop.timedate1",
1813 "SetTimezone",
1814 set_timezone_handler,
1815 m,
1816 "sb",
1817 tz,
1818 false);
1819 if (r < 0)
1820 return log_error_errno(r, "Could not set timezone: %m");
1821
1822 return 0;
1823 }
1824
1825 int manager_request_product_uuid(Manager *m, Link *link) {
1826 int r;
1827
1828 assert(m);
1829
1830 if (m->has_product_uuid)
1831 return 0;
1832
1833 log_debug("Requesting product UUID");
1834
1835 if (link) {
1836 DUID *duid;
1837
1838 assert_se(duid = link_get_duid(link));
1839
1840 r = set_ensure_allocated(&m->links_requesting_uuid, NULL);
1841 if (r < 0)
1842 return log_oom();
1843
1844 r = set_ensure_allocated(&m->duids_requesting_uuid, NULL);
1845 if (r < 0)
1846 return log_oom();
1847
1848 r = set_put(m->links_requesting_uuid, link);
1849 if (r < 0)
1850 return log_oom();
1851
1852 r = set_put(m->duids_requesting_uuid, duid);
1853 if (r < 0)
1854 return log_oom();
1855 }
1856
1857 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
1858 log_debug("Not connected to system bus, requesting product UUID later.");
1859 return 0;
1860 }
1861
1862 r = sd_bus_call_method_async(
1863 m->bus,
1864 NULL,
1865 "org.freedesktop.hostname1",
1866 "/org/freedesktop/hostname1",
1867 "org.freedesktop.hostname1",
1868 "GetProductUUID",
1869 get_product_uuid_handler,
1870 m,
1871 "b",
1872 false);
1873 if (r < 0)
1874 return log_warning_errno(r, "Failed to get product UUID: %m");
1875
1876 return 0;
1877 }
1878
1879 int manager_sysctl_ipv6_enabled(Manager *manager) {
1880 _cleanup_free_ char *value = NULL;
1881 int r;
1882
1883 if (manager->sysctl_ipv6_enabled >= 0)
1884 return manager->sysctl_ipv6_enabled;
1885
1886 r = sysctl_read_ip_property(AF_INET6, "all", "disable_ipv6", &value);
1887 if (r < 0)
1888 return log_warning_errno(r, "Failed to read net.ipv6.conf.all.disable_ipv6 sysctl property: %m");
1889
1890 manager->sysctl_ipv6_enabled = value[0] == '0';
1891 return manager->sysctl_ipv6_enabled;
1892 }