Merge pull request #15136 from yuwata/network-dhcp4-use-gateway

network: add a flag to ignore gateway provided by DHCP server
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-03-17 16:51:18 +01:00 committed by GitHub
commit 34bc838f15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 38 additions and 2 deletions

View file

@ -1474,7 +1474,13 @@
"link" scope will be used. For anything else, scope defaults to "global".</para> "link" scope will be used. For anything else, scope defaults to "global".</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
<varlistentry>
<term><varname>UseGateway=</varname></term>
<listitem>
<para>When true (the default), the gateway will be requested from the DHCP server and added to the
routing table with a metric of 1024, and a scope of "link".</para>
</listitem>
</varlistentry>
<varlistentry> <varlistentry>
<term><varname>UseTimezone=</varname></term> <term><varname>UseTimezone=</varname></term>

View file

@ -323,6 +323,9 @@ static int link_set_dhcp_routes(Link *link) {
} }
} }
if (!link->network->dhcp_use_gateway)
return 0;
r = sd_dhcp_lease_get_router(link->dhcp_lease, &router); r = sd_dhcp_lease_get_router(link->dhcp_lease, &router);
if (IN_SET(r, 0, -ENODATA)) if (IN_SET(r, 0, -ENODATA))
log_link_info(link, "DHCP: No gateway received from DHCP server."); log_link_info(link, "DHCP: No gateway received from DHCP server.");
@ -451,7 +454,7 @@ static int dhcp_remove_router(Link *link, sd_dhcp_lease *lease, const struct in_
assert(link); assert(link);
assert(address); assert(address);
if (!link->network->dhcp_use_routes) if (!link->network->dhcp_use_gateway)
return 0; return 0;
r = sd_dhcp_lease_get_router(lease, &router); r = sd_dhcp_lease_get_router(lease, &router);

View file

@ -163,6 +163,7 @@ DHCPv4.UseMTU, config_parse_bool,
DHCPv4.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp_use_hostname) DHCPv4.UseHostname, config_parse_bool, 0, offsetof(Network, dhcp_use_hostname)
DHCPv4.UseDomains, config_parse_dhcp_use_domains, 0, offsetof(Network, dhcp_use_domains) DHCPv4.UseDomains, config_parse_dhcp_use_domains, 0, offsetof(Network, dhcp_use_domains)
DHCPv4.UseRoutes, config_parse_bool, 0, offsetof(Network, dhcp_use_routes) DHCPv4.UseRoutes, config_parse_bool, 0, offsetof(Network, dhcp_use_routes)
DHCPv4.UseGateway, config_parse_bool, 0, offsetof(Network, dhcp_use_gateway)
DHCPv4.RequestOptions, config_parse_dhcp_request_options, 0, 0 DHCPv4.RequestOptions, config_parse_dhcp_request_options, 0, 0
DHCPv4.Anonymize, config_parse_bool, 0, offsetof(Network, dhcp_anonymize) DHCPv4.Anonymize, config_parse_bool, 0, offsetof(Network, dhcp_anonymize)
DHCPv4.SendHostname, config_parse_bool, 0, offsetof(Network, dhcp_send_hostname) DHCPv4.SendHostname, config_parse_bool, 0, offsetof(Network, dhcp_send_hostname)

View file

@ -384,6 +384,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
.dhcp_use_dns = true, .dhcp_use_dns = true,
.dhcp_use_hostname = true, .dhcp_use_hostname = true,
.dhcp_use_routes = true, .dhcp_use_routes = true,
.dhcp_use_gateway = true,
/* NOTE: this var might be overwritten by network_apply_anonymize_if_set */ /* NOTE: this var might be overwritten by network_apply_anonymize_if_set */
.dhcp_send_hostname = true, .dhcp_send_hostname = true,
.dhcp_send_release = true, .dhcp_send_release = true,

View file

@ -109,6 +109,7 @@ struct Network {
bool dhcp_use_sip; bool dhcp_use_sip;
bool dhcp_use_mtu; bool dhcp_use_mtu;
bool dhcp_use_routes; bool dhcp_use_routes;
bool dhcp_use_gateway;
bool dhcp_use_timezone; bool dhcp_use_timezone;
bool rapid_commit; bool rapid_commit;
bool dhcp_use_hostname; bool dhcp_use_hostname;

View file

@ -73,6 +73,7 @@ UseDNS=
RoutesToDNS= RoutesToDNS=
UseDomains= UseDomains=
UseRoutes= UseRoutes=
UseGateway=
IAID= IAID=
UserClass= UserClass=
UseNTP= UseNTP=

View file

@ -0,0 +1,9 @@
[Match]
Name=veth99
[Network]
DHCP=ipv4
IPv6AcceptRA=false
[DHCPv4]
UseGateway=no

View file

@ -2921,6 +2921,7 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
'dhcp-client-ipv4-dhcp-settings.network', 'dhcp-client-ipv4-dhcp-settings.network',
'dhcp-client-ipv4-only-ipv6-disabled.network', 'dhcp-client-ipv4-only-ipv6-disabled.network',
'dhcp-client-ipv4-only.network', 'dhcp-client-ipv4-only.network',
'dhcp-client-ipv4-use-gateway-no.network',
'dhcp-client-ipv4-use-routes-no.network', 'dhcp-client-ipv4-use-routes-no.network',
'dhcp-client-ipv6-only.network', 'dhcp-client-ipv6-only.network',
'dhcp-client-ipv6-rapid-commit.network', 'dhcp-client-ipv6-rapid-commit.network',
@ -3040,6 +3041,19 @@ class NetworkdDHCPClientTests(unittest.TestCase, Utilities):
self.assertRegex(output, r'default via 192.168.5.1 proto dhcp src 192.168.5.181 metric 1024') self.assertRegex(output, r'default via 192.168.5.1 proto dhcp src 192.168.5.181 metric 1024')
self.assertRegex(output, r'192.168.5.1 proto dhcp scope link src 192.168.5.181 metric 1024') self.assertRegex(output, r'192.168.5.1 proto dhcp scope link src 192.168.5.181 metric 1024')
def test_dhcp_client_ipv4_use_gateway_no(self):
copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network', 'dhcp-client-ipv4-use-gateway-no.network')
start_networkd()
self.wait_online(['veth-peer:carrier'])
start_dnsmasq(additional_options='--dhcp-option=option:dns-server,192.168.5.6,192.168.5.7', lease_time='2m')
self.wait_online(['veth99:routable', 'veth-peer:routable'])
output = check_output('ip route show dev veth99')
print(output)
self.assertRegex(output, r'192.168.5.0/24 via 192.168.5.5 proto dhcp src 192.168.5.181 metric 1024')
self.assertNotRegex(output, r'default via 192.168.5.1')
def test_dhcp_client_ipv4_ipv6(self): def test_dhcp_client_ipv4_ipv6(self):
copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network', 'dhcp-client-ipv6-only.network', copy_unit_to_networkd_unit_path('25-veth.netdev', 'dhcp-server-veth-peer.network', 'dhcp-client-ipv6-only.network',
'dhcp-client-ipv4-only.network') 'dhcp-client-ipv4-only.network')