Skip to contentSkip to conversation

net/ethernet: Use ether_addr_copy rather than memcpy

PATCH In review Greg Rose 3 participants 5 messages Aug 31, 2016

Subsystems (2)
lore
Share & tools
lore

Greg Rose

author
#0 lore raw

PATCH 1/1 net/ethernet: Use ether_addr_copy rather than memcpy

I'm not sure why this hasn't been done before because it seems obvious, so maybe there is some reason that memcpy is used instead of ether_addr_copy in this code. But let's try this anyway.

Change memcpy to ether_addr_copy.

---
 net/ethernet/eth.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
net/ethernet/eth.c +8 -8
index 66dff5e..cbcdb97 100644
@@ -95,10 +95,10 @@ int eth_header(struct sk_buff *skb, struct net_device *dev,
 
 	if (!saddr)
 		saddr = dev->dev_addr;
-	memcpy(eth->h_source, saddr, ETH_ALEN);
+	ether_addr_copy(eth->h_source, saddr);
 
 	if (daddr) {
-		memcpy(eth->h_dest, daddr, ETH_ALEN);
+		ether_addr_copy(eth->h_dest, daddr);
 		return ETH_HLEN;
 	}
 
@@ -211,7 +211,7 @@ EXPORT_SYMBOL(eth_type_trans);
 int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr)
 {
 	const struct ethhdr *eth = eth_hdr(skb);
-	memcpy(haddr, eth->h_source, ETH_ALEN);
+	ether_addr_copy(haddr, eth->h_source);
 	return ETH_ALEN;
 }
 EXPORT_SYMBOL(eth_header_parse);
@@ -236,8 +236,8 @@ int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh, __be16
 		return -1;
 
 	eth->h_proto = type;
-	memcpy(eth->h_source, dev->dev_addr, ETH_ALEN);
-	memcpy(eth->h_dest, neigh->ha, ETH_ALEN);
+	ether_addr_copy(eth->h_source, dev->dev_addr);
+	ether_addr_copy(eth->h_dest, neigh->ha);
 	hh->hh_len = ETH_HLEN;
 	return 0;
 }
@@ -255,8 +255,8 @@ void eth_header_cache_update(struct hh_cache *hh,
 			     const struct net_device *dev,
 			     const unsigned char *haddr)
 {
-	memcpy(((u8 *) hh->hh_data) + HH_DATA_OFF(sizeof(struct ethhdr)),
-	       haddr, ETH_ALEN);
+	ether_addr_copy(((u8 *)hh->hh_data) +
+			HH_DATA_OFF(sizeof(struct ethhdr)), haddr);
 }
 EXPORT_SYMBOL(eth_header_cache_update);
 
@@ -286,7 +286,7 @@ void eth_commit_mac_addr_change(struct net_device *dev, void *p)
 {
 	struct sockaddr *addr = p;
 
-	memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
+	ether_addr_copy(dev->dev_addr, addr->sa_data);
 }
 EXPORT_SYMBOL(eth_commit_mac_addr_change);
 
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 5 lines · Greg Rose

I'm not sure why this hasn't been done before because it seems obvious, so maybe there is some reason that memcpy is used instead of ether_addr_copy in this code.  But let's try this anyway.

Change memcpy to ether_addr_copy.

I looked at this once and gave up given the need to verify all the possible callers that require an __aligned(2) source address.

author
#2 lore raw
Quoted 10 lines · Joe Perches
Quoted 5 lines · Greg Rose

I'm not sure why this hasn't been done before because it seems obvious, so maybe there is some reason that memcpy is used instead of ether_addr_copy in this code. But let's try this anyway.

Change memcpy to ether_addr_copy.

I looked at this once and gave up given the need to verify all the possible callers that require an __aligned(2) source address.

Ha, I figured there must be some reason for it. OK, patch withdrawn.

Thanks!

#3 lore raw
In reply to Greg Rose
Quoted 5 lines · Greg Rose

I'm not sure why this hasn't been done before because it seems obvious, so maybe there is some reason that memcpy is used instead of ether_addr_copy in this code. But let's try this anyway.

Change memcpy to ether_addr_copy.

...

net/ethernet/eth.c EXPORT_SYMBOL(eth_type_trans); · line 214

Quoted 7 lines
net/ethernet/eth.c +1 -1
@@ -211,7 +211,7 @@ EXPORT_SYMBOL(eth_type_trans);
 int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr)
 {
 	const struct ethhdr *eth = eth_hdr(skb);
-	memcpy(haddr, eth->h_source, ETH_ALEN);
+	ether_addr_copy(haddr, eth->h_source);

Please carefully read ether_addr_copy() comments.

Not all arches are like x86

author
#4 lore raw
Quoted 20 lines · Eric Dumazet
Quoted 5 lines · Greg Rose

I'm not sure why this hasn't been done before because it seems obvious, so maybe there is some reason that memcpy is used instead of ether_addr_copy in this code. But let's try this anyway.

Change memcpy to ether_addr_copy.

...

net/ethernet/eth.c EXPORT_SYMBOL(eth_type_trans); · line 214

Quoted 7 lines
net/ethernet/eth.c +1 -1
@@ -211,7 +211,7 @@ EXPORT_SYMBOL(eth_type_trans);
 int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr)
 {
 	const struct ethhdr *eth = eth_hdr(skb);
-	memcpy(haddr, eth->h_source, ETH_ALEN);
+	ether_addr_copy(haddr, eth->h_source);

Please carefully read ether_addr_copy() comments.

Quoted 1 line

Not all arches are like x86

Thanks Eric, Joe set me straight already.

- Greg

Quoted 3 lines