Skip to content

Commit 17193d7

Browse files
committed
network: neighbor: use "struct hw_addr_data" to store link layer address
1 parent ca7d208 commit 17193d7

File tree

3 files changed

+13
-83
lines changed

3 files changed

+13
-83
lines changed

src/network/networkd-neighbor.c

Lines changed: 11 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ void neighbor_hash_func(const Neighbor *neighbor, struct siphash *state) {
9191
assert(neighbor);
9292

9393
siphash24_compress(&neighbor->family, sizeof(neighbor->family), state);
94-
siphash24_compress(&neighbor->lladdr_size, sizeof(neighbor->lladdr_size), state);
9594

9695
switch (neighbor->family) {
9796
case AF_INET:
@@ -104,7 +103,7 @@ void neighbor_hash_func(const Neighbor *neighbor, struct siphash *state) {
104103
break;
105104
}
106105

107-
siphash24_compress(&neighbor->lladdr, neighbor->lladdr_size, state);
106+
hw_addr_hash_func(&neighbor->ll_addr, state);
108107
}
109108

110109
int neighbor_compare_func(const Neighbor *a, const Neighbor *b) {
@@ -114,10 +113,6 @@ int neighbor_compare_func(const Neighbor *a, const Neighbor *b) {
114113
if (r != 0)
115114
return r;
116115

117-
r = CMP(a->lladdr_size, b->lladdr_size);
118-
if (r != 0)
119-
return r;
120-
121116
switch (a->family) {
122117
case AF_INET:
123118
case AF_INET6:
@@ -126,7 +121,7 @@ int neighbor_compare_func(const Neighbor *a, const Neighbor *b) {
126121
return r;
127122
}
128123

129-
return memcmp(&a->lladdr, &b->lladdr, a->lladdr_size);
124+
return hw_addr_compare(&a->ll_addr, &b->ll_addr);
130125
}
131126

132127
DEFINE_PRIVATE_HASH_OPS_WITH_KEY_DESTRUCTOR(neighbor_hash_ops, Neighbor, neighbor_hash_func, neighbor_compare_func, neighbor_free);
@@ -163,7 +158,7 @@ static int neighbor_add(Link *link, Neighbor *neighbor) {
163158
}
164159

165160
static void log_neighbor_debug(const Neighbor *neighbor, const char *str, const Link *link) {
166-
_cleanup_free_ char *state = NULL, *lladdr = NULL, *dst = NULL;
161+
_cleanup_free_ char *state = NULL, *dst = NULL;
167162

168163
assert(neighbor);
169164
assert(str);
@@ -172,19 +167,12 @@ static void log_neighbor_debug(const Neighbor *neighbor, const char *str, const
172167
return;
173168

174169
(void) network_config_state_to_string_alloc(neighbor->state, &state);
175-
if (neighbor->lladdr_size == sizeof(struct ether_addr))
176-
(void) ether_addr_to_string_alloc(&neighbor->lladdr.mac, &lladdr);
177-
else if (neighbor->lladdr_size == sizeof(struct in_addr))
178-
(void) in_addr_to_string(AF_INET, &neighbor->lladdr.ip, &lladdr);
179-
else if (neighbor->lladdr_size == sizeof(struct in6_addr))
180-
(void) in_addr_to_string(AF_INET6, &neighbor->lladdr.ip, &lladdr);
181-
182170
(void) in_addr_to_string(neighbor->family, &neighbor->in_addr, &dst);
183171

184172
log_link_debug(link,
185173
"%s %s neighbor (%s): lladdr: %s, dst: %s",
186174
str, strna(network_config_source_to_string(neighbor->source)), strna(state),
187-
strna(lladdr), strna(dst));
175+
HW_ADDR_TO_STR(&neighbor->ll_addr), strna(dst));
188176
}
189177

190178
static int neighbor_configure(
@@ -213,7 +201,7 @@ static int neighbor_configure(
213201
if (r < 0)
214202
return log_link_error_errno(link, r, "Could not set state: %m");
215203

216-
r = sd_netlink_message_append_data(req, NDA_LLADDR, &neighbor->lladdr, neighbor->lladdr_size);
204+
r = netlink_message_append_hw_addr(req, NDA_LLADDR, &neighbor->ll_addr);
217205
if (r < 0)
218206
return log_link_error_errno(link, r, "Could not append NDA_LLADDR attribute: %m");
219207

@@ -466,7 +454,6 @@ int request_process_neighbor(Request *req) {
466454

467455
int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
468456
_cleanup_(neighbor_freep) Neighbor *tmp = NULL;
469-
_cleanup_free_ void *lladdr = NULL;
470457
Neighbor *neighbor = NULL;
471458
uint16_t type, state;
472459
int ifindex, r;
@@ -536,15 +523,11 @@ int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message,
536523
return 0;
537524
}
538525

539-
r = sd_netlink_message_read_data(message, NDA_LLADDR, &tmp->lladdr_size, &lladdr);
526+
r = netlink_message_read_hw_addr(message, NDA_LLADDR, &tmp->ll_addr);
540527
if (r < 0) {
541-
log_link_warning_errno(link, r, "rtnl: received neighbor message without valid lladdr, ignoring: %m");
542-
return 0;
543-
} else if (!IN_SET(tmp->lladdr_size, sizeof(struct ether_addr), sizeof(struct in_addr), sizeof(struct in6_addr))) {
544-
log_link_warning(link, "rtnl: received neighbor message with invalid lladdr size (%zu), ignoring: %m", tmp->lladdr_size);
528+
log_link_warning_errno(link, r, "rtnl: received neighbor message without valid link layer address, ignoring: %m");
545529
return 0;
546530
}
547-
memcpy(&tmp->lladdr, lladdr, tmp->lladdr_size);
548531

549532
(void) neighbor_get(link, tmp, &neighbor);
550533

@@ -596,7 +579,7 @@ static int neighbor_section_verify(Neighbor *neighbor) {
596579
"Ignoring [Neighbor] section from line %u.",
597580
neighbor->section->filename, neighbor->section->line);
598581

599-
if (neighbor->lladdr_size == 0)
582+
if (neighbor->ll_addr.length == 0)
600583
return log_warning_errno(SYNTHETIC_ERRNO(EINVAL),
601584
"%s: Neighbor section without LinkLayerAddress= configured. "
602585
"Ignoring [Neighbor] section from line %u.",
@@ -666,51 +649,6 @@ int config_parse_neighbor_lladdr(
666649
void *data,
667650
void *userdata) {
668651

669-
Network *network = userdata;
670-
_cleanup_(neighbor_free_or_set_invalidp) Neighbor *n = NULL;
671-
int family, r;
672-
673-
assert(filename);
674-
assert(section);
675-
assert(lvalue);
676-
assert(rvalue);
677-
assert(data);
678-
679-
r = neighbor_new_static(network, filename, section_line, &n);
680-
if (r < 0)
681-
return log_oom();
682-
683-
r = parse_ether_addr(rvalue, &n->lladdr.mac);
684-
if (r >= 0)
685-
n->lladdr_size = sizeof(n->lladdr.mac);
686-
else {
687-
r = in_addr_from_string_auto(rvalue, &family, &n->lladdr.ip);
688-
if (r < 0) {
689-
log_syntax(unit, LOG_WARNING, filename, line, r,
690-
"Neighbor LinkLayerAddress= is invalid, ignoring assignment: %s",
691-
rvalue);
692-
return 0;
693-
}
694-
n->lladdr_size = family == AF_INET ? sizeof(n->lladdr.ip.in) : sizeof(n->lladdr.ip.in6);
695-
}
696-
697-
TAKE_PTR(n);
698-
699-
return 0;
700-
}
701-
702-
int config_parse_neighbor_hwaddr(
703-
const char *unit,
704-
const char *filename,
705-
unsigned line,
706-
const char *section,
707-
unsigned section_line,
708-
const char *lvalue,
709-
int ltype,
710-
const char *rvalue,
711-
void *data,
712-
void *userdata) {
713-
714652
Network *network = userdata;
715653
_cleanup_(neighbor_free_or_set_invalidp) Neighbor *n = NULL;
716654
int r;
@@ -725,15 +663,14 @@ int config_parse_neighbor_hwaddr(
725663
if (r < 0)
726664
return log_oom();
727665

728-
r = parse_ether_addr(rvalue, &n->lladdr.mac);
666+
r = parse_hw_addr(rvalue, &n->ll_addr);
729667
if (r < 0) {
730668
log_syntax(unit, LOG_WARNING, filename, line, r,
731-
"Neighbor MACAddress= is invalid, ignoring assignment: %s", rvalue);
669+
"Neighbor %s= is invalid, ignoring assignment: %s",
670+
lvalue, rvalue);
732671
return 0;
733672
}
734673

735-
n->lladdr_size = sizeof(n->lladdr.mac);
736674
TAKE_PTR(n);
737-
738675
return 0;
739676
}

src/network/networkd-neighbor.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ typedef struct Manager Manager;
1515
typedef struct Network Network;
1616
typedef struct Request Request;
1717

18-
union lladdr_union {
19-
struct ether_addr mac;
20-
union in_addr_union ip;
21-
};
22-
2318
typedef struct Neighbor {
2419
Network *network;
2520
Link *link;
@@ -29,8 +24,7 @@ typedef struct Neighbor {
2924

3025
int family;
3126
union in_addr_union in_addr;
32-
union lladdr_union lladdr;
33-
size_t lladdr_size;
27+
struct hw_addr_data ll_addr;
3428
} Neighbor;
3529

3630
Neighbor *neighbor_free(Neighbor *neighbor);
@@ -52,5 +46,4 @@ int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message,
5246
DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(Neighbor, neighbor);
5347

5448
CONFIG_PARSER_PROTOTYPE(config_parse_neighbor_address);
55-
CONFIG_PARSER_PROTOTYPE(config_parse_neighbor_hwaddr);
5649
CONFIG_PARSER_PROTOTYPE(config_parse_neighbor_lladdr);

src/network/networkd-network-gperf.gperf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ IPv6AddressLabel.Prefix, config_parse_address_label_prefix,
156156
IPv6AddressLabel.Label, config_parse_address_label, 0, 0
157157
Neighbor.Address, config_parse_neighbor_address, 0, 0
158158
Neighbor.LinkLayerAddress, config_parse_neighbor_lladdr, 0, 0
159-
Neighbor.MACAddress, config_parse_neighbor_hwaddr, 0, 0 /* deprecated */
159+
Neighbor.MACAddress, config_parse_neighbor_lladdr, 0, 0 /* deprecated */
160160
RoutingPolicyRule.TypeOfService, config_parse_routing_policy_rule_tos, 0, 0
161161
RoutingPolicyRule.Priority, config_parse_routing_policy_rule_priority, 0, 0
162162
RoutingPolicyRule.Table, config_parse_routing_policy_rule_table, 0, 0

0 commit comments

Comments
 (0)