]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd-manager.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / network / networkd-manager.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <sys/socket.h>
23 #include <linux/if.h>
24
25 #include "sd-daemon.h"
26 #include "sd-netlink.h"
27
28 #include "bus-util.h"
29 #include "conf-parser.h"
30 #include "def.h"
31 #include "libudev-private.h"
32 #include "local-addresses.h"
33 #include "netlink-util.h"
34 #include "networkd.h"
35 #include "path-util.h"
36 #include "set.h"
37 #include "udev-util.h"
38 #include "virt.h"
39
40 /* use 8 MB for receive socket kernel queue. */
41 #define RCVBUF_SIZE (8*1024*1024)
42
43 const char* const network_dirs[] = {
44 "/etc/systemd/network",
45 "/run/systemd/network",
46 "/usr/lib/systemd/network",
47 #ifdef HAVE_SPLIT_USR
48 "/lib/systemd/network",
49 #endif
50 NULL};
51
52 static int setup_default_address_pool(Manager *m) {
53 AddressPool *p;
54 int r;
55
56 assert(m);
57
58 /* Add in the well-known private address ranges. */
59
60 r = address_pool_new_from_string(m, &p, AF_INET6, "fc00::", 7);
61 if (r < 0)
62 return r;
63
64 r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
65 if (r < 0)
66 return r;
67
68 r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
69 if (r < 0)
70 return r;
71
72 r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
73 if (r < 0)
74 return r;
75
76 return 0;
77 }
78
79 static int on_bus_retry(sd_event_source *s, usec_t usec, void *userdata) {
80 Manager *m = userdata;
81
82 assert(s);
83 assert(m);
84
85 m->bus_retry_event_source = sd_event_source_unref(m->bus_retry_event_source);
86
87 manager_connect_bus(m);
88
89 return 0;
90 }
91
92 static int manager_reset_all(Manager *m) {
93 Link *link;
94 Iterator i;
95 int r;
96
97 assert(m);
98
99 HASHMAP_FOREACH(link, m->links, i) {
100 r = link_carrier_reset(link);
101 if (r < 0)
102 log_link_warning_errno(link, r, "Could not reset carrier: %m");
103 }
104
105 return 0;
106 }
107
108 static int match_prepare_for_sleep(sd_bus_message *message, void *userdata, sd_bus_error *ret_error) {
109 Manager *m = userdata;
110 int b, r;
111
112 assert(message);
113
114 r = sd_bus_message_read(message, "b", &b);
115 if (r < 0) {
116 log_debug_errno(r, "Failed to parse PrepareForSleep signal: %m");
117 return 0;
118 }
119
120 if (b)
121 return 0;
122
123 log_debug("Coming back from suspend, resetting all connections...");
124
125 manager_reset_all(m);
126
127 return 0;
128 }
129
130 int manager_connect_bus(Manager *m) {
131 int r;
132
133 assert(m);
134
135 r = sd_bus_default_system(&m->bus);
136 if (r == -ENOENT) {
137 /* We failed to connect? Yuck, we must be in early
138 * boot. Let's try in 5s again. As soon as we have
139 * kdbus we can stop doing this... */
140
141 log_debug_errno(r, "Failed to connect to bus, trying again in 5s: %m");
142
143 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);
144 if (r < 0)
145 return log_error_errno(r, "Failed to install bus reconnect time event: %m");
146
147 return 0;
148 }
149
150 if (r < 0)
151 return r;
152
153 r = sd_bus_add_match(m->bus, &m->prepare_for_sleep_slot,
154 "type='signal',"
155 "sender='org.freedesktop.login1',"
156 "interface='org.freedesktop.login1.Manager',"
157 "member='PrepareForSleep',"
158 "path='/org/freedesktop/login1'",
159 match_prepare_for_sleep,
160 m);
161 if (r < 0)
162 return log_error_errno(r, "Failed to add match for PrepareForSleep: %m");
163
164 r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/network1", "org.freedesktop.network1.Manager", manager_vtable, m);
165 if (r < 0)
166 return log_error_errno(r, "Failed to add manager object vtable: %m");
167
168 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/link", "org.freedesktop.network1.Link", link_vtable, link_object_find, m);
169 if (r < 0)
170 return log_error_errno(r, "Failed to add link object vtable: %m");
171
172 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/link", link_node_enumerator, m);
173 if (r < 0)
174 return log_error_errno(r, "Failed to add link enumerator: %m");
175
176 r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/network1/network", "org.freedesktop.network1.Network", network_vtable, network_object_find, m);
177 if (r < 0)
178 return log_error_errno(r, "Failed to add network object vtable: %m");
179
180 r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/network1/network", network_node_enumerator, m);
181 if (r < 0)
182 return log_error_errno(r, "Failed to add network enumerator: %m");
183
184 r = sd_bus_request_name(m->bus, "org.freedesktop.network1", 0);
185 if (r < 0)
186 return log_error_errno(r, "Failed to register name: %m");
187
188 r = sd_bus_attach_event(m->bus, m->event, 0);
189 if (r < 0)
190 return log_error_errno(r, "Failed to attach bus to event loop: %m");
191
192 return 0;
193 }
194
195 static int manager_udev_process_link(Manager *m, struct udev_device *device) {
196 Link *link = NULL;
197 int r, ifindex;
198
199 assert(m);
200 assert(device);
201
202 if (!streq_ptr(udev_device_get_action(device), "add"))
203 return 0;
204
205 ifindex = udev_device_get_ifindex(device);
206 if (ifindex <= 0) {
207 log_debug("Ignoring udev ADD event for device with invalid ifindex");
208 return 0;
209 }
210
211 r = link_get(m, ifindex, &link);
212 if (r == -ENODEV)
213 return 0;
214 else if (r < 0)
215 return r;
216
217 r = link_initialized(link, device);
218 if (r < 0)
219 return r;
220
221 return 0;
222 }
223
224 static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
225 Manager *m = userdata;
226 struct udev_monitor *monitor = m->udev_monitor;
227 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
228
229 device = udev_monitor_receive_device(monitor);
230 if (!device)
231 return -ENOMEM;
232
233 manager_udev_process_link(m, device);
234 return 0;
235 }
236
237 static int manager_connect_udev(Manager *m) {
238 int r;
239
240 /* udev does not initialize devices inside containers,
241 * so we rely on them being already initialized before
242 * entering the container */
243 if (detect_container() > 0)
244 return 0;
245
246 m->udev = udev_new();
247 if (!m->udev)
248 return -ENOMEM;
249
250 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
251 if (!m->udev_monitor)
252 return -ENOMEM;
253
254 r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
255 if (r < 0)
256 return log_error_errno(r, "Could not add udev monitor filter: %m");
257
258 r = udev_monitor_enable_receiving(m->udev_monitor);
259 if (r < 0) {
260 log_error("Could not enable udev monitor");
261 return r;
262 }
263
264 r = sd_event_add_io(m->event,
265 &m->udev_event_source,
266 udev_monitor_get_fd(m->udev_monitor),
267 EPOLLIN, manager_dispatch_link_udev,
268 m);
269 if (r < 0)
270 return r;
271
272 r = sd_event_source_set_description(m->udev_event_source, "networkd-udev");
273 if (r < 0)
274 return r;
275
276 return 0;
277 }
278
279 int manager_rtnl_process_address(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
280 Manager *m = userdata;
281 Link *link = NULL;
282 uint16_t type;
283 unsigned char flags;
284 int family;
285 unsigned char prefixlen;
286 unsigned char scope;
287 union in_addr_union in_addr;
288 struct ifa_cacheinfo cinfo;
289 Address *address = NULL;
290 char buf[INET6_ADDRSTRLEN], valid_buf[FORMAT_TIMESPAN_MAX];
291 const char *valid_str = NULL;
292 int r, ifindex;
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 address: %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_NEWADDR && type != RTM_DELADDR) {
311 log_warning("rtnl: received unexpected message type when processing address");
312 return 0;
313 }
314
315 r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
316 if (r < 0) {
317 log_warning_errno(r, "rtnl: could not get ifindex from address: %m");
318 return 0;
319 } else if (ifindex <= 0) {
320 log_warning("rtnl: received address message with invalid ifindex: %d", ifindex);
321 return 0;
322 } else {
323 r = link_get(m, ifindex, &link);
324 if (r < 0 || !link) {
325 /* when enumerating we might be out of sync, but we will
326 * get the address again, so just ignore it */
327 if (!m->enumerating)
328 log_warning("rtnl: received address for nonexistent link (%d), ignoring", ifindex);
329 return 0;
330 }
331 }
332
333 r = sd_rtnl_message_addr_get_family(message, &family);
334 if (r < 0 || !IN_SET(family, AF_INET, AF_INET6)) {
335 log_link_warning(link, "rtnl: received address with invalid family, ignoring.");
336 return 0;
337 }
338
339 r = sd_rtnl_message_addr_get_prefixlen(message, &prefixlen);
340 if (r < 0) {
341 log_link_warning_errno(link, r, "rtnl: received address with invalid prefixlen, ignoring: %m");
342 return 0;
343 }
344
345 r = sd_rtnl_message_addr_get_scope(message, &scope);
346 if (r < 0) {
347 log_link_warning_errno(link, r, "rtnl: received address with invalid scope, ignoring: %m");
348 return 0;
349 }
350
351 r = sd_rtnl_message_addr_get_flags(message, &flags);
352 if (r < 0) {
353 log_link_warning_errno(link, r, "rtnl: received address with invalid flags, ignoring: %m");
354 return 0;
355 }
356
357 switch (family) {
358 case AF_INET:
359 r = sd_netlink_message_read_in_addr(message, IFA_LOCAL, &in_addr.in);
360 if (r < 0) {
361 log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m");
362 return 0;
363 }
364
365 break;
366
367 case AF_INET6:
368 r = sd_netlink_message_read_in6_addr(message, IFA_ADDRESS, &in_addr.in6);
369 if (r < 0) {
370 log_link_warning_errno(link, r, "rtnl: received address without valid address, ignoring: %m");
371 return 0;
372 }
373
374 break;
375
376 default:
377 assert_not_reached("invalid address family");
378 }
379
380 if (!inet_ntop(family, &in_addr, buf, INET6_ADDRSTRLEN)) {
381 log_link_warning(link, "Could not print address");
382 return 0;
383 }
384
385 r = sd_netlink_message_read_cache_info(message, IFA_CACHEINFO, &cinfo);
386 if (r >= 0) {
387 if (cinfo.ifa_valid == CACHE_INFO_INFINITY_LIFE_TIME)
388 valid_str = "ever";
389 else
390 valid_str = format_timespan(valid_buf, FORMAT_TIMESPAN_MAX,
391 cinfo.ifa_valid * USEC_PER_SEC,
392 USEC_PER_SEC);
393 }
394
395 address_get(link, family, &in_addr, prefixlen, &address);
396
397 switch (type) {
398 case RTM_NEWADDR:
399 if (address)
400 log_link_debug(link, "Updating address: %s/%u (valid for %s)", buf, prefixlen, valid_str);
401 else {
402 /* An address appeared that we did not request */
403 r = address_add_foreign(link, family, &in_addr, prefixlen, &address);
404 if (r < 0) {
405 log_link_warning_errno(link, r, "Failed to add address %s/%u: %m", buf, prefixlen);
406 return 0;
407 } else
408 log_link_debug(link, "Adding address: %s/%u (valid for %s)", buf, prefixlen, valid_str);
409 }
410
411 address_update(address, flags, scope, &cinfo);
412
413 break;
414
415 case RTM_DELADDR:
416
417 if (address) {
418 log_link_debug(link, "Removing address: %s/%u (valid for %s)", buf, prefixlen, valid_str);
419 address_drop(address);
420 } else
421 log_link_warning(link, "Removing non-existent address: %s/%u (valid for %s)", buf, prefixlen, valid_str);
422
423 break;
424 default:
425 assert_not_reached("Received invalid RTNL message type");
426 }
427
428 return 1;
429 }
430
431 static int manager_rtnl_process_link(sd_netlink *rtnl, sd_netlink_message *message, void *userdata) {
432 Manager *m = userdata;
433 Link *link = NULL;
434 NetDev *netdev = NULL;
435 uint16_t type;
436 const char *name;
437 int r, ifindex;
438
439 assert(rtnl);
440 assert(message);
441 assert(m);
442
443 if (sd_netlink_message_is_error(message)) {
444 r = sd_netlink_message_get_errno(message);
445 if (r < 0)
446 log_warning_errno(r, "rtnl: Could not receive link: %m");
447
448 return 0;
449 }
450
451 r = sd_netlink_message_get_type(message, &type);
452 if (r < 0) {
453 log_warning_errno(r, "rtnl: Could not get message type: %m");
454 return 0;
455 } else if (type != RTM_NEWLINK && type != RTM_DELLINK) {
456 log_warning("rtnl: Received unexpected message type when processing link");
457 return 0;
458 }
459
460 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
461 if (r < 0) {
462 log_warning_errno(r, "rtnl: Could not get ifindex from link: %m");
463 return 0;
464 } else if (ifindex <= 0) {
465 log_warning("rtnl: received link message with invalid ifindex: %d", ifindex);
466 return 0;
467 } else
468 link_get(m, ifindex, &link);
469
470 r = sd_netlink_message_read_string(message, IFLA_IFNAME, &name);
471 if (r < 0) {
472 log_warning_errno(r, "rtnl: Received link message without ifname: %m");
473 return 0;
474 } else
475 netdev_get(m, name, &netdev);
476
477 switch (type) {
478 case RTM_NEWLINK:
479 if (!link) {
480 /* link is new, so add it */
481 r = link_add(m, message, &link);
482 if (r < 0) {
483 log_warning_errno(r, "Could not add new link: %m");
484 return 0;
485 }
486 }
487
488 if (netdev) {
489 /* netdev exists, so make sure the ifindex matches */
490 r = netdev_set_ifindex(netdev, message);
491 if (r < 0) {
492 log_warning_errno(r, "Could not set ifindex on netdev: %m");
493 return 0;
494 }
495 }
496
497 r = link_update(link, message);
498 if (r < 0)
499 return 0;
500
501 break;
502
503 case RTM_DELLINK:
504 link_drop(link);
505 netdev_drop(netdev);
506
507 break;
508
509 default:
510 assert_not_reached("Received invalid RTNL message type.");
511 }
512
513 return 1;
514 }
515
516 static int systemd_netlink_fd(void) {
517 int n, fd, rtnl_fd = -EINVAL;
518
519 n = sd_listen_fds(true);
520 if (n <= 0)
521 return -EINVAL;
522
523 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
524 if (sd_is_socket(fd, AF_NETLINK, SOCK_RAW, -1) > 0) {
525 if (rtnl_fd >= 0)
526 return -EINVAL;
527
528 rtnl_fd = fd;
529 }
530 }
531
532 return rtnl_fd;
533 }
534
535 static int manager_connect_rtnl(Manager *m) {
536 int fd, r;
537
538 assert(m);
539
540 fd = systemd_netlink_fd();
541 if (fd < 0)
542 r = sd_netlink_open(&m->rtnl);
543 else
544 r = sd_netlink_open_fd(&m->rtnl, fd);
545 if (r < 0)
546 return r;
547
548 r = sd_netlink_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
549 if (r < 0)
550 return r;
551
552 r = sd_netlink_attach_event(m->rtnl, m->event, 0);
553 if (r < 0)
554 return r;
555
556 r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
557 if (r < 0)
558 return r;
559
560 r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
561 if (r < 0)
562 return r;
563
564 r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m);
565 if (r < 0)
566 return r;
567
568 r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m);
569 if (r < 0)
570 return r;
571
572 return 0;
573 }
574
575 static int set_put_in_addr(Set *s, const struct in_addr *address) {
576 char *p;
577 int r;
578
579 assert(s);
580
581 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
582 if (r < 0)
583 return r;
584
585 r = set_consume(s, p);
586 if (r == -EEXIST)
587 return 0;
588
589 return r;
590 }
591
592 static int set_put_in_addrv(Set *s, const struct in_addr *addresses, int n) {
593 int r, i, c = 0;
594
595 assert(s);
596 assert(n <= 0 || addresses);
597
598 for (i = 0; i < n; i++) {
599 r = set_put_in_addr(s, addresses+i);
600 if (r < 0)
601 return r;
602
603 c += r;
604 }
605
606 return c;
607 }
608
609 static void print_string_set(FILE *f, const char *field, Set *s) {
610 bool space = false;
611 Iterator i;
612 char *p;
613
614 if (set_isempty(s))
615 return;
616
617 fputs(field, f);
618
619 SET_FOREACH(p, s, i) {
620 if (space)
621 fputc(' ', f);
622 fputs(p, f);
623 space = true;
624 }
625 fputc('\n', f);
626 }
627
628 static int manager_save(Manager *m) {
629 _cleanup_set_free_free_ Set *dns = NULL, *ntp = NULL, *domains = NULL;
630 Link *link;
631 Iterator i;
632 _cleanup_free_ char *temp_path = NULL;
633 _cleanup_fclose_ FILE *f = NULL;
634 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
635 const char *operstate_str;
636 int r;
637
638 assert(m);
639 assert(m->state_file);
640
641 /* We add all NTP and DNS server to a set, to filter out duplicates */
642 dns = set_new(&string_hash_ops);
643 if (!dns)
644 return -ENOMEM;
645
646 ntp = set_new(&string_hash_ops);
647 if (!ntp)
648 return -ENOMEM;
649
650 domains = set_new(&string_hash_ops);
651 if (!domains)
652 return -ENOMEM;
653
654 HASHMAP_FOREACH(link, m->links, i) {
655 if (link->flags & IFF_LOOPBACK)
656 continue;
657
658 if (link->operstate > operstate)
659 operstate = link->operstate;
660
661 if (!link->network)
662 continue;
663
664 /* First add the static configured entries */
665 r = set_put_strdupv(dns, link->network->dns);
666 if (r < 0)
667 return r;
668
669 r = set_put_strdupv(ntp, link->network->ntp);
670 if (r < 0)
671 return r;
672
673 r = set_put_strdupv(domains, link->network->domains);
674 if (r < 0)
675 return r;
676
677 if (!link->dhcp_lease)
678 continue;
679
680 /* Secondly, add the entries acquired via DHCP */
681 if (link->network->dhcp_dns) {
682 const struct in_addr *addresses;
683
684 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
685 if (r > 0) {
686 r = set_put_in_addrv(dns, addresses, r);
687 if (r < 0)
688 return r;
689 } else if (r < 0 && r != -ENODATA)
690 return r;
691 }
692
693 if (link->network->dhcp_ntp) {
694 const struct in_addr *addresses;
695
696 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
697 if (r > 0) {
698 r = set_put_in_addrv(ntp, addresses, r);
699 if (r < 0)
700 return r;
701 } else if (r < 0 && r != -ENODATA)
702 return r;
703 }
704
705 if (link->network->dhcp_domains) {
706 const char *domainname;
707
708 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
709 if (r >= 0) {
710 r = set_put_strdup(domains, domainname);
711 if (r < 0)
712 return r;
713 } else if (r != -ENODATA)
714 return r;
715 }
716 }
717
718 operstate_str = link_operstate_to_string(operstate);
719 assert(operstate_str);
720
721 r = fopen_temporary(m->state_file, &f, &temp_path);
722 if (r < 0)
723 return r;
724
725 fchmod(fileno(f), 0644);
726
727 fprintf(f,
728 "# This is private data. Do not parse.\n"
729 "OPER_STATE=%s\n", operstate_str);
730
731 print_string_set(f, "DNS=", dns);
732 print_string_set(f, "NTP=", ntp);
733 print_string_set(f, "DOMAINS=", domains);
734
735 r = fflush_and_check(f);
736 if (r < 0)
737 goto fail;
738
739 if (rename(temp_path, m->state_file) < 0) {
740 r = -errno;
741 goto fail;
742 }
743
744 if (m->operational_state != operstate) {
745 m->operational_state = operstate;
746 r = manager_send_changed(m, "OperationalState", NULL);
747 if (r < 0)
748 log_error_errno(r, "Could not emit changed OperationalState: %m");
749 }
750
751 m->dirty = false;
752
753 return 0;
754
755 fail:
756 (void) unlink(m->state_file);
757 (void) unlink(temp_path);
758
759 return log_error_errno(r, "Failed to save network state to %s: %m", m->state_file);
760 }
761
762 static int manager_dirty_handler(sd_event_source *s, void *userdata) {
763 Manager *m = userdata;
764 Link *link;
765 Iterator i;
766 int r;
767
768 assert(m);
769
770 if (m->dirty)
771 manager_save(m);
772
773 SET_FOREACH(link, m->dirty_links, i) {
774 r = link_save(link);
775 if (r >= 0)
776 link_clean(link);
777 }
778
779 return 1;
780 }
781
782 int manager_new(Manager **ret) {
783 _cleanup_manager_free_ Manager *m = NULL;
784 int r;
785
786 m = new0(Manager, 1);
787 if (!m)
788 return -ENOMEM;
789
790 m->state_file = strdup("/run/systemd/netif/state");
791 if (!m->state_file)
792 return -ENOMEM;
793
794 r = sd_event_default(&m->event);
795 if (r < 0)
796 return r;
797
798 sd_event_set_watchdog(m->event, true);
799
800 sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
801 sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
802
803 r = sd_event_add_post(m->event, NULL, manager_dirty_handler, m);
804 if (r < 0)
805 return r;
806
807 r = manager_connect_rtnl(m);
808 if (r < 0)
809 return r;
810
811 r = manager_connect_udev(m);
812 if (r < 0)
813 return r;
814
815 m->netdevs = hashmap_new(&string_hash_ops);
816 if (!m->netdevs)
817 return -ENOMEM;
818
819 LIST_HEAD_INIT(m->networks);
820
821 r = setup_default_address_pool(m);
822 if (r < 0)
823 return r;
824
825 *ret = m;
826 m = NULL;
827
828 return 0;
829 }
830
831 void manager_free(Manager *m) {
832 Network *network;
833 NetDev *netdev;
834 Link *link;
835 AddressPool *pool;
836
837 if (!m)
838 return;
839
840 free(m->state_file);
841
842 while ((link = hashmap_first(m->links)))
843 link_unref(link);
844 hashmap_free(m->links);
845
846 while ((network = m->networks))
847 network_free(network);
848
849 hashmap_free(m->networks_by_name);
850
851 while ((netdev = hashmap_first(m->netdevs)))
852 netdev_unref(netdev);
853 hashmap_free(m->netdevs);
854
855 while ((pool = m->address_pools))
856 address_pool_free(pool);
857
858 sd_netlink_unref(m->rtnl);
859 sd_event_unref(m->event);
860
861 sd_event_source_unref(m->udev_event_source);
862 udev_monitor_unref(m->udev_monitor);
863 udev_unref(m->udev);
864
865 sd_bus_unref(m->bus);
866 sd_bus_slot_unref(m->prepare_for_sleep_slot);
867 sd_event_source_unref(m->bus_retry_event_source);
868
869 free(m);
870 }
871
872 static bool manager_check_idle(void *userdata) {
873 Manager *m = userdata;
874 Link *link;
875 Iterator i;
876
877 assert(m);
878
879 HASHMAP_FOREACH(link, m->links, i) {
880 /* we are not woken on udev activity, so let's just wait for the
881 * pending udev event */
882 if (link->state == LINK_STATE_PENDING)
883 return false;
884
885 if (!link->network)
886 continue;
887
888 /* we are not woken on netork activity, so let's stay around */
889 if (link_lldp_enabled(link) ||
890 link_ipv4ll_enabled(link) ||
891 link_dhcp4_server_enabled(link) ||
892 link_dhcp4_enabled(link) ||
893 link_dhcp6_enabled(link))
894 return false;
895 }
896
897 return true;
898 }
899
900 int manager_run(Manager *m) {
901 Link *link;
902 Iterator i;
903
904 assert(m);
905
906 /* The dirty handler will deal with future serialization, but the first one
907 must be done explicitly. */
908
909 manager_save(m);
910
911 HASHMAP_FOREACH(link, m->links, i)
912 link_save(link);
913
914 if (m->bus)
915 return bus_event_loop_with_idle(
916 m->event,
917 m->bus,
918 "org.freedesktop.network1",
919 DEFAULT_EXIT_USEC,
920 manager_check_idle,
921 m);
922 else
923 /* failed to connect to the bus, so we lose exit-on-idle logic,
924 this should not happen except if dbus is not around at all */
925 return sd_event_loop(m->event);
926 }
927
928 int manager_load_config(Manager *m) {
929 int r;
930
931 /* update timestamp */
932 paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
933
934 r = netdev_load(m);
935 if (r < 0)
936 return r;
937
938 r = network_load(m);
939 if (r < 0)
940 return r;
941
942 return 0;
943 }
944
945 bool manager_should_reload(Manager *m) {
946 return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
947 }
948
949 int manager_rtnl_enumerate_links(Manager *m) {
950 _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL, *reply = NULL;
951 sd_netlink_message *link;
952 int r;
953
954 assert(m);
955 assert(m->rtnl);
956
957 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
958 if (r < 0)
959 return r;
960
961 r = sd_netlink_message_request_dump(req, true);
962 if (r < 0)
963 return r;
964
965 r = sd_netlink_call(m->rtnl, req, 0, &reply);
966 if (r < 0)
967 return r;
968
969 for (link = reply; link; link = sd_netlink_message_next(link)) {
970 int k;
971
972 m->enumerating = true;
973
974 k = manager_rtnl_process_link(m->rtnl, link, m);
975 if (k < 0)
976 r = k;
977
978 m->enumerating = false;
979 }
980
981 return r;
982 }
983
984 int manager_rtnl_enumerate_addresses(Manager *m) {
985 _cleanup_netlink_message_unref_ sd_netlink_message *req = NULL, *reply = NULL;
986 sd_netlink_message *addr;
987 int r;
988
989 assert(m);
990 assert(m->rtnl);
991
992 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, 0);
993 if (r < 0)
994 return r;
995
996 r = sd_netlink_message_request_dump(req, true);
997 if (r < 0)
998 return r;
999
1000 r = sd_netlink_call(m->rtnl, req, 0, &reply);
1001 if (r < 0)
1002 return r;
1003
1004 for (addr = reply; addr; addr = sd_netlink_message_next(addr)) {
1005 int k;
1006
1007 m->enumerating = true;
1008
1009 k = manager_rtnl_process_address(m->rtnl, addr, m);
1010 if (k < 0)
1011 r = k;
1012
1013 m->enumerating = false;
1014 }
1015
1016 return r;
1017 }
1018
1019 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
1020 AddressPool *p;
1021 int r;
1022
1023 assert(m);
1024 assert(prefixlen > 0);
1025 assert(found);
1026
1027 LIST_FOREACH(address_pools, p, m->address_pools) {
1028 if (p->family != family)
1029 continue;
1030
1031 r = address_pool_acquire(p, prefixlen, found);
1032 if (r != 0)
1033 return r;
1034 }
1035
1036 return 0;
1037 }
1038
1039 Link* manager_find_uplink(Manager *m, Link *exclude) {
1040 _cleanup_free_ struct local_address *gateways = NULL;
1041 int n, i;
1042
1043 assert(m);
1044
1045 /* Looks for a suitable "uplink", via black magic: an
1046 * interface that is up and where the default route with the
1047 * highest priority points to. */
1048
1049 n = local_gateways(m->rtnl, 0, AF_UNSPEC, &gateways);
1050 if (n < 0) {
1051 log_warning_errno(n, "Failed to determine list of default gateways: %m");
1052 return NULL;
1053 }
1054
1055 for (i = 0; i < n; i++) {
1056 Link *link;
1057
1058 link = hashmap_get(m->links, INT_TO_PTR(gateways[i].ifindex));
1059 if (!link) {
1060 log_debug("Weird, found a gateway for a link we don't know. Ignoring.");
1061 continue;
1062 }
1063
1064 if (link == exclude)
1065 continue;
1066
1067 if (link->operstate < LINK_OPERSTATE_ROUTABLE)
1068 continue;
1069
1070 return link;
1071 }
1072
1073 return NULL;
1074 }
1075
1076 void manager_dirty(Manager *manager) {
1077 assert(manager);
1078
1079 /* the serialized state in /run is no longer up-to-date */
1080 manager->dirty = true;
1081 }