From 34b63c9e458b08e1894ea458687c8450817762b8 Mon Sep 17 00:00:00 2001 From: Andre Kalb Date: Thu, 30 Jun 2022 10:01:34 +0200 Subject: [PATCH] network: Add support to select an IPv4 link-local start address --- man/systemd.network.xml | 12 +++++++ src/network/networkd-ipv4ll.c | 41 ++++++++++++++++++++++++ src/network/networkd-ipv4ll.h | 1 + src/network/networkd-network-gperf.gperf | 1 + src/network/networkd-network.h | 1 + test/fuzz/fuzz-network-parser/directives | 1 + 6 files changed, 57 insertions(+) diff --git a/man/systemd.network.xml b/man/systemd.network.xml index eae27389ed..d1eca399df 100644 --- a/man/systemd.network.xml +++ b/man/systemd.network.xml @@ -398,6 +398,18 @@ + + IPv4LLStartAddress= + + Specifies the first IPv4 link-local address to try. Takes an IPv4 address + for example 169.254.1.2, from the link-local address range 169.254.0.0/16. + This setting may be useful if the device should always have the same address + as long as there is no address conflict. When unset, a random address will be automatically selected. + Defaults to unset. + + + + IPv4LLRoute= diff --git a/src/network/networkd-ipv4ll.c b/src/network/networkd-ipv4ll.c index 67eb9d2aa1..8833bee233 100644 --- a/src/network/networkd-ipv4ll.c +++ b/src/network/networkd-ipv4ll.c @@ -263,3 +263,44 @@ int config_parse_ipv4ll( return 0; } + +int config_parse_ipv4ll_address( + const char* unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + union in_addr_union a; + struct in_addr *ipv4ll_address = ASSERT_PTR(data); + int r; + + assert(filename); + assert(lvalue); + assert(rvalue); + + if (isempty(rvalue)) { + *ipv4ll_address = (struct in_addr) {}; + return 0; + } + + r = in_addr_from_string(AF_INET, rvalue, &a); + if (r < 0) { + log_syntax(unit, LOG_WARNING, filename, line, r, + "Failed to parse %s=, ignoring assignment: %s", lvalue, rvalue); + return 0; + } + if (!in4_addr_is_link_local(&a.in)) { + log_syntax(unit, LOG_WARNING, filename, line, 0, + "Not a IPv4 link local address, ignoring assignment: %s", rvalue); + return 0; + } + + *ipv4ll_address = a.in; + return 0; +} diff --git a/src/network/networkd-ipv4ll.h b/src/network/networkd-ipv4ll.h index 82acc2ec70..f5c6928535 100644 --- a/src/network/networkd-ipv4ll.h +++ b/src/network/networkd-ipv4ll.h @@ -11,3 +11,4 @@ int ipv4ll_configure(Link *link); int ipv4ll_update_mac(Link *link); CONFIG_PARSER_PROTOTYPE(config_parse_ipv4ll); +CONFIG_PARSER_PROTOTYPE(config_parse_ipv4ll_address); diff --git a/src/network/networkd-network-gperf.gperf b/src/network/networkd-network-gperf.gperf index ceaaa6a0f7..0b0c8da27b 100644 --- a/src/network/networkd-network-gperf.gperf +++ b/src/network/networkd-network-gperf.gperf @@ -106,6 +106,7 @@ Network.DHCPServer, config_parse_bool, Network.LinkLocalAddressing, config_parse_link_local_address_family, 0, offsetof(Network, link_local) Network.IPv6LinkLocalAddressGenerationMode, config_parse_ipv6_link_local_address_gen_mode, 0, offsetof(Network, ipv6ll_address_gen_mode) Network.IPv6StableSecretAddress, config_parse_in_addr_non_null, AF_INET6, offsetof(Network, ipv6ll_stable_secret) +Network.IPv4LLStartAddress, config_parse_ipv4ll_address, 0, offsetof(Network, ipv4ll_start_address) Network.IPv4LLRoute, config_parse_bool, 0, offsetof(Network, ipv4ll_route) Network.DefaultRouteOnDevice, config_parse_bool, 0, offsetof(Network, default_route_on_device) Network.LLDP, config_parse_lldp_mode, 0, offsetof(Network, lldp_mode) diff --git a/src/network/networkd-network.h b/src/network/networkd-network.h index 98e6159040..c653124a9c 100644 --- a/src/network/networkd-network.h +++ b/src/network/networkd-network.h @@ -208,6 +208,7 @@ struct Network { AddressFamily link_local; IPv6LinkLocalAddressGenMode ipv6ll_address_gen_mode; struct in6_addr ipv6ll_stable_secret; + struct in_addr ipv4ll_start_address; bool ipv4ll_route; /* IPv6 RA support */ diff --git a/test/fuzz/fuzz-network-parser/directives b/test/fuzz/fuzz-network-parser/directives index 276f3c9307..ea0de6660c 100644 --- a/test/fuzz/fuzz-network-parser/directives +++ b/test/fuzz/fuzz-network-parser/directives @@ -235,6 +235,7 @@ VXLAN= L2TP= MACsec= LinkLocalAddressing= +IPv4LLStartAddress= IPv6LinkLocalAddressGenerationMode= IPv6StableSecretAddress= ConfigureWithoutCarrier=