2a66b67e79126f01d2aff534a263aa77d6d7aa14
[oweals/gnunet.git] / src / include / gnunet_tun_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2010, 2011, 2012 Christian Grothoff
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_tun_lib.h
23  * @brief standard TCP/IP network structs and IP checksum calculations for TUN interaction
24  * @author Philipp Toelke
25  * @author Christian Grothoff
26  */
27 #ifndef GNUNET_TUN_LIB_H
28 #define GNUNET_TUN_LIB_H
29
30 #include "gnunet_util_lib.h"
31
32
33 /* see http://www.iana.org/assignments/ethernet-numbers */
34 #ifndef ETH_P_IPV4
35 /**
36  * Number for IPv4
37  */
38 #define ETH_P_IPV4 0x0800
39 #endif
40
41 #ifndef ETH_P_IPV6
42 /**
43  * Number for IPv6
44  */
45 #define ETH_P_IPV6 0x86DD
46 #endif
47
48
49 GNUNET_NETWORK_STRUCT_BEGIN
50
51 /**
52  * Header from Linux TUN interface.
53  */ 
54 struct GNUNET_TUN_Layer2PacketHeader
55 {
56   /**
57    * Some flags (unused).
58    */ 
59   uint16_t flags;
60
61   /**
62    * Here we get an ETH_P_-number.
63    */
64   uint16_t proto;
65 };
66
67
68 /**
69  * Standard IPv4 header.
70  */
71 struct GNUNET_TUN_IPv4Header
72 {
73   unsigned header_length:4 GNUNET_PACKED;
74   unsigned version:4 GNUNET_PACKED;
75   uint8_t diff_serv;
76   uint16_t total_length GNUNET_PACKED;
77   uint16_t identification GNUNET_PACKED;
78   unsigned flags:3 GNUNET_PACKED;
79   unsigned fragmentation_offset:13 GNUNET_PACKED;
80   uint8_t ttl;
81   uint8_t protocol;
82   uint16_t checksum GNUNET_PACKED;
83   struct in_addr source_address GNUNET_PACKED;
84   struct in_addr destination_address GNUNET_PACKED;
85 };
86
87
88 /**
89  * Standard IPv6 header.
90  */
91 struct GNUNET_TUN_IPv6Header
92 {
93   unsigned traffic_class_h:4 GNUNET_PACKED;
94   unsigned version:4 GNUNET_PACKED;
95   unsigned traffic_class_l:4 GNUNET_PACKED;
96   unsigned flow_label:20 GNUNET_PACKED;
97   uint16_t payload_length GNUNET_PACKED;
98   uint8_t next_header;
99   uint8_t hop_limit;
100   struct in6_addr source_address GNUNET_PACKED;
101   struct in6_addr destination_address GNUNET_PACKED;
102 };
103
104
105 /**
106  * TCP packet header (FIXME: rename!)
107  */
108 struct GNUNET_TUN_TcpHeader
109 {
110   unsigned spt:16 GNUNET_PACKED;
111   unsigned dpt:16 GNUNET_PACKED;
112   unsigned seq:32 GNUNET_PACKED;
113   unsigned ack:32 GNUNET_PACKED;
114   /**
115    * Reserved.
116    */
117   unsigned rsv:4 GNUNET_PACKED;
118   /**
119    * Number of 32-bit words in TCP header.
120    */
121   unsigned off:4 GNUNET_PACKED;
122   unsigned flg:8 GNUNET_PACKED;
123   /**
124    * Window size.
125    */
126   unsigned wsz:16 GNUNET_PACKED;
127   unsigned crc:16 GNUNET_PACKED;
128   /**
129    * Urgent pointer.
130    */
131   unsigned urg:16 GNUNET_PACKED;
132 };
133
134
135 /**
136  * UDP packet header  (FIXME: rename!)
137  */
138 struct GNUNET_TUN_UdpHeader
139 {
140   uint16_t spt GNUNET_PACKED;
141   uint16_t dpt GNUNET_PACKED;
142   uint16_t len GNUNET_PACKED;
143   uint16_t crc GNUNET_PACKED;
144 };
145
146
147 /**
148  * DNS header.
149  */
150 struct GNUNET_TUN_DnsHeader
151 {
152   uint16_t id GNUNET_PACKED;
153   uint16_t flags GNUNET_PACKED;
154   uint16_t qdcount GNUNET_PACKED;
155   uint16_t ancount GNUNET_PACKED;
156   uint16_t nscount GNUNET_PACKED;
157   uint16_t arcount GNUNET_PACKED;
158 };
159
160 #define GNUNET_TUN_ICMPTYPE_ECHO_REPLY 0
161 #define GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE 3
162 #define GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH 4
163 #define GNUNET_TUN_ICMPTYPE_REDIRECT_MESSAGE 5
164 #define GNUNET_TUN_ICMPTYPE_ECHO_REQUEST 8
165 #define GNUNET_TUN_ICMPTYPE_ROUTER_ADVERTISEMENT 9
166 #define GNUNET_TUN_ICMPTYPE_ROUTER_SOLICITATION 10
167 #define GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED 11
168
169 #define GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE 1
170 #define GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG 2
171 #define GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED 3
172 #define GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM 4
173 #define GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST 128
174 #define GNUNET_TUN_ICMPTYPE6_ECHO_REPLY 129
175
176 /**
177  * ICMP header.
178  */
179 struct GNUNET_TUN_IcmpHeader {
180   uint8_t type;         
181   uint8_t code;          
182   uint16_t crc;
183
184   union {
185     /**
186      * ICMP Echo (request/reply) 
187      */
188     struct {
189       uint16_t  identifier;
190       uint16_t  sequence_number;
191     } echo;
192
193     /**
194      * ICMP Destination Unreachable (RFC 1191) 
195      */
196     struct ih_pmtu {
197       uint16_t empty;
198       uint16_t next_hop_mtu;
199       /* followed by original IP header + first 8 bytes of original IP datagram */
200     } destination_unreachable;
201
202     /**
203      * ICMP Redirect 
204      */ 
205     struct in_addr redirect_gateway_address;    
206
207     /**
208      * MTU for packets that are too big (IPv6).
209      */
210     uint32_t packet_too_big_mtu;
211
212   } quench;
213
214 };
215
216
217 GNUNET_NETWORK_STRUCT_END
218
219
220 /**
221  * Initialize an IPv4 header.
222  *
223  * @param ip header to initialize
224  * @param protocol protocol to use (i.e. IPPROTO_UDP)
225  * @param payload_length number of bytes of payload that follow (excluding IPv4 header)
226  * @param src source IP address to use
227  * @param dst destination IP address to use
228  */
229 void
230 GNUNET_TUN_initialize_ipv4_header (struct GNUNET_TUN_IPv4Header *ip,
231                                    uint8_t protocol,
232                                    uint16_t payload_length,
233                                    const struct in_addr *src,
234                                    const struct in_addr *dst);
235
236
237 /**
238  * Initialize an IPv6 header.
239  *
240  * @param ip header to initialize
241  * @param protocol protocol to use (i.e. IPPROTO_UDP)
242  * @param payload_length number of bytes of payload that follow (excluding IPv4 header)
243  * @param src source IP address to use
244  * @param dst destination IP address to use
245  */
246 void
247 GNUNET_TUN_initialize_ipv6_header (struct GNUNET_TUN_IPv6Header *ip,
248                                    uint8_t protocol,
249                                    uint16_t payload_length,
250                                    const struct in6_addr *src,
251                                    const struct in6_addr *dst);
252
253 /**
254  * Calculate IPv4 TCP checksum.
255  *
256  * @param ip ipv4 header fully initialized
257  * @param tcp TCP header (initialized except for CRC)
258  * @param payload the TCP payload
259  * @param payload_length number of bytes of TCP payload
260  */
261 void
262 GNUNET_TUN_calculate_tcp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
263                                     struct GNUNET_TUN_TcpHeader *tcp,
264                                     const void *payload,
265                                     uint16_t payload_length);
266
267 /**
268  * Calculate IPv6 TCP checksum.
269  *
270  * @param ip ipv6 header fully initialized
271  * @param tcp TCP header (initialized except for CRC)
272  * @param payload the TCP payload
273  * @param payload_length number of bytes of TCP payload
274  */
275 void
276 GNUNET_TUN_calculate_tcp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
277                                     struct GNUNET_TUN_TcpHeader *tcp,
278                                     const void *payload,
279                                     uint16_t payload_length);
280
281 /**
282  * Calculate IPv4 UDP checksum.
283  *
284  * @param ip ipv4 header fully initialized
285  * @param udp UDP header (initialized except for CRC)
286  * @param payload the UDP payload
287  * @param payload_length number of bytes of UDP payload
288  */
289 void
290 GNUNET_TUN_calculate_udp4_checksum (const struct GNUNET_TUN_IPv4Header *ip,
291                                     struct GNUNET_TUN_UdpHeader *udp,
292                                     const void *payload,
293                                     uint16_t payload_length);
294
295
296 /**
297  * Calculate IPv6 UDP checksum.
298  *
299  * @param ip ipv6 header fully initialized
300  * @param udp UDP header (initialized except for CRC)
301  * @param payload the UDP payload
302  * @param payload_length number of bytes of UDP payload
303  */
304 void
305 GNUNET_TUN_calculate_udp6_checksum (const struct GNUNET_TUN_IPv6Header *ip,
306                                     struct GNUNET_TUN_UdpHeader *udp,
307                                     const void *payload,
308                                     uint16_t payload_length);
309
310
311 /**
312  * Calculate ICMP checksum.
313  *
314  * @param icmp IMCP header (initialized except for CRC)
315  * @param payload the ICMP payload
316  * @param payload_length number of bytes of ICMP payload
317  */
318 void
319 GNUNET_TUN_calculate_icmp_checksum (struct GNUNET_TUN_IcmpHeader *icmp,
320                                     const void *payload,
321                                     uint16_t payload_length);
322
323
324 #endif