]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-manager.c
Merge pull request #12828 from yuwata/network-routing-policy-rule-add-missing-entries
[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 _cleanup_(routing_policy_rule_freep) RoutingPolicyRule *tmp = NULL;
729 RoutingPolicyRule *rule = NULL;
730 const char *iif = NULL, *oif = NULL;
731 Manager *m = userdata;
732 unsigned flags;
733 uint16_t type;
734 int r;
735
736 assert(rtnl);
737 assert(message);
738 assert(m);
739
740 if (sd_netlink_message_is_error(message)) {
741 r = sd_netlink_message_get_errno(message);
742 if (r < 0)
743 log_warning_errno(r, "rtnl: failed to receive rule, ignoring: %m");
744
745 return 0;
746 }
747
748 r = sd_netlink_message_get_type(message, &type);
749 if (r < 0) {
750 log_warning_errno(r, "rtnl: could not get message type, ignoring: %m");
751 return 0;
752 } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) {
753 log_warning("rtnl: received unexpected message type '%u' when processing rule, ignoring", type);
754 return 0;
755 }
756
757 r = routing_policy_rule_new(&tmp);
758 if (r < 0) {
759 log_oom();
760 return 0;
761 }
762
763 r = sd_rtnl_message_get_family(message, &tmp->family);
764 if (r < 0) {
765 log_warning_errno(r, "rtnl: could not get rule family, ignoring: %m");
766 return 0;
767 } else if (!IN_SET(tmp->family, AF_INET, AF_INET6)) {
768 log_debug("rtnl: received address with invalid family %u, ignoring", tmp->family);
769 return 0;
770 }
771
772 switch (tmp->family) {
773 case AF_INET:
774 r = sd_netlink_message_read_in_addr(message, FRA_SRC, &tmp->from.in);
775 if (r < 0 && r != -ENODATA) {
776 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
777 return 0;
778 } else if (r >= 0) {
779 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen);
780 if (r < 0) {
781 log_warning_errno(r, "rtnl: failed to retrieve rule from prefix length, ignoring: %m");
782 return 0;
783 }
784 }
785
786 r = sd_netlink_message_read_in_addr(message, FRA_DST, &tmp->to.in);
787 if (r < 0 && r != -ENODATA) {
788 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
789 return 0;
790 } else if (r >= 0) {
791 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen);
792 if (r < 0) {
793 log_warning_errno(r, "rtnl: failed to retrieve rule to prefix length, ignoring: %m");
794 return 0;
795 }
796 }
797
798 break;
799
800 case AF_INET6:
801 r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &tmp->from.in6);
802 if (r < 0 && r != -ENODATA) {
803 log_warning_errno(r, "rtnl: could not get FRA_SRC attribute, ignoring: %m");
804 return 0;
805 } else if (r >= 0) {
806 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &tmp->from_prefixlen);
807 if (r < 0) {
808 log_warning_errno(r, "rtnl: failed to retrieve rule from prefix length, ignoring: %m");
809 return 0;
810 }
811 }
812
813 r = sd_netlink_message_read_in6_addr(message, FRA_DST, &tmp->to.in6);
814 if (r < 0 && r != -ENODATA) {
815 log_warning_errno(r, "rtnl: could not get FRA_DST attribute, ignoring: %m");
816 return 0;
817 } else if (r >= 0) {
818 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &tmp->to_prefixlen);
819 if (r < 0) {
820 log_warning_errno(r, "rtnl: failed to retrieve rule to prefix length, ignoring: %m");
821 return 0;
822 }
823 }
824
825 break;
826
827 default:
828 assert_not_reached("Received unsupported address family");
829 }
830
831 if (tmp->from_prefixlen == 0 && tmp->to_prefixlen == 0)
832 return 0;
833
834 r = sd_rtnl_message_routing_policy_rule_get_flags(message, &flags);
835 if (r < 0) {
836 log_warning_errno(r, "rtnl: could not get flag, ignoring: %m");
837 return 0;
838 }
839 tmp->invert_rule = flags & FIB_RULE_INVERT;
840
841 r = sd_netlink_message_read_u32(message, FRA_FWMARK, &tmp->fwmark);
842 if (r < 0 && r != -ENODATA) {
843 log_warning_errno(r, "rtnl: could not get FRA_FWMARK attribute, ignoring: %m");
844 return 0;
845 }
846
847 r = sd_netlink_message_read_u32(message, FRA_FWMASK, &tmp->fwmask);
848 if (r < 0 && r != -ENODATA) {
849 log_warning_errno(r, "rtnl: could not get FRA_FWMASK attribute, ignoring: %m");
850 return 0;
851 }
852
853 r = sd_netlink_message_read_u32(message, FRA_PRIORITY, &tmp->priority);
854 if (r < 0 && r != -ENODATA) {
855 log_warning_errno(r, "rtnl: could not get FRA_PRIORITY attribute, ignoring: %m");
856 return 0;
857 }
858
859 r = sd_netlink_message_read_u32(message, FRA_TABLE, &tmp->table);
860 if (r < 0 && r != -ENODATA) {
861 log_warning_errno(r, "rtnl: could not get FRA_TABLE attribute, ignoring: %m");
862 return 0;
863 }
864
865 r = sd_rtnl_message_routing_policy_rule_get_tos(message, &tmp->tos);
866 if (r < 0 && r != -ENODATA) {
867 log_warning_errno(r, "rtnl: could not get ip rule TOS, ignoring: %m");
868 return 0;
869 }
870
871 r = sd_netlink_message_read_string(message, FRA_IIFNAME, &iif);
872 if (r < 0 && r != -ENODATA) {
873 log_warning_errno(r, "rtnl: could not get FRA_IIFNAME attribute, ignoring: %m");
874 return 0;
875 }
876 r = free_and_strdup(&tmp->iif, iif);
877 if (r < 0)
878 return log_oom();
879
880 r = sd_netlink_message_read_string(message, FRA_OIFNAME, &oif);
881 if (r < 0 && r != -ENODATA) {
882 log_warning_errno(r, "rtnl: could not get FRA_OIFNAME attribute, ignoring: %m");
883 return 0;
884 }
885 r = free_and_strdup(&tmp->oif, oif);
886 if (r < 0)
887 return log_oom();
888
889 r = sd_netlink_message_read_u8(message, FRA_IP_PROTO, &tmp->protocol);
890 if (r < 0 && r != -ENODATA) {
891 log_warning_errno(r, "rtnl: could not get FRA_IP_PROTO attribute, ignoring: %m");
892 return 0;
893 }
894
895 r = sd_netlink_message_read(message, FRA_SPORT_RANGE, sizeof(tmp->sport), &tmp->sport);
896 if (r < 0 && r != -ENODATA) {
897 log_warning_errno(r, "rtnl: could not get FRA_SPORT_RANGE attribute, ignoring: %m");
898 return 0;
899 }
900
901 r = sd_netlink_message_read(message, FRA_DPORT_RANGE, sizeof(tmp->dport), &tmp->dport);
902 if (r < 0 && r != -ENODATA) {
903 log_warning_errno(r, "rtnl: could not get FRA_DPORT_RANGE attribute, ignoring: %m");
904 return 0;
905 }
906
907 (void) routing_policy_rule_get(m, tmp, &rule);
908
909 switch (type) {
910 case RTM_NEWRULE:
911 if (!rule) {
912 r = routing_policy_rule_add_foreign(m, tmp, &rule);
913 if (r < 0) {
914 log_warning_errno(r, "Could not add rule, ignoring: %m");
915 return 0;
916 }
917 }
918 break;
919 case RTM_DELRULE:
920 routing_policy_rule_free(rule);
921
922 break;
923
924 default:
925 assert_not_reached("Received invalid RTNL message type");
926 }
927
928 return 1;
929 }
930
931 static int systemd_netlink_fd(void) {
932 int n, fd, rtnl_fd = -EINVAL;
933
934 n = sd_listen_fds(true);
935 if (n <= 0)
936 return -EINVAL;
937
938 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
939 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
940 if (rtnl_fd >= 0)
941 return -EINVAL;
942
943 rtnl_fd = fd;
944 }
945 }
946
947 return rtnl_fd;
948 }
949
950 static int manager_connect_genl(Manager *m) {
951 int r;
952
953 assert(m);
954
955 r = sd_genl_socket_open(&m->genl);
956 if (r < 0)
957 return r;
958
959 r = sd_netlink_inc_rcvbuf(m->genl, RCVBUF_SIZE);
960 if (r < 0)
961 return r;
962
963 r = sd_netlink_attach_event(m->genl, m->event, 0);
964 if (r < 0)
965 return r;
966
967 return 0;
968 }
969
970 static int manager_connect_rtnl(Manager *m) {
971 int fd, r;
972
973 assert(m);
974
975 fd = systemd_netlink_fd();
976 if (fd < 0)
977 r = sd_netlink_open(&m->rtnl);
978 else
979 r = sd_netlink_open_fd(&m->rtnl, fd);
980 if (r < 0)
981 return r;
982
983 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
984 if (r < 0)
985 return r;
986
987 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
988 if (r < 0)
989 return r;
990
991 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
992 if (r < 0)
993 return r;
994
995 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, &manager_rtnl_process_link, NULL, m, "network-rtnl_process_link");
996 if (r < 0)
997 return r;
998
999 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
1000 if (r < 0)
1001 return r;
1002
1003 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, &manager_rtnl_process_address, NULL, m, "network-rtnl_process_address");
1004 if (r < 0)
1005 return r;
1006
1007 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
1008 if (r < 0)
1009 return r;
1010
1011 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELROUTE, &manager_rtnl_process_route, NULL, m, "network-rtnl_process_route");
1012 if (r < 0)
1013 return r;
1014
1015 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
1016 if (r < 0)
1017 return r;
1018
1019 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELRULE, &manager_rtnl_process_rule, NULL, m, "network-rtnl_process_rule");
1020 if (r < 0)
1021 return r;
1022
1023 return 0;
1024 }
1025
1026 static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) {
1027 char *p;
1028 int r;
1029
1030 assert(s);
1031 assert(address);
1032
1033 r = in_addr_to_string(address->family, &address->address, &p);
1034 if (r < 0)
1035 return r;
1036
1037 r = ordered_set_consume(s, p);
1038 if (r == -EEXIST)
1039 return 0;
1040
1041 return r;
1042 }
1043
1044 static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) {
1045 int r, c = 0;
1046 unsigned i;
1047
1048 assert(s);
1049 assert(addresses || n == 0);
1050
1051 for (i = 0; i < n; i++) {
1052 r = ordered_set_put_in_addr_data(s, addresses+i);
1053 if (r < 0)
1054 return r;
1055
1056 c += r;
1057 }
1058
1059 return c;
1060 }
1061
1062 static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) {
1063 char *p;
1064 int r;
1065
1066 assert(s);
1067 assert(address);
1068
1069 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
1070 if (r < 0)
1071 return r;
1072
1073 r = ordered_set_consume(s, p);
1074 if (r == -EEXIST)
1075 return 0;
1076
1077 return r;
1078 }
1079
1080 static int ordered_set_put_in4_addrv(OrderedSet *s,
1081 const struct in_addr *addresses,
1082 size_t n,
1083 bool (*predicate)(const struct in_addr *addr)) {
1084 int r, c = 0;
1085 size_t i;
1086
1087 assert(s);
1088 assert(n == 0 || addresses);
1089
1090 for (i = 0; i < n; i++) {
1091 if (predicate && !predicate(&addresses[i]))
1092 continue;
1093 r = ordered_set_put_in4_addr(s, addresses+i);
1094 if (r < 0)
1095 return r;
1096
1097 c += r;
1098 }
1099
1100 return c;
1101 }
1102
1103 static int manager_save(Manager *m) {
1104 _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL;
1105 Link *link;
1106 Iterator i;
1107 _cleanup_free_ char *temp_path = NULL;
1108 _cleanup_fclose_ FILE *f = NULL;
1109 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
1110 const char *operstate_str;
1111 int r;
1112
1113 assert(m);
1114 assert(m->state_file);
1115
1116 /* We add all NTP and DNS server to a set, to filter out duplicates */
1117 dns = ordered_set_new(&string_hash_ops);
1118 if (!dns)
1119 return -ENOMEM;
1120
1121 ntp = ordered_set_new(&string_hash_ops);
1122 if (!ntp)
1123 return -ENOMEM;
1124
1125 search_domains = ordered_set_new(&dns_name_hash_ops);
1126 if (!search_domains)
1127 return -ENOMEM;
1128
1129 route_domains = ordered_set_new(&dns_name_hash_ops);
1130 if (!route_domains)
1131 return -ENOMEM;
1132
1133 HASHMAP_FOREACH(link, m->links, i) {
1134 if (link->flags & IFF_LOOPBACK)
1135 continue;
1136
1137 if (link->operstate > operstate)
1138 operstate = link->operstate;
1139
1140 if (!link->network)
1141 continue;
1142
1143 /* First add the static configured entries */
1144 r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns);
1145 if (r < 0)
1146 return r;
1147
1148 r = ordered_set_put_strdupv(ntp, link->network->ntp);
1149 if (r < 0)
1150 return r;
1151
1152 r = ordered_set_put_string_set(search_domains, link->network->search_domains);
1153 if (r < 0)
1154 return r;
1155
1156 r = ordered_set_put_string_set(route_domains, link->network->route_domains);
1157 if (r < 0)
1158 return r;
1159
1160 if (!link->dhcp_lease)
1161 continue;
1162
1163 /* Secondly, add the entries acquired via DHCP */
1164 if (link->network->dhcp_use_dns) {
1165 const struct in_addr *addresses;
1166
1167 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
1168 if (r > 0) {
1169 r = ordered_set_put_in4_addrv(dns, addresses, r, in4_addr_is_non_local);
1170 if (r < 0)
1171 return r;
1172 } else if (r < 0 && r != -ENODATA)
1173 return r;
1174 }
1175
1176 if (link->network->dhcp_use_ntp) {
1177 const struct in_addr *addresses;
1178
1179 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
1180 if (r > 0) {
1181 r = ordered_set_put_in4_addrv(ntp, addresses, r, in4_addr_is_non_local);
1182 if (r < 0)
1183 return r;
1184 } else if (r < 0 && r != -ENODATA)
1185 return r;
1186 }
1187
1188 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
1189 const char *domainname;
1190 char **domains = NULL;
1191
1192 OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains;
1193 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
1194 if (r >= 0) {
1195 r = ordered_set_put_strdup(target_domains, domainname);
1196 if (r < 0)
1197 return r;
1198 } else if (r != -ENODATA)
1199 return r;
1200
1201 r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains);
1202 if (r >= 0) {
1203 r = ordered_set_put_strdupv(target_domains, domains);
1204 if (r < 0)
1205 return r;
1206 } else if (r != -ENODATA)
1207 return r;
1208 }
1209 }
1210
1211 operstate_str = link_operstate_to_string(operstate);
1212 assert(operstate_str);
1213
1214 r = fopen_temporary(m->state_file, &f, &temp_path);
1215 if (r < 0)
1216 return r;
1217
1218 (void) fchmod(fileno(f), 0644);
1219
1220 fprintf(f,
1221 "# This is private data. Do not parse.\n"
1222 "OPER_STATE=%s\n", operstate_str);
1223
1224 ordered_set_print(f, "DNS=", dns);
1225 ordered_set_print(f, "NTP=", ntp);
1226 ordered_set_print(f, "DOMAINS=", search_domains);
1227 ordered_set_print(f, "ROUTE_DOMAINS=", route_domains);
1228
1229 r = routing_policy_serialize_rules(m->rules, f);
1230 if (r < 0)
1231 goto fail;
1232
1233 r = fflush_and_check(f);
1234 if (r < 0)
1235 goto fail;
1236
1237 if (rename(temp_path, m->state_file) < 0) {
1238 r = -errno;
1239 goto fail;
1240 }
1241
1242 if (m->operational_state != operstate) {
1243 m->operational_state = operstate;
1244 r = manager_send_changed(m, "OperationalState", NULL);
1245 if (r < 0)
1246 log_error_errno(r, "Could not emit changed OperationalState: %m");
1247 }
1248
1249 m->dirty = false;
1250
1251 return 0;
1252
1253 fail:
1254 (void) unlink(m->state_file);
1255 (void) unlink(temp_path);
1256
1257 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
1258 }
1259
1260 static int manager_dirty_handler(sd_event_source *s, void *userdata) {
1261 Manager *m = userdata;
1262 Link *link;
1263 Iterator i;
1264
1265 assert(m);
1266
1267 if (m->dirty)
1268 manager_save(m);
1269
1270 SET_FOREACH(link, m->dirty_links, i)
1271 if (link_save(link) >= 0)
1272 link_clean(link);
1273
1274 return 1;
1275 }
1276
1277 Link *manager_dhcp6_prefix_get(Manager *m, struct in6_addr *addr) {
1278 assert_return(m, NULL);
1279 assert_return(addr, NULL);
1280
1281 return hashmap_get(m->dhcp6_prefixes, addr);
1282 }
1283
1284 static int dhcp6_route_add_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
1285 int r;
1286
1287 assert(link);
1288
1289 r = sd_netlink_message_get_errno(m);
1290 if (r < 0 && r != -EEXIST)
1291 log_link_debug_errno(link, r, "Received error adding DHCPv6 Prefix Delegation route: %m");
1292
1293 return 0;
1294 }
1295
1296 int manager_dhcp6_prefix_add(Manager *m, struct in6_addr *addr, Link *link) {
1297 _cleanup_free_ struct in6_addr *a = NULL;
1298 _cleanup_free_ char *buf = NULL;
1299 Link *assigned_link;
1300 Route *route;
1301 int r;
1302
1303 assert_return(m, -EINVAL);
1304 assert_return(addr, -EINVAL);
1305
1306 r = route_add(link, AF_INET6, (union in_addr_union *) addr, 64,
1307 0, 0, 0, &route);
1308 if (r < 0)
1309 return r;
1310
1311 r = route_configure(route, link, dhcp6_route_add_handler);
1312 if (r < 0)
1313 return r;
1314
1315 (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
1316 log_link_debug(link, "Adding prefix route %s/64", strnull(buf));
1317
1318 assigned_link = hashmap_get(m->dhcp6_prefixes, addr);
1319 if (assigned_link) {
1320 assert(assigned_link == link);
1321 return 0;
1322 }
1323
1324 a = newdup(struct in6_addr, addr, 1);
1325 if (!a)
1326 return -ENOMEM;
1327
1328 r = hashmap_ensure_allocated(&m->dhcp6_prefixes, &in6_addr_hash_ops);
1329 if (r < 0)
1330 return r;
1331
1332 r = hashmap_put(m->dhcp6_prefixes, a, link);
1333 if (r < 0)
1334 return r;
1335
1336 TAKE_PTR(a);
1337 link_ref(link);
1338 return 0;
1339 }
1340
1341 static int dhcp6_route_remove_handler(sd_netlink *nl, sd_netlink_message *m, Link *link) {
1342 int r;
1343
1344 assert(link);
1345
1346 r = sd_netlink_message_get_errno(m);
1347 if (r < 0)
1348 log_link_debug_errno(link, r, "Received error on DHCPv6 Prefix Delegation route removal: %m");
1349
1350 return 1;
1351 }
1352
1353 static int manager_dhcp6_prefix_remove(Manager *m, struct in6_addr *addr) {
1354 _cleanup_free_ struct in6_addr *a = NULL;
1355 _cleanup_(link_unrefp) Link *l = NULL;
1356 _cleanup_free_ char *buf = NULL;
1357 Route *route;
1358 int r;
1359
1360 assert_return(m, -EINVAL);
1361 assert_return(addr, -EINVAL);
1362
1363 l = hashmap_remove2(m->dhcp6_prefixes, addr, (void **) &a);
1364 if (!l)
1365 return -EINVAL;
1366
1367 (void) sd_radv_remove_prefix(l->radv, addr, 64);
1368 r = route_get(l, AF_INET6, (union in_addr_union *) addr, 64, 0, 0, 0, &route);
1369 if (r < 0)
1370 return r;
1371
1372 r = route_remove(route, l, dhcp6_route_remove_handler);
1373 if (r < 0)
1374 return r;
1375
1376 (void) in_addr_to_string(AF_INET6, (union in_addr_union *) addr, &buf);
1377 log_link_debug(l, "Removing prefix route %s/64", strnull(buf));
1378
1379 return 0;
1380 }
1381
1382 int manager_dhcp6_prefix_remove_all(Manager *m, Link *link) {
1383 struct in6_addr *addr;
1384 Iterator i;
1385 Link *l;
1386
1387 assert_return(m, -EINVAL);
1388 assert_return(link, -EINVAL);
1389
1390 HASHMAP_FOREACH_KEY(l, addr, m->dhcp6_prefixes, i)
1391 if (l == link)
1392 (void) manager_dhcp6_prefix_remove(m, addr);
1393
1394 return 0;
1395 }
1396
1397 int manager_new(Manager **ret) {
1398 _cleanup_(manager_freep) Manager *m = NULL;
1399 int r;
1400
1401 m = new(Manager, 1);
1402 if (!m)
1403 return -ENOMEM;
1404
1405 *m = (Manager) {
1406 .speed_meter_interval_usec = SPEED_METER_DEFAULT_TIME_INTERVAL,
1407 };
1408
1409 m->state_file = strdup("/run/systemd/netif/state");
1410 if (!m->state_file)
1411 return -ENOMEM;
1412
1413 r = sd_event_default(&m->event);
1414 if (r < 0)
1415 return r;
1416
1417 (void) sd_event_set_watchdog(m->event, true);
1418 (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
1419 (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
1420
1421 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
1422 if (r < 0)
1423 return r;
1424
1425 r = manager_connect_rtnl(m);
1426 if (r < 0)
1427 return r;
1428
1429 r = manager_connect_genl(m);
1430 if (r < 0)
1431 return r;
1432
1433 r = manager_connect_udev(m);
1434 if (r < 0)
1435 return r;
1436
1437 r = sd_resolve_default(&m->resolve);
1438 if (r < 0)
1439 return r;
1440
1441 r = sd_resolve_attach_event(m->resolve, m->event, 0);
1442 if (r < 0)
1443 return r;
1444
1445 r = setup_default_address_pool(m);
1446 if (r < 0)
1447 return r;
1448
1449 m->duid.type = DUID_TYPE_EN;
1450
1451 (void) routing_policy_load_rules(m->state_file, &m->rules_saved);
1452
1453 *ret = TAKE_PTR(m);
1454
1455 return 0;
1456 }
1457
1458 void manager_free(Manager *m) {
1459 struct in6_addr *a;
1460 AddressPool *pool;
1461 Link *link;
1462
1463 if (!m)
1464 return;
1465
1466 free(m->state_file);
1467
1468 while ((a = hashmap_first_key(m->dhcp6_prefixes)))
1469 (void) manager_dhcp6_prefix_remove(m, a);
1470 hashmap_free(m->dhcp6_prefixes);
1471
1472 while ((link = hashmap_steal_first(m->links))) {
1473 if (link->dhcp6_client)
1474 (void) dhcp6_lease_pd_prefix_lost(link->dhcp6_client, link);
1475
1476 (void) link_stop_clients(link, true);
1477
1478 link_unref(link);
1479 }
1480
1481 m->dirty_links = set_free_with_destructor(m->dirty_links, link_unref);
1482 m->links_requesting_uuid = set_free_with_destructor(m->links_requesting_uuid, link_unref);
1483 m->links = hashmap_free_with_destructor(m->links, link_unref);
1484
1485 m->duids_requesting_uuid = set_free(m->duids_requesting_uuid);
1486 m->networks = ordered_hashmap_free_with_destructor(m->networks, network_unref);
1487
1488 m->netdevs = hashmap_free_with_destructor(m->netdevs, netdev_unref);
1489
1490 while ((pool = m->address_pools))
1491 address_pool_free(pool);
1492
1493 /* routing_policy_rule_free() access m->rules and m->rules_foreign.
1494 * So, it is necessary to set NULL after the sets are freed. */
1495 m->rules = set_free_with_destructor(m->rules, routing_policy_rule_free);
1496 m->rules_foreign = set_free_with_destructor(m->rules_foreign, routing_policy_rule_free);
1497 set_free_with_destructor(m->rules_saved, routing_policy_rule_free);
1498
1499 sd_netlink_unref(m->rtnl);
1500 sd_netlink_unref(m->genl);
1501 sd_resolve_unref(m->resolve);
1502
1503 sd_event_source_unref(m->speed_meter_event_source);
1504 sd_event_unref(m->event);
1505
1506 sd_device_monitor_unref(m->device_monitor);
1507
1508 sd_bus_flush_close_unref(m->bus);
1509
1510 free(m->dynamic_timezone);
1511 free(m->dynamic_hostname);
1512
1513 free(m);
1514 }
1515
1516 int manager_start(Manager *m) {
1517 Link *link;
1518 Iterator i;
1519 int r;
1520
1521 assert(m);
1522
1523 r = manager_start_speed_meter(m);
1524 if (r < 0)
1525 return log_error_errno(r, "Failed to initialize speed meter: %m");
1526
1527 /* The dirty handler will deal with future serialization, but the first one
1528 must be done explicitly. */
1529
1530 manager_save(m);
1531
1532 HASHMAP_FOREACH(link, m->links, i)
1533 link_save(link);
1534
1535 return 0;
1536 }
1537
1538 int manager_load_config(Manager *m) {
1539 int r;
1540
1541 /* update timestamp */
1542 paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, true);
1543
1544 r = netdev_load(m);
1545 if (r < 0)
1546 return r;
1547
1548 r = network_load(m);
1549 if (r < 0)
1550 return r;
1551
1552 return 0;
1553 }
1554
1555 bool manager_should_reload(Manager *m) {
1556 return paths_check_timestamp(NETWORK_DIRS, &m->network_dirs_ts_usec, false);
1557 }
1558
1559 int manager_rtnl_enumerate_links(Manager *m) {
1560 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1561 sd_netlink_message *link;
1562 int r;
1563
1564 assert(m);
1565 assert(m->rtnl);
1566
1567 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
1568 if (r < 0)
1569 return r;
1570
1571 r = sd_netlink_message_request_dump(req, true);
1572 if (r < 0)
1573 return r;
1574
1575 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1576 if (r < 0)
1577 return r;
1578
1579 for (link = reply; link; link = sd_netlink_message_next(link)) {
1580 int k;
1581
1582 m->enumerating = true;
1583
1584 k = manager_rtnl_process_link(m->rtnl, link, m);
1585 if (k < 0)
1586 r = k;
1587
1588 m->enumerating = false;
1589 }
1590
1591 return r;
1592 }
1593
1594 int manager_rtnl_enumerate_addresses(Manager *m) {
1595 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1596 sd_netlink_message *addr;
1597 int r;
1598
1599 assert(m);
1600 assert(m->rtnl);
1601
1602 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
1603 if (r < 0)
1604 return r;
1605
1606 r = sd_netlink_message_request_dump(req, true);
1607 if (r < 0)
1608 return r;
1609
1610 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1611 if (r < 0)
1612 return r;
1613
1614 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
1615 int k;
1616
1617 m->enumerating = true;
1618
1619 k = manager_rtnl_process_address(m->rtnl, addr, m);
1620 if (k < 0)
1621 r = k;
1622
1623 m->enumerating = false;
1624 }
1625
1626 return r;
1627 }
1628
1629 int manager_rtnl_enumerate_routes(Manager *m) {
1630 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1631 sd_netlink_message *route;
1632 int r;
1633
1634 assert(m);
1635 assert(m->rtnl);
1636
1637 r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0);
1638 if (r < 0)
1639 return r;
1640
1641 r = sd_netlink_message_request_dump(req, true);
1642 if (r < 0)
1643 return r;
1644
1645 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1646 if (r < 0)
1647 return r;
1648
1649 for (route = reply; route; route = sd_netlink_message_next(route)) {
1650 int k;
1651
1652 m->enumerating = true;
1653
1654 k = manager_rtnl_process_route(m->rtnl, route, m);
1655 if (k < 0)
1656 r = k;
1657
1658 m->enumerating = false;
1659 }
1660
1661 return r;
1662 }
1663
1664 int manager_rtnl_enumerate_rules(Manager *m) {
1665 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1666 sd_netlink_message *rule;
1667 int r;
1668
1669 assert(m);
1670 assert(m->rtnl);
1671
1672 r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0);
1673 if (r < 0)
1674 return r;
1675
1676 r = sd_netlink_message_request_dump(req, true);
1677 if (r < 0)
1678 return r;
1679
1680 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1681 if (r < 0) {
1682 if (r == -EOPNOTSUPP) {
1683 log_debug("FIB Rules are not supported by the kernel. Ignoring.");
1684 return 0;
1685 }
1686
1687 return r;
1688 }
1689
1690 for (rule = reply; rule; rule = sd_netlink_message_next(rule)) {
1691 int k;
1692
1693 m->enumerating = true;
1694
1695 k = manager_rtnl_process_rule(m->rtnl, rule, m);
1696 if (k < 0)
1697 r = k;
1698
1699 m->enumerating = false;
1700 }
1701
1702 return r;
1703 }
1704
1705 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
1706 AddressPool *p;
1707 int r;
1708
1709 assert(m);
1710 assert(prefixlen > 0);
1711 assert(found);
1712
1713 LIST_FOREACH(address_pools, p, m->address_pools) {
1714 if (p->family != family)
1715 continue;
1716
1717 r = address_pool_acquire(p, prefixlen, found);
1718 if (r != 0)
1719 return r;
1720 }
1721
1722 return 0;
1723 }
1724
1725 Link* manager_find_uplink(Manager *m, Link *exclude) {
1726 _cleanup_free_ struct local_address *gateways = NULL;
1727 int n, i;
1728
1729 assert(m);
1730
1731 /* Looks for a suitable "uplink", via black magic: an
1732 * interface that is up and where the default route with the
1733 * highest priority points to. */
1734
1735 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
1736 if (n < 0) {
1737 log_warning_errno(n, "Failed to determine list of default gateways: %m");
1738 return NULL;
1739 }
1740
1741 for (i = 0; i < n; i++) {
1742 Link *link;
1743
1744 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
1745 if (!link) {
1746 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
1747 continue;
1748 }
1749
1750 if (link == exclude)
1751 continue;
1752
1753 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
1754 continue;
1755
1756 return link;
1757 }
1758
1759 return NULL;
1760 }
1761
1762 void manager_dirty(Manager *manager) {
1763 assert(manager);
1764
1765 /* the serialized state in /run is no longer up-to-date */
1766 manager->dirty = true;
1767 }
1768
1769 static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
1770 Manager *manager = userdata;
1771 const sd_bus_error *e;
1772
1773 assert(m);
1774 assert(manager);
1775
1776 e = sd_bus_message_get_error(m);
1777 if (e)
1778 log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message);
1779
1780 return 1;
1781 }
1782
1783 int manager_set_hostname(Manager *m, const char *hostname) {
1784 int r;
1785
1786 log_debug("Setting transient hostname: '%s'", strna(hostname));
1787
1788 if (free_and_strdup(&m->dynamic_hostname, hostname) < 0)
1789 return log_oom();
1790
1791 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
1792 log_debug("Not connected to system bus, setting hostname later.");
1793 return 0;
1794 }
1795
1796 r = sd_bus_call_method_async(
1797 m->bus,
1798 NULL,
1799 "org.freedesktop.hostname1",
1800 "/org/freedesktop/hostname1",
1801 "org.freedesktop.hostname1",
1802 "SetHostname",
1803 set_hostname_handler,
1804 m,
1805 "sb",
1806 hostname,
1807 false);
1808
1809 if (r < 0)
1810 return log_error_errno(r, "Could not set transient hostname: %m");
1811
1812 return 0;
1813 }
1814
1815 static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
1816 Manager *manager = userdata;
1817 const sd_bus_error *e;
1818
1819 assert(m);
1820 assert(manager);
1821
1822 e = sd_bus_message_get_error(m);
1823 if (e)
1824 log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message);
1825
1826 return 1;
1827 }
1828
1829 int manager_set_timezone(Manager *m, const char *tz) {
1830 int r;
1831
1832 assert(m);
1833 assert(tz);
1834
1835 log_debug("Setting system timezone: '%s'", tz);
1836 if (free_and_strdup(&m->dynamic_timezone, tz) < 0)
1837 return log_oom();
1838
1839 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
1840 log_debug("Not connected to system bus, setting timezone later.");
1841 return 0;
1842 }
1843
1844 r = sd_bus_call_method_async(
1845 m->bus,
1846 NULL,
1847 "org.freedesktop.timedate1",
1848 "/org/freedesktop/timedate1",
1849 "org.freedesktop.timedate1",
1850 "SetTimezone",
1851 set_timezone_handler,
1852 m,
1853 "sb",
1854 tz,
1855 false);
1856 if (r < 0)
1857 return log_error_errno(r, "Could not set timezone: %m");
1858
1859 return 0;
1860 }
1861
1862 int manager_request_product_uuid(Manager *m, Link *link) {
1863 int r;
1864
1865 assert(m);
1866
1867 if (m->has_product_uuid)
1868 return 0;
1869
1870 log_debug("Requesting product UUID");
1871
1872 if (link) {
1873 DUID *duid;
1874
1875 assert_se(duid = link_get_duid(link));
1876
1877 r = set_ensure_allocated(&m->links_requesting_uuid, NULL);
1878 if (r < 0)
1879 return log_oom();
1880
1881 r = set_ensure_allocated(&m->duids_requesting_uuid, NULL);
1882 if (r < 0)
1883 return log_oom();
1884
1885 r = set_put(m->links_requesting_uuid, link);
1886 if (r < 0)
1887 return log_oom();
1888
1889 r = set_put(m->duids_requesting_uuid, duid);
1890 if (r < 0)
1891 return log_oom();
1892 }
1893
1894 if (!m->bus || sd_bus_is_ready(m->bus) <= 0) {
1895 log_debug("Not connected to system bus, requesting product UUID later.");
1896 return 0;
1897 }
1898
1899 r = sd_bus_call_method_async(
1900 m->bus,
1901 NULL,
1902 "org.freedesktop.hostname1",
1903 "/org/freedesktop/hostname1",
1904 "org.freedesktop.hostname1",
1905 "GetProductUUID",
1906 get_product_uuid_handler,
1907 m,
1908 "b",
1909 false);
1910 if (r < 0)
1911 return log_warning_errno(r, "Failed to get product UUID: %m");
1912
1913 return 0;
1914 }