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
44 /* Just needed for HAVE_SOCKADDR_IN_SIN_LEN test macro! */
45 #include "gnunet_config.h"
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <arpa/inet.h>
52 #include <sys/types.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_icmp.h>
61 #include <netinet/in.h>
64 * Must match IP given in the server.
66 #define DUMMY_IP "192.0.2.86"
68 #define NAT_TRAV_PORT 22225
77 * Version (4 bits) + Internet header length (4 bits)
97 * Flags (3 bits) + Fragment offset (13 bits)
99 uint16_t flags_frag_offset;
122 * Destination address
128 * Format of ICMP packet.
130 struct icmp_ttl_exceeded_header
140 /* followed by original payload */
143 struct icmp_echo_header
155 * Beginning of UDP packet.
169 * Socket we use to send our fake ICMP replies.
174 * Target "dummy" address of the packet we pretend to respond to.
176 static struct in_addr dummy;
181 static uint16_t port;
185 * CRC-16 for IP/ICMP headers.
187 * @param data what to calculate the CRC over
188 * @param bytes number of bytes in data (must be multiple of 2)
189 * @return the CRC 16.
192 calc_checksum (const uint16_t *data,
199 for (i=0;i<bytes/2;i++)
201 sum = (sum & 0xffff) + (sum >> 16);
202 sum = htons(0xffff - sum);
208 * Send an ICMP message to the target.
210 * @param my_ip source address
211 * @param other target address
214 send_icmp_udp (const struct in_addr *my_ip,
215 const struct in_addr *other)
217 char packet[sizeof(struct ip_header) * 2 +
218 sizeof(struct icmp_ttl_exceeded_header) +
219 sizeof(struct udp_header)];
220 struct ip_header ip_pkt;
221 struct icmp_ttl_exceeded_header icmp_pkt;
222 struct udp_header udp_pkt;
223 struct sockaddr_in dst;
227 /* ip header: send to (known) ip address */
229 ip_pkt.vers_ihl = 0x45;
231 ip_pkt.pkt_len = htons(sizeof (packet));
232 ip_pkt.id = htons(256);
233 ip_pkt.flags_frag_offset = 0;
235 ip_pkt.proto = IPPROTO_ICMP;
237 ip_pkt.src_ip = my_ip->s_addr;
238 ip_pkt.dst_ip = other->s_addr;
239 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
240 sizeof (struct ip_header)));
243 sizeof(struct ip_header));
244 off += sizeof(struct ip_header);
246 icmp_pkt.type = ICMP_TIME_EXCEEDED;
248 icmp_pkt.checksum = 0;
252 sizeof(struct icmp_ttl_exceeded_header));
253 off += sizeof(struct icmp_ttl_exceeded_header);
255 /* ip header of the presumably 'lost' udp packet */
256 ip_pkt.vers_ihl = 0x45;
258 ip_pkt.pkt_len = htons(sizeof (struct ip_header) +
259 sizeof(struct udp_header));
260 ip_pkt.id = htons(0);
261 ip_pkt.flags_frag_offset = 0;
263 ip_pkt.proto = IPPROTO_UDP;
265 ip_pkt.src_ip = other->s_addr;
266 ip_pkt.dst_ip = dummy.s_addr;
267 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
268 sizeof (struct ip_header)));
271 sizeof(struct ip_header));
272 off += sizeof(struct ip_header);
274 /* build UDP header */
275 udp_pkt.src_port = htons(NAT_TRAV_PORT);
276 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
277 udp_pkt.length = htons (sizeof (struct udp_header));
278 udp_pkt.crc = htons (port);
281 sizeof(struct udp_header));
282 off += sizeof(struct udp_header);
284 /* set ICMP checksum */
285 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(struct ip_header)],
286 sizeof (struct icmp_ttl_exceeded_header) +
287 sizeof (struct ip_header) +
288 sizeof (struct udp_header)));
289 memcpy (&packet[sizeof(struct ip_header)],
291 sizeof (struct icmp_ttl_exceeded_header));
292 memset (&dst, 0, sizeof (dst));
293 dst.sin_family = AF_INET;
294 #if HAVE_SOCKADDR_IN_SIN_LEN
295 dst.sin_len = sizeof (struct sockaddr_in);
297 dst.sin_addr = *other;
298 err = sendto(rawsock,
301 (struct sockaddr*)&dst,
306 "sendto failed: %s\n", strerror(errno));
308 else if (off != (size_t) err)
311 "Error: partial send of ICMP message\n");
317 * Send an ICMP message to the target.
319 * @param my_ip source address
320 * @param other target address
323 send_icmp (const struct in_addr *my_ip,
324 const struct in_addr *other)
326 struct ip_header ip_pkt;
327 struct icmp_ttl_exceeded_header icmp_pkt;
328 struct icmp_echo_header icmp_echo;
329 struct sockaddr_in dst;
330 char packet[sizeof (struct ip_header) * 2 +
331 sizeof (struct icmp_ttl_exceeded_header) +
332 sizeof (struct icmp_echo_header)];
336 /* ip header: send to (known) ip address */
338 ip_pkt.vers_ihl = 0x45;
340 ip_pkt.pkt_len = sizeof (packet);
342 ip_pkt.flags_frag_offset = 0;
343 ip_pkt.ttl = IPDEFTTL;
344 ip_pkt.proto = IPPROTO_ICMP;
346 ip_pkt.src_ip = my_ip->s_addr;
347 ip_pkt.dst_ip = other->s_addr;
348 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
349 sizeof (struct ip_header)));
350 memcpy (&packet[off],
352 sizeof (struct ip_header));
353 off = sizeof (ip_pkt);
355 /* icmp reply: time exceeded */
356 icmp_pkt.type = ICMP_TIME_EXCEEDED;
358 icmp_pkt.checksum = 0;
360 memcpy (&packet[off],
362 sizeof (struct icmp_ttl_exceeded_header));
363 off += sizeof (struct icmp_ttl_exceeded_header);
365 /* ip header of the presumably 'lost' udp packet */
366 ip_pkt.vers_ihl = 0x45;
368 ip_pkt.pkt_len = (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
370 ip_pkt.flags_frag_offset = 0;
371 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
372 ip_pkt.proto = IPPROTO_ICMP;
373 ip_pkt.src_ip = other->s_addr;
374 ip_pkt.dst_ip = dummy.s_addr;
376 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
377 sizeof (struct ip_header)));
378 memcpy (&packet[off],
380 sizeof (struct ip_header));
381 off += sizeof (struct ip_header);
383 icmp_echo.type = ICMP_ECHO;
385 icmp_echo.reserved = htonl (port);
386 icmp_echo.checksum = 0;
387 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
388 sizeof (struct icmp_echo_header)));
389 memcpy (&packet[off],
391 sizeof(struct icmp_echo_header));
393 /* no go back to calculate ICMP packet checksum */
394 off = sizeof (struct ip_header);
395 icmp_pkt.checksum = htons(calc_checksum((uint16_t*) &packet[off],
396 sizeof (struct icmp_ttl_exceeded_header) +
397 sizeof(struct ip_header) +
398 sizeof(struct icmp_echo_header)));
399 memcpy (&packet[off],
401 sizeof (struct icmp_ttl_exceeded_header));
403 /* prepare for transmission */
404 memset (&dst, 0, sizeof (dst));
405 dst.sin_family = AF_INET;
406 #if HAVE_SOCKADDR_IN_SIN_LEN
407 dst.sin_len = sizeof (struct sockaddr_in);
409 dst.sin_addr = *other;
410 err = sendto(rawsock,
413 (struct sockaddr*)&dst,
414 sizeof(struct sockaddr_in));
418 "sendto failed: %s\n", strerror(errno));
420 else if (sizeof (packet) != (size_t) err)
423 "Error: partial send of ICMP message\n");
429 * Create an ICMP raw socket for writing.
431 * @return -1 on error
439 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
443 "Error opening RAW socket: %s\n",
447 if (-1 == setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
448 (char *)&one, sizeof(one)))
451 "setsockopt failed: %s\n",
456 if (-1 == setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
457 (char *)&one, sizeof(one)))
460 "setsockopt failed: %s\n",
470 main (int argc, char *const *argv)
472 struct in_addr external;
473 struct in_addr target;
480 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
483 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
484 (1 != inet_pton (AF_INET, argv[2], &target)) )
487 "Error parsing IPv4 address: %s\n",
491 if ( (1 != sscanf (argv[3], "%u", &p) ) ||
496 "Error parsing port value `%s'\n",
501 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
504 "Internal error converting dummy IP to binary.\n");
507 if (-1 == (rawsock = make_raw_socket()))
510 if (0 != setresuid (uid, uid, uid))
513 "Failed to setresuid: %s\n",
515 /* not critical, continue anyway */
517 send_icmp (&external,
519 send_icmp_udp (&external,
525 /* end of gnunet-nat-client.c */