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 (for now, later SUID will do)
24 * This code will work under GNU/Linux only (or maybe BSDs, but never W32)
25 * @author Christian Grothoff
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
31 #include <sys/select.h>
33 #include <sys/types.h>
40 #include <netinet/ip.h>
41 #include <netinet/ip_icmp.h>
42 #include <netinet/in.h>
47 * Number of UDP ports to keep open (typically >= 256)
49 #define NUM_UDP_PORTS 256
52 * Number of ICMP replies to send per message received (typically >= 1024)
54 #define NUM_ICMP_REPLIES 1024
57 * How often do we send our UDP messages to keep ports open? (typically < 100ms)
59 #define UDP_SEND_FREQUENCY_MS 50
62 * Port we use for the dummy target.
64 #define NAT_TRAV_PORT 2222
67 * How often do we retry to open and bind a UDP socket before giving up?
77 uint16_t flags_frag_offset;
89 uint16_t mlen_aka_reply_port_magic;
90 uint16_t checksum_aka_my_magic;
100 struct udp_packet udp;
104 static int udpsocks[NUM_UDP_PORTS];
106 static uint16_t udpports[NUM_UDP_PORTS];
112 static struct in_addr dummy;
114 static struct in_addr target;
118 * create a random port number that is not totally
119 * unlikely to be chosen by the nat box.
121 static uint16_t make_port ()
123 return 1024 + ( (unsigned int)rand ()) % (63 * 1024 - 2);
128 * create a fresh udp socket bound to a random local port.
131 make_udp_socket (uint16_t *port)
135 struct sockaddr_in src;
137 for (tries=0;tries<MAX_TRIES;tries++)
139 ret = socket (AF_INET, SOCK_DGRAM, 0);
143 "Error opening udp socket: %s\n",
147 if (ret >= FD_SETSIZE)
150 "Socket number too large (%d > %u)\n",
152 (unsigned int) FD_SETSIZE);
156 memset (&src, 0, sizeof (src));
157 src.sin_family = AF_INET;
158 src.sin_port = htons (make_port ());
159 if (0 != bind (ret, (struct sockaddr*) &src, sizeof (src)))
164 *port = ntohs (src.sin_port);
168 "Error binding udp socket: %s\n",
175 calc_checksum(const uint16_t *data,
182 for (i=0;i<bytes/2;i++)
184 sum = (sum & 0xffff) + (sum >> 16);
185 sum = htons(0xffff - sum);
191 * send an icmp message to the target.
193 * @param my_ip source address (our ip address)
194 * @param other target address
195 * @param target_port_number fake port number to put into icmp response
196 * as well as the icmpextradata as 'my_magic'
197 * @param source_port_number magic_number that enables the other peer to
198 * identify our port number ('reply in response to') to
199 * put in the data portion; 0 if we are initiating;
200 * goes into 'reply_port_magic' of the icmpextradata
203 send_icmp (const struct in_addr *my_ip,
204 const struct in_addr *other,
205 uint16_t target_port_number,
206 uint16_t source_port_number)
208 struct ip_packet ip_pkt;
209 struct icmp_packet icmp_pkt;
210 struct sockaddr_in dst;
211 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
215 /* ip header: send to (known) ip address */
217 memset(&ip_pkt, 0, sizeof(ip_pkt));
218 ip_pkt.vers_ihl = 0x45;//|(pkt_len>>2);//5;//(ipversion << 4) | (iphdr_size >> 2);
220 ip_pkt.pkt_len = sizeof (packet); /* huh? */
221 ip_pkt.id = 1; /* kernel will change anyway!? */
222 ip_pkt.flags_frag_offset = 0;
223 ip_pkt.ttl = IPDEFTTL;
224 ip_pkt.proto = IPPROTO_ICMP;
226 ip_pkt.src_ip = my_ip->s_addr;
227 ip_pkt.dst_ip = other->s_addr;
228 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
229 memcpy (packet, &ip_pkt, sizeof (ip_pkt));
230 off += sizeof (ip_pkt);
232 /* icmp reply: time exceeded */
233 memset(&icmp_pkt, 0, sizeof(icmp_pkt));
234 icmp_pkt.type = ICMP_TIME_EXCEEDED;
235 icmp_pkt.code = ICMP_HOST_UNREACH;
236 icmp_pkt.reserved = 0;
237 icmp_pkt.checksum = 0;
239 /* ip header of the presumably 'lost' udp packet */
240 icmp_pkt.ip.vers_ihl = 0x45;
242 /* no idea why i need to shift the bits here, but not on ip_pkt->pkt_len... */
243 icmp_pkt.ip.pkt_len = (sizeof (ip_pkt) + sizeof (icmp_pkt)) << 8;
244 icmp_pkt.ip.id = 1; /* kernel sets proper value htons(ip_id_counter); */
245 icmp_pkt.ip.flags_frag_offset = 0;
246 icmp_pkt.ip.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
247 icmp_pkt.ip.proto = IPPROTO_UDP;
248 icmp_pkt.ip.src_ip = other->s_addr;
249 icmp_pkt.ip.dst_ip = dummy.s_addr;
250 icmp_pkt.ip.checksum = 0;
251 icmp_pkt.ip.checksum = htons(calc_checksum((uint16_t*)&icmp_pkt.ip, sizeof (icmp_pkt.ip)));
252 icmp_pkt.udp.source_port = htons (target_port_number);
253 icmp_pkt.udp.dst_port = htons (NAT_TRAV_PORT);
254 icmp_pkt.udp.mlen_aka_reply_port_magic = htons (source_port_number);
255 icmp_pkt.udp.checksum_aka_my_magic = htons (target_port_number);
256 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&icmp_pkt, sizeof (icmp_pkt)));
257 memcpy (&packet[off], &icmp_pkt, sizeof (icmp_pkt));
258 off += sizeof (icmp_pkt);
260 memset (&dst, 0, sizeof (dst));
261 dst.sin_family = AF_INET;
262 dst.sin_addr = *other;
263 err = sendto(rawsock,
266 (struct sockaddr*)&dst,
267 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
270 "sendto failed: %s\n", strerror(errno));
271 } else if (err != off)
273 "Error: partial send of ICMP message\n");
278 * We discovered the IP address of the other peer.
279 * Try to connect back to it.
282 try_connect (const struct in_addr *my_ip,
283 const struct in_addr *other,
287 char sbuf [INET_ADDRSTRLEN];
290 "Sending %u ICMPs to `%s' with reply magic %u\n",
297 for (i=0;i<NUM_ICMP_REPLIES;i++)
298 send_icmp (my_ip, other, make_port(), port_magic);
303 process_icmp_response (const struct in_addr *my_ip,
310 uint16_t reply_magic;
312 struct ip_packet ip_pkt;
313 struct icmp_packet icmp_pkt;
316 have = read (s, buf, sizeof (buf));
320 "Error reading raw socket: %s\n",
325 if (have != sizeof (struct ip_packet) + sizeof (struct icmp_packet))
328 "Received ICMP message of unexpected size: %u bytes\n",
329 (unsigned int) have);
333 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
334 off += sizeof (ip_pkt);
335 memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
336 off += sizeof (icmp_pkt);
338 if ( (ip_pkt.proto == IPPROTO_ICMP) &&
339 (icmp_pkt.type == ICMP_DEST_UNREACH) &&
340 (icmp_pkt.code == ICMP_HOST_UNREACH) )
342 /* this is what is normal due to our UDP traffic */
345 if ( (ip_pkt.proto == IPPROTO_ICMP) &&
346 (icmp_pkt.type == ICMP_TIME_EXCEEDED) &&
347 (icmp_pkt.code == ICMP_HOST_UNREACH) )
349 /* this is what we might see on loopback: this is the format
350 we as the client send out (the host uses 'ICMP_NET_UNREACH');
354 if ( (ip_pkt.proto != IPPROTO_ICMP) ||
355 (icmp_pkt.type != ICMP_TIME_EXCEEDED) ||
356 (icmp_pkt.code != ICMP_NET_UNREACH) )
358 /* Note the expected client response and not the normal network response */
361 "Received unexpected ICMP message contents (%u, %u, %u), ignoring\n",
368 memcpy(&sip, &ip_pkt.src_ip, sizeof (sip));
369 reply_magic = ntohs (icmp_pkt.udp.checksum_aka_my_magic);
370 my_magic = ntohs (icmp_pkt.udp.mlen_aka_reply_port_magic);
371 local_port = ntohs (icmp_pkt.udp.source_port);
375 /* we get these a lot during loopback testing... */
377 "Received ICMP without hint as to which port worked, dropping\n");
383 "Received ICMP from `%s' with outgoing port %u, listen port %u and incoming for other peer %u\n",
394 try_connect (my_ip, &sip, reply_magic);
398 send_icmp (my_ip, &target, my_magic, reply_magic);
399 printf ("%s:%u listen on %u\n",
406 /* technically, we're done here! */
417 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
421 "Error opening RAW socket: %s\n",
425 if (ret >= FD_SETSIZE)
428 "Socket number too large (%d > %u)\n",
430 (unsigned int) FD_SETSIZE);
444 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
448 "Error opening RAW socket: %s\n",
452 if (ret >= FD_SETSIZE)
455 "Socket number too large (%d > %u)\n",
457 (unsigned int) FD_SETSIZE);
461 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
462 (char *)&one, sizeof(one)) == -1)
464 "setsockopt failed: %s\n",
466 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
467 (char *)&one, sizeof(one)) == -1)
469 "setsockopt failed: %s\n",
476 main (int argc, char *const *argv)
478 struct in_addr external;
483 struct sockaddr_in dst;
489 "This program must be started with our IP, the targets external IP and the dummy IP address as arguments.\n");
492 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
493 (1 != inet_pton (AF_INET, argv[2], &target)) ||
494 (1 != inet_pton (AF_INET, argv[3], &dummy)) )
497 "Error parsing IPv4 address: %s\n",
501 memset (&dst, 0, sizeof (dst));
502 dst.sin_family = AF_INET;
503 dst.sin_port = htons (NAT_TRAV_PORT);
504 dst.sin_addr = dummy;
506 if (-1 == (icmpsock = make_icmp_socket()))
508 if (-1 == (rawsock = make_raw_socket()))
513 for (i=0;i<NUM_UDP_PORTS;i++)
514 udpsocks[i] = make_udp_socket (&udpports[i]);
519 FD_SET (icmpsock, &rs);
521 tv.tv_usec = UDP_SEND_FREQUENCY_MS * 1000;
522 select (icmpsock + 1, &rs, NULL, NULL, &tv);
523 /* FIXME: do I need my external IP here? */
524 if (FD_ISSET (icmpsock, &rs))
526 process_icmp_response (&external, icmpsock);
531 "Sending UDP message to %s:%u\n",
535 if (-1 == sendto (udpsocks[pos],
537 (struct sockaddr*) &dst, sizeof (dst)))
540 "sendto failed: %s\n",
542 close (udpsocks[pos]);
543 udpsocks[pos] = make_udp_socket (&udpports[pos]);
548 "Sending fake ICMP message to %s:%u\n",
552 send_icmp (&external,
556 pos = (pos+1) % NUM_UDP_PORTS;
562 /* end of gnunet-nat-client.c */