blame where blame is due
[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  * - Nathan Evans
42  */
43 #define _GNU_SOURCE
44 #include <sys/types.h> 
45 #include <sys/socket.h>
46 #include <arpa/inet.h>
47 #include <sys/types.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <stdlib.h>
53 #include <stdint.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_icmp.h>
56 #include <netinet/in.h> 
57
58 /**
59  * Must match IP given in the server.
60  */
61 #define DUMMY_IP "192.0.2.86"
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 uint32_t port;
111
112 static uint16_t 
113 calc_checksum(const uint16_t *data, 
114               unsigned int bytes)
115 {
116   uint32_t sum;
117   unsigned int i;
118
119   sum = 0;
120   for (i=0;i<bytes/2;i++) 
121     sum += data[i];        
122   sum = (sum & 0xffff) + (sum >> 16);
123   sum = htons(0xffff - sum);
124   return sum;
125 }
126
127
128 static void
129 make_echo (const struct in_addr *src_ip,
130            struct icmp_echo_packet *echo, uint32_t num)
131 {
132   memset(echo, 0, sizeof(struct icmp_echo_packet));
133   echo->type = ICMP_ECHO;
134   echo->code = 0;
135   echo->reserved = 0;
136   echo->checksum = 0;
137   echo->data = htons(num);
138   echo->checksum = htons(calc_checksum((uint16_t*)echo, 
139                                        sizeof (struct icmp_echo_packet)));
140 }
141
142
143 /**
144  * Send an ICMP message to the target.
145  *
146  * @param my_ip source address
147  * @param other target address
148  */
149 static void
150 send_icmp_udp (const struct in_addr *my_ip,
151                const struct in_addr *other)
152 {
153   struct ip_packet ip_pkt;
154   struct icmp_packet icmp_pkt;
155   struct udp_packet udp_pkt;
156
157   struct sockaddr_in dst;
158   char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
159
160   size_t off;
161   int err;
162
163   /* ip header: send to (known) ip address */
164   off = 0;
165   memset(&ip_pkt, 0, sizeof(ip_pkt));
166   ip_pkt.vers_ihl = 0x45;
167   ip_pkt.tos = 0;
168   ip_pkt.pkt_len = htons(sizeof (packet));
169   ip_pkt.id = htons(256);
170   ip_pkt.flags_frag_offset = 0;
171   ip_pkt.ttl = 128;
172   ip_pkt.proto = IPPROTO_ICMP;
173   ip_pkt.checksum = 0;
174   ip_pkt.src_ip = my_ip->s_addr;
175   ip_pkt.dst_ip = other->s_addr;
176   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
177   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
178   off += sizeof(ip_pkt);
179
180   /* ip header of the presumably 'lost' udp packet */
181   ip_pkt.vers_ihl = 0x45;
182   ip_pkt.tos = 0;
183   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
184
185   icmp_pkt.type = 11; /* TTL exceeded */
186   icmp_pkt.code = 0;
187   icmp_pkt.checksum = 0;
188   icmp_pkt.reserved = 0;
189   memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
190   off += sizeof(icmp_pkt);
191
192   /* build inner IP header */
193   memset(&ip_pkt, 0, sizeof(ip_pkt));
194   ip_pkt.vers_ihl = 0x45;
195   ip_pkt.tos = 0;
196   ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
197   ip_pkt.id = htons(0);
198   ip_pkt.flags_frag_offset = 0;
199   ip_pkt.ttl = 128;
200   ip_pkt.proto = IPPROTO_UDP;
201   ip_pkt.checksum = 0;
202   ip_pkt.src_ip = other->s_addr;
203   ip_pkt.dst_ip = dummy.s_addr;
204   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
205   memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
206   off += sizeof(ip_pkt);
207
208   /* build UDP header */
209   udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
210   udp_pkt.dst_port = htons(NAT_TRAV_PORT);
211
212   memset(&udp_pkt.length, 0, sizeof(uint32_t));
213   udp_pkt.length = htonl(port);
214   memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
215   off += sizeof(udp_pkt);
216
217   /* set ICMP checksum */
218   icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
219                             sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
220   memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
221
222
223   memset (&dst, 0, sizeof (dst));
224   dst.sin_family = AF_INET;
225   dst.sin_addr = *other;
226   err = sendto(rawsock,
227                packet,
228                off, 0,
229                (struct sockaddr*)&dst,
230                sizeof(dst));
231
232   if (err < 0)
233     {
234       fprintf(stderr,
235               "sendto failed: %s\n", strerror(errno));
236     }
237   else if (err != off)
238     {
239       fprintf(stderr,
240               "Error: partial send of ICMP message\n");
241     }
242 }
243
244
245 /**
246  * Send an ICMP message to the target.
247  *
248  * @param my_ip source address
249  * @param other target address
250  */
251 static void
252 send_icmp (const struct in_addr *my_ip,
253            const struct in_addr *other)
254 {
255   struct ip_packet ip_pkt;
256   struct icmp_packet *icmp_pkt;
257   struct icmp_echo_packet icmp_echo;
258   struct sockaddr_in dst;
259   char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
260
261   size_t off;
262   int err;
263
264   /* ip header: send to (known) ip address */
265   off = 0;
266   memset(&ip_pkt, 0, sizeof(ip_pkt));
267   ip_pkt.vers_ihl = 0x45;
268   ip_pkt.tos = 0;
269   ip_pkt.pkt_len = sizeof (packet); /* huh? */
270   ip_pkt.id = 1; 
271   ip_pkt.flags_frag_offset = 0;
272   ip_pkt.ttl = IPDEFTTL;
273   ip_pkt.proto = IPPROTO_ICMP;
274   ip_pkt.checksum = 0; 
275   ip_pkt.src_ip = my_ip->s_addr;
276   ip_pkt.dst_ip = other->s_addr;
277   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
278   memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
279   off += sizeof (ip_pkt);
280   /* icmp reply: time exceeded */
281   icmp_pkt = (struct icmp_packet*) &packet[off];
282   memset(icmp_pkt, 0, sizeof(struct icmp_packet));
283   icmp_pkt->type = ICMP_TIME_EXCEEDED;
284   icmp_pkt->code = 0; 
285   icmp_pkt->reserved = 0;
286   icmp_pkt->checksum = 0;
287
288   off += sizeof (struct icmp_packet);
289
290   /* ip header of the presumably 'lost' udp packet */
291   ip_pkt.vers_ihl = 0x45;
292   ip_pkt.tos = 0;
293   ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
294
295   ip_pkt.id = 1; 
296   ip_pkt.flags_frag_offset = 0;
297   ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
298   ip_pkt.proto = IPPROTO_ICMP;
299   ip_pkt.src_ip = other->s_addr;
300   ip_pkt.dst_ip = dummy.s_addr;
301   ip_pkt.checksum = 0;
302   ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));  
303   memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
304   off += sizeof (struct ip_packet);
305
306   make_echo (other, &icmp_echo, port);
307   memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
308   off += sizeof (struct icmp_echo_packet);
309
310   icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
311                                              sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
312
313   memset (&dst, 0, sizeof (dst));
314   dst.sin_family = AF_INET;
315   dst.sin_addr = *other;
316   err = sendto(rawsock, 
317                packet, 
318                off, 0, 
319                (struct sockaddr*)&dst, 
320                sizeof(dst)); /* or sizeof 'struct sockaddr'? */
321   if (err < 0) 
322     {
323       fprintf(stderr,
324               "sendto failed: %s\n", strerror(errno));
325     }
326   else if (err != off) 
327     {
328       fprintf(stderr,
329               "Error: partial send of ICMP message\n");
330     }
331 }
332
333
334 static int
335 make_raw_socket ()
336 {
337   const int one = 1;
338   int ret;
339
340   ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
341   if (-1 == ret)
342     {
343       fprintf (stderr,
344                "Error opening RAW socket: %s\n",
345                strerror (errno));
346       return -1;
347     }  
348   if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
349                  (char *)&one, sizeof(one)) == -1)
350     fprintf(stderr,
351             "setsockopt failed: %s\n",
352             strerror (errno));
353   if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
354                  (char *)&one, sizeof(one)) == -1)
355     fprintf(stderr,
356             "setsockopt failed: %s\n",
357             strerror (errno));
358   return ret;
359 }
360
361
362 int
363 main (int argc, char *const *argv)
364 {
365   struct in_addr external;
366   struct in_addr target;
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 */