-changing exit helper code to automatically do the network configuration for an exit...
[oweals/gnunet.git] / src / nat / gnunet-helper-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/nat/gnunet-helper-nat-client-windows.c
23  * @brief Tool to help bypass NATs using ICMP method; must run as
24  *        administrator on W32
25  *        This code is forx 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 #define FD_SETSIZE 1024
47 #include <winsock2.h>
48 #include <ws2tcpip.h>
49 #include <sys/time.h>
50 #include <sys/types.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <errno.h>
55 #include <stdlib.h>
56 #include <stdint.h>
57 #include <time.h>
58
59
60 #define ICMP_ECHO 8
61 #define IPDEFTTL 64
62 #define ICMP_TIME_EXCEEDED 11
63
64 /**
65  * Must match IP given in the server.
66  */
67 #define DUMMY_IP "192.0.2.86"
68
69 #define NAT_TRAV_PORT 22225
70
71 /**
72  * IPv4 header.
73  */
74 struct ip_header
75 {
76
77   /**
78    * Version (4 bits) + Internet header length (4 bits)
79    */
80   uint8_t vers_ihl;
81
82   /**
83    * Type of service
84    */
85   uint8_t tos;
86
87   /**
88    * Total length
89    */
90   uint16_t pkt_len;
91
92   /**
93    * Identification
94    */
95   uint16_t id;
96
97   /**
98    * Flags (3 bits) + Fragment offset (13 bits)
99    */
100   uint16_t flags_frag_offset;
101
102   /**
103    * Time to live
104    */
105   uint8_t ttl;
106
107   /**
108    * Protocol
109    */
110   uint8_t proto;
111
112   /**
113    * Header checksum
114    */
115   uint16_t checksum;
116
117   /**
118    * Source address
119    */
120   uint32_t src_ip;
121
122   /**
123    * Destination address
124    */
125   uint32_t dst_ip;
126 };
127
128
129 /**
130  * Format of ICMP packet.
131  */
132 struct icmp_ttl_exceeded_header
133 {
134   uint8_t type;
135
136   uint8_t code;
137
138   uint16_t checksum;
139
140   uint32_t unused;
141
142   /* followed by original payload */
143 };
144
145 struct icmp_echo_header
146 {
147   uint8_t type;
148
149   uint8_t code;
150
151   uint16_t checksum;
152
153   uint32_t reserved;
154 };
155
156 /**
157  * Beginning of UDP packet.
158  */
159 struct udp_header
160 {
161   uint16_t src_port;
162
163   uint16_t dst_port;
164
165   uint16_t length;
166
167   uint16_t crc;
168 };
169
170
171 /**
172  * Socket we use to send our ICMP packets.
173  */
174 static SOCKET rawsock;
175
176 /**
177  * Target "dummy" address.
178  */
179 static struct in_addr dummy;
180
181 /**
182  * Port we are listening on (communicated to the server).
183  */
184 static uint16_t port;
185
186
187
188 /**
189  * Convert IPv4 address from text to binary form.
190  *
191  * @param af address family
192  * @param cp the address to print
193  * @param buf where to write the address result
194  * @return 1 on success
195  */
196 static int
197 inet_pton (int af, const char *cp, struct in_addr *buf)
198 {
199   buf->s_addr = inet_addr (cp);
200   if (buf->s_addr == INADDR_NONE)
201   {
202     fprintf (stderr, "Error %d handling address %s", WSAGetLastError (), cp);
203     return 0;
204   }
205   return 1;
206 }
207
208
209 /**
210  * CRC-16 for IP/ICMP headers.
211  *
212  * @param data what to calculate the CRC over
213  * @param bytes number of bytes in data (must be multiple of 2)
214  * @return the CRC 16.
215  */
216 static uint16_t
217 calc_checksum (const uint16_t * data, unsigned int bytes)
218 {
219   uint32_t sum;
220   unsigned int i;
221
222   sum = 0;
223   for (i = 0; i < bytes / 2; i++)
224     sum += data[i];
225   sum = (sum & 0xffff) + (sum >> 16);
226   sum = htons (0xffff - sum);
227   return sum;
228 }
229
230
231 /**
232  * Send an ICMP message to the target.
233  *
234  * @param my_ip source address
235  * @param other target address
236  */
237 static void
238 send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
239 {
240   char packet[sizeof (struct ip_header) * 2 +
241               sizeof (struct icmp_ttl_exceeded_header) +
242               sizeof (struct udp_header)];
243   struct ip_header ip_pkt;
244   struct icmp_ttl_exceeded_header icmp_pkt;
245   struct udp_header udp_pkt;
246   struct sockaddr_in dst;
247   size_t off;
248   int err;
249
250   /* ip header: send to (known) ip address */
251   off = 0;
252   ip_pkt.vers_ihl = 0x45;
253   ip_pkt.tos = 0;
254   ip_pkt.pkt_len = htons (sizeof (packet));
255   ip_pkt.id = htons (256);
256   ip_pkt.flags_frag_offset = 0;
257   ip_pkt.ttl = 128;
258   ip_pkt.proto = IPPROTO_ICMP;
259   ip_pkt.checksum = 0;
260   ip_pkt.src_ip = my_ip->s_addr;
261   ip_pkt.dst_ip = other->s_addr;
262   ip_pkt.checksum =
263       htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
264   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
265   off += sizeof (struct ip_header);
266
267   icmp_pkt.type = ICMP_TIME_EXCEEDED;
268   icmp_pkt.code = 0;
269   icmp_pkt.checksum = 0;
270   icmp_pkt.unused = 0;
271   memcpy (&packet[off], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header));
272   off += sizeof (struct icmp_ttl_exceeded_header);
273
274   /* ip header of the presumably 'lost' udp packet */
275   ip_pkt.vers_ihl = 0x45;
276   ip_pkt.tos = 0;
277   ip_pkt.pkt_len =
278       htons (sizeof (struct ip_header) + sizeof (struct udp_header));
279   ip_pkt.id = htons (0);
280   ip_pkt.flags_frag_offset = 0;
281   ip_pkt.ttl = 128;
282   ip_pkt.proto = IPPROTO_UDP;
283   ip_pkt.checksum = 0;
284   ip_pkt.src_ip = other->s_addr;
285   ip_pkt.dst_ip = dummy.s_addr;
286   ip_pkt.checksum =
287       htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
288   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
289   off += sizeof (struct ip_header);
290
291   /* build UDP header */
292   udp_pkt.src_port = htons (NAT_TRAV_PORT);
293   udp_pkt.dst_port = htons (NAT_TRAV_PORT);
294   udp_pkt.length = htons (port);
295   udp_pkt.crc = 0;
296   memcpy (&packet[off], &udp_pkt, sizeof (struct udp_header));
297   off += sizeof (struct udp_header);
298
299   /* no go back to calculate ICMP packet checksum */
300   icmp_pkt.checksum =
301       htons (calc_checksum
302              ((uint16_t *) & packet[off],
303               sizeof (struct icmp_ttl_exceeded_header) +
304               sizeof (struct ip_header) + sizeof (struct udp_header)));
305   memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt,
306           sizeof (struct icmp_ttl_exceeded_header));
307
308   memset (&dst, 0, sizeof (dst));
309   dst.sin_family = AF_INET;
310   dst.sin_addr = *other;
311   err =
312       sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
313               sizeof (dst));
314   if (err < 0)
315   {
316     fprintf (stderr, "sendto failed: %s\n", strerror (errno));
317   }
318   else if (sizeof (packet) != (size_t) err)
319   {
320     fprintf (stderr, "Error: partial send of ICMP message\n");
321   }
322 }
323
324
325 /**
326  * Send an ICMP message to the target.
327  *
328  * @param my_ip source address
329  * @param other target address
330  */
331 static void
332 send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
333 {
334   struct ip_header ip_pkt;
335   struct icmp_ttl_exceeded_header icmp_ttl;
336   struct icmp_echo_header icmp_echo;
337   struct sockaddr_in dst;
338   char packet[sizeof (struct ip_header) * 2 +
339               sizeof (struct icmp_ttl_exceeded_header) +
340               sizeof (struct icmp_echo_header)];
341   size_t off;
342   int err;
343
344   /* ip header: send to (known) ip address */
345   off = 0;
346   ip_pkt.vers_ihl = 0x45;
347   ip_pkt.tos = 0;
348   ip_pkt.pkt_len = htons (sizeof (packet));
349   ip_pkt.id = htons (256);
350   ip_pkt.flags_frag_offset = 0;
351   ip_pkt.ttl = IPDEFTTL;
352   ip_pkt.proto = IPPROTO_ICMP;
353   ip_pkt.checksum = 0;
354   ip_pkt.src_ip = my_ip->s_addr;
355   ip_pkt.dst_ip = other->s_addr;
356   ip_pkt.checksum =
357       htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
358   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
359   off += sizeof (ip_pkt);
360
361   /* icmp reply: time exceeded */
362   icmp_ttl.type = ICMP_TIME_EXCEEDED;
363   icmp_ttl.code = 0;
364   icmp_ttl.checksum = 0;
365   icmp_ttl.unused = 0;
366   memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
367   off += sizeof (struct icmp_ttl_exceeded_header);
368
369   /* ip header of the presumably 'lost' udp packet */
370   ip_pkt.vers_ihl = 0x45;
371   ip_pkt.tos = 0;
372   ip_pkt.pkt_len =
373       htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
374   ip_pkt.id = htons (256);
375   ip_pkt.flags_frag_offset = 0;
376   ip_pkt.ttl = 1;               /* real TTL would be 1 on a time exceeded packet */
377   ip_pkt.proto = IPPROTO_ICMP;
378   ip_pkt.src_ip = other->s_addr;
379   ip_pkt.dst_ip = dummy.s_addr;
380   ip_pkt.checksum = 0;
381   ip_pkt.checksum =
382       htons (calc_checksum ((uint16_t *) & ip_pkt, sizeof (struct ip_header)));
383   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
384   off += sizeof (struct ip_header);
385
386   icmp_echo.type = ICMP_ECHO;
387   icmp_echo.code = 0;
388   icmp_echo.reserved = htonl (port);
389   icmp_echo.checksum = 0;
390   icmp_echo.checksum =
391       htons (calc_checksum
392              ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
393   memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
394
395   /* no go back to calculate ICMP packet checksum */
396   off = sizeof (struct ip_header);
397   icmp_ttl.checksum =
398       htons (calc_checksum
399              ((uint16_t *) & packet[off],
400               sizeof (struct icmp_ttl_exceeded_header) +
401               sizeof (struct ip_header) + sizeof (struct icmp_echo_header)));
402   memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
403
404   memset (&dst, 0, sizeof (dst));
405   dst.sin_family = AF_INET;
406   dst.sin_addr = *other;
407
408   err =
409       sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
410               sizeof (dst));
411
412   if (err < 0)
413   {
414     fprintf (stderr, "sendto failed: %s\n", strerror (errno));
415   }
416   else if (sizeof (packet) != (size_t) err)
417   {
418     fprintf (stderr, "Error: partial send of ICMP message\n");
419   }
420 }
421
422
423 /**
424  * Create an ICMP raw socket.
425  *
426  * @return INVALID_SOCKET on error
427  */
428 static SOCKET
429 make_raw_socket ()
430 {
431   DWORD bOptVal = TRUE;
432   int bOptLen = sizeof (bOptVal);
433   SOCKET ret;
434
435   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
436   if (INVALID_SOCKET == ret)
437   {
438     fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
439     return INVALID_SOCKET;
440   }
441   if (0 !=
442       setsockopt (ret, SOL_SOCKET, SO_BROADCAST, (char *) &bOptVal, bOptLen))
443   {
444     fprintf (stderr, "Error setting SO_BROADCAST to ON: %s\n",
445              strerror (errno));
446     closesocket (rawsock);
447     return INVALID_SOCKET;
448   }
449
450   if (0 != setsockopt (ret, IPPROTO_IP, IP_HDRINCL, (char *) &bOptVal, bOptLen))
451   {
452     fprintf (stderr, "Error setting IP_HDRINCL to ON: %s\n", strerror (errno));
453     closesocket (rawsock);
454     return INVALID_SOCKET;
455   }
456   return ret;
457 }
458
459
460 int
461 main (int argc, char *const *argv)
462 {
463   struct in_addr external;
464   struct in_addr target;
465   WSADATA wsaData;
466
467   unsigned int p;
468
469   if (argc != 4)
470   {
471     fprintf (stderr,
472              "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
473     return 1;
474   }
475   if ((1 != inet_pton (AF_INET, argv[1], &external)) ||
476       (1 != inet_pton (AF_INET, argv[2], &target)))
477   {
478     fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
479     return 1;
480   }
481   if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
482   {
483     fprintf (stderr, "Error parsing port value `%s'\n", argv[3]);
484     return 1;
485   }
486   port = (uint16_t) p;
487
488   if (0 != WSAStartup (MAKEWORD (2, 1), &wsaData))
489   {
490     fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
491     return 2;
492   }
493   if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
494   {
495     fprintf (stderr, "Internal error converting dummy IP to binary.\n");
496     return 2;
497   }
498   if (-1 == (rawsock = make_raw_socket ()))
499     return 3;
500   send_icmp (&external, &target);
501   send_icmp_udp (&external, &target);
502   closesocket (rawsock);
503   WSACleanup ();
504   return 0;
505 }
506
507 /* end of gnunet-helper-nat-client-windows.c */