2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
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
40 * - Christian Grothoff
45 /* Just needed for HAVE_SOCKADDR_IN_SIN_LEN test macro! */
46 #include "gnunet_config.h"
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <arpa/inet.h>
51 #include <sys/types.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip_icmp.h>
60 #include <netinet/in.h>
63 * Must match IP given in the server.
65 #define DUMMY_IP "192.0.2.86"
67 #define NAT_TRAV_PORT 22225
76 * Version (4 bits) + Internet header length (4 bits)
96 * Flags (3 bits) + Fragment offset (13 bits)
98 uint16_t flags_frag_offset;
121 * Destination address
127 * Format of ICMP packet.
140 struct icmp_echo_packet
154 * Beginning of UDP packet.
166 * Socket we use to send our fake ICMP replies.
171 * Target "dummy" address of the packet we pretend to respond to.
173 static struct in_addr dummy;
178 static uint16_t port;
182 * CRC-16 for IP/ICMP headers.
184 * @param data what to calculate the CRC over
185 * @param bytes number of bytes in data (must be multiple of 2)
186 * @return the CRC 16.
189 calc_checksum(const uint16_t *data,
196 for (i=0;i<bytes/2;i++)
198 sum = (sum & 0xffff) + (sum >> 16);
199 sum = htons(0xffff - sum);
205 * Send an ICMP message to the target.
207 * @param my_ip source address
208 * @param other target address
211 send_icmp_udp (const struct in_addr *my_ip,
212 const struct in_addr *other)
214 struct ip_packet ip_pkt;
215 struct icmp_packet icmp_pkt;
216 struct udp_packet udp_pkt;
218 struct sockaddr_in dst;
219 char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
224 /* ip header: send to (known) ip address */
226 memset(&ip_pkt, 0, sizeof(ip_pkt));
227 ip_pkt.vers_ihl = 0x45;
229 ip_pkt.pkt_len = htons(sizeof (packet));
230 ip_pkt.id = htons(256);
231 ip_pkt.flags_frag_offset = 0;
233 ip_pkt.proto = IPPROTO_ICMP;
235 ip_pkt.src_ip = my_ip->s_addr;
236 ip_pkt.dst_ip = other->s_addr;
237 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
238 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
239 off += sizeof(ip_pkt);
241 /* ip header of the presumably 'lost' udp packet */
242 ip_pkt.vers_ihl = 0x45;
244 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
246 icmp_pkt.type = 11; /* TTL exceeded */
248 icmp_pkt.checksum = 0;
249 icmp_pkt.reserved = 0;
250 memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
251 off += sizeof(icmp_pkt);
253 /* build inner IP header */
254 memset(&ip_pkt, 0, sizeof(ip_pkt));
255 ip_pkt.vers_ihl = 0x45;
257 ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
258 ip_pkt.id = htons(0);
259 ip_pkt.flags_frag_offset = 0;
261 ip_pkt.proto = IPPROTO_UDP;
263 ip_pkt.src_ip = other->s_addr;
264 ip_pkt.dst_ip = dummy.s_addr;
265 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
266 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
267 off += sizeof(ip_pkt);
269 /* build UDP header */
270 udp_pkt.src_port = htons(NAT_TRAV_PORT);
271 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
273 memset(&udp_pkt.length, 0, sizeof(uint32_t));
274 udp_pkt.length = htons (port);
275 memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
276 off += sizeof(udp_pkt);
278 /* set ICMP checksum */
279 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
280 sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
281 memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
284 memset (&dst, 0, sizeof (dst));
285 dst.sin_family = AF_INET;
286 dst.sin_addr = *other;
287 err = sendto(rawsock,
290 (struct sockaddr*)&dst,
296 "sendto failed: %s\n", strerror(errno));
301 "Error: partial send of ICMP message\n");
307 * Send an ICMP message to the target.
309 * @param my_ip source address
310 * @param other target address
313 send_icmp (const struct in_addr *my_ip,
314 const struct in_addr *other)
316 struct ip_packet ip_pkt;
317 struct icmp_packet icmp_pkt;
318 struct icmp_echo_packet icmp_echo;
319 struct sockaddr_in dst;
320 char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
325 /* ip header: send to (known) ip address */
326 ip_pkt.vers_ihl = 0x45;
328 ip_pkt.pkt_len = sizeof (packet); /* huh? */
330 ip_pkt.flags_frag_offset = 0;
331 ip_pkt.ttl = IPDEFTTL;
332 ip_pkt.proto = IPPROTO_ICMP;
334 ip_pkt.src_ip = my_ip->s_addr;
335 ip_pkt.dst_ip = other->s_addr;
336 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
337 memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
338 off = sizeof (ip_pkt);
340 /* icmp reply: time exceeded */
341 icmp_pkt.type = ICMP_TIME_EXCEEDED;
343 icmp_pkt.reserved = 0;
344 icmp_pkt.checksum = 0;
345 memcpy (&packet[off],
347 sizeof (struct icmp_packet));
348 off += sizeof (struct icmp_packet);
350 /* ip header of the presumably 'lost' udp packet */
351 ip_pkt.vers_ihl = 0x45;
353 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
355 ip_pkt.flags_frag_offset = 0;
356 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
357 ip_pkt.proto = IPPROTO_ICMP;
358 ip_pkt.src_ip = other->s_addr;
359 ip_pkt.dst_ip = dummy.s_addr;
361 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
362 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
363 off += sizeof (struct ip_packet);
365 icmp_echo.type = ICMP_ECHO;
367 icmp_echo.reserved = 0;
368 icmp_echo.checksum = 0;
369 icmp_echo.data = htons(port);
370 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
371 sizeof (struct icmp_echo_packet)));
372 memcpy (&packet[off],
374 sizeof(struct icmp_echo_packet));
376 /* no go back to calculate ICMP packet checksum */
377 off = sizeof (ip_pkt);
378 icmp_pkt.checksum = htons(calc_checksum(&packet[off],
379 sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
380 memcpy (&packet[off],
382 sizeof (struct icmp_packet));
384 /* prepare for transmission */
385 memset (&dst, 0, sizeof (dst));
386 dst.sin_family = AF_INET;
387 #if HAVE_SOCKADDR_IN_SIN_LEN
388 dst.sin_len = sizeof (struct sockaddr_in);
390 dst.sin_addr = *other;
391 err = sendto(rawsock,
394 (struct sockaddr*)&dst,
395 sizeof(struct sockaddr_in));
399 "sendto failed: %s\n", strerror(errno));
404 "Error: partial send of ICMP message\n");
410 * Create an ICMP raw socket for writing.
412 * @return -1 on error
420 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
424 "Error opening RAW socket: %s\n",
428 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
429 (char *)&one, sizeof(one)) == -1)
432 "setsockopt failed: %s\n",
437 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
438 (char *)&one, sizeof(one)) == -1)
441 "setsockopt failed: %s\n",
451 main (int argc, char *const *argv)
453 struct in_addr external;
454 struct in_addr target;
461 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
464 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
465 (1 != inet_pton (AF_INET, argv[2], &target)) )
468 "Error parsing IPv4 address: %s\n",
472 if ( (1 != sscanf (argv[3], "%u", &p) ) ||
477 "Error parsing port value `%s'\n",
482 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
485 "Internal error converting dummy IP to binary.\n");
488 if (-1 == (rawsock = make_raw_socket()))
491 if (0 != setresuid (uid, uid, uid))
494 "Failed to setresuid: %s\n",
496 /* not critical, continue anyway */
498 send_icmp (&external,
500 send_icmp_udp (&external,
506 /* end of gnunet-nat-client.c */