networkd: link - (de)serialize IPv4LL and DHCPv4 addresses

This initializes the clients to try rebinding the preexisting
addresses before doing anything else.
This commit is contained in:
Tom Gundersen 2015-10-01 22:29:50 +02:00
parent c4a03a5669
commit 0bc70f1d9c
3 changed files with 73 additions and 11 deletions

View File

@ -533,9 +533,11 @@ int dhcp4_configure(Link *link) {
assert(link->network);
assert(link->network->dhcp & ADDRESS_FAMILY_IPV4);
r = sd_dhcp_client_new(&link->dhcp_client);
if (r < 0)
return r;
if (!link->dhcp_client) {
r = sd_dhcp_client_new(&link->dhcp_client);
if (r < 0)
return r;
}
r = sd_dhcp_client_attach_event(link->dhcp_client, NULL, 0);
if (r < 0)

View File

@ -208,9 +208,11 @@ int ipv4ll_configure(Link *link) {
assert(link->network);
assert(link->network->link_local & ADDRESS_FAMILY_IPV4);
r = sd_ipv4ll_new(&link->ipv4ll);
if (r < 0)
return r;
if (!link->ipv4ll) {
r = sd_ipv4ll_new(&link->ipv4ll);
if (r < 0)
return r;
}
if (link->udev_device) {
r = net_get_unique_predictable_data(link->udev_device, seed);

View File

@ -2133,7 +2133,11 @@ int link_initialized(Link *link, struct udev_device *device) {
}
static int link_load(Link *link) {
_cleanup_free_ char *network_file = NULL, *addresses = NULL;
_cleanup_free_ char *network_file = NULL,
*addresses = NULL,
*dhcp4_address = NULL,
*ipv4ll_address = NULL;
union in_addr_union address;
int r;
assert(link);
@ -2141,6 +2145,8 @@ static int link_load(Link *link) {
r = parse_env_file(link->state_file, NEWLINE,
"NETWORK_FILE", &network_file,
"ADDRESSES", &addresses,
"DHCP4_ADDRESS", &dhcp4_address,
"IPV4LL_ADDRESS", &ipv4ll_address,
NULL);
if (r < 0 && r != -ENOENT)
return log_link_error_errno(link, r, "Failed to read %s: %m", link->state_file);
@ -2182,7 +2188,6 @@ network_file_fail:
char *prefixlen_str;
int family;
unsigned char prefixlen;
union in_addr_union address;
prefixlen_str = strchr(*address_str, '/');
if (!prefixlen_str) {
@ -2210,6 +2215,42 @@ network_file_fail:
}
}
if (dhcp4_address) {
r = in_addr_from_string(AF_INET, dhcp4_address, &address);
if (r < 0) {
log_link_debug_errno(link, r, "Falied to parse DHCPv4 address %s: %m", dhcp4_address);
goto dhcp4_address_fail;
}
r = sd_dhcp_client_new(&link->dhcp_client);
if (r < 0)
return log_link_error_errno(link, r, "Falied to create DHCPv4 client: %m");
r = sd_dhcp_client_set_request_address(link->dhcp_client, &address.in);
if (r < 0)
return log_link_error_errno(link, r, "Falied to set inital DHCPv4 address %s: %m", dhcp4_address);
}
dhcp4_address_fail:
if (ipv4ll_address) {
r = in_addr_from_string(AF_INET, ipv4ll_address, &address);
if (r < 0) {
log_link_debug_errno(link, r, "Falied to parse IPv4LL address %s: %m", ipv4ll_address);
goto ipv4ll_address_fail;
}
r = sd_ipv4ll_new(&link->ipv4ll);
if (r < 0)
return log_link_error_errno(link, r, "Falied to create IPv4LL client: %m");
r = sd_ipv4ll_set_address(link->ipv4ll, &address.in);
if (r < 0)
return log_link_error_errno(link, r, "Falied to set inital IPv4LL address %s: %m", ipv4ll_address);
}
ipv4ll_address_fail:
return 0;
}
@ -2696,15 +2737,21 @@ int link_save(Link *link) {
}
if (link->dhcp_lease) {
struct in_addr address;
const char *tz = NULL;
assert(link->network);
r = sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz);
if (r >= 0)
fprintf(f, "TIMEZONE=%s\n", tz);
}
if (link->dhcp_lease) {
assert(link->network);
r = sd_dhcp_lease_get_address(link->dhcp_lease, &address);
if (r >= 0) {
fputs("DHCP4_ADDRESS=", f);
serialize_in_addrs(f, &address, 1);
fputc('\n', f);
}
r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
if (r < 0)
@ -2716,6 +2763,17 @@ int link_save(Link *link) {
} else
unlink(link->lease_file);
if (link->ipv4ll) {
struct in_addr address;
r = sd_ipv4ll_get_address(link->ipv4ll, &address);
if (r >= 0) {
fputs("IPV4LL_ADDRESS=", f);
serialize_in_addrs(f, &address, 1);
fputc('\n', f);
}
}
if (link->lldp) {
assert(link->network);