add UDP inside ICMP for NAT punch
[oweals/gnunet.git] / src / transport / gnunet-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/transport/gnunet-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  */
42 #define _GNU_SOURCE
43 #include <sys/types.h> 
44 #include <sys/socket.h>
45 #include <arpa/inet.h>
46 #include <sys/types.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <errno.h>
51 #include <stdlib.h>
52 #include <stdint.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_icmp.h>
55 #include <netinet/in.h> 
56
57 /**
58  * Must match IP given in the server.
59  */
60 #define DUMMY_IP "1.2.3.4"
61 #define HAVE_PORT 1
62
63 #define NAT_TRAV_PORT 22225
64
65 struct ip_packet 
66 {
67   uint8_t vers_ihl;
68   uint8_t tos;
69   uint16_t pkt_len;
70   uint16_t id;
71   uint16_t flags_frag_offset;
72   uint8_t ttl;
73   uint8_t proto;
74   uint16_t checksum;
75   uint32_t src_ip;
76   uint32_t dst_ip;
77 };
78
79 struct icmp_packet 
80 {
81   uint8_t type;
82   uint8_t code;
83   uint16_t checksum;
84   uint32_t reserved;
85
86 };
87
88 struct icmp_echo_packet
89 {
90   uint8_t type;
91   uint8_t code;
92   uint16_t checksum;
93   uint32_t reserved;
94   uint32_t data;
95 };
96
97 struct udp_packet
98 {
99   uint16_t src_port;
100
101   uint16_t dst_port;
102
103   uint32_t length;
104 };
105
106 static int rawsock;
107
108 static struct in_addr dummy;
109  
110 static struct in_addr target;
111
112 #if HAVE_PORT
113   static uint32_t port;
114 #endif
115
116 static uint16_t 
117 calc_checksum(const uint16_t *data, 
118               unsigned int bytes)
119 {
120   uint32_t sum;
121   unsigned int i;
122
123   sum = 0;
124   for (i=0;i<bytes/2;i++) 
125     sum += data[i];        
126   sum = (sum & 0xffff) + (sum >> 16);
127   sum = htons(0xffff - sum);
128   return sum;
129 }
130
131
132
133 #if HAVE_PORT
134
135 static void
136 make_echo (const struct in_addr *src_ip,
137            struct icmp_echo_packet *echo, uint32_t num)
138 {
139   memset(echo, 0, sizeof(struct icmp_echo_packet));
140   echo->type = ICMP_ECHO;
141   echo->code = 0;
142   echo->reserved = 0;
143   echo->checksum = 0;
144   echo->data = htons(num);
145   echo->checksum = htons(calc_checksum((uint16_t*)echo, 
146                                        sizeof (struct icmp_echo_packet)));
147 }
148 #else
149 static void
150 make_echo (const struct in_addr *src_ip,
151            struct icmp_packet *echo)
152 {
153   memset(echo, 0, sizeof(struct icmp_packet));
154   echo->type = ICMP_ECHO;
155   echo->code = 0;
156   echo->reserved = 0;
157   echo->checksum = 0;
158   echo->checksum = htons(calc_checksum((uint16_t*)echo,
159                                        sizeof (struct icmp_packet)));
160 }
161 #endif
162
163 /**
164  * Send an ICMP message to the target.
165  *
166  * @param my_ip source address
167  * @param other target address
168  */
169 static void
170 send_icmp_udp (const struct in_addr *my_ip,
171                const struct in_addr *other)
172 {
173   struct ip_packet ip_pkt;
174   struct icmp_packet icmp_pkt;
175   struct udp_packet udp_pkt;
176
177   struct sockaddr_in dst;
178   char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
179
180   size_t off;
181   int err;
182
183   /* ip header: send to (known) ip address */
184   off = 0;
185   memset(&ip_pkt, 0, sizeof(ip_pkt));
186   ip_pkt.vers_ihl = 0x45;
187   ip_pkt.tos = 0;
188   ip_pkt.pkt_len = htons(sizeof (packet));
189   ip_pkt.id = htons(256);
190   ip_pkt.flags_frag_offset = 0;
191   ip_pkt.ttl = 128;
192   ip_pkt.proto = IPPROTO_ICMP;
193   ip_pkt.checksum = 0;
194   ip_pkt.src_ip = my_ip->s_addr;
195   ip_pkt.dst_ip = other->s_addr;
196   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
197   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
198   off += sizeof(ip_pkt);
199
200   /* ip header of the presumably 'lost' udp packet */
201   ip_pkt.vers_ihl = 0x45;
202   ip_pkt.tos = 0;
203   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
204
205   icmp_pkt.type = 11; /* TTL exceeded */
206   icmp_pkt.code = 0;
207   icmp_pkt.checksum = 0;
208   icmp_pkt.reserved = 0;
209   memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
210   off += sizeof(icmp_pkt);
211
212   /* build inner IP header */
213   memset(&ip_pkt, 0, sizeof(ip_pkt));
214   ip_pkt.vers_ihl = 0x45;
215   ip_pkt.tos = 0;
216   ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
217   ip_pkt.id = htons(0);
218   ip_pkt.flags_frag_offset = 0;
219   ip_pkt.ttl = 128;
220   ip_pkt.proto = IPPROTO_UDP;
221   ip_pkt.checksum = 0;
222   ip_pkt.src_ip = other->s_addr;
223   ip_pkt.dst_ip = dummy.s_addr;
224   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
225   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
226   off += sizeof(ip_pkt);
227
228   /* build UDP header */
229   udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
230   udp_pkt.dst_port = htons(NAT_TRAV_PORT);
231
232   memset(&udp_pkt.length, 0, sizeof(uint32_t));
233 #if HAVE_PORT
234   udp_pkt.length = htonl(port);
235 #endif
236   memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
237   off += sizeof(udp_pkt);
238
239   /* set ICMP checksum */
240   icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
241                             sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
242   memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
243
244
245   memset (&dst, 0, sizeof (dst));
246   dst.sin_family = AF_INET;
247   dst.sin_addr = *other;
248   err = sendto(rawsock,
249                packet,
250                off, 0,
251                (struct sockaddr*)&dst,
252                sizeof(dst));
253
254   if (err < 0)
255     {
256       fprintf(stderr,
257               "sendto failed: %s\n", strerror(errno));
258     }
259   else if (err != off)
260     {
261       fprintf(stderr,
262               "Error: partial send of ICMP message\n");
263     }
264 }
265
266
267 /**
268  * Send an ICMP message to the target.
269  *
270  * @param my_ip source address
271  * @param other target address
272  */
273 static void
274 send_icmp (const struct in_addr *my_ip,
275            const struct in_addr *other)
276 {
277   struct ip_packet ip_pkt;
278   struct icmp_packet *icmp_pkt;
279 #if HAVE_PORT
280   struct icmp_echo_packet icmp_echo;
281 #else
282   struct icmp_packet icmp_echo;
283 #endif
284   struct sockaddr_in dst;
285 #if HAVE_PORT
286   char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
287 #else
288   char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet)*2];
289 #endif
290   size_t off;
291   int err;
292
293   /* ip header: send to (known) ip address */
294   off = 0;
295   memset(&ip_pkt, 0, sizeof(ip_pkt));
296   ip_pkt.vers_ihl = 0x45;
297   ip_pkt.tos = 0;
298   ip_pkt.pkt_len = sizeof (packet); /* huh? */
299   ip_pkt.id = 1; 
300   ip_pkt.flags_frag_offset = 0;
301   ip_pkt.ttl = IPDEFTTL;
302   ip_pkt.proto = IPPROTO_ICMP;
303   ip_pkt.checksum = 0; 
304   ip_pkt.src_ip = my_ip->s_addr;
305   ip_pkt.dst_ip = other->s_addr;
306   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
307   memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
308   off += sizeof (ip_pkt);
309   /* icmp reply: time exceeded */
310   icmp_pkt = (struct icmp_packet*) &packet[off];
311   memset(icmp_pkt, 0, sizeof(struct icmp_packet));
312   icmp_pkt->type = ICMP_TIME_EXCEEDED;
313   icmp_pkt->code = 0; 
314   icmp_pkt->reserved = 0;
315   icmp_pkt->checksum = 0;
316
317   off += sizeof (struct icmp_packet);
318
319   /* ip header of the presumably 'lost' udp packet */
320   ip_pkt.vers_ihl = 0x45;
321   ip_pkt.tos = 0;
322 #if HAVE_PORT
323   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
324 #else
325   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_packet));
326 #endif
327
328   ip_pkt.id = 1; 
329   ip_pkt.flags_frag_offset = 0;
330   ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
331   ip_pkt.proto = IPPROTO_ICMP;
332   ip_pkt.src_ip = other->s_addr;
333   ip_pkt.dst_ip = dummy.s_addr;
334   ip_pkt.checksum = 0;
335   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));  
336   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
337   off += sizeof (struct ip_packet);
338
339 #if HAVE_PORT
340   make_echo (other, &icmp_echo, port);
341   memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
342   off += sizeof (struct icmp_echo_packet);
343 #else
344   make_echo (other, &icmp_echo);
345   memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_packet));
346   off += sizeof (struct icmp_packet);
347 #endif
348
349 #if HAVE_PORT
350   icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
351                                              sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
352
353 #else
354   icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt, 
355                                              sizeof (struct icmp_packet)*2 + sizeof(struct ip_packet)));
356
357 #endif
358
359
360
361   memset (&dst, 0, sizeof (dst));
362   dst.sin_family = AF_INET;
363   dst.sin_addr = *other;
364   err = sendto(rawsock, 
365                packet, 
366                off, 0, 
367                (struct sockaddr*)&dst, 
368                sizeof(dst)); /* or sizeof 'struct sockaddr'? */
369   if (err < 0) 
370     {
371       fprintf(stderr,
372               "sendto failed: %s\n", strerror(errno));
373     }
374   else if (err != off) 
375     {
376       fprintf(stderr,
377               "Error: partial send of ICMP message\n");
378     }
379 }
380
381
382 static int
383 make_raw_socket ()
384 {
385   const int one = 1;
386   int ret;
387
388   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
389   if (-1 == ret)
390     {
391       fprintf (stderr,
392                "Error opening RAW socket: %s\n",
393                strerror (errno));
394       return -1;
395     }  
396   if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
397                  (char *)&one, sizeof(one)) == -1)
398     fprintf(stderr,
399             "setsockopt failed: %s\n",
400             strerror (errno));
401   if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
402                  (char *)&one, sizeof(one)) == -1)
403     fprintf(stderr,
404             "setsockopt failed: %s\n",
405             strerror (errno));
406   return ret;
407 }
408
409
410 int
411 main (int argc, char *const *argv)
412 {
413   struct in_addr external;
414   uid_t uid;
415
416   if (-1 == (rawsock = make_raw_socket()))
417     return 1;     
418   uid = getuid ();
419   if (0 != setresuid (uid, uid, uid))
420     fprintf (stderr,
421              "Failed to setresuid: %s\n",
422              strerror (errno));
423 #if HAVE_PORT
424   if (argc != 4)
425     {
426       fprintf (stderr,
427                "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
428       return 1;
429     }
430   port = atoi(argv[3]);
431 #else
432   if (argc != 3)
433     {
434       fprintf (stderr,
435                "This program must be started with our IP and the targets external IP as arguments.\n");
436       return 1;
437     }
438 #endif
439   if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
440        (1 != inet_pton (AF_INET, argv[2], &target)) )
441     {
442       fprintf (stderr,
443                "Error parsing IPv4 address: %s\n",
444                strerror (errno));
445       return 1;
446     }
447   if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) abort ();
448   send_icmp (&external,
449              &target);
450   send_icmp_udp (&external,
451              &target);
452   close (rawsock);
453   return 0;
454 }
455
456 /* end of gnunet-nat-client.c */