]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-manager.c
4e3f9e35c62175e377d2f7b486f10c66c647c300
[thirdparty/systemd.git] / src / network / networkd-manager.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2013 Tom Gundersen <teg@jklm.no>
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <sys/socket.h>
21 #include <linux/if.h>
22
23 #include "sd-daemon.h"
24 #include "sd-netlink.h"
25
26 #include "alloc-util.h"
27 #include "bus-util.h"
28 #include "conf-parser.h"
29 #include "def.h"
30 #include "dns-domain.h"
31 #include "fd-util.h"
32 #include "fileio.h"
33 #include "libudev-private.h"
34 #include "local-addresses.h"
35 #include "netlink-util.h"
36 #include "networkd-manager.h"
37 #include "ordered-set.h"
38 #include "path-util.h"
39 #include "set.h"
40 #include "udev-util.h"
41 #include "virt.h"
42
43 /* use 8 MB for receive socket kernel queue. */
44 #define RCVBUF_SIZE (8*1024*1024)
45
46 const char* const network_dirs[] = {
47 "/etc/systemd/network",
48 "/run/systemd/network",
49 "/usr/lib/systemd/network",
50 #ifdef HAVE_SPLIT_USR
51 "/lib/systemd/network",
52 #endif
53 NULL};
54
55 static int setup_default_address_pool(Manager *m) {
56 AddressPool *p;
57 int r;
58
59 assert(m);
60
61 /* Add in the well-known private address ranges. */
62
63 r = address_pool_new_from_string(m, &p, AF_INET6, "fc00::", 7);
64 if (r < 0)
65 return r;
66
67 r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
68 if (r < 0)
69 return r;
70
71 r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
72 if (r < 0)
73 return r;
74
75 r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
76 if (r < 0)
77 return r;
78
79 return 0;
80 }
81
82 static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
83 Manager *m = userdata;
84
85 assert(s);
86 assert(m);
87
88 m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
89
90 manager_connect_bus(m);
91
92 return 0;
93 }
94
95 static int manager_reset_all(Manager *m) {
96 Link *link;
97 Iterator i;
98 int r;
99
100 assert(m);
101
102 HASHMAP_FOREACH(link, m->links, i) {
103 r = link_carrier_reset(link);
104 if (r < 0)
105 log_link_warning_errno(link, r, "Could not reset carrier: %m");
106 }
107
108 return 0;
109 }
110
111 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
112 Manager *m = userdata;
113 int b, r;
114
115 assert(message);
116
117 r = sd_bus_message_read(message, "b", &b);
118 if (r < 0) {
119 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
120 return 0;
121 }
122
123 if (b)
124 return 0;
125
126 log_debug("Coming back from suspend, resetting all connections...");
127
128 manager_reset_all(m);
129
130 return 0;
131 }
132
133 int manager_connect_bus(Manager *m) {
134 int r;
135
136 assert(m);
137
138 r = sd_bus_default_system(&m->bus);
139 if (r == -ENOENT) {
140 /* We failed to connect? Yuck, we must be in early
141 * boot. Let's try in 5s again. As soon as we have
142 * kdbus we can stop doing this... */
143
144 log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m");
145
146 r = sd_event_add_time(m->event, &m->bus_retry_event_source, CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 5*USEC_PER_SEC, 0, on_bus_retry, m);
147 if (r < 0)
148 return log_error_errno(r, "Failed to install bus reconnect time event: %m");
149
150 return 0;
151 }
152
153 if (r < 0)
154 return r;
155
156 r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot,
157 "type='signal',"
158 "sender='org.freedesktop.login1',"
159 "interface='org.freedesktop.login1.Manager',"
160 "member='PrepareForSleep',"
161 "path='/org/freedesktop/login1'",
162 match_prepare_for_sleep,
163 m);
164 if (r < 0)
165 return log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
166
167 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m);
168 if (r < 0)
169 return log_error_errno(r, "Failed to add manager object vtable: %m");
170
171 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m);
172 if (r < 0)
173 return log_error_errno(r, "Failed to add link object vtable: %m");
174
175 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m);
176 if (r < 0)
177 return log_error_errno(r, "Failed to add link enumerator: %m");
178
179 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m);
180 if (r < 0)
181 return log_error_errno(r, "Failed to add network object vtable: %m");
182
183 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m);
184 if (r < 0)
185 return log_error_errno(r, "Failed to add network enumerator: %m");
186
187 r = sd_bus_request_name(m->bus, "org.freedesktop.network1", 0);
188 if (r < 0)
189 return log_error_errno(r, "Failed to register name: %m");
190
191 r = sd_bus_attach_event(m->bus, m->event, 0);
192 if (r < 0)
193 return log_error_errno(r, "Failed to attach bus to event loop: %m");
194
195 /* Did we get a timezone or transient hostname from DHCP while D-Bus wasn't up yet? */
196 if (m->dynamic_hostname) {
197 r = manager_set_hostname(m, m->dynamic_hostname);
198 if (r < 0)
199 return r;
200 }
201 if (m->dynamic_timezone) {
202 r = manager_set_timezone(m, m->dynamic_timezone);
203 if (r < 0)
204 return r;
205 }
206
207 return 0;
208 }
209
210 static int manager_udev_process_link(Manager *m, struct udev_device *device) {
211 Link *link = NULL;
212 int r, ifindex;
213
214 assert(m);
215 assert(device);
216
217 if (!streq_ptr(udev_device_get_action(device), "add"))
218 return 0;
219
220 ifindex = udev_device_get_ifindex(device);
221 if (ifindex <= 0) {
222 log_debug("Ignoring udev ADD event for device with invalid ifindex");
223 return 0;
224 }
225
226 r = link_get(m, ifindex, &link);
227 if (r == -ENODEV)
228 return 0;
229 else if (r < 0)
230 return r;
231
232 r = link_initialized(link, device);
233 if (r < 0)
234 return r;
235
236 return 0;
237 }
238
239 static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
240 Manager *m = userdata;
241 struct udev_monitor *monitor = m->udev_monitor;
242 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
243
244 device = udev_monitor_receive_device(monitor);
245 if (!device)
246 return -ENOMEM;
247
248 manager_udev_process_link(m, device);
249 return 0;
250 }
251
252 static int manager_connect_udev(Manager *m) {
253 int r;
254
255 /* udev does not initialize devices inside containers,
256 * so we rely on them being already initialized before
257 * entering the container */
258 if (detect_container() > 0)
259 return 0;
260
261 m->udev = udev_new();
262 if (!m->udev)
263 return -ENOMEM;
264
265 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
266 if (!m->udev_monitor)
267 return -ENOMEM;
268
269 r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
270 if (r < 0)
271 return log_error_errno(r, "Could not add udev monitor filter: %m");
272
273 r = udev_monitor_enable_receiving(m->udev_monitor);
274 if (r < 0) {
275 log_error("Could not enable udev monitor");
276 return r;
277 }
278
279 r = sd_event_add_io(m->event,
280 &m->udev_event_source,
281 udev_monitor_get_fd(m->udev_monitor),
282 EPOLLIN, manager_dispatch_link_udev,
283 m);
284 if (r < 0)
285 return r;
286
287 r = sd_event_source_set_description(m->udev_event_source, "networkd-udev");
288 if (r < 0)
289 return r;
290
291 return 0;
292 }
293
294 int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
295 Manager *m = userdata;
296 Link *link = NULL;
297 uint16_t type;
298 uint32_t ifindex, priority = 0;
299 unsigned char protocol, scope, tos, table;
300 int family;
301 unsigned char dst_prefixlen, src_prefixlen;
302 union in_addr_union dst = {}, gw = {}, src = {}, prefsrc = {};
303 Route *route = NULL;
304 int r;
305
306 assert(rtnl);
307 assert(message);
308 assert(m);
309
310 if (sd_netlink_message_is_error(message)) {
311 r = sd_netlink_message_get_errno(message);
312 if (r < 0)
313 log_warning_errno(r, "rtnl: failed to receive route: %m");
314
315 return 0;
316 }
317
318 r = sd_netlink_message_get_type(message, &type);
319 if (r < 0) {
320 log_warning_errno(r, "rtnl: could not get message type: %m");
321 return 0;
322 } else if (type != RTM_NEWROUTE && type != RTM_DELROUTE) {
323 log_warning("rtnl: received unexpected message type when processing route");
324 return 0;
325 }
326
327 r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex);
328 if (r == -ENODATA) {
329 log_debug("rtnl: received route without ifindex, ignoring");
330 return 0;
331 } else if (r < 0) {
332 log_warning_errno(r, "rtnl: could not get ifindex from route, ignoring: %m");
333 return 0;
334 } else if (ifindex <= 0) {
335 log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex);
336 return 0;
337 } else {
338 r = link_get(m, ifindex, &link);
339 if (r < 0 || !link) {
340 /* when enumerating we might be out of sync, but we will
341 * get the route again, so just ignore it */
342 if (!m->enumerating)
343 log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex);
344 return 0;
345 }
346 }
347
348 r = sd_rtnl_message_route_get_family(message, &family);
349 if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) {
350 log_link_warning(link, "rtnl: received address with invalid family, ignoring.");
351 return 0;
352 }
353
354 r = sd_rtnl_message_route_get_protocol(message, &protocol);
355 if (r < 0) {
356 log_warning_errno(r, "rtnl: could not get route protocol: %m");
357 return 0;
358 }
359
360 switch (family) {
361 case AF_INET:
362 r = sd_netlink_message_read_in_addr(message, RTA_DST, &dst.in);
363 if (r < 0 && r != -ENODATA) {
364 log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m");
365 return 0;
366 }
367
368 r = sd_netlink_message_read_in_addr(message, RTA_GATEWAY, &gw.in);
369 if (r < 0 && r != -ENODATA) {
370 log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m");
371 return 0;
372 }
373
374 r = sd_netlink_message_read_in_addr(message, RTA_SRC, &src.in);
375 if (r < 0 && r != -ENODATA) {
376 log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m");
377 return 0;
378 }
379
380 r = sd_netlink_message_read_in_addr(message, RTA_PREFSRC, &prefsrc.in);
381 if (r < 0 && r != -ENODATA) {
382 log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m");
383 return 0;
384 }
385
386 break;
387
388 case AF_INET6:
389 r = sd_netlink_message_read_in6_addr(message, RTA_DST, &dst.in6);
390 if (r < 0 && r != -ENODATA) {
391 log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m");
392 return 0;
393 }
394
395 r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &gw.in6);
396 if (r < 0 && r != -ENODATA) {
397 log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m");
398 return 0;
399 }
400
401 r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &src.in6);
402 if (r < 0 && r != -ENODATA) {
403 log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m");
404 return 0;
405 }
406
407 r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &prefsrc.in6);
408 if (r < 0 && r != -ENODATA) {
409 log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m");
410 return 0;
411 }
412
413 break;
414
415 default:
416 log_link_debug(link, "rtnl: ignoring unsupported address family: %d", family);
417 return 0;
418 }
419
420 r = sd_rtnl_message_route_get_dst_prefixlen(message, &dst_prefixlen);
421 if (r < 0) {
422 log_link_warning_errno(link, r, "rtnl: received route with invalid destination prefixlen, ignoring: %m");
423 return 0;
424 }
425
426 r = sd_rtnl_message_route_get_src_prefixlen(message, &src_prefixlen);
427 if (r < 0) {
428 log_link_warning_errno(link, r, "rtnl: received route with invalid source prefixlen, ignoring: %m");
429 return 0;
430 }
431
432 r = sd_rtnl_message_route_get_scope(message, &scope);
433 if (r < 0) {
434 log_link_warning_errno(link, r, "rtnl: received route with invalid scope, ignoring: %m");
435 return 0;
436 }
437
438 r = sd_rtnl_message_route_get_tos(message, &tos);
439 if (r < 0) {
440 log_link_warning_errno(link, r, "rtnl: received route with invalid tos, ignoring: %m");
441 return 0;
442 }
443
444 r = sd_rtnl_message_route_get_table(message, &table);
445 if (r < 0) {
446 log_link_warning_errno(link, r, "rtnl: received route with invalid table, ignoring: %m");
447 return 0;
448 }
449
450 r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &priority);
451 if (r < 0 && r != -ENODATA) {
452 log_link_warning_errno(link, r, "rtnl: received route with invalid priority, ignoring: %m");
453 return 0;
454 }
455
456 route_get(link, family, &dst, dst_prefixlen, tos, priority, table, &route);
457
458 switch (type) {
459 case RTM_NEWROUTE:
460 if (!route) {
461 /* A route appeared that we did not request */
462 r = route_add_foreign(link, family, &dst, dst_prefixlen, tos, priority, table, &route);
463 if (r < 0)
464 return 0;
465 }
466
467 route_update(route, &src, src_prefixlen, &gw, &prefsrc, scope, protocol);
468
469 break;
470
471 case RTM_DELROUTE:
472 route_free(route);
473 break;
474
475 default:
476 assert_not_reached("Received invalid RTNL message type");
477 }
478
479 return 1;
480 }
481
482 int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
483 Manager *m = userdata;
484 Link *link = NULL;
485 uint16_t type;
486 unsigned char flags;
487 int family;
488 unsigned char prefixlen;
489 unsigned char scope;
490 union in_addr_union in_addr;
491 struct ifa_cacheinfo cinfo;
492 Address *address = NULL;
493 char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX];
494 const char *valid_str = NULL;
495 int r, ifindex;
496
497 assert(rtnl);
498 assert(message);
499 assert(m);
500
501 if (sd_netlink_message_is_error(message)) {
502 r = sd_netlink_message_get_errno(message);
503 if (r < 0)
504 log_warning_errno(r, "rtnl: failed to receive address: %m");
505
506 return 0;
507 }
508
509 r = sd_netlink_message_get_type(message, &type);
510 if (r < 0) {
511 log_warning_errno(r, "rtnl: could not get message type: %m");
512 return 0;
513 } else if (type != RTM_NEWADDR && type != RTM_DELADDR) {
514 log_warning("rtnl: received unexpected message type when processing address");
515 return 0;
516 }
517
518 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
519 if (r < 0) {
520 log_warning_errno(r, "rtnl: could not get ifindex from address: %m");
521 return 0;
522 } else if (ifindex <= 0) {
523 log_warning("rtnl: received address message with invalid ifindex: %d", ifindex);
524 return 0;
525 } else {
526 r = link_get(m, ifindex, &link);
527 if (r < 0 || !link) {
528 /* when enumerating we might be out of sync, but we will
529 * get the address again, so just ignore it */
530 if (!m->enumerating)
531 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex);
532 return 0;
533 }
534 }
535
536 r = sd_rtnl_message_addr_get_family(message, &family);
537 if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) {
538 log_link_warning(link, "rtnl: received address with invalid family, ignoring.");
539 return 0;
540 }
541
542 r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen);
543 if (r < 0) {
544 log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m");
545 return 0;
546 }
547
548 r = sd_rtnl_message_addr_get_scope(message, &scope);
549 if (r < 0) {
550 log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m");
551 return 0;
552 }
553
554 r = sd_rtnl_message_addr_get_flags(message, &flags);
555 if (r < 0) {
556 log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m");
557 return 0;
558 }
559
560 switch (family) {
561 case AF_INET:
562 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in);
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 case AF_INET6:
571 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6);
572 if (r < 0) {
573 log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m");
574 return 0;
575 }
576
577 break;
578
579 default:
580 log_link_debug(link, "rtnl: ignoring unsupported address family: %d", family);
581 }
582
583 if (!inet_ntop(family, &in_addr, buf, INET6_ADDRSTRLEN)) {
584 log_link_warning(link, "Could not print address");
585 return 0;
586 }
587
588 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
589 if (r >= 0) {
590 if (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
596 address_get(link, family, &in_addr, prefixlen, &address);
597
598 switch (type) {
599 case RTM_NEWADDR:
600 if (address)
601 log_link_debug(link, "Updating address: %s/%u (valid %s%s)", buf, prefixlen,
602 valid_str ? "for " : "forever", valid_str ?: "");
603 else {
604 /* An address appeared that we did not request */
605 r = address_add_foreign(link, family, &in_addr, prefixlen, &address);
606 if (r < 0) {
607 log_link_warning_errno(link, r, "Failed to add address %s/%u: %m", buf, prefixlen);
608 return 0;
609 } else
610 log_link_debug(link, "Adding address: %s/%u (valid %s%s)", buf, prefixlen,
611 valid_str ? "for " : "forever", valid_str ?: "");
612 }
613
614 address_update(address, flags, scope, &cinfo);
615
616 break;
617
618 case RTM_DELADDR:
619
620 if (address) {
621 log_link_debug(link, "Removing address: %s/%u (valid %s%s)", buf, prefixlen,
622 valid_str ? "for " : "forever", valid_str ?: "");
623 address_drop(address);
624 } else
625 log_link_warning(link, "Removing non-existent address: %s/%u (valid %s%s)", buf, prefixlen,
626 valid_str ? "for " : "forever", valid_str ?: "");
627
628 break;
629 default:
630 assert_not_reached("Received invalid RTNL message type");
631 }
632
633 return 1;
634 }
635
636 static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
637 Manager *m = userdata;
638 Link *link = NULL;
639 NetDev *netdev = NULL;
640 uint16_t type;
641 const char *name;
642 int r, ifindex;
643
644 assert(rtnl);
645 assert(message);
646 assert(m);
647
648 if (sd_netlink_message_is_error(message)) {
649 r = sd_netlink_message_get_errno(message);
650 if (r < 0)
651 log_warning_errno(r, "rtnl: Could not receive link: %m");
652
653 return 0;
654 }
655
656 r = sd_netlink_message_get_type(message, &type);
657 if (r < 0) {
658 log_warning_errno(r, "rtnl: Could not get message type: %m");
659 return 0;
660 } else if (type != RTM_NEWLINK && type != RTM_DELLINK) {
661 log_warning("rtnl: Received unexpected message type when processing link");
662 return 0;
663 }
664
665 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
666 if (r < 0) {
667 log_warning_errno(r, "rtnl: Could not get ifindex from link: %m");
668 return 0;
669 } else if (ifindex <= 0) {
670 log_warning("rtnl: received link message with invalid ifindex: %d", ifindex);
671 return 0;
672 }
673
674 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
675 if (r < 0) {
676 log_warning_errno(r, "rtnl: Received link message without ifname: %m");
677 return 0;
678 }
679
680 (void) link_get(m, ifindex, &link);
681 (void) netdev_get(m, name, &netdev);
682
683 switch (type) {
684 case RTM_NEWLINK:
685 if (!link) {
686 /* link is new, so add it */
687 r = link_add(m, message, &link);
688 if (r < 0) {
689 log_warning_errno(r, "Could not add new link: %m");
690 return 0;
691 }
692 }
693
694 if (netdev) {
695 /* netdev exists, so make sure the ifindex matches */
696 r = netdev_set_ifindex(netdev, message);
697 if (r < 0) {
698 log_warning_errno(r, "Could not set ifindex on netdev: %m");
699 return 0;
700 }
701 }
702
703 r = link_update(link, message);
704 if (r < 0)
705 return 0;
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 static int systemd_netlink_fd(void) {
723 int n, fd, rtnl_fd = -EINVAL;
724
725 n = sd_listen_fds(true);
726 if (n <= 0)
727 return -EINVAL;
728
729 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
730 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
731 if (rtnl_fd >= 0)
732 return -EINVAL;
733
734 rtnl_fd = fd;
735 }
736 }
737
738 return rtnl_fd;
739 }
740
741 static int manager_connect_rtnl(Manager *m) {
742 int fd, r;
743
744 assert(m);
745
746 fd = systemd_netlink_fd();
747 if (fd < 0)
748 r = sd_netlink_open(&m->rtnl);
749 else
750 r = sd_netlink_open_fd(&m->rtnl, fd);
751 if (r < 0)
752 return r;
753
754 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
755 if (r < 0)
756 return r;
757
758 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
759 if (r < 0)
760 return r;
761
762 r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
763 if (r < 0)
764 return r;
765
766 r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
767 if (r < 0)
768 return r;
769
770 r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m);
771 if (r < 0)
772 return r;
773
774 r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m);
775 if (r < 0)
776 return r;
777
778 r = sd_netlink_add_match(m->rtnl, RTM_NEWROUTE, &manager_rtnl_process_route, m);
779 if (r < 0)
780 return r;
781
782 r = sd_netlink_add_match(m->rtnl, RTM_DELROUTE, &manager_rtnl_process_route, m);
783 if (r < 0)
784 return r;
785
786 return 0;
787 }
788
789 static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) {
790 char *p;
791 int r;
792
793 assert(s);
794 assert(address);
795
796 r = in_addr_to_string(address->family, &address->address, &p);
797 if (r < 0)
798 return r;
799
800 r = ordered_set_consume(s, p);
801 if (r == -EEXIST)
802 return 0;
803
804 return r;
805 }
806
807 static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) {
808 int r, c = 0;
809 unsigned i;
810
811 assert(s);
812 assert(addresses || n == 0);
813
814 for (i = 0; i < n; i++) {
815 r = ordered_set_put_in_addr_data(s, addresses+i);
816 if (r < 0)
817 return r;
818
819 c += r;
820 }
821
822 return c;
823 }
824
825 static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) {
826 char *p;
827 int r;
828
829 assert(s);
830 assert(address);
831
832 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
833 if (r < 0)
834 return r;
835
836 r = ordered_set_consume(s, p);
837 if (r == -EEXIST)
838 return 0;
839
840 return r;
841 }
842
843 static int ordered_set_put_in4_addrv(OrderedSet *s, const struct in_addr *addresses, unsigned n) {
844 int r, c = 0;
845 unsigned i;
846
847 assert(s);
848 assert(n == 0 || addresses);
849
850 for (i = 0; i < n; i++) {
851 r = ordered_set_put_in4_addr(s, addresses+i);
852 if (r < 0)
853 return r;
854
855 c += r;
856 }
857
858 return c;
859 }
860
861 static void print_string_set(FILE *f, const char *field, OrderedSet *s) {
862 bool space = false;
863 Iterator i;
864 char *p;
865
866 if (ordered_set_isempty(s))
867 return;
868
869 fputs(field, f);
870
871 ORDERED_SET_FOREACH(p, s, i)
872 fputs_with_space(f, p, NULL, &space);
873
874 fputc('\n', f);
875 }
876
877 static int manager_save(Manager *m) {
878 _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL;
879 Link *link;
880 Iterator i;
881 _cleanup_free_ char *temp_path = NULL;
882 _cleanup_fclose_ FILE *f = NULL;
883 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
884 const char *operstate_str;
885 int r;
886
887 assert(m);
888 assert(m->state_file);
889
890 /* We add all NTP and DNS server to a set, to filter out duplicates */
891 dns = ordered_set_new(&string_hash_ops);
892 if (!dns)
893 return -ENOMEM;
894
895 ntp = ordered_set_new(&string_hash_ops);
896 if (!ntp)
897 return -ENOMEM;
898
899 search_domains = ordered_set_new(&dns_name_hash_ops);
900 if (!search_domains)
901 return -ENOMEM;
902
903 route_domains = ordered_set_new(&dns_name_hash_ops);
904 if (!route_domains)
905 return -ENOMEM;
906
907 HASHMAP_FOREACH(link, m->links, i) {
908 if (link->flags & IFF_LOOPBACK)
909 continue;
910
911 if (link->operstate > operstate)
912 operstate = link->operstate;
913
914 if (!link->network)
915 continue;
916
917 /* First add the static configured entries */
918 r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns);
919 if (r < 0)
920 return r;
921
922 r = ordered_set_put_strdupv(ntp, link->network->ntp);
923 if (r < 0)
924 return r;
925
926 r = ordered_set_put_strdupv(search_domains, link->network->search_domains);
927 if (r < 0)
928 return r;
929
930 r = ordered_set_put_strdupv(route_domains, link->network->route_domains);
931 if (r < 0)
932 return r;
933
934 if (!link->dhcp_lease)
935 continue;
936
937 /* Secondly, add the entries acquired via DHCP */
938 if (link->network->dhcp_use_dns) {
939 const struct in_addr *addresses;
940
941 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
942 if (r > 0) {
943 r = ordered_set_put_in4_addrv(dns, addresses, r);
944 if (r < 0)
945 return r;
946 } else if (r < 0 && r != -ENODATA)
947 return r;
948 }
949
950 if (link->network->dhcp_use_ntp) {
951 const struct in_addr *addresses;
952
953 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
954 if (r > 0) {
955 r = ordered_set_put_in4_addrv(ntp, addresses, r);
956 if (r < 0)
957 return r;
958 } else if (r < 0 && r != -ENODATA)
959 return r;
960 }
961
962 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
963 const char *domainname;
964
965 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
966 if (r >= 0) {
967
968 if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES)
969 r = ordered_set_put_strdup(search_domains, domainname);
970 else
971 r = ordered_set_put_strdup(route_domains, domainname);
972
973 if (r < 0)
974 return r;
975 } else if (r != -ENODATA)
976 return r;
977 }
978 }
979
980 operstate_str = link_operstate_to_string(operstate);
981 assert(operstate_str);
982
983 r = fopen_temporary(m->state_file, &f, &temp_path);
984 if (r < 0)
985 return r;
986
987 (void) fchmod(fileno(f), 0644);
988
989 fprintf(f,
990 "# This is private data. Do not parse.\n"
991 "OPER_STATE=%s\n", operstate_str);
992
993 print_string_set(f, "DNS=", dns);
994 print_string_set(f, "NTP=", ntp);
995 print_string_set(f, "DOMAINS=", search_domains);
996 print_string_set(f, "ROUTE_DOMAINS=", route_domains);
997
998 r = fflush_and_check(f);
999 if (r < 0)
1000 goto fail;
1001
1002 if (rename(temp_path, m->state_file) < 0) {
1003 r = -errno;
1004 goto fail;
1005 }
1006
1007 if (m->operational_state != operstate) {
1008 m->operational_state = operstate;
1009 r = manager_send_changed(m, "OperationalState", NULL);
1010 if (r < 0)
1011 log_error_errno(r, "Could not emit changed OperationalState: %m");
1012 }
1013
1014 m->dirty = false;
1015
1016 return 0;
1017
1018 fail:
1019 (void) unlink(m->state_file);
1020 (void) unlink(temp_path);
1021
1022 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
1023 }
1024
1025 static int manager_dirty_handler(sd_event_source *s, void *userdata) {
1026 Manager *m = userdata;
1027 Link *link;
1028 Iterator i;
1029 int r;
1030
1031 assert(m);
1032
1033 if (m->dirty)
1034 manager_save(m);
1035
1036 SET_FOREACH(link, m->dirty_links, i) {
1037 r = link_save(link);
1038 if (r >= 0)
1039 link_clean(link);
1040 }
1041
1042 return 1;
1043 }
1044
1045 int manager_new(Manager **ret) {
1046 _cleanup_manager_free_ Manager *m = NULL;
1047 int r;
1048
1049 m = new0(Manager, 1);
1050 if (!m)
1051 return -ENOMEM;
1052
1053 m->state_file = strdup("/run/systemd/netif/state");
1054 if (!m->state_file)
1055 return -ENOMEM;
1056
1057 r = sd_event_default(&m->event);
1058 if (r < 0)
1059 return r;
1060
1061 sd_event_set_watchdog(m->event, true);
1062
1063 sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
1064 sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
1065
1066 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
1067 if (r < 0)
1068 return r;
1069
1070 r = manager_connect_rtnl(m);
1071 if (r < 0)
1072 return r;
1073
1074 r = manager_connect_udev(m);
1075 if (r < 0)
1076 return r;
1077
1078 m->netdevs = hashmap_new(&string_hash_ops);
1079 if (!m->netdevs)
1080 return -ENOMEM;
1081
1082 LIST_HEAD_INIT(m->networks);
1083
1084 r = setup_default_address_pool(m);
1085 if (r < 0)
1086 return r;
1087
1088 m->duid.type = DUID_TYPE_EN;
1089
1090 *ret = m;
1091 m = NULL;
1092
1093 return 0;
1094 }
1095
1096 void manager_free(Manager *m) {
1097 Network *network;
1098 NetDev *netdev;
1099 Link *link;
1100 AddressPool *pool;
1101
1102 if (!m)
1103 return;
1104
1105 free(m->state_file);
1106
1107 while ((link = hashmap_first(m->links)))
1108 link_unref(link);
1109 hashmap_free(m->links);
1110
1111 while ((network = m->networks))
1112 network_free(network);
1113
1114 hashmap_free(m->networks_by_name);
1115
1116 while ((netdev = hashmap_first(m->netdevs)))
1117 netdev_unref(netdev);
1118 hashmap_free(m->netdevs);
1119
1120 while ((pool = m->address_pools))
1121 address_pool_free(pool);
1122
1123 sd_netlink_unref(m->rtnl);
1124 sd_event_unref(m->event);
1125
1126 sd_event_source_unref(m->udev_event_source);
1127 udev_monitor_unref(m->udev_monitor);
1128 udev_unref(m->udev);
1129
1130 sd_bus_unref(m->bus);
1131 sd_bus_slot_unref(m->prepare_for_sleep_slot);
1132 sd_event_source_unref(m->bus_retry_event_source);
1133
1134 free(m->dynamic_timezone);
1135 free(m->dynamic_hostname);
1136
1137 free(m);
1138 }
1139
1140 static bool manager_check_idle(void *userdata) {
1141 Manager *m = userdata;
1142 Link *link;
1143 Iterator i;
1144
1145 assert(m);
1146
1147 /* Check whether we are idle now. The only case when we decide to be idle is when there's only a loopback
1148 * device around, for which we have no configuration, and which already left the PENDING state. In all other
1149 * cases we are not idle. */
1150
1151 HASHMAP_FOREACH(link, m->links, i) {
1152 /* We are not woken on udev activity, so let's just wait for the pending udev event */
1153 if (link->state == LINK_STATE_PENDING)
1154 return false;
1155
1156 if ((link->flags & IFF_LOOPBACK) == 0)
1157 return false;
1158
1159 if (link->network)
1160 return false;
1161 }
1162
1163 return true;
1164 }
1165
1166 int manager_run(Manager *m) {
1167 Link *link;
1168 Iterator i;
1169
1170 assert(m);
1171
1172 /* The dirty handler will deal with future serialization, but the first one
1173 must be done explicitly. */
1174
1175 manager_save(m);
1176
1177 HASHMAP_FOREACH(link, m->links, i)
1178 link_save(link);
1179
1180 if (m->bus)
1181 return bus_event_loop_with_idle(
1182 m->event,
1183 m->bus,
1184 "org.freedesktop.network1",
1185 DEFAULT_EXIT_USEC,
1186 manager_check_idle,
1187 m);
1188 else
1189 /* failed to connect to the bus, so we lose exit-on-idle logic,
1190 this should not happen except if dbus is not around at all */
1191 return sd_event_loop(m->event);
1192 }
1193
1194 int manager_load_config(Manager *m) {
1195 int r;
1196
1197 /* update timestamp */
1198 paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
1199
1200 r = netdev_load(m);
1201 if (r < 0)
1202 return r;
1203
1204 r = network_load(m);
1205 if (r < 0)
1206 return r;
1207
1208 return 0;
1209 }
1210
1211 bool manager_should_reload(Manager *m) {
1212 return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
1213 }
1214
1215 int manager_rtnl_enumerate_links(Manager *m) {
1216 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1217 sd_netlink_message *link;
1218 int r;
1219
1220 assert(m);
1221 assert(m->rtnl);
1222
1223 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
1224 if (r < 0)
1225 return r;
1226
1227 r = sd_netlink_message_request_dump(req, true);
1228 if (r < 0)
1229 return r;
1230
1231 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1232 if (r < 0)
1233 return r;
1234
1235 for (link = reply; link; link = sd_netlink_message_next(link)) {
1236 int k;
1237
1238 m->enumerating = true;
1239
1240 k = manager_rtnl_process_link(m->rtnl, link, m);
1241 if (k < 0)
1242 r = k;
1243
1244 m->enumerating = false;
1245 }
1246
1247 return r;
1248 }
1249
1250 int manager_rtnl_enumerate_addresses(Manager *m) {
1251 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1252 sd_netlink_message *addr;
1253 int r;
1254
1255 assert(m);
1256 assert(m->rtnl);
1257
1258 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
1259 if (r < 0)
1260 return r;
1261
1262 r = sd_netlink_message_request_dump(req, true);
1263 if (r < 0)
1264 return r;
1265
1266 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1267 if (r < 0)
1268 return r;
1269
1270 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
1271 int k;
1272
1273 m->enumerating = true;
1274
1275 k = manager_rtnl_process_address(m->rtnl, addr, m);
1276 if (k < 0)
1277 r = k;
1278
1279 m->enumerating = false;
1280 }
1281
1282 return r;
1283 }
1284
1285 int manager_rtnl_enumerate_routes(Manager *m) {
1286 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1287 sd_netlink_message *route;
1288 int r;
1289
1290 assert(m);
1291 assert(m->rtnl);
1292
1293 r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0);
1294 if (r < 0)
1295 return r;
1296
1297 r = sd_netlink_message_request_dump(req, true);
1298 if (r < 0)
1299 return r;
1300
1301 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1302 if (r < 0)
1303 return r;
1304
1305 for (route = reply; route; route = sd_netlink_message_next(route)) {
1306 int k;
1307
1308 m->enumerating = true;
1309
1310 k = manager_rtnl_process_route(m->rtnl, route, m);
1311 if (k < 0)
1312 r = k;
1313
1314 m->enumerating = false;
1315 }
1316
1317 return r;
1318 }
1319
1320 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
1321 AddressPool *p;
1322 int r;
1323
1324 assert(m);
1325 assert(prefixlen > 0);
1326 assert(found);
1327
1328 LIST_FOREACH(address_pools, p, m->address_pools) {
1329 if (p->family != family)
1330 continue;
1331
1332 r = address_pool_acquire(p, prefixlen, found);
1333 if (r != 0)
1334 return r;
1335 }
1336
1337 return 0;
1338 }
1339
1340 Link* manager_find_uplink(Manager *m, Link *exclude) {
1341 _cleanup_free_ struct local_address *gateways = NULL;
1342 int n, i;
1343
1344 assert(m);
1345
1346 /* Looks for a suitable "uplink", via black magic: an
1347 * interface that is up and where the default route with the
1348 * highest priority points to. */
1349
1350 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
1351 if (n < 0) {
1352 log_warning_errno(n, "Failed to determine list of default gateways: %m");
1353 return NULL;
1354 }
1355
1356 for (i = 0; i < n; i++) {
1357 Link *link;
1358
1359 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
1360 if (!link) {
1361 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
1362 continue;
1363 }
1364
1365 if (link == exclude)
1366 continue;
1367
1368 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
1369 continue;
1370
1371 return link;
1372 }
1373
1374 return NULL;
1375 }
1376
1377 void manager_dirty(Manager *manager) {
1378 assert(manager);
1379
1380 /* the serialized state in /run is no longer up-to-date */
1381 manager->dirty = true;
1382 }
1383
1384 static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
1385 Manager *manager = userdata;
1386 const sd_bus_error *e;
1387
1388 assert(m);
1389 assert(manager);
1390
1391 e = sd_bus_message_get_error(m);
1392 if (e)
1393 log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message);
1394
1395 return 1;
1396 }
1397
1398 int manager_set_hostname(Manager *m, const char *hostname) {
1399 int r;
1400
1401 log_debug("Setting transient hostname: '%s'", strna(hostname));
1402 if (free_and_strdup(&m->dynamic_hostname, hostname) < 0)
1403 return log_oom();
1404
1405 if (!m->bus) {
1406 /* TODO: replace by assert when we can rely on kdbus */
1407 log_info("Not connected to system bus, ignoring transient hostname.");
1408 return 0;
1409 }
1410
1411 r = sd_bus_call_method_async(
1412 m->bus,
1413 NULL,
1414 "org.freedesktop.hostname1",
1415 "/org/freedesktop/hostname1",
1416 "org.freedesktop.hostname1",
1417 "SetHostname",
1418 set_hostname_handler,
1419 m,
1420 "sb",
1421 hostname,
1422 false);
1423
1424 if (r < 0)
1425 return log_error_errno(r, "Could not set transient hostname: %m");
1426
1427 return 0;
1428 }
1429
1430 static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
1431 Manager *manager = userdata;
1432 const sd_bus_error *e;
1433
1434 assert(m);
1435 assert(manager);
1436
1437 e = sd_bus_message_get_error(m);
1438 if (e)
1439 log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message);
1440
1441 return 1;
1442 }
1443
1444 int manager_set_timezone(Manager *m, const char *tz) {
1445 int r;
1446
1447 assert(m);
1448 assert(tz);
1449
1450 log_debug("Setting system timezone: '%s'", tz);
1451 if (free_and_strdup(&m->dynamic_timezone, tz) < 0)
1452 return log_oom();
1453
1454 if (!m->bus) {
1455 log_info("Not connected to system bus, ignoring timezone.");
1456 return 0;
1457 }
1458
1459 r = sd_bus_call_method_async(
1460 m->bus,
1461 NULL,
1462 "org.freedesktop.timedate1",
1463 "/org/freedesktop/timedate1",
1464 "org.freedesktop.timedate1",
1465 "SetTimezone",
1466 set_timezone_handler,
1467 m,
1468 "sb",
1469 tz,
1470 false);
1471 if (r < 0)
1472 return log_error_errno(r, "Could not set timezone: %m");
1473
1474 return 0;
1475 }