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.
This commit is contained in:
Yu Watanabe 2019-05-14 16:43:14 +09:00
parent 807341ec99
commit 5d5003ab35
7 changed files with 43 additions and 0 deletions

View File

@ -347,6 +347,15 @@
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>DefaultRouteOnDevice=</varname></term>
<listitem>
<para>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.
<programlisting>ip route add default dev veth99</programlisting></para>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>IPv6Token=</varname></term>
<listitem>

View File

@ -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)

View File

@ -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;

View File

@ -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;

View File

@ -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,

View File

@ -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);

View File

@ -89,6 +89,7 @@ IPMasquerade=
ProxyARP=
PrimarySlave=
IPv4LLRoute=
DefaultRouteOnDevice=
Address=
IPv6ProxyNDPAddress=
IPv6AcceptRA=