ip.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #ifndef _NETINET_IP_H_
  2. #define _NETINET_IP_H_
  3. /*
  4. * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  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 copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. * -
  35. * Portions Copyright (C) 2000 David J. Hudson <dave@humbug.demon.co.uk>
  36. *
  37. * This file is distributed in the hope that it will be useful, but WITHOUT
  38. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  39. * FITNESS FOR A PARTICULAR PURPOSE.
  40. *
  41. * You can redistribute this file and/or modify it under the terms of the GNU
  42. * General Public License (GPL) as published by the Free Software Foundation;
  43. * either version 2 of the License, or (at your discretion) any later version.
  44. * See the accompanying file "copying-gpl.txt" for more details.
  45. *
  46. * As a special exception to the GPL, permission is granted for additional
  47. * uses of the text contained in this file. See the accompanying file
  48. * "copying-liquorice.txt" for details.
  49. * -
  50. * Portions Copyright (c) 1983, 1993 by
  51. * The Regents of the University of California. All rights reserved.
  52. *
  53. * Redistribution and use in source and binary forms, with or without
  54. * modification, are permitted provided that the following conditions
  55. * are met:
  56. * 1. Redistributions of source code must retain the above copyright
  57. * notice, this list of conditions and the following disclaimer.
  58. * 2. Redistributions in binary form must reproduce the above copyright
  59. * notice, this list of conditions and the following disclaimer in the
  60. * documentation and/or other materials provided with the distribution.
  61. * 3. Neither the name of the University nor the names of its contributors
  62. * may be used to endorse or promote products derived from this software
  63. * without specific prior written permission.
  64. *
  65. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  66. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  67. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  68. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  69. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  70. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  71. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  72. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  73. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  74. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  75. * SUCH DAMAGE.
  76. */
  77. /*
  78. * $Log$
  79. * Revision 1.8 2008/10/05 16:47:04 haraldkipp
  80. * Removed attribute 'packed' from typedef.
  81. *
  82. * Revision 1.7 2008/08/20 06:56:59 haraldkipp
  83. * Implemented IP demultiplexer.
  84. *
  85. * Revision 1.6 2008/08/11 07:00:23 haraldkipp
  86. * BSD types replaced by stdint types (feature request #1282721).
  87. *
  88. * Revision 1.5 2005/08/02 17:46:49 haraldkipp
  89. * Major API documentation update.
  90. *
  91. * Revision 1.4 2005/04/05 17:38:45 haraldkipp
  92. * ARM7 memory alignment bugs fixed.
  93. *
  94. * Revision 1.3 2004/12/16 18:47:02 haraldkipp
  95. * Added Damian Slee's IP filter function.
  96. *
  97. * Revision 1.2 2004/03/16 16:48:28 haraldkipp
  98. * Added Jan Dubiec's H8/300 port.
  99. *
  100. * Revision 1.1.1.1 2003/05/09 14:41:14 haraldkipp
  101. * Initial using 3.2.1
  102. *
  103. * Revision 1.9 2003/02/04 18:00:45 harald
  104. * Version 3 released
  105. *
  106. * Revision 1.8 2002/06/26 17:29:21 harald
  107. * First pre-release with 2.4 stack
  108. *
  109. */
  110. #include <cfg/ip.h>
  111. #include <net/if_arp.h>
  112. /*!
  113. * \file netinet/ip.h
  114. * \brief Definitions for internet protocol version 4.
  115. *
  116. * RFC 791.
  117. */
  118. /*!
  119. * \addtogroup xgIP
  120. */
  121. /*@{*/
  122. /* Check if IP is a multicast address */
  123. #define IP_IS_MULTICAST(_a) ((ntohl(_a) & 0xF0000000U) == 0xE0000000U)
  124. #define IPVERSION 4 /*!< \brief IP protocol version. */
  125. #define IP_DF 0x4000 /*!< \brief Don't fragment flag. */
  126. #define IP_MF 0x2000 /*!< \brief More fragments flag. */
  127. #define IP_OFFMASK 0x1fff /*!< \brief Mask for fragmenting bits. */
  128. /*!
  129. * \brief Internet header type.
  130. */
  131. typedef struct ip IPHDR;
  132. typedef struct ip_opt IPHDR_OPT;
  133. /*!
  134. * \brief Structure of an internet header.
  135. */
  136. struct NUT_PACKED_TYPE ip {
  137. #ifndef __BIG_ENDIAN__
  138. #ifdef __IMAGECRAFT__
  139. unsigned ip_hl:4, /*!< \brief Header length. */
  140. ip_v:4; /*!< \brief Version. */
  141. #else
  142. uint8_t ip_hl:4, /*!< \brief Header length. */
  143. ip_v:4; /*!< \brief Version. */
  144. #endif
  145. #else /* #ifndef __BIG_ENDIAN__ */
  146. uint8_t ip_v:4, /*!< \brief Version. */
  147. ip_hl:4; /*!< \brief Header length. */
  148. #endif /* #ifndef __BIG_ENDIAN__ */
  149. uint8_t ip_tos; /*!< \brief Type of service. */
  150. short ip_len; /*!< \brief Total length. */
  151. uint16_t ip_id; /*!< \brief Identification. */
  152. short ip_off; /*!< \brief Fragment offset field. */
  153. uint8_t ip_ttl; /*!< \brief Time to live. */
  154. uint8_t ip_p; /*!< \brief Protocol. */
  155. uint16_t ip_sum; /*!< \brief Checksum. */
  156. uint32_t ip_src; /*!< \brief Source IP address. */
  157. uint32_t ip_dst; /*!< \brief Destination IP address. */
  158. };
  159. struct NUT_PACKED_TYPE ip_opt {
  160. #ifndef __BIG_ENDIAN__
  161. #ifdef __IMAGECRAFT__
  162. unsigned ip_hl:4, /*!< \brief Header length. */
  163. ip_v:4; /*!< \brief Version. */
  164. #else
  165. uint8_t ip_hl:4, /*!< \brief Header length. */
  166. ip_v:4; /*!< \brief Version. */
  167. #endif
  168. #else /* #ifndef __BIG_ENDIAN__ */
  169. uint8_t ip_v:4, /*!< \brief Version. */
  170. ip_hl:4; /*!< \brief Header length. */
  171. #endif /* #ifndef __BIG_ENDIAN__ */
  172. uint8_t ip_tos; /*!< \brief Type of service. */
  173. short ip_len; /*!< \brief Total length. */
  174. uint16_t ip_id; /*!< \brief Identification. */
  175. short ip_off; /*!< \brief Fragment offset field. */
  176. uint8_t ip_ttl; /*!< \brief Time to live. */
  177. uint8_t ip_p; /*!< \brief Protocol. */
  178. uint16_t ip_sum; /*!< \brief Checksum. */
  179. uint32_t ip_src; /*!< \brief Source IP address. */
  180. uint32_t ip_dst; /*!< \brief Destination IP address. */
  181. uint32_t ip_option; /*!< \brief Option. */
  182. };
  183. #define IPOPT_EOL 0 /*!< \brief End of option list. */
  184. #define IPOPT_NOP 1 /*!< \brief No operation. */
  185. #define IPOPT_RR 7 /*!< \brief Record packet route. */
  186. #define IPOPT_TS 68 /*!< \brief Timestamp. */
  187. #define IPOPT_SECURITY 130 /*!< \brief Provide s,c,h,tcc. */
  188. #define IPOPT_LSRR 131 /*!< \brief Loose source route. */
  189. #define IPOPT_SATID 136 /*!< \brief Satnet id. */
  190. #define IPOPT_SSRR 137 /*!< \brief Strict source route. */
  191. #define IPOPT_OPTVAL 0 /*!< \brief Option identifier offset. */
  192. #define IPOPT_OLEN 1 /*!< \brief Option length offset. */
  193. #define IPOPT_OFFSET 2 /*!< \brief Offset within option. */
  194. #define IPOPT_MINOFF 4 /*!< \brief Minimum offset within option. */
  195. #define MAXTTL 255 /*!< \brief Maximum time to live (seconds). */
  196. #define IPDEFTTL 64 /*!< \brief Default time to live. */
  197. #define IPFRAGTTL 60 /*!< \brief Time to live for fragments. */
  198. #define IPTTLDEC 1 /*!< \brief Subtracted from time to live when forwarding. */
  199. /*@}*/
  200. /*
  201. * API declarations.
  202. */
  203. #include <dev/netbuf.h>
  204. extern int NutIpOutput(uint8_t proto, uint32_t dest, NETBUF * nb);
  205. extern int NutIpForward(NETBUF *nb);
  206. /*
  207. * Kernel declarations.
  208. */
  209. #include <net/if_var.h>
  210. extern void NutIpInput(NUTDEVICE * dev, NETBUF * nb);
  211. /*
  212. * Ip Filter declarations.
  213. */
  214. typedef int (*NutIpFilterFunc) (uint32_t);
  215. extern void NutIpSetInputFilter(NutIpFilterFunc callbackFunc);
  216. extern int NutRegisterIpHandler(uint8_t prot, int (*hdlr)(NUTDEVICE *, NETBUF *));
  217. extern int (*ip_demux) (NUTDEVICE *, NETBUF *);
  218. #endif