along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
-#include <resolv.h>
+#include <sys/socket.h>
#include <linux/if.h>
+#include "conf-parser.h"
#include "path-util.h"
#include "networkd.h"
+#include "network-internal.h"
#include "libudev-private.h"
#include "udev-util.h"
#include "rtnl-util.h"
if (!m)
return -ENOMEM;
- m->state_file = strdup("/run/systemd/network/state");
+ m->state_file = strdup("/run/systemd/netif/state");
if (!m->state_file)
return -ENOMEM;
sd_event_set_watchdog(m->event, true);
- r = sd_rtnl_open(&m->rtnl, RTMGRP_LINK | RTMGRP_IPV4_IFADDR);
+ r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR,
+ RTNLGRP_IPV6_IFADDR);
if (r < 0)
return r;
static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
Manager *m = userdata;
Link *link = NULL;
+ NetDev *netdev = NULL;
uint16_t type;
char *name;
int r, ifindex;
if (r < 0 || ifindex <= 0) {
log_warning("rtnl: received link message without valid ifindex");
return 0;
- }
-
- link_get(m, ifindex, &link);
- if (type == RTM_DELLINK)
- link_drop(link);
- else if (!link) {
- /* link is new, so add it */
- r = link_add(m, message, &link);
- if (r < 0) {
- log_debug("could not add new link");
- return 0;
- }
- }
+ } else
+ link_get(m, ifindex, &link);
r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &name);
- if (r < 0)
+ if (r < 0 || !name) {
log_warning("rtnl: received link message without valid ifname");
- else {
- NetDev *netdev = NULL;
-
+ return 0;
+ } else
netdev_get(m, name, &netdev);
- if (type == RTM_DELLINK)
- netdev_drop(netdev);
- else if (netdev) {
+
+ switch (type) {
+ case RTM_NEWLINK:
+ if (!link) {
+ /* link is new, so add it */
+ r = link_add(m, message, &link);
+ if (r < 0) {
+ log_debug("could not add new link");
+ return 0;
+ }
+ }
+
+ if (netdev) {
+ /* netdev exists, so make sure the ifindex matches */
r = netdev_set_ifindex(netdev, message);
if (r < 0) {
log_debug("could not set ifindex on netdev");
return 0;
}
}
- }
- if (type == RTM_NEWLINK) {
r = link_update(link, message);
if (r < 0)
return 0;
+
+ break;
+
+ case RTM_DELLINK:
+ link_drop(link);
+ netdev_drop(netdev);
+
+ break;
+
+ default:
+ assert_not_reached("Received invalid RTNL message type.");
}
return 1;
if (r < 0)
return r;
+ r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &link_rtnl_process_address, m);
+ if (r < 0)
+ return r;
+
+ r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &link_rtnl_process_address, m);
+ if (r < 0)
+ return r;
+
return 0;
}
return 0;
}
-static void append_dns(FILE *f, struct in_addr *dns, unsigned char family, unsigned *count) {
- char buf[INET6_ADDRSTRLEN];
- const char *address;
-
- address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN);
- if (!address) {
- log_warning("Invalid DNS address. Ignoring.");
- return;
- }
-
- if (*count == MAXNS)
- fputs("# Too many DNS servers configured, the following entries "
- "will be ignored\n", f);
-
- fprintf(f, "nameserver %s\n", address);
-
- (*count) ++;
-}
-
-int manager_update_resolv_conf(Manager *m) {
- _cleanup_free_ char *temp_path = NULL;
- _cleanup_fclose_ FILE *f = NULL;
- Link *link;
- Iterator i;
- unsigned count = 0;
- const char *domainname = NULL;
- int r;
-
- assert(m);
-
- r = fopen_temporary("/run/systemd/network/resolv.conf", &f, &temp_path);
- if (r < 0)
- return r;
-
- fchmod(fileno(f), 0644);
-
- fputs("# This file is managed by systemd-networkd(8). Do not edit.\n#\n"
- "# Third party programs must not access this file directly, but\n"
- "# only through the symlink at /etc/resolv.conf. To manage\n"
- "# resolv.conf(5) in a different way, replace the symlink by a\n"
- "# static file or a different symlink.\n\n", f);
-
- HASHMAP_FOREACH(link, m->links, i) {
- if (link->dhcp_lease) {
- struct in_addr *nameservers;
- size_t nameservers_size;
-
- if (link->network->dhcp_dns) {
- r = sd_dhcp_lease_get_dns(link->dhcp_lease, &nameservers, &nameservers_size);
- if (r >= 0) {
- unsigned j;
-
- for (j = 0; j < nameservers_size; j++)
- append_dns(f, &nameservers[j], AF_INET, &count);
- }
- }
-
- if (link->network->dhcp_domainname && !domainname) {
- r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
- if (r >= 0)
- fprintf(f, "domain %s\n", domainname);
- }
- }
- }
-
- HASHMAP_FOREACH(link, m->links, i) {
- if (link->network && link->network->dns) {
- Address *address;
- Iterator j;
-
- SET_FOREACH(address, link->network->dns, j) {
- append_dns(f, &address->in_addr.in,
- address->family, &count);
- }
- }
- }
-
- fflush(f);
-
- if (ferror(f) || rename(temp_path, "/run/systemd/network/resolv.conf") < 0) {
- r = -errno;
- unlink("/run/systemd/network/resolv.conf");
- unlink(temp_path);
- return r;
- }
-
- return 0;
-}
-
int manager_save(Manager *m) {
Link *link;
Iterator i;
_cleanup_free_ char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
- const char *oper_state = "unknown";
- bool dormant, carrier;
+ LinkOperationalState operstate = LINK_OPERSTATE_UNKNOWN;
+ const char *operstate_str;
int r;
assert(m);
if (link->flags & IFF_LOOPBACK)
continue;
- if (link_has_carrier(link->flags, link->operstate))
- carrier = true;
- else if (link->operstate == IF_OPER_DORMANT)
- dormant = true;
+ if (link->operstate > operstate)
+ operstate = link->operstate;
}
- if (carrier)
- oper_state = "carrier";
- else if (dormant)
- oper_state = "dormant";
+ operstate_str = link_operstate_to_string(operstate);
+ assert(operstate_str);
r = fopen_temporary(m->state_file, &f, &temp_path);
if (r < 0)
fprintf(f,
"# This is private data. Do not parse.\n"
- "OPER_STATE=%s\n", oper_state);
+ "OPER_STATE=%s\n", operstate_str);
fflush(f);