net: compare_ether_addr[_64bits]() has no ordering
Subsystems (2)
PATCH 1/1 net: compare_ether_addr[_64bits]() has no ordering
From: Johannes Berg <[email protected]>
Neither compare_ether_addr() nor compare_ether_addr_64bits() (as it can fall back to the former) have comparison semantics like memcmp() where the sign of the return value indicates sort order. We had a bug in the wireless code due to a blind memcmp replacement because of this.
A cursory look suggests that the wireless bug was the only one due to this semantic difference.
--- include/linux/etherdevice.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
include/linux/etherdevice.h +6 -5
@@ -159,7 +159,8 @@ static inline void eth_hw_addr_random(st* @addr1: Pointer to a six-byte array containing the Ethernet address * @addr2: Pointer other six-byte array containing the Ethernet address * -* Compare two ethernet addresses, returns 0 if equal +* Compare two ethernet addresses, returns 0 if equal, non-zero otherwise. +* Unlike memcmp(), it doesn't return a value suitable for sorting. */ static inline unsigned compare_ether_addr (const u8 *addr1,const u8 *addr2) {
@@ -184,10 +185,10 @@ static inline unsigned long zap_last_2by* @addr1: Pointer to an array of 8 bytes * @addr2: Pointer to an other array of 8 bytes * -* Compare two ethernet addresses, returns 0 if equal. -* Same result than "memcmp(addr1, addr2, ETH_ALEN)" but without conditional -* branches, and possibly long word memory accesses on CPU allowing cheap -* unaligned memory reads. +* Compare two ethernet addresses, returns 0 if equal, non-zero otherwise. +* Unlike memcmp(), it doesn't return a value suitable for sorting. +* The function doesn't need any conditional branches and possibly uses +* word memory accesses on CPU allowing cheap unaligned memory reads. * arrays = { byte1, byte2, byte3, byte4, byte6, byte7, pad1, pad2} * * Please note that alignment of addr1 & addr2 is only guaranted to be 16 bits.
Quoted 8 lines · Johannes Berg
From: Johannes Berg <[email protected]>
Neither
compare_ether_addr()norcompare_ether_addr_64bits()(as it can fall back to the former) have comparison semantics likememcmp()where the sign of the return value indicates sort order. We had a bug in the wireless code due to a blind memcmp replacement because of this.
Quoted 7 lines
A cursory look suggests that the wireless bug was the only one due to this semantic difference.
Signed-off-by Johannes Berg <[email protected]>--- include/linux/etherdevice.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
The right way to avoid this kind of problems is to change these functions to return a bool
Quoted 12 lines · Eric Dumazet
Quoted 11 lines · Johannes Berg
From: Johannes Berg <[email protected]>
Neither
compare_ether_addr()norcompare_ether_addr_64bits()(as it can fall back to the former) have comparison semantics likememcmp()where the sign of the return value indicates sort order. We had a bug in the wireless code due to a blind memcmp replacement because of this.A cursory look suggests that the wireless bug was the only one due to this semantic difference.
Quoted 7 lines
Quoted 4 lines
Signed-off-by Johannes Berg <[email protected]>--- include/linux/etherdevice.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)The right way to avoid this kind of problems is to change these functions to return a bool
Well, I guess so, but that'd be a weird thing for a compare_ function... should probably be named equal_... then, but I'm not really able to do such a huge change on the first day after my vacation :-)
johannes
David Miller
maintainerFrom: Johannes Berg <[email protected]>
Date: Mon, 07 May 2012 16:12:41 +0200
Quoted 18 lines · Eric Dumazet
Quoted 17 lines · Johannes Berg
Quoted 15 lines
From: Johannes Berg <[email protected]>
Neither
compare_ether_addr()norcompare_ether_addr_64bits()(as it can fall back to the former) have comparison semantics likememcmp()where the sign of the return value indicates sort order. We had a bug in the wireless code due to a blind memcmp replacement because of this.A cursory look suggests that the wireless bug was the only one due to this semantic difference.
Signed-off-by Johannes Berg <[email protected]>--- include/linux/etherdevice.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
Quoted 6 lines
Quoted 2 lines
The right way to avoid this kind of problems is to change these functions to return a bool
Well, I guess so, but that'd be a weird thing for a
compare_function... should probably be namedequal_... then, but I'm not really able to do such a huge change on the first day after my vacation :-)
It's true the name could be improved, but changing the name is quite a large undertaking even with automated scripts.
Even the bool change is slightly painful, since all of the explicit tests against integers (%99.999 of these are in wireless BTW :-) would need to be adjusted.
For now, I'll just apply Johannes's comment fix.
Quoted 21 lines · David Miller
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Neither
compare_ether_addr()norcompare_ether_addr_64bits()(as it can fall back to the former) have comparison semantics likememcmp()where the sign of the return value indicates sort order. We had a bug in the wireless code due to a blind memcmp replacement because of this.A cursory look suggests that the wireless bug was the only one due to this semantic difference.
Signed-off-by Johannes Berg <[email protected]>--- include/linux/etherdevice.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)Quoted 6 lines
Quoted 2 lines
The right way to avoid this kind of problems is to change these functions to return a bool
Well, I guess so, but that'd be a weird thing for a
compare_function... should probably be namedequal_... then, but I'm not really able to do such a huge change on the first day after my vacation :-)
Quoted 6 lines
It's true the name could be improved, but changing the name is quite a large undertaking even with automated scripts.
Even the bool change is slightly painful, since all of the explicit tests against integers (%99.999 of these are in wireless BTW :-) would need to be adjusted.
I suppose I could fix those first and then later change the type, but I think having a "compare_ether_addr" function that returns *false* when they *match* would be rather confusing. I'd rather have "equal_ether_addr()" that returns *true* when they match.
I guess we could introduce equal_ether_addr() though and slowly convert, keeping compare_ether_addr() as a sort of wrapper around it.
johannes
David Miller
maintainerFrom: Johannes Berg <[email protected]>
Date: Tue, 08 May 2012 07:25:44 +0200
Quoted 7 lines
I suppose I could fix those first and then later change the type, but I think having a "
compare_ether_addr" function that returns *false* when they *match* would be rather confusing. I'd rather have "equal_ether_addr()" that returns *true* when they match.I guess we could introduce
equal_ether_addr()though and slowly convert, keepingcompare_ether_addr()as a sort of wrapper around it.
Indeed, this is one way to proceed.
Quoted 8 lines · David Miller
From: Johannes Berg <[email protected]>
Date: Tue, 08 May 2012 07:25:44 +0200Quoted 5 lines
I suppose I could fix those first and then later change the type, but I think having a "
compare_ether_addr" function that returns *false* when they *match* would be rather confusing. I'd rather have "equal_ether_addr()" that returns *true* when they match.
Quoted 4 lines
Quoted 2 lines
I guess we could introduce
equal_ether_addr()though and slowly convert, keepingcompare_ether_addr()as a sort of wrapper around it.Indeed, this is one way to proceed.
perhaps is_equal_ether_addr or is_same_ether_addr instead?
David Miller
maintainerFrom: Joe Perches <[email protected]>
Date: Mon, 07 May 2012 23:35:36 -0700
Quoted 9 lines · David Miller
Quoted 8 lines
From: Johannes Berg <[email protected]>
Date: Tue, 08 May 2012 07:25:44 +0200Quoted 5 lines
I suppose I could fix those first and then later change the type, but I think having a "
compare_ether_addr" function that returns *false* when they *match* would be rather confusing. I'd rather have "equal_ether_addr()" that returns *true* when they match.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I guess we could introduce
equal_ether_addr()though and slowly convert, keepingcompare_ether_addr()as a sort of wrapper around it.Indeed, this is one way to proceed.
perhaps
is_equal_ether_addroris_same_ether_addrinstead?
Hmmm, my first choice would have been "eth_addr_equal()"
Quoted 12 lines · David Miller
From: Joe Perches <[email protected]>
Date: Mon, 07 May 2012 23:35:36 -0700Quoted 9 lines · David Miller
Quoted 8 lines
From: Johannes Berg <[email protected]>
Date: Tue, 08 May 2012 07:25:44 +0200Quoted 5 lines
I suppose I could fix those first and then later change the type, but I think having a "
compare_ether_addr" function that returns *false* when they *match* would be rather confusing. I'd rather have "equal_ether_addr()" that returns *true* when they match.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I guess we could introduce
equal_ether_addr()though and slowly convert, keepingcompare_ether_addr()as a sort of wrapper around it.Indeed, this is one way to proceed.
perhaps
is_equal_ether_addroris_same_ether_addrinstead?Hmmm, my first choice would have been "
eth_addr_equal()"
Perhaps ether_addr_equal for some API naming semi-consistency.
$ grep "\bint.*is_.*ether_addr" include/linux/etherdevice.h static inline int is_zero_ether_addr(const u8 *addr) static inline int is_multicast_ether_addr(const u8 *addr) static inline int is_local_ether_addr(const u8 *addr) static inline int is_broadcast_ether_addr(const u8 *addr) static inline int is_unicast_ether_addr(const u8 *addr) static inline int is_valid_ether_addr(const u8 *addr)
Perhaps all of these should be bool too
(patch in a separate email)
PATCH 1/1 etherdev.h: Convert int is_<foo>_ether_addr to bool
In reply to David MillerMake the return value explicitly true or false.
---
I grepped through the uses, there are a couple of
tests that store to an int that are unaffected.
There are also a couple of senseless tests of
is_broadcast_ether_addr() || is_multicast_ether_addr()
in staging that could be improved.
include/linux/etherdevice.h | 12 ++++++------ 1 files changed, 6 insertions(+), 6 deletions(-)
include/linux/etherdevice.h +6 -6
@@ -59,7 +59,7 @@ extern struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,* * Return true if the address is all zeroes. */ -static inline int is_zero_ether_addr (const u8 *addr) +static inline bool is_zero_ether_addr (const u8 *addr) {return !(addr[0 ] | addr[1 ] | addr[2 ] | addr[3 ] | addr[4 ] | addr[5 ]); }
@@ -71,7 +71,7 @@ static inline int is_zero_ether_addr(const u8 *addr)* Return true if the address is a multicast address. * By definition the broadcast address is also a multicast address. */ -static inline int is_multicast_ether_addr (const u8 *addr) +static inline bool is_multicast_ether_addr (const u8 *addr) {return 0x01 & addr[0 ]; }
@@ -82,7 +82,7 @@ static inline int is_multicast_ether_addr(const u8 *addr)* * Return true if the address is a local address. */ -static inline int is_local_ether_addr (const u8 *addr) +static inline bool is_local_ether_addr (const u8 *addr) {return 0x02 & addr[0 ]; }
@@ -93,7 +93,7 @@ static inline int is_local_ether_addr(const u8 *addr)* * Return true if the address is the broadcast address. */ -static inline int is_broadcast_ether_addr (const u8 *addr) +static inline bool is_broadcast_ether_addr (const u8 *addr) {return (addr[0 ] & addr[1 ] & addr[2 ] & addr[3 ] & addr[4 ] & addr[5 ]) ==0xff ; }
@@ -104,7 +104,7 @@ static inline int is_broadcast_ether_addr(const u8 *addr)* * Return true if the address is a unicast address. */ -static inline int is_unicast_ether_addr (const u8 *addr) +static inline bool is_unicast_ether_addr (const u8 *addr) {return !is_multicast_ether_addr (addr); }
@@ -118,7 +118,7 @@ static inline int is_unicast_ether_addr(const u8 *addr)* * Return true if the address is valid. */ -static inline int is_valid_ether_addr (const u8 *addr) +static inline bool is_valid_ether_addr (const u8 *addr) {/* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to * explicitly check for it here. */
David Miller
maintainerRe: [PATCH] etherdev.h: Convert int is_<foo>_ether_addr to bool
From: Joe Perches <[email protected]>
Date: Tue, 08 May 2012 09:44:40 -0700
Quoted 3 lines
Make the return value explicitly true or false.
Signed-off-by Joe Perches <[email protected]>
Looks good, applied, thanks Joe.