etherdevice.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. NET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Definitions for the Ethernet handlers.
  7. *
  8. * Version: @(#)eth.h 1.0.4 05/13/93
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. *
  13. * Relocated to include/linux where it belongs by Alan Cox
  14. * <gw4pts@gw4pts.ampr.org>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. *
  21. * WARNING: This move may well be temporary. This file will get merged with others RSN.
  22. *
  23. */
  24. #ifndef _LINUX_ETHERDEVICE_H
  25. #define _LINUX_ETHERDEVICE_H
  26. #include <linux/if_ether.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/random.h>
  29. #include <asm/unaligned.h>
  30. #ifdef __KERNEL__
  31. extern __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
  32. extern const struct header_ops eth_header_ops;
  33. extern int eth_header(struct sk_buff *skb, struct net_device *dev,
  34. unsigned short type,
  35. const void *daddr, const void *saddr, unsigned len);
  36. extern int eth_rebuild_header(struct sk_buff *skb);
  37. extern int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr);
  38. extern int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh);
  39. extern void eth_header_cache_update(struct hh_cache *hh,
  40. const struct net_device *dev,
  41. const unsigned char *haddr);
  42. extern int eth_mac_addr(struct net_device *dev, void *p);
  43. extern int eth_change_mtu(struct net_device *dev, int new_mtu);
  44. extern int eth_validate_addr(struct net_device *dev);
  45. extern struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count);
  46. #define alloc_etherdev(sizeof_priv) alloc_etherdev_mq(sizeof_priv, 1)
  47. /**
  48. * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
  49. * @addr: Pointer to a six-byte array containing the Ethernet address
  50. *
  51. * Return true if the address is all zeroes.
  52. */
  53. static inline int is_zero_ether_addr(const u8 *addr)
  54. {
  55. return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
  56. }
  57. /**
  58. * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
  59. * @addr: Pointer to a six-byte array containing the Ethernet address
  60. *
  61. * Return true if the address is a multicast address.
  62. * By definition the broadcast address is also a multicast address.
  63. */
  64. static inline int is_multicast_ether_addr(const u8 *addr)
  65. {
  66. return (0x01 & addr[0]);
  67. }
  68. /**
  69. * is_local_ether_addr - Determine if the Ethernet address is locally-assigned one (IEEE 802).
  70. * @addr: Pointer to a six-byte array containing the Ethernet address
  71. *
  72. * Return true if the address is a local address.
  73. */
  74. static inline int is_local_ether_addr(const u8 *addr)
  75. {
  76. return (0x02 & addr[0]);
  77. }
  78. /**
  79. * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
  80. * @addr: Pointer to a six-byte array containing the Ethernet address
  81. *
  82. * Return true if the address is the broadcast address.
  83. */
  84. static inline int is_broadcast_ether_addr(const u8 *addr)
  85. {
  86. return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
  87. }
  88. /**
  89. * is_valid_ether_addr - Determine if the given Ethernet address is valid
  90. * @addr: Pointer to a six-byte array containing the Ethernet address
  91. *
  92. * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
  93. * a multicast address, and is not FF:FF:FF:FF:FF:FF.
  94. *
  95. * Return true if the address is valid.
  96. */
  97. static inline int is_valid_ether_addr(const u8 *addr)
  98. {
  99. /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
  100. * explicitly check for it here. */
  101. return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
  102. }
  103. /**
  104. * random_ether_addr - Generate software assigned random Ethernet address
  105. * @addr: Pointer to a six-byte array containing the Ethernet address
  106. *
  107. * Generate a random Ethernet address (MAC) that is not multicast
  108. * and has the local assigned bit set.
  109. */
  110. static inline void random_ether_addr(u8 *addr)
  111. {
  112. get_random_bytes (addr, ETH_ALEN);
  113. addr [0] &= 0xfe; /* clear multicast bit */
  114. addr [0] |= 0x02; /* set local assignment bit (IEEE802) */
  115. }
  116. /**
  117. * compare_ether_addr - Compare two Ethernet addresses
  118. * @addr1: Pointer to a six-byte array containing the Ethernet address
  119. * @addr2: Pointer other six-byte array containing the Ethernet address
  120. *
  121. * Compare two ethernet addresses, returns 0 if equal
  122. */
  123. static inline unsigned compare_ether_addr(const u8 *addr1, const u8 *addr2)
  124. {
  125. const u16 *a = (const u16 *) addr1;
  126. const u16 *b = (const u16 *) addr2;
  127. BUILD_BUG_ON(ETH_ALEN != 6);
  128. return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) != 0;
  129. }
  130. static inline unsigned long zap_last_2bytes(unsigned long value)
  131. {
  132. #ifdef __BIG_ENDIAN
  133. return value >> 16;
  134. #else
  135. return value << 16;
  136. #endif
  137. }
  138. /**
  139. * compare_ether_addr_64bits - Compare two Ethernet addresses
  140. * @addr1: Pointer to an array of 8 bytes
  141. * @addr2: Pointer to an other array of 8 bytes
  142. *
  143. * Compare two ethernet addresses, returns 0 if equal.
  144. * Same result than "memcmp(addr1, addr2, ETH_ALEN)" but without conditional
  145. * branches, and possibly long word memory accesses on CPU allowing cheap
  146. * unaligned memory reads.
  147. * arrays = { byte1, byte2, byte3, byte4, byte6, byte7, pad1, pad2}
  148. *
  149. * Please note that alignment of addr1 & addr2 is only guaranted to be 16 bits.
  150. */
  151. static inline unsigned compare_ether_addr_64bits(const u8 addr1[6+2],
  152. const u8 addr2[6+2])
  153. {
  154. #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  155. unsigned long fold = ((*(unsigned long *)addr1) ^
  156. (*(unsigned long *)addr2));
  157. if (sizeof(fold) == 8)
  158. return zap_last_2bytes(fold) != 0;
  159. fold |= zap_last_2bytes((*(unsigned long *)(addr1 + 4)) ^
  160. (*(unsigned long *)(addr2 + 4)));
  161. return fold != 0;
  162. #else
  163. return compare_ether_addr(addr1, addr2);
  164. #endif
  165. }
  166. /**
  167. * is_etherdev_addr - Tell if given Ethernet address belongs to the device.
  168. * @dev: Pointer to a device structure
  169. * @addr: Pointer to a six-byte array containing the Ethernet address
  170. *
  171. * Compare passed address with all addresses of the device. Return true if the
  172. * address if one of the device addresses.
  173. *
  174. * Note that this function calls compare_ether_addr_64bits() so take care of
  175. * the right padding.
  176. */
  177. static inline bool is_etherdev_addr(const struct net_device *dev,
  178. const u8 addr[6 + 2])
  179. {
  180. struct netdev_hw_addr *ha;
  181. int res = 1;
  182. rcu_read_lock();
  183. for_each_dev_addr(dev, ha) {
  184. res = compare_ether_addr_64bits(addr, ha->addr);
  185. if (!res)
  186. break;
  187. }
  188. rcu_read_unlock();
  189. return !res;
  190. }
  191. #endif /* __KERNEL__ */
  192. /**
  193. * compare_ether_header - Compare two Ethernet headers
  194. * @a: Pointer to Ethernet header
  195. * @b: Pointer to Ethernet header
  196. *
  197. * Compare two ethernet headers, returns 0 if equal.
  198. * This assumes that the network header (i.e., IP header) is 4-byte
  199. * aligned OR the platform can handle unaligned access. This is the
  200. * case for all packets coming into netif_receive_skb or similar
  201. * entry points.
  202. */
  203. static inline int compare_ether_header(const void *a, const void *b)
  204. {
  205. u32 *a32 = (u32 *)((u8 *)a + 2);
  206. u32 *b32 = (u32 *)((u8 *)b + 2);
  207. return (*(u16 *)a ^ *(u16 *)b) | (a32[0] ^ b32[0]) |
  208. (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]);
  209. }
  210. #endif /* _LINUX_ETHERDEVICE_H */