if_ether.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #ifndef _NETINET_IF_ETHER_H_
  2. #define _NETINET_IF_ETHER_H_
  3. /*
  4. * Copyright (C) 2008 by egnite GmbH.
  5. * Copyright (C) 2001-2003 by egnite Software GmbH.
  6. * Copyright (c) 1983, 1993 by The Regents of the University of California.
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the copyright holders nor the names of
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  33. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * For additional information see http://www.ethernut.de/
  37. */
  38. /*
  39. * $Id: if_ether.h 4937 2013-01-22 11:38:42Z haraldkipp $
  40. */
  41. #include <net/if_var.h>
  42. #include <net/if_arp.h>
  43. /*!
  44. * \file netinet/if_ether.h
  45. * \brief Ethernet interface definitions.
  46. */
  47. /*! \brief Length of an Ethernet address. */
  48. #define ETHER_ADDR_LEN 6
  49. /*! \brief Length of the Ethernet type field. */
  50. #define ETHER_TYPE_LEN 2
  51. /*! \brief Length of the Ethernet CRC. */
  52. #define ETHER_CRC_LEN 4
  53. /*! \brief Length of an Ethernet header. */
  54. #define ETHER_HDR_LEN (ETHER_ADDR_LEN + ETHER_ADDR_LEN + ETHER_TYPE_LEN)
  55. #ifndef ETHER_MIN_LEN
  56. /*! \brief Minimum frame length, including CRC. */
  57. #define ETHER_MIN_LEN 64
  58. #endif
  59. #ifndef ETHER_MAX_LEN
  60. /*! \brief Maximum frame length, including CRC. */
  61. #define ETHER_MAX_LEN 1518
  62. #endif
  63. /*!
  64. * \typedef ETHERHDR
  65. * \brief Ethernet protocol header type.
  66. */
  67. typedef struct ether_header ETHERHDR;
  68. /*!
  69. * \struct ether_header if_ether.h netinet/if_ether.h
  70. * \brief Ethernet protocol header.
  71. */
  72. struct NUT_PACKED_TYPE ether_header {
  73. /*! \brief Destination MAC address. */
  74. uint8_t ether_dhost[ETHER_ADDR_LEN];
  75. /*! \brief Source MAC address. */
  76. uint8_t ether_shost[ETHER_ADDR_LEN];
  77. /*! \brief Protocol type. */
  78. uint16_t ether_type;
  79. };
  80. /*! \brief Ethernet maximum transfer unit.
  81. *
  82. * Must be checked by the hardware driver.
  83. */
  84. #define ETHERMTU (ETHER_MAX_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
  85. /*! \brief Ethernet minimum transfer unit.
  86. *
  87. * Must be checked by the hardware driver.
  88. */
  89. #define ETHERMIN (ETHER_MIN_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
  90. #define ETHERTYPE_IP 0x0800 /*!< \brief IP protocol */
  91. #define ETHERTYPE_ARP 0x0806 /*!< \brief Address resolution protocol */
  92. /*!
  93. * \brief Determine if a given Ethernet address is zero.
  94. *
  95. * \param ea Pointer to a character array containing the address.
  96. *
  97. * Return 1 if the address is zero. Otherwise 0 is returned.
  98. */
  99. #define ETHER_IS_ZERO(ea) (((ea)[0] | (ea)[1] | (ea)[2] | (ea)[3] | (ea)[4] | (ea)[5]) == 0)
  100. /*!
  101. * \brief Determine if a given Ethernet address is a broadcast address.
  102. *
  103. * \param ea Pointer to a character array containing the address.
  104. *
  105. * Return 1 if the address is a broadcast address. Otherwise 0 is returned.
  106. */
  107. #define ETHER_IS_BROADCAST(ea) (((ea)[0] & (ea)[1] & (ea)[2] & (ea)[3] & (ea)[4] & (ea)[5]) == 0xFF)
  108. /*!
  109. * \brief Determine if a given Ethernet address is a multicast address.
  110. *
  111. * The broadcast address is defined as a special multicast address.
  112. *
  113. * \param ea Pointer to a character array containing the address.
  114. *
  115. * Return 1 if the address is a multicast address. Otherwise 0 is returned.
  116. */
  117. #define ETHER_IS_MULTICAST(ea) ((ea)[0] & 1)
  118. /*!
  119. * \brief Determine if a given Ethernet address is a unicast address.
  120. *
  121. * By definition, an address with all zeros is not a valid unicast address.
  122. *
  123. * \param ea Pointer to a character array containing the address.
  124. *
  125. * Return 1 if the address is a unicast address. Otherwise 0 is returned.
  126. */
  127. #define ETHER_IS_UNICAST(ea) (!ETHER_IS_ZERO(ea) && !ETHER_IS_MULTICAST(ea))
  128. /* ASCII conversion function prototypes. */
  129. extern uint8_t *ether_aton(const char *str);
  130. extern char *ether_ntoa(const uint8_t *mac);
  131. /*!
  132. * \struct ether_arp if_ether.h netinet/if_ether.h
  133. * \brief Ethernet ARP protocol structure.
  134. *
  135. * See RFC 826 for protocol description.
  136. */
  137. /*!
  138. * \typedef ETHERARP
  139. * \brief Ethernet ARP protocol type.
  140. */
  141. typedef struct NUT_PACKED_TYPE ether_arp {
  142. ARPHDR ea_hdr; /*!< \brief Fixed-size header. */
  143. uint8_t arp_sha[6]; /*!< \brief Source hardware address. */
  144. uint32_t arp_spa; /*!< \brief Source protocol address. */
  145. uint8_t arp_tha[6]; /*!< \brief Target hardware address. */
  146. uint32_t arp_tpa; /*!< \brief Target protocol address. */
  147. } ETHERARP;
  148. /* ARP function prototypes. */
  149. extern void NutArpInput(NUTDEVICE *dev, NETBUF *nb);
  150. extern NETBUF *NutArpAllocNetBuf(uint16_t type, uint32_t ip, uint8_t *mac);
  151. extern int NutArpOutput(NUTDEVICE *dev, NETBUF *nb);
  152. extern void NutArpCacheUpdate(NUTDEVICE *dev, uint32_t ip, uint8_t *ha);
  153. extern int NutArpCacheQuery(NUTDEVICE *dev, uint32_t ip, uint8_t *mac);
  154. #endif