workingish?
[oweals/gnunet.git] / src / transport / gnunet-nat-client-windows.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 Christian Grothoff (and other contributing authors)
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 src/transport/gnunet-nat-client-windows.c
23  * @brief Tool to help bypass NATs using ICMP method; must run as
24  *        root (SUID will do) or administrator on W32
25  *        This code will work under GNU/Linux or W32.
26  * @author Nathan Evans
27  *
28  * This program will send ONE ICMP message using RAW sockets
29  * to the IP address specified as the second argument.  Since
30  * it uses RAW sockets, it must be installed SUID or run as 'root'.
31  * In order to keep the security risk of the resulting SUID binary
32  * minimal, the program ONLY opens the RAW socket with root
33  * privileges, then drops them and only then starts to process
34  * command line arguments.  The code also does not link against
35  * any shared libraries (except libc) and is strictly minimal
36  * (except for checking for errors).  The following list of people
37  * have reviewed this code and considered it safe since the last
38  * modification (if you reviewed it, please have your name added
39  * to the list):
40  *
41  * - Christian Grothoff
42  * - Nathan Evans
43  */
44 #define _GNU_SOURCE
45
46 #include <ws2tcpip.h>
47 #include <winsock2.h>
48 #include <sys/time.h>
49 #include <sys/types.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <string.h>
53 #include <errno.h>
54 #include <stdlib.h>
55 #include <stdint.h>
56 #include <time.h>
57
58
59 typedef unsigned int uid_t;
60 typedef SOCKET Socket;
61 typedef unsigned short ushort;
62 #define ICMP_ECHO 8
63 #define IPDEFTTL        64              /* default ttl, from RFC 1340 */
64 #define ICMP_TIME_EXCEEDED      11      /* Time Exceeded                */
65
66 /**
67  * Must match IP given in the server.
68  */
69 #define DUMMY_IP "192.0.2.86"
70
71 #define NAT_TRAV_PORT 22225
72
73 struct ip_packet 
74 {
75   uint8_t vers_ihl;
76   uint8_t tos;
77   uint16_t pkt_len;
78   uint16_t id;
79   uint16_t flags_frag_offset;
80   uint8_t ttl;
81   uint8_t proto;
82   uint16_t checksum;
83   uint32_t src_ip;
84   uint32_t dst_ip;
85 };
86
87 struct icmp_packet 
88 {
89   uint8_t type;
90   uint8_t code;
91   uint16_t checksum;
92   uint32_t reserved;
93
94 };
95
96 struct icmp_echo_packet
97 {
98   uint8_t type;
99   uint8_t code;
100   uint16_t checksum;
101   uint32_t reserved;
102   uint32_t data;
103 };
104
105 struct udp_packet
106 {
107   uint16_t src_port;
108
109   uint16_t dst_port;
110
111   uint32_t length;
112 };
113
114 static Socket rawsock;
115
116 static struct in_addr dummy;
117  
118 static uint32_t port;
119
120 /**
121  * @param af address family
122  * @param cp the address to print
123  * @param buf where to write the address result
124  */
125 static int inet_pton (int af, char *cp, struct in_addr *buf)
126 {
127   //ret = WSAStringToAddress (cp, af, NULL, (LPSOCKADDR)buf, &ssize);
128   buf->s_addr = inet_addr(cp);
129   if (buf->s_addr == INADDR_NONE)
130     {
131       fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp);
132       return 0;
133     }
134   else
135     return 1;
136 }
137
138 static uint16_t 
139 calc_checksum(const uint16_t *data, 
140               unsigned int bytes)
141 {
142   uint32_t sum;
143   unsigned int i;
144
145   sum = 0;
146   for (i=0;i<bytes/2;i++) 
147     sum += data[i];        
148   sum = (sum & 0xffff) + (sum >> 16);
149   sum = htons(0xffff - sum);
150   return sum;
151 }
152
153
154 static void
155 make_echo (const struct in_addr *src_ip,
156            struct icmp_echo_packet *echo, uint32_t num)
157 {
158   memset(echo, 0, sizeof(struct icmp_echo_packet));
159   echo->type = ICMP_ECHO;
160   echo->code = 0;
161   echo->reserved = 0;
162   echo->checksum = 0;
163   echo->data = htons(num);
164   echo->checksum = htons(calc_checksum((uint16_t*)echo, 
165                                        sizeof (struct icmp_echo_packet)));
166 }
167
168 /**
169  * Send an ICMP message to the target.
170  *
171  * @param my_ip source address
172  * @param other target address
173  */
174 static void
175 send_icmp_udp (const struct in_addr *my_ip,
176                const struct in_addr *other)
177 {
178   struct ip_packet ip_pkt;
179   struct icmp_packet icmp_pkt;
180   struct udp_packet udp_pkt;
181
182   struct sockaddr_in dst;
183   char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
184
185   size_t off;
186   int err;
187
188   /* ip header: send to (known) ip address */
189   off = 0;
190   memset(&ip_pkt, 0, sizeof(ip_pkt));
191   ip_pkt.vers_ihl = 0x45;
192   ip_pkt.tos = 0;
193   ip_pkt.pkt_len = htons(sizeof (packet));
194   ip_pkt.id = htons(256);
195   ip_pkt.flags_frag_offset = 0;
196   ip_pkt.ttl = 128;
197   ip_pkt.proto = IPPROTO_ICMP;
198   ip_pkt.checksum = 0;
199   ip_pkt.src_ip = my_ip->s_addr;
200   ip_pkt.dst_ip = other->s_addr;
201   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
202   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
203   off += sizeof(ip_pkt);
204
205   /* ip header of the presumably 'lost' udp packet */
206   ip_pkt.vers_ihl = 0x45;
207   ip_pkt.tos = 0;
208   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
209
210   icmp_pkt.type = 11; /* TTL exceeded */
211   icmp_pkt.code = 0;
212   icmp_pkt.checksum = 0;
213   icmp_pkt.reserved = 0;
214   memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
215   off += sizeof(icmp_pkt);
216
217   /* build inner IP header */
218   memset(&ip_pkt, 0, sizeof(ip_pkt));
219   ip_pkt.vers_ihl = 0x45;
220   ip_pkt.tos = 0;
221   ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
222   ip_pkt.id = htons(0);
223   ip_pkt.flags_frag_offset = 0;
224   ip_pkt.ttl = 128;
225   ip_pkt.proto = IPPROTO_UDP;
226   ip_pkt.checksum = 0;
227   ip_pkt.src_ip = other->s_addr;
228   ip_pkt.dst_ip = dummy.s_addr;
229   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
230   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
231   off += sizeof(ip_pkt);
232
233   /* build UDP header */
234   udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
235   udp_pkt.dst_port = htons(NAT_TRAV_PORT);
236
237   memset(&udp_pkt.length, 0, sizeof(uint32_t));
238   udp_pkt.length = htonl(port);
239   memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
240   off += sizeof(udp_pkt);
241
242   /* set ICMP checksum */
243   icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
244                             sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
245   memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
246
247
248   memset (&dst, 0, sizeof (dst));
249   dst.sin_family = AF_INET;
250   dst.sin_addr = *other;
251   err = sendto(rawsock,
252                packet,
253                off, 0,
254                (struct sockaddr*)&dst,
255                sizeof(dst));
256
257   if (err < 0)
258     {
259       fprintf(stderr,
260               "sendto failed: %s\n", strerror(errno));
261     }
262   else if (err != off)
263     {
264       fprintf(stderr,
265               "Error: partial send of ICMP message\n");
266     }
267 }
268
269
270 /**
271  * Send an ICMP message to the target.
272  *
273  * @param my_ip source address
274  * @param other target address
275  */
276 static void
277 send_icmp (const struct in_addr *my_ip,
278            const struct in_addr *other)
279 {
280   struct ip_packet ip_pkt;
281   struct icmp_packet *icmp_pkt;
282   struct icmp_echo_packet icmp_echo;
283   struct sockaddr_in dst;
284   char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
285
286   size_t off;
287   int err;
288
289   memset(packet, 0, sizeof(packet));
290   /* ip header: send to (known) ip address */
291   off = 0;
292   memset(&ip_pkt, 0, sizeof(ip_pkt));
293   ip_pkt.vers_ihl = 0x45;
294   ip_pkt.tos = 0;
295   ip_pkt.pkt_len = sizeof (packet); /* huh? */
296   ip_pkt.id = 1; 
297   ip_pkt.flags_frag_offset = 0;
298   ip_pkt.ttl = IPDEFTTL;
299   ip_pkt.proto = IPPROTO_ICMP;
300   ip_pkt.checksum = 0; 
301   ip_pkt.src_ip = my_ip->s_addr;
302   ip_pkt.dst_ip = other->s_addr;
303   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
304
305   memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
306   off += sizeof (ip_pkt);
307   memset (&dst, 0, sizeof (dst));
308   dst.sin_family = AF_INET;
309   //dst.sin_addr = *other;
310   dst.sin_addr = *other;
311
312   /* icmp reply: time exceeded */
313   icmp_pkt = (struct icmp_packet*) &packet[off];
314   memset(icmp_pkt, 0, sizeof(struct icmp_packet));
315   icmp_pkt->type = ICMP_TIME_EXCEEDED;
316   icmp_pkt->code = 0; 
317   icmp_pkt->reserved = 0;
318   icmp_pkt->checksum = 0;
319
320   off += sizeof (struct icmp_packet);
321
322   /* ip header of the presumably 'lost' udp packet */
323   ip_pkt.vers_ihl = 0x45;
324   ip_pkt.tos = 0;
325   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
326   ip_pkt.id = 1; 
327   ip_pkt.flags_frag_offset = 0;
328   ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
329   ip_pkt.proto = IPPROTO_ICMP;
330   ip_pkt.src_ip = other->s_addr;
331   ip_pkt.dst_ip = dummy.s_addr;
332   ip_pkt.checksum = 0;
333   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));  
334   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
335   off += sizeof (struct ip_packet);
336
337   make_echo (other, &icmp_echo, port);
338   memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
339   off += sizeof (struct icmp_echo_packet);
340
341   icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
342                                              sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
343
344   err = sendto(rawsock, 
345                &packet[0],
346                off, 0,
347                (struct sockaddr*)&dst, 
348                sizeof(dst)); /* or sizeof 'struct sockaddr'? */
349
350   if (err < 0) 
351     {
352       fprintf(stderr,
353               "sendto failed: %s\n", strerror(errno));
354     }
355   else if (err != off) 
356     {
357       fprintf(stderr,
358               "Error: partial send of ICMP message\n");
359     }
360 }
361
362
363 static Socket
364 make_raw_socket ()
365 {
366   DWORD bOptVal = TRUE;
367   int bOptLen = sizeof(bOptVal);
368   Socket ret;
369
370   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
371   if (-1 == ret)
372     {
373       fprintf (stderr,
374                "Error opening RAW socket: %s\n",
375                strerror (errno));
376       return -1;
377     }  
378   if (setsockopt(rawsock, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != 0)
379     fprintf(stderr, "Error setting SO_BROADCAST: ON\n");
380
381   if (setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen) != 0)
382     fprintf(stderr, "Error setting IP_HDRINCL: ON\n");
383
384   return ret;
385 }
386
387
388 int
389 main (int argc, char *const *argv)
390 {
391   struct in_addr external;
392   struct in_addr target;
393
394   // WSA startup
395   WSADATA wsaData;
396   if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
397   {
398       fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
399       return 4;                       // ERROR
400   }
401
402   if (-1 == (rawsock = make_raw_socket()))
403     return 1;
404
405   if (argc != 4)
406     {
407       fprintf (stderr,
408                "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
409       return 1;
410     }
411   port = atoi(argv[3]);
412
413   if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
414        (1 != inet_pton (AF_INET, argv[2], &target)) )
415     {
416       fprintf (stderr,
417                "Error parsing IPv4 address: %s\n",
418                strerror (errno));
419       return 1;
420     }
421   if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
422     {
423       fprintf (stderr,
424                "Error parsing IPv4 address: %s\n",
425                strerror (errno));
426       abort ();
427     }
428
429   send_icmp (&external,
430              &target);
431   send_icmp_udp (&external,
432                  &target);
433
434   WSACleanup ();
435   closesocket (rawsock);
436   return 0;
437 }
438
439 /* end of gnunet-nat-client-windows.c */