xref: /openbmc/linux/include/linux/etherdevice.h (revision ca958a1b)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  NET  is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		Definitions for the Ethernet handlers.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Version:	@(#)eth.h	1.0.4	05/13/93
101da177e4SLinus Torvalds  *
1102c30a84SJesper Juhl  * Authors:	Ross Biro
121da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *		Relocated to include/linux where it belongs by Alan Cox
151da177e4SLinus Torvalds  *							<gw4pts@gw4pts.ampr.org>
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds #ifndef _LINUX_ETHERDEVICE_H
181da177e4SLinus Torvalds #define _LINUX_ETHERDEVICE_H
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/if_ether.h>
21bcd61272SArnd Bergmann #include <linux/netdevice.h>
221da177e4SLinus Torvalds #include <linux/random.h>
23b86cd700SHeiner Kallweit #include <linux/crc32.h>
241f87e235SEric Dumazet #include <asm/unaligned.h>
250d74c42fSJoe Perches #include <asm/bitsperlong.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #ifdef __KERNEL__
28c7f5d105SDavid S. Miller struct device;
29433baf07SJakub Kicinski struct fwnode_handle;
30433baf07SJakub Kicinski 
31c7f5d105SDavid S. Miller int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr);
32ba882580SJakub Kicinski int platform_get_ethdev_address(struct device *dev, struct net_device *netdev);
33e728789cSMathieu Malaterre unsigned char *arch_get_platform_mac_address(void);
340e839df9SBartosz Golaszewski int nvmem_get_mac_address(struct device *dev, void *addrbuf);
350a14501eSJakub Kicinski int device_get_mac_address(struct device *dev, char *addr);
36d9eb4490SJakub Kicinski int device_get_ethdev_address(struct device *dev, struct net_device *netdev);
370a14501eSJakub Kicinski int fwnode_get_mac_address(struct fwnode_handle *fwnode, char *addr);
38433baf07SJakub Kicinski 
3959753ce8SAlexander Lobakin u32 eth_get_headlen(const struct net_device *dev, const void *data, u32 len);
40f629d208SJoe Perches __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
413b04dddeSStephen Hemminger extern const struct header_ops eth_header_ops;
423b04dddeSStephen Hemminger 
43f629d208SJoe Perches int eth_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
443b04dddeSStephen Hemminger 	       const void *daddr, const void *saddr, unsigned len);
45f629d208SJoe Perches int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr);
46f629d208SJoe Perches int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh,
47f629d208SJoe Perches 		     __be16 type);
48f629d208SJoe Perches void eth_header_cache_update(struct hh_cache *hh, const struct net_device *dev,
493b04dddeSStephen Hemminger 			     const unsigned char *haddr);
50ace53b2eSMaxim Mikityanskiy __be16 eth_header_parse_protocol(const struct sk_buff *skb);
51f629d208SJoe Perches int eth_prepare_mac_addr_change(struct net_device *dev, void *p);
52f629d208SJoe Perches void eth_commit_mac_addr_change(struct net_device *dev, void *p);
53f629d208SJoe Perches int eth_mac_addr(struct net_device *dev, void *p);
54f629d208SJoe Perches int eth_validate_addr(struct net_device *dev);
55ccad637bSStephen Hemminger 
56f629d208SJoe Perches struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,
5736909ea4STom Herbert 					    unsigned int rxqs);
58f25f4e44SPeter P Waskiewicz Jr #define alloc_etherdev(sizeof_priv) alloc_etherdev_mq(sizeof_priv, 1)
5936909ea4STom Herbert #define alloc_etherdev_mq(sizeof_priv, count) alloc_etherdev_mqs(sizeof_priv, count, count)
601da177e4SLinus Torvalds 
6140be0ddaSRafał Miłecki struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv,
6240be0ddaSRafał Miłecki 					   unsigned int txqs,
6340be0ddaSRafał Miłecki 					   unsigned int rxqs);
6440be0ddaSRafał Miłecki #define devm_alloc_etherdev(dev, sizeof_priv) devm_alloc_etherdev_mqs(dev, sizeof_priv, 1, 1)
6540be0ddaSRafał Miłecki 
66d4546c25SDavid Miller struct sk_buff *eth_gro_receive(struct list_head *head, struct sk_buff *skb);
679b174d88SJesse Gross int eth_gro_complete(struct sk_buff *skb, int nhoff);
689b174d88SJesse Gross 
69b3343a2aSJohn Fastabend /* Reserved Ethernet Addresses per IEEE 802.1Q */
702bc80059SBen Hutchings static const u8 eth_reserved_addr_base[ETH_ALEN] __aligned(2) =
712bc80059SBen Hutchings { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
7205428729SEgil Hjelmeland #define eth_stp_addr eth_reserved_addr_base
73b3343a2aSJohn Fastabend 
74b3343a2aSJohn Fastabend /**
7546acc460SBen Hutchings  * is_link_local_ether_addr - Determine if given Ethernet address is link-local
76b3343a2aSJohn Fastabend  * @addr: Pointer to a six-byte array containing the Ethernet address
77b3343a2aSJohn Fastabend  *
78b3343a2aSJohn Fastabend  * Return true if address is link local reserved addr (01:80:c2:00:00:0X) per
79b3343a2aSJohn Fastabend  * IEEE 802.1Q 8.6.3 Frame filtering.
802c722fe1SJoe Perches  *
812c722fe1SJoe Perches  * Please note: addr must be aligned to u16.
82b3343a2aSJohn Fastabend  */
is_link_local_ether_addr(const u8 * addr)8346acc460SBen Hutchings static inline bool is_link_local_ether_addr(const u8 *addr)
84b3343a2aSJohn Fastabend {
8546acc460SBen Hutchings 	__be16 *a = (__be16 *)addr;
862bc80059SBen Hutchings 	static const __be16 *b = (const __be16 *)eth_reserved_addr_base;
87b3343a2aSJohn Fastabend 	static const __be16 m = cpu_to_be16(0xfff0);
88b3343a2aSJohn Fastabend 
892c722fe1SJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
902c722fe1SJoe Perches 	return (((*(const u32 *)addr) ^ (*(const u32 *)b)) |
91fbaff3efSJesse Brandeburg 		(__force int)((a[2] ^ b[2]) & m)) == 0;
922c722fe1SJoe Perches #else
93b3343a2aSJohn Fastabend 	return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | ((a[2] ^ b[2]) & m)) == 0;
942c722fe1SJoe Perches #endif
95b3343a2aSJohn Fastabend }
96b3343a2aSJohn Fastabend 
971da177e4SLinus Torvalds /**
98c2da8acaSStephen Hemminger  * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
99c2da8acaSStephen Hemminger  * @addr: Pointer to a six-byte array containing the Ethernet address
100c2da8acaSStephen Hemminger  *
101c2da8acaSStephen Hemminger  * Return true if the address is all zeroes.
1022c722fe1SJoe Perches  *
1032c722fe1SJoe Perches  * Please note: addr must be aligned to u16.
1041da177e4SLinus Torvalds  */
is_zero_ether_addr(const u8 * addr)105b44907e6SJoe Perches static inline bool is_zero_ether_addr(const u8 *addr)
1061da177e4SLinus Torvalds {
1072c722fe1SJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1082c722fe1SJoe Perches 	return ((*(const u32 *)addr) | (*(const u16 *)(addr + 4))) == 0;
1092c722fe1SJoe Perches #else
1102c722fe1SJoe Perches 	return (*(const u16 *)(addr + 0) |
1112c722fe1SJoe Perches 		*(const u16 *)(addr + 2) |
1122c722fe1SJoe Perches 		*(const u16 *)(addr + 4)) == 0;
1132c722fe1SJoe Perches #endif
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /**
117c2da8acaSStephen Hemminger  * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
11879165121SMichael Ellerman  * @addr: Pointer to a six-byte array containing the Ethernet address
11979165121SMichael Ellerman  *
12079165121SMichael Ellerman  * Return true if the address is a multicast address.
12188df8ef5SStephen Hemminger  * By definition the broadcast address is also a multicast address.
12279165121SMichael Ellerman  */
is_multicast_ether_addr(const u8 * addr)123b44907e6SJoe Perches static inline bool is_multicast_ether_addr(const u8 *addr)
12479165121SMichael Ellerman {
125d54385ceSAlexander Duyck #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
126d54385ceSAlexander Duyck 	u32 a = *(const u32 *)addr;
127d54385ceSAlexander Duyck #else
128d54385ceSAlexander Duyck 	u16 a = *(const u16 *)addr;
129d54385ceSAlexander Duyck #endif
130d54385ceSAlexander Duyck #ifdef __BIG_ENDIAN
131d54385ceSAlexander Duyck 	return 0x01 & (a >> ((sizeof(a) * 8) - 8));
132d54385ceSAlexander Duyck #else
133d54385ceSAlexander Duyck 	return 0x01 & a;
134d54385ceSAlexander Duyck #endif
135d54385ceSAlexander Duyck }
136d54385ceSAlexander Duyck 
is_multicast_ether_addr_64bits(const u8 * addr)1372618a0daSKees Cook static inline bool is_multicast_ether_addr_64bits(const u8 *addr)
138d54385ceSAlexander Duyck {
139d54385ceSAlexander Duyck #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
140d54385ceSAlexander Duyck #ifdef __BIG_ENDIAN
141d54385ceSAlexander Duyck 	return 0x01 & ((*(const u64 *)addr) >> 56);
142d54385ceSAlexander Duyck #else
143d54385ceSAlexander Duyck 	return 0x01 & (*(const u64 *)addr);
144d54385ceSAlexander Duyck #endif
145d54385ceSAlexander Duyck #else
146d54385ceSAlexander Duyck 	return is_multicast_ether_addr(addr);
147d54385ceSAlexander Duyck #endif
14879165121SMichael Ellerman }
14979165121SMichael Ellerman 
150c2da8acaSStephen Hemminger /**
151bc2cda1eSRandy Dunlap  * is_local_ether_addr - Determine if the Ethernet address is locally-assigned one (IEEE 802).
152e024715fSPaolo 'Blaisorblade' Giarrusso  * @addr: Pointer to a six-byte array containing the Ethernet address
153e024715fSPaolo 'Blaisorblade' Giarrusso  *
154e024715fSPaolo 'Blaisorblade' Giarrusso  * Return true if the address is a local address.
155e024715fSPaolo 'Blaisorblade' Giarrusso  */
is_local_ether_addr(const u8 * addr)156b44907e6SJoe Perches static inline bool is_local_ether_addr(const u8 *addr)
157e024715fSPaolo 'Blaisorblade' Giarrusso {
158a02cec21SEric Dumazet 	return 0x02 & addr[0];
159e024715fSPaolo 'Blaisorblade' Giarrusso }
160e024715fSPaolo 'Blaisorblade' Giarrusso 
161e024715fSPaolo 'Blaisorblade' Giarrusso /**
162c2da8acaSStephen Hemminger  * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
163c2da8acaSStephen Hemminger  * @addr: Pointer to a six-byte array containing the Ethernet address
164c2da8acaSStephen Hemminger  *
165c2da8acaSStephen Hemminger  * Return true if the address is the broadcast address.
1662c722fe1SJoe Perches  *
1672c722fe1SJoe Perches  * Please note: addr must be aligned to u16.
168c2da8acaSStephen Hemminger  */
is_broadcast_ether_addr(const u8 * addr)169b44907e6SJoe Perches static inline bool is_broadcast_ether_addr(const u8 *addr)
1709bd481f8SJeff Garzik {
1712c722fe1SJoe Perches 	return (*(const u16 *)(addr + 0) &
1722c722fe1SJoe Perches 		*(const u16 *)(addr + 2) &
1732c722fe1SJoe Perches 		*(const u16 *)(addr + 4)) == 0xffff;
1749bd481f8SJeff Garzik }
1759bd481f8SJeff Garzik 
17679165121SMichael Ellerman /**
17751e7eed7STobias Klauser  * is_unicast_ether_addr - Determine if the Ethernet address is unicast
17851e7eed7STobias Klauser  * @addr: Pointer to a six-byte array containing the Ethernet address
17951e7eed7STobias Klauser  *
18051e7eed7STobias Klauser  * Return true if the address is a unicast address.
18151e7eed7STobias Klauser  */
is_unicast_ether_addr(const u8 * addr)182b44907e6SJoe Perches static inline bool is_unicast_ether_addr(const u8 *addr)
18351e7eed7STobias Klauser {
18451e7eed7STobias Klauser 	return !is_multicast_ether_addr(addr);
18551e7eed7STobias Klauser }
18651e7eed7STobias Klauser 
18751e7eed7STobias Klauser /**
1881da177e4SLinus Torvalds  * is_valid_ether_addr - Determine if the given Ethernet address is valid
1891da177e4SLinus Torvalds  * @addr: Pointer to a six-byte array containing the Ethernet address
1901da177e4SLinus Torvalds  *
1911da177e4SLinus Torvalds  * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
19279165121SMichael Ellerman  * a multicast address, and is not FF:FF:FF:FF:FF:FF.
1931da177e4SLinus Torvalds  *
1941da177e4SLinus Torvalds  * Return true if the address is valid.
1952c722fe1SJoe Perches  *
1962c722fe1SJoe Perches  * Please note: addr must be aligned to u16.
1971da177e4SLinus Torvalds  */
is_valid_ether_addr(const u8 * addr)198b44907e6SJoe Perches static inline bool is_valid_ether_addr(const u8 *addr)
1991da177e4SLinus Torvalds {
20079165121SMichael Ellerman 	/* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
20179165121SMichael Ellerman 	 * explicitly check for it here. */
20279165121SMichael Ellerman 	return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /**
2062c7a88c2SAlexander Duyck  * eth_proto_is_802_3 - Determine if a given Ethertype/length is a protocol
2072c7a88c2SAlexander Duyck  * @proto: Ethertype/length value to be tested
2082c7a88c2SAlexander Duyck  *
2092c7a88c2SAlexander Duyck  * Check that the value from the Ethertype/length field is a valid Ethertype.
2102c7a88c2SAlexander Duyck  *
2112c7a88c2SAlexander Duyck  * Return true if the valid is an 802.3 supported Ethertype.
2122c7a88c2SAlexander Duyck  */
eth_proto_is_802_3(__be16 proto)2132c7a88c2SAlexander Duyck static inline bool eth_proto_is_802_3(__be16 proto)
2142c7a88c2SAlexander Duyck {
2152c7a88c2SAlexander Duyck #ifndef __BIG_ENDIAN
2162c7a88c2SAlexander Duyck 	/* if CPU is little endian mask off bits representing LSB */
2172c7a88c2SAlexander Duyck 	proto &= htons(0xFF00);
2182c7a88c2SAlexander Duyck #endif
2192c7a88c2SAlexander Duyck 	/* cast both to u16 and compare since LSB can be ignored */
2202c7a88c2SAlexander Duyck 	return (__force u16)proto >= (__force u16)htons(ETH_P_802_3_MIN);
2212c7a88c2SAlexander Duyck }
2222c7a88c2SAlexander Duyck 
2232c7a88c2SAlexander Duyck /**
2240a4dd594SJoe Perches  * eth_random_addr - Generate software assigned random Ethernet address
2251da177e4SLinus Torvalds  * @addr: Pointer to a six-byte array containing the Ethernet address
2261da177e4SLinus Torvalds  *
2271da177e4SLinus Torvalds  * Generate a random Ethernet address (MAC) that is not multicast
2281da177e4SLinus Torvalds  * and has the local assigned bit set.
2291da177e4SLinus Torvalds  */
eth_random_addr(u8 * addr)2300a4dd594SJoe Perches static inline void eth_random_addr(u8 *addr)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	get_random_bytes(addr, ETH_ALEN);
2331da177e4SLinus Torvalds 	addr[0] &= 0xfe;	/* clear multicast bit */
2341da177e4SLinus Torvalds 	addr[0] |= 0x02;	/* set local assignment bit (IEEE802) */
2351da177e4SLinus Torvalds }
236360ac8e2SStephen Hemminger 
237360ac8e2SStephen Hemminger /**
238ad7eee98SJohannes Berg  * eth_broadcast_addr - Assign broadcast address
239ad7eee98SJohannes Berg  * @addr: Pointer to a six-byte array containing the Ethernet address
240ad7eee98SJohannes Berg  *
241ad7eee98SJohannes Berg  * Assign the broadcast address to the given address array.
242ad7eee98SJohannes Berg  */
eth_broadcast_addr(u8 * addr)243ad7eee98SJohannes Berg static inline void eth_broadcast_addr(u8 *addr)
244ad7eee98SJohannes Berg {
245ad7eee98SJohannes Berg 	memset(addr, 0xff, ETH_ALEN);
246ad7eee98SJohannes Berg }
247ad7eee98SJohannes Berg 
248ad7eee98SJohannes Berg /**
2496d57e907SDuan Jiong  * eth_zero_addr - Assign zero address
2506d57e907SDuan Jiong  * @addr: Pointer to a six-byte array containing the Ethernet address
2516d57e907SDuan Jiong  *
2526d57e907SDuan Jiong  * Assign the zero address to the given address array.
2536d57e907SDuan Jiong  */
eth_zero_addr(u8 * addr)2546d57e907SDuan Jiong static inline void eth_zero_addr(u8 *addr)
2556d57e907SDuan Jiong {
2566d57e907SDuan Jiong 	memset(addr, 0x00, ETH_ALEN);
2576d57e907SDuan Jiong }
2586d57e907SDuan Jiong 
2596d57e907SDuan Jiong /**
2601a0d6ae5SDanny Kukawka  * eth_hw_addr_random - Generate software assigned random Ethernet and
2611a0d6ae5SDanny Kukawka  * set device flag
262c1f79426SStefan Assmann  * @dev: pointer to net_device structure
263c1f79426SStefan Assmann  *
2641a0d6ae5SDanny Kukawka  * Generate a random Ethernet address (MAC) to be used by a net device
2651a0d6ae5SDanny Kukawka  * and set addr_assign_type so the state can be read by sysfs and be
2661a0d6ae5SDanny Kukawka  * used by userspace.
267c1f79426SStefan Assmann  */
eth_hw_addr_random(struct net_device * dev)2681a0d6ae5SDanny Kukawka static inline void eth_hw_addr_random(struct net_device *dev)
269c1f79426SStefan Assmann {
27054f2d8d6SJakub Kicinski 	u8 addr[ETH_ALEN];
27154f2d8d6SJakub Kicinski 
27254f2d8d6SJakub Kicinski 	eth_random_addr(addr);
27354f2d8d6SJakub Kicinski 	__dev_addr_set(dev, addr, ETH_ALEN);
274e41b2d7fSJiri Pirko 	dev->addr_assign_type = NET_ADDR_RANDOM;
275c1f79426SStefan Assmann }
276c1f79426SStefan Assmann 
277c1f79426SStefan Assmann /**
278b86cd700SHeiner Kallweit  * eth_hw_addr_crc - Calculate CRC from netdev_hw_addr
279b86cd700SHeiner Kallweit  * @ha: pointer to hardware address
280b86cd700SHeiner Kallweit  *
281b86cd700SHeiner Kallweit  * Calculate CRC from a hardware address as basis for filter hashes.
282b86cd700SHeiner Kallweit  */
eth_hw_addr_crc(struct netdev_hw_addr * ha)283b86cd700SHeiner Kallweit static inline u32 eth_hw_addr_crc(struct netdev_hw_addr *ha)
284b86cd700SHeiner Kallweit {
285b86cd700SHeiner Kallweit 	return ether_crc(ETH_ALEN, ha->addr);
286b86cd700SHeiner Kallweit }
287b86cd700SHeiner Kallweit 
288b86cd700SHeiner Kallweit /**
289286ab723SJoe Perches  * ether_addr_copy - Copy an Ethernet address
290286ab723SJoe Perches  * @dst: Pointer to a six-byte array Ethernet address destination
291286ab723SJoe Perches  * @src: Pointer to a six-byte array Ethernet address source
292286ab723SJoe Perches  *
293286ab723SJoe Perches  * Please note: dst & src must both be aligned to u16.
294286ab723SJoe Perches  */
ether_addr_copy(u8 * dst,const u8 * src)295286ab723SJoe Perches static inline void ether_addr_copy(u8 *dst, const u8 *src)
296286ab723SJoe Perches {
297286ab723SJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
298286ab723SJoe Perches 	*(u32 *)dst = *(const u32 *)src;
299286ab723SJoe Perches 	*(u16 *)(dst + 4) = *(const u16 *)(src + 4);
300286ab723SJoe Perches #else
301286ab723SJoe Perches 	u16 *a = (u16 *)dst;
302286ab723SJoe Perches 	const u16 *b = (const u16 *)src;
303286ab723SJoe Perches 
304286ab723SJoe Perches 	a[0] = b[0];
305286ab723SJoe Perches 	a[1] = b[1];
306286ab723SJoe Perches 	a[2] = b[2];
307286ab723SJoe Perches #endif
308286ab723SJoe Perches }
309286ab723SJoe Perches 
310286ab723SJoe Perches /**
31148eab831SJakub Kicinski  * eth_hw_addr_set - Assign Ethernet address to a net_device
31248eab831SJakub Kicinski  * @dev: pointer to net_device structure
31348eab831SJakub Kicinski  * @addr: address to assign
31448eab831SJakub Kicinski  *
31548eab831SJakub Kicinski  * Assign given address to the net_device, addr_assign_type is not changed.
31648eab831SJakub Kicinski  */
eth_hw_addr_set(struct net_device * dev,const u8 * addr)31748eab831SJakub Kicinski static inline void eth_hw_addr_set(struct net_device *dev, const u8 *addr)
31848eab831SJakub Kicinski {
3193f6cffb8SJakub Kicinski 	__dev_addr_set(dev, addr, ETH_ALEN);
32048eab831SJakub Kicinski }
32148eab831SJakub Kicinski 
32248eab831SJakub Kicinski /**
32383a093b4SBjørn Mork  * eth_hw_addr_inherit - Copy dev_addr from another net_device
32483a093b4SBjørn Mork  * @dst: pointer to net_device to copy dev_addr to
32583a093b4SBjørn Mork  * @src: pointer to net_device to copy dev_addr from
32683a093b4SBjørn Mork  *
32783a093b4SBjørn Mork  * Copy the Ethernet address from one net_device to another along with
32883a093b4SBjørn Mork  * the address attributes (addr_assign_type).
32983a093b4SBjørn Mork  */
eth_hw_addr_inherit(struct net_device * dst,struct net_device * src)33083a093b4SBjørn Mork static inline void eth_hw_addr_inherit(struct net_device *dst,
33183a093b4SBjørn Mork 				       struct net_device *src)
33283a093b4SBjørn Mork {
33383a093b4SBjørn Mork 	dst->addr_assign_type = src->addr_assign_type;
334f3956ebbSJakub Kicinski 	eth_hw_addr_set(dst, src->dev_addr);
33583a093b4SBjørn Mork }
33683a093b4SBjørn Mork 
33783a093b4SBjørn Mork /**
338a599b0f5SJoe Perches  * ether_addr_equal - Compare two Ethernet addresses
339a599b0f5SJoe Perches  * @addr1: Pointer to a six-byte array containing the Ethernet address
340a599b0f5SJoe Perches  * @addr2: Pointer other six-byte array containing the Ethernet address
341a599b0f5SJoe Perches  *
342048b899cSstephen hemminger  * Compare two Ethernet addresses, returns true if equal
3430d74c42fSJoe Perches  *
3440d74c42fSJoe Perches  * Please note: addr1 & addr2 must both be aligned to u16.
345a599b0f5SJoe Perches  */
ether_addr_equal(const u8 * addr1,const u8 * addr2)346a599b0f5SJoe Perches static inline bool ether_addr_equal(const u8 *addr1, const u8 *addr2)
347a599b0f5SJoe Perches {
3480d74c42fSJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
3490d74c42fSJoe Perches 	u32 fold = ((*(const u32 *)addr1) ^ (*(const u32 *)addr2)) |
3500d74c42fSJoe Perches 		   ((*(const u16 *)(addr1 + 4)) ^ (*(const u16 *)(addr2 + 4)));
351a599b0f5SJoe Perches 
3520d74c42fSJoe Perches 	return fold == 0;
3531f87e235SEric Dumazet #else
3540d74c42fSJoe Perches 	const u16 *a = (const u16 *)addr1;
3550d74c42fSJoe Perches 	const u16 *b = (const u16 *)addr2;
3560d74c42fSJoe Perches 
3570d74c42fSJoe Perches 	return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0;
3581f87e235SEric Dumazet #endif
3591f87e235SEric Dumazet }
3601f87e235SEric Dumazet 
3611f87e235SEric Dumazet /**
362baf523c9SJoe Perches  * ether_addr_equal_64bits - Compare two Ethernet addresses
363baf523c9SJoe Perches  * @addr1: Pointer to an array of 8 bytes
364baf523c9SJoe Perches  * @addr2: Pointer to an other array of 8 bytes
365baf523c9SJoe Perches  *
366048b899cSstephen hemminger  * Compare two Ethernet addresses, returns true if equal, false otherwise.
367baf523c9SJoe Perches  *
368baf523c9SJoe Perches  * The function doesn't need any conditional branches and possibly uses
369baf523c9SJoe Perches  * word memory accesses on CPU allowing cheap unaligned memory reads.
370048b899cSstephen hemminger  * arrays = { byte1, byte2, byte3, byte4, byte5, byte6, pad1, pad2 }
371baf523c9SJoe Perches  *
372048b899cSstephen hemminger  * Please note that alignment of addr1 & addr2 are only guaranteed to be 16 bits.
373baf523c9SJoe Perches  */
374baf523c9SJoe Perches 
ether_addr_equal_64bits(const u8 * addr1,const u8 * addr2)3752618a0daSKees Cook static inline bool ether_addr_equal_64bits(const u8 *addr1, const u8 *addr2)
376baf523c9SJoe Perches {
3770d74c42fSJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
3780d74c42fSJoe Perches 	u64 fold = (*(const u64 *)addr1) ^ (*(const u64 *)addr2);
379e550ba1aSJoe Perches 
3800d74c42fSJoe Perches #ifdef __BIG_ENDIAN
3810d74c42fSJoe Perches 	return (fold >> 16) == 0;
3820d74c42fSJoe Perches #else
3830d74c42fSJoe Perches 	return (fold << 16) == 0;
3840d74c42fSJoe Perches #endif
385e550ba1aSJoe Perches #else
386e550ba1aSJoe Perches 	return ether_addr_equal(addr1, addr2);
387e550ba1aSJoe Perches #endif
388baf523c9SJoe Perches }
389baf523c9SJoe Perches 
390baf523c9SJoe Perches /**
39173eaef87SJoe Perches  * ether_addr_equal_unaligned - Compare two not u16 aligned Ethernet addresses
39273eaef87SJoe Perches  * @addr1: Pointer to a six-byte array containing the Ethernet address
39373eaef87SJoe Perches  * @addr2: Pointer other six-byte array containing the Ethernet address
39473eaef87SJoe Perches  *
39573eaef87SJoe Perches  * Compare two Ethernet addresses, returns true if equal
39673eaef87SJoe Perches  *
39773eaef87SJoe Perches  * Please note: Use only when any Ethernet address may not be u16 aligned.
39873eaef87SJoe Perches  */
ether_addr_equal_unaligned(const u8 * addr1,const u8 * addr2)39973eaef87SJoe Perches static inline bool ether_addr_equal_unaligned(const u8 *addr1, const u8 *addr2)
40073eaef87SJoe Perches {
40173eaef87SJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
40273eaef87SJoe Perches 	return ether_addr_equal(addr1, addr2);
40373eaef87SJoe Perches #else
40473eaef87SJoe Perches 	return memcmp(addr1, addr2, ETH_ALEN) == 0;
40573eaef87SJoe Perches #endif
40673eaef87SJoe Perches }
40773eaef87SJoe Perches 
40873eaef87SJoe Perches /**
4094ae89ad9SJoe Perches  * ether_addr_equal_masked - Compare two Ethernet addresses with a mask
4104ae89ad9SJoe Perches  * @addr1: Pointer to a six-byte array containing the 1st Ethernet address
4114ae89ad9SJoe Perches  * @addr2: Pointer to a six-byte array containing the 2nd Ethernet address
4124ae89ad9SJoe Perches  * @mask: Pointer to a six-byte array containing the Ethernet address bitmask
4134ae89ad9SJoe Perches  *
4144ae89ad9SJoe Perches  * Compare two Ethernet addresses with a mask, returns true if for every bit
4154ae89ad9SJoe Perches  * set in the bitmask the equivalent bits in the ethernet addresses are equal.
4164ae89ad9SJoe Perches  * Using a mask with all bits set is a slower ether_addr_equal.
4174ae89ad9SJoe Perches  */
ether_addr_equal_masked(const u8 * addr1,const u8 * addr2,const u8 * mask)4184ae89ad9SJoe Perches static inline bool ether_addr_equal_masked(const u8 *addr1, const u8 *addr2,
4194ae89ad9SJoe Perches 					   const u8 *mask)
4204ae89ad9SJoe Perches {
4214ae89ad9SJoe Perches 	int i;
4224ae89ad9SJoe Perches 
4234ae89ad9SJoe Perches 	for (i = 0; i < ETH_ALEN; i++) {
4244ae89ad9SJoe Perches 		if ((addr1[i] ^ addr2[i]) & mask[i])
4254ae89ad9SJoe Perches 			return false;
4264ae89ad9SJoe Perches 	}
4274ae89ad9SJoe Perches 
4284ae89ad9SJoe Perches 	return true;
4294ae89ad9SJoe Perches }
4304ae89ad9SJoe Perches 
ether_addr_is_ipv4_mcast(const u8 * addr)431e8013f8eSCasper Andersson static inline bool ether_addr_is_ipv4_mcast(const u8 *addr)
432e8013f8eSCasper Andersson {
433e8013f8eSCasper Andersson 	u8 base[ETH_ALEN] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
434e8013f8eSCasper Andersson 	u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
435e8013f8eSCasper Andersson 
436e8013f8eSCasper Andersson 	return ether_addr_equal_masked(addr, base, mask);
437e8013f8eSCasper Andersson }
438e8013f8eSCasper Andersson 
ether_addr_is_ipv6_mcast(const u8 * addr)439e8013f8eSCasper Andersson static inline bool ether_addr_is_ipv6_mcast(const u8 *addr)
440e8013f8eSCasper Andersson {
441e8013f8eSCasper Andersson 	u8 base[ETH_ALEN] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
442e8013f8eSCasper Andersson 	u8 mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
443e8013f8eSCasper Andersson 
444e8013f8eSCasper Andersson 	return ether_addr_equal_masked(addr, base, mask);
445e8013f8eSCasper Andersson }
446e8013f8eSCasper Andersson 
ether_addr_is_ip_mcast(const u8 * addr)447e8013f8eSCasper Andersson static inline bool ether_addr_is_ip_mcast(const u8 *addr)
448e8013f8eSCasper Andersson {
449e8013f8eSCasper Andersson 	return ether_addr_is_ipv4_mcast(addr) ||
450e8013f8eSCasper Andersson 		ether_addr_is_ipv6_mcast(addr);
451e8013f8eSCasper Andersson }
452e8013f8eSCasper Andersson 
4534ae89ad9SJoe Perches /**
45459527581SAndrew Lunn  * ether_addr_to_u64 - Convert an Ethernet address into a u64 value.
45559527581SAndrew Lunn  * @addr: Pointer to a six-byte array containing the Ethernet address
45659527581SAndrew Lunn  *
45759527581SAndrew Lunn  * Return a u64 value of the address
45859527581SAndrew Lunn  */
ether_addr_to_u64(const u8 * addr)45959527581SAndrew Lunn static inline u64 ether_addr_to_u64(const u8 *addr)
46059527581SAndrew Lunn {
46159527581SAndrew Lunn 	u64 u = 0;
46259527581SAndrew Lunn 	int i;
46359527581SAndrew Lunn 
46459527581SAndrew Lunn 	for (i = 0; i < ETH_ALEN; i++)
46559527581SAndrew Lunn 		u = u << 8 | addr[i];
46659527581SAndrew Lunn 
46759527581SAndrew Lunn 	return u;
46859527581SAndrew Lunn }
46959527581SAndrew Lunn 
47059527581SAndrew Lunn /**
47159527581SAndrew Lunn  * u64_to_ether_addr - Convert a u64 to an Ethernet address.
47259527581SAndrew Lunn  * @u: u64 to convert to an Ethernet MAC address
47359527581SAndrew Lunn  * @addr: Pointer to a six-byte array to contain the Ethernet address
47459527581SAndrew Lunn  */
u64_to_ether_addr(u64 u,u8 * addr)47559527581SAndrew Lunn static inline void u64_to_ether_addr(u64 u, u8 *addr)
47659527581SAndrew Lunn {
47759527581SAndrew Lunn 	int i;
47859527581SAndrew Lunn 
47959527581SAndrew Lunn 	for (i = ETH_ALEN - 1; i >= 0; i--) {
48059527581SAndrew Lunn 		addr[i] = u & 0xff;
48159527581SAndrew Lunn 		u = u >> 8;
48259527581SAndrew Lunn 	}
48359527581SAndrew Lunn }
48459527581SAndrew Lunn 
48559527581SAndrew Lunn /**
48659527581SAndrew Lunn  * eth_addr_dec - Decrement the given MAC address
48759527581SAndrew Lunn  *
48859527581SAndrew Lunn  * @addr: Pointer to a six-byte array containing Ethernet address to decrement
48959527581SAndrew Lunn  */
eth_addr_dec(u8 * addr)49059527581SAndrew Lunn static inline void eth_addr_dec(u8 *addr)
49159527581SAndrew Lunn {
49259527581SAndrew Lunn 	u64 u = ether_addr_to_u64(addr);
49359527581SAndrew Lunn 
49459527581SAndrew Lunn 	u--;
49559527581SAndrew Lunn 	u64_to_ether_addr(u, addr);
49659527581SAndrew Lunn }
49759527581SAndrew Lunn 
49859527581SAndrew Lunn /**
4991c5c12eeSTao Ren  * eth_addr_inc() - Increment the given MAC address.
5001c5c12eeSTao Ren  * @addr: Pointer to a six-byte array containing Ethernet address to increment.
5011c5c12eeSTao Ren  */
eth_addr_inc(u8 * addr)5021c5c12eeSTao Ren static inline void eth_addr_inc(u8 *addr)
5031c5c12eeSTao Ren {
5041c5c12eeSTao Ren 	u64 u = ether_addr_to_u64(addr);
5051c5c12eeSTao Ren 
5061c5c12eeSTao Ren 	u++;
5071c5c12eeSTao Ren 	u64_to_ether_addr(u, addr);
5081c5c12eeSTao Ren }
5091c5c12eeSTao Ren 
5101c5c12eeSTao Ren /**
5117390609bSMichael Walle  * eth_addr_add() - Add (or subtract) an offset to/from the given MAC address.
5127390609bSMichael Walle  *
5137390609bSMichael Walle  * @offset: Offset to add.
5147390609bSMichael Walle  * @addr: Pointer to a six-byte array containing Ethernet address to increment.
5157390609bSMichael Walle  */
eth_addr_add(u8 * addr,long offset)5167390609bSMichael Walle static inline void eth_addr_add(u8 *addr, long offset)
5177390609bSMichael Walle {
5187390609bSMichael Walle 	u64 u = ether_addr_to_u64(addr);
5197390609bSMichael Walle 
5207390609bSMichael Walle 	u += offset;
5217390609bSMichael Walle 	u64_to_ether_addr(u, addr);
5227390609bSMichael Walle }
5237390609bSMichael Walle 
5247390609bSMichael Walle /**
525f001fde5SJiri Pirko  * is_etherdev_addr - Tell if given Ethernet address belongs to the device.
526f001fde5SJiri Pirko  * @dev: Pointer to a device structure
527f001fde5SJiri Pirko  * @addr: Pointer to a six-byte array containing the Ethernet address
528f001fde5SJiri Pirko  *
529f001fde5SJiri Pirko  * Compare passed address with all addresses of the device. Return true if the
530f001fde5SJiri Pirko  * address if one of the device addresses.
531f001fde5SJiri Pirko  *
532e550ba1aSJoe Perches  * Note that this function calls ether_addr_equal_64bits() so take care of
533f001fde5SJiri Pirko  * the right padding.
534f001fde5SJiri Pirko  */
is_etherdev_addr(const struct net_device * dev,const u8 addr[6+2])535f001fde5SJiri Pirko static inline bool is_etherdev_addr(const struct net_device *dev,
536f001fde5SJiri Pirko 				    const u8 addr[6 + 2])
537f001fde5SJiri Pirko {
538f001fde5SJiri Pirko 	struct netdev_hw_addr *ha;
539e550ba1aSJoe Perches 	bool res = false;
540f001fde5SJiri Pirko 
541f001fde5SJiri Pirko 	rcu_read_lock();
542f001fde5SJiri Pirko 	for_each_dev_addr(dev, ha) {
543e550ba1aSJoe Perches 		res = ether_addr_equal_64bits(addr, ha->addr);
544e550ba1aSJoe Perches 		if (res)
545f001fde5SJiri Pirko 			break;
546f001fde5SJiri Pirko 	}
547f001fde5SJiri Pirko 	rcu_read_unlock();
548e550ba1aSJoe Perches 	return res;
549f001fde5SJiri Pirko }
55079165121SMichael Ellerman #endif	/* __KERNEL__ */
5511da177e4SLinus Torvalds 
552aa4b9f53SHerbert Xu /**
553aa4b9f53SHerbert Xu  * compare_ether_header - Compare two Ethernet headers
554aa4b9f53SHerbert Xu  * @a: Pointer to Ethernet header
555aa4b9f53SHerbert Xu  * @b: Pointer to Ethernet header
556aa4b9f53SHerbert Xu  *
557048b899cSstephen hemminger  * Compare two Ethernet headers, returns 0 if equal.
558aa4b9f53SHerbert Xu  * This assumes that the network header (i.e., IP header) is 4-byte
559aa4b9f53SHerbert Xu  * aligned OR the platform can handle unaligned access.  This is the
560aa4b9f53SHerbert Xu  * case for all packets coming into netif_receive_skb or similar
561aa4b9f53SHerbert Xu  * entry points.
562aa4b9f53SHerbert Xu  */
563aa4b9f53SHerbert Xu 
compare_ether_header(const void * a,const void * b)56440d0802bSEric Dumazet static inline unsigned long compare_ether_header(const void *a, const void *b)
565aa4b9f53SHerbert Xu {
56640d0802bSEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
56740d0802bSEric Dumazet 	unsigned long fold;
56840d0802bSEric Dumazet 
56940d0802bSEric Dumazet 	/*
57040d0802bSEric Dumazet 	 * We want to compare 14 bytes:
57140d0802bSEric Dumazet 	 *  [a0 ... a13] ^ [b0 ... b13]
57240d0802bSEric Dumazet 	 * Use two long XOR, ORed together, with an overlap of two bytes.
57340d0802bSEric Dumazet 	 *  [a0  a1  a2  a3  a4  a5  a6  a7 ] ^ [b0  b1  b2  b3  b4  b5  b6  b7 ] |
57440d0802bSEric Dumazet 	 *  [a6  a7  a8  a9  a10 a11 a12 a13] ^ [b6  b7  b8  b9  b10 b11 b12 b13]
57540d0802bSEric Dumazet 	 * This means the [a6 a7] ^ [b6 b7] part is done two times.
57640d0802bSEric Dumazet 	*/
57740d0802bSEric Dumazet 	fold = *(unsigned long *)a ^ *(unsigned long *)b;
57840d0802bSEric Dumazet 	fold |= *(unsigned long *)(a + 6) ^ *(unsigned long *)(b + 6);
57940d0802bSEric Dumazet 	return fold;
58040d0802bSEric Dumazet #else
581aa4b9f53SHerbert Xu 	u32 *a32 = (u32 *)((u8 *)a + 2);
582aa4b9f53SHerbert Xu 	u32 *b32 = (u32 *)((u8 *)b + 2);
583aa4b9f53SHerbert Xu 
584aa4b9f53SHerbert Xu 	return (*(u16 *)a ^ *(u16 *)b) | (a32[0] ^ b32[0]) |
585aa4b9f53SHerbert Xu 	       (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]);
58640d0802bSEric Dumazet #endif
587aa4b9f53SHerbert Xu }
588aa4b9f53SHerbert Xu 
5899c0c1124SAlexander Duyck /**
590e80094a4SJakub Kicinski  * eth_hw_addr_gen - Generate and assign Ethernet address to a port
591e80094a4SJakub Kicinski  * @dev: pointer to port's net_device structure
592e80094a4SJakub Kicinski  * @base_addr: base Ethernet address
593e80094a4SJakub Kicinski  * @id: offset to add to the base address
594e80094a4SJakub Kicinski  *
595e80094a4SJakub Kicinski  * Generate a MAC address using a base address and an offset and assign it
596e80094a4SJakub Kicinski  * to a net_device. Commonly used by switch drivers which need to compute
597e80094a4SJakub Kicinski  * addresses for all their ports. addr_assign_type is not changed.
598e80094a4SJakub Kicinski  */
eth_hw_addr_gen(struct net_device * dev,const u8 * base_addr,unsigned int id)599e80094a4SJakub Kicinski static inline void eth_hw_addr_gen(struct net_device *dev, const u8 *base_addr,
600e80094a4SJakub Kicinski 				   unsigned int id)
601e80094a4SJakub Kicinski {
602e80094a4SJakub Kicinski 	u64 u = ether_addr_to_u64(base_addr);
603e80094a4SJakub Kicinski 	u8 addr[ETH_ALEN];
604e80094a4SJakub Kicinski 
605e80094a4SJakub Kicinski 	u += id;
606e80094a4SJakub Kicinski 	u64_to_ether_addr(u, addr);
607e80094a4SJakub Kicinski 	eth_hw_addr_set(dev, addr);
608e80094a4SJakub Kicinski }
609e80094a4SJakub Kicinski 
610e80094a4SJakub Kicinski /**
611ca958a1bSRahul Rameshbabu  * eth_skb_pkt_type - Assign packet type if destination address does not match
612ca958a1bSRahul Rameshbabu  * @skb: Assigned a packet type if address does not match @dev address
613ca958a1bSRahul Rameshbabu  * @dev: Network device used to compare packet address against
614ca958a1bSRahul Rameshbabu  *
615ca958a1bSRahul Rameshbabu  * If the destination MAC address of the packet does not match the network
616ca958a1bSRahul Rameshbabu  * device address, assign an appropriate packet type.
617ca958a1bSRahul Rameshbabu  */
eth_skb_pkt_type(struct sk_buff * skb,const struct net_device * dev)618ca958a1bSRahul Rameshbabu static inline void eth_skb_pkt_type(struct sk_buff *skb,
619ca958a1bSRahul Rameshbabu 				    const struct net_device *dev)
620ca958a1bSRahul Rameshbabu {
621ca958a1bSRahul Rameshbabu 	const struct ethhdr *eth = eth_hdr(skb);
622ca958a1bSRahul Rameshbabu 
623ca958a1bSRahul Rameshbabu 	if (unlikely(!ether_addr_equal_64bits(eth->h_dest, dev->dev_addr))) {
624ca958a1bSRahul Rameshbabu 		if (unlikely(is_multicast_ether_addr_64bits(eth->h_dest))) {
625ca958a1bSRahul Rameshbabu 			if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
626ca958a1bSRahul Rameshbabu 				skb->pkt_type = PACKET_BROADCAST;
627ca958a1bSRahul Rameshbabu 			else
628ca958a1bSRahul Rameshbabu 				skb->pkt_type = PACKET_MULTICAST;
629ca958a1bSRahul Rameshbabu 		} else {
630ca958a1bSRahul Rameshbabu 			skb->pkt_type = PACKET_OTHERHOST;
631ca958a1bSRahul Rameshbabu 		}
632ca958a1bSRahul Rameshbabu 	}
633ca958a1bSRahul Rameshbabu }
634ca958a1bSRahul Rameshbabu 
635ca958a1bSRahul Rameshbabu /**
6369c0c1124SAlexander Duyck  * eth_skb_pad - Pad buffer to mininum number of octets for Ethernet frame
6379c0c1124SAlexander Duyck  * @skb: Buffer to pad
6389c0c1124SAlexander Duyck  *
6399c0c1124SAlexander Duyck  * An Ethernet frame should have a minimum size of 60 bytes.  This function
6409c0c1124SAlexander Duyck  * takes short frames and pads them with zeros up to the 60 byte limit.
6419c0c1124SAlexander Duyck  */
eth_skb_pad(struct sk_buff * skb)6429c0c1124SAlexander Duyck static inline int eth_skb_pad(struct sk_buff *skb)
6439c0c1124SAlexander Duyck {
6449c0c1124SAlexander Duyck 	return skb_put_padto(skb, ETH_ZLEN);
6459c0c1124SAlexander Duyck }
6469c0c1124SAlexander Duyck 
6471da177e4SLinus Torvalds #endif	/* _LINUX_ETHERDEVICE_H */
648