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 (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));
293 memset (&dst, 0, sizeof (dst));
294 dst.sin_family = AF_INET;
295 #if HAVE_SOCKADDR_IN_SIN_LEN
296 dst.sin_len = sizeof (struct sockaddr_in);
298 dst.sin_addr = *other;
299 err = sendto(rawsock,
302 (struct sockaddr*)&dst,
307 "sendto failed: %s\n", strerror(errno));
309 else if (sizeof (packet) != (size_t) err)
312 "Error: partial send of ICMP message\n");
318 * Send an ICMP message to the target.
320 * @param my_ip source address
321 * @param other target address
324 send_icmp (const struct in_addr *my_ip,
325 const struct in_addr *other)
327 struct ip_header ip_pkt;
328 struct icmp_ttl_exceeded_header icmp_ttl;
329 struct icmp_echo_header icmp_echo;
330 struct sockaddr_in dst;
331 char packet[sizeof (struct ip_header) * 2 +
332 sizeof (struct icmp_ttl_exceeded_header) +
333 sizeof (struct icmp_echo_header)];
337 /* ip header: send to (known) ip address */
339 ip_pkt.vers_ihl = 0x45;
341 ip_pkt.pkt_len = htons (sizeof (packet));
342 ip_pkt.id = htons (256);
343 ip_pkt.flags_frag_offset = 0;
344 ip_pkt.ttl = IPDEFTTL;
345 ip_pkt.proto = IPPROTO_ICMP;
347 ip_pkt.src_ip = my_ip->s_addr;
348 ip_pkt.dst_ip = other->s_addr;
349 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
350 sizeof (struct ip_header)));
351 memcpy (&packet[off],
353 sizeof (struct ip_header));
354 off = sizeof (ip_pkt);
356 /* icmp reply: time exceeded */
357 icmp_ttl.type = ICMP_TIME_EXCEEDED;
359 icmp_ttl.checksum = 0;
361 memcpy (&packet[off],
363 sizeof (struct icmp_ttl_exceeded_header));
364 off += sizeof (struct icmp_ttl_exceeded_header);
366 /* ip header of the presumably 'lost' udp packet */
367 ip_pkt.vers_ihl = 0x45;
369 ip_pkt.pkt_len = htons (sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
370 ip_pkt.id = htons (256);
371 ip_pkt.flags_frag_offset = 0;
372 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
373 ip_pkt.proto = IPPROTO_ICMP;
374 ip_pkt.src_ip = other->s_addr;
375 ip_pkt.dst_ip = dummy.s_addr;
377 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
378 sizeof (struct ip_header)));
379 memcpy (&packet[off],
381 sizeof (struct ip_header));
382 off += sizeof (struct ip_header);
384 icmp_echo.type = ICMP_ECHO;
386 icmp_echo.reserved = htonl (port);
387 icmp_echo.checksum = 0;
388 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
389 sizeof (struct icmp_echo_header)));
390 memcpy (&packet[off],
392 sizeof(struct icmp_echo_header));
394 /* no go back to calculate ICMP packet checksum */
395 off = sizeof (struct ip_header);
396 icmp_ttl.checksum = htons(calc_checksum((uint16_t*) &packet[off],
397 sizeof (struct icmp_ttl_exceeded_header) +
398 sizeof (struct ip_header) +
399 sizeof (struct icmp_echo_header)));
400 memcpy (&packet[off],
402 sizeof (struct icmp_ttl_exceeded_header));
404 /* prepare for transmission */
405 memset (&dst, 0, sizeof (dst));
406 dst.sin_family = AF_INET;
407 #if HAVE_SOCKADDR_IN_SIN_LEN
408 dst.sin_len = sizeof (struct sockaddr_in);
410 dst.sin_addr = *other;
411 err = sendto(rawsock,
414 (struct sockaddr*)&dst,
419 "sendto failed: %s\n", strerror(errno));
421 else if (sizeof (packet) != (size_t) err)
424 "Error: partial send of ICMP message\n");
430 * Create an ICMP raw socket for writing.
432 * @return -1 on error
440 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
444 "Error opening RAW socket: %s\n",
448 if (0 != setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
449 (char *)&one, sizeof(one)))
452 "setsockopt failed: %s\n",
457 if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
458 (char *)&one, sizeof(one)))
461 "setsockopt failed: %s\n",
471 main (int argc, char *const *argv)
473 struct in_addr external;
474 struct in_addr target;
481 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
484 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
485 (1 != inet_pton (AF_INET, argv[2], &target)) )
488 "Error parsing IPv4 address: %s\n",
492 if ( (1 != sscanf (argv[3], "%u", &p) ) ||
497 "Error parsing port value `%s'\n",
502 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
505 "Internal error converting dummy IP to binary.\n");
508 if (-1 == (rawsock = make_raw_socket()))
511 if (0 != setresuid (uid, uid, uid))
514 "Failed to setresuid: %s\n",
516 /* not critical, continue anyway */
518 send_icmp (&external,
520 send_icmp_udp (&external,
526 /* end of gnunet-nat-client.c */