1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

Merge pull request #12496 from yuwata/network-on-device-default-route

network: add DefaultRouteOnDevice= setting in [Network] section
This commit is contained in:
Yu Watanabe 2019-05-15 22:46:25 +02:00 committed by GitHub
commit 05dc2132e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 53 additions and 2 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

@ -34,7 +34,9 @@ static int ipv4ll_address_lost(Link *link) {
address->prefixlen = 16;
address->scope = RT_SCOPE_LINK;
address_remove(address, link, NULL);
r = address_remove(address, link, NULL);
if (r < 0)
return r;
r = route_new(&route);
if (r < 0)
@ -44,7 +46,9 @@ static int ipv4ll_address_lost(Link *link) {
route->scope = RT_SCOPE_LINK;
route->priority = IPV4LL_ROUTE_METRIC;
route_remove(route, link, NULL);
r = route_remove(route, link, NULL);
if (r < 0)
return r;
link_check_ready(link);

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=

View File

@ -5,6 +5,8 @@ Name=dummy98
IPv6AcceptRA=no
Address=2001:1234:5:8f63::1/128
Address=149.10.124.58/28
DefaultRouteOnDevice=yes
IPv4LLRoute=yes
[Route]
Destination=2001:1234:5:8fff:ff:ff:ff:ff/128

View File

@ -1203,12 +1203,14 @@ class NetworkdNetWorkTests(unittest.TestCase, Utilities):
print(output)
self.assertRegex(output, '149.10.124.48/28 proto kernel scope link src 149.10.124.58')
self.assertRegex(output, '149.10.124.64 proto static scope link')
self.assertRegex(output, '169.254.0.0/16 proto static scope link metric 2048')
self.assertRegex(output, '192.168.1.1 proto static initcwnd 20')
self.assertRegex(output, '192.168.1.2 proto static initrwnd 30')
output = subprocess.check_output(['ip', '-4', 'route', 'show', 'dev', 'dummy98', 'default']).rstrip().decode('utf-8')
self.assertRegex(output, 'default via 149.10.125.65 proto static onlink')
self.assertRegex(output, 'default via 149.10.124.64 proto static')
self.assertRegex(output, 'default proto static')
output = subprocess.check_output(['ip', 'route', 'show', 'type', 'blackhole']).rstrip().decode('utf-8')
print(output)