1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. NET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * Definitions for the Ethernet handlers.
9 * Version: @(#)eth.h 1.0.4 05/13/93
12 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
14 * Relocated to include/linux where it belongs by Alan Cox
15 * <gw4pts@gw4pts.ampr.org>
17 #ifndef _LINUX_ETHERDEVICE_H
18 #define _LINUX_ETHERDEVICE_H
20 #include <linux/if_ether.h>
21 #include <linux/netdevice.h>
22 #include <linux/random.h>
23 #include <linux/crc32.h>
24 #include <linux/unaligned.h>
25 #include <asm/bitsperlong.h>
31 int eth_platform_get_mac_address(struct device
*dev
, u8
*mac_addr
);
32 int platform_get_ethdev_address(struct device
*dev
, struct net_device
*netdev
);
33 unsigned char *arch_get_platform_mac_address(void);
34 int nvmem_get_mac_address(struct device
*dev
, void *addrbuf
);
35 int device_get_mac_address(struct device
*dev
, char *addr
);
36 int device_get_ethdev_address(struct device
*dev
, struct net_device
*netdev
);
37 int fwnode_get_mac_address(struct fwnode_handle
*fwnode
, char *addr
);
39 u32
eth_get_headlen(const struct net_device
*dev
, const void *data
, u32 len
);
40 __be16
eth_type_trans(struct sk_buff
*skb
, struct net_device
*dev
);
41 extern const struct header_ops eth_header_ops
;
43 int eth_header(struct sk_buff
*skb
, struct net_device
*dev
, unsigned short type
,
44 const void *daddr
, const void *saddr
, unsigned len
);
45 int eth_header_parse(const struct sk_buff
*skb
, unsigned char *haddr
);
46 int eth_header_cache(const struct neighbour
*neigh
, struct hh_cache
*hh
,
48 void eth_header_cache_update(struct hh_cache
*hh
, const struct net_device
*dev
,
49 const unsigned char *haddr
);
50 __be16
eth_header_parse_protocol(const struct sk_buff
*skb
);
51 int eth_prepare_mac_addr_change(struct net_device
*dev
, void *p
);
52 void eth_commit_mac_addr_change(struct net_device
*dev
, void *p
);
53 int eth_mac_addr(struct net_device
*dev
, void *p
);
54 int eth_validate_addr(struct net_device
*dev
);
56 struct net_device
*alloc_etherdev_mqs(int sizeof_priv
, unsigned int txqs
,
58 #define alloc_etherdev(sizeof_priv) alloc_etherdev_mq(sizeof_priv, 1)
59 #define alloc_etherdev_mq(sizeof_priv, count) alloc_etherdev_mqs(sizeof_priv, count, count)
61 struct net_device
*devm_alloc_etherdev_mqs(struct device
*dev
, int sizeof_priv
,
64 #define devm_alloc_etherdev(dev, sizeof_priv) devm_alloc_etherdev_mqs(dev, sizeof_priv, 1, 1)
66 struct sk_buff
*eth_gro_receive(struct list_head
*head
, struct sk_buff
*skb
);
67 int eth_gro_complete(struct sk_buff
*skb
, int nhoff
);
69 /* Reserved Ethernet Addresses per IEEE 802.1Q */
70 static const u8 eth_reserved_addr_base
[ETH_ALEN
] __aligned(2) =
71 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
72 #define eth_stp_addr eth_reserved_addr_base
74 static const u8 eth_ipv4_mcast_addr_base
[ETH_ALEN
] __aligned(2) =
75 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
77 static const u8 eth_ipv6_mcast_addr_base
[ETH_ALEN
] __aligned(2) =
78 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
81 * is_link_local_ether_addr - Determine if given Ethernet address is link-local
82 * @addr: Pointer to a six-byte array containing the Ethernet address
84 * Return true if address is link local reserved addr (01:80:c2:00:00:0X) per
85 * IEEE 802.1Q 8.6.3 Frame filtering.
87 * Please note: addr must be aligned to u16.
89 static inline bool is_link_local_ether_addr(const u8
*addr
)
91 __be16
*a
= (__be16
*)addr
;
92 static const __be16
*b
= (const __be16
*)eth_reserved_addr_base
;
93 static const __be16 m
= cpu_to_be16(0xfff0);
95 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
96 return (((*(const u32
*)addr
) ^ (*(const u32
*)b
)) |
97 (__force
int)((a
[2] ^ b
[2]) & m
)) == 0;
99 return ((a
[0] ^ b
[0]) | (a
[1] ^ b
[1]) | ((a
[2] ^ b
[2]) & m
)) == 0;
104 * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
105 * @addr: Pointer to a six-byte array containing the Ethernet address
107 * Return true if the address is all zeroes.
109 * Please note: addr must be aligned to u16.
111 static inline bool is_zero_ether_addr(const u8
*addr
)
113 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
114 return ((*(const u32
*)addr
) | (*(const u16
*)(addr
+ 4))) == 0;
116 return (*(const u16
*)(addr
+ 0) |
117 *(const u16
*)(addr
+ 2) |
118 *(const u16
*)(addr
+ 4)) == 0;
123 * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
124 * @addr: Pointer to a six-byte array containing the Ethernet address
126 * Return true if the address is a multicast address.
127 * By definition the broadcast address is also a multicast address.
129 static inline bool is_multicast_ether_addr(const u8
*addr
)
131 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
132 u32 a
= *(const u32
*)addr
;
134 u16 a
= *(const u16
*)addr
;
137 return 0x01 & (a
>> ((sizeof(a
) * 8) - 8));
143 static inline bool is_multicast_ether_addr_64bits(const u8
*addr
)
145 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
147 return 0x01 & ((*(const u64
*)addr
) >> 56);
149 return 0x01 & (*(const u64
*)addr
);
152 return is_multicast_ether_addr(addr
);
157 * is_local_ether_addr - Determine if the Ethernet address is locally-assigned one (IEEE 802).
158 * @addr: Pointer to a six-byte array containing the Ethernet address
160 * Return true if the address is a local address.
162 static inline bool is_local_ether_addr(const u8
*addr
)
164 return 0x02 & addr
[0];
168 * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
169 * @addr: Pointer to a six-byte array containing the Ethernet address
171 * Return true if the address is the broadcast address.
173 * Please note: addr must be aligned to u16.
175 static inline bool is_broadcast_ether_addr(const u8
*addr
)
177 return (*(const u16
*)(addr
+ 0) &
178 *(const u16
*)(addr
+ 2) &
179 *(const u16
*)(addr
+ 4)) == 0xffff;
183 * is_unicast_ether_addr - Determine if the Ethernet address is unicast
184 * @addr: Pointer to a six-byte array containing the Ethernet address
186 * Return true if the address is a unicast address.
188 static inline bool is_unicast_ether_addr(const u8
*addr
)
190 return !is_multicast_ether_addr(addr
);
194 * is_valid_ether_addr - Determine if the given Ethernet address is valid
195 * @addr: Pointer to a six-byte array containing the Ethernet address
197 * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
198 * a multicast address, and is not FF:FF:FF:FF:FF:FF.
200 * Return true if the address is valid.
202 * Please note: addr must be aligned to u16.
204 static inline bool is_valid_ether_addr(const u8
*addr
)
206 /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
207 * explicitly check for it here. */
208 return !is_multicast_ether_addr(addr
) && !is_zero_ether_addr(addr
);
212 * eth_proto_is_802_3 - Determine if a given Ethertype/length is a protocol
213 * @proto: Ethertype/length value to be tested
215 * Check that the value from the Ethertype/length field is a valid Ethertype.
217 * Return true if the valid is an 802.3 supported Ethertype.
219 static inline bool eth_proto_is_802_3(__be16 proto
)
222 /* if CPU is little endian mask off bits representing LSB */
223 proto
&= htons(0xFF00);
225 /* cast both to u16 and compare since LSB can be ignored */
226 return (__force u16
)proto
>= (__force u16
)htons(ETH_P_802_3_MIN
);
230 * eth_random_addr - Generate software assigned random Ethernet address
231 * @addr: Pointer to a six-byte array containing the Ethernet address
233 * Generate a random Ethernet address (MAC) that is not multicast
234 * and has the local assigned bit set.
236 static inline void eth_random_addr(u8
*addr
)
238 get_random_bytes(addr
, ETH_ALEN
);
239 addr
[0] &= 0xfe; /* clear multicast bit */
240 addr
[0] |= 0x02; /* set local assignment bit (IEEE802) */
244 * eth_broadcast_addr - Assign broadcast address
245 * @addr: Pointer to a six-byte array containing the Ethernet address
247 * Assign the broadcast address to the given address array.
249 static inline void eth_broadcast_addr(u8
*addr
)
251 memset(addr
, 0xff, ETH_ALEN
);
255 * eth_zero_addr - Assign zero address
256 * @addr: Pointer to a six-byte array containing the Ethernet address
258 * Assign the zero address to the given address array.
260 static inline void eth_zero_addr(u8
*addr
)
262 memset(addr
, 0x00, ETH_ALEN
);
266 * eth_hw_addr_random - Generate software assigned random Ethernet and
268 * @dev: pointer to net_device structure
270 * Generate a random Ethernet address (MAC) to be used by a net device
271 * and set addr_assign_type so the state can be read by sysfs and be
274 static inline void eth_hw_addr_random(struct net_device
*dev
)
278 eth_random_addr(addr
);
279 __dev_addr_set(dev
, addr
, ETH_ALEN
);
280 dev
->addr_assign_type
= NET_ADDR_RANDOM
;
284 * eth_hw_addr_crc - Calculate CRC from netdev_hw_addr
285 * @ha: pointer to hardware address
287 * Calculate CRC from a hardware address as basis for filter hashes.
289 static inline u32
eth_hw_addr_crc(struct netdev_hw_addr
*ha
)
291 return ether_crc(ETH_ALEN
, ha
->addr
);
295 * ether_addr_copy - Copy an Ethernet address
296 * @dst: Pointer to a six-byte array Ethernet address destination
297 * @src: Pointer to a six-byte array Ethernet address source
299 * Please note: dst & src must both be aligned to u16.
301 static inline void ether_addr_copy(u8
*dst
, const u8
*src
)
303 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
304 *(u32
*)dst
= *(const u32
*)src
;
305 *(u16
*)(dst
+ 4) = *(const u16
*)(src
+ 4);
308 const u16
*b
= (const u16
*)src
;
317 * eth_hw_addr_set - Assign Ethernet address to a net_device
318 * @dev: pointer to net_device structure
319 * @addr: address to assign
321 * Assign given address to the net_device, addr_assign_type is not changed.
323 static inline void eth_hw_addr_set(struct net_device
*dev
, const u8
*addr
)
325 __dev_addr_set(dev
, addr
, ETH_ALEN
);
329 * eth_hw_addr_inherit - Copy dev_addr from another net_device
330 * @dst: pointer to net_device to copy dev_addr to
331 * @src: pointer to net_device to copy dev_addr from
333 * Copy the Ethernet address from one net_device to another along with
334 * the address attributes (addr_assign_type).
336 static inline void eth_hw_addr_inherit(struct net_device
*dst
,
337 struct net_device
*src
)
339 dst
->addr_assign_type
= src
->addr_assign_type
;
340 eth_hw_addr_set(dst
, src
->dev_addr
);
344 * ether_addr_equal - Compare two Ethernet addresses
345 * @addr1: Pointer to a six-byte array containing the Ethernet address
346 * @addr2: Pointer other six-byte array containing the Ethernet address
348 * Compare two Ethernet addresses, returns true if equal
350 * Please note: addr1 & addr2 must both be aligned to u16.
352 static inline bool ether_addr_equal(const u8
*addr1
, const u8
*addr2
)
354 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
355 u32 fold
= ((*(const u32
*)addr1
) ^ (*(const u32
*)addr2
)) |
356 ((*(const u16
*)(addr1
+ 4)) ^ (*(const u16
*)(addr2
+ 4)));
360 const u16
*a
= (const u16
*)addr1
;
361 const u16
*b
= (const u16
*)addr2
;
363 return ((a
[0] ^ b
[0]) | (a
[1] ^ b
[1]) | (a
[2] ^ b
[2])) == 0;
368 * ether_addr_equal_64bits - Compare two Ethernet addresses
369 * @addr1: Pointer to an array of 8 bytes
370 * @addr2: Pointer to an other array of 8 bytes
372 * Compare two Ethernet addresses, returns true if equal, false otherwise.
374 * The function doesn't need any conditional branches and possibly uses
375 * word memory accesses on CPU allowing cheap unaligned memory reads.
376 * arrays = { byte1, byte2, byte3, byte4, byte5, byte6, pad1, pad2 }
378 * Please note that alignment of addr1 & addr2 are only guaranteed to be 16 bits.
381 static inline bool ether_addr_equal_64bits(const u8
*addr1
, const u8
*addr2
)
383 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
384 u64 fold
= (*(const u64
*)addr1
) ^ (*(const u64
*)addr2
);
387 return (fold
>> 16) == 0;
389 return (fold
<< 16) == 0;
392 return ether_addr_equal(addr1
, addr2
);
397 * ether_addr_equal_unaligned - Compare two not u16 aligned Ethernet addresses
398 * @addr1: Pointer to a six-byte array containing the Ethernet address
399 * @addr2: Pointer other six-byte array containing the Ethernet address
401 * Compare two Ethernet addresses, returns true if equal
403 * Please note: Use only when any Ethernet address may not be u16 aligned.
405 static inline bool ether_addr_equal_unaligned(const u8
*addr1
, const u8
*addr2
)
407 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
408 return ether_addr_equal(addr1
, addr2
);
410 return memcmp(addr1
, addr2
, ETH_ALEN
) == 0;
415 * ether_addr_equal_masked - Compare two Ethernet addresses with a mask
416 * @addr1: Pointer to a six-byte array containing the 1st Ethernet address
417 * @addr2: Pointer to a six-byte array containing the 2nd Ethernet address
418 * @mask: Pointer to a six-byte array containing the Ethernet address bitmask
420 * Compare two Ethernet addresses with a mask, returns true if for every bit
421 * set in the bitmask the equivalent bits in the ethernet addresses are equal.
422 * Using a mask with all bits set is a slower ether_addr_equal.
424 static inline bool ether_addr_equal_masked(const u8
*addr1
, const u8
*addr2
,
429 for (i
= 0; i
< ETH_ALEN
; i
++) {
430 if ((addr1
[i
] ^ addr2
[i
]) & mask
[i
])
437 static inline bool ether_addr_is_ipv4_mcast(const u8
*addr
)
439 u8 mask
[ETH_ALEN
] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
441 return ether_addr_equal_masked(addr
, eth_ipv4_mcast_addr_base
, mask
);
444 static inline bool ether_addr_is_ipv6_mcast(const u8
*addr
)
446 u8 mask
[ETH_ALEN
] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
448 return ether_addr_equal_masked(addr
, eth_ipv6_mcast_addr_base
, mask
);
451 static inline bool ether_addr_is_ip_mcast(const u8
*addr
)
453 return ether_addr_is_ipv4_mcast(addr
) ||
454 ether_addr_is_ipv6_mcast(addr
);
458 * ether_addr_to_u64 - Convert an Ethernet address into a u64 value.
459 * @addr: Pointer to a six-byte array containing the Ethernet address
461 * Return a u64 value of the address
463 static inline u64
ether_addr_to_u64(const u8
*addr
)
468 for (i
= 0; i
< ETH_ALEN
; i
++)
469 u
= u
<< 8 | addr
[i
];
475 * u64_to_ether_addr - Convert a u64 to an Ethernet address.
476 * @u: u64 to convert to an Ethernet MAC address
477 * @addr: Pointer to a six-byte array to contain the Ethernet address
479 static inline void u64_to_ether_addr(u64 u
, u8
*addr
)
483 for (i
= ETH_ALEN
- 1; i
>= 0; i
--) {
490 * eth_addr_dec - Decrement the given MAC address
492 * @addr: Pointer to a six-byte array containing Ethernet address to decrement
494 static inline void eth_addr_dec(u8
*addr
)
496 u64 u
= ether_addr_to_u64(addr
);
499 u64_to_ether_addr(u
, addr
);
503 * eth_addr_inc() - Increment the given MAC address.
504 * @addr: Pointer to a six-byte array containing Ethernet address to increment.
506 static inline void eth_addr_inc(u8
*addr
)
508 u64 u
= ether_addr_to_u64(addr
);
511 u64_to_ether_addr(u
, addr
);
515 * eth_addr_add() - Add (or subtract) an offset to/from the given MAC address.
517 * @offset: Offset to add.
518 * @addr: Pointer to a six-byte array containing Ethernet address to increment.
520 static inline void eth_addr_add(u8
*addr
, long offset
)
522 u64 u
= ether_addr_to_u64(addr
);
525 u64_to_ether_addr(u
, addr
);
529 * is_etherdev_addr - Tell if given Ethernet address belongs to the device.
530 * @dev: Pointer to a device structure
531 * @addr: Pointer to a six-byte array containing the Ethernet address
533 * Compare passed address with all addresses of the device. Return true if the
534 * address if one of the device addresses.
536 * Note that this function calls ether_addr_equal_64bits() so take care of
539 static inline bool is_etherdev_addr(const struct net_device
*dev
,
540 const u8 addr
[6 + 2])
542 struct netdev_hw_addr
*ha
;
546 for_each_dev_addr(dev
, ha
) {
547 res
= ether_addr_equal_64bits(addr
, ha
->addr
);
554 #endif /* __KERNEL__ */
557 * compare_ether_header - Compare two Ethernet headers
558 * @a: Pointer to Ethernet header
559 * @b: Pointer to Ethernet header
561 * Compare two Ethernet headers, returns 0 if equal.
562 * This assumes that the network header (i.e., IP header) is 4-byte
563 * aligned OR the platform can handle unaligned access. This is the
564 * case for all packets coming into netif_receive_skb or similar
568 static inline unsigned long compare_ether_header(const void *a
, const void *b
)
570 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
574 * We want to compare 14 bytes:
575 * [a0 ... a13] ^ [b0 ... b13]
576 * Use two long XOR, ORed together, with an overlap of two bytes.
577 * [a0 a1 a2 a3 a4 a5 a6 a7 ] ^ [b0 b1 b2 b3 b4 b5 b6 b7 ] |
578 * [a6 a7 a8 a9 a10 a11 a12 a13] ^ [b6 b7 b8 b9 b10 b11 b12 b13]
579 * This means the [a6 a7] ^ [b6 b7] part is done two times.
581 fold
= *(unsigned long *)a
^ *(unsigned long *)b
;
582 fold
|= *(unsigned long *)(a
+ 6) ^ *(unsigned long *)(b
+ 6);
585 u32
*a32
= (u32
*)((u8
*)a
+ 2);
586 u32
*b32
= (u32
*)((u8
*)b
+ 2);
588 return (*(u16
*)a
^ *(u16
*)b
) | (a32
[0] ^ b32
[0]) |
589 (a32
[1] ^ b32
[1]) | (a32
[2] ^ b32
[2]);
594 * eth_hw_addr_gen - Generate and assign Ethernet address to a port
595 * @dev: pointer to port's net_device structure
596 * @base_addr: base Ethernet address
597 * @id: offset to add to the base address
599 * Generate a MAC address using a base address and an offset and assign it
600 * to a net_device. Commonly used by switch drivers which need to compute
601 * addresses for all their ports. addr_assign_type is not changed.
603 static inline void eth_hw_addr_gen(struct net_device
*dev
, const u8
*base_addr
,
606 u64 u
= ether_addr_to_u64(base_addr
);
610 u64_to_ether_addr(u
, addr
);
611 eth_hw_addr_set(dev
, addr
);
615 * eth_skb_pkt_type - Assign packet type if destination address does not match
616 * @skb: Assigned a packet type if address does not match @dev address
617 * @dev: Network device used to compare packet address against
619 * If the destination MAC address of the packet does not match the network
620 * device address, assign an appropriate packet type.
622 static inline void eth_skb_pkt_type(struct sk_buff
*skb
,
623 const struct net_device
*dev
)
625 const struct ethhdr
*eth
= eth_hdr(skb
);
627 if (unlikely(!ether_addr_equal_64bits(eth
->h_dest
, dev
->dev_addr
))) {
628 if (unlikely(is_multicast_ether_addr_64bits(eth
->h_dest
))) {
629 if (ether_addr_equal_64bits(eth
->h_dest
, dev
->broadcast
))
630 skb
->pkt_type
= PACKET_BROADCAST
;
632 skb
->pkt_type
= PACKET_MULTICAST
;
634 skb
->pkt_type
= PACKET_OTHERHOST
;
639 static inline struct ethhdr
*eth_skb_pull_mac(struct sk_buff
*skb
)
641 struct ethhdr
*eth
= (struct ethhdr
*)skb
->data
;
643 skb_pull_inline(skb
, ETH_HLEN
);
648 * eth_skb_pad - Pad buffer to minimum number of octets for Ethernet frame
649 * @skb: Buffer to pad
651 * An Ethernet frame should have a minimum size of 60 bytes. This function
652 * takes short frames and pads them with zeros up to the 60 byte limit.
654 static inline int eth_skb_pad(struct sk_buff
*skb
)
656 return skb_put_padto(skb
, ETH_ZLEN
);
659 #endif /* _LINUX_ETHERDEVICE_H */