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, unsigned char *haddr);
46 int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh,
47 __be16 type);
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);
55
56 struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,
57 unsigned int rxqs);
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)
60
61 struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv,
62 unsigned int txqs,
63 unsigned int rxqs);
64 #define devm_alloc_etherdev(dev, sizeof_priv) devm_alloc_etherdev_mqs(dev, sizeof_priv, 1, 1)
65
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);
68
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
73
74 static const u8 eth_ipv4_mcast_addr_base[ETH_ALEN] __aligned(2) =
75 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
76
77 static const u8 eth_ipv6_mcast_addr_base[ETH_ALEN] __aligned(2) =
78 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
79
80 /**
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
83 *
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.
86 *
87 * Please note: addr must be aligned to u16.
88 */
is_link_local_ether_addr(const u8 * addr)89 static inline bool is_link_local_ether_addr(const u8 *addr)
90 {
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);
94
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;
98 #else
99 return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | ((a[2] ^ b[2]) & m)) == 0;
100 #endif
101 }
102
103 /**
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
106 *
107 * Return true if the address is all zeroes.
108 *
109 * Please note: addr must be aligned to u16.
110 */
is_zero_ether_addr(const u8 * addr)111 static inline bool is_zero_ether_addr(const u8 *addr)
112 {
113 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
114 return ((*(const u32 *)addr) | (*(const u16 *)(addr + 4))) == 0;
115 #else
116 return (*(const u16 *)(addr + 0) |
117 *(const u16 *)(addr + 2) |
118 *(const u16 *)(addr + 4)) == 0;
119 #endif
120 }
121
122 /**
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
125 *
126 * Return true if the address is a multicast address.
127 * By definition the broadcast address is also a multicast address.
128 */
is_multicast_ether_addr(const u8 * addr)129 static inline bool is_multicast_ether_addr(const u8 *addr)
130 {
131 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
132 u32 a = *(const u32 *)addr;
133 #else
134 u16 a = *(const u16 *)addr;
135 #endif
136 #ifdef __BIG_ENDIAN
137 return 0x01 & (a >> ((sizeof(a) * 8) - 8));
138 #else
139 return 0x01 & a;
140 #endif
141 }
142
is_multicast_ether_addr_64bits(const u8 * addr)143 static inline bool is_multicast_ether_addr_64bits(const u8 *addr)
144 {
145 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
146 #ifdef __BIG_ENDIAN
147 return 0x01 & ((*(const u64 *)addr) >> 56);
148 #else
149 return 0x01 & (*(const u64 *)addr);
150 #endif
151 #else
152 return is_multicast_ether_addr(addr);
153 #endif
154 }
155
156 /**
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
159 *
160 * Return true if the address is a local address.
161 */
is_local_ether_addr(const u8 * addr)162 static inline bool is_local_ether_addr(const u8 *addr)
163 {
164 return 0x02 & addr[0];
165 }
166
167 /**
168 * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
169 * @addr: Pointer to a six-byte array containing the Ethernet address
170 *
171 * Return true if the address is the broadcast address.
172 *
173 * Please note: addr must be aligned to u16.
174 */
is_broadcast_ether_addr(const u8 * addr)175 static inline bool is_broadcast_ether_addr(const u8 *addr)
176 {
177 return (*(const u16 *)(addr + 0) &
178 *(const u16 *)(addr + 2) &
179 *(const u16 *)(addr + 4)) == 0xffff;
180 }
181
182 /**
183 * is_unicast_ether_addr - Determine if the Ethernet address is unicast
184 * @addr: Pointer to a six-byte array containing the Ethernet address
185 *
186 * Return true if the address is a unicast address.
187 */
is_unicast_ether_addr(const u8 * addr)188 static inline bool is_unicast_ether_addr(const u8 *addr)
189 {
190 return !is_multicast_ether_addr(addr);
191 }
192
193 /**
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
196 *
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.
199 *
200 * Return true if the address is valid.
201 *
202 * Please note: addr must be aligned to u16.
203 */
is_valid_ether_addr(const u8 * addr)204 static inline bool is_valid_ether_addr(const u8 *addr)
205 {
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);
209 }
210
211 /**
212 * eth_proto_is_802_3 - Determine if a given Ethertype/length is a protocol
213 * @proto: Ethertype/length value to be tested
214 *
215 * Check that the value from the Ethertype/length field is a valid Ethertype.
216 *
217 * Return true if the valid is an 802.3 supported Ethertype.
218 */
eth_proto_is_802_3(__be16 proto)219 static inline bool eth_proto_is_802_3(__be16 proto)
220 {
221 #ifndef __BIG_ENDIAN
222 /* if CPU is little endian mask off bits representing LSB */
223 proto &= htons(0xFF00);
224 #endif
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);
227 }
228
229 /**
230 * eth_random_addr - Generate software assigned random Ethernet address
231 * @addr: Pointer to a six-byte array containing the Ethernet address
232 *
233 * Generate a random Ethernet address (MAC) that is not multicast
234 * and has the local assigned bit set.
235 */
eth_random_addr(u8 * addr)236 static inline void eth_random_addr(u8 *addr)
237 {
238 get_random_bytes(addr, ETH_ALEN);
239 addr[0] &= 0xfe; /* clear multicast bit */
240 addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
241 }
242
243 /**
244 * eth_broadcast_addr - Assign broadcast address
245 * @addr: Pointer to a six-byte array containing the Ethernet address
246 *
247 * Assign the broadcast address to the given address array.
248 */
eth_broadcast_addr(u8 * addr)249 static inline void eth_broadcast_addr(u8 *addr)
250 {
251 memset(addr, 0xff, ETH_ALEN);
252 }
253
254 /**
255 * eth_zero_addr - Assign zero address
256 * @addr: Pointer to a six-byte array containing the Ethernet address
257 *
258 * Assign the zero address to the given address array.
259 */
eth_zero_addr(u8 * addr)260 static inline void eth_zero_addr(u8 *addr)
261 {
262 memset(addr, 0x00, ETH_ALEN);
263 }
264
265 /**
266 * eth_hw_addr_random - Generate software assigned random Ethernet and
267 * set device flag
268 * @dev: pointer to net_device structure
269 *
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
272 * used by userspace.
273 */
eth_hw_addr_random(struct net_device * dev)274 static inline void eth_hw_addr_random(struct net_device *dev)
275 {
276 u8 addr[ETH_ALEN];
277
278 eth_random_addr(addr);
279 __dev_addr_set(dev, addr, ETH_ALEN);
280 dev->addr_assign_type = NET_ADDR_RANDOM;
281 }
282
283 /**
284 * eth_hw_addr_crc - Calculate CRC from netdev_hw_addr
285 * @ha: pointer to hardware address
286 *
287 * Calculate CRC from a hardware address as basis for filter hashes.
288 */
eth_hw_addr_crc(struct netdev_hw_addr * ha)289 static inline u32 eth_hw_addr_crc(struct netdev_hw_addr *ha)
290 {
291 return ether_crc(ETH_ALEN, ha->addr);
292 }
293
294 /**
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
298 *
299 * Please note: dst & src must both be aligned to u16.
300 */
ether_addr_copy(u8 * dst,const u8 * src)301 static inline void ether_addr_copy(u8 *dst, const u8 *src)
302 {
303 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
304 *(u32 *)dst = *(const u32 *)src;
305 *(u16 *)(dst + 4) = *(const u16 *)(src + 4);
306 #else
307 u16 *a = (u16 *)dst;
308 const u16 *b = (const u16 *)src;
309
310 a[0] = b[0];
311 a[1] = b[1];
312 a[2] = b[2];
313 #endif
314 }
315
316 /**
317 * eth_hw_addr_set - Assign Ethernet address to a net_device
318 * @dev: pointer to net_device structure
319 * @addr: address to assign
320 *
321 * Assign given address to the net_device, addr_assign_type is not changed.
322 */
eth_hw_addr_set(struct net_device * dev,const u8 * addr)323 static inline void eth_hw_addr_set(struct net_device *dev, const u8 *addr)
324 {
325 __dev_addr_set(dev, addr, ETH_ALEN);
326 }
327
328 /**
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
332 *
333 * Copy the Ethernet address from one net_device to another along with
334 * the address attributes (addr_assign_type).
335 */
eth_hw_addr_inherit(struct net_device * dst,struct net_device * src)336 static inline void eth_hw_addr_inherit(struct net_device *dst,
337 struct net_device *src)
338 {
339 dst->addr_assign_type = src->addr_assign_type;
340 eth_hw_addr_set(dst, src->dev_addr);
341 }
342
343 /**
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
347 *
348 * Compare two Ethernet addresses, returns true if equal
349 *
350 * Please note: addr1 & addr2 must both be aligned to u16.
351 */
ether_addr_equal(const u8 * addr1,const u8 * addr2)352 static inline bool ether_addr_equal(const u8 *addr1, const u8 *addr2)
353 {
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)));
357
358 return fold == 0;
359 #else
360 const u16 *a = (const u16 *)addr1;
361 const u16 *b = (const u16 *)addr2;
362
363 return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0;
364 #endif
365 }
366
367 /**
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
371 *
372 * Compare two Ethernet addresses, returns true if equal, false otherwise.
373 *
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 }
377 *
378 * Please note that alignment of addr1 & addr2 are only guaranteed to be 16 bits.
379 */
380
ether_addr_equal_64bits(const u8 * addr1,const u8 * addr2)381 static inline bool ether_addr_equal_64bits(const u8 *addr1, const u8 *addr2)
382 {
383 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
384 u64 fold = (*(const u64 *)addr1) ^ (*(const u64 *)addr2);
385
386 #ifdef __BIG_ENDIAN
387 return (fold >> 16) == 0;
388 #else
389 return (fold << 16) == 0;
390 #endif
391 #else
392 return ether_addr_equal(addr1, addr2);
393 #endif
394 }
395
396 /**
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
400 *
401 * Compare two Ethernet addresses, returns true if equal
402 *
403 * Please note: Use only when any Ethernet address may not be u16 aligned.
404 */
ether_addr_equal_unaligned(const u8 * addr1,const u8 * addr2)405 static inline bool ether_addr_equal_unaligned(const u8 *addr1, const u8 *addr2)
406 {
407 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
408 return ether_addr_equal(addr1, addr2);
409 #else
410 return memcmp(addr1, addr2, ETH_ALEN) == 0;
411 #endif
412 }
413
414 /**
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
419 *
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.
423 */
ether_addr_equal_masked(const u8 * addr1,const u8 * addr2,const u8 * mask)424 static inline bool ether_addr_equal_masked(const u8 *addr1, const u8 *addr2,
425 const u8 *mask)
426 {
427 int i;
428
429 for (i = 0; i < ETH_ALEN; i++) {
430 if ((addr1[i] ^ addr2[i]) & mask[i])
431 return false;
432 }
433
434 return true;
435 }
436
ether_addr_is_ipv4_mcast(const u8 * addr)437 static inline bool ether_addr_is_ipv4_mcast(const u8 *addr)
438 {
439 u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 };
440
441 return ether_addr_equal_masked(addr, eth_ipv4_mcast_addr_base, mask);
442 }
443
ether_addr_is_ipv6_mcast(const u8 * addr)444 static inline bool ether_addr_is_ipv6_mcast(const u8 *addr)
445 {
446 u8 mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 };
447
448 return ether_addr_equal_masked(addr, eth_ipv6_mcast_addr_base, mask);
449 }
450
ether_addr_is_ip_mcast(const u8 * addr)451 static inline bool ether_addr_is_ip_mcast(const u8 *addr)
452 {
453 return ether_addr_is_ipv4_mcast(addr) ||
454 ether_addr_is_ipv6_mcast(addr);
455 }
456
457 /**
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
460 *
461 * Return a u64 value of the address
462 */
ether_addr_to_u64(const u8 * addr)463 static inline u64 ether_addr_to_u64(const u8 *addr)
464 {
465 u64 u = 0;
466 int i;
467
468 for (i = 0; i < ETH_ALEN; i++)
469 u = u << 8 | addr[i];
470
471 return u;
472 }
473
474 /**
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
478 */
u64_to_ether_addr(u64 u,u8 * addr)479 static inline void u64_to_ether_addr(u64 u, u8 *addr)
480 {
481 int i;
482
483 for (i = ETH_ALEN - 1; i >= 0; i--) {
484 addr[i] = u & 0xff;
485 u = u >> 8;
486 }
487 }
488
489 /**
490 * eth_addr_dec - Decrement the given MAC address
491 *
492 * @addr: Pointer to a six-byte array containing Ethernet address to decrement
493 */
eth_addr_dec(u8 * addr)494 static inline void eth_addr_dec(u8 *addr)
495 {
496 u64 u = ether_addr_to_u64(addr);
497
498 u--;
499 u64_to_ether_addr(u, addr);
500 }
501
502 /**
503 * eth_addr_inc() - Increment the given MAC address.
504 * @addr: Pointer to a six-byte array containing Ethernet address to increment.
505 */
eth_addr_inc(u8 * addr)506 static inline void eth_addr_inc(u8 *addr)
507 {
508 u64 u = ether_addr_to_u64(addr);
509
510 u++;
511 u64_to_ether_addr(u, addr);
512 }
513
514 /**
515 * eth_addr_add() - Add (or subtract) an offset to/from the given MAC address.
516 *
517 * @offset: Offset to add.
518 * @addr: Pointer to a six-byte array containing Ethernet address to increment.
519 */
eth_addr_add(u8 * addr,long offset)520 static inline void eth_addr_add(u8 *addr, long offset)
521 {
522 u64 u = ether_addr_to_u64(addr);
523
524 u += offset;
525 u64_to_ether_addr(u, addr);
526 }
527
528 /**
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
532 *
533 * Compare passed address with all addresses of the device. Return true if the
534 * address if one of the device addresses.
535 *
536 * Note that this function calls ether_addr_equal_64bits() so take care of
537 * the right padding.
538 */
is_etherdev_addr(const struct net_device * dev,const u8 addr[6+2])539 static inline bool is_etherdev_addr(const struct net_device *dev,
540 const u8 addr[6 + 2])
541 {
542 struct netdev_hw_addr *ha;
543 bool res = false;
544
545 rcu_read_lock();
546 for_each_dev_addr(dev, ha) {
547 res = ether_addr_equal_64bits(addr, ha->addr);
548 if (res)
549 break;
550 }
551 rcu_read_unlock();
552 return res;
553 }
554 #endif /* __KERNEL__ */
555
556 /**
557 * compare_ether_header - Compare two Ethernet headers
558 * @a: Pointer to Ethernet header
559 * @b: Pointer to Ethernet header
560 *
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
565 * entry points.
566 */
567
compare_ether_header(const void * a,const void * b)568 static inline unsigned long compare_ether_header(const void *a, const void *b)
569 {
570 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
571 unsigned long fold;
572
573 /*
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.
580 */
581 fold = *(unsigned long *)a ^ *(unsigned long *)b;
582 fold |= *(unsigned long *)(a + 6) ^ *(unsigned long *)(b + 6);
583 return fold;
584 #else
585 u32 *a32 = (u32 *)((u8 *)a + 2);
586 u32 *b32 = (u32 *)((u8 *)b + 2);
587
588 return (*(u16 *)a ^ *(u16 *)b) | (a32[0] ^ b32[0]) |
589 (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]);
590 #endif
591 }
592
593 /**
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
598 *
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.
602 */
eth_hw_addr_gen(struct net_device * dev,const u8 * base_addr,unsigned int id)603 static inline void eth_hw_addr_gen(struct net_device *dev, const u8 *base_addr,
604 unsigned int id)
605 {
606 u64 u = ether_addr_to_u64(base_addr);
607 u8 addr[ETH_ALEN];
608
609 u += id;
610 u64_to_ether_addr(u, addr);
611 eth_hw_addr_set(dev, addr);
612 }
613
614 /**
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
618 *
619 * If the destination MAC address of the packet does not match the network
620 * device address, assign an appropriate packet type.
621 */
eth_skb_pkt_type(struct sk_buff * skb,const struct net_device * dev)622 static inline void eth_skb_pkt_type(struct sk_buff *skb,
623 const struct net_device *dev)
624 {
625 const struct ethhdr *eth = eth_hdr(skb);
626
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;
631 else
632 skb->pkt_type = PACKET_MULTICAST;
633 } else {
634 skb->pkt_type = PACKET_OTHERHOST;
635 }
636 }
637 }
638
eth_skb_pull_mac(struct sk_buff * skb)639 static inline struct ethhdr *eth_skb_pull_mac(struct sk_buff *skb)
640 {
641 struct ethhdr *eth = (struct ethhdr *)skb->data;
642
643 skb_pull_inline(skb, ETH_HLEN);
644 return eth;
645 }
646
647 /**
648 * eth_skb_pad - Pad buffer to minimum number of octets for Ethernet frame
649 * @skb: Buffer to pad
650 *
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.
653 */
eth_skb_pad(struct sk_buff * skb)654 static inline int eth_skb_pad(struct sk_buff *skb)
655 {
656 return skb_put_padto(skb, ETH_ZLEN);
657 }
658
659 #endif /* _LINUX_ETHERDEVICE_H */
660