indentation
[oweals/gnunet.git] / src / nat / gnunet-helper-nat-client.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.c
23  * @brief Tool to help bypass NATs using ICMP method; must run as root (SUID will do)
24  *        This code will work under GNU/Linux only.
25  * @author Christian Grothoff
26  *
27  * This program will send ONE ICMP message using RAW sockets
28  * to the IP address specified as the second argument.  Since
29  * it uses RAW sockets, it must be installed SUID or run as 'root'.
30  * In order to keep the security risk of the resulting SUID binary
31  * minimal, the program ONLY opens the RAW socket with root
32  * privileges, then drops them and only then starts to process
33  * command line arguments.  The code also does not link against
34  * any shared libraries (except libc) and is strictly minimal
35  * (except for checking for errors).  The following list of people
36  * have reviewed this code and considered it safe since the last
37  * modification (if you reviewed it, please have your name added
38  * to the list):
39  *
40  * - Christian Grothoff
41  * - Nathan Evans
42  * - Benjamin Kuperman (22 Aug 2010)
43  */
44 #if HAVE_CONFIG_H
45 /* Just needed for HAVE_SOCKADDR_IN_SIN_LEN test macro! */
46 #include "gnunet_config.h"
47 #else
48 #define _GNU_SOURCE
49 #endif
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <arpa/inet.h>
53 #include <sys/types.h>
54 #include <unistd.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <errno.h>
58 #include <stdlib.h>
59 #include <stdint.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip_icmp.h>
62 #include <netinet/in.h>
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  * Format of ICMP packet.
130  */
131 struct icmp_ttl_exceeded_header
132 {
133   uint8_t type;
134
135   uint8_t code;
136
137   uint16_t checksum;
138
139   uint32_t unused;
140
141   /* followed by original payload */
142 };
143
144 struct icmp_echo_header
145 {
146   uint8_t type;
147
148   uint8_t code;
149
150   uint16_t checksum;
151
152   uint32_t reserved;
153 };
154
155 /**
156  * Beginning of UDP packet.
157  */
158 struct udp_header
159 {
160   uint16_t src_port;
161
162   uint16_t dst_port;
163
164   uint16_t length;
165
166   uint16_t crc;
167 };
168
169 /**
170  * Socket we use to send our fake ICMP replies.
171  */
172 static int rawsock;
173
174 /**
175  * Target "dummy" address of the packet we pretend to respond to.
176  */
177 static struct in_addr dummy;
178
179 /**
180  * Our "source" port.
181  */
182 static uint16_t port;
183
184
185 /**
186  * CRC-16 for IP/ICMP headers.
187  *
188  * @param data what to calculate the CRC over
189  * @param bytes number of bytes in data (must be multiple of 2)
190  * @return the CRC 16.
191  */
192 static uint16_t
193 calc_checksum (const uint16_t * data, unsigned int bytes)
194 {
195   uint32_t sum;
196   unsigned int i;
197
198   sum = 0;
199   for (i = 0; i < bytes / 2; i++)
200     sum += data[i];
201   sum = (sum & 0xffff) + (sum >> 16);
202   sum = htons (0xffff - sum);
203   return sum;
204 }
205
206
207 /**
208  * Send an ICMP message to the target.
209  *
210  * @param my_ip source address
211  * @param other target address
212  */
213 static void
214 send_icmp_udp (const struct in_addr *my_ip, const struct in_addr *other)
215 {
216   char packet[sizeof (struct ip_header) * 2 +
217               sizeof (struct icmp_ttl_exceeded_header) +
218               sizeof (struct udp_header)];
219   struct ip_header ip_pkt;
220   struct icmp_ttl_exceeded_header icmp_pkt;
221   struct udp_header udp_pkt;
222   struct sockaddr_in dst;
223   size_t off;
224   int err;
225
226   /* ip header: send to (known) ip address */
227   off = 0;
228   ip_pkt.vers_ihl = 0x45;
229   ip_pkt.tos = 0;
230   ip_pkt.pkt_len = htons (sizeof (packet));
231   ip_pkt.id = htons (256);
232   ip_pkt.flags_frag_offset = 0;
233   ip_pkt.ttl = 128;
234   ip_pkt.proto = IPPROTO_ICMP;
235   ip_pkt.checksum = 0;
236   ip_pkt.src_ip = my_ip->s_addr;
237   ip_pkt.dst_ip = other->s_addr;
238   ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt,
239                                           sizeof (struct ip_header)));
240   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
241   off += sizeof (struct ip_header);
242
243   icmp_pkt.type = ICMP_TIME_EXCEEDED;
244   icmp_pkt.code = 0;
245   icmp_pkt.checksum = 0;
246   icmp_pkt.unused = 0;
247   memcpy (&packet[off], &icmp_pkt, sizeof (struct icmp_ttl_exceeded_header));
248   off += sizeof (struct icmp_ttl_exceeded_header);
249
250   /* ip header of the presumably 'lost' udp packet */
251   ip_pkt.vers_ihl = 0x45;
252   ip_pkt.tos = 0;
253   ip_pkt.pkt_len = htons (sizeof (struct ip_header) +
254                           sizeof (struct udp_header));
255   ip_pkt.id = htons (0);
256   ip_pkt.flags_frag_offset = 0;
257   ip_pkt.ttl = 128;
258   ip_pkt.proto = IPPROTO_UDP;
259   ip_pkt.checksum = 0;
260   ip_pkt.src_ip = other->s_addr;
261   ip_pkt.dst_ip = dummy.s_addr;
262   ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt,
263                                           sizeof (struct ip_header)));
264   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
265   off += sizeof (struct ip_header);
266
267   /* build UDP header */
268   udp_pkt.src_port = htons (NAT_TRAV_PORT);
269   udp_pkt.dst_port = htons (NAT_TRAV_PORT);
270   udp_pkt.length = htons (port);
271   udp_pkt.crc = 0;
272   memcpy (&packet[off], &udp_pkt, sizeof (struct udp_header));
273   off += sizeof (struct udp_header);
274
275   /* set ICMP checksum */
276   icmp_pkt.checksum =
277       htons (calc_checksum
278              ((uint16_t *) & packet[sizeof (struct ip_header)],
279               sizeof (struct icmp_ttl_exceeded_header) +
280               sizeof (struct ip_header) + sizeof (struct udp_header)));
281   memcpy (&packet[sizeof (struct ip_header)], &icmp_pkt,
282           sizeof (struct icmp_ttl_exceeded_header));
283
284   memset (&dst, 0, sizeof (dst));
285   dst.sin_family = AF_INET;
286 #if HAVE_SOCKADDR_IN_SIN_LEN
287   dst.sin_len = sizeof (struct sockaddr_in);
288 #endif
289   dst.sin_addr = *other;
290   err = sendto (rawsock,
291                 packet,
292                 sizeof (packet), 0, (struct sockaddr *) &dst, sizeof (dst));
293   if (err < 0)
294   {
295     fprintf (stderr, "sendto failed: %s\n", strerror (errno));
296   }
297   else if (sizeof (packet) != (size_t) err)
298   {
299     fprintf (stderr, "Error: partial send of ICMP message\n");
300   }
301 }
302
303
304 /**
305  * Send an ICMP message to the target.
306  *
307  * @param my_ip source address
308  * @param other target address
309  */
310 static void
311 send_icmp (const struct in_addr *my_ip, const struct in_addr *other)
312 {
313   struct ip_header ip_pkt;
314   struct icmp_ttl_exceeded_header icmp_ttl;
315   struct icmp_echo_header icmp_echo;
316   struct sockaddr_in dst;
317   char packet[sizeof (struct ip_header) * 2 +
318               sizeof (struct icmp_ttl_exceeded_header) +
319               sizeof (struct icmp_echo_header)];
320   size_t off;
321   int err;
322
323   /* ip header: send to (known) ip address */
324   off = 0;
325   ip_pkt.vers_ihl = 0x45;
326   ip_pkt.tos = 0;
327   ip_pkt.pkt_len = htons (sizeof (packet));
328   ip_pkt.id = htons (256);
329   ip_pkt.flags_frag_offset = 0;
330   ip_pkt.ttl = IPDEFTTL;
331   ip_pkt.proto = IPPROTO_ICMP;
332   ip_pkt.checksum = 0;
333   ip_pkt.src_ip = my_ip->s_addr;
334   ip_pkt.dst_ip = other->s_addr;
335   ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt,
336                                           sizeof (struct ip_header)));
337   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
338   off = sizeof (ip_pkt);
339
340   /* icmp reply: time exceeded */
341   icmp_ttl.type = ICMP_TIME_EXCEEDED;
342   icmp_ttl.code = 0;
343   icmp_ttl.checksum = 0;
344   icmp_ttl.unused = 0;
345   memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
346   off += sizeof (struct icmp_ttl_exceeded_header);
347
348   /* ip header of the presumably 'lost' udp packet */
349   ip_pkt.vers_ihl = 0x45;
350   ip_pkt.tos = 0;
351   ip_pkt.pkt_len =
352       htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
353   ip_pkt.id = htons (256);
354   ip_pkt.flags_frag_offset = 0;
355   ip_pkt.ttl = 1;               /* real TTL would be 1 on a time exceeded packet */
356   ip_pkt.proto = IPPROTO_ICMP;
357   ip_pkt.src_ip = other->s_addr;
358   ip_pkt.dst_ip = dummy.s_addr;
359   ip_pkt.checksum = 0;
360   ip_pkt.checksum = htons (calc_checksum ((uint16_t *) & ip_pkt,
361                                           sizeof (struct ip_header)));
362   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_header));
363   off += sizeof (struct ip_header);
364
365   icmp_echo.type = ICMP_ECHO;
366   icmp_echo.code = 0;
367   icmp_echo.reserved = htonl (port);
368   icmp_echo.checksum = 0;
369   icmp_echo.checksum = htons (calc_checksum ((uint16_t *) & icmp_echo,
370                                              sizeof (struct icmp_echo_header)));
371   memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
372
373   /* no go back to calculate ICMP packet checksum */
374   off = sizeof (struct ip_header);
375   icmp_ttl.checksum = htons (calc_checksum ((uint16_t *) & packet[off],
376                                             sizeof (struct
377                                                     icmp_ttl_exceeded_header) +
378                                             sizeof (struct ip_header) +
379                                             sizeof (struct icmp_echo_header)));
380   memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
381
382   /* prepare for transmission */
383   memset (&dst, 0, sizeof (dst));
384   dst.sin_family = AF_INET;
385 #if HAVE_SOCKADDR_IN_SIN_LEN
386   dst.sin_len = sizeof (struct sockaddr_in);
387 #endif
388   dst.sin_addr = *other;
389   err = sendto (rawsock,
390                 packet,
391                 sizeof (packet), 0, (struct sockaddr *) &dst, sizeof (dst));
392   if (err < 0)
393   {
394     fprintf (stderr, "sendto failed: %s\n", strerror (errno));
395   }
396   else if (sizeof (packet) != (size_t) err)
397   {
398     fprintf (stderr, "Error: partial send of ICMP message\n");
399   }
400 }
401
402
403 /**
404  * Create an ICMP raw socket for writing.
405  *
406  * @return -1 on error
407  */
408 static int
409 make_raw_socket ()
410 {
411   const int one = 1;
412   int ret;
413
414   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
415   if (-1 == ret)
416   {
417     fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
418     return -1;
419   }
420   if (0 != setsockopt (ret, SOL_SOCKET, SO_BROADCAST,
421                        (char *) &one, sizeof (one)))
422   {
423     fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
424     close (ret);
425     return -1;
426   }
427   if (0 != setsockopt (ret, IPPROTO_IP, IP_HDRINCL,
428                        (char *) &one, sizeof (one)))
429   {
430     fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
431     close (ret);
432     return -1;
433   }
434   return ret;
435 }
436
437
438 int
439 main (int argc, char *const *argv)
440 {
441   struct in_addr external;
442   struct in_addr target;
443   uid_t uid;
444   unsigned int p;
445
446   if (4 != argc)
447   {
448     fprintf (stderr,
449              "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
450     return 1;
451   }
452   if ((1 != inet_pton (AF_INET, argv[1], &external)) ||
453       (1 != inet_pton (AF_INET, argv[2], &target)))
454   {
455     fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
456     return 1;
457   }
458   if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
459   {
460     fprintf (stderr, "Error parsing port value `%s'\n", argv[3]);
461     return 1;
462   }
463   port = (uint16_t) p;
464   if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
465   {
466     fprintf (stderr, "Internal error converting dummy IP to binary.\n");
467     return 2;
468   }
469   if (-1 == (rawsock = make_raw_socket ()))
470     return 2;
471   uid = getuid ();
472   if (0 != setresuid (uid, uid, uid))
473   {
474     fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
475     /* not critical, continue anyway */
476   }
477   send_icmp (&external, &target);
478   send_icmp_udp (&external, &target);
479   close (rawsock);
480   return 0;
481 }
482
483 /* end of gnunet-helper-nat-client.c */