HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/network/networkd-ndisc.c

120,194 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 55a7e8e732530f41eff357a99ddee6543d65c12f

Lines 3001–3387 of 3,387

        NDiscDNSSL *dnssl;
        SET_FOREACH(dnssl, link->ndisc_dnssl) {
                if (!in6_addr_equal(&dnssl->router, &original_address))
                        continue;
 
                dnssl->router = current_address;
        }
 
        NDiscCaptivePortal *cp;
        SET_FOREACH(cp, link->ndisc_captive_portals) {
                if (!in6_addr_equal(&cp->router, &original_address))
                        continue;
 
                cp->router = current_address;
        }
 
        NDiscPREF64 *p64;
        SET_FOREACH(p64, link->ndisc_pref64) {
                if (!in6_addr_equal(&p64->router, &original_address))
                        continue;
 
                p64->router = current_address;
        }
 
        NDiscDNR *dnr;
        SET_FOREACH(dnr, link->ndisc_dnr) {
                if (!in6_addr_equal(&dnr->router, &original_address))
                        continue;
 
                dnr->router = current_address;
        }
 
        return 0;
}
 
static int ndisc_neighbor_handler(Link *link, sd_ndisc_neighbor *na) {
        int r;
 
        assert(link);
        assert(na);
 
        r = sd_ndisc_neighbor_is_router(na);
        if (r < 0)
                return r;
        if (r == 0)
                r = ndisc_neighbor_handle_non_router_message(link, na);
        else
                r = ndisc_neighbor_handle_router_message(link, na);
        if (r < 0)
                return r;
 
        return 0;
}
 
static void ndisc_handler(sd_ndisc *nd, sd_ndisc_event_t event, void *message, void *userdata) {
        Link *link = ASSERT_PTR(userdata);
        int r;
 
        if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
                return;
 
        switch (event) {
 
        case SD_NDISC_EVENT_ROUTER:
                r = ndisc_router_handler(link, ASSERT_PTR(message));
                if (r < 0 && r != -EBADMSG) {
                        link_enter_failed(link);
                        return;
                }
                break;
 
        case SD_NDISC_EVENT_NEIGHBOR:
                r = ndisc_neighbor_handler(link, ASSERT_PTR(message));
                if (r < 0 && r != -EBADMSG) {
                        link_enter_failed(link);
                        return;
                }
                break;
 
        case SD_NDISC_EVENT_REDIRECT:
                r = ndisc_redirect_handler(link, ASSERT_PTR(message));
                if (r < 0 && r != -EBADMSG) {
                        log_link_warning_errno(link, r, "Failed to process Redirect message: %m");
                        link_enter_failed(link);
                        return;
                }
                break;
 
        case SD_NDISC_EVENT_TIMEOUT:
                log_link_debug(link, "NDisc handler get timeout event");
                if (link->ndisc_messages == 0) {
                        link->ndisc_configured = true;
                        link_check_ready(link);
                }
                break;
 
        default:
                log_link_debug(link, "Received unsupported NDisc event, ignoring.");
        }
}
 
static int ndisc_configure(Link *link) {
        int r;
 
        assert(link);
 
        if (!link_ndisc_enabled(link))
                return 0;
 
        if (link->ndisc)
                return -EBUSY; /* Already configured. */
 
        r = sd_ndisc_new(&link->ndisc);
        if (r < 0)
                return r;
 
        r = sd_ndisc_attach_event(link->ndisc, link->manager->event, 0);
        if (r < 0)
                return r;
 
        if (link->hw_addr.length == ETH_ALEN) {
                r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
                if (r < 0)
                        return r;
        }
 
        r = sd_ndisc_set_ifindex(link->ndisc, link->ifindex);
        if (r < 0)
                return r;
 
        r = sd_ndisc_set_callback(link->ndisc, ndisc_handler, link);
        if (r < 0)
                return r;
 
        return 0;
}
 
static bool ndisc_can_start(Link *link) {
        assert(link);
 
        if (!link->ndisc || !link->dhcp6_client)
                return false;
 
        if (!link_has_carrier(link))
                return false;
 
        if (in6_addr_is_null(&link->ipv6ll_address))
                return false;
 
        return true;
}
 
int ndisc_start(Link *link) {
        int r;
 
        assert(link);
 
        if (!ndisc_can_start(link))
                return 0;
 
        r = sd_ndisc_set_link_local_address(link->ndisc, &link->ipv6ll_address);
        if (r < 0)
                return r;
 
        log_link_debug(link, "Discovering IPv6 routers");
 
        r = sd_ndisc_start(link->ndisc);
        if (r < 0)
                return r;
 
        return 1;
}
 
int ndisc_update_mac(Link *link) {
        int r;
 
        assert(link);
 
        if (!link->ndisc || link->hw_addr.length != ETH_ALEN)
                return 0;
 
        r = sd_ndisc_set_mac(link->ndisc, &link->hw_addr.ether);
        if (r < 0)
                return r;
 
        if (sd_ndisc_is_running(link->ndisc) <= 0)
                return 0;
 
        /* Only restart if a start is possible right now. Otherwise keep the running client,
         * whatever makes the conditions true again should also restart discovery. */
        if (!ndisc_can_start(link)) {
                log_link_debug(link, "MAC address changed, but not restarting IPv6 Router Discovery now.");
                return 0;
        }
 
        /* A changed MAC can mean a changed network, e.g. on a bond failover. Restart discovery to
         * solicit RAs instead of waiting for the next unsolicited one. Keep learned state, so use
         * sd_ndisc_stop() rather than ndisc_stop(). */
        r = sd_ndisc_stop(link->ndisc);
        if (r < 0)
                return r;
 
        r = ndisc_start(link);
        if (r < 0)
                return r;
        /* Should be unreachable, otherwise check ndisc_can_start(). */
        if (r == 0)
                log_link_debug(link, "MAC address changed, but IPv6 Router Discovery was not restarted.");
 
        return 0;
}
 
static int ndisc_process_request(Request *req, Link *link, void *userdata) {
        int r;
 
        assert(link);
 
        if (!link_is_ready_to_configure(link, /* allow_unmanaged= */ false))
                return 0;
 
        r = ndisc_configure(link);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to configure IPv6 Router Discovery: %m");
 
        r = ndisc_start(link);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to start IPv6 Router Discovery: %m");
 
        log_link_debug(link, "IPv6 Router Discovery is configured%s.",
                       r > 0 ? " and started" : "");
        return 1;
}
 
int link_request_ndisc(Link *link) {
        int r;
 
        assert(link);
 
        if (!link_ndisc_enabled(link))
                return 0;
 
        if (link->ndisc)
                return 0;
 
        r = link_queue_request(link, REQUEST_TYPE_NDISC, ndisc_process_request, NULL);
        if (r < 0)
                return log_link_warning_errno(link, r, "Failed to request configuring of the IPv6 Router Discovery: %m");
 
        log_link_debug(link, "Requested configuring of the IPv6 Router Discovery.");
        return 0;
}
 
int link_drop_ndisc_config(Link *link, Network *network) {
        int r, ret = 0;
 
        assert(link);
        assert(link->network);
 
        if (link->network == network)
                return 0; /* .network file is unchanged. It is not necessary to reconfigure the client. */
 
        if (!link_ndisc_enabled(link)) {
                /* NDisc is disabled. Stop the client if it is running and flush configs. */
                ret = ndisc_stop(link);
                ndisc_flush(link);
                link->ndisc = sd_ndisc_unref(link->ndisc);
                return ret;
        }
 
        /* Even if the client was previously enabled and also enabled in the new .network file, detailed
         * settings for the client may be different. Let's unref() the client. */
        link->ndisc = sd_ndisc_unref(link->ndisc);
 
        /* Get if NDisc was enabled or not. */
        Network *current = link->network;
        link->network = network;
        bool enabled = link_ndisc_enabled(link);
        link->network = current;
 
        /* If previously explicitly disabled, there should be nothing to drop.
         * If we do not know the previous setting of the client, e.g. when networkd is restarted, in that
         * case we do not have the previous .network file assigned to the interface, then  let's assume no
         * detailed configuration is changed. Hopefully, unmatching configurations will be dropped after
         * their lifetime. */
        if (!enabled)
                return 0;
 
        assert(network);
 
        /* Redirect messages will be ignored. Drop configurations based on the previously received redirect
         * messages. */
        if (!network->ndisc_use_redirect)
                (void) ndisc_drop_redirect(link, /* router= */ NULL);
 
        /* If one of the route setting is changed, drop all routes. */
        if (link->network->ndisc_use_gateway != network->ndisc_use_gateway ||
            link->network->ndisc_use_route_prefix != network->ndisc_use_route_prefix ||
            link->network->ndisc_use_onlink_prefix != network->ndisc_use_onlink_prefix ||
            link->network->ndisc_quickack != network->ndisc_quickack ||
            link->network->ndisc_route_metric_high != network->ndisc_route_metric_high ||
            link->network->ndisc_route_metric_medium != network->ndisc_route_metric_medium ||
            link->network->ndisc_route_metric_low != network->ndisc_route_metric_low ||
            !set_equal(link->network->ndisc_deny_listed_router, network->ndisc_deny_listed_router) ||
            !set_equal(link->network->ndisc_allow_listed_router, network->ndisc_allow_listed_router) ||
            !set_equal(link->network->ndisc_deny_listed_prefix, network->ndisc_deny_listed_prefix) ||
            !set_equal(link->network->ndisc_allow_listed_prefix, network->ndisc_allow_listed_prefix) ||
            !set_equal(link->network->ndisc_deny_listed_route_prefix, network->ndisc_deny_listed_route_prefix) ||
            !set_equal(link->network->ndisc_allow_listed_route_prefix, network->ndisc_allow_listed_route_prefix)) {
                Route *route;
                SET_FOREACH(route, link->manager->routes) {
                        if (route->source != NETWORK_CONFIG_SOURCE_NDISC)
                                continue;
 
                        if (!route_is_bound_to_link(route, link))
                                continue;
 
                        if (route->protocol == RTPROT_REDIRECT)
                                continue; /* redirect route is handled by ndisc_drop_redirect(). */
 
                        r = route_remove_and_cancel(route, link->manager);
                        if (r < 0)
                                RET_GATHER(ret, log_link_warning_errno(link, r, "Failed to remove SLAAC route, ignoring: %m"));
                }
 
                RET_GATHER(ret, ndisc_remove_unused_nexthops(link));
        }
 
        /* If SLAAC address is disabled, drop all addresses. */
        if (!network->ndisc_use_autonomous_prefix ||
            !set_equal(link->network->ndisc_tokens, network->ndisc_tokens) ||
            !set_equal(link->network->ndisc_deny_listed_prefix, network->ndisc_deny_listed_prefix) ||
            !set_equal(link->network->ndisc_allow_listed_prefix, network->ndisc_allow_listed_prefix)) {
                Address *address;
                SET_FOREACH(address, link->addresses) {
                        if (address->source != NETWORK_CONFIG_SOURCE_NDISC)
                                continue;
 
                        r = address_remove_and_cancel(address, link);
                        if (r < 0)
                                RET_GATHER(ret, log_link_warning_errno(link, r, "Failed to remove SLAAC address, ignoring: %m"));
                }
        }
 
        if (!network->ndisc_use_mtu)
                link->ndisc_mtu = 0;
 
        return ret;
}
 
int ndisc_stop(Link *link) {
        assert(link);
 
        link->ndisc_expire = sd_event_source_disable_unref(link->ndisc_expire);
 
        return sd_ndisc_stop(link->ndisc);
}
 
void ndisc_flush(Link *link) {
        assert(link);
 
        /* First, drop pending requests. */
        (void) link_drop_requests(link, NETWORK_CONFIG_SOURCE_NDISC);
 
        /* Remove all addresses, routes, RDNSS, DNSSL, DNR, and Captive Portal entries, without exception. */
        (void) ndisc_drop_outdated(link, /* router= */ NULL, /* timestamp_usec= */ USEC_INFINITY);
        (void) ndisc_drop_redirect(link, /* router= */ NULL);
 
        link->ndisc_routers_by_sender = hashmap_free(link->ndisc_routers_by_sender);
        link->ndisc_rdnss = set_free(link->ndisc_rdnss);
        link->ndisc_dnssl = set_free(link->ndisc_dnssl);
        link->ndisc_captive_portals = set_free(link->ndisc_captive_portals);
        link->ndisc_pref64 = set_free(link->ndisc_pref64);
        link->ndisc_redirects = set_free(link->ndisc_redirects);
        link->ndisc_dnr = set_free(link->ndisc_dnr);
        link->ndisc_mtu = 0;
}
 
static const char* const ndisc_start_dhcp6_client_table[_IPV6_ACCEPT_RA_START_DHCP6_CLIENT_MAX] = {
        [IPV6_ACCEPT_RA_START_DHCP6_CLIENT_NO]     = "no",
        [IPV6_ACCEPT_RA_START_DHCP6_CLIENT_ALWAYS] = "always",
        [IPV6_ACCEPT_RA_START_DHCP6_CLIENT_YES]    = "yes",
};
 
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(ndisc_start_dhcp6_client, IPv6AcceptRAStartDHCP6Client, IPV6_ACCEPT_RA_START_DHCP6_CLIENT_YES);
 
DEFINE_CONFIG_PARSE_ENUM(config_parse_ndisc_start_dhcp6_client, ndisc_start_dhcp6_client, IPv6AcceptRAStartDHCP6Client);