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