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.
141 struct icmp_echo_packet
155 * Beginning of UDP packet.
167 * Socket we use to send our fake ICMP replies.
172 * Target "dummy" address of the packet we pretend to respond to.
174 static struct in_addr dummy;
179 static uint16_t port;
183 * CRC-16 for IP/ICMP headers.
185 * @param data what to calculate the CRC over
186 * @param bytes number of bytes in data (must be multiple of 2)
187 * @return the CRC 16.
190 calc_checksum(const uint16_t *data,
197 for (i=0;i<bytes/2;i++)
199 sum = (sum & 0xffff) + (sum >> 16);
200 sum = htons(0xffff - sum);
206 * Send an ICMP message to the target.
208 * @param my_ip source address
209 * @param other target address
212 send_icmp_udp (const struct in_addr *my_ip,
213 const struct in_addr *other)
215 struct ip_packet ip_pkt;
216 struct icmp_packet icmp_pkt;
217 struct udp_packet udp_pkt;
219 struct sockaddr_in dst;
220 char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
225 /* ip header: send to (known) ip address */
227 memset(&ip_pkt, 0, sizeof(ip_pkt));
228 ip_pkt.vers_ihl = 0x45;
230 ip_pkt.pkt_len = htons(sizeof (packet));
231 ip_pkt.id = htons(256);
232 ip_pkt.flags_frag_offset = 0;
234 ip_pkt.proto = IPPROTO_ICMP;
236 ip_pkt.src_ip = my_ip->s_addr;
237 ip_pkt.dst_ip = other->s_addr;
238 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
239 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
240 off += sizeof(ip_pkt);
242 /* ip header of the presumably 'lost' udp packet */
243 ip_pkt.vers_ihl = 0x45;
245 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
247 icmp_pkt.type = 11; /* TTL exceeded */
249 icmp_pkt.checksum = 0;
250 icmp_pkt.reserved = 0;
251 memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
252 off += sizeof(icmp_pkt);
254 /* build inner IP header */
255 memset(&ip_pkt, 0, sizeof(ip_pkt));
256 ip_pkt.vers_ihl = 0x45;
258 ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
259 ip_pkt.id = htons(0);
260 ip_pkt.flags_frag_offset = 0;
262 ip_pkt.proto = IPPROTO_UDP;
264 ip_pkt.src_ip = other->s_addr;
265 ip_pkt.dst_ip = dummy.s_addr;
266 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
267 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
268 off += sizeof(ip_pkt);
270 /* build UDP header */
271 udp_pkt.src_port = htons(NAT_TRAV_PORT);
272 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
274 memset(&udp_pkt.length, 0, sizeof(uint32_t));
275 udp_pkt.length = htons (port);
276 memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
277 off += sizeof(udp_pkt);
279 /* set ICMP checksum */
280 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
281 sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
282 memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
285 memset (&dst, 0, sizeof (dst));
286 dst.sin_family = AF_INET;
287 dst.sin_addr = *other;
288 err = sendto(rawsock,
291 (struct sockaddr*)&dst,
297 "sendto failed: %s\n", strerror(errno));
302 "Error: partial send of ICMP message\n");
308 * Send an ICMP message to the target.
310 * @param my_ip source address
311 * @param other target address
314 send_icmp (const struct in_addr *my_ip,
315 const struct in_addr *other)
317 struct ip_packet ip_pkt;
318 struct icmp_packet icmp_pkt;
319 struct icmp_echo_packet icmp_echo;
320 struct sockaddr_in dst;
321 char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
326 /* ip header: send to (known) ip address */
327 ip_pkt.vers_ihl = 0x45;
329 ip_pkt.pkt_len = sizeof (packet); /* huh? */
331 ip_pkt.flags_frag_offset = 0;
332 ip_pkt.ttl = IPDEFTTL;
333 ip_pkt.proto = IPPROTO_ICMP;
335 ip_pkt.src_ip = my_ip->s_addr;
336 ip_pkt.dst_ip = other->s_addr;
337 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
338 memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
339 off = sizeof (ip_pkt);
341 /* icmp reply: time exceeded */
342 icmp_pkt.type = ICMP_TIME_EXCEEDED;
344 icmp_pkt.reserved = 0;
345 icmp_pkt.checksum = 0;
346 memcpy (&packet[off],
348 sizeof (struct icmp_packet));
349 off += sizeof (struct icmp_packet);
351 /* ip header of the presumably 'lost' udp packet */
352 ip_pkt.vers_ihl = 0x45;
354 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
356 ip_pkt.flags_frag_offset = 0;
357 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
358 ip_pkt.proto = IPPROTO_ICMP;
359 ip_pkt.src_ip = other->s_addr;
360 ip_pkt.dst_ip = dummy.s_addr;
362 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
363 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
364 off += sizeof (struct ip_packet);
366 icmp_echo.type = ICMP_ECHO;
368 icmp_echo.reserved = 0;
369 icmp_echo.checksum = 0;
370 icmp_echo.data = htons(port);
371 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
372 sizeof (struct icmp_echo_packet)));
373 memcpy (&packet[off],
375 sizeof(struct icmp_echo_packet));
377 /* no go back to calculate ICMP packet checksum */
378 off = sizeof (ip_pkt);
379 icmp_pkt.checksum = htons(calc_checksum((uint16_t*) &packet[off],
380 sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
381 memcpy (&packet[off],
383 sizeof (struct icmp_packet));
385 /* prepare for transmission */
386 memset (&dst, 0, sizeof (dst));
387 dst.sin_family = AF_INET;
388 #if HAVE_SOCKADDR_IN_SIN_LEN
389 dst.sin_len = sizeof (struct sockaddr_in);
391 dst.sin_addr = *other;
392 err = sendto(rawsock,
395 (struct sockaddr*)&dst,
396 sizeof(struct sockaddr_in));
400 "sendto failed: %s\n", strerror(errno));
405 "Error: partial send of ICMP message\n");
411 * Create an ICMP raw socket for writing.
413 * @return -1 on error
421 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
425 "Error opening RAW socket: %s\n",
429 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
430 (char *)&one, sizeof(one)) == -1)
433 "setsockopt failed: %s\n",
438 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
439 (char *)&one, sizeof(one)) == -1)
442 "setsockopt failed: %s\n",
452 main (int argc, char *const *argv)
454 struct in_addr external;
455 struct in_addr target;
462 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
465 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
466 (1 != inet_pton (AF_INET, argv[2], &target)) )
469 "Error parsing IPv4 address: %s\n",
473 if ( (1 != sscanf (argv[3], "%u", &p) ) ||
478 "Error parsing port value `%s'\n",
483 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
486 "Internal error converting dummy IP to binary.\n");
489 if (-1 == (rawsock = make_raw_socket()))
492 if (0 != setresuid (uid, uid, uid))
495 "Failed to setresuid: %s\n",
497 /* not critical, continue anyway */
499 send_icmp (&external,
501 send_icmp_udp (&external,
507 /* end of gnunet-nat-client.c */