static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) { _cleanup_address_free_ Address *ll_addr = NULL; _cleanup_route_free_ Route *route = NULL; struct in_addr address; int r; assert(ll); assert(link); r = sd_ipv4ll_get_address(ll, &address); if (r == -ENOENT) return 0; else if (r < 0) return r; log_link_debug(link, "IPv4 link-local claim %u.%u.%u.%u", ADDRESS_FMT_VAL(address)); r = address_new(&ll_addr); if (r < 0) return r; ll_addr->family = AF_INET; ll_addr->in_addr.in = address; ll_addr->prefixlen = 16; ll_addr->broadcast.s_addr = ll_addr->in_addr.in.s_addr | htonl(0xfffffffflu >> ll_addr->prefixlen); ll_addr->scope = RT_SCOPE_LINK; r = address_configure(ll_addr, link, ipv4ll_address_handler, false); if (r < 0) return r; link->ipv4ll_address = false; r = route_new(&route); if (r < 0) return r; route->family = AF_INET; route->scope = RT_SCOPE_LINK; route->protocol = RTPROT_STATIC; route->priority = IPV4LL_ROUTE_METRIC; r = route_configure(route, link, ipv4ll_route_handler); if (r < 0) return r; link->ipv4ll_route = false; return 0; }
static int ipv4ll_address_lost(Link *link) { _cleanup_address_free_ Address *address = NULL; _cleanup_route_free_ Route *route = NULL; struct in_addr addr; int r; assert(link); link->ipv4ll_route = false; link->ipv4ll_address = false; r = sd_ipv4ll_get_address(link->ipv4ll, &addr); if (r < 0) return 0; log_link_debug(link, "IPv4 link-local release %u.%u.%u.%u", ADDRESS_FMT_VAL(addr)); r = address_new_dynamic(&address); if (r < 0) { log_link_error(link, "Could not allocate address: %s", strerror(-r)); return r; } address->family = AF_INET; address->in_addr.in = addr; address->prefixlen = 16; address->scope = RT_SCOPE_LINK; address_drop(address, link, &link_address_drop_handler); r = route_new_dynamic(&route, RTPROT_UNSPEC); if (r < 0) { log_link_error(link, "Could not allocate route: %s", strerror(-r)); return r; } route->family = AF_INET; route->scope = RT_SCOPE_LINK; route->metrics = IPV4LL_ROUTE_METRIC; route_drop(route, link, &link_route_drop_handler); link_client_handler(link); return 0; }
static int ipv4ll_address_lost(Link *link) { _cleanup_address_free_ Address *address = NULL; _cleanup_route_free_ Route *route = NULL; struct in_addr addr; int r; assert(link); link->ipv4ll_route = false; link->ipv4ll_address = false; r = sd_ipv4ll_get_address(link->ipv4ll, &addr); if (r < 0) return 0; log_link_debug(link, "IPv4 link-local release %u.%u.%u.%u", ADDRESS_FMT_VAL(addr)); r = address_new(&address); if (r < 0) { log_link_error_errno(link, r, "Could not allocate address: %m"); return r; } address->family = AF_INET; address->in_addr.in = addr; address->prefixlen = 16; address->scope = RT_SCOPE_LINK; address_remove(address, link, &link_address_remove_handler); r = route_new(&route); if (r < 0) { log_link_error_errno(link, r, "Could not allocate route: %m"); return r; } route->family = AF_INET; route->scope = RT_SCOPE_LINK; route->priority = IPV4LL_ROUTE_METRIC; route_remove(route, link, &link_route_remove_handler); link_check_ready(link); return 0; }
static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { sd_dhcp_lease *lease; struct in_addr address; struct in_addr netmask; struct in_addr gateway; unsigned prefixlen; uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME; int r; assert(client); assert(link); r = sd_dhcp_client_get_lease(client, &lease); if (r < 0) return log_link_error_errno(link, r, "DHCP error: No lease: %m"); r = sd_dhcp_lease_get_address(lease, &address); if (r < 0) return log_link_error_errno(link, r, "DHCP error: No address: %m"); r = sd_dhcp_lease_get_netmask(lease, &netmask); if (r < 0) return log_link_error_errno(link, r, "DHCP error: No netmask: %m"); prefixlen = in_addr_netmask_to_prefixlen(&netmask); r = sd_dhcp_lease_get_router(lease, &gateway); if (r < 0 && r != -ENODATA) return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m"); if (r >= 0) log_struct(LOG_INFO, LOG_LINK_INTERFACE(link), LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", 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(LOG_INFO, LOG_LINK_INTERFACE(link), LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u", ADDRESS_FMT_VAL(address), prefixlen), "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address), "PREFIXLEN=%u", prefixlen, NULL); link->dhcp_lease = sd_dhcp_lease_ref(lease); link_dirty(link); if (link->network->dhcp_use_mtu) { uint16_t mtu; r = sd_dhcp_lease_get_mtu(lease, &mtu); if (r >= 0) { r = link_set_mtu(link, mtu); if (r < 0) log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu); } } if (link->network->dhcp_use_hostname) { const char *hostname = NULL; if (link->network->dhcp_hostname) hostname = link->network->dhcp_hostname; else (void) sd_dhcp_lease_get_hostname(lease, &hostname); if (hostname) { r = manager_set_hostname(link->manager, hostname); if (r < 0) log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname); } } if (link->network->dhcp_use_timezone) { const char *tz = NULL; (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); if (tz) { r = manager_set_timezone(link->manager, tz); if (r < 0) log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz); } } if (!link->network->dhcp_critical) { r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); if (r < 0) { log_link_warning_errno(link, r, "DHCP error: no lifetime: %m"); return r; } } r = dhcp4_update_address(link, &address, &netmask, lifetime); if (r < 0) { log_link_warning_errno(link, r, "Could not update IP address: %m"); link_enter_failed(link); return r; } return 0; }
static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { const struct in_addr *router; sd_dhcp_lease *lease; struct in_addr address; struct in_addr netmask; unsigned prefixlen; uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME; int r; assert(client); assert(link); r = sd_dhcp_client_get_lease(client, &lease); if (r < 0) return log_link_error_errno(link, r, "DHCP error: No lease: %m"); r = sd_dhcp_lease_get_address(lease, &address); if (r < 0) return log_link_error_errno(link, r, "DHCP error: No address: %m"); r = sd_dhcp_lease_get_netmask(lease, &netmask); if (r < 0) return log_link_error_errno(link, r, "DHCP error: No netmask: %m"); prefixlen = in4_addr_netmask_to_prefixlen(&netmask); r = sd_dhcp_lease_get_router(lease, &router); if (r < 0 && r != -ENODATA) return log_link_error_errno(link, r, "DHCP error: Could not get gateway: %m"); if (r > 0 && !in4_addr_is_null(&router[0])) log_struct(LOG_INFO, LOG_LINK_INTERFACE(link), LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", ADDRESS_FMT_VAL(address), prefixlen, ADDRESS_FMT_VAL(router[0])), "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address), "PREFIXLEN=%u", prefixlen, "GATEWAY=%u.%u.%u.%u", ADDRESS_FMT_VAL(router[0])); else log_struct(LOG_INFO, LOG_LINK_INTERFACE(link), LOG_LINK_MESSAGE(link, "DHCPv4 address %u.%u.%u.%u/%u", ADDRESS_FMT_VAL(address), prefixlen), "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address), "PREFIXLEN=%u", prefixlen); link->dhcp_lease = sd_dhcp_lease_ref(lease); link_dirty(link); if (link->network->dhcp_use_mtu) { uint16_t mtu; r = sd_dhcp_lease_get_mtu(lease, &mtu); if (r >= 0) { r = link_set_mtu(link, mtu, true); if (r < 0) log_link_error_errno(link, r, "Failed to set MTU to %" PRIu16 ": %m", mtu); } } if (link->network->dhcp_use_hostname) { const char *dhcpname = NULL; _cleanup_free_ char *hostname = NULL; if (link->network->dhcp_hostname) dhcpname = link->network->dhcp_hostname; else (void) sd_dhcp_lease_get_hostname(lease, &dhcpname); if (dhcpname) { r = shorten_overlong(dhcpname, &hostname); if (r < 0) log_link_warning_errno(link, r, "Unable to shorten overlong DHCP hostname '%s', ignoring: %m", dhcpname); if (r == 1) log_link_notice(link, "Overlong DHCP hostname received, shortened from '%s' to '%s'", dhcpname, hostname); } if (hostname) { r = manager_set_hostname(link->manager, hostname); if (r < 0) log_link_error_errno(link, r, "Failed to set transient hostname to '%s': %m", hostname); } } if (link->network->dhcp_use_timezone) { const char *tz = NULL; (void) sd_dhcp_lease_get_timezone(link->dhcp_lease, &tz); if (tz) { r = manager_set_timezone(link->manager, tz); if (r < 0) log_link_error_errno(link, r, "Failed to set timezone to '%s': %m", tz); } } if (!link->network->dhcp_critical) { r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); if (r < 0) return log_link_warning_errno(link, r, "DHCP error: no lifetime: %m"); } r = dhcp4_update_address(link, &address, &netmask, lifetime); if (r < 0) { log_link_warning_errno(link, r, "Could not update IP address: %m"); link_enter_failed(link); return r; } return 0; }
static int dhcp_lease_acquired(sd_dhcp_client *client, Link *link) { sd_dhcp_lease *lease; struct in_addr address; struct in_addr netmask; struct in_addr gateway; unsigned prefixlen; uint32_t lifetime = CACHE_INFO_INFINITY_LIFE_TIME; int r; assert(client); assert(link); r = sd_dhcp_client_get_lease(client, &lease); if (r < 0) { log_link_warning(link, "DHCP error: no lease: %s", strerror(-r)); return r; } r = sd_dhcp_lease_get_address(lease, &address); if (r < 0) { log_link_warning(link, "DHCP error: no address: %s", strerror(-r)); return r; } r = sd_dhcp_lease_get_netmask(lease, &netmask); if (r < 0) { log_link_warning(link, "DHCP error: no netmask: %s", strerror(-r)); return r; } prefixlen = in_addr_netmask_to_prefixlen(&netmask); r = sd_dhcp_lease_get_router(lease, &gateway); if (r < 0 && r != -ENOENT) { log_link_warning(link, "DHCP error: could not get gateway: %s", strerror(-r)); return r; } if (r >= 0) log_link_struct(link, LOG_INFO, "MESSAGE=%-*s: DHCPv4 address %u.%u.%u.%u/%u via %u.%u.%u.%u", IFNAMSIZ, 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_link_struct(link, LOG_INFO, "MESSAGE=%-*s: DHCPv4 address %u.%u.%u.%u/%u", IFNAMSIZ, link->ifname, ADDRESS_FMT_VAL(address), prefixlen, "ADDRESS=%u.%u.%u.%u", ADDRESS_FMT_VAL(address), "PREFIXLEN=%u", prefixlen, NULL); link->dhcp_lease = lease; if (link->network->dhcp_mtu) { uint16_t mtu; r = sd_dhcp_lease_get_mtu(lease, &mtu); if (r >= 0) { r = link_set_mtu(link, mtu); if (r < 0) log_link_error(link, "Failed to set MTU " "to %" PRIu16, mtu); } } if (link->network->dhcp_hostname) { const char *hostname; r = sd_dhcp_lease_get_hostname(lease, &hostname); if (r >= 0) { r = link_set_hostname(link, hostname); if (r < 0) log_link_error(link, "Failed to set transient hostname to '%s'", hostname); } } if (!link->network->dhcp_critical) { r = sd_dhcp_lease_get_lifetime(link->dhcp_lease, &lifetime); if (r < 0) { log_link_warning(link, "DHCP error: no lifetime: %s", strerror(-r)); return r; } } r = dhcp4_update_address(link, &address, &netmask, lifetime); if (r < 0) { log_link_warning(link, "could not update IP address: %s", strerror(-r)); link_enter_failed(link); return r; } return 0; }