HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/network/networkd-route.c

76,269 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 81cfe84add80ad69bea35e0c0b1fdcc5fc052928

Lines 2001–2102 of 2,102

                [ROUTE_METRIC_FASTOPEN_NO_COOKIE] = { .parser = config_parse_route_metric,           .ltype = RTAX_FASTOPEN_NO_COOKIE, .offset = 0,                                                   },
        };
 
        _cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
        Network *network = ASSERT_PTR(userdata);
        int r;
 
        assert(filename);
 
        if (streq(section, "Network")) {
                assert(streq_ptr(lvalue, "Gateway"));
 
                /* Clear all previously defined routes when Gateway= (empty) is set in [Network] section */
                if (isempty(rvalue)) {
                        network->routes_by_section = hashmap_free(network->routes_by_section);
                        return 0;
                }
 
                /* we are not in an Route section, so use line number instead */
                r = route_new_static(network, filename, line, &route);
        } else
                r = route_new_static(network, filename, section_line, &route);
        if (r == -ENOMEM)
                return log_oom();
        if (r < 0) {
                log_syntax(unit, LOG_WARNING, filename, line, r,
                           "Failed to allocate route, ignoring assignment: %m");
                return 0;
        }
 
        r = config_section_parse(table, ELEMENTSOF(table),
                                 unit, filename, line, section, section_line, lvalue, ltype, rvalue, route);
        if (r <= 0)
                return r;
 
        TAKE_PTR(route);
        return 0;
}
 
int route_section_verify(Route *route) {
        int r;
 
        assert(route);
        assert(route->section);
 
        if (section_is_invalid(route->section))
                return -EINVAL;
 
        /* Currently, we do not support static route with finite lifetime. */
        assert(route->lifetime_usec == USEC_INFINITY);
 
        r = route_section_verify_nexthops(route);
        if (r < 0)
                return r;
 
        /* table */
        if (!route->table_set && route->network && route->network->vrf) {
                route->table = VRF(route->network->vrf)->table;
                route->table_set = true;
        }
 
        if (!route->table_set && IN_SET(route->type, RTN_LOCAL, RTN_BROADCAST, RTN_ANYCAST, RTN_NAT))
                route->table = RT_TABLE_LOCAL;
 
        /* scope */
        if (!route->scope_set && route->family == AF_INET) {
                if (IN_SET(route->type, RTN_LOCAL, RTN_NAT))
                        route->scope = RT_SCOPE_HOST;
                else if (IN_SET(route->type, RTN_BROADCAST, RTN_ANYCAST, RTN_MULTICAST))
                        route->scope = RT_SCOPE_LINK;
                else if (IN_SET(route->type, RTN_UNICAST, RTN_UNSPEC) &&
                         !route->gateway_from_dhcp_or_ra &&
                         !in_addr_is_set(route->nexthop.family, &route->nexthop.gw) &&
                         ordered_set_isempty(route->nexthops) &&
                         route->nexthop_id == 0)
                        route->scope = RT_SCOPE_LINK;
        }
 
        /* IPv6 route */
        if (route->family == AF_INET6) {
                if (route->scope != RT_SCOPE_UNIVERSE) {
                        log_section_warning(route->section, "Scope= is specified for IPv6 route. It will be ignored.");
                        route->scope = RT_SCOPE_UNIVERSE;
                }
 
                if (route->priority == 0)
                        route->priority = IP6_RT_PRIO_USER;
        }
 
        return 0;
}
 
void network_drop_invalid_routes(Network *network) {
        Route *route;
 
        assert(network);
 
        HASHMAP_FOREACH(route, network->routes_by_section)
                if (route_section_verify(route) < 0)
                        route_detach(route);
}