+
+ switch (event) {
+ case DHCP_EVENT_NO_LEASE:
+ log_debug_link(link, "IP address in use.");
+ break;
+ case DHCP_EVENT_EXPIRED:
+ case DHCP_EVENT_STOP:
+ case DHCP_EVENT_IP_CHANGE:
+ if (link->network->dhcp_critical) {
+ log_error_link(link, "DHCPv4 connection considered system critical, "
+ "ignoring request to reconfigure it.");
+ return;
+ }
+
+ if (link->dhcp_lease) {
+ r = dhcp_lease_lost(link);
+ if (r < 0) {
+ link_enter_failed(link);
+ return;
+ }
+ }
+
+ if (event == DHCP_EVENT_IP_CHANGE) {
+ r = dhcp_lease_acquired(client, link);
+ if (r < 0) {
+ link_enter_failed(link);
+ return;
+ }
+ }
+
+ if (event == DHCP_EVENT_EXPIRED && link->network->ipv4ll) {
+ if (!sd_ipv4ll_is_running(link->ipv4ll))
+ r = sd_ipv4ll_start(link->ipv4ll);
+ else if (ipv4ll_is_bound(link->ipv4ll))
+ r = ipv4ll_address_update(link, false);
+ if (r < 0) {
+ link_enter_failed(link);
+ return;
+ }
+ }
+
+ break;
+ case DHCP_EVENT_IP_ACQUIRE:
+ r = dhcp_lease_acquired(client, link);
+ if (r < 0) {
+ link_enter_failed(link);
+ return;
+ }
+ if (link->ipv4ll) {
+ if (ipv4ll_is_bound(link->ipv4ll))
+ r = ipv4ll_address_update(link, true);
+ else
+ r = sd_ipv4ll_stop(link->ipv4ll);
+ if (r < 0) {
+ link_enter_failed(link);
+ return;
+ }
+ }
+ break;
+ default:
+ if (event < 0)
+ log_warning_link(link, "DHCP error: %s", strerror(-event));
+ else
+ log_warning_link(link, "DHCP unknown event: %d", event);
+ break;
+ }
+
+ return;
+}
+
+static int ipv4ll_address_update(Link *link, bool deprecate) {
+ int r;
+ struct in_addr addr;
+
+ assert(link);
+
+ r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
+ if (r >= 0) {
+ _cleanup_address_free_ Address *address = NULL;
+
+ log_debug_link(link, "IPv4 link-local %s %u.%u.%u.%u",
+ deprecate ? "deprecate" : "approve",
+ ADDRESS_FMT_VAL(addr));
+
+ r = address_new_dynamic(&address);
+ if (r < 0) {
+ log_error_link(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->cinfo.ifa_prefered = deprecate ? 0 : CACHE_INFO_INFINITY_LIFE_TIME;
+ address->broadcast.s_addr = address->in_addr.in.s_addr | htonl(0xfffffffflu >> address->prefixlen);
+
+ address_update(address, link, &address_update_handler);
+ }
+
+ return 0;
+
+}
+
+static int ipv4ll_address_lost(Link *link) {
+ int r;
+ struct in_addr addr;
+
+ assert(link);
+
+ r = sd_ipv4ll_get_address(link->ipv4ll, &addr);
+ if (r >= 0) {
+ _cleanup_address_free_ Address *address = NULL;
+ _cleanup_route_free_ Route *route = NULL;
+
+ log_debug_link(link, "IPv4 link-local release %u.%u.%u.%u",
+ ADDRESS_FMT_VAL(addr));
+
+ r = address_new_dynamic(&address);
+ if (r < 0) {
+ log_error_link(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, &address_drop_handler);
+
+ r = route_new_dynamic(&route);
+ if (r < 0) {
+ log_error_link(link, "Could not allocate route: %s",
+ strerror(-r));
+ return r;
+ }
+
+ route->family = AF_INET;
+ route->scope = RT_SCOPE_LINK;
+ route->metrics = 99;
+
+ route_drop(route, link, &route_drop_handler);
+ }
+
+ return 0;
+}
+
+static bool ipv4ll_is_bound(sd_ipv4ll *ll) {
+ int r;
+ struct in_addr addr;
+
+ assert(ll);
+
+ r = sd_ipv4ll_get_address(ll, &addr);
+ if (r < 0)
+ return false;
+ return true;
+}
+
+static int ipv4ll_address_claimed(sd_ipv4ll *ll, Link *link) {
+ struct in_addr address;
+ int r;
+
+ assert(ll);
+ assert(link);
+
+ r = sd_ipv4ll_get_address(ll, &address);
+ if (r < 0)
+ return r;
+
+ log_struct_link(LOG_INFO, link,
+ "MESSAGE=%s: IPv4 link-local address %u.%u.%u.%u",
+ link->ifname,
+ ADDRESS_FMT_VAL(address),
+ NULL);
+
+ link_enter_set_addresses(link);
+
+ return 0;
+}
+
+static void ipv4ll_handler(sd_ipv4ll *ll, int event, void *userdata){
+ Link *link = userdata;
+ int r;
+
+ assert(link);
+ assert(link->network);
+ assert(link->manager);
+
+ switch(event) {
+ case IPV4LL_EVENT_STOP:
+ case IPV4LL_EVENT_CONFLICT:
+ r = ipv4ll_address_lost(link);
+ if (r < 0) {
+ link_enter_failed(link);
+ return;
+ }
+ break;
+ case IPV4LL_EVENT_BIND:
+ r = ipv4ll_address_claimed(ll, link);
+ if (r < 0) {
+ link_enter_failed(link);
+ return;
+ }
+ break;
+ default:
+ if (event < 0)
+ log_warning_link(link, "IPv4 link-local error: %s", strerror(-event));
+ else
+ log_warning_link(link, "IPv4 link-local unknown event: %d", event);
+ break;
+ }
+}
+
+static int link_acquire_conf(Link *link) {
+ int r;
+
+ assert(link);
+ assert(link->network);
+ assert(link->manager);
+ assert(link->manager->event);
+
+ if (link->network->ipv4ll) {
+ assert(link->ipv4ll);
+
+ log_debug_link(link, "acquiring IPv4 link-local address");
+
+ r = sd_ipv4ll_start(link->ipv4ll);
+ if (r < 0)
+ return r;
+ }
+
+ if (link->network->dhcp) {
+ assert(link->dhcp_client);
+
+ log_debug_link(link, "acquiring DHCPv4 lease");
+
+ r = sd_dhcp_client_start(link->dhcp_client);
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+static int link_update_flags(Link *link, unsigned flags) {
+ unsigned flags_added, flags_removed, generic_flags;
+ bool carrier_gained, carrier_lost;
+ int r;
+
+ assert(link);
+
+ if (link->state == LINK_STATE_FAILED)
+ return 0;
+
+ if (link->flags == flags)
+ return 0;
+
+ flags_added = (link->flags ^ flags) & flags;
+ flags_removed = (link->flags ^ flags) & link->flags;
+ generic_flags = ~(IFF_UP | IFF_LOWER_UP | IFF_RUNNING);
+
+ /* consider link to have carrier when both RUNNING and LOWER_UP, as RUNNING
+ may mean that the oper state is unknown, in which case we should fall back to
+ simply trust LOWER_UP, even thought that is less reliable
+ */
+ carrier_gained = ((flags_added & IFF_LOWER_UP) && (flags & IFF_RUNNING)) ||
+ ((flags_added & IFF_RUNNING) && (flags & IFF_LOWER_UP));
+ carrier_lost = ((link->flags & (IFF_RUNNING | IFF_LOWER_UP)) ==
+ (IFF_RUNNING | IFF_LOWER_UP)) &&
+ (flags_removed & (IFF_LOWER_UP | IFF_RUNNING));
+
+ link->flags = flags;
+
+ if (!link->network)
+ /* not currently managing this link
+ we track state changes, but don't log them
+ they will be logged if and when a network is
+ applied */
+ return 0;
+
+ if (flags_added & IFF_UP)
+ log_info_link(link, "link is up");
+ else if (flags_removed & IFF_UP)
+ log_info_link(link, "link is down");
+
+ if (flags_added & IFF_LOWER_UP)
+ log_debug_link(link, "link is lower up");
+ else if (flags_removed & IFF_LOWER_UP)
+ log_debug_link(link, "link is lower down");
+
+ if (flags_added & IFF_RUNNING)
+ log_debug_link(link, "link is running");
+ else if (flags_removed & IFF_RUNNING)
+ log_debug_link(link, "link is not running");
+
+ if (flags_added & generic_flags)
+ log_debug_link(link, "ignored link flags gained: %#.8x",
+ flags_added & generic_flags);
+
+ if (flags_removed & generic_flags)
+ log_debug_link(link, "ignored link flags lost: %#.8x",
+ flags_removed & generic_flags);
+
+ if (carrier_gained) {
+ log_info_link(link, "gained carrier");
+
+ if (link->network->dhcp || link->network->ipv4ll) {
+ r = link_acquire_conf(link);
+ if (r < 0) {
+ log_warning_link(link, "Could not acquire configuration: %s", strerror(-r));
+ link_enter_failed(link);
+ return r;
+ }
+ }
+ } else if (carrier_lost) {
+ log_info_link(link, "lost carrier");
+
+ if (link->network->dhcp) {
+ r = sd_dhcp_client_stop(link->dhcp_client);
+ if (r < 0) {
+ log_warning_link(link, "Could not stop DHCPv4 client: %s", strerror(-r));
+ link_enter_failed(link);
+ return r;
+ }
+ }
+
+ if (link->network->ipv4ll) {
+ r = sd_ipv4ll_stop(link->ipv4ll);
+ if (r < 0) {
+ log_warning_link(link, "Could not stop IPv4 link-local: %s", strerror(-r));
+ link_enter_failed(link);
+ return r;
+ }
+ }
+ }
+
+ return 0;
+}
+
+static int link_up_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+ Link *link = userdata;
+ int r;
+
+ assert(link);
+
+ if (link->state == LINK_STATE_FAILED)
+ return 1;
+
+ r = sd_rtnl_message_get_errno(m);
+ if (r >= 0)
+ link_update_flags(link, link->flags | IFF_UP);
+ else
+ log_struct_link(LOG_WARNING, link,
+ "MESSAGE=%s: could not bring up interface: %s",
+ link->ifname, strerror(-r),
+ "ERRNO=%d", -r,
+ NULL);
+ return 1;
+}
+
+static int link_up(Link *link) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+ int r;
+
+ assert(link);
+ assert(link->manager);
+ assert(link->manager->rtnl);
+
+ log_debug_link(link, "bringing link up");
+
+ r = sd_rtnl_message_new_link(link->manager->rtnl, &req,
+ RTM_SETLINK, link->ifindex);
+ if (r < 0) {
+ log_error_link(link, "Could not allocate RTM_SETLINK message");
+ return r;