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 /* use 8 MB for receive socket kernel queue. */
40 #define RCVBUF_SIZE (8*1024*1024)
42 const char* const network_dirs[] = {
43 "/etc/systemd/network",
44 "/run/systemd/network",
45 "/usr/lib/systemd/network",
47 "/lib/systemd/network",
51 static int setup_default_address_pool(Manager *m) {
57 /* Add in the well-known private address ranges. */
59 r = address_pool_new_from_string(m, &p, AF_INET6, "fc00::", 7);
63 r = address_pool_new_from_string(m, &p, AF_INET, "192.168.0.0", 16);
67 r = address_pool_new_from_string(m, &p, AF_INET, "172.16.0.0", 12);
71 r = address_pool_new_from_string(m, &p, AF_INET, "10.0.0.0", 8);
78 int manager_new(Manager **ret) {
79 _cleanup_manager_free_ Manager *m = NULL;
86 m->state_file = strdup("/run/systemd/netif/state");
90 r = sd_event_default(&m->event);
94 sd_event_set_watchdog(m->event, true);
96 sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
97 sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
99 r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR,
100 RTNLGRP_IPV6_IFADDR);
104 r = sd_rtnl_inc_rcvbuf(m->rtnl, RCVBUF_SIZE);
108 r = sd_bus_default_system(&m->bus);
109 if (r < 0 && r != -ENOENT) /* TODO: drop when we can rely on kdbus */
112 /* udev does not initialize devices inside containers,
113 * so we rely on them being already initialized before
114 * entering the container */
115 if (detect_container(NULL) <= 0) {
116 m->udev = udev_new();
120 m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev");
121 if (!m->udev_monitor)
125 m->netdevs = hashmap_new(&string_hash_ops);
129 LIST_HEAD_INIT(m->networks);
131 r = setup_default_address_pool(m);
141 void manager_free(Manager *m) {
152 udev_monitor_unref(m->udev_monitor);
154 sd_bus_unref(m->bus);
155 sd_event_source_unref(m->udev_event_source);
156 sd_event_unref(m->event);
158 while ((link = hashmap_first(m->links)))
160 hashmap_free(m->links);
162 while ((network = m->networks))
163 network_free(network);
165 while ((netdev = hashmap_first(m->netdevs)))
166 netdev_unref(netdev);
167 hashmap_free(m->netdevs);
169 while ((pool = m->address_pools))
170 address_pool_free(pool);
172 sd_rtnl_unref(m->rtnl);
177 int manager_load_config(Manager *m) {
180 /* update timestamp */
181 paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
194 bool manager_should_reload(Manager *m) {
195 return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
198 static int manager_udev_process_link(Manager *m, struct udev_device *device) {
205 if (!streq_ptr(udev_device_get_action(device), "add"))
208 ifindex = udev_device_get_ifindex(device);
210 log_debug("ignoring udev ADD event for device with invalid ifindex");
214 r = link_get(m, ifindex, &link);
220 r = link_initialized(link, device);
227 static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
228 Manager *m = userdata;
230 NetDev *netdev = NULL;
239 r = sd_rtnl_message_get_type(message, &type);
241 log_warning("rtnl: could not get message type");
245 r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
246 if (r < 0 || ifindex <= 0) {
247 log_warning("rtnl: received link message without valid ifindex");
250 link_get(m, ifindex, &link);
252 r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &name);
253 if (r < 0 || !name) {
254 log_warning("rtnl: received link message without valid ifname");
257 netdev_get(m, name, &netdev);
262 /* link is new, so add it */
263 r = link_add(m, message, &link);
265 log_debug("could not add new link: %s",
272 /* netdev exists, so make sure the ifindex matches */
273 r = netdev_set_ifindex(netdev, message);
275 log_debug("could not set ifindex on netdev");
280 r = link_update(link, message);
293 assert_not_reached("Received invalid RTNL message type.");
299 int manager_rtnl_enumerate_links(Manager *m) {
300 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL, *reply = NULL;
301 sd_rtnl_message *link;
307 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
311 r = sd_rtnl_message_request_dump(req, true);
315 r = sd_rtnl_call(m->rtnl, req, 0, &reply);
319 for (link = reply; link; link = sd_rtnl_message_next(link)) {
322 k = sd_rtnl_message_get_type(link, &type);
326 if (type != RTM_NEWLINK)
329 k = manager_rtnl_process_link(m->rtnl, link, m);
337 static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
338 Manager *m = userdata;
339 struct udev_monitor *monitor = m->udev_monitor;
340 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
342 device = udev_monitor_receive_device(monitor);
346 manager_udev_process_link(m, device);
350 int manager_udev_listen(Manager *m) {
353 if (detect_container(NULL) > 0)
356 assert(m->udev_monitor);
358 r = udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "net", NULL);
360 log_error("Could not add udev monitor filter: %s", strerror(-r));
364 r = udev_monitor_enable_receiving(m->udev_monitor);
366 log_error("Could not enable udev monitor");
370 r = sd_event_add_io(m->event,
371 &m->udev_event_source,
372 udev_monitor_get_fd(m->udev_monitor),
373 EPOLLIN, manager_dispatch_link_udev,
378 r = sd_event_source_set_description(m->udev_event_source, "networkd-udev");
385 int manager_rtnl_listen(Manager *m) {
390 r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
394 r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
398 r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
402 r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &link_rtnl_process_address, m);
406 r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &link_rtnl_process_address, m);
413 int manager_bus_listen(Manager *m) {
418 if (!m->bus) /* TODO: drop when we can rely on kdbus */
421 r = sd_bus_attach_event(m->bus, m->event, 0);
428 static int set_put_in_addr(Set *s, const struct in_addr *address) {
434 r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
438 r = set_consume(s, p);
445 static int set_put_in_addrv(Set *s, const struct in_addr *addresses, int n) {
449 assert(n <= 0 || addresses);
451 for (i = 0; i < n; i++) {
452 r = set_put_in_addr(s, addresses+i);
462 static void print_string_set(FILE *f, const char *field, Set *s) {
472 SET_FOREACH(p, s, i) {
481 int manager_save(Manager *m) {
482 _cleanup_set_free_free_ Set *dns = NULL, *ntp = NULL, *domains = NULL;
485 _cleanup_free_ char *temp_path = NULL;
486 _cleanup_fclose_ FILE *f = NULL;
487 LinkOperationalState operstate = LINK_OPERSTATE_OFF;
488 const char *operstate_str;
492 assert(m->state_file);
494 /* We add all NTP and DNS server to a set, to filter out duplicates */
495 dns = set_new(&string_hash_ops);
499 ntp = set_new(&string_hash_ops);
503 domains = set_new(&string_hash_ops);
507 HASHMAP_FOREACH(link, m->links, i) {
508 if (link->flags & IFF_LOOPBACK)
511 if (link->operstate > operstate)
512 operstate = link->operstate;
517 /* First add the static configured entries */
518 r = set_put_strdupv(dns, link->network->dns);
522 r = set_put_strdupv(ntp, link->network->ntp);
526 r = set_put_strdupv(domains, link->network->domains);
530 if (!link->dhcp_lease)
533 /* Secondly, add the entries acquired via DHCP */
534 if (link->network->dhcp_dns) {
535 const struct in_addr *addresses;
537 r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
539 r = set_put_in_addrv(dns, addresses, r);
542 } else if (r < 0 && r != -ENOENT)
546 if (link->network->dhcp_ntp) {
547 const struct in_addr *addresses;
549 r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
551 r = set_put_in_addrv(ntp, addresses, r);
554 } else if (r < 0 && r != -ENOENT)
558 if (link->network->dhcp_domains) {
559 const char *domainname;
561 r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
563 r = set_put_strdup(domains, domainname);
566 } else if (r != -ENOENT)
571 operstate_str = link_operstate_to_string(operstate);
572 assert(operstate_str);
574 r = fopen_temporary(m->state_file, &f, &temp_path);
578 fchmod(fileno(f), 0644);
581 "# This is private data. Do not parse.\n"
582 "OPER_STATE=%s\n", operstate_str);
584 print_string_set(f, "DNS=", dns);
585 print_string_set(f, "NTP=", ntp);
586 print_string_set(f, "DOMAINS=", domains);
588 r = fflush_and_check(f);
592 if (rename(temp_path, m->state_file) < 0) {
600 log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r));
601 unlink(m->state_file);
606 int manager_address_pool_acquire(Manager *m, int family, unsigned prefixlen, union in_addr_union *found) {
611 assert(prefixlen > 0);
614 LIST_FOREACH(address_pools, p, m->address_pools) {
615 if (p->family != family)
618 r = address_pool_acquire(p, prefixlen, found);