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
64 #define ICMP_TIME_EXCEEDED 11
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   buf->s_addr = inet_addr(cp);
128   if (buf->s_addr == INADDR_NONE)
129     {
130       fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp);
131       return 0;
132     }
133   else
134     return 1;
135 }
136
137 static uint16_t 
138 calc_checksum(const uint16_t *data, 
139               unsigned int bytes)
140 {
141   uint32_t sum;
142   unsigned int i;
143
144   sum = 0;
145   for (i=0;i<bytes/2;i++) 
146     sum += data[i];        
147   sum = (sum & 0xffff) + (sum >> 16);
148   sum = htons(0xffff - sum);
149   return sum;
150 }
151
152
153 static void
154 make_echo (const struct in_addr *src_ip,
155            struct icmp_echo_packet *echo, uint32_t num)
156 {
157   memset(echo, 0, sizeof(struct icmp_echo_packet));
158   echo->type = ICMP_ECHO;
159   echo->code = 0;
160   echo->reserved = 0;
161   echo->checksum = 0;
162   echo->data = htons(num);
163   echo->checksum = htons(calc_checksum((uint16_t*)echo, 
164                                        sizeof (struct icmp_echo_packet)));
165 }
166
167 /**
168  * Send an ICMP message to the target.
169  *
170  * @param my_ip source address
171  * @param other target address
172  */
173 static void
174 send_icmp_udp (const struct in_addr *my_ip,
175                const struct in_addr *other)
176 {
177   struct ip_packet ip_pkt;
178   struct icmp_packet icmp_pkt;
179   struct udp_packet udp_pkt;
180
181   struct sockaddr_in dst;
182   char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
183
184   size_t off;
185   int err;
186
187   /* ip header: send to (known) ip address */
188   off = 0;
189   memset(&ip_pkt, 0, sizeof(ip_pkt));
190   ip_pkt.vers_ihl = 0x45;
191   ip_pkt.tos = 0;
192   ip_pkt.pkt_len = htons(sizeof (packet));
193   ip_pkt.id = htons(256);
194   ip_pkt.flags_frag_offset = 0;
195   ip_pkt.ttl = 128;
196   ip_pkt.proto = IPPROTO_ICMP;
197   ip_pkt.checksum = 0;
198   ip_pkt.src_ip = my_ip->s_addr;
199   ip_pkt.dst_ip = other->s_addr;
200   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
201   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
202   off += sizeof(ip_pkt);
203
204   /* ip header of the presumably 'lost' udp packet */
205   ip_pkt.vers_ihl = 0x45;
206   ip_pkt.tos = 0;
207   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
208
209   icmp_pkt.type = 11; /* TTL exceeded */
210   icmp_pkt.code = 0;
211   icmp_pkt.checksum = 0;
212   icmp_pkt.reserved = 0;
213   memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
214   off += sizeof(icmp_pkt);
215
216   /* build inner IP header */
217   memset(&ip_pkt, 0, sizeof(ip_pkt));
218   ip_pkt.vers_ihl = 0x45;
219   ip_pkt.tos = 0;
220   ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
221   ip_pkt.id = htons(0);
222   ip_pkt.flags_frag_offset = 0;
223   ip_pkt.ttl = 128;
224   ip_pkt.proto = IPPROTO_UDP;
225   ip_pkt.checksum = 0;
226   ip_pkt.src_ip = other->s_addr;
227   ip_pkt.dst_ip = dummy.s_addr;
228   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
229   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
230   off += sizeof(ip_pkt);
231
232   /* build UDP header */
233   udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
234   udp_pkt.dst_port = htons(NAT_TRAV_PORT);
235
236   memset(&udp_pkt.length, 0, sizeof(uint32_t));
237   udp_pkt.length = htonl(port);
238   memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
239   off += sizeof(udp_pkt);
240
241   /* set ICMP checksum */
242   icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
243                             sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
244   memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
245
246
247   memset (&dst, 0, sizeof (dst));
248   dst.sin_family = AF_INET;
249   dst.sin_addr = *other;
250   err = sendto(rawsock,
251                packet,
252                off, 0,
253                (struct sockaddr*)&dst,
254                sizeof(dst));
255
256   if (err < 0)
257     {
258       fprintf(stderr,
259               "sendto failed: %s\n", strerror(errno));
260     }
261   else if (err != off)
262     {
263       fprintf(stderr,
264               "Error: partial send of ICMP message\n");
265     }
266 }
267
268
269 /**
270  * Send an ICMP message to the target.
271  *
272  * @param my_ip source address
273  * @param other target address
274  */
275 static void
276 send_icmp (const struct in_addr *my_ip,
277            const struct in_addr *other)
278 {
279   struct ip_packet ip_pkt;
280   struct icmp_packet *icmp_pkt;
281   struct icmp_echo_packet icmp_echo;
282   struct sockaddr_in dst;
283   char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
284
285   size_t off;
286   int err;
287
288   memset(packet, 0, sizeof(packet));
289   /* ip header: send to (known) ip address */
290   off = 0;
291   memset(&ip_pkt, 0, sizeof(ip_pkt));
292   ip_pkt.vers_ihl = 0x45;
293   ip_pkt.tos = 0;
294   ip_pkt.pkt_len = sizeof (packet); /* huh? */
295   ip_pkt.id = 1; 
296   ip_pkt.flags_frag_offset = 0;
297   ip_pkt.ttl = IPDEFTTL;
298   ip_pkt.proto = IPPROTO_ICMP;
299   ip_pkt.checksum = 0; 
300   ip_pkt.src_ip = my_ip->s_addr;
301   ip_pkt.dst_ip = other->s_addr;
302   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
303
304   memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
305   off += sizeof (ip_pkt);
306   memset (&dst, 0, sizeof (dst));
307   dst.sin_family = AF_INET;
308   dst.sin_addr = *other;
309
310   /* icmp reply: time exceeded */
311   icmp_pkt = (struct icmp_packet*) &packet[off];
312   memset(icmp_pkt, 0, sizeof(struct icmp_packet));
313   icmp_pkt->type = ICMP_TIME_EXCEEDED;
314   icmp_pkt->code = 0; 
315   icmp_pkt->reserved = 0;
316   icmp_pkt->checksum = 0;
317
318   off += sizeof (struct icmp_packet);
319
320   /* ip header of the presumably 'lost' udp packet */
321   ip_pkt.vers_ihl = 0x45;
322   ip_pkt.tos = 0;
323   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
324   ip_pkt.id = 1; 
325   ip_pkt.flags_frag_offset = 0;
326   ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
327   ip_pkt.proto = IPPROTO_ICMP;
328   ip_pkt.src_ip = other->s_addr;
329   ip_pkt.dst_ip = dummy.s_addr;
330   ip_pkt.checksum = 0;
331   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));  
332   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
333   off += sizeof (struct ip_packet);
334
335   make_echo (other, &icmp_echo, port);
336   memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
337   off += sizeof (struct icmp_echo_packet);
338
339   icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
340                                              sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
341
342   err = sendto(rawsock, 
343                &packet[0],
344                off, 0,
345                (struct sockaddr*)&dst, 
346                sizeof(dst)); /* or sizeof 'struct sockaddr'? */
347
348   if (err < 0) 
349     {
350       fprintf(stderr,
351               "sendto failed: %s\n", strerror(errno));
352     }
353   else if (err != off) 
354     {
355       fprintf(stderr,
356               "Error: partial send of ICMP message\n");
357     }
358 }
359
360
361 static Socket
362 make_raw_socket ()
363 {
364   DWORD bOptVal = TRUE;
365   int bOptLen = sizeof(bOptVal);
366   Socket ret;
367
368   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
369   if (-1 == ret)
370     {
371       fprintf (stderr,
372                "Error opening RAW socket: %s\n",
373                strerror (errno));
374       return -1;
375     }  
376   if (setsockopt(rawsock, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != 0)
377     fprintf(stderr, "Error setting SO_BROADCAST: ON\n");
378
379   if (setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen) != 0)
380     fprintf(stderr, "Error setting IP_HDRINCL: ON\n");
381
382   return ret;
383 }
384
385
386 int
387 main (int argc, char *const *argv)
388 {
389   struct in_addr external;
390   struct in_addr target;
391
392   WSADATA wsaData;
393   if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
394   {
395       fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
396       return 4;
397   }
398
399   if (-1 == (rawsock = make_raw_socket()))
400     return 1;
401
402   if (argc != 4)
403     {
404       fprintf (stderr,
405                "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
406       return 1;
407     }
408   port = atoi(argv[3]);
409
410   if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
411        (1 != inet_pton (AF_INET, argv[2], &target)) )
412     {
413       fprintf (stderr,
414                "Error parsing IPv4 address: %s\n",
415                strerror (errno));
416       return 1;
417     }
418   if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
419     {
420       fprintf (stderr,
421                "Error parsing IPv4 address: %s\n",
422                strerror (errno));
423       abort ();
424     }
425
426   send_icmp (&external,
427              &target);
428   send_icmp_udp (&external,
429                  &target);
430
431   WSACleanup ();
432   closesocket (rawsock);
433   return 0;
434 }
435
436 /* end of gnunet-nat-client-windows.c */