disable local addresses
[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 =
239       htons (calc_checksum ((uint16_t *) & ip_pkt, 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 =
254       htons (sizeof (struct ip_header) + 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 =
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   /* 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 =
291       sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
292               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 =
336       htons (calc_checksum ((uint16_t *) & ip_pkt, 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 =
361       htons (calc_checksum ((uint16_t *) & ip_pkt, 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 =
370       htons (calc_checksum
371              ((uint16_t *) & icmp_echo, sizeof (struct icmp_echo_header)));
372   memcpy (&packet[off], &icmp_echo, sizeof (struct icmp_echo_header));
373
374   /* no go back to calculate ICMP packet checksum */
375   off = sizeof (struct ip_header);
376   icmp_ttl.checksum =
377       htons (calc_checksum
378              ((uint16_t *) & packet[off],
379               sizeof (struct icmp_ttl_exceeded_header) +
380               sizeof (struct ip_header) + sizeof (struct icmp_echo_header)));
381   memcpy (&packet[off], &icmp_ttl, sizeof (struct icmp_ttl_exceeded_header));
382
383   /* prepare for transmission */
384   memset (&dst, 0, sizeof (dst));
385   dst.sin_family = AF_INET;
386 #if HAVE_SOCKADDR_IN_SIN_LEN
387   dst.sin_len = sizeof (struct sockaddr_in);
388 #endif
389   dst.sin_addr = *other;
390   err =
391       sendto (rawsock, packet, sizeof (packet), 0, (struct sockaddr *) &dst,
392               sizeof (dst));
393   if (err < 0)
394   {
395     fprintf (stderr, "sendto failed: %s\n", strerror (errno));
396   }
397   else if (sizeof (packet) != (size_t) err)
398   {
399     fprintf (stderr, "Error: partial send of ICMP message\n");
400   }
401 }
402
403
404 /**
405  * Create an ICMP raw socket for writing.
406  *
407  * @return -1 on error
408  */
409 static int
410 make_raw_socket ()
411 {
412   const int one = 1;
413   int ret;
414
415   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
416   if (-1 == ret)
417   {
418     fprintf (stderr, "Error opening RAW socket: %s\n", strerror (errno));
419     return -1;
420   }
421   if (0 !=
422       setsockopt (ret, SOL_SOCKET, SO_BROADCAST, (char *) &one, sizeof (one)))
423   {
424     fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
425     close (ret);
426     return -1;
427   }
428   if (0 !=
429       setsockopt (ret, IPPROTO_IP, IP_HDRINCL, (char *) &one, sizeof (one)))
430   {
431     fprintf (stderr, "setsockopt failed: %s\n", strerror (errno));
432     close (ret);
433     return -1;
434   }
435   return ret;
436 }
437
438
439 int
440 main (int argc, char *const *argv)
441 {
442   struct in_addr external;
443   struct in_addr target;
444   uid_t uid;
445   unsigned int p;
446
447   if (4 != argc)
448   {
449     fprintf (stderr,
450              "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
451     return 1;
452   }
453   if ((1 != inet_pton (AF_INET, argv[1], &external)) ||
454       (1 != inet_pton (AF_INET, argv[2], &target)))
455   {
456     fprintf (stderr, "Error parsing IPv4 address: %s\n", strerror (errno));
457     return 1;
458   }
459   if ((1 != sscanf (argv[3], "%u", &p)) || (0 == p) || (0xFFFF < p))
460   {
461     fprintf (stderr, "Error parsing port value `%s'\n", argv[3]);
462     return 1;
463   }
464   port = (uint16_t) p;
465   if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
466   {
467     fprintf (stderr, "Internal error converting dummy IP to binary.\n");
468     return 2;
469   }
470   if (-1 == (rawsock = make_raw_socket ()))
471     return 2;
472   uid = getuid ();
473   if (0 != setresuid (uid, uid, uid))
474   {
475     fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
476     /* not critical, continue anyway */
477   }
478   send_icmp (&external, &target);
479   send_icmp_udp (&external, &target);
480   close (rawsock);
481   return 0;
482 }
483
484 /* end of gnunet-helper-nat-client.c */