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