xref: /linux/include/linux/etherdevice.h (revision a1d9d8e833781c44ab688708804ce35f20f3cbbd)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
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.
6  *
7  *		Definitions for the Ethernet handlers.
8  *
9  * Version:	@(#)eth.h	1.0.4	05/13/93
10  *
11  * Authors:	Ross Biro
12  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
13  *
14  *		Relocated to include/linux where it belongs by Alan Cox
15  *							<gw4pts@gw4pts.ampr.org>
16  */
17 #ifndef _LINUX_ETHERDEVICE_H
18 #define _LINUX_ETHERDEVICE_H
19 
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>
26 
27 #ifdef __KERNEL__
28 struct device;
29 struct fwnode_handle;
30 
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);
38 
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;
42 
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, const struct net_device *dev,
46 		     unsigned char *haddr);
47 int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh,
48 		     __be16 type);
49 void eth_header_cache_update(struct hh_cache *hh, const struct net_device *dev,
50 			     const unsigned char *haddr);
51 __be16 eth_header_parse_protocol(const struct sk_buff *skb);
52 int eth_prepare_mac_addr_change(struct net_device *dev, void *p);
53 void eth_commit_mac_addr_change(struct net_device *dev, void *p);
54 int eth_mac_addr(struct net_device *dev, void *p);
55 int eth_validate_addr(struct net_device *dev);
56 
57 struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,
58 					    unsigned int rxqs);
59 #define alloc_etherdev(sizeof_priv) alloc_etherdev_mq(sizeof_priv, 1)
60 #define alloc_etherdev_mq(sizeof_priv, count) alloc_etherdev_mqs(sizeof_priv, count, count)
61 
62 struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv,
63 					   unsigned int txqs,
64 					   unsigned int rxqs);
65 #define devm_alloc_etherdev(dev, sizeof_priv) devm_alloc_etherdev_mqs(dev, sizeof_priv, 1, 1)
66 
67 struct sk_buff *eth_gro_receive(struct list_head *head, struct sk_buff *skb);
68 int eth_gro_complete(struct sk_buff *skb, int nhoff);
69 
70 /* Reserved Ethernet Addresses per IEEE 802.1Q */
71 static const u8 eth_reserved_addr_base[ETH_ALEN] __aligned(2) =
72 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 };
73 #define eth_stp_addr eth_reserved_addr_base
74 
75 static const u8 eth_ipv4_mcast_addr_base[ETH_ALEN] __aligned(2) =
76 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
77 
78 static const u8 eth_ipv6_mcast_addr_base[ETH_ALEN] __aligned(2) =
79 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
80 
81 /**
82  * is_link_local_ether_addr - Determine if given Ethernet address is link-local
83  * @addr: Pointer to a six-byte array containing the Ethernet address
84  *
85  * Return: true if address is link local reserved addr (01:80:c2:00:00:0X) per
86  * IEEE 802.1Q 8.6.3 Frame filtering.
87  *
88  * Please note: addr must be aligned to u16.
89  */
is_link_local_ether_addr(const u8 * addr)90 static inline bool is_link_local_ether_addr(const u8 *addr)
91 {
92 	__be16 *a = (__be16 *)addr;
93 	static const __be16 *b = (const __be16 *)eth_reserved_addr_base;
94 	static const __be16 m = cpu_to_be16(0xfff0);
95 
96 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
97 	return (((*(const u32 *)addr) ^ (*(const u32 *)b)) |
98 		(__force int)((a[2] ^ b[2]) & m)) == 0;
99 #else
100 	return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | ((a[2] ^ b[2]) & m)) == 0;
101 #endif
102 }
103 
104 /**
105  * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
106  * @addr: Pointer to a six-byte array containing the Ethernet address
107  *
108  * Return: true if the address is all zeroes.
109  *
110  * Please note: addr must be aligned to u16.
111  */
is_zero_ether_addr(const u8 * addr)112 static inline bool is_zero_ether_addr(const u8 *addr)
113 {
114 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
115 	return ((*(const u32 *)addr) | (*(const u16 *)(addr + 4))) == 0;
116 #else
117 	return (*(const u16 *)(addr + 0) |
118 		*(const u16 *)(addr + 2) |
119 		*(const u16 *)(addr + 4)) == 0;
120 #endif
121 }
122 
123 /**
124  * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
125  * @addr: Pointer to a six-byte array containing the Ethernet address
126  *
127  * Return: true if the address is a multicast address.
128  * By definition the broadcast address is also a multicast address.
129  */
is_multicast_ether_addr(const u8 * addr)130 static inline bool is_multicast_ether_addr(const u8 *addr)
131 {
132 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
133 	u32 a = *(const u32 *)addr;
134 #else
135 	u16 a = *(const u16 *)addr;
136 #endif
137 #ifdef __BIG_ENDIAN
138 	return 0x01 & (a >> ((sizeof(a) * 8) - 8));
139 #else
140 	return 0x01 & a;
141 #endif
142 }
143 
is_multicast_ether_addr_64bits(const u8 * addr)144 static inline bool is_multicast_ether_addr_64bits(const u8 *addr)
145 {
146 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
147 #ifdef __BIG_ENDIAN
148 	return 0x01 & ((*(const u64 *)addr) >> 56);
149 #else
150 	return 0x01 & (*(const u64 *)addr);
151 #endif
152 #else
153 	return is_multicast_ether_addr(addr);
154 #endif
155 }
156 
157 /**
158  * is_local_ether_addr - Determine if the Ethernet address is locally-assigned one (IEEE 802).
159  * @addr: Pointer to a six-byte array containing the Ethernet address
160  *
161  * Return: true if the address is a local address.
162  */
is_local_ether_addr(const u8 * addr)163 static inline bool is_local_ether_addr(const u8 *addr)
164 {
165 	return 0x02 & addr[0];
166 }
167 
168 /**
169  * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
170  * @addr: Pointer to a six-byte array containing the Ethernet address
171  *
172  * Return: true if the address is the broadcast address.
173  *
174  * Please note: addr must be aligned to u16.
175  */
is_broadcast_ether_addr(const u8 * addr)176 static inline bool is_broadcast_ether_addr(const u8 *addr)
177 {
178 	return (*(const u16 *)(addr + 0) &
179 		*(const u16 *)(addr + 2) &
180 		*(const u16 *)(addr + 4)) == 0xffff;
181 }
182 
183 /**
184  * is_unicast_ether_addr - Determine if the Ethernet address is unicast
185  * @addr: Pointer to a six-byte array containing the Ethernet address
186  *
187  * Return: true if the address is a unicast address.
188  */
is_unicast_ether_addr(const u8 * addr)189 static inline bool is_unicast_ether_addr(const u8 *addr)
190 {
191 	return !is_multicast_ether_addr(addr);
192 }
193 
194 /**
195  * is_valid_ether_addr - Determine if the given Ethernet address is valid
196  * @addr: Pointer to a six-byte array containing the Ethernet address
197  *
198  * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
199  * a multicast address, and is not FF:FF:FF:FF:FF:FF.
200  *
201  * Return: true if the address is valid.
202  *
203  * Please note: addr must be aligned to u16.
204  */
is_valid_ether_addr(const u8 * addr)205 static inline bool is_valid_ether_addr(const u8 *addr)
206 {
207 	/* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
208 	 * explicitly check for it here. */
209 	return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
210 }
211 
212 /**
213  * eth_proto_is_802_3 - Determine if a given Ethertype/length is a protocol
214  * @proto: Ethertype/length value to be tested
215  *
216  * Check that the value from the Ethertype/length field is a valid Ethertype.
217  *
218  * Return: true if the valid is an 802.3 supported Ethertype.
219  */
eth_proto_is_802_3(__be16 proto)220 static inline bool eth_proto_is_802_3(__be16 proto)
221 {
222 #ifndef __BIG_ENDIAN
223 	/* if CPU is little endian mask off bits representing LSB */
224 	proto &= htons(0xFF00);
225 #endif
226 	/* cast both to u16 and compare since LSB can be ignored */
227 	return (__force u16)proto >= (__force u16)htons(ETH_P_802_3_MIN);
228 }
229 
230 /**
231  * eth_random_addr - Generate software assigned random Ethernet address
232  * @addr: Pointer to a six-byte array containing the Ethernet address
233  *
234  * Generate a random Ethernet address (MAC) that is not multicast
235  * and has the local assigned bit set.
236  */
eth_random_addr(u8 * addr)237 static inline void eth_random_addr(u8 *addr)
238 {
239 	get_random_bytes(addr, ETH_ALEN);
240 	addr[0] &= 0xfe;	/* clear multicast bit */
241 	addr[0] |= 0x02;	/* set local assignment bit (IEEE802) */
242 }
243 
244 /**
245  * eth_broadcast_addr - Assign broadcast address
246  * @addr: Pointer to a six-byte array containing the Ethernet address
247  *
248  * Assign the broadcast address to the given address array.
249  */
eth_broadcast_addr(u8 * addr)250 static inline void eth_broadcast_addr(u8 *addr)
251 {
252 	memset(addr, 0xff, ETH_ALEN);
253 }
254 
255 /**
256  * eth_zero_addr - Assign zero address
257  * @addr: Pointer to a six-byte array containing the Ethernet address
258  *
259  * Assign the zero address to the given address array.
260  */
eth_zero_addr(u8 * addr)261 static inline void eth_zero_addr(u8 *addr)
262 {
263 	memset(addr, 0x00, ETH_ALEN);
264 }
265 
266 /**
267  * eth_hw_addr_random - Generate software assigned random Ethernet and
268  * set device flag
269  * @dev: pointer to net_device structure
270  *
271  * Generate a random Ethernet address (MAC) to be used by a net device
272  * and set addr_assign_type so the state can be read by sysfs and be
273  * used by userspace.
274  */
eth_hw_addr_random(struct net_device * dev)275 static inline void eth_hw_addr_random(struct net_device *dev)
276 {
277 	u8 addr[ETH_ALEN];
278 
279 	eth_random_addr(addr);
280 	__dev_addr_set(dev, addr, ETH_ALEN);
281 	dev->addr_assign_type = NET_ADDR_RANDOM;
282 }
283 
284 /**
285  * eth_hw_addr_crc - Calculate CRC from netdev_hw_addr
286  * @ha: pointer to hardware address
287  *
288  * Calculate CRC from a hardware address as basis for filter hashes.
289  */
eth_hw_addr_crc(struct netdev_hw_addr * ha)290 static inline u32 eth_hw_addr_crc(struct netdev_hw_addr *ha)
291 {
292 	return ether_crc(ETH_ALEN, ha->addr);
293 }
294 
295 /**
296  * ether_addr_copy - Copy an Ethernet address
297  * @dst: Pointer to a six-byte array Ethernet address destination
298  * @src: Pointer to a six-byte array Ethernet address source
299  *
300  * Please note: dst & src must both be aligned to u16.
301  */
ether_addr_copy(u8 * dst,const u8 * src)302 static inline void ether_addr_copy(u8 *dst, const u8 *src)
303 {
304 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
305 	*(u32 *)dst = *(const u32 *)src;
306 	*(u16 *)(dst + 4) = *(const u16 *)(src + 4);
307 #else
308 	u16 *a = (u16 *)dst;
309 	const u16 *b = (const u16 *)src;
310 
311 	a[0] = b[0];
312 	a[1] = b[1];
313 	a[2] = b[2];
314 #endif
315 }
316 
317 /**
318  * eth_hw_addr_set - Assign Ethernet address to a net_device
319  * @dev: pointer to net_device structure
320  * @addr: address to assign
321  *
322  * Assign given address to the net_device, addr_assign_type is not changed.
323  */
eth_hw_addr_set(struct net_device * dev,const u8 * addr)324 static inline void eth_hw_addr_set(struct net_device *dev, const u8 *addr)
325 {
326 	__dev_addr_set(dev, addr, ETH_ALEN);
327 }
328 
329 /**
330  * eth_hw_addr_inherit - Copy dev_addr from another net_device
331  * @dst: pointer to net_device to copy dev_addr to
332  * @src: pointer to net_device to copy dev_addr from
333  *
334  * Copy the Ethernet address from one net_device to another along with
335  * the address attributes (addr_assign_type).
336  */
eth_hw_addr_inherit(struct net_device * dst,struct net_device * src)337 static inline void eth_hw_addr_inherit(struct net_device *dst,
338 				       struct net_device *src)
339 {
340 	dst->addr_assign_type = src->addr_assign_type;
341 	eth_hw_addr_set(dst, src->dev_addr);
342 }
343 
344 /**
345  * ether_addr_equal - Compare two Ethernet addresses
346  * @addr1: Pointer to a six-byte array containing the Ethernet address
347  * @addr2: Pointer other six-byte array containing the Ethernet address
348  *
349  * Compare two Ethernet addresses, returns true if equal
350  *
351  * Please note: addr1 & addr2 must both be aligned to u16.
352  */
ether_addr_equal(const u8 * addr1,const u8 * addr2)353 static inline bool ether_addr_equal(const u8 *addr1, const u8 *addr2)
354 {
355 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
356 	u32 fold = ((*(const u32 *)addr1) ^ (*(const u32 *)addr2)) |
357 		   ((*(const u16 *)(addr1 + 4)) ^ (*(const u16 *)(addr2 + 4)));
358 
359 	return fold == 0;
360 #else
361 	const u16 *a = (const u16 *)addr1;
362 	const u16 *b = (const u16 *)addr2;
363 
364 	return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0;
365 #endif
366 }
367 
368 /**
369  * ether_addr_equal_64bits - Compare two Ethernet addresses
370  * @addr1: Pointer to an array of 8 bytes
371  * @addr2: Pointer to an other array of 8 bytes
372  *
373  * Compare two Ethernet addresses, returns true if equal, false otherwise.
374  *
375  * The function doesn't need any conditional branches and possibly uses
376  * word memory accesses on CPU allowing cheap unaligned memory reads.
377  * arrays = { byte1, byte2, byte3, byte4, byte5, byte6, pad1, pad2 }
378  *
379  * Please note that alignment of addr1 & addr2 are only guaranteed to be 16 bits.
380  */
381 
ether_addr_equal_64bits(const u8 * addr1,const u8 * addr2)382 static inline bool ether_addr_equal_64bits(const u8 *addr1, const u8 *addr2)
383 {
384 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
385 	u64 fold = (*(const u64 *)addr1) ^ (*(const u64 *)addr2);
386 
387 #ifdef __BIG_ENDIAN
388 	return (fold >> 16) == 0;
389 #else
390 	return (fold << 16) == 0;
391 #endif
392 #else
393 	return ether_addr_equal(addr1, addr2);
394 #endif
395 }
396 
397 /**
398  * ether_addr_equal_unaligned - Compare two not u16 aligned Ethernet addresses
399  * @addr1: Pointer to a six-byte array containing the Ethernet address
400  * @addr2: Pointer other six-byte array containing the Ethernet address
401  *
402  * Compare two Ethernet addresses, returns true if equal
403  *
404  * Please note: Use only when any Ethernet address may not be u16 aligned.
405  */
ether_addr_equal_unaligned(const u8 * addr1,const u8 * addr2)406 static inline bool ether_addr_equal_unaligned(const u8 *addr1, const u8 *addr2)
407 {
408 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
409 	return ether_addr_equal(addr1, addr2);
410 #else
411 	return memcmp(addr1, addr2, ETH_ALEN) == 0;
412 #endif
413 }
414 
415 /**
416  * ether_addr_equal_masked - Compare two Ethernet addresses with a mask
417  * @addr1: Pointer to a six-byte array containing the 1st Ethernet address
418  * @addr2: Pointer to a six-byte array containing the 2nd Ethernet address
419  * @mask: Pointer to a six-byte array containing the Ethernet address bitmask
420  *
421  * Compare two Ethernet addresses with a mask, returns true if for every bit
422  * set in the bitmask the equivalent bits in the ethernet addresses are equal.
423  * Using a mask with all bits set is a slower ether_addr_equal.
424  */
ether_addr_equal_masked(const u8 * addr1,const u8 * addr2,const u8 * mask)425 static inline bool ether_addr_equal_masked(const u8 *addr1, const u8 *addr2,
426 					   const u8 *mask)
427 {
428 	int i;
429 
430 	for (i = 0; i < ETH_ALEN; i++) {
431 		if ((addr1[i] ^ addr2[i]) & mask[i])
432 			return false;
433 	}
434 
435 	return true;
436 }
437 
ether_addr_is_ipv4_mcast(const u8 * addr)438 static inline bool ether_addr_is_ipv4_mcast(const u8 *addr)
439 {
440 	u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
441 
442 	return ether_addr_equal_masked(addr, eth_ipv4_mcast_addr_base, mask);
443 }
444 
ether_addr_is_ipv6_mcast(const u8 * addr)445 static inline bool ether_addr_is_ipv6_mcast(const u8 *addr)
446 {
447 	u8 mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
448 
449 	return ether_addr_equal_masked(addr, eth_ipv6_mcast_addr_base, mask);
450 }
451 
ether_addr_is_ip_mcast(const u8 * addr)452 static inline bool ether_addr_is_ip_mcast(const u8 *addr)
453 {
454 	return ether_addr_is_ipv4_mcast(addr) ||
455 		ether_addr_is_ipv6_mcast(addr);
456 }
457 
458 /**
459  * ether_addr_to_u64 - Convert an Ethernet address into a u64 value.
460  * @addr: Pointer to a six-byte array containing the Ethernet address
461  *
462  * Return: a u64 value of the address
463  */
ether_addr_to_u64(const u8 * addr)464 static inline u64 ether_addr_to_u64(const u8 *addr)
465 {
466 	u64 u = 0;
467 	int i;
468 
469 	for (i = 0; i < ETH_ALEN; i++)
470 		u = u << 8 | addr[i];
471 
472 	return u;
473 }
474 
475 /**
476  * u64_to_ether_addr - Convert a u64 to an Ethernet address.
477  * @u: u64 to convert to an Ethernet MAC address
478  * @addr: Pointer to a six-byte array to contain the Ethernet address
479  */
u64_to_ether_addr(u64 u,u8 * addr)480 static inline void u64_to_ether_addr(u64 u, u8 *addr)
481 {
482 	int i;
483 
484 	for (i = ETH_ALEN - 1; i >= 0; i--) {
485 		addr[i] = u & 0xff;
486 		u = u >> 8;
487 	}
488 }
489 
490 /**
491  * eth_addr_dec - Decrement the given MAC address
492  *
493  * @addr: Pointer to a six-byte array containing Ethernet address to decrement
494  */
eth_addr_dec(u8 * addr)495 static inline void eth_addr_dec(u8 *addr)
496 {
497 	u64 u = ether_addr_to_u64(addr);
498 
499 	u--;
500 	u64_to_ether_addr(u, addr);
501 }
502 
503 /**
504  * eth_addr_inc() - Increment the given MAC address.
505  * @addr: Pointer to a six-byte array containing Ethernet address to increment.
506  */
eth_addr_inc(u8 * addr)507 static inline void eth_addr_inc(u8 *addr)
508 {
509 	u64 u = ether_addr_to_u64(addr);
510 
511 	u++;
512 	u64_to_ether_addr(u, addr);
513 }
514 
515 /**
516  * eth_addr_add() - Add (or subtract) an offset to/from the given MAC address.
517  *
518  * @offset: Offset to add.
519  * @addr: Pointer to a six-byte array containing Ethernet address to increment.
520  */
eth_addr_add(u8 * addr,long offset)521 static inline void eth_addr_add(u8 *addr, long offset)
522 {
523 	u64 u = ether_addr_to_u64(addr);
524 
525 	u += offset;
526 	u64_to_ether_addr(u, addr);
527 }
528 
529 /**
530  * is_etherdev_addr - Tell if given Ethernet address belongs to the device.
531  * @dev: Pointer to a device structure
532  * @addr: Pointer to a six-byte array containing the Ethernet address
533  *
534  * Compare passed address with all addresses of the device. Return true if the
535  * address if one of the device addresses.
536  *
537  * Note that this function calls ether_addr_equal_64bits() so take care of
538  * the right padding.
539  */
is_etherdev_addr(const struct net_device * dev,const u8 addr[6+2])540 static inline bool is_etherdev_addr(const struct net_device *dev,
541 				    const u8 addr[6 + 2])
542 {
543 	struct netdev_hw_addr *ha;
544 	bool res = false;
545 
546 	rcu_read_lock();
547 	for_each_dev_addr(dev, ha) {
548 		res = ether_addr_equal_64bits(addr, ha->addr);
549 		if (res)
550 			break;
551 	}
552 	rcu_read_unlock();
553 	return res;
554 }
555 #endif	/* __KERNEL__ */
556 
557 /**
558  * compare_ether_header - Compare two Ethernet headers
559  * @a: Pointer to Ethernet header
560  * @b: Pointer to Ethernet header
561  *
562  * Compare two Ethernet headers, returns 0 if equal.
563  * This assumes that the network header (i.e., IP header) is 4-byte
564  * aligned OR the platform can handle unaligned access.  This is the
565  * case for all packets coming into netif_receive_skb or similar
566  * entry points.
567  */
568 
compare_ether_header(const void * a,const void * b)569 static inline unsigned long compare_ether_header(const void *a, const void *b)
570 {
571 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
572 	unsigned long fold;
573 
574 	/*
575 	 * We want to compare 14 bytes:
576 	 *  [a0 ... a13] ^ [b0 ... b13]
577 	 * Use two long XOR, ORed together, with an overlap of two bytes.
578 	 *  [a0  a1  a2  a3  a4  a5  a6  a7 ] ^ [b0  b1  b2  b3  b4  b5  b6  b7 ] |
579 	 *  [a6  a7  a8  a9  a10 a11 a12 a13] ^ [b6  b7  b8  b9  b10 b11 b12 b13]
580 	 * This means the [a6 a7] ^ [b6 b7] part is done two times.
581 	*/
582 	fold = *(unsigned long *)a ^ *(unsigned long *)b;
583 	fold |= *(unsigned long *)(a + 6) ^ *(unsigned long *)(b + 6);
584 	return fold;
585 #else
586 	u32 *a32 = (u32 *)((u8 *)a + 2);
587 	u32 *b32 = (u32 *)((u8 *)b + 2);
588 
589 	return (*(u16 *)a ^ *(u16 *)b) | (a32[0] ^ b32[0]) |
590 	       (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]);
591 #endif
592 }
593 
594 /**
595  * eth_hw_addr_gen - Generate and assign Ethernet address to a port
596  * @dev: pointer to port's net_device structure
597  * @base_addr: base Ethernet address
598  * @id: offset to add to the base address
599  *
600  * Generate a MAC address using a base address and an offset and assign it
601  * to a net_device. Commonly used by switch drivers which need to compute
602  * addresses for all their ports. addr_assign_type is not changed.
603  */
eth_hw_addr_gen(struct net_device * dev,const u8 * base_addr,unsigned int id)604 static inline void eth_hw_addr_gen(struct net_device *dev, const u8 *base_addr,
605 				   unsigned int id)
606 {
607 	u64 u = ether_addr_to_u64(base_addr);
608 	u8 addr[ETH_ALEN];
609 
610 	u += id;
611 	u64_to_ether_addr(u, addr);
612 	eth_hw_addr_set(dev, addr);
613 }
614 
615 /**
616  * eth_skb_pkt_type - Assign packet type if destination address does not match
617  * @skb: Assigned a packet type if address does not match @dev address
618  * @dev: Network device used to compare packet address against
619  *
620  * If the destination MAC address of the packet does not match the network
621  * device address, assign an appropriate packet type.
622  */
eth_skb_pkt_type(struct sk_buff * skb,const struct net_device * dev)623 static inline void eth_skb_pkt_type(struct sk_buff *skb,
624 				    const struct net_device *dev)
625 {
626 	const struct ethhdr *eth = eth_hdr(skb);
627 
628 	if (unlikely(!ether_addr_equal_64bits(eth->h_dest, dev->dev_addr))) {
629 		if (unlikely(is_multicast_ether_addr_64bits(eth->h_dest))) {
630 			if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
631 				skb->pkt_type = PACKET_BROADCAST;
632 			else
633 				skb->pkt_type = PACKET_MULTICAST;
634 		} else {
635 			skb->pkt_type = PACKET_OTHERHOST;
636 		}
637 	}
638 }
639 
eth_skb_pull_mac(struct sk_buff * skb)640 static inline struct ethhdr *eth_skb_pull_mac(struct sk_buff *skb)
641 {
642 	struct ethhdr *eth = (struct ethhdr *)skb->data;
643 
644 	skb_pull_inline(skb, ETH_HLEN);
645 	return eth;
646 }
647 
648 /**
649  * eth_skb_pad - Pad buffer to minimum number of octets for Ethernet frame
650  * @skb: Buffer to pad
651  *
652  * An Ethernet frame should have a minimum size of 60 bytes.  This function
653  * takes short frames and pads them with zeros up to the 60 byte limit.
654  */
eth_skb_pad(struct sk_buff * skb)655 static inline int eth_skb_pad(struct sk_buff *skb)
656 {
657 	return skb_put_padto(skb, ETH_ZLEN);
658 }
659 
660 #endif	/* _LINUX_ETHERDEVICE_H */
661