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_ops);
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_unref(m->event);
151 while ((link = hashmap_first(m->links)))
153 hashmap_free(m->links);
155 while ((network = m->networks))
156 network_free(network);
158 while ((netdev = hashmap_first(m->netdevs)))
159 netdev_unref(netdev);
160 hashmap_free(m->netdevs);
162 while ((pool = m->address_pools))
163 address_pool_free(pool);
165 sd_rtnl_unref(m->rtnl);
170 int manager_load_config(Manager *m) {
173 /* update timestamp */
174 paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
187 bool manager_should_reload(Manager *m) {
188 return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
191 static int manager_udev_process_link(Manager *m, struct udev_device *device) {
198 if (!streq_ptr(udev_device_get_action(device), "add"))
201 ifindex = udev_device_get_ifindex(device);
203 log_debug("ignoring udev ADD event for device with invalid ifindex");
207 r = link_get(m, ifindex, &link);
213 r = link_initialized(link, device);
220 static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
221 Manager *m = userdata;
223 NetDev *netdev = NULL;
232 r = sd_rtnl_message_get_type(message, &type);
234 log_warning("rtnl: could not get message type");
238 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
239 if (r < 0 || ifindex <= 0) {
240 log_warning("rtnl: received link message without valid ifindex");
243 link_get(m, ifindex, &link);
245 r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &name);
246 if (r < 0 || !name) {
247 log_warning("rtnl: received link message without valid ifname");
250 netdev_get(m, name, &netdev);
255 /* link is new, so add it */
256 r = link_add(m, message, &link);
258 log_debug("could not add new link: %s",
265 /* netdev exists, so make sure the ifindex matches */
266 r = netdev_set_ifindex(netdev, message);
268 log_debug("could not set ifindex on netdev");
273 r = link_update(link, message);
286 assert_not_reached("Received invalid RTNL message type.");
292 int manager_rtnl_enumerate_links(Manager *m) {
293 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
294 sd_rtnl_message *link;
300 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
304 r = sd_rtnl_message_request_dump(req, true);
308 r = sd_rtnl_call(m->rtnl, req, 0, &reply);
312 for (link = reply; link; link = sd_rtnl_message_next(link)) {
315 k = sd_rtnl_message_get_type(link, &type);
319 if (type != RTM_NEWLINK)
322 k = manager_rtnl_process_link(m->rtnl, link, m);
330 static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
331 Manager *m = userdata;
332 struct udev_monitor *monitor = m->udev_monitor;
333 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
335 device = udev_monitor_receive_device(monitor);
339 manager_udev_process_link(m, device);
343 int manager_udev_listen(Manager *m) {
346 if (detect_container(NULL) > 0)
349 assert(m->udev_monitor);
351 r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
353 log_error("Could not add udev monitor filter: %s", strerror(-r));
357 r = udev_monitor_enable_receiving(m->udev_monitor);
359 log_error("Could not enable udev monitor");
363 r = sd_event_add_io(m->event,
364 &m->udev_event_source,
365 udev_monitor_get_fd(m->udev_monitor),
366 EPOLLIN, manager_dispatch_link_udev,
371 r = sd_event_source_set_name(m->udev_event_source, "networkd-udev");
378 int manager_rtnl_listen(Manager *m) {
383 r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
387 r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
391 r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
395 r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &link_rtnl_process_address, m);
399 r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &link_rtnl_process_address, m);
406 int manager_bus_listen(Manager *m) {
411 if (!m->bus) /* TODO: drop when we can rely on kdbus */
414 r = sd_bus_attach_event(m->bus, m->event, 0);
421 static int set_put_in_addr(Set *s, const struct in_addr *address) {
427 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
431 r = set_consume(s, p);
438 static int set_put_in_addrv(Set *s, const struct in_addr *addresses, int n) {
442 assert(n <= 0 || addresses);
444 for (i = 0; i < n; i++) {
445 r = set_put_in_addr(s, addresses+i);
455 static void print_string_set(FILE *f, const char *field, Set *s) {
465 SET_FOREACH(p, s, i) {
474 int manager_save(Manager *m) {
475 _cleanup_set_free_free_ Set *dns = NULL, *ntp = NULL, *domains = NULL;
478 _cleanup_free_ char *temp_path = NULL;
479 _cleanup_fclose_ FILE *f = NULL;
480 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
481 const char *operstate_str;
485 assert(m->state_file);
487 /* We add all NTP and DNS server to a set, to filter out duplicates */
488 dns = set_new(&string_hash_ops);
492 ntp = set_new(&string_hash_ops);
496 domains = set_new(&string_hash_ops);
500 HASHMAP_FOREACH(link, m->links, i) {
501 if (link->flags & IFF_LOOPBACK)
504 if (link->operstate > operstate)
505 operstate = link->operstate;
510 /* First add the static configured entries */
511 r = set_put_strdupv(dns, link->network->dns);
515 r = set_put_strdupv(ntp, link->network->ntp);
519 r = set_put_strdupv(domains, link->network->domains);
523 if (!link->dhcp_lease)
526 /* Secondly, add the entries acquired via DHCP */
527 if (link->network->dhcp_dns) {
528 const struct in_addr *addresses;
530 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
532 r = set_put_in_addrv(dns, addresses, r);
535 } else if (r < 0 && r != -ENOENT)
539 if (link->network->dhcp_ntp) {
540 const struct in_addr *addresses;
542 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
544 r = set_put_in_addrv(ntp, addresses, r);
547 } else if (r < 0 && r != -ENOENT)
551 if (link->network->dhcp_domains) {
552 const char *domainname;
554 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
556 r = set_put_strdup(domains, domainname);
559 } else if (r != -ENOENT)
564 operstate_str = link_operstate_to_string(operstate);
565 assert(operstate_str);
567 r = fopen_temporary(m->state_file, &f, &temp_path);
571 fchmod(fileno(f), 0644);
574 "# This is private data. Do not parse.\n"
575 "OPER_STATE=%s\n", operstate_str);
577 print_string_set(f, "DNS=", dns);
578 print_string_set(f, "NTP=", ntp);
579 print_string_set(f, "DOMAINS=", domains);
581 r = fflush_and_check(f);
585 if (rename(temp_path, m->state_file) < 0) {
593 log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r));
594 unlink(m->state_file);
599 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
604 assert(prefixlen > 0);
607 LIST_FOREACH(address_pools, p, m->address_pools) {
608 if (p->family != family)
611 r = address_pool_acquire(p, prefixlen, found);