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