diff --git a/src/libsystemd-network/sd-dhcp-lease.c b/src/libsystemd-network/sd-dhcp-lease.c index acdcca87ecf..3e43ab635f0 100644 --- a/src/libsystemd-network/sd-dhcp-lease.c +++ b/src/libsystemd-network/sd-dhcp-lease.c @@ -126,7 +126,10 @@ int sd_dhcp_lease_get_router(sd_dhcp_lease *lease, struct in_addr *addr) { assert_return(lease, -EINVAL); assert_return(addr, -EINVAL); - addr->s_addr = lease->router; + if (lease->router != INADDR_ANY) + addr->s_addr = lease->router; + else + return -ENOENT; return 0; } @@ -297,6 +300,7 @@ int dhcp_lease_new(sd_dhcp_lease **ret) { if (!lease) return -ENOMEM; + lease->router = INADDR_ANY; lease->n_ref = REFCNT_INIT; *ret = lease; @@ -370,18 +374,16 @@ int dhcp_lease_save(sd_dhcp_lease *lease, const char *lease_file) { "# This is private data. Do not parse.\n" "ADDRESS=%s\n", inet_ntoa(address)); - r = sd_dhcp_lease_get_router(lease, &address); - if (r < 0) - goto finish; - - fprintf(f, "ROUTER=%s\n", inet_ntoa(address)); - r = sd_dhcp_lease_get_netmask(lease, &address); if (r < 0) goto finish; fprintf(f, "NETMASK=%s\n", inet_ntoa(address)); + r = sd_dhcp_lease_get_router(lease, &address); + if (r >= 0) + fprintf(f, "ROUTER=%s\n", inet_ntoa(address)); + r = sd_dhcp_lease_get_server_identifier(lease, &address); if (r >= 0) fprintf(f, "SERVER_ADDRESS=%s\n", @@ -474,11 +476,13 @@ int dhcp_lease_load(const char *lease_file, sd_dhcp_lease **ret) { lease->address = addr.s_addr; - r = inet_pton(AF_INET, router, &addr); - if (r < 0) - return r; + if (router) { + r = inet_pton(AF_INET, router, &addr); + if (r < 0) + return r; - lease->router = addr.s_addr; + lease->router = addr.s_addr; + } r = inet_pton(AF_INET, netmask, &addr); if (r < 0) diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index d3b2b14b217..058fbc24f11 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -301,55 +301,60 @@ static int link_enter_set_routes(Link *link) { struct in_addr gateway; r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); - if (r < 0) { - log_warning_link(link, "DHCP error: no router: %s", - strerror(-r)); + if (r < 0 && r != -ENOENT) { + log_warning_link(link, "DHCP error: %s", strerror(-r)); return r; } - r = route_new_dynamic(&route); - if (r < 0) { - log_error_link(link, "Could not allocate route: %s", - strerror(-r)); - return r; + if (r >= 0) { + r = route_new_dynamic(&route); + if (r < 0) { + log_error_link(link, "Could not allocate route: %s", + strerror(-r)); + return r; + } + + r = route_new_dynamic(&route_gw); + if (r < 0) { + log_error_link(link, "Could not allocate route: %s", + strerror(-r)); + return r; + } + + /* The dhcp netmask may mask out the gateway. Add an explicit + * route for the gw host so that we can route no matter the + * netmask or existing kernel route tables. */ + route_gw->family = AF_INET; + route_gw->dst_addr.in = gateway; + route_gw->dst_prefixlen = 32; + route_gw->scope = RT_SCOPE_LINK; + + r = route_configure(route_gw, link, &route_handler); + if (r < 0) { + log_warning_link(link, + "could not set host route: %s", strerror(-r)); + return r; + } + + link->route_messages ++; + + route->family = AF_INET; + route->in_addr.in = gateway; + + r = route_configure(route, link, &route_handler); + if (r < 0) { + log_warning_link(link, + "could not set routes: %s", strerror(-r)); + link_enter_failed(link); + return r; + } + + link->route_messages ++; } + } - r = route_new_dynamic(&route_gw); - if (r < 0) { - log_error_link(link, "Could not allocate route: %s", - strerror(-r)); - return r; - } - - /* The dhcp netmask may mask out the gateway. Add an explicit - * route for the gw host so that we can route no matter the - * netmask or existing kernel route tables. */ - route_gw->family = AF_INET; - route_gw->dst_addr.in = gateway; - route_gw->dst_prefixlen = 32; - route_gw->scope = RT_SCOPE_LINK; - - r = route_configure(route_gw, link, &route_handler); - if (r < 0) { - log_warning_link(link, - "could not set host route: %s", strerror(-r)); - return r; - } - - link->route_messages ++; - - route->family = AF_INET; - route->in_addr.in = gateway; - - r = route_configure(route, link, &route_handler); - if (r < 0) { - log_warning_link(link, - "could not set routes: %s", strerror(-r)); - link_enter_failed(link); - return r; - } - - link->route_messages ++; + if (link->route_messages == 0) { + link_enter_configured(link); } return 0; @@ -680,29 +685,31 @@ static int dhcp_lease_lost(Link *link) { r = address_new_dynamic(&address); if (r >= 0) { + r = sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); + if (r >= 0) { + r = route_new_dynamic(&route_gw); + if (r >= 0) { + route_gw->family = AF_INET; + route_gw->dst_addr.in = gateway; + route_gw->dst_prefixlen = 32; + route_gw->scope = RT_SCOPE_LINK; + + route_drop(route_gw, link, &route_drop_handler); + } + + r = route_new_dynamic(&route); + if (r >= 0) { + route->family = AF_INET; + route->in_addr.in = gateway; + + route_drop(route, link, &route_drop_handler); + } + } + sd_dhcp_lease_get_address(link->dhcp_lease, &addr); sd_dhcp_lease_get_netmask(link->dhcp_lease, &netmask); - sd_dhcp_lease_get_router(link->dhcp_lease, &gateway); prefixlen = net_netmask_to_prefixlen(&netmask); - r = route_new_dynamic(&route_gw); - if (r >= 0) { - route_gw->family = AF_INET; - route_gw->dst_addr.in = gateway; - route_gw->dst_prefixlen = 32; - route_gw->scope = RT_SCOPE_LINK; - - route_drop(route_gw, link, &route_drop_handler); - } - - r = route_new_dynamic(&route); - if (r >= 0) { - route->family = AF_INET; - route->in_addr.in = gateway; - - route_drop(route, link, &route_drop_handler); - } - address->family = AF_INET; address->in_addr.in = addr; address->prefixlen = prefixlen; @@ -777,25 +784,36 @@ static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { prefixlen = net_netmask_to_prefixlen(&netmask); r = sd_dhcp_lease_get_router(lease, &gateway); - if (r < 0) { - log_warning_link(link, "DHCP error: no router: %s", - strerror(-r)); + if (r < 0 && r != -ENOENT) { + log_warning_link(link, "DHCP error: %s", strerror(-r)); return r; } - log_struct_link(LOG_INFO, link, - "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", - link->ifname, - ADDRESS_FMT_VAL(address), - prefixlen, - ADDRESS_FMT_VAL(gateway), - "ADDRESS=%u.%u.%u.%u", - ADDRESS_FMT_VAL(address), - "PREFIXLEN=%u", - prefixlen, - "GATEWAY=%u.%u.%u.%u", - ADDRESS_FMT_VAL(gateway), - NULL); + if (r >= 0) + log_struct_link(LOG_INFO, link, + "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", + link->ifname, + ADDRESS_FMT_VAL(address), + prefixlen, + ADDRESS_FMT_VAL(gateway), + "ADDRESS=%u.%u.%u.%u", + ADDRESS_FMT_VAL(address), + "PREFIXLEN=%u", + prefixlen, + "GATEWAY=%u.%u.%u.%u", + ADDRESS_FMT_VAL(gateway), + NULL); + else + log_struct_link(LOG_INFO, link, + "MESSAGE=%s: DHCPv4 address %u.%u.%u.%u/%u", + link->ifname, + ADDRESS_FMT_VAL(address), + prefixlen, + "ADDRESS=%u.%u.%u.%u", + ADDRESS_FMT_VAL(address), + "PREFIXLEN=%u", + prefixlen, + NULL); link->dhcp_lease = lease;