Skip to content

Commit d11ba68

Browse files
committed
network/radv: use sd_ndisc_prefix and friends to manage prefixes and so on
No functional change, just refactoring.
1 parent 29d9672 commit d11ba68

File tree

2 files changed

+48
-51
lines changed

2 files changed

+48
-51
lines changed

src/network/networkd-radv.c

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ static int prefix_new_static(Network *network, const char *filename, unsigned se
8080
.network = network,
8181
.section = TAKE_PTR(n),
8282

83-
.flags = ND_OPT_PI_FLAG_ONLINK | ND_OPT_PI_FLAG_AUTO,
84-
.preferred_lifetime = RADV_DEFAULT_PREFERRED_LIFETIME_USEC,
85-
.valid_lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC,
83+
.prefix.flags = ND_OPT_PI_FLAG_ONLINK | ND_OPT_PI_FLAG_AUTO,
84+
.prefix.valid_lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC,
85+
.prefix.preferred_lifetime = RADV_DEFAULT_PREFERRED_LIFETIME_USEC,
86+
.prefix.valid_until = USEC_INFINITY,
87+
.prefix.preferred_until = USEC_INFINITY,
8688
};
8789

8890
r = hashmap_ensure_put(&network->prefixes_by_section, &config_section_hash_ops, prefix->section, prefix);
@@ -137,7 +139,8 @@ static int route_prefix_new_static(Network *network, const char *filename, unsig
137139
.network = network,
138140
.section = TAKE_PTR(n),
139141

140-
.lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC,
142+
.route.lifetime = RADV_DEFAULT_VALID_LIFETIME_USEC,
143+
.route.valid_until = USEC_INFINITY,
141144
};
142145

143146
r = hashmap_ensure_put(&network->route_prefixes_by_section, &config_section_hash_ops, prefix->section, prefix);
@@ -192,7 +195,8 @@ static int prefix64_new_static(Network *network, const char *filename, unsigned
192195
.network = network,
193196
.section = TAKE_PTR(n),
194197

195-
.lifetime = RADV_PREF64_DEFAULT_LIFETIME_USEC,
198+
.prefix64.lifetime = RADV_PREF64_DEFAULT_LIFETIME_USEC,
199+
.prefix64.valid_until = USEC_INFINITY,
196200
};
197201

198202
r = hashmap_ensure_put(&network->pref64_prefixes_by_section, &config_section_hash_ops, prefix->section, prefix);
@@ -217,11 +221,11 @@ int link_request_radv_addresses(Link *link) {
217221
continue;
218222

219223
/* radv_generate_addresses() below requires the prefix length <= 64. */
220-
if (p->prefixlen > 64)
224+
if (p->prefix.prefixlen > 64)
221225
continue;
222226

223227
_cleanup_hashmap_free_ Hashmap *tokens_by_address = NULL;
224-
r = radv_generate_addresses(link, p->tokens, &p->prefix, p->prefixlen, &tokens_by_address);
228+
r = radv_generate_addresses(link, p->tokens, &p->prefix.address, p->prefix.prefixlen, &tokens_by_address);
225229
if (r < 0)
226230
return r;
227231

@@ -237,7 +241,7 @@ int link_request_radv_addresses(Link *link) {
237241
address->source = NETWORK_CONFIG_SOURCE_STATIC;
238242
address->family = AF_INET6;
239243
address->in_addr.in6 = *a;
240-
address->prefixlen = p->prefixlen;
244+
address->prefixlen = p->prefix.prefixlen;
241245
address->route_metric = p->route_metric;
242246
address->token = ipv6_token_ref(token);
243247

@@ -285,23 +289,23 @@ static int radv_set_prefix(Link *link, Prefix *prefix) {
285289
if (r < 0)
286290
return r;
287291

288-
r = sd_radv_prefix_set_prefix(p, &prefix->prefix, prefix->prefixlen);
292+
r = sd_radv_prefix_set_prefix(p, &prefix->prefix.address, prefix->prefix.prefixlen);
289293
if (r < 0)
290294
return r;
291295

292-
r = sd_radv_prefix_set_preferred_lifetime(p, prefix->preferred_lifetime, USEC_INFINITY);
296+
r = sd_radv_prefix_set_preferred_lifetime(p, prefix->prefix.preferred_lifetime, prefix->prefix.preferred_until);
293297
if (r < 0)
294298
return r;
295299

296-
r = sd_radv_prefix_set_valid_lifetime(p, prefix->valid_lifetime, USEC_INFINITY);
300+
r = sd_radv_prefix_set_valid_lifetime(p, prefix->prefix.valid_lifetime, prefix->prefix.valid_until);
297301
if (r < 0)
298302
return r;
299303

300-
r = sd_radv_prefix_set_onlink(p, FLAGS_SET(prefix->flags, ND_OPT_PI_FLAG_ONLINK));
304+
r = sd_radv_prefix_set_onlink(p, FLAGS_SET(prefix->prefix.flags, ND_OPT_PI_FLAG_ONLINK));
301305
if (r < 0)
302306
return r;
303307

304-
r = sd_radv_prefix_set_address_autoconfiguration(p, FLAGS_SET(prefix->flags, ND_OPT_PI_FLAG_AUTO));
308+
r = sd_radv_prefix_set_address_autoconfiguration(p, FLAGS_SET(prefix->prefix.flags, ND_OPT_PI_FLAG_AUTO));
305309
if (r < 0)
306310
return r;
307311

@@ -320,11 +324,11 @@ static int radv_set_route_prefix(Link *link, RoutePrefix *prefix) {
320324
if (r < 0)
321325
return r;
322326

323-
r = sd_radv_route_prefix_set_prefix(p, &prefix->prefix, prefix->prefixlen);
327+
r = sd_radv_route_prefix_set_prefix(p, &prefix->route.address, prefix->route.prefixlen);
324328
if (r < 0)
325329
return r;
326330

327-
r = sd_radv_route_prefix_set_lifetime(p, prefix->lifetime, USEC_INFINITY);
331+
r = sd_radv_route_prefix_set_lifetime(p, prefix->route.lifetime, prefix->route.valid_until);
328332
if (r < 0)
329333
return r;
330334

@@ -343,7 +347,7 @@ static int radv_set_pref64_prefix(Link *link, Prefix64 *prefix) {
343347
if (r < 0)
344348
return r;
345349

346-
r = sd_radv_pref64_prefix_set_prefix(p, &prefix->prefix, prefix->prefixlen, prefix->lifetime);
350+
r = sd_radv_pref64_prefix_set_prefix(p, &prefix->prefix64.prefix, prefix->prefix64.prefixlen, prefix->prefix64.lifetime);
347351
if (r < 0)
348352
return r;
349353

@@ -781,36 +785,36 @@ static int prefix_section_verify(Prefix *p) {
781785
if (section_is_invalid(p->section))
782786
return -EINVAL;
783787

784-
if (in6_addr_is_null(&p->prefix))
788+
if (in6_addr_is_null(&p->prefix.address))
785789
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
786790
"%s: [IPv6Prefix] section without Prefix= field configured, "
787791
"or specified prefix is the null address. "
788792
"Ignoring [IPv6Prefix] section from line %u.",
789793
p->section->filename, p->section->line);
790794

791-
if (p->prefixlen < 3 || p->prefixlen > 128)
795+
if (p->prefix.prefixlen < 3 || p->prefix.prefixlen > 128)
792796
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
793797
"%s: Invalid prefix length %u is specified in [IPv6Prefix] section. "
794798
"Valid range is 3…128. Ignoring [IPv6Prefix] section from line %u.",
795-
p->section->filename, p->prefixlen, p->section->line);
799+
p->section->filename, p->prefix.prefixlen, p->section->line);
796800

797-
if (p->prefixlen > 64) {
801+
if (p->prefix.prefixlen > 64) {
798802
log_info("%s:%u: Unusual prefix length %u (> 64) is specified in [IPv6Prefix] section from line %s%s.",
799803
p->section->filename, p->section->line,
800-
p->prefixlen,
804+
p->prefix.prefixlen,
801805
p->assign ? ", refusing to assign an address in " : "",
802-
p->assign ? IN6_ADDR_PREFIX_TO_STRING(&p->prefix, p->prefixlen) : "");
806+
p->assign ? IN6_ADDR_PREFIX_TO_STRING(&p->prefix.address, p->prefix.prefixlen) : "");
803807

804808
p->assign = false;
805809
}
806810

807-
if (p->preferred_lifetime > p->valid_lifetime)
811+
if (p->prefix.preferred_lifetime > p->prefix.valid_lifetime)
808812
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
809813
"%s: The preferred lifetime %s is longer than the valid lifetime %s. "
810814
"Ignoring [IPv6Prefix] section from line %u.",
811815
p->section->filename,
812-
FORMAT_TIMESPAN(p->preferred_lifetime, USEC_PER_SEC),
813-
FORMAT_TIMESPAN(p->valid_lifetime, USEC_PER_SEC),
816+
FORMAT_TIMESPAN(p->prefix.preferred_lifetime, USEC_PER_SEC),
817+
FORMAT_TIMESPAN(p->prefix.valid_lifetime, USEC_PER_SEC),
814818
p->section->line);
815819

816820
return 0;
@@ -820,11 +824,11 @@ static int route_prefix_section_verify(RoutePrefix *p) {
820824
if (section_is_invalid(p->section))
821825
return -EINVAL;
822826

823-
if (p->prefixlen > 128)
827+
if (p->route.prefixlen > 128)
824828
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
825829
"%s: Invalid prefix length %u is specified in [IPv6RoutePrefix] section. "
826830
"Valid range is 0…128. Ignoring [IPv6RoutePrefix] section from line %u.",
827-
p->section->filename, p->prefixlen, p->section->line);
831+
p->section->filename, p->route.prefixlen, p->section->line);
828832

829833
return 0;
830834
}
@@ -915,15 +919,15 @@ int config_parse_prefix(
915919
if (r < 0)
916920
return log_oom();
917921

918-
r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->prefixlen);
922+
r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->prefix.prefixlen);
919923
if (r < 0) {
920924
log_syntax(unit, LOG_WARNING, filename, line, r,
921925
"Prefix is invalid, ignoring assignment: %s", rvalue);
922926
return 0;
923927
}
924928

925-
(void) in6_addr_mask(&a.in6, p->prefixlen);
926-
p->prefix = a.in6;
929+
(void) in6_addr_mask(&a.in6, p->prefix.prefixlen);
930+
p->prefix.address = a.in6;
927931

928932
TAKE_PTR(p);
929933
return 0;
@@ -962,7 +966,7 @@ int config_parse_prefix_boolean(
962966
}
963967

964968
if (ltype != 0)
965-
SET_FLAG(p->flags, ltype, r);
969+
SET_FLAG(p->prefix.flags, ltype, r);
966970
else {
967971
assert(streq(lvalue, "Assign"));
968972
p->assign = r;
@@ -1012,9 +1016,9 @@ int config_parse_prefix_lifetime(
10121016
}
10131017

10141018
if (streq(lvalue, "PreferredLifetimeSec"))
1015-
p->preferred_lifetime = usec;
1019+
p->prefix.preferred_lifetime = usec;
10161020
else if (streq(lvalue, "ValidLifetimeSec"))
1017-
p->valid_lifetime = usec;
1021+
p->prefix.valid_lifetime = usec;
10181022
else
10191023
assert_not_reached();
10201024

@@ -1119,15 +1123,15 @@ int config_parse_route_prefix(
11191123
if (r < 0)
11201124
return log_oom();
11211125

1122-
r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->prefixlen);
1126+
r = in_addr_prefix_from_string(rvalue, AF_INET6, &a, &p->route.prefixlen);
11231127
if (r < 0) {
11241128
log_syntax(unit, LOG_WARNING, filename, line, r,
11251129
"Route prefix is invalid, ignoring assignment: %s", rvalue);
11261130
return 0;
11271131
}
11281132

1129-
(void) in6_addr_mask(&a.in6, p->prefixlen);
1130-
p->prefix = a.in6;
1133+
(void) in6_addr_mask(&a.in6, p->route.prefixlen);
1134+
p->route.address = a.in6;
11311135

11321136
TAKE_PTR(p);
11331137
return 0;
@@ -1172,7 +1176,7 @@ int config_parse_route_prefix_lifetime(
11721176
return 0;
11731177
}
11741178

1175-
p->lifetime = usec;
1179+
p->route.lifetime = usec;
11761180

11771181
TAKE_PTR(p);
11781182
return 0;
@@ -1219,8 +1223,8 @@ int config_parse_pref64_prefix(
12191223
}
12201224

12211225
(void) in6_addr_mask(&a.in6, prefixlen);
1222-
p->prefix = a.in6;
1223-
p->prefixlen = prefixlen;
1226+
p->prefix64.prefix = a.in6;
1227+
p->prefix64.prefixlen = prefixlen;
12241228

12251229
TAKE_PTR(p);
12261230
return 0;
@@ -1265,7 +1269,7 @@ int config_parse_pref64_prefix_lifetime(
12651269
return 0;
12661270
}
12671271

1268-
p->lifetime = usec;
1272+
p->prefix64.lifetime = usec;
12691273

12701274
TAKE_PTR(p);
12711275
return 0;

src/network/networkd-radv.h

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "in-addr-util.h"
1414
#include "conf-parser.h"
15+
#include "ndisc-option.h"
1516
#include "networkd-util.h"
1617

1718
typedef struct Link Link;
@@ -30,11 +31,7 @@ typedef struct Prefix {
3031
Network *network;
3132
ConfigSection *section;
3233

33-
uint8_t flags;
34-
uint8_t prefixlen;
35-
struct in6_addr prefix;
36-
usec_t preferred_lifetime;
37-
usec_t valid_lifetime;
34+
sd_ndisc_prefix prefix;
3835

3936
bool assign;
4037
uint32_t route_metric;
@@ -45,18 +42,14 @@ typedef struct RoutePrefix {
4542
Network *network;
4643
ConfigSection *section;
4744

48-
struct in6_addr prefix;
49-
uint8_t prefixlen;
50-
usec_t lifetime;
45+
sd_ndisc_route route;
5146
} RoutePrefix;
5247

5348
typedef struct Prefix64 {
5449
Network *network;
5550
ConfigSection *section;
5651

57-
struct in6_addr prefix;
58-
uint8_t prefixlen;
59-
usec_t lifetime;
52+
sd_ndisc_prefix64 prefix64;
6053
} Prefix64;
6154

6255
Prefix* prefix_free(Prefix *prefix);

0 commit comments

Comments
 (0)