]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/network/networkd-manager.c
Merge pull request #3247 from fbuihuu/ask-passowrd-on-all-consoles
[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>
3bef724f 22
fc2f9534 23#include "sd-daemon.h"
07630cea 24#include "sd-netlink.h"
fc2f9534 25
b5efdb8a 26#include "alloc-util.h"
a97dcc12 27#include "bus-util.h"
07630cea 28#include "conf-parser.h"
a97dcc12 29#include "def.h"
482d1aeb 30#include "dns-domain.h"
3ffd4af2 31#include "fd-util.h"
0d39fa9c 32#include "fileio.h"
07630cea 33#include "libudev-private.h"
4f5f911e 34#include "local-addresses.h"
07630cea 35#include "netlink-util.h"
fc2f9534 36#include "networkd.h"
00616955 37#include "ordered-set.h"
07630cea
LP
38#include "path-util.h"
39#include "set.h"
40#include "udev-util.h"
41#include "virt.h"
505f8da7 42
be660c37
AR
43/* use 8 MB for receive socket kernel queue. */
44#define RCVBUF_SIZE (8*1024*1024)
45
2ad8416d
ZJS
46const char* const network_dirs[] = {
47 "/etc/systemd/network",
48 "/run/systemd/network",
49 "/usr/lib/systemd/network",
eed0eee8 50#ifdef HAVE_SPLIT_USR
2ad8416d
ZJS
51 "/lib/systemd/network",
52#endif
53 NULL};
54
11bf3cce
LP
55static 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
9c0a72f9
TG
82static 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
95static 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)
2f3cf1f9 105 log_link_warning_errno(link, r, "Could not reset carrier: %m");
9c0a72f9
TG
106 }
107
108 return 0;
109}
110
19070062 111static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
9c0a72f9
TG
112 Manager *m = userdata;
113 int b, r;
114
19070062 115 assert(message);
9c0a72f9
TG
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
133int 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;
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
9c0a72f9
TG
195 return 0;
196}
197
5fae368b
TG
198static int manager_udev_process_link(Manager *m, struct udev_device *device) {
199 Link *link = NULL;
200 int r, ifindex;
5544ee85 201
5fae368b
TG
202 assert(m);
203 assert(device);
5544ee85 204
5fae368b
TG
205 if (!streq_ptr(udev_device_get_action(device), "add"))
206 return 0;
5544ee85 207
5fae368b
TG
208 ifindex = udev_device_get_ifindex(device);
209 if (ifindex <= 0) {
2f3cf1f9 210 log_debug("Ignoring udev ADD event for device with invalid ifindex");
5fae368b 211 return 0;
5544ee85
TG
212 }
213
5fae368b
TG
214 r = link_get(m, ifindex, &link);
215 if (r == -ENODEV)
216 return 0;
217 else if (r < 0)
f579559b
TG
218 return r;
219
5fae368b 220 r = link_initialized(link, device);
44de0efc
LP
221 if (r < 0)
222 return r;
223
5fae368b
TG
224 return 0;
225}
be660c37 226
5fae368b
TG
227static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
228 Manager *m = userdata;
229 struct udev_monitor *monitor = m->udev_monitor;
230 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
f579559b 231
5fae368b
TG
232 device = udev_monitor_receive_device(monitor);
233 if (!device)
02b59d57
TG
234 return -ENOMEM;
235
5fae368b 236 manager_udev_process_link(m, device);
f579559b
TG
237 return 0;
238}
239
5fae368b
TG
240static int manager_connect_udev(Manager *m) {
241 int r;
f579559b 242
5fae368b
TG
243 /* udev does not initialize devices inside containers,
244 * so we rely on them being already initialized before
245 * entering the container */
75f86906 246 if (detect_container() > 0)
5fae368b 247 return 0;
f579559b 248
5fae368b
TG
249 m->udev = udev_new();
250 if (!m->udev)
251 return -ENOMEM;
02b59d57 252
5fae368b
TG
253 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
254 if (!m->udev_monitor)
255 return -ENOMEM;
02b59d57 256
5fae368b 257 r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
02b59d57 258 if (r < 0)
5fae368b 259 return log_error_errno(r, "Could not add udev monitor filter: %m");
02b59d57 260
5fae368b
TG
261 r = udev_monitor_enable_receiving(m->udev_monitor);
262 if (r < 0) {
263 log_error("Could not enable udev monitor");
02b59d57 264 return r;
667fcc6d 265 }
505f8da7 266
5fae368b
TG
267 r = sd_event_add_io(m->event,
268 &m->udev_event_source,
269 udev_monitor_get_fd(m->udev_monitor),
270 EPOLLIN, manager_dispatch_link_udev,
271 m);
272 if (r < 0)
667fcc6d 273 return r;
505f8da7 274
5fae368b 275 r = sd_event_source_set_description(m->udev_event_source, "networkd-udev");
505f8da7
TG
276 if (r < 0)
277 return r;
11a7f229 278
505f8da7
TG
279 return 0;
280}
f579559b 281
1c8e710c
TG
282int manager_rtnl_process_route(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
283 Manager *m = userdata;
284 Link *link = NULL;
285 uint16_t type;
286 uint32_t ifindex, priority = 0;
287 unsigned char protocol, scope, tos, table;
288 int family;
289 unsigned char dst_prefixlen, src_prefixlen;
290 union in_addr_union dst = {}, gw = {}, src = {}, prefsrc = {};
291 Route *route = NULL;
292 int r;
293
294 assert(rtnl);
295 assert(message);
296 assert(m);
297
298 if (sd_netlink_message_is_error(message)) {
299 r = sd_netlink_message_get_errno(message);
300 if (r < 0)
301 log_warning_errno(r, "rtnl: failed to receive route: %m");
302
303 return 0;
304 }
305
306 r = sd_netlink_message_get_type(message, &type);
307 if (r < 0) {
308 log_warning_errno(r, "rtnl: could not get message type: %m");
309 return 0;
310 } else if (type != RTM_NEWROUTE && type != RTM_DELROUTE) {
311 log_warning("rtnl: received unexpected message type when processing route");
312 return 0;
313 }
314
315 r = sd_netlink_message_read_u32(message, RTA_OIF, &ifindex);
316 if (r == -ENODATA) {
317 log_debug("rtnl: received route without ifindex, ignoring");
318 return 0;
319 } else if (r < 0) {
320 log_warning_errno(r, "rtnl: could not get ifindex from route, ignoring: %m");
321 return 0;
322 } else if (ifindex <= 0) {
323 log_warning("rtnl: received route message with invalid ifindex, ignoring: %d", ifindex);
324 return 0;
325 } else {
326 r = link_get(m, ifindex, &link);
327 if (r < 0 || !link) {
328 /* when enumerating we might be out of sync, but we will
329 * get the route again, so just ignore it */
330 if (!m->enumerating)
331 log_warning("rtnl: received route for nonexistent link (%d), ignoring", ifindex);
332 return 0;
333 }
334 }
335
336 r = sd_rtnl_message_route_get_family(message, &family);
337 if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) {
338 log_link_warning(link, "rtnl: received address with invalid family, ignoring.");
339 return 0;
340 }
341
342 r = sd_rtnl_message_route_get_protocol(message, &protocol);
343 if (r < 0) {
344 log_warning_errno(r, "rtnl: could not get route protocol: %m");
345 return 0;
346 }
347
348 switch (family) {
349 case AF_INET:
350 r = sd_netlink_message_read_in_addr(message, RTA_DST, &dst.in);
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_in_addr(message, RTA_GATEWAY, &gw.in);
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_in_addr(message, RTA_SRC, &src.in);
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_in_addr(message, RTA_PREFSRC, &prefsrc.in);
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 case AF_INET6:
377 r = sd_netlink_message_read_in6_addr(message, RTA_DST, &dst.in6);
378 if (r < 0 && r != -ENODATA) {
379 log_link_warning_errno(link, r, "rtnl: received route without valid destination, ignoring: %m");
380 return 0;
381 }
382
383 r = sd_netlink_message_read_in6_addr(message, RTA_GATEWAY, &gw.in6);
384 if (r < 0 && r != -ENODATA) {
385 log_link_warning_errno(link, r, "rtnl: received route with invalid gateway, ignoring: %m");
386 return 0;
387 }
388
389 r = sd_netlink_message_read_in6_addr(message, RTA_SRC, &src.in6);
390 if (r < 0 && r != -ENODATA) {
391 log_link_warning_errno(link, r, "rtnl: received route with invalid source, ignoring: %m");
392 return 0;
393 }
394
395 r = sd_netlink_message_read_in6_addr(message, RTA_PREFSRC, &prefsrc.in6);
396 if (r < 0 && r != -ENODATA) {
397 log_link_warning_errno(link, r, "rtnl: received route with invalid preferred source, ignoring: %m");
398 return 0;
399 }
400
401 break;
402
403 default:
404 log_link_debug(link, "rtnl: ignoring unsupported address family: %d", family);
405 return 0;
406 }
407
408 r = sd_rtnl_message_route_get_dst_prefixlen(message, &dst_prefixlen);
409 if (r < 0) {
410 log_link_warning_errno(link, r, "rtnl: received route with invalid destination prefixlen, ignoring: %m");
411 return 0;
412 }
413
414 r = sd_rtnl_message_route_get_src_prefixlen(message, &src_prefixlen);
415 if (r < 0) {
416 log_link_warning_errno(link, r, "rtnl: received route with invalid source prefixlen, ignoring: %m");
417 return 0;
418 }
419
420 r = sd_rtnl_message_route_get_scope(message, &scope);
421 if (r < 0) {
422 log_link_warning_errno(link, r, "rtnl: received route with invalid scope, ignoring: %m");
423 return 0;
424 }
425
426 r = sd_rtnl_message_route_get_tos(message, &tos);
427 if (r < 0) {
428 log_link_warning_errno(link, r, "rtnl: received route with invalid tos, ignoring: %m");
429 return 0;
430 }
431
432 r = sd_rtnl_message_route_get_table(message, &table);
433 if (r < 0) {
434 log_link_warning_errno(link, r, "rtnl: received route with invalid table, ignoring: %m");
435 return 0;
436 }
437
438 r = sd_netlink_message_read_u32(message, RTA_PRIORITY, &priority);
439 if (r < 0 && r != -ENODATA) {
440 log_link_warning_errno(link, r, "rtnl: received route with invalid priority, ignoring: %m");
441 return 0;
442 }
443
444 route_get(link, family, &dst, dst_prefixlen, tos, priority, table, &route);
445
446 switch (type) {
447 case RTM_NEWROUTE:
448 if (!route) {
449 /* A route appeared that we did not request */
450 r = route_add_foreign(link, family, &dst, dst_prefixlen, tos, priority, table, &route);
451 if (r < 0)
452 return 0;
453 }
454
455 route_update(route, &src, src_prefixlen, &gw, &prefsrc, scope, protocol);
456
457 break;
458
459 case RTM_DELROUTE:
b9642f41 460 route_free(route);
1c8e710c 461 break;
b9642f41 462
1c8e710c
TG
463 default:
464 assert_not_reached("Received invalid RTNL message type");
465 }
466
467 return 1;
468}
469
200a0868
TG
470int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
471 Manager *m = userdata;
472 Link *link = NULL;
473 uint16_t type;
200a0868 474 unsigned char flags;
054f0db4
TG
475 int family;
476 unsigned char prefixlen;
477 unsigned char scope;
478 union in_addr_union in_addr;
479 struct ifa_cacheinfo cinfo;
480 Address *address = NULL;
200a0868
TG
481 char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX];
482 const char *valid_str = NULL;
483 int r, ifindex;
484
485 assert(rtnl);
486 assert(message);
487 assert(m);
488
489 if (sd_netlink_message_is_error(message)) {
490 r = sd_netlink_message_get_errno(message);
491 if (r < 0)
492 log_warning_errno(r, "rtnl: failed to receive address: %m");
493
494 return 0;
495 }
496
497 r = sd_netlink_message_get_type(message, &type);
498 if (r < 0) {
499 log_warning_errno(r, "rtnl: could not get message type: %m");
500 return 0;
501 } else if (type != RTM_NEWADDR && type != RTM_DELADDR) {
502 log_warning("rtnl: received unexpected message type when processing address");
503 return 0;
504 }
505
506 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
507 if (r < 0) {
508 log_warning_errno(r, "rtnl: could not get ifindex from address: %m");
509 return 0;
510 } else if (ifindex <= 0) {
511 log_warning("rtnl: received address message with invalid ifindex: %d", ifindex);
512 return 0;
513 } else {
514 r = link_get(m, ifindex, &link);
515 if (r < 0 || !link) {
516 /* when enumerating we might be out of sync, but we will
517 * get the address again, so just ignore it */
518 if (!m->enumerating)
519 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex);
520 return 0;
521 }
522 }
523
054f0db4
TG
524 r = sd_rtnl_message_addr_get_family(message, &family);
525 if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) {
200a0868
TG
526 log_link_warning(link, "rtnl: received address with invalid family, ignoring.");
527 return 0;
528 }
529
054f0db4 530 r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen);
200a0868
TG
531 if (r < 0) {
532 log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m");
533 return 0;
534 }
535
054f0db4 536 r = sd_rtnl_message_addr_get_scope(message, &scope);
200a0868
TG
537 if (r < 0) {
538 log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m");
539 return 0;
540 }
541
542 r = sd_rtnl_message_addr_get_flags(message, &flags);
543 if (r < 0) {
544 log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m");
545 return 0;
546 }
200a0868 547
054f0db4 548 switch (family) {
200a0868 549 case AF_INET:
054f0db4 550 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in);
200a0868
TG
551 if (r < 0) {
552 log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m");
553 return 0;
554 }
555
556 break;
557
558 case AF_INET6:
054f0db4 559 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6);
200a0868
TG
560 if (r < 0) {
561 log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m");
562 return 0;
563 }
564
565 break;
566
567 default:
1c8e710c 568 log_link_debug(link, "rtnl: ignoring unsupported address family: %d", family);
200a0868
TG
569 }
570
054f0db4 571 if (!inet_ntop(family, &in_addr, buf, INET6_ADDRSTRLEN)) {
200a0868
TG
572 log_link_warning(link, "Could not print address");
573 return 0;
574 }
575
054f0db4 576 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
200a0868 577 if (r >= 0) {
4058d339 578 if (cinfo.ifa_valid != CACHE_INFO_INFINITY_LIFE_TIME)
200a0868 579 valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
054f0db4 580 cinfo.ifa_valid * USEC_PER_SEC,
200a0868
TG
581 USEC_PER_SEC);
582 }
583
054f0db4 584 address_get(link, family, &in_addr, prefixlen, &address);
200a0868
TG
585
586 switch (type) {
587 case RTM_NEWADDR:
36c32f61 588 if (address)
4058d339
TG
589 log_link_debug(link, "Updating address: %s/%u (valid %s%s)", buf, prefixlen,
590 valid_str ? "for " : "forever", valid_str ?: "");
36c32f61 591 else {
adda1ed9
TG
592 /* An address appeared that we did not request */
593 r = address_add_foreign(link, family, &in_addr, prefixlen, &address);
cf1d700d 594 if (r < 0) {
054f0db4 595 log_link_warning_errno(link, r, "Failed to add address %s/%u: %m", buf, prefixlen);
cf1d700d
TG
596 return 0;
597 } else
4058d339
TG
598 log_link_debug(link, "Adding address: %s/%u (valid %s%s)", buf, prefixlen,
599 valid_str ? "for " : "forever", valid_str ?: "");
200a0868
TG
600 }
601
58fda79c 602 address_update(address, flags, scope, &cinfo);
36c32f61 603
200a0868
TG
604 break;
605
606 case RTM_DELADDR:
607
054f0db4 608 if (address) {
4058d339
TG
609 log_link_debug(link, "Removing address: %s/%u (valid %s%s)", buf, prefixlen,
610 valid_str ? "for " : "forever", valid_str ?: "");
91b5f997 611 address_drop(address);
200a0868 612 } else
4058d339
TG
613 log_link_warning(link, "Removing non-existent address: %s/%u (valid %s%s)", buf, prefixlen,
614 valid_str ? "for " : "forever", valid_str ?: "");
200a0868
TG
615
616 break;
617 default:
618 assert_not_reached("Received invalid RTNL message type");
619 }
620
621 return 1;
622}
623
1c4baffc 624static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
505f8da7
TG
625 Manager *m = userdata;
626 Link *link = NULL;
4d473d5d 627 NetDev *netdev = NULL;
f2236469 628 uint16_t type;
ca4e095a 629 const char *name;
505f8da7 630 int r, ifindex;
f579559b 631
505f8da7
TG
632 assert(rtnl);
633 assert(message);
f579559b
TG
634 assert(m);
635
1c4baffc
TG
636 if (sd_netlink_message_is_error(message)) {
637 r = sd_netlink_message_get_errno(message);
45af44d4 638 if (r < 0)
2f3cf1f9 639 log_warning_errno(r, "rtnl: Could not receive link: %m");
45af44d4
TG
640
641 return 0;
642 }
643
1c4baffc 644 r = sd_netlink_message_get_type(message, &type);
f2236469 645 if (r < 0) {
2f3cf1f9 646 log_warning_errno(r, "rtnl: Could not get message type: %m");
f2236469 647 return 0;
cdfee943 648 } else if (type != RTM_NEWLINK && type != RTM_DELLINK) {
2f3cf1f9 649 log_warning("rtnl: Received unexpected message type when processing link");
cdfee943 650 return 0;
f2236469
TG
651 }
652
505f8da7 653 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
45af44d4 654 if (r < 0) {
2f3cf1f9 655 log_warning_errno(r, "rtnl: Could not get ifindex from link: %m");
45af44d4
TG
656 return 0;
657 } else if (ifindex <= 0) {
658 log_warning("rtnl: received link message with invalid ifindex: %d", ifindex);
505f8da7 659 return 0;
a0db8e46 660 }
f579559b 661
1c4baffc 662 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
45af44d4 663 if (r < 0) {
2f3cf1f9 664 log_warning_errno(r, "rtnl: Received link message without ifname: %m");
4d473d5d 665 return 0;
a0db8e46
ZJS
666 }
667
668 (void) link_get(m, ifindex, &link);
669 (void) netdev_get(m, name, &netdev);
4d473d5d
TG
670
671 switch (type) {
672 case RTM_NEWLINK:
673 if (!link) {
674 /* link is new, so add it */
675 r = link_add(m, message, &link);
676 if (r < 0) {
2f3cf1f9 677 log_warning_errno(r, "Could not add new link: %m");
4d473d5d
TG
678 return 0;
679 }
680 }
681
682 if (netdev) {
683 /* netdev exists, so make sure the ifindex matches */
505f8da7
TG
684 r = netdev_set_ifindex(netdev, message);
685 if (r < 0) {
2f3cf1f9 686 log_warning_errno(r, "Could not set ifindex on netdev: %m");
505f8da7
TG
687 return 0;
688 }
689 }
e1202047 690
f2236469
TG
691 r = link_update(link, message);
692 if (r < 0)
693 return 0;
4d473d5d
TG
694
695 break;
696
697 case RTM_DELLINK:
698 link_drop(link);
699 netdev_drop(netdev);
700
701 break;
702
703 default:
704 assert_not_reached("Received invalid RTNL message type.");
f2236469 705 }
505f8da7
TG
706
707 return 1;
708}
709
5fae368b
TG
710static int systemd_netlink_fd(void) {
711 int n, fd, rtnl_fd = -EINVAL;
712
713 n = sd_listen_fds(true);
714 if (n <= 0)
715 return -EINVAL;
716
717 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
718 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
719 if (rtnl_fd >= 0)
720 return -EINVAL;
721
722 rtnl_fd = fd;
723 }
724 }
725
726 return rtnl_fd;
727}
728
729static int manager_connect_rtnl(Manager *m) {
730 int fd, r;
505f8da7
TG
731
732 assert(m);
505f8da7 733
5fae368b
TG
734 fd = systemd_netlink_fd();
735 if (fd < 0)
1c4baffc 736 r = sd_netlink_open(&m->rtnl);
5fae368b 737 else
1c4baffc 738 r = sd_netlink_open_fd(&m->rtnl, fd);
505f8da7
TG
739 if (r < 0)
740 return r;
741
1c4baffc 742 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
f579559b 743 if (r < 0)
bf5332d2 744 return r;
f579559b 745
1c4baffc 746 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
505f8da7
TG
747 if (r < 0)
748 return r;
f579559b 749
1c4baffc 750 r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
5fae368b
TG
751 if (r < 0)
752 return r;
505f8da7 753
1c4baffc 754 r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
5fae368b
TG
755 if (r < 0)
756 return r;
45af44d4 757
200a0868 758 r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m);
5fae368b
TG
759 if (r < 0)
760 return r;
761
200a0868 762 r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m);
5fae368b
TG
763 if (r < 0)
764 return r;
765
1c8e710c
TG
766 r = sd_netlink_add_match(m->rtnl, RTM_NEWROUTE, &manager_rtnl_process_route, m);
767 if (r < 0)
768 return r;
769
770 r = sd_netlink_add_match(m->rtnl, RTM_DELROUTE, &manager_rtnl_process_route, m);
771 if (r < 0)
772 return r;
773
5fae368b 774 return 0;
45af44d4 775}
505f8da7 776
00616955 777static int ordered_set_put_in_addr(OrderedSet *s, const struct in_addr *address) {
84de38c5
TG
778 char *p;
779 int r;
780
781 assert(s);
782
783 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
784 if (r < 0)
785 return r;
786
00616955 787 r = ordered_set_consume(s, p);
84de38c5
TG
788 if (r == -EEXIST)
789 return 0;
790
791 return r;
792}
793
00616955 794static int ordered_set_put_in_addrv(OrderedSet *s, const struct in_addr *addresses, int n) {
84de38c5
TG
795 int r, i, c = 0;
796
797 assert(s);
798 assert(n <= 0 || addresses);
799
800 for (i = 0; i < n; i++) {
00616955 801 r = ordered_set_put_in_addr(s, addresses+i);
84de38c5
TG
802 if (r < 0)
803 return r;
804
805 c += r;
806 }
807
808 return c;
809}
810
00616955 811static void print_string_set(FILE *f, const char *field, OrderedSet *s) {
84de38c5
TG
812 bool space = false;
813 Iterator i;
814 char *p;
815
00616955 816 if (ordered_set_isempty(s))
84de38c5
TG
817 return;
818
819 fputs(field, f);
820
d390f8ef
LP
821 ORDERED_SET_FOREACH(p, s, i)
822 fputs_with_space(f, p, NULL, &space);
823
84de38c5
TG
824 fputc('\n', f);
825}
826
827static int manager_save(Manager *m) {
00616955 828 _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL;
84de38c5
TG
829 Link *link;
830 Iterator i;
831 _cleanup_free_ char *temp_path = NULL;
832 _cleanup_fclose_ FILE *f = NULL;
833 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
834 const char *operstate_str;
835 int r;
836
837 assert(m);
838 assert(m->state_file);
839
840 /* We add all NTP and DNS server to a set, to filter out duplicates */
00616955 841 dns = ordered_set_new(&string_hash_ops);
84de38c5
TG
842 if (!dns)
843 return -ENOMEM;
844
00616955 845 ntp = ordered_set_new(&string_hash_ops);
84de38c5
TG
846 if (!ntp)
847 return -ENOMEM;
848
482d1aeb 849 search_domains = ordered_set_new(&dns_name_hash_ops);
3df9bec5
LP
850 if (!search_domains)
851 return -ENOMEM;
852
482d1aeb 853 route_domains = ordered_set_new(&dns_name_hash_ops);
3df9bec5 854 if (!route_domains)
84de38c5
TG
855 return -ENOMEM;
856
857 HASHMAP_FOREACH(link, m->links, i) {
858 if (link->flags & IFF_LOOPBACK)
859 continue;
860
861 if (link->operstate > operstate)
862 operstate = link->operstate;
863
864 if (!link->network)
865 continue;
866
867 /* First add the static configured entries */
00616955 868 r = ordered_set_put_strdupv(dns, link->network->dns);
84de38c5
TG
869 if (r < 0)
870 return r;
871
00616955 872 r = ordered_set_put_strdupv(ntp, link->network->ntp);
84de38c5
TG
873 if (r < 0)
874 return r;
875
00616955 876 r = ordered_set_put_strdupv(search_domains, link->network->search_domains);
3df9bec5
LP
877 if (r < 0)
878 return r;
879
00616955 880 r = ordered_set_put_strdupv(route_domains, link->network->route_domains);
84de38c5
TG
881 if (r < 0)
882 return r;
883
884 if (!link->dhcp_lease)
885 continue;
886
887 /* Secondly, add the entries acquired via DHCP */
27cb34f5 888 if (link->network->dhcp_use_dns) {
84de38c5
TG
889 const struct in_addr *addresses;
890
891 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
892 if (r > 0) {
00616955 893 r = ordered_set_put_in_addrv(dns, addresses, r);
84de38c5
TG
894 if (r < 0)
895 return r;
896 } else if (r < 0 && r != -ENODATA)
897 return r;
898 }
899
27cb34f5 900 if (link->network->dhcp_use_ntp) {
84de38c5
TG
901 const struct in_addr *addresses;
902
903 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
904 if (r > 0) {
00616955 905 r = ordered_set_put_in_addrv(ntp, addresses, r);
84de38c5
TG
906 if (r < 0)
907 return r;
908 } else if (r < 0 && r != -ENODATA)
909 return r;
910 }
911
b2a81c0b 912 if (link->network->dhcp_use_domains != DHCP_USE_DOMAINS_NO) {
84de38c5
TG
913 const char *domainname;
914
915 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
916 if (r >= 0) {
b2a81c0b
LP
917
918 if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES)
919 r = ordered_set_put_strdup(search_domains, domainname);
920 else
921 r = ordered_set_put_strdup(route_domains, domainname);
922
84de38c5
TG
923 if (r < 0)
924 return r;
925 } else if (r != -ENODATA)
926 return r;
927 }
928 }
929
930 operstate_str = link_operstate_to_string(operstate);
931 assert(operstate_str);
932
933 r = fopen_temporary(m->state_file, &f, &temp_path);
934 if (r < 0)
935 return r;
936
937 fchmod(fileno(f), 0644);
938
939 fprintf(f,
940 "# This is private data. Do not parse.\n"
941 "OPER_STATE=%s\n", operstate_str);
942
943 print_string_set(f, "DNS=", dns);
944 print_string_set(f, "NTP=", ntp);
3df9bec5
LP
945 print_string_set(f, "DOMAINS=", search_domains);
946 print_string_set(f, "ROUTE_DOMAINS=", route_domains);
84de38c5
TG
947
948 r = fflush_and_check(f);
949 if (r < 0)
950 goto fail;
951
952 if (rename(temp_path, m->state_file) < 0) {
953 r = -errno;
954 goto fail;
955 }
956
957 if (m->operational_state != operstate) {
958 m->operational_state = operstate;
959 r = manager_send_changed(m, "OperationalState", NULL);
960 if (r < 0)
961 log_error_errno(r, "Could not emit changed OperationalState: %m");
962 }
963
964 m->dirty = false;
965
966 return 0;
967
968fail:
969 (void) unlink(m->state_file);
970 (void) unlink(temp_path);
971
972 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
973}
974
975static int manager_dirty_handler(sd_event_source *s, void *userdata) {
976 Manager *m = userdata;
977 Link *link;
978 Iterator i;
979 int r;
980
981 assert(m);
982
983 if (m->dirty)
984 manager_save(m);
985
986 SET_FOREACH(link, m->dirty_links, i) {
987 r = link_save(link);
988 if (r >= 0)
989 link_clean(link);
990 }
991
992 return 1;
993}
994
5fae368b
TG
995int manager_new(Manager **ret) {
996 _cleanup_manager_free_ Manager *m = NULL;
45af44d4 997 int r;
f579559b 998
5fae368b
TG
999 m = new0(Manager, 1);
1000 if (!m)
1001 return -ENOMEM;
45af44d4 1002
5fae368b
TG
1003 m->state_file = strdup("/run/systemd/netif/state");
1004 if (!m->state_file)
1005 return -ENOMEM;
1006
1007 r = sd_event_default(&m->event);
45af44d4
TG
1008 if (r < 0)
1009 return r;
1010
5fae368b
TG
1011 sd_event_set_watchdog(m->event, true);
1012
1013 sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
1014 sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
1015
84de38c5
TG
1016 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
1017 if (r < 0)
1018 return r;
1019
5fae368b 1020 r = manager_connect_rtnl(m);
45af44d4
TG
1021 if (r < 0)
1022 return r;
1023
5fae368b
TG
1024 r = manager_connect_udev(m);
1025 if (r < 0)
1026 return r;
45af44d4 1027
5fae368b
TG
1028 m->netdevs = hashmap_new(&string_hash_ops);
1029 if (!m->netdevs)
1030 return -ENOMEM;
f579559b 1031
5fae368b 1032 LIST_HEAD_INIT(m->networks);
f579559b 1033
5fae368b
TG
1034 r = setup_default_address_pool(m);
1035 if (r < 0)
1036 return r;
f579559b 1037
8341a5c3 1038 m->duid.type = DUID_TYPE_EN;
413708d1 1039
5fae368b
TG
1040 *ret = m;
1041 m = NULL;
f579559b 1042
f579559b
TG
1043 return 0;
1044}
1045
5fae368b
TG
1046void manager_free(Manager *m) {
1047 Network *network;
1048 NetDev *netdev;
1049 Link *link;
1050 AddressPool *pool;
f579559b 1051
5fae368b
TG
1052 if (!m)
1053 return;
505f8da7 1054
5fae368b 1055 free(m->state_file);
505f8da7 1056
5fae368b
TG
1057 while ((link = hashmap_first(m->links)))
1058 link_unref(link);
1059 hashmap_free(m->links);
f579559b 1060
5fae368b
TG
1061 while ((network = m->networks))
1062 network_free(network);
1063
dbffab87
TG
1064 hashmap_free(m->networks_by_name);
1065
5fae368b
TG
1066 while ((netdev = hashmap_first(m->netdevs)))
1067 netdev_unref(netdev);
1068 hashmap_free(m->netdevs);
1069
1070 while ((pool = m->address_pools))
1071 address_pool_free(pool);
1072
1c4baffc 1073 sd_netlink_unref(m->rtnl);
2f5b4a77 1074 sd_event_unref(m->event);
5fae368b 1075
7d20d375
TG
1076 sd_event_source_unref(m->udev_event_source);
1077 udev_monitor_unref(m->udev_monitor);
1078 udev_unref(m->udev);
1079
1080 sd_bus_unref(m->bus);
1081 sd_bus_slot_unref(m->prepare_for_sleep_slot);
1082 sd_event_source_unref(m->bus_retry_event_source);
1083
5fae368b
TG
1084 free(m);
1085}
1086
a97dcc12
TG
1087static bool manager_check_idle(void *userdata) {
1088 Manager *m = userdata;
1089 Link *link;
1090 Iterator i;
1091
1092 assert(m);
1093
9dc907f9
LP
1094 /* Check whether we are idle now. The only case when we decide to be idle is when there's only a loopback
1095 * device around, for which we have no configuration, and which already left the PENDING state. In all other
1096 * cases we are not idle. */
1097
a97dcc12 1098 HASHMAP_FOREACH(link, m->links, i) {
9dc907f9 1099 /* We are not woken on udev activity, so let's just wait for the pending udev event */
a97dcc12
TG
1100 if (link->state == LINK_STATE_PENDING)
1101 return false;
1102
9dc907f9
LP
1103 if ((link->flags & IFF_LOOPBACK) == 0)
1104 return false;
a97dcc12 1105
9dc907f9 1106 if (link->network)
a97dcc12
TG
1107 return false;
1108 }
1109
1110 return true;
1111}
1112
1113int manager_run(Manager *m) {
84de38c5
TG
1114 Link *link;
1115 Iterator i;
1116
a97dcc12
TG
1117 assert(m);
1118
84de38c5
TG
1119 /* The dirty handler will deal with future serialization, but the first one
1120 must be done explicitly. */
1121
1122 manager_save(m);
1123
1124 HASHMAP_FOREACH(link, m->links, i)
1125 link_save(link);
1126
f806f64c
TG
1127 if (m->bus)
1128 return bus_event_loop_with_idle(
1129 m->event,
1130 m->bus,
1131 "org.freedesktop.network1",
1132 DEFAULT_EXIT_USEC,
1133 manager_check_idle,
1134 m);
1135 else
1136 /* failed to connect to the bus, so we lose exit-on-idle logic,
1137 this should not happen except if dbus is not around at all */
1138 return sd_event_loop(m->event);
a97dcc12
TG
1139}
1140
5fae368b
TG
1141int manager_load_config(Manager *m) {
1142 int r;
1143
1144 /* update timestamp */
1145 paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
1146
1147 r = netdev_load(m);
f579559b
TG
1148 if (r < 0)
1149 return r;
1150
5fae368b 1151 r = network_load(m);
9021bb9f
TG
1152 if (r < 0)
1153 return r;
1154
f579559b
TG
1155 return 0;
1156}
f882c247 1157
5fae368b
TG
1158bool manager_should_reload(Manager *m) {
1159 return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
1160}
1161
1162int manager_rtnl_enumerate_links(Manager *m) {
4afd3348 1163 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1c4baffc 1164 sd_netlink_message *link;
f882c247
TG
1165 int r;
1166
5da8149f 1167 assert(m);
5fae368b 1168 assert(m->rtnl);
5da8149f 1169
5fae368b 1170 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
f882c247
TG
1171 if (r < 0)
1172 return r;
1173
1c4baffc 1174 r = sd_netlink_message_request_dump(req, true);
dd3efc09
TG
1175 if (r < 0)
1176 return r;
1177
1c4baffc 1178 r = sd_netlink_call(m->rtnl, req, 0, &reply);
f2236469
TG
1179 if (r < 0)
1180 return r;
1181
1c4baffc 1182 for (link = reply; link; link = sd_netlink_message_next(link)) {
5fae368b 1183 int k;
2e9f08ea 1184
6a24f148
TG
1185 m->enumerating = true;
1186
5fae368b
TG
1187 k = manager_rtnl_process_link(m->rtnl, link, m);
1188 if (k < 0)
1189 r = k;
6a24f148
TG
1190
1191 m->enumerating = false;
5fae368b 1192 }
2e9f08ea 1193
5fae368b 1194 return r;
f882c247 1195}
3bef724f 1196
5fae368b 1197int manager_rtnl_enumerate_addresses(Manager *m) {
4afd3348 1198 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1c4baffc 1199 sd_netlink_message *addr;
1346b1f0
TG
1200 int r;
1201
5fae368b
TG
1202 assert(m);
1203 assert(m->rtnl);
bcbca829 1204
5fae368b
TG
1205 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
1206 if (r < 0)
1207 return r;
bcbca829 1208
1c4baffc 1209 r = sd_netlink_message_request_dump(req, true);
1346b1f0
TG
1210 if (r < 0)
1211 return r;
1212
1c4baffc 1213 r = sd_netlink_call(m->rtnl, req, 0, &reply);
5fae368b
TG
1214 if (r < 0)
1215 return r;
1216
1c4baffc 1217 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
5fae368b
TG
1218 int k;
1219
6a24f148
TG
1220 m->enumerating = true;
1221
200a0868 1222 k = manager_rtnl_process_address(m->rtnl, addr, m);
5fae368b
TG
1223 if (k < 0)
1224 r = k;
6a24f148
TG
1225
1226 m->enumerating = false;
5fae368b
TG
1227 }
1228
1229 return r;
1346b1f0
TG
1230}
1231
1c8e710c 1232int manager_rtnl_enumerate_routes(Manager *m) {
4afd3348 1233 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1c8e710c
TG
1234 sd_netlink_message *route;
1235 int r;
1236
1237 assert(m);
1238 assert(m->rtnl);
1239
1240 r = sd_rtnl_message_new_route(m->rtnl, &req, RTM_GETROUTE, 0, 0);
1241 if (r < 0)
1242 return r;
1243
1244 r = sd_netlink_message_request_dump(req, true);
1245 if (r < 0)
1246 return r;
1247
1248 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1249 if (r < 0)
1250 return r;
1251
1252 for (route = reply; route; route = sd_netlink_message_next(route)) {
1253 int k;
1254
1255 m->enumerating = true;
1256
1257 k = manager_rtnl_process_route(m->rtnl, route, m);
1258 if (k < 0)
1259 r = k;
1260
1261 m->enumerating = false;
1262 }
1263
1264 return r;
1265}
1266
0dd25fb9 1267int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
11bf3cce
LP
1268 AddressPool *p;
1269 int r;
1270
1271 assert(m);
1272 assert(prefixlen > 0);
1273 assert(found);
1274
1275 LIST_FOREACH(address_pools, p, m->address_pools) {
1276 if (p->family != family)
1277 continue;
1278
1279 r = address_pool_acquire(p, prefixlen, found);
1280 if (r != 0)
1281 return r;
1282 }
1283
1284 return 0;
1285}
4f5f911e
LP
1286
1287Link* manager_find_uplink(Manager *m, Link *exclude) {
1288 _cleanup_free_ struct local_address *gateways = NULL;
1289 int n, i;
1290
1291 assert(m);
1292
1293 /* Looks for a suitable "uplink", via black magic: an
1294 * interface that is up and where the default route with the
1295 * highest priority points to. */
1296
1297 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
1298 if (n < 0) {
1299 log_warning_errno(n, "Failed to determine list of default gateways: %m");
1300 return NULL;
1301 }
1302
1303 for (i = 0; i < n; i++) {
1304 Link *link;
1305
1306 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
1307 if (!link) {
c2c940bd 1308 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
4f5f911e
LP
1309 continue;
1310 }
1311
1312 if (link == exclude)
1313 continue;
1314
1315 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
1316 continue;
1317
1318 return link;
1319 }
1320
1321 return NULL;
1322}
84de38c5
TG
1323
1324void manager_dirty(Manager *manager) {
1325 assert(manager);
1326
1327 /* the serialized state in /run is no longer up-to-date */
1328 manager->dirty = true;
1329}