Skip to content

Commit c4ff062

Browse files
committed
network: add RouteMetric= setting in [Address] section
1 parent a8d21c9 commit c4ff062

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-0
lines changed

man/systemd.network.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,15 @@ IPv6Token=prefixstable:2002:da8:1::</programlisting></para>
10991099
Defaults to <literal>global</literal>.</para>
11001100
</listitem>
11011101
</varlistentry>
1102+
<varlistentry>
1103+
<term><varname>RouteMetric=</varname></term>
1104+
<listitem>
1105+
<para>The metric of the prefix route, which is pointing to the subnet of the configured IP
1106+
address, taking the configured prefix length into account. Takes an unsigned integer in the
1107+
range 0…4294967295. When unset or set to 0, the kernel's default value is used. This
1108+
setting will be ignored when <varname>AddPrefixRoute=</varname> is false.</para>
1109+
</listitem>
1110+
</varlistentry>
11021111
<varlistentry>
11031112
<term><varname>HomeAddress=</varname></term>
11041113
<listitem>

src/network/networkd-address.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,10 @@ int address_configure(
918918
if (r < 0)
919919
return log_link_error_errno(link, r, "Could not append IFA_CACHEINFO attribute: %m");
920920

921+
r = sd_netlink_message_append_u32(req, IFA_RT_PRIORITY, address->route_metric);
922+
if (r < 0)
923+
return log_link_error_errno(link, r, "Could not append IFA_RT_PRIORITY attribute: %m");
924+
921925
k = address_add(link, address, &a);
922926
if (k < 0)
923927
return log_link_error_errno(link, k, "Could not add address: %m");
@@ -1801,6 +1805,48 @@ int config_parse_address_scope(
18011805
return 0;
18021806
}
18031807

1808+
int config_parse_address_route_metric(
1809+
const char *unit,
1810+
const char *filename,
1811+
unsigned line,
1812+
const char *section,
1813+
unsigned section_line,
1814+
const char *lvalue,
1815+
int ltype,
1816+
const char *rvalue,
1817+
void *data,
1818+
void *userdata) {
1819+
1820+
Network *network = userdata;
1821+
_cleanup_(address_free_or_set_invalidp) Address *n = NULL;
1822+
int r;
1823+
1824+
assert(filename);
1825+
assert(section);
1826+
assert(lvalue);
1827+
assert(rvalue);
1828+
assert(data);
1829+
1830+
r = address_new_static(network, filename, section_line, &n);
1831+
if (r == -ENOMEM)
1832+
return log_oom();
1833+
if (r < 0) {
1834+
log_syntax(unit, LOG_WARNING, filename, line, r,
1835+
"Failed to allocate new address, ignoring assignment: %m");
1836+
return 0;
1837+
}
1838+
1839+
r = safe_atou32(rvalue, &n->route_metric);
1840+
if (r < 0) {
1841+
log_syntax(unit, LOG_WARNING, filename, line, r,
1842+
"Could not parse %s=, ignoring assignment: %s", lvalue, rvalue);
1843+
return 0;
1844+
}
1845+
1846+
TAKE_PTR(n);
1847+
return 0;
1848+
}
1849+
18041850
int config_parse_duplicate_address_detection(
18051851
const char *unit,
18061852
const char *filename,

src/network/networkd-address.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef struct Address {
2828
unsigned char prefixlen;
2929
unsigned char scope;
3030
uint32_t flags;
31+
uint32_t route_metric; /* route metric for prefix route */
3132
char *label;
3233

3334
int set_broadcast;
@@ -83,6 +84,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_label);
8384
CONFIG_PARSER_PROTOTYPE(config_parse_lifetime);
8485
CONFIG_PARSER_PROTOTYPE(config_parse_address_flags);
8586
CONFIG_PARSER_PROTOTYPE(config_parse_address_scope);
87+
CONFIG_PARSER_PROTOTYPE(config_parse_address_route_metric);
8688
CONFIG_PARSER_PROTOTYPE(config_parse_duplicate_address_detection);
8789

8890
#define IPV4_ADDRESS_FMT_STR "%u.%u.%u.%u"

src/network/networkd-network-gperf.gperf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ Address.AddPrefixRoute, config_parse_address_flags,
146146
Address.AutoJoin, config_parse_address_flags, IFA_F_MCAUTOJOIN, 0
147147
Address.DuplicateAddressDetection, config_parse_duplicate_address_detection, 0, 0
148148
Address.Scope, config_parse_address_scope, 0, 0
149+
Address.RouteMetric, config_parse_address_route_metric, 0, 0
149150
IPv6AddressLabel.Prefix, config_parse_address_label_prefix, 0, 0
150151
IPv6AddressLabel.Label, config_parse_address_label, 0, 0
151152
Neighbor.Address, config_parse_neighbor_address, 0, 0

test/fuzz/fuzz-network-parser/directives.network

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ ManageTemporaryAddress=
270270
Broadcast=
271271
Peer=
272272
Label=
273+
RouteMetric=
273274
[RoutingPolicyRule]
274275
Table=
275276
IncomingInterface=

0 commit comments

Comments
 (0)