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