if_ether.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* $OpenBSD: if_ether.h,v 1.57 2015/06/23 13:20:17 mpi Exp $ */
  2. /* $NetBSD: if_ether.h,v 1.22 1996/05/11 13:00:00 mycroft Exp $ */
  3. /*
  4. * Copyright (c) 1982, 1986, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * @(#)if_ether.h 8.1 (Berkeley) 6/10/93
  32. */
  33. #ifndef _NETINET_IF_ETHER_H_
  34. #define _NETINET_IF_ETHER_H_
  35. /*
  36. * Some basic Ethernet constants.
  37. */
  38. #define ETHER_ADDR_LEN 6 /* Ethernet address length */
  39. #define ETHER_TYPE_LEN 2 /* Ethernet type field length */
  40. #define ETHER_CRC_LEN 4 /* Ethernet CRC length */
  41. #define ETHER_HDR_LEN ((ETHER_ADDR_LEN * 2) + ETHER_TYPE_LEN)
  42. #define ETHER_MIN_LEN 64 /* Minimum frame length, CRC included */
  43. #define ETHER_MAX_LEN 1518 /* Maximum frame length, CRC included */
  44. #define ETHER_MAX_DIX_LEN 1536 /* Maximum DIX frame length */
  45. /*
  46. * Some Ethernet extensions.
  47. */
  48. #define ETHER_VLAN_ENCAP_LEN 4 /* len of 802.1Q VLAN encapsulation */
  49. /*
  50. * Mbuf adjust factor to force 32-bit alignment of IP header.
  51. * Drivers should do m_adj(m, ETHER_ALIGN) when setting up a
  52. * receive so the upper layers get the IP header properly aligned
  53. * past the 14-byte Ethernet header.
  54. */
  55. #define ETHER_ALIGN 2 /* driver adjust for IP hdr alignment */
  56. /*
  57. * Ethernet address - 6 octets
  58. */
  59. struct ether_addr {
  60. u_int8_t ether_addr_octet[ETHER_ADDR_LEN];
  61. };
  62. /*
  63. * The length of the combined header.
  64. */
  65. struct ether_header {
  66. u_int8_t ether_dhost[ETHER_ADDR_LEN];
  67. u_int8_t ether_shost[ETHER_ADDR_LEN];
  68. u_int16_t ether_type;
  69. };
  70. #include <net/ethertypes.h>
  71. #define ETHER_IS_MULTICAST(addr) (*(addr) & 0x01) /* is address mcast/bcast? */
  72. #define ETHERMTU (ETHER_MAX_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
  73. #define ETHERMIN (ETHER_MIN_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
  74. /*
  75. * Ethernet CRC32 polynomials (big- and little-endian verions).
  76. */
  77. #define ETHER_CRC_POLY_LE 0xedb88320
  78. #define ETHER_CRC_POLY_BE 0x04c11db6
  79. #ifdef _KERNEL
  80. /*
  81. * Macro to map an IP multicast address to an Ethernet multicast address.
  82. * The high-order 25 bits of the Ethernet address are statically assigned,
  83. * and the low-order 23 bits are taken from the low end of the IP address.
  84. */
  85. #define ETHER_MAP_IP_MULTICAST(ipaddr, enaddr) \
  86. /* struct in_addr *ipaddr; */ \
  87. /* u_int8_t enaddr[ETHER_ADDR_LEN]; */ \
  88. do { \
  89. (enaddr)[0] = 0x01; \
  90. (enaddr)[1] = 0x00; \
  91. (enaddr)[2] = 0x5e; \
  92. (enaddr)[3] = ((u_int8_t *)ipaddr)[1] & 0x7f; \
  93. (enaddr)[4] = ((u_int8_t *)ipaddr)[2]; \
  94. (enaddr)[5] = ((u_int8_t *)ipaddr)[3]; \
  95. } while (/* CONSTCOND */ 0)
  96. /*
  97. * Macro to map an IPv6 multicast address to an Ethernet multicast address.
  98. * The high-order 16 bits of the Ethernet address are statically assigned,
  99. * and the low-order 32 bits are taken from the low end of the IPv6 address.
  100. */
  101. #define ETHER_MAP_IPV6_MULTICAST(ip6addr, enaddr) \
  102. /* struct in6_addr *ip6addr; */ \
  103. /* u_int8_t enaddr[ETHER_ADDR_LEN]; */ \
  104. do { \
  105. (enaddr)[0] = 0x33; \
  106. (enaddr)[1] = 0x33; \
  107. (enaddr)[2] = ((u_int8_t *)ip6addr)[12]; \
  108. (enaddr)[3] = ((u_int8_t *)ip6addr)[13]; \
  109. (enaddr)[4] = ((u_int8_t *)ip6addr)[14]; \
  110. (enaddr)[5] = ((u_int8_t *)ip6addr)[15]; \
  111. } while (/* CONSTCOND */ 0)
  112. void ether_fakeaddr(struct ifnet *);
  113. #endif
  114. /*
  115. * Ethernet Address Resolution Protocol.
  116. *
  117. * See RFC 826 for protocol description. Structure below is adapted
  118. * to resolving internet addresses. Field names used correspond to
  119. * RFC 826.
  120. */
  121. struct ether_arp {
  122. struct arphdr ea_hdr; /* fixed-size header */
  123. u_int8_t arp_sha[ETHER_ADDR_LEN]; /* sender hardware address */
  124. u_int8_t arp_spa[4]; /* sender protocol address */
  125. u_int8_t arp_tha[ETHER_ADDR_LEN]; /* target hardware address */
  126. u_int8_t arp_tpa[4]; /* target protocol address */
  127. };
  128. #define arp_hrd ea_hdr.ar_hrd
  129. #define arp_pro ea_hdr.ar_pro
  130. #define arp_hln ea_hdr.ar_hln
  131. #define arp_pln ea_hdr.ar_pln
  132. #define arp_op ea_hdr.ar_op
  133. #ifdef _KERNEL
  134. #include <net/if_var.h> /* for "struct ifnet" */
  135. /*
  136. * Structure shared between the ethernet driver modules and
  137. * the address resolution code. For example, each ec_softc or il_softc
  138. * begins with this structure.
  139. */
  140. struct arpcom {
  141. struct ifnet ac_if; /* network-visible interface */
  142. u_int8_t ac_enaddr[ETHER_ADDR_LEN]; /* ethernet hardware address */
  143. char ac__pad[2]; /* pad for some machines */
  144. LIST_HEAD(, ether_multi) ac_multiaddrs; /* list of multicast addrs */
  145. int ac_multicnt; /* length of ac_multiaddrs */
  146. int ac_multirangecnt; /* number of mcast ranges */
  147. };
  148. #endif
  149. struct sockaddr_inarp {
  150. u_int8_t sin_len;
  151. u_int8_t sin_family;
  152. u_int16_t sin_port;
  153. struct in_addr sin_addr;
  154. struct in_addr sin_srcaddr;
  155. u_int16_t sin_tos;
  156. u_int16_t sin_other;
  157. #define SIN_PROXY 1
  158. };
  159. /*
  160. * IP and ethernet specific routing flags
  161. */
  162. #define RTF_USETRAILERS RTF_PROTO1 /* use trailers */
  163. #define RTF_ANNOUNCE RTF_PROTO2 /* announce new arp entry */
  164. #define RTF_PERMANENT_ARP RTF_PROTO3 /* only manual overwrite of entry */
  165. #ifdef _KERNEL
  166. extern u_int8_t etherbroadcastaddr[ETHER_ADDR_LEN];
  167. extern u_int8_t ether_ipmulticast_min[ETHER_ADDR_LEN];
  168. extern u_int8_t ether_ipmulticast_max[ETHER_ADDR_LEN];
  169. extern struct niqueue arpintrq;
  170. void arpwhohas(struct arpcom *, struct in_addr *);
  171. void arpintr(void);
  172. int arpresolve(struct ifnet *,
  173. struct rtentry *, struct mbuf *, struct sockaddr *, u_char *);
  174. void arp_ifinit(struct arpcom *, struct ifaddr *);
  175. void arp_rtrequest(int, struct rtentry *);
  176. int ether_addmulti(struct ifreq *, struct arpcom *);
  177. int ether_delmulti(struct ifreq *, struct arpcom *);
  178. int ether_multiaddr(struct sockaddr *, u_int8_t[], u_int8_t[]);
  179. /*
  180. * Ethernet multicast address structure. There is one of these for each
  181. * multicast address or range of multicast addresses that we are supposed
  182. * to listen to on a particular interface. They are kept in a linked list,
  183. * rooted in the interface's arpcom structure. (This really has nothing to
  184. * do with ARP, or with the Internet address family, but this appears to be
  185. * the minimally-disrupting place to put it.)
  186. */
  187. struct ether_multi {
  188. u_int8_t enm_addrlo[ETHER_ADDR_LEN]; /* low or only address of range */
  189. u_int8_t enm_addrhi[ETHER_ADDR_LEN]; /* high or only address of range */
  190. struct arpcom *enm_ac; /* back pointer to arpcom */
  191. u_int enm_refcount; /* no. claims to this addr/range */
  192. LIST_ENTRY(ether_multi) enm_list;
  193. };
  194. /*
  195. * Structure used by macros below to remember position when stepping through
  196. * all of the ether_multi records.
  197. */
  198. struct ether_multistep {
  199. struct ether_multi *e_enm;
  200. };
  201. /*
  202. * Macro for looking up the ether_multi record for a given range of Ethernet
  203. * multicast addresses connected to a given arpcom structure. If no matching
  204. * record is found, "enm" returns NULL.
  205. */
  206. #define ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm) \
  207. /* u_int8_t addrlo[ETHER_ADDR_LEN]; */ \
  208. /* u_int8_t addrhi[ETHER_ADDR_LEN]; */ \
  209. /* struct arpcom *ac; */ \
  210. /* struct ether_multi *enm; */ \
  211. do { \
  212. for ((enm) = LIST_FIRST(&(ac)->ac_multiaddrs); \
  213. (enm) != NULL && \
  214. (memcmp((enm)->enm_addrlo, (addrlo), ETHER_ADDR_LEN) != 0 ||\
  215. memcmp((enm)->enm_addrhi, (addrhi), ETHER_ADDR_LEN) != 0); \
  216. (enm) = LIST_NEXT((enm), enm_list)); \
  217. } while (/* CONSTCOND */ 0)
  218. /*
  219. * Macro to step through all of the ether_multi records, one at a time.
  220. * The current position is remembered in "step", which the caller must
  221. * provide. ETHER_FIRST_MULTI(), below, must be called to initialize "step"
  222. * and get the first record. Both macros return a NULL "enm" when there
  223. * are no remaining records.
  224. */
  225. #define ETHER_NEXT_MULTI(step, enm) \
  226. /* struct ether_multistep step; */ \
  227. /* struct ether_multi *enm; */ \
  228. do { \
  229. if (((enm) = (step).e_enm) != NULL) \
  230. (step).e_enm = LIST_NEXT((enm), enm_list); \
  231. } while (/* CONSTCOND */ 0)
  232. #define ETHER_FIRST_MULTI(step, ac, enm) \
  233. /* struct ether_multistep step; */ \
  234. /* struct arpcom *ac; */ \
  235. /* struct ether_multi *enm; */ \
  236. do { \
  237. (step).e_enm = LIST_FIRST(&(ac)->ac_multiaddrs); \
  238. ETHER_NEXT_MULTI((step), (enm)); \
  239. } while (/* CONSTCOND */ 0)
  240. #ifdef NFSCLIENT
  241. extern struct ifnet *revarp_ifp;
  242. #endif /* NFSCLIENT */
  243. void arprequest(struct ifnet *, u_int32_t *, u_int32_t *, u_int8_t *);
  244. int arpproxy(struct in_addr, u_int);
  245. void revarpinput(struct mbuf *);
  246. void in_revarpinput(struct mbuf *);
  247. void revarprequest(struct ifnet *);
  248. int revarpwhoarewe(struct ifnet *, struct in_addr *, struct in_addr *);
  249. int revarpwhoami(struct in_addr *, struct ifnet *);
  250. int db_show_arptab(void);
  251. u_int32_t ether_crc32_le_update(u_int32_t crc, const u_int8_t *, size_t);
  252. u_int32_t ether_crc32_be_update(u_int32_t crc, const u_int8_t *, size_t);
  253. u_int32_t ether_crc32_le(const u_int8_t *, size_t);
  254. u_int32_t ether_crc32_be(const u_int8_t *, size_t);
  255. #else
  256. __BEGIN_DECLS
  257. char *ether_ntoa(struct ether_addr *);
  258. struct ether_addr *ether_aton(const char *);
  259. int ether_ntohost(char *, struct ether_addr *);
  260. int ether_hostton(const char *, struct ether_addr *);
  261. int ether_line(const char *, struct ether_addr *, char *);
  262. __END_DECLS
  263. #endif /* _KERNEL */
  264. #endif /* _NETINET_IF_ETHER_H_ */