From: Yu Watanabe Date: Tue, 14 May 2019 07:43:14 +0000 (+0900) Subject: network: add DefaultRouteOnDevice= setting in [Network] section X-Git-Tag: v243-rc1~434^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5d5003ab355154fea5a332068e725fbbdf9385d3;p=thirdparty%2Fsystemd.git network: add DefaultRouteOnDevice= setting in [Network] section When enabled, then default route bound to the interface will be created. This is useful when adding routes on point-to-point interfaces. Closes #788. --- diff --git a/man/systemd.network.xml b/man/systemd.network.xml index 2ce7c7096e4..dd0535a06ee 100644 --- a/man/systemd.network.xml +++ b/man/systemd.network.xml @@ -347,6 +347,15 @@ + + DefaultRouteOnDevice= + + Takes a boolean. If set to true, sets up the default route bound to the interface. + Defaults to false. This is useful when creating routes on point-to-point interfaces. + This is equivalent to e.g. the following. + ip route add default dev veth99 + + IPv6Token= diff --git a/src/network/networkd-network-gperf.gperf b/src/network/networkd-network-gperf.gperf index f87d7def44c..3c6b5ac1004 100644 --- a/src/network/networkd-network-gperf.gperf +++ b/src/network/networkd-network-gperf.gperf @@ -53,6 +53,7 @@ Network.DHCP, config_parse_dhcp, Network.DHCPServer, config_parse_bool, 0, offsetof(Network, dhcp_server) Network.LinkLocalAddressing, config_parse_link_local_address_family_boolean, 0, offsetof(Network, link_local) Network.IPv4LLRoute, config_parse_bool, 0, offsetof(Network, ipv4ll_route) +Network.DefaultRouteOnDevice, config_parse_bool, 0, offsetof(Network, default_route_on_device) Network.IPv6Token, config_parse_ipv6token, 0, offsetof(Network, ipv6_token) Network.LLDP, config_parse_lldp_mode, 0, offsetof(Network, lldp_mode) Network.EmitLLDP, config_parse_lldp_emit, 0, offsetof(Network, lldp_emit) diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c index 56a45379c16..755cd604dca 100644 --- a/src/network/networkd-network.c +++ b/src/network/networkd-network.c @@ -454,6 +454,11 @@ int network_load_one(Manager *manager, const char *filename) { if (r < 0) log_warning_errno(r, "%s: Failed to add IPv4LL route, ignoring: %m", network->filename); + r = network_add_default_route_on_device(network); + if (r < 0) + log_warning_errno(r, "%s: Failed to add default route on device, ignoring: %m", + network->filename); + r = ordered_hashmap_ensure_allocated(&manager->networks, &string_hash_ops); if (r < 0) return r; diff --git a/src/network/networkd-network.h b/src/network/networkd-network.h index d00062fe599..22c7dc5afd7 100644 --- a/src/network/networkd-network.h +++ b/src/network/networkd-network.h @@ -151,6 +151,8 @@ struct Network { AddressFamilyBoolean link_local; bool ipv4ll_route; + bool default_route_on_device; + /* IPv6 prefix delegation support */ RADVPrefixDelegation router_prefix_delegation; usec_t router_lifetime_usec; diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c index 179eeac017b..24ef5b46bc7 100644 --- a/src/network/networkd-route.c +++ b/src/network/networkd-route.c @@ -712,6 +712,30 @@ int network_add_ipv4ll_route(Network *network) { return 0; } +int network_add_default_route_on_device(Network *network) { + _cleanup_(route_free_or_set_invalidp) Route *n = NULL; + int r; + + assert(network); + + if (!network->default_route_on_device) + return 0; + + /* DefaultRouteOnDevice= is in [Network] section. */ + r = route_new_static(network, NULL, 0, &n); + if (r < 0) + return r; + + r = in_addr_from_string(AF_INET, "169.254.0.0", &n->dst); + if (r < 0) + return r; + + n->family = AF_INET; + + TAKE_PTR(n); + return 0; +} + int config_parse_gateway( const char *unit, const char *filename, diff --git a/src/network/networkd-route.h b/src/network/networkd-route.h index 892da25bd56..4643f778547 100644 --- a/src/network/networkd-route.h +++ b/src/network/networkd-route.h @@ -62,6 +62,7 @@ int route_section_verify(Route *route, Network *network); DEFINE_NETWORK_SECTION_FUNCTIONS(Route, route_free); int network_add_ipv4ll_route(Network *network); +int network_add_default_route_on_device(Network *network); CONFIG_PARSER_PROTOTYPE(config_parse_gateway); CONFIG_PARSER_PROTOTYPE(config_parse_preferred_src); diff --git a/test/fuzz/fuzz-network-parser/directives.network b/test/fuzz/fuzz-network-parser/directives.network index 06813c29fe3..ef29c7951f3 100644 --- a/test/fuzz/fuzz-network-parser/directives.network +++ b/test/fuzz/fuzz-network-parser/directives.network @@ -89,6 +89,7 @@ IPMasquerade= ProxyARP= PrimarySlave= IPv4LLRoute= +DefaultRouteOnDevice= Address= IPv6ProxyNDPAddress= IPv6AcceptRA=