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