proto.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * TAP-Windows -- A kernel driver to provide virtual tap
  3. * device functionality on Windows.
  4. *
  5. * This code was inspired by the CIPE-Win32 driver by Damion K. Wilson.
  6. *
  7. * This source code is Copyright (C) 2002-2010 OpenVPN Technologies, Inc.,
  8. * and is released under the GPL version 2 (see below).
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program (see the file COPYING included with this
  21. * distribution); if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. //============================================================
  25. // MAC address, Ethernet header, and ARP
  26. //============================================================
  27. #pragma pack(1)
  28. #define IP_HEADER_SIZE 20
  29. #define IPV6_HEADER_SIZE 40
  30. typedef unsigned char MACADDR [6];
  31. typedef unsigned long IPADDR;
  32. typedef unsigned char IPV6ADDR [16];
  33. //-----------------
  34. // Ethernet address
  35. //-----------------
  36. typedef struct {
  37. MACADDR addr;
  38. } ETH_ADDR;
  39. typedef struct {
  40. ETH_ADDR list[NIC_MAX_MCAST_LIST];
  41. } MC_LIST;
  42. //----------------
  43. // Ethernet header
  44. //----------------
  45. typedef struct
  46. {
  47. MACADDR dest; /* destination eth addr */
  48. MACADDR src; /* source ether addr */
  49. # define ETH_P_IP 0x0800 /* IPv4 protocol */
  50. # define ETH_P_IPV6 0x86DD /* IPv6 protocol */
  51. # define ETH_P_ARP 0x0806 /* ARP protocol */
  52. USHORT proto; /* packet type ID field */
  53. } ETH_HEADER, *PETH_HEADER;
  54. //----------------
  55. // ARP packet
  56. //----------------
  57. typedef struct
  58. {
  59. MACADDR m_MAC_Destination; // Reverse these two
  60. MACADDR m_MAC_Source; // to answer ARP requests
  61. USHORT m_Proto; // 0x0806
  62. # define MAC_ADDR_TYPE 0x0001
  63. USHORT m_MAC_AddressType; // 0x0001
  64. USHORT m_PROTO_AddressType; // 0x0800
  65. UCHAR m_MAC_AddressSize; // 0x06
  66. UCHAR m_PROTO_AddressSize; // 0x04
  67. # define ARP_REQUEST 0x0001
  68. # define ARP_REPLY 0x0002
  69. USHORT m_ARP_Operation; // 0x0001 for ARP request, 0x0002 for ARP reply
  70. MACADDR m_ARP_MAC_Source;
  71. IPADDR m_ARP_IP_Source;
  72. MACADDR m_ARP_MAC_Destination;
  73. IPADDR m_ARP_IP_Destination;
  74. }
  75. ARP_PACKET, *PARP_PACKET;
  76. //----------
  77. // IP Header
  78. //----------
  79. typedef struct {
  80. # define IPH_GET_VER(v) (((v) >> 4) & 0x0F)
  81. # define IPH_GET_LEN(v) (((v) & 0x0F) << 2)
  82. UCHAR version_len;
  83. UCHAR tos;
  84. USHORT tot_len;
  85. USHORT id;
  86. # define IP_OFFMASK 0x1fff
  87. USHORT frag_off;
  88. UCHAR ttl;
  89. # define IPPROTO_UDP 17 /* UDP protocol */
  90. # define IPPROTO_TCP 6 /* TCP protocol */
  91. # define IPPROTO_ICMP 1 /* ICMP protocol */
  92. # define IPPROTO_IGMP 2 /* IGMP protocol */
  93. UCHAR protocol;
  94. USHORT check;
  95. ULONG saddr;
  96. ULONG daddr;
  97. /* The options start here. */
  98. } IPHDR;
  99. //-----------
  100. // UDP header
  101. //-----------
  102. typedef struct {
  103. USHORT source;
  104. USHORT dest;
  105. USHORT len;
  106. USHORT check;
  107. } UDPHDR;
  108. //--------------------------
  109. // TCP header, per RFC 793.
  110. //--------------------------
  111. typedef struct {
  112. USHORT source; /* source port */
  113. USHORT dest; /* destination port */
  114. ULONG seq; /* sequence number */
  115. ULONG ack_seq; /* acknowledgement number */
  116. # define TCPH_GET_DOFF(d) (((d) & 0xF0) >> 2)
  117. UCHAR doff_res;
  118. # define TCPH_FIN_MASK (1<<0)
  119. # define TCPH_SYN_MASK (1<<1)
  120. # define TCPH_RST_MASK (1<<2)
  121. # define TCPH_PSH_MASK (1<<3)
  122. # define TCPH_ACK_MASK (1<<4)
  123. # define TCPH_URG_MASK (1<<5)
  124. # define TCPH_ECE_MASK (1<<6)
  125. # define TCPH_CWR_MASK (1<<7)
  126. UCHAR flags;
  127. USHORT window;
  128. USHORT check;
  129. USHORT urg_ptr;
  130. } TCPHDR;
  131. #define TCPOPT_EOL 0
  132. #define TCPOPT_NOP 1
  133. #define TCPOPT_MAXSEG 2
  134. #define TCPOLEN_MAXSEG 4
  135. //------------
  136. // IPv6 Header
  137. //------------
  138. typedef struct {
  139. UCHAR version_prio;
  140. UCHAR flow_lbl[3];
  141. USHORT payload_len;
  142. # define IPPROTO_ICMPV6 0x3a /* ICMP protocol v6 */
  143. UCHAR nexthdr;
  144. UCHAR hop_limit;
  145. IPV6ADDR saddr;
  146. IPV6ADDR daddr;
  147. } IPV6HDR;
  148. //--------------------------------------------
  149. // IPCMPv6 NS/NA Packets (RFC4443 and RFC4861)
  150. //--------------------------------------------
  151. // Neighbor Solictiation - RFC 4861, 4.3
  152. // (this is just the ICMPv6 part of the packet)
  153. typedef struct {
  154. UCHAR type;
  155. # define ICMPV6_TYPE_NS 135 // neighbour solicitation
  156. UCHAR code;
  157. # define ICMPV6_CODE_0 0 // no specific sub-code for NS/NA
  158. USHORT checksum;
  159. ULONG reserved;
  160. IPV6ADDR target_addr;
  161. } ICMPV6_NS;
  162. // Neighbor Advertisement - RFC 4861, 4.4 + 4.6/4.6.1
  163. // (this is just the ICMPv6 payload)
  164. typedef struct {
  165. UCHAR type;
  166. # define ICMPV6_TYPE_NA 136 // neighbour advertisement
  167. UCHAR code;
  168. # define ICMPV6_CODE_0 0 // no specific sub-code for NS/NA
  169. USHORT checksum;
  170. UCHAR rso_bits; // Router(0), Solicited(2), Ovrrd(4)
  171. UCHAR reserved[3];
  172. IPV6ADDR target_addr;
  173. // always include "Target Link-layer Address" option (RFC 4861 4.6.1)
  174. UCHAR opt_type;
  175. #define ICMPV6_OPTION_TLLA 2
  176. UCHAR opt_length;
  177. #define ICMPV6_LENGTH_TLLA 1 // multiplied by 8 -> 1 = 8 bytes
  178. MACADDR target_macaddr;
  179. } ICMPV6_NA;
  180. // this is the complete packet with Ethernet and IPv6 headers
  181. typedef struct {
  182. ETH_HEADER eth;
  183. IPV6HDR ipv6;
  184. ICMPV6_NA icmpv6;
  185. } ICMPV6_NA_PKT;
  186. #pragma pack()