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, <[email protected]>
131da177e4SLinus Torvalds *
141da177e4SLinus Torvalds * Relocated to include/linux where it belongs by Alan Cox
151da177e4SLinus Torvalds * <[email protected]>
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>
245f60d5f6SAl Viro #include <linux/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
74e1900d7bSDiogo Ivo static const u8 eth_ipv4_mcast_addr_base[ETH_ALEN] __aligned(2) =
75e1900d7bSDiogo Ivo { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
76e1900d7bSDiogo Ivo
77e1900d7bSDiogo Ivo static const u8 eth_ipv6_mcast_addr_base[ETH_ALEN] __aligned(2) =
78e1900d7bSDiogo Ivo { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
79e1900d7bSDiogo Ivo
80b3343a2aSJohn Fastabend /**
8146acc460SBen Hutchings * is_link_local_ether_addr - Determine if given Ethernet address is link-local
82b3343a2aSJohn Fastabend * @addr: Pointer to a six-byte array containing the Ethernet address
83b3343a2aSJohn Fastabend *
84*3f330db3SJakub Kicinski * Return: true if address is link local reserved addr (01:80:c2:00:00:0X) per
85b3343a2aSJohn Fastabend * IEEE 802.1Q 8.6.3 Frame filtering.
862c722fe1SJoe Perches *
872c722fe1SJoe Perches * Please note: addr must be aligned to u16.
88b3343a2aSJohn Fastabend */
is_link_local_ether_addr(const u8 * addr)8946acc460SBen Hutchings static inline bool is_link_local_ether_addr(const u8 *addr)
90b3343a2aSJohn Fastabend {
9146acc460SBen Hutchings __be16 *a = (__be16 *)addr;
922bc80059SBen Hutchings static const __be16 *b = (const __be16 *)eth_reserved_addr_base;
93b3343a2aSJohn Fastabend static const __be16 m = cpu_to_be16(0xfff0);
94b3343a2aSJohn Fastabend
952c722fe1SJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
962c722fe1SJoe Perches return (((*(const u32 *)addr) ^ (*(const u32 *)b)) |
97fbaff3efSJesse Brandeburg (__force int)((a[2] ^ b[2]) & m)) == 0;
982c722fe1SJoe Perches #else
99b3343a2aSJohn Fastabend return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | ((a[2] ^ b[2]) & m)) == 0;
1002c722fe1SJoe Perches #endif
101b3343a2aSJohn Fastabend }
102b3343a2aSJohn Fastabend
1031da177e4SLinus Torvalds /**
104c2da8acaSStephen Hemminger * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
105c2da8acaSStephen Hemminger * @addr: Pointer to a six-byte array containing the Ethernet address
106c2da8acaSStephen Hemminger *
107*3f330db3SJakub Kicinski * Return: true if the address is all zeroes.
1082c722fe1SJoe Perches *
1092c722fe1SJoe Perches * Please note: addr must be aligned to u16.
1101da177e4SLinus Torvalds */
is_zero_ether_addr(const u8 * addr)111b44907e6SJoe Perches static inline bool is_zero_ether_addr(const u8 *addr)
1121da177e4SLinus Torvalds {
1132c722fe1SJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
1142c722fe1SJoe Perches return ((*(const u32 *)addr) | (*(const u16 *)(addr + 4))) == 0;
1152c722fe1SJoe Perches #else
1162c722fe1SJoe Perches return (*(const u16 *)(addr + 0) |
1172c722fe1SJoe Perches *(const u16 *)(addr + 2) |
1182c722fe1SJoe Perches *(const u16 *)(addr + 4)) == 0;
1192c722fe1SJoe Perches #endif
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds
1221da177e4SLinus Torvalds /**
123c2da8acaSStephen Hemminger * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
12479165121SMichael Ellerman * @addr: Pointer to a six-byte array containing the Ethernet address
12579165121SMichael Ellerman *
126*3f330db3SJakub Kicinski * Return: true if the address is a multicast address.
12788df8ef5SStephen Hemminger * By definition the broadcast address is also a multicast address.
12879165121SMichael Ellerman */
is_multicast_ether_addr(const u8 * addr)129b44907e6SJoe Perches static inline bool is_multicast_ether_addr(const u8 *addr)
13079165121SMichael Ellerman {
131d54385ceSAlexander Duyck #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
132d54385ceSAlexander Duyck u32 a = *(const u32 *)addr;
133d54385ceSAlexander Duyck #else
134d54385ceSAlexander Duyck u16 a = *(const u16 *)addr;
135d54385ceSAlexander Duyck #endif
136d54385ceSAlexander Duyck #ifdef __BIG_ENDIAN
137d54385ceSAlexander Duyck return 0x01 & (a >> ((sizeof(a) * 8) - 8));
138d54385ceSAlexander Duyck #else
139d54385ceSAlexander Duyck return 0x01 & a;
140d54385ceSAlexander Duyck #endif
141d54385ceSAlexander Duyck }
142d54385ceSAlexander Duyck
is_multicast_ether_addr_64bits(const u8 * addr)1432618a0daSKees Cook static inline bool is_multicast_ether_addr_64bits(const u8 *addr)
144d54385ceSAlexander Duyck {
145d54385ceSAlexander Duyck #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
146d54385ceSAlexander Duyck #ifdef __BIG_ENDIAN
147d54385ceSAlexander Duyck return 0x01 & ((*(const u64 *)addr) >> 56);
148d54385ceSAlexander Duyck #else
149d54385ceSAlexander Duyck return 0x01 & (*(const u64 *)addr);
150d54385ceSAlexander Duyck #endif
151d54385ceSAlexander Duyck #else
152d54385ceSAlexander Duyck return is_multicast_ether_addr(addr);
153d54385ceSAlexander Duyck #endif
15479165121SMichael Ellerman }
15579165121SMichael Ellerman
156c2da8acaSStephen Hemminger /**
157bc2cda1eSRandy Dunlap * is_local_ether_addr - Determine if the Ethernet address is locally-assigned one (IEEE 802).
158e024715fSPaolo 'Blaisorblade' Giarrusso * @addr: Pointer to a six-byte array containing the Ethernet address
159e024715fSPaolo 'Blaisorblade' Giarrusso *
160*3f330db3SJakub Kicinski * Return: true if the address is a local address.
161e024715fSPaolo 'Blaisorblade' Giarrusso */
is_local_ether_addr(const u8 * addr)162b44907e6SJoe Perches static inline bool is_local_ether_addr(const u8 *addr)
163e024715fSPaolo 'Blaisorblade' Giarrusso {
164a02cec21SEric Dumazet return 0x02 & addr[0];
165e024715fSPaolo 'Blaisorblade' Giarrusso }
166e024715fSPaolo 'Blaisorblade' Giarrusso
167e024715fSPaolo 'Blaisorblade' Giarrusso /**
168c2da8acaSStephen Hemminger * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
169c2da8acaSStephen Hemminger * @addr: Pointer to a six-byte array containing the Ethernet address
170c2da8acaSStephen Hemminger *
171*3f330db3SJakub Kicinski * Return: true if the address is the broadcast address.
1722c722fe1SJoe Perches *
1732c722fe1SJoe Perches * Please note: addr must be aligned to u16.
174c2da8acaSStephen Hemminger */
is_broadcast_ether_addr(const u8 * addr)175b44907e6SJoe Perches static inline bool is_broadcast_ether_addr(const u8 *addr)
1769bd481f8SJeff Garzik {
1772c722fe1SJoe Perches return (*(const u16 *)(addr + 0) &
1782c722fe1SJoe Perches *(const u16 *)(addr + 2) &
1792c722fe1SJoe Perches *(const u16 *)(addr + 4)) == 0xffff;
1809bd481f8SJeff Garzik }
1819bd481f8SJeff Garzik
18279165121SMichael Ellerman /**
18351e7eed7STobias Klauser * is_unicast_ether_addr - Determine if the Ethernet address is unicast
18451e7eed7STobias Klauser * @addr: Pointer to a six-byte array containing the Ethernet address
18551e7eed7STobias Klauser *
186*3f330db3SJakub Kicinski * Return: true if the address is a unicast address.
18751e7eed7STobias Klauser */
is_unicast_ether_addr(const u8 * addr)188b44907e6SJoe Perches static inline bool is_unicast_ether_addr(const u8 *addr)
18951e7eed7STobias Klauser {
19051e7eed7STobias Klauser return !is_multicast_ether_addr(addr);
19151e7eed7STobias Klauser }
19251e7eed7STobias Klauser
19351e7eed7STobias Klauser /**
1941da177e4SLinus Torvalds * is_valid_ether_addr - Determine if the given Ethernet address is valid
1951da177e4SLinus Torvalds * @addr: Pointer to a six-byte array containing the Ethernet address
1961da177e4SLinus Torvalds *
1971da177e4SLinus Torvalds * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
19879165121SMichael Ellerman * a multicast address, and is not FF:FF:FF:FF:FF:FF.
1991da177e4SLinus Torvalds *
200*3f330db3SJakub Kicinski * Return: true if the address is valid.
2012c722fe1SJoe Perches *
2022c722fe1SJoe Perches * Please note: addr must be aligned to u16.
2031da177e4SLinus Torvalds */
is_valid_ether_addr(const u8 * addr)204b44907e6SJoe Perches static inline bool is_valid_ether_addr(const u8 *addr)
2051da177e4SLinus Torvalds {
20679165121SMichael Ellerman /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
20779165121SMichael Ellerman * explicitly check for it here. */
20879165121SMichael Ellerman return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds
2111da177e4SLinus Torvalds /**
2122c7a88c2SAlexander Duyck * eth_proto_is_802_3 - Determine if a given Ethertype/length is a protocol
2132c7a88c2SAlexander Duyck * @proto: Ethertype/length value to be tested
2142c7a88c2SAlexander Duyck *
2152c7a88c2SAlexander Duyck * Check that the value from the Ethertype/length field is a valid Ethertype.
2162c7a88c2SAlexander Duyck *
217*3f330db3SJakub Kicinski * Return: true if the valid is an 802.3 supported Ethertype.
2182c7a88c2SAlexander Duyck */
eth_proto_is_802_3(__be16 proto)2192c7a88c2SAlexander Duyck static inline bool eth_proto_is_802_3(__be16 proto)
2202c7a88c2SAlexander Duyck {
2212c7a88c2SAlexander Duyck #ifndef __BIG_ENDIAN
2222c7a88c2SAlexander Duyck /* if CPU is little endian mask off bits representing LSB */
2232c7a88c2SAlexander Duyck proto &= htons(0xFF00);
2242c7a88c2SAlexander Duyck #endif
2252c7a88c2SAlexander Duyck /* cast both to u16 and compare since LSB can be ignored */
2262c7a88c2SAlexander Duyck return (__force u16)proto >= (__force u16)htons(ETH_P_802_3_MIN);
2272c7a88c2SAlexander Duyck }
2282c7a88c2SAlexander Duyck
2292c7a88c2SAlexander Duyck /**
2300a4dd594SJoe Perches * eth_random_addr - Generate software assigned random Ethernet address
2311da177e4SLinus Torvalds * @addr: Pointer to a six-byte array containing the Ethernet address
2321da177e4SLinus Torvalds *
2331da177e4SLinus Torvalds * Generate a random Ethernet address (MAC) that is not multicast
2341da177e4SLinus Torvalds * and has the local assigned bit set.
2351da177e4SLinus Torvalds */
eth_random_addr(u8 * addr)2360a4dd594SJoe Perches static inline void eth_random_addr(u8 *addr)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds get_random_bytes(addr, ETH_ALEN);
2391da177e4SLinus Torvalds addr[0] &= 0xfe; /* clear multicast bit */
2401da177e4SLinus Torvalds addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
2411da177e4SLinus Torvalds }
242360ac8e2SStephen Hemminger
243360ac8e2SStephen Hemminger /**
244ad7eee98SJohannes Berg * eth_broadcast_addr - Assign broadcast address
245ad7eee98SJohannes Berg * @addr: Pointer to a six-byte array containing the Ethernet address
246ad7eee98SJohannes Berg *
247ad7eee98SJohannes Berg * Assign the broadcast address to the given address array.
248ad7eee98SJohannes Berg */
eth_broadcast_addr(u8 * addr)249ad7eee98SJohannes Berg static inline void eth_broadcast_addr(u8 *addr)
250ad7eee98SJohannes Berg {
251ad7eee98SJohannes Berg memset(addr, 0xff, ETH_ALEN);
252ad7eee98SJohannes Berg }
253ad7eee98SJohannes Berg
254ad7eee98SJohannes Berg /**
2556d57e907SDuan Jiong * eth_zero_addr - Assign zero address
2566d57e907SDuan Jiong * @addr: Pointer to a six-byte array containing the Ethernet address
2576d57e907SDuan Jiong *
2586d57e907SDuan Jiong * Assign the zero address to the given address array.
2596d57e907SDuan Jiong */
eth_zero_addr(u8 * addr)2606d57e907SDuan Jiong static inline void eth_zero_addr(u8 *addr)
2616d57e907SDuan Jiong {
2626d57e907SDuan Jiong memset(addr, 0x00, ETH_ALEN);
2636d57e907SDuan Jiong }
2646d57e907SDuan Jiong
2656d57e907SDuan Jiong /**
2661a0d6ae5SDanny Kukawka * eth_hw_addr_random - Generate software assigned random Ethernet and
2671a0d6ae5SDanny Kukawka * set device flag
268c1f79426SStefan Assmann * @dev: pointer to net_device structure
269c1f79426SStefan Assmann *
2701a0d6ae5SDanny Kukawka * Generate a random Ethernet address (MAC) to be used by a net device
2711a0d6ae5SDanny Kukawka * and set addr_assign_type so the state can be read by sysfs and be
2721a0d6ae5SDanny Kukawka * used by userspace.
273c1f79426SStefan Assmann */
eth_hw_addr_random(struct net_device * dev)2741a0d6ae5SDanny Kukawka static inline void eth_hw_addr_random(struct net_device *dev)
275c1f79426SStefan Assmann {
27654f2d8d6SJakub Kicinski u8 addr[ETH_ALEN];
27754f2d8d6SJakub Kicinski
27854f2d8d6SJakub Kicinski eth_random_addr(addr);
27954f2d8d6SJakub Kicinski __dev_addr_set(dev, addr, ETH_ALEN);
280e41b2d7fSJiri Pirko dev->addr_assign_type = NET_ADDR_RANDOM;
281c1f79426SStefan Assmann }
282c1f79426SStefan Assmann
283c1f79426SStefan Assmann /**
284b86cd700SHeiner Kallweit * eth_hw_addr_crc - Calculate CRC from netdev_hw_addr
285b86cd700SHeiner Kallweit * @ha: pointer to hardware address
286b86cd700SHeiner Kallweit *
287b86cd700SHeiner Kallweit * Calculate CRC from a hardware address as basis for filter hashes.
288b86cd700SHeiner Kallweit */
eth_hw_addr_crc(struct netdev_hw_addr * ha)289b86cd700SHeiner Kallweit static inline u32 eth_hw_addr_crc(struct netdev_hw_addr *ha)
290b86cd700SHeiner Kallweit {
291b86cd700SHeiner Kallweit return ether_crc(ETH_ALEN, ha->addr);
292b86cd700SHeiner Kallweit }
293b86cd700SHeiner Kallweit
294b86cd700SHeiner Kallweit /**
295286ab723SJoe Perches * ether_addr_copy - Copy an Ethernet address
296286ab723SJoe Perches * @dst: Pointer to a six-byte array Ethernet address destination
297286ab723SJoe Perches * @src: Pointer to a six-byte array Ethernet address source
298286ab723SJoe Perches *
299286ab723SJoe Perches * Please note: dst & src must both be aligned to u16.
300286ab723SJoe Perches */
ether_addr_copy(u8 * dst,const u8 * src)301286ab723SJoe Perches static inline void ether_addr_copy(u8 *dst, const u8 *src)
302286ab723SJoe Perches {
303286ab723SJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
304286ab723SJoe Perches *(u32 *)dst = *(const u32 *)src;
305286ab723SJoe Perches *(u16 *)(dst + 4) = *(const u16 *)(src + 4);
306286ab723SJoe Perches #else
307286ab723SJoe Perches u16 *a = (u16 *)dst;
308286ab723SJoe Perches const u16 *b = (const u16 *)src;
309286ab723SJoe Perches
310286ab723SJoe Perches a[0] = b[0];
311286ab723SJoe Perches a[1] = b[1];
312286ab723SJoe Perches a[2] = b[2];
313286ab723SJoe Perches #endif
314286ab723SJoe Perches }
315286ab723SJoe Perches
316286ab723SJoe Perches /**
31748eab831SJakub Kicinski * eth_hw_addr_set - Assign Ethernet address to a net_device
31848eab831SJakub Kicinski * @dev: pointer to net_device structure
31948eab831SJakub Kicinski * @addr: address to assign
32048eab831SJakub Kicinski *
32148eab831SJakub Kicinski * Assign given address to the net_device, addr_assign_type is not changed.
32248eab831SJakub Kicinski */
eth_hw_addr_set(struct net_device * dev,const u8 * addr)32348eab831SJakub Kicinski static inline void eth_hw_addr_set(struct net_device *dev, const u8 *addr)
32448eab831SJakub Kicinski {
3253f6cffb8SJakub Kicinski __dev_addr_set(dev, addr, ETH_ALEN);
32648eab831SJakub Kicinski }
32748eab831SJakub Kicinski
32848eab831SJakub Kicinski /**
32983a093b4SBjørn Mork * eth_hw_addr_inherit - Copy dev_addr from another net_device
33083a093b4SBjørn Mork * @dst: pointer to net_device to copy dev_addr to
33183a093b4SBjørn Mork * @src: pointer to net_device to copy dev_addr from
33283a093b4SBjørn Mork *
33383a093b4SBjørn Mork * Copy the Ethernet address from one net_device to another along with
33483a093b4SBjørn Mork * the address attributes (addr_assign_type).
33583a093b4SBjørn Mork */
eth_hw_addr_inherit(struct net_device * dst,struct net_device * src)33683a093b4SBjørn Mork static inline void eth_hw_addr_inherit(struct net_device *dst,
33783a093b4SBjørn Mork struct net_device *src)
33883a093b4SBjørn Mork {
33983a093b4SBjørn Mork dst->addr_assign_type = src->addr_assign_type;
340f3956ebbSJakub Kicinski eth_hw_addr_set(dst, src->dev_addr);
34183a093b4SBjørn Mork }
34283a093b4SBjørn Mork
34383a093b4SBjørn Mork /**
344a599b0f5SJoe Perches * ether_addr_equal - Compare two Ethernet addresses
345a599b0f5SJoe Perches * @addr1: Pointer to a six-byte array containing the Ethernet address
346a599b0f5SJoe Perches * @addr2: Pointer other six-byte array containing the Ethernet address
347a599b0f5SJoe Perches *
348048b899cSstephen hemminger * Compare two Ethernet addresses, returns true if equal
3490d74c42fSJoe Perches *
3500d74c42fSJoe Perches * Please note: addr1 & addr2 must both be aligned to u16.
351a599b0f5SJoe Perches */
ether_addr_equal(const u8 * addr1,const u8 * addr2)352a599b0f5SJoe Perches static inline bool ether_addr_equal(const u8 *addr1, const u8 *addr2)
353a599b0f5SJoe Perches {
3540d74c42fSJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
3550d74c42fSJoe Perches u32 fold = ((*(const u32 *)addr1) ^ (*(const u32 *)addr2)) |
3560d74c42fSJoe Perches ((*(const u16 *)(addr1 + 4)) ^ (*(const u16 *)(addr2 + 4)));
357a599b0f5SJoe Perches
3580d74c42fSJoe Perches return fold == 0;
3591f87e235SEric Dumazet #else
3600d74c42fSJoe Perches const u16 *a = (const u16 *)addr1;
3610d74c42fSJoe Perches const u16 *b = (const u16 *)addr2;
3620d74c42fSJoe Perches
3630d74c42fSJoe Perches return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0;
3641f87e235SEric Dumazet #endif
3651f87e235SEric Dumazet }
3661f87e235SEric Dumazet
3671f87e235SEric Dumazet /**
368baf523c9SJoe Perches * ether_addr_equal_64bits - Compare two Ethernet addresses
369baf523c9SJoe Perches * @addr1: Pointer to an array of 8 bytes
370baf523c9SJoe Perches * @addr2: Pointer to an other array of 8 bytes
371baf523c9SJoe Perches *
372048b899cSstephen hemminger * Compare two Ethernet addresses, returns true if equal, false otherwise.
373baf523c9SJoe Perches *
374baf523c9SJoe Perches * The function doesn't need any conditional branches and possibly uses
375baf523c9SJoe Perches * word memory accesses on CPU allowing cheap unaligned memory reads.
376048b899cSstephen hemminger * arrays = { byte1, byte2, byte3, byte4, byte5, byte6, pad1, pad2 }
377baf523c9SJoe Perches *
378048b899cSstephen hemminger * Please note that alignment of addr1 & addr2 are only guaranteed to be 16 bits.
379baf523c9SJoe Perches */
380baf523c9SJoe Perches
ether_addr_equal_64bits(const u8 * addr1,const u8 * addr2)3812618a0daSKees Cook static inline bool ether_addr_equal_64bits(const u8 *addr1, const u8 *addr2)
382baf523c9SJoe Perches {
3830d74c42fSJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
3840d74c42fSJoe Perches u64 fold = (*(const u64 *)addr1) ^ (*(const u64 *)addr2);
385e550ba1aSJoe Perches
3860d74c42fSJoe Perches #ifdef __BIG_ENDIAN
3870d74c42fSJoe Perches return (fold >> 16) == 0;
3880d74c42fSJoe Perches #else
3890d74c42fSJoe Perches return (fold << 16) == 0;
3900d74c42fSJoe Perches #endif
391e550ba1aSJoe Perches #else
392e550ba1aSJoe Perches return ether_addr_equal(addr1, addr2);
393e550ba1aSJoe Perches #endif
394baf523c9SJoe Perches }
395baf523c9SJoe Perches
396baf523c9SJoe Perches /**
39773eaef87SJoe Perches * ether_addr_equal_unaligned - Compare two not u16 aligned Ethernet addresses
39873eaef87SJoe Perches * @addr1: Pointer to a six-byte array containing the Ethernet address
39973eaef87SJoe Perches * @addr2: Pointer other six-byte array containing the Ethernet address
40073eaef87SJoe Perches *
40173eaef87SJoe Perches * Compare two Ethernet addresses, returns true if equal
40273eaef87SJoe Perches *
40373eaef87SJoe Perches * Please note: Use only when any Ethernet address may not be u16 aligned.
40473eaef87SJoe Perches */
ether_addr_equal_unaligned(const u8 * addr1,const u8 * addr2)40573eaef87SJoe Perches static inline bool ether_addr_equal_unaligned(const u8 *addr1, const u8 *addr2)
40673eaef87SJoe Perches {
40773eaef87SJoe Perches #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
40873eaef87SJoe Perches return ether_addr_equal(addr1, addr2);
40973eaef87SJoe Perches #else
41073eaef87SJoe Perches return memcmp(addr1, addr2, ETH_ALEN) == 0;
41173eaef87SJoe Perches #endif
41273eaef87SJoe Perches }
41373eaef87SJoe Perches
41473eaef87SJoe Perches /**
4154ae89ad9SJoe Perches * ether_addr_equal_masked - Compare two Ethernet addresses with a mask
4164ae89ad9SJoe Perches * @addr1: Pointer to a six-byte array containing the 1st Ethernet address
4174ae89ad9SJoe Perches * @addr2: Pointer to a six-byte array containing the 2nd Ethernet address
4184ae89ad9SJoe Perches * @mask: Pointer to a six-byte array containing the Ethernet address bitmask
4194ae89ad9SJoe Perches *
4204ae89ad9SJoe Perches * Compare two Ethernet addresses with a mask, returns true if for every bit
4214ae89ad9SJoe Perches * set in the bitmask the equivalent bits in the ethernet addresses are equal.
4224ae89ad9SJoe Perches * Using a mask with all bits set is a slower ether_addr_equal.
4234ae89ad9SJoe Perches */
ether_addr_equal_masked(const u8 * addr1,const u8 * addr2,const u8 * mask)4244ae89ad9SJoe Perches static inline bool ether_addr_equal_masked(const u8 *addr1, const u8 *addr2,
4254ae89ad9SJoe Perches const u8 *mask)
4264ae89ad9SJoe Perches {
4274ae89ad9SJoe Perches int i;
4284ae89ad9SJoe Perches
4294ae89ad9SJoe Perches for (i = 0; i < ETH_ALEN; i++) {
4304ae89ad9SJoe Perches if ((addr1[i] ^ addr2[i]) & mask[i])
4314ae89ad9SJoe Perches return false;
4324ae89ad9SJoe Perches }
4334ae89ad9SJoe Perches
4344ae89ad9SJoe Perches return true;
4354ae89ad9SJoe Perches }
4364ae89ad9SJoe Perches
ether_addr_is_ipv4_mcast(const u8 * addr)437e8013f8eSCasper Andersson static inline bool ether_addr_is_ipv4_mcast(const u8 *addr)
438e8013f8eSCasper Andersson {
439e8013f8eSCasper Andersson u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
440e8013f8eSCasper Andersson
441e1900d7bSDiogo Ivo return ether_addr_equal_masked(addr, eth_ipv4_mcast_addr_base, mask);
442e8013f8eSCasper Andersson }
443e8013f8eSCasper Andersson
ether_addr_is_ipv6_mcast(const u8 * addr)444e8013f8eSCasper Andersson static inline bool ether_addr_is_ipv6_mcast(const u8 *addr)
445e8013f8eSCasper Andersson {
446e8013f8eSCasper Andersson u8 mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
447e8013f8eSCasper Andersson
448e1900d7bSDiogo Ivo return ether_addr_equal_masked(addr, eth_ipv6_mcast_addr_base, mask);
449e8013f8eSCasper Andersson }
450e8013f8eSCasper Andersson
ether_addr_is_ip_mcast(const u8 * addr)451e8013f8eSCasper Andersson static inline bool ether_addr_is_ip_mcast(const u8 *addr)
452e8013f8eSCasper Andersson {
453e8013f8eSCasper Andersson return ether_addr_is_ipv4_mcast(addr) ||
454e8013f8eSCasper Andersson ether_addr_is_ipv6_mcast(addr);
455e8013f8eSCasper Andersson }
456e8013f8eSCasper Andersson
4574ae89ad9SJoe Perches /**
45859527581SAndrew Lunn * ether_addr_to_u64 - Convert an Ethernet address into a u64 value.
45959527581SAndrew Lunn * @addr: Pointer to a six-byte array containing the Ethernet address
46059527581SAndrew Lunn *
461*3f330db3SJakub Kicinski * Return: a u64 value of the address
46259527581SAndrew Lunn */
ether_addr_to_u64(const u8 * addr)46359527581SAndrew Lunn static inline u64 ether_addr_to_u64(const u8 *addr)
46459527581SAndrew Lunn {
46559527581SAndrew Lunn u64 u = 0;
46659527581SAndrew Lunn int i;
46759527581SAndrew Lunn
46859527581SAndrew Lunn for (i = 0; i < ETH_ALEN; i++)
46959527581SAndrew Lunn u = u << 8 | addr[i];
47059527581SAndrew Lunn
47159527581SAndrew Lunn return u;
47259527581SAndrew Lunn }
47359527581SAndrew Lunn
47459527581SAndrew Lunn /**
47559527581SAndrew Lunn * u64_to_ether_addr - Convert a u64 to an Ethernet address.
47659527581SAndrew Lunn * @u: u64 to convert to an Ethernet MAC address
47759527581SAndrew Lunn * @addr: Pointer to a six-byte array to contain the Ethernet address
47859527581SAndrew Lunn */
u64_to_ether_addr(u64 u,u8 * addr)47959527581SAndrew Lunn static inline void u64_to_ether_addr(u64 u, u8 *addr)
48059527581SAndrew Lunn {
48159527581SAndrew Lunn int i;
48259527581SAndrew Lunn
48359527581SAndrew Lunn for (i = ETH_ALEN - 1; i >= 0; i--) {
48459527581SAndrew Lunn addr[i] = u & 0xff;
48559527581SAndrew Lunn u = u >> 8;
48659527581SAndrew Lunn }
48759527581SAndrew Lunn }
48859527581SAndrew Lunn
48959527581SAndrew Lunn /**
49059527581SAndrew Lunn * eth_addr_dec - Decrement the given MAC address
49159527581SAndrew Lunn *
49259527581SAndrew Lunn * @addr: Pointer to a six-byte array containing Ethernet address to decrement
49359527581SAndrew Lunn */
eth_addr_dec(u8 * addr)49459527581SAndrew Lunn static inline void eth_addr_dec(u8 *addr)
49559527581SAndrew Lunn {
49659527581SAndrew Lunn u64 u = ether_addr_to_u64(addr);
49759527581SAndrew Lunn
49859527581SAndrew Lunn u--;
49959527581SAndrew Lunn u64_to_ether_addr(u, addr);
50059527581SAndrew Lunn }
50159527581SAndrew Lunn
50259527581SAndrew Lunn /**
5031c5c12eeSTao Ren * eth_addr_inc() - Increment the given MAC address.
5041c5c12eeSTao Ren * @addr: Pointer to a six-byte array containing Ethernet address to increment.
5051c5c12eeSTao Ren */
eth_addr_inc(u8 * addr)5061c5c12eeSTao Ren static inline void eth_addr_inc(u8 *addr)
5071c5c12eeSTao Ren {
5081c5c12eeSTao Ren u64 u = ether_addr_to_u64(addr);
5091c5c12eeSTao Ren
5101c5c12eeSTao Ren u++;
5111c5c12eeSTao Ren u64_to_ether_addr(u, addr);
5121c5c12eeSTao Ren }
5131c5c12eeSTao Ren
5141c5c12eeSTao Ren /**
5157390609bSMichael Walle * eth_addr_add() - Add (or subtract) an offset to/from the given MAC address.
5167390609bSMichael Walle *
5177390609bSMichael Walle * @offset: Offset to add.
5187390609bSMichael Walle * @addr: Pointer to a six-byte array containing Ethernet address to increment.
5197390609bSMichael Walle */
eth_addr_add(u8 * addr,long offset)5207390609bSMichael Walle static inline void eth_addr_add(u8 *addr, long offset)
5217390609bSMichael Walle {
5227390609bSMichael Walle u64 u = ether_addr_to_u64(addr);
5237390609bSMichael Walle
5247390609bSMichael Walle u += offset;
5257390609bSMichael Walle u64_to_ether_addr(u, addr);
5267390609bSMichael Walle }
5277390609bSMichael Walle
5287390609bSMichael Walle /**
529f001fde5SJiri Pirko * is_etherdev_addr - Tell if given Ethernet address belongs to the device.
530f001fde5SJiri Pirko * @dev: Pointer to a device structure
531f001fde5SJiri Pirko * @addr: Pointer to a six-byte array containing the Ethernet address
532f001fde5SJiri Pirko *
533f001fde5SJiri Pirko * Compare passed address with all addresses of the device. Return true if the
534f001fde5SJiri Pirko * address if one of the device addresses.
535f001fde5SJiri Pirko *
536e550ba1aSJoe Perches * Note that this function calls ether_addr_equal_64bits() so take care of
537f001fde5SJiri Pirko * the right padding.
538f001fde5SJiri Pirko */
is_etherdev_addr(const struct net_device * dev,const u8 addr[6+2])539f001fde5SJiri Pirko static inline bool is_etherdev_addr(const struct net_device *dev,
540f001fde5SJiri Pirko const u8 addr[6 + 2])
541f001fde5SJiri Pirko {
542f001fde5SJiri Pirko struct netdev_hw_addr *ha;
543e550ba1aSJoe Perches bool res = false;
544f001fde5SJiri Pirko
545f001fde5SJiri Pirko rcu_read_lock();
546f001fde5SJiri Pirko for_each_dev_addr(dev, ha) {
547e550ba1aSJoe Perches res = ether_addr_equal_64bits(addr, ha->addr);
548e550ba1aSJoe Perches if (res)
549f001fde5SJiri Pirko break;
550f001fde5SJiri Pirko }
551f001fde5SJiri Pirko rcu_read_unlock();
552e550ba1aSJoe Perches return res;
553f001fde5SJiri Pirko }
55479165121SMichael Ellerman #endif /* __KERNEL__ */
5551da177e4SLinus Torvalds
556aa4b9f53SHerbert Xu /**
557aa4b9f53SHerbert Xu * compare_ether_header - Compare two Ethernet headers
558aa4b9f53SHerbert Xu * @a: Pointer to Ethernet header
559aa4b9f53SHerbert Xu * @b: Pointer to Ethernet header
560aa4b9f53SHerbert Xu *
561048b899cSstephen hemminger * Compare two Ethernet headers, returns 0 if equal.
562aa4b9f53SHerbert Xu * This assumes that the network header (i.e., IP header) is 4-byte
563aa4b9f53SHerbert Xu * aligned OR the platform can handle unaligned access. This is the
564aa4b9f53SHerbert Xu * case for all packets coming into netif_receive_skb or similar
565aa4b9f53SHerbert Xu * entry points.
566aa4b9f53SHerbert Xu */
567aa4b9f53SHerbert Xu
compare_ether_header(const void * a,const void * b)56840d0802bSEric Dumazet static inline unsigned long compare_ether_header(const void *a, const void *b)
569aa4b9f53SHerbert Xu {
57040d0802bSEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
57140d0802bSEric Dumazet unsigned long fold;
57240d0802bSEric Dumazet
57340d0802bSEric Dumazet /*
57440d0802bSEric Dumazet * We want to compare 14 bytes:
57540d0802bSEric Dumazet * [a0 ... a13] ^ [b0 ... b13]
57640d0802bSEric Dumazet * Use two long XOR, ORed together, with an overlap of two bytes.
57740d0802bSEric Dumazet * [a0 a1 a2 a3 a4 a5 a6 a7 ] ^ [b0 b1 b2 b3 b4 b5 b6 b7 ] |
57840d0802bSEric Dumazet * [a6 a7 a8 a9 a10 a11 a12 a13] ^ [b6 b7 b8 b9 b10 b11 b12 b13]
57940d0802bSEric Dumazet * This means the [a6 a7] ^ [b6 b7] part is done two times.
58040d0802bSEric Dumazet */
58140d0802bSEric Dumazet fold = *(unsigned long *)a ^ *(unsigned long *)b;
58240d0802bSEric Dumazet fold |= *(unsigned long *)(a + 6) ^ *(unsigned long *)(b + 6);
58340d0802bSEric Dumazet return fold;
58440d0802bSEric Dumazet #else
585aa4b9f53SHerbert Xu u32 *a32 = (u32 *)((u8 *)a + 2);
586aa4b9f53SHerbert Xu u32 *b32 = (u32 *)((u8 *)b + 2);
587aa4b9f53SHerbert Xu
588aa4b9f53SHerbert Xu return (*(u16 *)a ^ *(u16 *)b) | (a32[0] ^ b32[0]) |
589aa4b9f53SHerbert Xu (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]);
59040d0802bSEric Dumazet #endif
591aa4b9f53SHerbert Xu }
592aa4b9f53SHerbert Xu
5939c0c1124SAlexander Duyck /**
594e80094a4SJakub Kicinski * eth_hw_addr_gen - Generate and assign Ethernet address to a port
595e80094a4SJakub Kicinski * @dev: pointer to port's net_device structure
596e80094a4SJakub Kicinski * @base_addr: base Ethernet address
597e80094a4SJakub Kicinski * @id: offset to add to the base address
598e80094a4SJakub Kicinski *
599e80094a4SJakub Kicinski * Generate a MAC address using a base address and an offset and assign it
600e80094a4SJakub Kicinski * to a net_device. Commonly used by switch drivers which need to compute
601e80094a4SJakub Kicinski * addresses for all their ports. addr_assign_type is not changed.
602e80094a4SJakub Kicinski */
eth_hw_addr_gen(struct net_device * dev,const u8 * base_addr,unsigned int id)603e80094a4SJakub Kicinski static inline void eth_hw_addr_gen(struct net_device *dev, const u8 *base_addr,
604e80094a4SJakub Kicinski unsigned int id)
605e80094a4SJakub Kicinski {
606e80094a4SJakub Kicinski u64 u = ether_addr_to_u64(base_addr);
607e80094a4SJakub Kicinski u8 addr[ETH_ALEN];
608e80094a4SJakub Kicinski
609e80094a4SJakub Kicinski u += id;
610e80094a4SJakub Kicinski u64_to_ether_addr(u, addr);
611e80094a4SJakub Kicinski eth_hw_addr_set(dev, addr);
612e80094a4SJakub Kicinski }
613e80094a4SJakub Kicinski
614e80094a4SJakub Kicinski /**
6156e159fd6SRahul Rameshbabu * eth_skb_pkt_type - Assign packet type if destination address does not match
6166e159fd6SRahul Rameshbabu * @skb: Assigned a packet type if address does not match @dev address
6176e159fd6SRahul Rameshbabu * @dev: Network device used to compare packet address against
6186e159fd6SRahul Rameshbabu *
6196e159fd6SRahul Rameshbabu * If the destination MAC address of the packet does not match the network
6206e159fd6SRahul Rameshbabu * device address, assign an appropriate packet type.
6216e159fd6SRahul Rameshbabu */
eth_skb_pkt_type(struct sk_buff * skb,const struct net_device * dev)6226e159fd6SRahul Rameshbabu static inline void eth_skb_pkt_type(struct sk_buff *skb,
6236e159fd6SRahul Rameshbabu const struct net_device *dev)
6246e159fd6SRahul Rameshbabu {
6256e159fd6SRahul Rameshbabu const struct ethhdr *eth = eth_hdr(skb);
6266e159fd6SRahul Rameshbabu
6276e159fd6SRahul Rameshbabu if (unlikely(!ether_addr_equal_64bits(eth->h_dest, dev->dev_addr))) {
6286e159fd6SRahul Rameshbabu if (unlikely(is_multicast_ether_addr_64bits(eth->h_dest))) {
6296e159fd6SRahul Rameshbabu if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
6306e159fd6SRahul Rameshbabu skb->pkt_type = PACKET_BROADCAST;
6316e159fd6SRahul Rameshbabu else
6326e159fd6SRahul Rameshbabu skb->pkt_type = PACKET_MULTICAST;
6336e159fd6SRahul Rameshbabu } else {
6346e159fd6SRahul Rameshbabu skb->pkt_type = PACKET_OTHERHOST;
6356e159fd6SRahul Rameshbabu }
6366e159fd6SRahul Rameshbabu }
6376e159fd6SRahul Rameshbabu }
6386e159fd6SRahul Rameshbabu
eth_skb_pull_mac(struct sk_buff * skb)6393998d184SDaniel Borkmann static inline struct ethhdr *eth_skb_pull_mac(struct sk_buff *skb)
6403998d184SDaniel Borkmann {
6413998d184SDaniel Borkmann struct ethhdr *eth = (struct ethhdr *)skb->data;
6423998d184SDaniel Borkmann
6433998d184SDaniel Borkmann skb_pull_inline(skb, ETH_HLEN);
6443998d184SDaniel Borkmann return eth;
6453998d184SDaniel Borkmann }
6463998d184SDaniel Borkmann
6476e159fd6SRahul Rameshbabu /**
64870d0bb45SSimon Horman * eth_skb_pad - Pad buffer to minimum number of octets for Ethernet frame
6499c0c1124SAlexander Duyck * @skb: Buffer to pad
6509c0c1124SAlexander Duyck *
6519c0c1124SAlexander Duyck * An Ethernet frame should have a minimum size of 60 bytes. This function
6529c0c1124SAlexander Duyck * takes short frames and pads them with zeros up to the 60 byte limit.
6539c0c1124SAlexander Duyck */
eth_skb_pad(struct sk_buff * skb)6549c0c1124SAlexander Duyck static inline int eth_skb_pad(struct sk_buff *skb)
6559c0c1124SAlexander Duyck {
6569c0c1124SAlexander Duyck return skb_put_padto(skb, ETH_ZLEN);
6579c0c1124SAlexander Duyck }
6589c0c1124SAlexander Duyck
6591da177e4SLinus Torvalds #endif /* _LINUX_ETHERDEVICE_H */
660