etharp.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
  3. * Copyright (c) 2003-2004 Leon Woestenberg <leon.woestenberg@axon.tv>
  4. * Copyright (c) 2003-2004 Axon Digital Design B.V., The Netherlands.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  21. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  23. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  26. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. * OF SUCH DAMAGE.
  28. *
  29. * This file is part of the lwIP TCP/IP stack.
  30. *
  31. * Author: Adam Dunkels <adam@sics.se>
  32. *
  33. */
  34. #ifndef __NETIF_ETHARP_H__
  35. #define __NETIF_ETHARP_H__
  36. #include "lwip/opt.h"
  37. #if LWIP_ARP || LWIP_ETHERNET /* don't build if not configured for use in lwipopts.h */
  38. #include "lwip/pbuf.h"
  39. #include "lwip/ip_addr.h"
  40. #include "lwip/netif.h"
  41. #include "lwip/ip.h"
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #ifndef ETHARP_HWADDR_LEN
  46. #define ETHARP_HWADDR_LEN 6
  47. #endif
  48. #ifdef PACK_STRUCT_USE_INCLUDES
  49. # include "arch/bpstruct.h"
  50. #endif
  51. PACK_STRUCT_BEGIN
  52. struct eth_addr {
  53. PACK_STRUCT_FIELD(u8_t addr[ETHARP_HWADDR_LEN]);
  54. } PACK_STRUCT_STRUCT;
  55. PACK_STRUCT_END
  56. #ifdef PACK_STRUCT_USE_INCLUDES
  57. # include "arch/epstruct.h"
  58. #endif
  59. #ifdef PACK_STRUCT_USE_INCLUDES
  60. # include "arch/bpstruct.h"
  61. #endif
  62. PACK_STRUCT_BEGIN
  63. /** Ethernet header */
  64. struct eth_hdr {
  65. #if ETH_PAD_SIZE
  66. PACK_STRUCT_FIELD(u8_t padding[ETH_PAD_SIZE]);
  67. #endif
  68. PACK_STRUCT_FIELD(struct eth_addr dest);
  69. PACK_STRUCT_FIELD(struct eth_addr src);
  70. PACK_STRUCT_FIELD(u16_t type);
  71. } PACK_STRUCT_STRUCT;
  72. PACK_STRUCT_END
  73. #ifdef PACK_STRUCT_USE_INCLUDES
  74. # include "arch/epstruct.h"
  75. #endif
  76. #define SIZEOF_ETH_HDR (14 + ETH_PAD_SIZE)
  77. #if ETHARP_SUPPORT_VLAN
  78. #ifdef PACK_STRUCT_USE_INCLUDES
  79. # include "arch/bpstruct.h"
  80. #endif
  81. PACK_STRUCT_BEGIN
  82. /** VLAN header inserted between ethernet header and payload
  83. * if 'type' in ethernet header is ETHTYPE_VLAN.
  84. * See IEEE802.Q */
  85. struct eth_vlan_hdr {
  86. PACK_STRUCT_FIELD(u16_t tpid);
  87. PACK_STRUCT_FIELD(u16_t prio_vid);
  88. } PACK_STRUCT_STRUCT;
  89. PACK_STRUCT_END
  90. #ifdef PACK_STRUCT_USE_INCLUDES
  91. # include "arch/epstruct.h"
  92. #endif
  93. #define SIZEOF_VLAN_HDR 4
  94. #define VLAN_ID(vlan_hdr) (htons((vlan_hdr)->prio_vid) & 0xFFF)
  95. #endif /* ETHARP_SUPPORT_VLAN */
  96. #ifdef PACK_STRUCT_USE_INCLUDES
  97. # include "arch/bpstruct.h"
  98. #endif
  99. PACK_STRUCT_BEGIN
  100. /** the ARP message, see RFC 826 ("Packet format") */
  101. struct etharp_hdr {
  102. PACK_STRUCT_FIELD(u16_t hwtype);
  103. PACK_STRUCT_FIELD(u16_t proto);
  104. PACK_STRUCT_FIELD(u8_t hwlen);
  105. PACK_STRUCT_FIELD(u8_t protolen);
  106. PACK_STRUCT_FIELD(u16_t opcode);
  107. PACK_STRUCT_FIELD(struct eth_addr shwaddr);
  108. PACK_STRUCT_FIELD(struct ip_addr2 sipaddr);
  109. PACK_STRUCT_FIELD(struct eth_addr dhwaddr);
  110. PACK_STRUCT_FIELD(struct ip_addr2 dipaddr);
  111. } PACK_STRUCT_STRUCT;
  112. PACK_STRUCT_END
  113. #ifdef PACK_STRUCT_USE_INCLUDES
  114. # include "arch/epstruct.h"
  115. #endif
  116. #define SIZEOF_ETHARP_HDR 28
  117. #define SIZEOF_ETHARP_MINSIZE 46
  118. #define SIZEOF_ETHARP_PACKET (SIZEOF_ETH_HDR + SIZEOF_ETHARP_HDR)
  119. #define SIZEOF_ETHARP_WITHPAD (SIZEOF_ETH_HDR + SIZEOF_ETHARP_MINSIZE)
  120. /** 5 seconds period */
  121. #define ARP_TMR_INTERVAL 5000
  122. #define ETHTYPE_ARP 0x0806
  123. #define ETHTYPE_IP 0x0800
  124. #define ETHTYPE_VLAN 0x8100
  125. #define ETHTYPE_PPPOEDISC 0x8863 /* PPP Over Ethernet Discovery Stage */
  126. #define ETHTYPE_PPPOE 0x8864 /* PPP Over Ethernet Session Stage */
  127. #define ETHTYPE_PAE 0x888e
  128. /** MEMCPY-like macro to copy to/from struct eth_addr's that are local variables
  129. * or known to be 32-bit aligned within the protocol header. */
  130. #ifndef ETHADDR32_COPY
  131. #define ETHADDR32_COPY(src, dst) SMEMCPY(src, dst, ETHARP_HWADDR_LEN)
  132. #endif
  133. /** MEMCPY-like macro to copy to/from struct eth_addr's that are no local
  134. * variables and known to be 16-bit aligned within the protocol header. */
  135. #ifndef ETHADDR16_COPY
  136. #define ETHADDR16_COPY(src, dst) SMEMCPY(src, dst, ETHARP_HWADDR_LEN)
  137. #endif
  138. #if LWIP_ARP /* don't build if not configured for use in lwipopts.h */
  139. /** ARP message types (opcodes) */
  140. #define ARP_REQUEST 1
  141. #define ARP_REPLY 2
  142. /** Define this to 1 and define LWIP_ARP_FILTER_NETIF_FN(pbuf, netif, type)
  143. * to a filter function that returns the correct netif when using multiple
  144. * netifs on one hardware interface where the netif's low-level receive
  145. * routine cannot decide for the correct netif (e.g. when mapping multiple
  146. * IP addresses to one hardware interface).
  147. */
  148. #ifndef LWIP_ARP_FILTER_NETIF
  149. #define LWIP_ARP_FILTER_NETIF 0
  150. #endif
  151. #if ARP_QUEUEING
  152. /** struct for queueing outgoing packets for unknown address
  153. * defined here to be accessed by memp.h
  154. */
  155. struct etharp_q_entry {
  156. struct etharp_q_entry *next;
  157. struct pbuf *p;
  158. };
  159. #endif /* ARP_QUEUEING */
  160. #define etharp_init() /* Compatibility define, not init needed. */
  161. void etharp_tmr(void)ICACHE_FLASH_ATTR;
  162. s8_t etharp_find_addr(struct netif *netif, ip_addr_t *ipaddr,
  163. struct eth_addr **eth_ret, ip_addr_t **ip_ret)ICACHE_FLASH_ATTR;
  164. err_t etharp_output(struct netif *netif, struct pbuf *q, ip_addr_t *ipaddr)ICACHE_FLASH_ATTR;
  165. err_t etharp_query(struct netif *netif, ip_addr_t *ipaddr, struct pbuf *q)ICACHE_FLASH_ATTR;
  166. err_t etharp_request(struct netif *netif, ip_addr_t *ipaddr)ICACHE_FLASH_ATTR;
  167. /** For Ethernet network interfaces, we might want to send "gratuitous ARP";
  168. * this is an ARP packet sent by a node in order to spontaneously cause other
  169. * nodes to update an entry in their ARP cache.
  170. * From RFC 3220 "IP Mobility Support for IPv4" section 4.6. */
  171. #define etharp_gratuitous(netif) etharp_request((netif), &(netif)->ip_addr)
  172. void etharp_cleanup_netif(struct netif *netif);
  173. #if ETHARP_SUPPORT_STATIC_ENTRIES
  174. err_t etharp_add_static_entry(ip_addr_t *ipaddr, struct eth_addr *ethaddr)ICACHE_FLASH_ATTR;
  175. err_t etharp_remove_static_entry(ip_addr_t *ipaddr)ICACHE_FLASH_ATTR;
  176. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  177. #if LWIP_AUTOIP
  178. err_t etharp_raw(struct netif *netif, const struct eth_addr *ethsrc_addr,
  179. const struct eth_addr *ethdst_addr,
  180. const struct eth_addr *hwsrc_addr, const ip_addr_t *ipsrc_addr,
  181. const struct eth_addr *hwdst_addr, const ip_addr_t *ipdst_addr,
  182. const u16_t opcode)ICACHE_FLASH_ATTR;
  183. #endif /* LWIP_AUTOIP */
  184. #endif /* LWIP_ARP */
  185. err_t ethernet_input(struct pbuf *p, struct netif *netif)ICACHE_FLASH_ATTR;
  186. #define eth_addr_cmp(addr1, addr2) (memcmp((addr1)->addr, (addr2)->addr, ETHARP_HWADDR_LEN) == 0)
  187. extern const struct eth_addr ethbroadcast, ethzero;
  188. #endif /* LWIP_ARP || LWIP_ETHERNET */
  189. #if 0
  190. /** Ethernet header */
  191. #ifndef ETHARP_HWADDR_LEN
  192. #define ETHARP_HWADDR_LEN 6
  193. #endif
  194. struct eth_addr {
  195. PACK_STRUCT_FIELD(u8_t addr[ETHARP_HWADDR_LEN]);
  196. } PACK_STRUCT_STRUCT;
  197. struct eth_hdr {
  198. #if ETH_PAD_SIZE
  199. PACK_STRUCT_FIELD(u8_t padding[ETH_PAD_SIZE]);
  200. #endif
  201. PACK_STRUCT_FIELD(struct eth_addr dest);
  202. PACK_STRUCT_FIELD(struct eth_addr src);
  203. PACK_STRUCT_FIELD(u16_t type);
  204. } PACK_STRUCT_STRUCT;
  205. #ifdef PACK_STRUCT_USE_INCLUDES
  206. # include "arch/epstruct.h"
  207. #endif
  208. #define SIZEOF_ETH_HDR (14 + ETH_PAD_SIZE)
  209. #endif
  210. #ifdef __cplusplus
  211. }
  212. #endif
  213. #endif /* __NETIF_ARP_H__ */