while ((address = network->static_addresses))
address_free(address);
}
- if (network->n_static_routes > 0) {
- Route *route;
-
+ if (!hashmap_isempty(network->routes_by_section))
log_warning("%s: Cannot set routes when Bond= is specified, ignoring routes.",
network->filename);
- while ((route = network->static_routes))
- route_free(route);
- }
+ network->routes_by_section = hashmap_free_with_destructor(network->routes_by_section, route_free);
}
if (network->link_local < 0)
static Network *network_free(Network *network) {
Address *address;
- Route *route;
if (!network)
return NULL;
netdev_unref(network->vrf);
hashmap_free_with_destructor(network->stacked_netdevs, netdev_unref);
- while ((route = network->static_routes))
- route_free(route);
-
while ((address = network->static_addresses))
address_free(address);
set_free_free(network->ipv6_proxy_ndp_addresses);
hashmap_free(network->addresses_by_section);
- hashmap_free(network->routes_by_section);
+ hashmap_free_with_destructor(network->routes_by_section, route_free);
hashmap_free_with_destructor(network->nexthops_by_section, nexthop_free);
hashmap_free_with_destructor(network->fdb_entries_by_section, fdb_entry_free);
hashmap_free_with_destructor(network->mdb_entries_by_section, mdb_entry_free);
if (address->family == AF_INET6)
return true;
- LIST_FOREACH(routes, route, network->static_routes)
+ HASHMAP_FOREACH(route, network->routes_by_section)
if (route->family == AF_INET6)
return true;
assert(network);
assert(ret);
- assert(!!filename == (section_line > 0));
-
- if (filename) {
- r = network_config_section_new(filename, section_line, &n);
- if (r < 0)
- return r;
+ assert(filename);
+ assert(section_line > 0);
- route = hashmap_get(network->routes_by_section, n);
- if (route) {
- *ret = TAKE_PTR(route);
+ r = network_config_section_new(filename, section_line, &n);
+ if (r < 0)
+ return r;
- return 0;
- }
+ route = hashmap_get(network->routes_by_section, n);
+ if (route) {
+ *ret = TAKE_PTR(route);
+ return 0;
}
- if (network->n_static_routes >= routes_max())
+ if (hashmap_size(network->routes_by_section) >= routes_max())
return -E2BIG;
r = route_new(&route);
route->protocol = RTPROT_STATIC;
route->network = network;
- LIST_PREPEND(routes, network->static_routes, route);
- network->n_static_routes++;
-
- if (filename) {
- route->section = TAKE_PTR(n);
+ route->section = TAKE_PTR(n);
- r = hashmap_ensure_allocated(&network->routes_by_section, &network_config_hash_ops);
- if (r < 0)
- return r;
+ r = hashmap_ensure_allocated(&network->routes_by_section, &network_config_hash_ops);
+ if (r < 0)
+ return r;
- r = hashmap_put(network->routes_by_section, route->section, route);
- if (r < 0)
- return r;
- }
+ r = hashmap_put(network->routes_by_section, route->section, route);
+ if (r < 0)
+ return r;
*ret = TAKE_PTR(route);
-
return 0;
}
return NULL;
if (route->network) {
- LIST_REMOVE(routes, route->network->static_routes, route);
-
- assert(route->network->n_static_routes > 0);
- route->network->n_static_routes--;
-
- if (route->section)
- hashmap_remove(route->network->routes_by_section, route->section);
+ assert(route->section);
+ hashmap_remove(route->network->routes_by_section, route->section);
}
network_config_section_free(route->section);
if (!link->network)
return false;
- LIST_FOREACH(routes, net_route, link->network->static_routes)
+ HASHMAP_FOREACH(net_route, link->network->routes_by_section)
if (route_equal(net_route, route))
return true;
/* First add the routes that enable us to talk to gateways, then add in the others that need a gateway. */
for (phase = 0; phase < _PHASE_MAX; phase++)
- LIST_FOREACH(routes, rt, link->network->static_routes) {
+ HASHMAP_FOREACH(rt, link->network->routes_by_section) {
if (rt->gateway_from_dhcp)
continue;
int network_add_ipv4ll_route(Network *network) {
_cleanup_(route_free_or_set_invalidp) Route *n = NULL;
+ unsigned section_line;
int r;
assert(network);
if (!network->ipv4ll_route)
return 0;
+ section_line = hashmap_find_free_section_line(network->routes_by_section);
+
/* IPv4LLRoute= is in [Network] section. */
- r = route_new_static(network, NULL, 0, &n);
+ r = route_new_static(network, network->filename, section_line, &n);
if (r < 0)
return r;
int network_add_default_route_on_device(Network *network) {
_cleanup_(route_free_or_set_invalidp) Route *n = NULL;
+ unsigned section_line;
int r;
assert(network);
if (!network->default_route_on_device)
return 0;
+ section_line = hashmap_find_free_section_line(network->routes_by_section);
+
/* DefaultRouteOnDevice= is in [Network] section. */
- r = route_new_static(network, NULL, 0, &n);
+ r = route_new_static(network, network->filename, section_line, &n);
if (r < 0)
return r;
assert(data);
if (streq(section, "Network")) {
- /* we are not in an Route section, so treat
- * this as the special '0' section */
- r = route_new_static(network, NULL, 0, &n);
+ /* we are not in an Route section, so use line number instead */
+ r = route_new_static(network, filename, line, &n);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
}
void network_verify_routes(Network *network) {
- Route *route, *route_next;
+ Route *route;
assert(network);
- LIST_FOREACH_SAFE(routes, route, route_next, network->static_routes)
+ HASHMAP_FOREACH(route, network->routes_by_section)
if (route_section_verify(route, network) < 0)
route_free(route);
}