]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-manager.c
Merge pull request #6922 from poettering/symlink-sockets
[thirdparty/systemd.git] / src / network / networkd-manager.c
CommitLineData
f579559b
TG
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
091a364c 20#include <sys/socket.h>
bbf7c048 21#include <linux/if.h>
bce67bbe 22#include <linux/fib_rules.h>
3bef724f 23
fc2f9534 24#include "sd-daemon.h"
07630cea 25#include "sd-netlink.h"
fc2f9534 26
b5efdb8a 27#include "alloc-util.h"
a97dcc12 28#include "bus-util.h"
07630cea 29#include "conf-parser.h"
a97dcc12 30#include "def.h"
482d1aeb 31#include "dns-domain.h"
3ffd4af2 32#include "fd-util.h"
0d39fa9c 33#include "fileio.h"
07630cea 34#include "libudev-private.h"
4f5f911e 35#include "local-addresses.h"
07630cea 36#include "netlink-util.h"
23f53b99 37#include "networkd-manager.h"
00616955 38#include "ordered-set.h"
07630cea
LP
39#include "path-util.h"
40#include "set.h"
41#include "udev-util.h"
42#include "virt.h"
505f8da7 43
be660c37
AR
44/* use 8 MB for receive socket kernel queue. */
45#define RCVBUF_SIZE (8*1024*1024)
46
2ad8416d
ZJS
47const char* const network_dirs[] = {
48 "/etc/systemd/network",
49 "/run/systemd/network",
50 "/usr/lib/systemd/network",
eed0eee8 51#ifdef HAVE_SPLIT_USR
2ad8416d
ZJS
52 "/lib/systemd/network",
53#endif
54 NULL};
55
11bf3cce
LP
56static int setup_default_address_pool(Manager *m) {
57 AddressPool *p;
58 int r;
59
60 assert(m);
61
62 /* Add in the well-known private address ranges. */
63
64 r = address_pool_new_from_string(m, &p, AF_INET6, "fc00::", 7);
65 if (r < 0)
66 return r;
67
68 r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
69 if (r < 0)
70 return r;
71
72 r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
73 if (r < 0)
74 return r;
75
76 r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
77 if (r < 0)
78 return r;
79
80 return 0;
81}
82
9c0a72f9
TG
83static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
84 Manager *m = userdata;
85
86 assert(s);
87 assert(m);
88
89 m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
90
91 manager_connect_bus(m);
92
93 return 0;
94}
95
96static int manager_reset_all(Manager *m) {
97 Link *link;
98 Iterator i;
99 int r;
100
101 assert(m);
102
103 HASHMAP_FOREACH(link, m->links, i) {
104 r = link_carrier_reset(link);
105 if (r < 0)
2f3cf1f9 106 log_link_warning_errno(link, r, "Could not reset carrier: %m");
9c0a72f9
TG
107 }
108
109 return 0;
110}
111
19070062 112static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
9c0a72f9
TG
113 Manager *m = userdata;
114 int b, r;
115
19070062 116 assert(message);
9c0a72f9
TG
117
118 r = sd_bus_message_read(message, "b", &b);
119 if (r < 0) {
120 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
121 return 0;
122 }
123
124 if (b)
125 return 0;
126
127 log_debug("Coming back from suspend, resetting all connections...");
128
129 manager_reset_all(m);
130
131 return 0;
132}
133
134int manager_connect_bus(Manager *m) {
135 int r;
136
137 assert(m);
138
139 r = sd_bus_default_system(&m->bus);
fb72b1d9 140 if (r < 0) {
9c0a72f9 141 /* We failed to connect? Yuck, we must be in early
d7ea7bb8 142 * boot. Let's try in 5s again. */
9c0a72f9
TG
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;
7d6884b6
TA
151 }
152
153 if (r < 0)
9c0a72f9
TG
154 return r;
155
9c0a72f9
TG
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
e331e246
TG
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");
3175fcde
TG
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");
e331e246
TG
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
7901cea1
MP
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
9c0a72f9
TG
207 return 0;
208}
209
5fae368b
TG
210static int manager_udev_process_link(Manager *m, struct udev_device *device) {
211 Link *link = NULL;
212 int r, ifindex;
5544ee85 213
5fae368b
TG
214 assert(m);
215 assert(device);
5544ee85 216
5fae368b
TG
217 if (!streq_ptr(udev_device_get_action(device), "add"))
218 return 0;
5544ee85 219
5fae368b
TG
220 ifindex = udev_device_get_ifindex(device);
221 if (ifindex <= 0) {
2f3cf1f9 222 log_debug("Ignoring udev ADD event for device with invalid ifindex");
5fae368b 223 return 0;
5544ee85
TG
224 }
225
5fae368b
TG
226 r = link_get(m, ifindex, &link);
227 if (r == -ENODEV)
228 return 0;
229 else if (r < 0)
f579559b
TG
230 return r;
231
5fae368b 232 r = link_initialized(link, device);
44de0efc
LP
233 if (r < 0)
234 return r;
235
5fae368b
TG
236 return 0;
237}
be660c37 238
5fae368b
TG
239static 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;
f579559b 243
5fae368b
TG
244 device = udev_monitor_receive_device(monitor);
245 if (!device)
02b59d57
TG
246 return -ENOMEM;
247
5fae368b 248 manager_udev_process_link(m, device);
f579559b
TG
249 return 0;
250}
251
5fae368b
TG
252static int manager_connect_udev(Manager *m) {
253 int r;
f579559b 254
5fae368b
TG
255 /* udev does not initialize devices inside containers,
256 * so we rely on them being already initialized before
257 * entering the container */
75f86906 258 if (detect_container() > 0)
5fae368b 259 return 0;
f579559b 260
5fae368b
TG
261 m->udev = udev_new();
262 if (!m->udev)
263 return -ENOMEM;
02b59d57 264
5fae368b
TG
265 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
266 if (!m->udev_monitor)
267 return -ENOMEM;
02b59d57 268
5fae368b 269 r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
02b59d57 270 if (r < 0)
5fae368b 271 return log_error_errno(r, "Could not add udev monitor filter: %m");
02b59d57 272
5fae368b
TG
273 r = udev_monitor_enable_receiving(m->udev_monitor);
274 if (r < 0) {
275 log_error("Could not enable udev monitor");
02b59d57 276 return r;
667fcc6d 277 }
505f8da7 278
5fae368b
TG
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)
667fcc6d 285 return r;
505f8da7 286
5fae368b 287 r = sd_event_source_set_description(m->udev_event_source, "networkd-udev");
505f8da7
TG
288 if (r < 0)
289 return r;
11a7f229 290
505f8da7
TG
291 return 0;
292}
f579559b 293
1c8e710c
TG
294int 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;
983226f3 299 unsigned char protocol, scope, tos, table, rt_type;
1c8e710c
TG
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
983226f3
SS
444 r = sd_rtnl_message_route_get_type(message, &rt_type);
445 if (r < 0) {
446 log_link_warning_errno(link, r, "rtnl: received route with invalid type, ignoring: %m");
447 return 0;
448 }
449
1c8e710c
TG
450 r = sd_rtnl_message_route_get_table(message, &table);
451 if (r < 0) {
452 log_link_warning_errno(link, r, "rtnl: received route with invalid table, ignoring: %m");
453 return 0;
454 }
455
456 r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &priority);
457 if (r < 0 && r != -ENODATA) {
458 log_link_warning_errno(link, r, "rtnl: received route with invalid priority, ignoring: %m");
459 return 0;
460 }
461
462 route_get(link, family, &dst, dst_prefixlen, tos, priority, table, &route);
463
464 switch (type) {
465 case RTM_NEWROUTE:
466 if (!route) {
467 /* A route appeared that we did not request */
468 r = route_add_foreign(link, family, &dst, dst_prefixlen, tos, priority, table, &route);
469 if (r < 0)
470 return 0;
471 }
472
983226f3 473 route_update(route, &src, src_prefixlen, &gw, &prefsrc, scope, rt_type, protocol);
1c8e710c
TG
474
475 break;
476
477 case RTM_DELROUTE:
b9642f41 478 route_free(route);
1c8e710c 479 break;
b9642f41 480
1c8e710c
TG
481 default:
482 assert_not_reached("Received invalid RTNL message type");
483 }
484
485 return 1;
486}
487
200a0868
TG
488int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
489 Manager *m = userdata;
490 Link *link = NULL;
491 uint16_t type;
200a0868 492 unsigned char flags;
054f0db4
TG
493 int family;
494 unsigned char prefixlen;
495 unsigned char scope;
496 union in_addr_union in_addr;
497 struct ifa_cacheinfo cinfo;
498 Address *address = NULL;
200a0868
TG
499 char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX];
500 const char *valid_str = NULL;
501 int r, ifindex;
502
503 assert(rtnl);
504 assert(message);
505 assert(m);
506
507 if (sd_netlink_message_is_error(message)) {
508 r = sd_netlink_message_get_errno(message);
509 if (r < 0)
510 log_warning_errno(r, "rtnl: failed to receive address: %m");
511
512 return 0;
513 }
514
515 r = sd_netlink_message_get_type(message, &type);
516 if (r < 0) {
517 log_warning_errno(r, "rtnl: could not get message type: %m");
518 return 0;
519 } else if (type != RTM_NEWADDR && type != RTM_DELADDR) {
520 log_warning("rtnl: received unexpected message type when processing address");
521 return 0;
522 }
523
524 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
525 if (r < 0) {
526 log_warning_errno(r, "rtnl: could not get ifindex from address: %m");
527 return 0;
528 } else if (ifindex <= 0) {
529 log_warning("rtnl: received address message with invalid ifindex: %d", ifindex);
530 return 0;
531 } else {
532 r = link_get(m, ifindex, &link);
533 if (r < 0 || !link) {
534 /* when enumerating we might be out of sync, but we will
535 * get the address again, so just ignore it */
536 if (!m->enumerating)
537 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex);
538 return 0;
539 }
540 }
541
054f0db4
TG
542 r = sd_rtnl_message_addr_get_family(message, &family);
543 if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) {
200a0868
TG
544 log_link_warning(link, "rtnl: received address with invalid family, ignoring.");
545 return 0;
546 }
547
054f0db4 548 r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen);
200a0868
TG
549 if (r < 0) {
550 log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m");
551 return 0;
552 }
553
054f0db4 554 r = sd_rtnl_message_addr_get_scope(message, &scope);
200a0868
TG
555 if (r < 0) {
556 log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m");
557 return 0;
558 }
559
560 r = sd_rtnl_message_addr_get_flags(message, &flags);
561 if (r < 0) {
562 log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m");
563 return 0;
564 }
200a0868 565
054f0db4 566 switch (family) {
200a0868 567 case AF_INET:
054f0db4 568 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in);
200a0868
TG
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 case AF_INET6:
054f0db4 577 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6);
200a0868
TG
578 if (r < 0) {
579 log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m");
580 return 0;
581 }
582
583 break;
584
585 default:
1c8e710c 586 log_link_debug(link, "rtnl: ignoring unsupported address family: %d", family);
200a0868
TG
587 }
588
054f0db4 589 if (!inet_ntop(family, &in_addr, buf, INET6_ADDRSTRLEN)) {
200a0868
TG
590 log_link_warning(link, "Could not print address");
591 return 0;
592 }
593
054f0db4 594 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
200a0868 595 if (r >= 0) {
4058d339 596 if (cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME)
200a0868 597 valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
054f0db4 598 cinfo.ifa_valid * USEC_PER_SEC,
200a0868
TG
599 USEC_PER_SEC);
600 }
601
054f0db4 602 address_get(link, family, &in_addr, prefixlen, &address);
200a0868
TG
603
604 switch (type) {
605 case RTM_NEWADDR:
36c32f61 606 if (address)
4058d339
TG
607 log_link_debug(link, "Updating address: %s/%u (valid %s%s)", buf, prefixlen,
608 valid_str ? "for " : "forever", valid_str ?: "");
36c32f61 609 else {
adda1ed9
TG
610 /* An address appeared that we did not request */
611 r = address_add_foreign(link, family, &in_addr, prefixlen, &address);
cf1d700d 612 if (r < 0) {
054f0db4 613 log_link_warning_errno(link, r, "Failed to add address %s/%u: %m", buf, prefixlen);
cf1d700d
TG
614 return 0;
615 } else
4058d339
TG
616 log_link_debug(link, "Adding address: %s/%u (valid %s%s)", buf, prefixlen,
617 valid_str ? "for " : "forever", valid_str ?: "");
200a0868
TG
618 }
619
58fda79c 620 address_update(address, flags, scope, &cinfo);
36c32f61 621
200a0868
TG
622 break;
623
624 case RTM_DELADDR:
625
054f0db4 626 if (address) {
4058d339
TG
627 log_link_debug(link, "Removing address: %s/%u (valid %s%s)", buf, prefixlen,
628 valid_str ? "for " : "forever", valid_str ?: "");
91b5f997 629 address_drop(address);
200a0868 630 } else
4058d339
TG
631 log_link_warning(link, "Removing non-existent address: %s/%u (valid %s%s)", buf, prefixlen,
632 valid_str ? "for " : "forever", valid_str ?: "");
200a0868
TG
633
634 break;
635 default:
636 assert_not_reached("Received invalid RTNL message type");
637 }
638
639 return 1;
640}
641
1c4baffc 642static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
505f8da7
TG
643 Manager *m = userdata;
644 Link *link = NULL;
4d473d5d 645 NetDev *netdev = NULL;
f2236469 646 uint16_t type;
ca4e095a 647 const char *name;
505f8da7 648 int r, ifindex;
f579559b 649
505f8da7
TG
650 assert(rtnl);
651 assert(message);
f579559b
TG
652 assert(m);
653
1c4baffc
TG
654 if (sd_netlink_message_is_error(message)) {
655 r = sd_netlink_message_get_errno(message);
45af44d4 656 if (r < 0)
2f3cf1f9 657 log_warning_errno(r, "rtnl: Could not receive link: %m");
45af44d4
TG
658
659 return 0;
660 }
661
1c4baffc 662 r = sd_netlink_message_get_type(message, &type);
f2236469 663 if (r < 0) {
2f3cf1f9 664 log_warning_errno(r, "rtnl: Could not get message type: %m");
f2236469 665 return 0;
cdfee943 666 } else if (type != RTM_NEWLINK && type != RTM_DELLINK) {
2f3cf1f9 667 log_warning("rtnl: Received unexpected message type when processing link");
cdfee943 668 return 0;
f2236469
TG
669 }
670
505f8da7 671 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
45af44d4 672 if (r < 0) {
2f3cf1f9 673 log_warning_errno(r, "rtnl: Could not get ifindex from link: %m");
45af44d4
TG
674 return 0;
675 } else if (ifindex <= 0) {
676 log_warning("rtnl: received link message with invalid ifindex: %d", ifindex);
505f8da7 677 return 0;
a0db8e46 678 }
f579559b 679
1c4baffc 680 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
45af44d4 681 if (r < 0) {
2f3cf1f9 682 log_warning_errno(r, "rtnl: Received link message without ifname: %m");
4d473d5d 683 return 0;
a0db8e46
ZJS
684 }
685
686 (void) link_get(m, ifindex, &link);
687 (void) netdev_get(m, name, &netdev);
4d473d5d
TG
688
689 switch (type) {
690 case RTM_NEWLINK:
691 if (!link) {
692 /* link is new, so add it */
693 r = link_add(m, message, &link);
694 if (r < 0) {
2f3cf1f9 695 log_warning_errno(r, "Could not add new link: %m");
4d473d5d
TG
696 return 0;
697 }
698 }
699
700 if (netdev) {
701 /* netdev exists, so make sure the ifindex matches */
505f8da7
TG
702 r = netdev_set_ifindex(netdev, message);
703 if (r < 0) {
2f3cf1f9 704 log_warning_errno(r, "Could not set ifindex on netdev: %m");
505f8da7
TG
705 return 0;
706 }
707 }
e1202047 708
f2236469
TG
709 r = link_update(link, message);
710 if (r < 0)
711 return 0;
4d473d5d
TG
712
713 break;
714
715 case RTM_DELLINK:
716 link_drop(link);
717 netdev_drop(netdev);
718
719 break;
720
721 default:
722 assert_not_reached("Received invalid RTNL message type.");
f2236469 723 }
505f8da7
TG
724
725 return 1;
726}
727
bce67bbe
SS
728int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
729 uint8_t tos = 0, to_prefixlen = 0, from_prefixlen = 0;
730 RoutingPolicyRule *rule = NULL;
731 union in_addr_union to, from;
732 uint32_t fwmark = 0, table = 0;
733 Manager *m = userdata;
734 uint16_t type;
735 int family;
736 int r;
737
738 assert(rtnl);
739 assert(message);
740 assert(m);
741
742 if (sd_netlink_message_is_error(message)) {
743 r = sd_netlink_message_get_errno(message);
744 if (r < 0)
745 log_warning_errno(r, "rtnl: failed to receive rule: %m");
746
747 return 0;
748 }
749
750 r = sd_netlink_message_get_type(message, &type);
751 if (r < 0) {
752 log_warning_errno(r, "rtnl: could not get message type: %m");
753 return 0;
754 } else if (!IN_SET(type, RTM_NEWRULE, RTM_DELRULE)) {
755 log_warning("rtnl: received unexpected message type '%u' when processing rule.", type);
756 return 0;
757 }
758
759 r = sd_rtnl_message_get_family(message, &family);
615ded62
YW
760 if (r < 0) {
761 log_warning_errno(r, "rtnl: could not get rule family: %m");
762 return 0;
763 } else if (!IN_SET(family, AF_INET, AF_INET6)) {
764 log_debug("rtnl: received address with invalid family %u, ignoring.", family);
bce67bbe
SS
765 return 0;
766 }
767
768 switch (family) {
769 case AF_INET:
770 r = sd_netlink_message_read_in_addr(message, FRA_SRC, &from.in);
771 if (r >= 0) {
772 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &from_prefixlen);
773 if (r < 0)
774 log_warning_errno(r, "rtnl: failed to retrive rule from prefix length: %m");
775 }
776
777 r = sd_netlink_message_read_in_addr(message, FRA_DST, &to.in);
778 if (r >= 0) {
779 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &to_prefixlen);
780 if (r < 0)
781 log_warning_errno(r, "rtnl: failed to retrive rule to prefix length: %m");
782 }
783
784 break;
785
786 case AF_INET6:
787 r = sd_netlink_message_read_in6_addr(message, FRA_SRC, &from.in6);
788 if (r >= 0) {
789 r = sd_rtnl_message_routing_policy_rule_get_rtm_src_prefixlen(message, &from_prefixlen);
790 if (r < 0)
791 log_warning_errno(r, "rtnl: failed to retrive rule from prefix length: %m");
792 }
793
794 r = sd_netlink_message_read_in6_addr(message, FRA_DST, &to.in6);
795 if (r >= 0) {
796 r = sd_rtnl_message_routing_policy_rule_get_rtm_dst_prefixlen(message, &to_prefixlen);
797 if (r < 0)
798 log_warning_errno(r, "rtnl: failed to retrive rule to prefix length: %m");
799 }
800
801 break;
802
803 default:
615ded62 804 assert_not_reached("Received unsupported rule family");
bce67bbe
SS
805 }
806
807 if (from_prefixlen == 0 && to_prefixlen == 0)
808 return 0;
809
810 (void) sd_netlink_message_read_u32(message, FRA_FWMARK, &fwmark);
811 (void) sd_netlink_message_read_u32(message, FRA_TABLE, &table);
812 (void) sd_rtnl_message_routing_policy_rule_get_tos(message, &tos);
813
814 (void) routing_policy_rule_get(m, family, &from, from_prefixlen, &to, to_prefixlen, tos, fwmark, table, &rule);
815
816 switch (type) {
817 case RTM_NEWRULE:
818 if(!rule) {
819 r = routing_policy_rule_add_foreign(m, family, &from, from_prefixlen, &to, to_prefixlen, tos, fwmark, table, &rule);
820 if (r < 0) {
821 log_warning_errno(r, "Could not add rule: %m");
822 return 0;
823 }
824 }
825 break;
826 case RTM_DELRULE:
827 routing_policy_rule_free(rule);
828
829 break;
830
831 default:
832 assert_not_reached("Received invalid RTNL message type");
833 }
834
835 return 1;
836}
837
5fae368b
TG
838static int systemd_netlink_fd(void) {
839 int n, fd, rtnl_fd = -EINVAL;
840
841 n = sd_listen_fds(true);
842 if (n <= 0)
843 return -EINVAL;
844
845 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
846 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
847 if (rtnl_fd >= 0)
848 return -EINVAL;
849
850 rtnl_fd = fd;
851 }
852 }
853
854 return rtnl_fd;
855}
856
857static int manager_connect_rtnl(Manager *m) {
858 int fd, r;
505f8da7
TG
859
860 assert(m);
505f8da7 861
5fae368b
TG
862 fd = systemd_netlink_fd();
863 if (fd < 0)
1c4baffc 864 r = sd_netlink_open(&m->rtnl);
5fae368b 865 else
1c4baffc 866 r = sd_netlink_open_fd(&m->rtnl, fd);
505f8da7
TG
867 if (r < 0)
868 return r;
869
1c4baffc 870 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
f579559b 871 if (r < 0)
bf5332d2 872 return r;
f579559b 873
1c4baffc 874 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
505f8da7
TG
875 if (r < 0)
876 return r;
f579559b 877
1c4baffc 878 r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
5fae368b
TG
879 if (r < 0)
880 return r;
505f8da7 881
1c4baffc 882 r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
5fae368b
TG
883 if (r < 0)
884 return r;
45af44d4 885
200a0868 886 r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m);
5fae368b
TG
887 if (r < 0)
888 return r;
889
200a0868 890 r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m);
5fae368b
TG
891 if (r < 0)
892 return r;
893
1c8e710c
TG
894 r = sd_netlink_add_match(m->rtnl, RTM_NEWROUTE, &manager_rtnl_process_route, m);
895 if (r < 0)
896 return r;
897
898 r = sd_netlink_add_match(m->rtnl, RTM_DELROUTE, &manager_rtnl_process_route, m);
899 if (r < 0)
900 return r;
901
bce67bbe
SS
902 r = sd_netlink_add_match(m->rtnl, RTM_NEWRULE, &manager_rtnl_process_rule, m);
903 if (r < 0)
904 return r;
905
906 r = sd_netlink_add_match(m->rtnl, RTM_DELRULE, &manager_rtnl_process_rule, m);
907 if (r < 0)
908 return r;
909
5fae368b 910 return 0;
45af44d4 911}
505f8da7 912
5512a963 913static int ordered_set_put_in_addr_data(OrderedSet *s, const struct in_addr_data *address) {
84de38c5
TG
914 char *p;
915 int r;
916
917 assert(s);
5512a963
LP
918 assert(address);
919
920 r = in_addr_to_string(address->family, &address->address, &p);
921 if (r < 0)
922 return r;
923
924 r = ordered_set_consume(s, p);
925 if (r == -EEXIST)
926 return 0;
927
928 return r;
929}
930
931static int ordered_set_put_in_addr_datav(OrderedSet *s, const struct in_addr_data *addresses, unsigned n) {
932 int r, c = 0;
933 unsigned i;
934
935 assert(s);
936 assert(addresses || n == 0);
937
938 for (i = 0; i < n; i++) {
939 r = ordered_set_put_in_addr_data(s, addresses+i);
940 if (r < 0)
941 return r;
942
943 c += r;
944 }
945
946 return c;
947}
948
949static int ordered_set_put_in4_addr(OrderedSet *s, const struct in_addr *address) {
950 char *p;
951 int r;
952
953 assert(s);
954 assert(address);
84de38c5
TG
955
956 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
957 if (r < 0)
958 return r;
959
00616955 960 r = ordered_set_consume(s, p);
84de38c5
TG
961 if (r == -EEXIST)
962 return 0;
963
964 return r;
965}
966
5512a963
LP
967static int ordered_set_put_in4_addrv(OrderedSet *s, const struct in_addr *addresses, unsigned n) {
968 int r, c = 0;
969 unsigned i;
84de38c5
TG
970
971 assert(s);
5512a963 972 assert(n == 0 || addresses);
84de38c5
TG
973
974 for (i = 0; i < n; i++) {
5512a963 975 r = ordered_set_put_in4_addr(s, addresses+i);
84de38c5
TG
976 if (r < 0)
977 return r;
978
979 c += r;
980 }
981
982 return c;
983}
984
00616955 985static void print_string_set(FILE *f, const char *field, OrderedSet *s) {
84de38c5
TG
986 bool space = false;
987 Iterator i;
988 char *p;
989
00616955 990 if (ordered_set_isempty(s))
84de38c5
TG
991 return;
992
4b61c875 993 fputs_unlocked(field, f);
84de38c5 994
d390f8ef
LP
995 ORDERED_SET_FOREACH(p, s, i)
996 fputs_with_space(f, p, NULL, &space);
997
4b61c875 998 fputc_unlocked('\n', f);
84de38c5
TG
999}
1000
1001static int manager_save(Manager *m) {
00616955 1002 _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL;
bce67bbe
SS
1003 RoutingPolicyRule *rule = NULL;
1004 bool space = false;
84de38c5
TG
1005 Link *link;
1006 Iterator i;
1007 _cleanup_free_ char *temp_path = NULL;
1008 _cleanup_fclose_ FILE *f = NULL;
1009 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
1010 const char *operstate_str;
1011 int r;
1012
1013 assert(m);
1014 assert(m->state_file);
1015
1016 /* We add all NTP and DNS server to a set, to filter out duplicates */
00616955 1017 dns = ordered_set_new(&string_hash_ops);
84de38c5
TG
1018 if (!dns)
1019 return -ENOMEM;
1020
00616955 1021 ntp = ordered_set_new(&string_hash_ops);
84de38c5
TG
1022 if (!ntp)
1023 return -ENOMEM;
1024
482d1aeb 1025 search_domains = ordered_set_new(&dns_name_hash_ops);
3df9bec5
LP
1026 if (!search_domains)
1027 return -ENOMEM;
1028
482d1aeb 1029 route_domains = ordered_set_new(&dns_name_hash_ops);
3df9bec5 1030 if (!route_domains)
84de38c5
TG
1031 return -ENOMEM;
1032
1033 HASHMAP_FOREACH(link, m->links, i) {
1034 if (link->flags & IFF_LOOPBACK)
1035 continue;
1036
1037 if (link->operstate > operstate)
1038 operstate = link->operstate;
1039
1040 if (!link->network)
1041 continue;
1042
1043 /* First add the static configured entries */
5512a963 1044 r = ordered_set_put_in_addr_datav(dns, link->network->dns, link->network->n_dns);
84de38c5
TG
1045 if (r < 0)
1046 return r;
1047
00616955 1048 r = ordered_set_put_strdupv(ntp, link->network->ntp);
84de38c5
TG
1049 if (r < 0)
1050 return r;
1051
00616955 1052 r = ordered_set_put_strdupv(search_domains, link->network->search_domains);
3df9bec5
LP
1053 if (r < 0)
1054 return r;
1055
00616955 1056 r = ordered_set_put_strdupv(route_domains, link->network->route_domains);
84de38c5
TG
1057 if (r < 0)
1058 return r;
1059
1060 if (!link->dhcp_lease)
1061 continue;
1062
1063 /* Secondly, add the entries acquired via DHCP */
27cb34f5 1064 if (link->network->dhcp_use_dns) {
84de38c5
TG
1065 const struct in_addr *addresses;
1066
1067 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
1068 if (r > 0) {
5512a963 1069 r = ordered_set_put_in4_addrv(dns, addresses, r);
84de38c5
TG
1070 if (r < 0)
1071 return r;
1072 } else if (r < 0 && r != -ENODATA)
1073 return r;
1074 }
1075
27cb34f5 1076 if (link->network->dhcp_use_ntp) {
84de38c5
TG
1077 const struct in_addr *addresses;
1078
1079 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
1080 if (r > 0) {
5512a963 1081 r = ordered_set_put_in4_addrv(ntp, addresses, r);
84de38c5
TG
1082 if (r < 0)
1083 return r;
1084 } else if (r < 0 && r != -ENODATA)
1085 return r;
1086 }
1087
b2a81c0b 1088 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
84de38c5 1089 const char *domainname;
b85bc551 1090 char **domains = NULL;
84de38c5 1091
b85bc551 1092 OrderedSet *target_domains = (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES) ? search_domains : route_domains;
84de38c5
TG
1093 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
1094 if (r >= 0) {
b85bc551
DW
1095 r = ordered_set_put_strdup(target_domains, domainname);
1096 if (r < 0)
1097 return r;
1098 } else if (r != -ENODATA)
1099 return r;
b2a81c0b 1100
b85bc551
DW
1101 r = sd_dhcp_lease_get_search_domains(link->dhcp_lease, &domains);
1102 if (r >= 0) {
1103 r = ordered_set_put_strdupv(target_domains, domains);
84de38c5
TG
1104 if (r < 0)
1105 return r;
1106 } else if (r != -ENODATA)
1107 return r;
1108 }
1109 }
1110
1111 operstate_str = link_operstate_to_string(operstate);
1112 assert(operstate_str);
1113
1114 r = fopen_temporary(m->state_file, &f, &temp_path);
1115 if (r < 0)
1116 return r;
1117
5512a963 1118 (void) fchmod(fileno(f), 0644);
84de38c5
TG
1119
1120 fprintf(f,
1121 "# This is private data. Do not parse.\n"
1122 "OPER_STATE=%s\n", operstate_str);
1123
1124 print_string_set(f, "DNS=", dns);
1125 print_string_set(f, "NTP=", ntp);
3df9bec5
LP
1126 print_string_set(f, "DOMAINS=", search_domains);
1127 print_string_set(f, "ROUTE_DOMAINS=", route_domains);
84de38c5 1128
bce67bbe
SS
1129 SET_FOREACH(rule, m->rules, i) {
1130 _cleanup_free_ char *from_str = NULL, *to_str = NULL;
1131 fputs("RULE=", f);
1132
1133 if (!in_addr_is_null(rule->family, &rule->from)) {
1134 r = in_addr_to_string(rule->family, &rule->from, &from_str);
1135 if (r < 0)
1136 goto fail;
1137 }
1138
1139 if (!in_addr_is_null(rule->family, &rule->to)) {
1140 r = in_addr_to_string(rule->family, &rule->to, &to_str);
1141 if (r < 0)
1142 goto fail;
1143 }
1144
1145 fprintf(f, "from=%s%s/%hhu to=%s%s/%hhu tos=%hhu fwmark=%"PRIu32"/%"PRIu32" table=%hhu", space ? " " : "", from_str,
1146 rule->from_prefixlen, space ? " " : "", to_str, rule->to_prefixlen, rule->tos, rule->fwmark, rule->fwmask, rule->table);
1147
1148 fputc('\n', f);
1149 }
1150
84de38c5
TG
1151 r = fflush_and_check(f);
1152 if (r < 0)
1153 goto fail;
1154
1155 if (rename(temp_path, m->state_file) < 0) {
1156 r = -errno;
1157 goto fail;
1158 }
1159
1160 if (m->operational_state != operstate) {
1161 m->operational_state = operstate;
1162 r = manager_send_changed(m, "OperationalState", NULL);
1163 if (r < 0)
1164 log_error_errno(r, "Could not emit changed OperationalState: %m");
1165 }
1166
1167 m->dirty = false;
1168
1169 return 0;
1170
1171fail:
1172 (void) unlink(m->state_file);
1173 (void) unlink(temp_path);
1174
1175 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
1176}
1177
1178static int manager_dirty_handler(sd_event_source *s, void *userdata) {
1179 Manager *m = userdata;
1180 Link *link;
1181 Iterator i;
1182 int r;
1183
1184 assert(m);
1185
1186 if (m->dirty)
1187 manager_save(m);
1188
1189 SET_FOREACH(link, m->dirty_links, i) {
1190 r = link_save(link);
1191 if (r >= 0)
1192 link_clean(link);
1193 }
1194
1195 return 1;
1196}
1197
b76d99d9 1198int manager_new(Manager **ret, sd_event *event) {
5fae368b 1199 _cleanup_manager_free_ Manager *m = NULL;
45af44d4 1200 int r;
f579559b 1201
5fae368b
TG
1202 m = new0(Manager, 1);
1203 if (!m)
1204 return -ENOMEM;
45af44d4 1205
5fae368b
TG
1206 m->state_file = strdup("/run/systemd/netif/state");
1207 if (!m->state_file)
1208 return -ENOMEM;
1209
b76d99d9 1210 m->event = sd_event_ref(event);
5fae368b 1211
84de38c5
TG
1212 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
1213 if (r < 0)
1214 return r;
1215
5fae368b 1216 r = manager_connect_rtnl(m);
45af44d4
TG
1217 if (r < 0)
1218 return r;
1219
5fae368b
TG
1220 r = manager_connect_udev(m);
1221 if (r < 0)
1222 return r;
45af44d4 1223
5fae368b
TG
1224 m->netdevs = hashmap_new(&string_hash_ops);
1225 if (!m->netdevs)
1226 return -ENOMEM;
f579559b 1227
5fae368b 1228 LIST_HEAD_INIT(m->networks);
f579559b 1229
5fae368b
TG
1230 r = setup_default_address_pool(m);
1231 if (r < 0)
1232 return r;
f579559b 1233
8341a5c3 1234 m->duid.type = DUID_TYPE_EN;
413708d1 1235
bce67bbe
SS
1236 (void) routing_policy_rule_load(m);
1237
5fae368b
TG
1238 *ret = m;
1239 m = NULL;
f579559b 1240
f579559b
TG
1241 return 0;
1242}
1243
5fae368b 1244void manager_free(Manager *m) {
bce67bbe 1245 RoutingPolicyRule *rule;
5fae368b
TG
1246 Network *network;
1247 NetDev *netdev;
1248 Link *link;
1249 AddressPool *pool;
f579559b 1250
5fae368b
TG
1251 if (!m)
1252 return;
505f8da7 1253
5fae368b 1254 free(m->state_file);
505f8da7 1255
bce67bbe
SS
1256 while ((network = m->networks))
1257 network_free(network);
1258
5fae368b
TG
1259 while ((link = hashmap_first(m->links)))
1260 link_unref(link);
1261 hashmap_free(m->links);
f579559b 1262
dbffab87
TG
1263 hashmap_free(m->networks_by_name);
1264
5fae368b
TG
1265 while ((netdev = hashmap_first(m->netdevs)))
1266 netdev_unref(netdev);
1267 hashmap_free(m->netdevs);
1268
1269 while ((pool = m->address_pools))
1270 address_pool_free(pool);
1271
bce67bbe
SS
1272 set_free(m->rules);
1273 set_free(m->rules_foreign);
1274
1275 while ((rule = set_steal_first(m->rules_saved)))
1276 free(rule);
1277
1278 set_free(m->rules_saved);
1279
1c4baffc 1280 sd_netlink_unref(m->rtnl);
2f5b4a77 1281 sd_event_unref(m->event);
5fae368b 1282
7d20d375
TG
1283 sd_event_source_unref(m->udev_event_source);
1284 udev_monitor_unref(m->udev_monitor);
1285 udev_unref(m->udev);
1286
1287 sd_bus_unref(m->bus);
1288 sd_bus_slot_unref(m->prepare_for_sleep_slot);
1289 sd_event_source_unref(m->bus_retry_event_source);
1290
7901cea1
MP
1291 free(m->dynamic_timezone);
1292 free(m->dynamic_hostname);
1293
5fae368b
TG
1294 free(m);
1295}
1296
b76d99d9 1297int manager_start(Manager *m) {
84de38c5
TG
1298 Link *link;
1299 Iterator i;
1300
a97dcc12
TG
1301 assert(m);
1302
84de38c5
TG
1303 /* The dirty handler will deal with future serialization, but the first one
1304 must be done explicitly. */
1305
1306 manager_save(m);
1307
1308 HASHMAP_FOREACH(link, m->links, i)
1309 link_save(link);
1310
b76d99d9 1311 return 0;
a97dcc12
TG
1312}
1313
5fae368b
TG
1314int manager_load_config(Manager *m) {
1315 int r;
1316
1317 /* update timestamp */
1318 paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
1319
1320 r = netdev_load(m);
f579559b
TG
1321 if (r < 0)
1322 return r;
1323
5fae368b 1324 r = network_load(m);
9021bb9f
TG
1325 if (r < 0)
1326 return r;
1327
f579559b
TG
1328 return 0;
1329}
f882c247 1330
5fae368b
TG
1331bool manager_should_reload(Manager *m) {
1332 return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
1333}
1334
1335int manager_rtnl_enumerate_links(Manager *m) {
4afd3348 1336 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1c4baffc 1337 sd_netlink_message *link;
f882c247
TG
1338 int r;
1339
5da8149f 1340 assert(m);
5fae368b 1341 assert(m->rtnl);
5da8149f 1342
5fae368b 1343 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
f882c247
TG
1344 if (r < 0)
1345 return r;
1346
1c4baffc 1347 r = sd_netlink_message_request_dump(req, true);
dd3efc09
TG
1348 if (r < 0)
1349 return r;
1350
1c4baffc 1351 r = sd_netlink_call(m->rtnl, req, 0, &reply);
f2236469
TG
1352 if (r < 0)
1353 return r;
1354
1c4baffc 1355 for (link = reply; link; link = sd_netlink_message_next(link)) {
5fae368b 1356 int k;
2e9f08ea 1357
6a24f148
TG
1358 m->enumerating = true;
1359
5fae368b
TG
1360 k = manager_rtnl_process_link(m->rtnl, link, m);
1361 if (k < 0)
1362 r = k;
6a24f148
TG
1363
1364 m->enumerating = false;
5fae368b 1365 }
2e9f08ea 1366
5fae368b 1367 return r;
f882c247 1368}
3bef724f 1369
5fae368b 1370int manager_rtnl_enumerate_addresses(Manager *m) {
4afd3348 1371 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1c4baffc 1372 sd_netlink_message *addr;
1346b1f0
TG
1373 int r;
1374
5fae368b
TG
1375 assert(m);
1376 assert(m->rtnl);
bcbca829 1377
5fae368b
TG
1378 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
1379 if (r < 0)
1380 return r;
bcbca829 1381
1c4baffc 1382 r = sd_netlink_message_request_dump(req, true);
1346b1f0
TG
1383 if (r < 0)
1384 return r;
1385
1c4baffc 1386 r = sd_netlink_call(m->rtnl, req, 0, &reply);
5fae368b
TG
1387 if (r < 0)
1388 return r;
1389
1c4baffc 1390 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
5fae368b
TG
1391 int k;
1392
6a24f148
TG
1393 m->enumerating = true;
1394
200a0868 1395 k = manager_rtnl_process_address(m->rtnl, addr, m);
5fae368b
TG
1396 if (k < 0)
1397 r = k;
6a24f148
TG
1398
1399 m->enumerating = false;
5fae368b
TG
1400 }
1401
1402 return r;
1346b1f0
TG
1403}
1404
1c8e710c 1405int manager_rtnl_enumerate_routes(Manager *m) {
4afd3348 1406 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1c8e710c
TG
1407 sd_netlink_message *route;
1408 int r;
1409
1410 assert(m);
1411 assert(m->rtnl);
1412
1413 r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0);
1414 if (r < 0)
1415 return r;
1416
1417 r = sd_netlink_message_request_dump(req, true);
1418 if (r < 0)
1419 return r;
1420
1421 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1422 if (r < 0)
1423 return r;
1424
1425 for (route = reply; route; route = sd_netlink_message_next(route)) {
1426 int k;
1427
1428 m->enumerating = true;
1429
1430 k = manager_rtnl_process_route(m->rtnl, route, m);
1431 if (k < 0)
1432 r = k;
1433
1434 m->enumerating = false;
1435 }
1436
1437 return r;
1438}
1439
bce67bbe
SS
1440int manager_rtnl_enumerate_rules(Manager *m) {
1441 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1442 sd_netlink_message *rule;
1443 int r;
1444
1445 assert(m);
1446 assert(m->rtnl);
1447
1448 r = sd_rtnl_message_new_routing_policy_rule(m->rtnl, &req, RTM_GETRULE, 0);
1449 if (r < 0)
1450 return r;
1451
1452 r = sd_netlink_message_request_dump(req, true);
1453 if (r < 0)
1454 return r;
1455
1456 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1457 if (r < 0)
1458 return r;
1459
1460 for (rule = reply; rule; rule = sd_netlink_message_next(rule)) {
1461 int k;
1462
1463 m->enumerating = true;
1464
1465 k = manager_rtnl_process_rule(m->rtnl, rule, m);
1466 if (k < 0)
1467 r = k;
1468
1469 m->enumerating = false;
1470 }
1471
1472 return r;
1473}
1474
0dd25fb9 1475int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
11bf3cce
LP
1476 AddressPool *p;
1477 int r;
1478
1479 assert(m);
1480 assert(prefixlen > 0);
1481 assert(found);
1482
1483 LIST_FOREACH(address_pools, p, m->address_pools) {
1484 if (p->family != family)
1485 continue;
1486
1487 r = address_pool_acquire(p, prefixlen, found);
1488 if (r != 0)
1489 return r;
1490 }
1491
1492 return 0;
1493}
4f5f911e
LP
1494
1495Link* manager_find_uplink(Manager *m, Link *exclude) {
1496 _cleanup_free_ struct local_address *gateways = NULL;
1497 int n, i;
1498
1499 assert(m);
1500
1501 /* Looks for a suitable "uplink", via black magic: an
1502 * interface that is up and where the default route with the
1503 * highest priority points to. */
1504
1505 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
1506 if (n < 0) {
1507 log_warning_errno(n, "Failed to determine list of default gateways: %m");
1508 return NULL;
1509 }
1510
1511 for (i = 0; i < n; i++) {
1512 Link *link;
1513
1514 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
1515 if (!link) {
c2c940bd 1516 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
4f5f911e
LP
1517 continue;
1518 }
1519
1520 if (link == exclude)
1521 continue;
1522
1523 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
1524 continue;
1525
1526 return link;
1527 }
1528
1529 return NULL;
1530}
84de38c5
TG
1531
1532void manager_dirty(Manager *manager) {
1533 assert(manager);
1534
1535 /* the serialized state in /run is no longer up-to-date */
1536 manager->dirty = true;
1537}
59eb33e0
MP
1538
1539static int set_hostname_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
1540 Manager *manager = userdata;
1541 const sd_bus_error *e;
1542
1543 assert(m);
1544 assert(manager);
1545
1546 e = sd_bus_message_get_error(m);
1547 if (e)
1548 log_warning_errno(sd_bus_error_get_errno(e), "Could not set hostname: %s", e->message);
1549
1550 return 1;
1551}
1552
1553int manager_set_hostname(Manager *m, const char *hostname) {
1554 int r;
1555
1556 log_debug("Setting transient hostname: '%s'", strna(hostname));
7901cea1
MP
1557 if (free_and_strdup(&m->dynamic_hostname, hostname) < 0)
1558 return log_oom();
59eb33e0
MP
1559
1560 if (!m->bus) {
1561 /* TODO: replace by assert when we can rely on kdbus */
1562 log_info("Not connected to system bus, ignoring transient hostname.");
1563 return 0;
1564 }
1565
1566 r = sd_bus_call_method_async(
1567 m->bus,
1568 NULL,
1569 "org.freedesktop.hostname1",
1570 "/org/freedesktop/hostname1",
1571 "org.freedesktop.hostname1",
1572 "SetHostname",
1573 set_hostname_handler,
1574 m,
1575 "sb",
1576 hostname,
1577 false);
1578
1579 if (r < 0)
1580 return log_error_errno(r, "Could not set transient hostname: %m");
1581
1582 return 0;
1583}
1584
1585static int set_timezone_handler(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
1586 Manager *manager = userdata;
1587 const sd_bus_error *e;
1588
1589 assert(m);
1590 assert(manager);
1591
1592 e = sd_bus_message_get_error(m);
1593 if (e)
1594 log_warning_errno(sd_bus_error_get_errno(e), "Could not set timezone: %s", e->message);
1595
1596 return 1;
1597}
1598
1599int manager_set_timezone(Manager *m, const char *tz) {
1600 int r;
1601
1602 assert(m);
1603 assert(tz);
1604
1605 log_debug("Setting system timezone: '%s'", tz);
7901cea1
MP
1606 if (free_and_strdup(&m->dynamic_timezone, tz) < 0)
1607 return log_oom();
59eb33e0
MP
1608
1609 if (!m->bus) {
1610 log_info("Not connected to system bus, ignoring timezone.");
1611 return 0;
1612 }
1613
1614 r = sd_bus_call_method_async(
1615 m->bus,
1616 NULL,
1617 "org.freedesktop.timedate1",
1618 "/org/freedesktop/timedate1",
1619 "org.freedesktop.timedate1",
1620 "SetTimezone",
1621 set_timezone_handler,
1622 m,
1623 "sb",
1624 tz,
1625 false);
1626 if (r < 0)
1627 return log_error_errno(r, "Could not set timezone: %m");
1628
1629 return 0;
1630}