}
static void link_free(Link *link) {
+ Address *address;
+
if (!link)
return;
assert(link->manager);
+ while ((address = link->addresses)) {
+ LIST_REMOVE(addresses, link->addresses, address);
+ address_free(address);
+ }
+
sd_dhcp_client_unref(link->dhcp_client);
sd_dhcp_lease_unref(link->dhcp_lease);
struct in_addr netmask;
struct in_addr gateway;
unsigned prefixlen;
- struct in_addr *nameservers;
- size_t nameservers_size;
int r;
assert(client);
link->dhcp_lease = lease;
- if (link->network->dhcp_dns) {
- r = sd_dhcp_lease_get_dns(lease, &nameservers, &nameservers_size);
- if (r >= 0) {
- r = manager_update_resolv_conf(link->manager);
- if (r < 0)
- log_error_link(link, "Failed to update resolv.conf");
- }
- }
-
if (link->network->dhcp_mtu) {
uint16_t mtu;
if (r < 0)
/* if we got a message without operstate, take it to mean
the state was unchanged */
- operstate = link->operstate;
+ operstate = link->kernel_operstate;
- if ((link->flags == flags) && (link->operstate == operstate))
+ if ((link->flags == flags) && (link->kernel_operstate == operstate))
return 0;
if (link->flags != flags) {
unknown_flags_removed);
}
- carrier_gained = !link_has_carrier(link->flags, link->operstate) &&
+ carrier_gained = !link_has_carrier(link->flags, link->kernel_operstate) &&
link_has_carrier(flags, operstate);
- carrier_lost = link_has_carrier(link->flags, link->operstate) &&
+ carrier_lost = link_has_carrier(link->flags, link->kernel_operstate) &&
!link_has_carrier(flags, operstate);
link->flags = flags;
- link->operstate = operstate;
+ link->kernel_operstate = operstate;
link_save(link);
}
}
- if (link_has_carrier(link->flags, link->operstate)) {
+ if (link_has_carrier(link->flags, link->kernel_operstate)) {
r = link_acquire_conf(link);
if (r < 0)
return r;
if (device)
link->udev_device = udev_device_ref(device);
- log_info_link(link, "udev initialized link");
+ log_debug_link(link, "udev initialized link");
r = network_get(link->manager, device, link->ifname, &link->mac, &network);
if (r == -ENOENT) {
Link *link = NULL;
uint16_t type;
_cleanup_address_free_ Address *address = NULL;
+ Address *ad;
char buf[INET6_ADDRSTRLEN];
+ bool address_dropped = false;
int r, ifindex;
assert(rtnl);
r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen);
if (r < 0) {
- log_warning_link(link, "rtnl: recevied address with invalid prefixlen, ignoring");
+ log_warning_link(link, "rtnl: received address with invalid prefixlen, ignoring");
+ return 0;
+ }
+
+ r = sd_rtnl_message_addr_get_scope(message, &address->scope);
+ if (r < 0) {
+ log_warning_link(link, "rtnl: received address with invalid scope, ignoring");
return 0;
}
return 0;
}
+ LIST_FOREACH(addresses, ad, link->addresses) {
+ if (address_equal(ad, address)) {
+ LIST_REMOVE(addresses, link->addresses, ad);
+
+ address_free(ad);
+
+ address_dropped = true;
+
+ break;
+ }
+ }
+
switch (type) {
case RTM_NEWADDR:
- log_info_link(link, "added address: %s/%u", buf,
- address->prefixlen);
- break;
+ if (!address_dropped)
+ log_debug_link(link, "added address: %s/%u", buf,
+ address->prefixlen);
+
+ LIST_PREPEND(addresses, link->addresses, address);
+ address = NULL;
+ break;
case RTM_DELADDR:
- log_info_link(link, "removed address: %s/%u", buf,
- address->prefixlen);
+ if (address_dropped)
+ log_debug_link(link, "removed address: %s/%u", buf,
+ address->prefixlen);
+
break;
default:
assert_not_reached("Received invalid RTNL message type");
if (udev_device_get_is_initialized(device) <= 0) {
/* not yet ready */
- log_info_link(link, "udev initializing link...");
+ log_debug_link(link, "udev initializing link...");
return 0;
}
}
return link_update_flags(link, m);
}
+static void serialize_addresses(FILE *f, const char *key, Address *address) {
+ Address *ad;
+
+ assert(f);
+ assert(key);
+
+ if (!address)
+ return;
+
+ fprintf(f, "%s=", key);
+
+ LIST_FOREACH(addresses, ad, address) {
+ char buf[INET6_ADDRSTRLEN];
+
+ if (inet_ntop(ad->family, &ad->in_addr, buf, INET6_ADDRSTRLEN))
+ fprintf(f, "%s%s", buf, (ad->addresses_next) ? " ": "");
+ }
+
+ fputs("\n", f);
+}
+
+static void link_update_operstate(Link *link) {
+
+ assert(link);
+
+ if (link->kernel_operstate == IF_OPER_DORMANT)
+ link->operstate = LINK_OPERSTATE_DORMANT;
+ else if (link_has_carrier(link->flags, link->kernel_operstate)) {
+ Address *address;
+ uint8_t scope = RT_SCOPE_NOWHERE;
+
+ /* if we have carrier, check what addresses we have */
+ LIST_FOREACH(addresses, address, link->addresses) {
+ if (address->scope < scope)
+ scope = address->scope;
+ }
+
+ if (scope < RT_SCOPE_SITE)
+ /* universally accessible addresses found */
+ link->operstate = LINK_OPERSTATE_ROUTABLE;
+ else if (scope < RT_SCOPE_HOST)
+ /* only link or site local addresses found */
+ link->operstate = LINK_OPERSTATE_DEGRADED;
+ else
+ /* no useful addresses found */
+ link->operstate = LINK_OPERSTATE_CARRIER;
+ } else
+ link->operstate = LINK_OPERSTATE_UNKNOWN;
+}
+
int link_save(Link *link) {
_cleanup_free_ char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL;
- const char *admin_state, *oper_state = "unknown";
+ const char *admin_state, *oper_state;
int r;
assert(link);
assert(link->lease_file);
assert(link->manager);
+ link_update_operstate(link);
+
r = manager_save(link->manager);
if (r < 0)
return r;
admin_state = link_state_to_string(link->state);
assert(admin_state);
- if (link->operstate == IF_OPER_DORMANT)
- oper_state = "dormant";
- else if (link_has_carrier(link->flags, link->operstate))
- oper_state = "carrier";
+ oper_state = link_operstate_to_string(link->operstate);
+ assert(oper_state);
r = fopen_temporary(link->state_file, &f, &temp_path);
if (r < 0)
"FLAGS=%u\n",
admin_state, oper_state, link->flags);
+ if (link->network) {
+ serialize_addresses(f, "DNS", link->network->dns);
+ serialize_addresses(f, "NTP", link->network->ntp);
+ }
+
if (link->dhcp_lease) {
r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
if (r < 0)
goto finish;
- fprintf(f, "DHCP_LEASE=%s\n", link->lease_file);
+ fprintf(f,
+ "DHCP_LEASE=%s\n"
+ "DHCP_USE_DNS=%s\n"
+ "DHCP_USE_NTP=%s\n",
+ link->lease_file,
+ yes_no(link->network->dhcp_dns),
+ yes_no(link->network->dhcp_ntp));
} else
unlink(link->lease_file);
};
DEFINE_STRING_TABLE_LOOKUP(link_state, LinkState);
+
+static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = {
+ [LINK_OPERSTATE_UNKNOWN] = "unknown",
+ [LINK_OPERSTATE_DORMANT] = "dormant",
+ [LINK_OPERSTATE_CARRIER] = "carrier",
+ [LINK_OPERSTATE_DEGRADED] = "degraded",
+ [LINK_OPERSTATE_ROUTABLE] = "routable",
+};
+
+DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState);