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