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 */
328 ip_pkt.vers_ihl = 0x45;
330 ip_pkt.pkt_len = sizeof (packet); /* huh? */
332 ip_pkt.flags_frag_offset = 0;
333 ip_pkt.ttl = IPDEFTTL;
334 ip_pkt.proto = IPPROTO_ICMP;
336 ip_pkt.src_ip = my_ip->s_addr;
337 ip_pkt.dst_ip = other->s_addr;
338 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
339 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
340 off = sizeof (ip_pkt);
342 /* icmp reply: time exceeded */
343 icmp_pkt.type = ICMP_TIME_EXCEEDED;
345 icmp_pkt.reserved = 0;
346 icmp_pkt.checksum = 0;
347 memcpy (&packet[off],
349 sizeof (struct icmp_packet));
350 off += sizeof (struct icmp_packet);
352 /* ip header of the presumably 'lost' udp packet */
353 ip_pkt.vers_ihl = 0x45;
355 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
357 ip_pkt.flags_frag_offset = 0;
358 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
359 ip_pkt.proto = IPPROTO_ICMP;
360 ip_pkt.src_ip = other->s_addr;
361 ip_pkt.dst_ip = dummy.s_addr;
363 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
364 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
365 off += sizeof (struct ip_packet);
367 icmp_echo.type = ICMP_ECHO;
369 icmp_echo.reserved = 0;
370 icmp_echo.checksum = 0;
371 icmp_echo.data = htons(port);
372 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
373 sizeof (struct icmp_echo_packet)));
374 memcpy (&packet[off],
376 sizeof(struct icmp_echo_packet));
378 /* no go back to calculate ICMP packet checksum */
379 off = sizeof (ip_pkt);
380 icmp_pkt.checksum = htons(calc_checksum((uint16_t*) &packet[off],
381 sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
382 memcpy (&packet[off],
384 sizeof (struct icmp_packet));
386 /* prepare for transmission */
387 memset (&dst, 0, sizeof (dst));
388 dst.sin_family = AF_INET;
389 #if HAVE_SOCKADDR_IN_SIN_LEN
390 dst.sin_len = sizeof (struct sockaddr_in);
392 dst.sin_addr = *other;
393 err = sendto(rawsock,
396 (struct sockaddr*)&dst,
397 sizeof(struct sockaddr_in));
401 "sendto failed: %s\n", strerror(errno));
403 else if (err != sizeof (packet))
406 "Error: partial send of ICMP message\n");
412 * Create an ICMP raw socket for writing.
414 * @return -1 on error
422 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
426 "Error opening RAW socket: %s\n",
430 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
431 (char *)&one, sizeof(one)) == -1)
434 "setsockopt failed: %s\n",
439 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
440 (char *)&one, sizeof(one)) == -1)
443 "setsockopt failed: %s\n",
453 main (int argc, char *const *argv)
455 struct in_addr external;
456 struct in_addr target;
463 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
466 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
467 (1 != inet_pton (AF_INET, argv[2], &target)) )
470 "Error parsing IPv4 address: %s\n",
474 if ( (1 != sscanf (argv[3], "%u", &p) ) ||
479 "Error parsing port value `%s'\n",
484 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
487 "Internal error converting dummy IP to binary.\n");
490 if (-1 == (rawsock = make_raw_socket()))
493 if (0 != setresuid (uid, uid, uid))
496 "Failed to setresuid: %s\n",
498 /* not critical, continue anyway */
500 send_icmp (&external,
502 send_icmp_udp (&external,
508 /* end of gnunet-nat-client.c */