1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
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.
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.
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/>.
22 #include <sys/socket.h>
25 #include "conf-parser.h"
26 #include "path-util.h"
28 #include "networkd-netdev.h"
29 #include "networkd-link.h"
30 #include "network-internal.h"
31 #include "libudev-private.h"
32 #include "udev-util.h"
33 #include "rtnl-util.h"
39 const char* const network_dirs[] = {
40 "/etc/systemd/network",
41 "/run/systemd/network",
42 "/usr/lib/systemd/network",
44 "/lib/systemd/network",
48 static int setup_default_address_pool(Manager *m) {
54 /* Add in the well-known private address ranges. */
56 r = address_pool_new_from_string(m, &p, AF_INET6, "fc00::", 7);
60 r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
64 r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
68 r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
75 int manager_new(Manager **ret) {
76 _cleanup_manager_free_ Manager *m = NULL;
83 m->state_file = strdup("/run/systemd/netif/state");
87 r = sd_event_default(&m->event);
91 sd_event_set_watchdog(m->event, true);
93 sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
94 sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
96 r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR,
101 r = sd_bus_default_system(&m->bus);
102 if (r < 0 && r != -ENOENT) /* TODO: drop when we can rely on kdbus */
105 /* udev does not initialize devices inside containers,
106 * so we rely on them being already initialized before
107 * entering the container */
108 if (detect_container(NULL) <= 0) {
109 m->udev = udev_new();
113 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
114 if (!m->udev_monitor)
118 m->netdevs = hashmap_new(string_hash_func, string_compare_func);
122 LIST_HEAD_INIT(m->networks);
124 r = setup_default_address_pool(m);
134 void manager_free(Manager *m) {
145 udev_monitor_unref(m->udev_monitor);
147 sd_bus_unref(m->bus);
148 sd_event_source_unref(m->udev_event_source);
149 sd_event_source_unref(m->sigterm_event_source);
150 sd_event_source_unref(m->sigint_event_source);
151 sd_event_unref(m->event);
153 while ((link = hashmap_first(m->links)))
155 hashmap_free(m->links);
157 while ((network = m->networks))
158 network_free(network);
160 while ((netdev = hashmap_first(m->netdevs)))
161 netdev_unref(netdev);
162 hashmap_free(m->netdevs);
164 while ((pool = m->address_pools))
165 address_pool_free(pool);
167 sd_rtnl_unref(m->rtnl);
172 int manager_load_config(Manager *m) {
175 /* update timestamp */
176 paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
189 bool manager_should_reload(Manager *m) {
190 return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
193 static int manager_udev_process_link(Manager *m, struct udev_device *device) {
200 if (!streq_ptr(udev_device_get_action(device), "add"))
203 ifindex = udev_device_get_ifindex(device);
205 log_debug("ignoring udev ADD event for device with invalid ifindex");
209 r = link_get(m, ifindex, &link);
215 r = link_initialized(link, device);
222 static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
223 Manager *m = userdata;
225 NetDev *netdev = NULL;
234 r = sd_rtnl_message_get_type(message, &type);
236 log_warning("rtnl: could not get message type");
240 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
241 if (r < 0 || ifindex <= 0) {
242 log_warning("rtnl: received link message without valid ifindex");
245 link_get(m, ifindex, &link);
247 r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &name);
248 if (r < 0 || !name) {
249 log_warning("rtnl: received link message without valid ifname");
252 netdev_get(m, name, &netdev);
257 /* link is new, so add it */
258 r = link_add(m, message, &link);
260 log_debug("could not add new link: %s",
267 /* netdev exists, so make sure the ifindex matches */
268 r = netdev_set_ifindex(netdev, message);
270 log_debug("could not set ifindex on netdev");
275 r = link_update(link, message);
288 assert_not_reached("Received invalid RTNL message type.");
294 int manager_rtnl_enumerate_links(Manager *m) {
295 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
296 sd_rtnl_message *link;
302 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
306 r = sd_rtnl_message_request_dump(req, true);
310 r = sd_rtnl_call(m->rtnl, req, 0, &reply);
314 for (link = reply; link; link = sd_rtnl_message_next(link)) {
317 k = sd_rtnl_message_get_type(link, &type);
321 if (type != RTM_NEWLINK)
324 k = manager_rtnl_process_link(m->rtnl, link, m);
332 static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
333 Manager *m = userdata;
334 struct udev_monitor *monitor = m->udev_monitor;
335 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
337 device = udev_monitor_receive_device(monitor);
341 manager_udev_process_link(m, device);
345 int manager_udev_listen(Manager *m) {
348 if (detect_container(NULL) > 0)
351 assert(m->udev_monitor);
353 r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
355 log_error("Could not add udev monitor filter: %s", strerror(-r));
359 r = udev_monitor_enable_receiving(m->udev_monitor);
361 log_error("Could not enable udev monitor");
365 r = sd_event_add_io(m->event,
366 &m->udev_event_source,
367 udev_monitor_get_fd(m->udev_monitor),
368 EPOLLIN, manager_dispatch_link_udev,
376 int manager_rtnl_listen(Manager *m) {
381 r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
385 r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
389 r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
393 r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &link_rtnl_process_address, m);
397 r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &link_rtnl_process_address, m);
404 int manager_bus_listen(Manager *m) {
409 if (!m->bus) /* TODO: drop when we can rely on kdbus */
412 r = sd_bus_attach_event(m->bus, m->event, 0);
419 static int set_put_in_addr(Set *s, const struct in_addr *address) {
425 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
429 r = set_consume(s, p);
436 static int set_put_in_addrv(Set *s, const struct in_addr *addresses, int n) {
440 assert(n <= 0 || addresses);
442 for (i = 0; i < n; i++) {
443 r = set_put_in_addr(s, addresses+i);
453 static void print_string_set(FILE *f, const char *field, Set *s) {
463 SET_FOREACH(p, s, i) {
472 int manager_save(Manager *m) {
473 _cleanup_set_free_free_ Set *dns = NULL, *ntp = NULL, *domains = NULL;
476 _cleanup_free_ char *temp_path = NULL;
477 _cleanup_fclose_ FILE *f = NULL;
478 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
479 const char *operstate_str;
483 assert(m->state_file);
485 /* We add all NTP and DNS server to a set, to filter out duplicates */
486 dns = set_new(string_hash_func, string_compare_func);
490 ntp = set_new(string_hash_func, string_compare_func);
494 domains = set_new(string_hash_func, string_compare_func);
498 HASHMAP_FOREACH(link, m->links, i) {
499 if (link->flags & IFF_LOOPBACK)
502 if (link->operstate > operstate)
503 operstate = link->operstate;
508 /* First add the static configured entries */
509 r = set_put_strdupv(dns, link->network->dns);
513 r = set_put_strdupv(ntp, link->network->ntp);
517 r = set_put_strdupv(domains, link->network->domains);
521 if (!link->dhcp_lease)
524 /* Secondly, add the entries acquired via DHCP */
525 if (link->network->dhcp_dns) {
526 const struct in_addr *addresses;
528 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
530 r = set_put_in_addrv(dns, addresses, r);
533 } else if (r < 0 && r != -ENOENT)
537 if (link->network->dhcp_ntp) {
538 const struct in_addr *addresses;
540 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
542 r = set_put_in_addrv(ntp, addresses, r);
545 } else if (r < 0 && r != -ENOENT)
549 if (link->network->dhcp_domains) {
550 const char *domainname;
552 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
554 r = set_put_strdup(domains, domainname);
557 } else if (r != -ENOENT)
562 operstate_str = link_operstate_to_string(operstate);
563 assert(operstate_str);
565 r = fopen_temporary(m->state_file, &f, &temp_path);
569 fchmod(fileno(f), 0644);
572 "# This is private data. Do not parse.\n"
573 "OPER_STATE=%s\n", operstate_str);
575 print_string_set(f, "DNS=", dns);
576 print_string_set(f, "NTP=", ntp);
577 print_string_set(f, "DOMAINS=", domains);
579 r = fflush_and_check(f);
583 if (rename(temp_path, m->state_file) < 0) {
591 log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r));
592 unlink(m->state_file);
597 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
602 assert(prefixlen > 0);
605 LIST_FOREACH(address_pools, p, m->address_pools) {
606 if (p->family != family)
609 r = address_pool_acquire(p, prefixlen, found);