etherdevice.h 18 KB

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