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
42 * - Benjamin Kuperman (22 Aug 2010)
45 /* Just needed for HAVE_SOCKADDR_IN_SIN_LEN test macro! */
46 #include "gnunet_config.h"
50 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <arpa/inet.h>
53 #include <sys/types.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip_icmp.h>
62 #include <netinet/in.h>
65 * Must match IP given in the server.
67 #define DUMMY_IP "192.0.2.86"
69 #define NAT_TRAV_PORT 22225
78 * Version (4 bits) + Internet header length (4 bits)
98 * Flags (3 bits) + Fragment offset (13 bits)
100 uint16_t flags_frag_offset;
123 * Destination address
129 * Format of ICMP packet.
131 struct icmp_ttl_exceeded_header
141 /* followed by original payload */
144 struct icmp_echo_header
156 * Beginning of UDP packet.
170 * Socket we use to send our fake ICMP replies.
175 * Target "dummy" address of the packet we pretend to respond to.
177 static struct in_addr dummy;
182 static uint16_t port;
186 * CRC-16 for IP/ICMP headers.
188 * @param data what to calculate the CRC over
189 * @param bytes number of bytes in data (must be multiple of 2)
190 * @return the CRC 16.
193 calc_checksum (const uint16_t *data,
200 for (i=0;i<bytes/2;i++)
202 sum = (sum & 0xffff) + (sum >> 16);
203 sum = htons(0xffff - sum);
209 * Send an ICMP message to the target.
211 * @param my_ip source address
212 * @param other target address
215 send_icmp_udp (const struct in_addr *my_ip,
216 const struct in_addr *other)
218 char packet[sizeof(struct ip_header) * 2 +
219 sizeof(struct icmp_ttl_exceeded_header) +
220 sizeof(struct udp_header)];
221 struct ip_header ip_pkt;
222 struct icmp_ttl_exceeded_header icmp_pkt;
223 struct udp_header udp_pkt;
224 struct sockaddr_in dst;
228 /* ip header: send to (known) ip address */
230 ip_pkt.vers_ihl = 0x45;
232 ip_pkt.pkt_len = htons (sizeof (packet));
233 ip_pkt.id = htons(256);
234 ip_pkt.flags_frag_offset = 0;
236 ip_pkt.proto = IPPROTO_ICMP;
238 ip_pkt.src_ip = my_ip->s_addr;
239 ip_pkt.dst_ip = other->s_addr;
240 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
241 sizeof (struct ip_header)));
244 sizeof(struct ip_header));
245 off += sizeof(struct ip_header);
247 icmp_pkt.type = ICMP_TIME_EXCEEDED;
249 icmp_pkt.checksum = 0;
253 sizeof(struct icmp_ttl_exceeded_header));
254 off += sizeof(struct icmp_ttl_exceeded_header);
256 /* ip header of the presumably 'lost' udp packet */
257 ip_pkt.vers_ihl = 0x45;
259 ip_pkt.pkt_len = htons(sizeof (struct ip_header) +
260 sizeof (struct udp_header));
261 ip_pkt.id = htons(0);
262 ip_pkt.flags_frag_offset = 0;
264 ip_pkt.proto = IPPROTO_UDP;
266 ip_pkt.src_ip = other->s_addr;
267 ip_pkt.dst_ip = dummy.s_addr;
268 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
269 sizeof (struct ip_header)));
272 sizeof(struct ip_header));
273 off += sizeof(struct ip_header);
275 /* build UDP header */
276 udp_pkt.src_port = htons(NAT_TRAV_PORT);
277 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
278 udp_pkt.length = htons (port);
282 sizeof(struct udp_header));
283 off += sizeof(struct udp_header);
285 /* set ICMP checksum */
286 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(struct ip_header)],
287 sizeof (struct icmp_ttl_exceeded_header) +
288 sizeof (struct ip_header) +
289 sizeof (struct udp_header)));
290 memcpy (&packet[sizeof(struct ip_header)],
292 sizeof (struct icmp_ttl_exceeded_header));
294 memset (&dst, 0, sizeof (dst));
295 dst.sin_family = AF_INET;
296 #if HAVE_SOCKADDR_IN_SIN_LEN
297 dst.sin_len = sizeof (struct sockaddr_in);
299 dst.sin_addr = *other;
300 err = sendto(rawsock,
303 (struct sockaddr*)&dst,
308 "sendto failed: %s\n", strerror(errno));
310 else if (sizeof (packet) != (size_t) err)
313 "Error: partial send of ICMP message\n");
319 * Send an ICMP message to the target.
321 * @param my_ip source address
322 * @param other target address
325 send_icmp (const struct in_addr *my_ip,
326 const struct in_addr *other)
328 struct ip_header ip_pkt;
329 struct icmp_ttl_exceeded_header icmp_ttl;
330 struct icmp_echo_header icmp_echo;
331 struct sockaddr_in dst;
332 char packet[sizeof (struct ip_header) * 2 +
333 sizeof (struct icmp_ttl_exceeded_header) +
334 sizeof (struct icmp_echo_header)];
338 /* ip header: send to (known) ip address */
340 ip_pkt.vers_ihl = 0x45;
342 ip_pkt.pkt_len = htons (sizeof (packet));
343 ip_pkt.id = htons (256);
344 ip_pkt.flags_frag_offset = 0;
345 ip_pkt.ttl = IPDEFTTL;
346 ip_pkt.proto = IPPROTO_ICMP;
348 ip_pkt.src_ip = my_ip->s_addr;
349 ip_pkt.dst_ip = other->s_addr;
350 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
351 sizeof (struct ip_header)));
352 memcpy (&packet[off],
354 sizeof (struct ip_header));
355 off = sizeof (ip_pkt);
357 /* icmp reply: time exceeded */
358 icmp_ttl.type = ICMP_TIME_EXCEEDED;
360 icmp_ttl.checksum = 0;
362 memcpy (&packet[off],
364 sizeof (struct icmp_ttl_exceeded_header));
365 off += sizeof (struct icmp_ttl_exceeded_header);
367 /* ip header of the presumably 'lost' udp packet */
368 ip_pkt.vers_ihl = 0x45;
370 ip_pkt.pkt_len = htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
371 ip_pkt.id = htons (256);
372 ip_pkt.flags_frag_offset = 0;
373 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
374 ip_pkt.proto = IPPROTO_ICMP;
375 ip_pkt.src_ip = other->s_addr;
376 ip_pkt.dst_ip = dummy.s_addr;
378 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
379 sizeof (struct ip_header)));
380 memcpy (&packet[off],
382 sizeof (struct ip_header));
383 off += sizeof (struct ip_header);
385 icmp_echo.type = ICMP_ECHO;
387 icmp_echo.reserved = htonl (port);
388 icmp_echo.checksum = 0;
389 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
390 sizeof (struct icmp_echo_header)));
391 memcpy (&packet[off],
393 sizeof(struct icmp_echo_header));
395 /* no go back to calculate ICMP packet checksum */
396 off = sizeof (struct ip_header);
397 icmp_ttl.checksum = htons(calc_checksum((uint16_t*) &packet[off],
398 sizeof (struct icmp_ttl_exceeded_header) +
399 sizeof (struct ip_header) +
400 sizeof (struct icmp_echo_header)));
401 memcpy (&packet[off],
403 sizeof (struct icmp_ttl_exceeded_header));
405 /* prepare for transmission */
406 memset (&dst, 0, sizeof (dst));
407 dst.sin_family = AF_INET;
408 #if HAVE_SOCKADDR_IN_SIN_LEN
409 dst.sin_len = sizeof (struct sockaddr_in);
411 dst.sin_addr = *other;
412 err = sendto(rawsock,
415 (struct sockaddr*)&dst,
420 "sendto failed: %s\n", strerror(errno));
422 else if (sizeof (packet) != (size_t) err)
425 "Error: partial send of ICMP message\n");
431 * Create an ICMP raw socket for writing.
433 * @return -1 on error
441 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
445 "Error opening RAW socket: %s\n",
449 if (0 != setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
450 (char *)&one, sizeof(one)))
453 "setsockopt failed: %s\n",
458 if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
459 (char *)&one, sizeof(one)))
462 "setsockopt failed: %s\n",
472 main (int argc, char *const *argv)
474 struct in_addr external;
475 struct in_addr target;
482 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
485 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
486 (1 != inet_pton (AF_INET, argv[2], &target)) )
489 "Error parsing IPv4 address: %s\n",
493 if ( (1 != sscanf (argv[3], "%u", &p) ) ||
498 "Error parsing port value `%s'\n",
503 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
506 "Internal error converting dummy IP to binary.\n");
509 if (-1 == (rawsock = make_raw_socket()))
512 if (0 != setresuid (uid, uid, uid))
515 "Failed to setresuid: %s\n",
517 /* not critical, continue anyway */
519 send_icmp (&external,
521 send_icmp_udp (&external,
527 /* end of gnunet-nat-client.c */