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>
45 * Number of UDP ports to keep open.
47 #define NUM_UDP_PORTS 512
50 * How often do we send our UDP messages to keep ports open?
52 #define UDP_SEND_FREQUENCY_MS 500
55 * Port we use for the dummy target.
57 #define NAT_TRAV_PORT 2222
60 * How often do we retry to open and bind a UDP socket before giving up?
71 uint16_t flags_frag_offset;
91 uint16_t mlen_aka_reply_port_magic;
92 uint16_t checksum_aka_my_magic;
97 * Structure of the data we tack on to the fake ICMP reply
98 * (last 4 bytes of the 64 bytes).
103 * if this is a reply to an icmp, what was the 'my_magic'
104 * value from the original icmp?
106 uint16_t reply_port_magic;
109 * magic value of the sender of this icmp message.
114 static int udpsocks[NUM_UDP_PORTS];
116 static uint16_t udpports[NUM_UDP_PORTS];
122 static struct in_addr dummy;
126 * create a random port number that is not totally
127 * unlikely to be chosen by the nat box.
129 static uint16_t make_port ()
131 return 1024 + ( (unsigned int)rand ()) % (63 * 1024 - 2);
136 * create a fresh udp socket bound to a random local port.
139 make_udp_socket (uint16_t *port)
143 struct sockaddr_in src;
145 for (tries=0;tries<MAX_TRIES;tries++)
147 ret = socket (AF_INET, SOCK_DGRAM, 0);
151 "Error opening udp socket: %s\n",
155 if (ret >= FD_SETSIZE)
158 "Socket number too large (%d > %u)\n",
160 (unsigned int) FD_SETSIZE);
164 memset (&src, 0, sizeof (src));
165 src.sin_family = AF_INET;
166 src.sin_port = htons (make_port ());
167 if (0 != bind (ret, (struct sockaddr*) &src, sizeof (src)))
172 *port = ntohs (src.sin_port);
176 "Error binding udp socket: %s\n",
183 calc_checksum(const uint16_t *data,
190 for (i=0;i<bytes/2;i++)
192 sum = (sum & 0xffff) + (sum >> 16);
193 sum = htons(0xffff - sum);
199 * send an icmp message to the target.
201 * @param my_ip source address (our ip address)
202 * @param other target address
203 * @param target_port_number fake port number to put into icmp response
204 * as well as the icmpextradata as 'my_magic'
205 * @param source_port_number magic_number that enables the other peer to
206 * identify our port number ('reply in response to') to
207 * put in the data portion; 0 if we are initiating;
208 * goes into 'reply_port_magic' of the icmpextradata
211 send_icmp (const struct in_addr *my_ip,
212 const struct in_addr *other,
213 uint16_t target_port_number,
214 uint16_t source_port_number)
216 struct ip_packet ip_pkt;
217 struct icmp_packet icmp_pkt;
218 struct udp_packet udp_pkt;
219 struct sockaddr_in dst;
220 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt) + sizeof (udp_pkt)];
224 /* ip header: send to (known) ip address */
226 memset(&ip_pkt, 0, sizeof(ip_pkt));
227 ip_pkt.vers_ihl = 0x45;//|(pkt_len>>2);//5;//(ipversion << 4) | (iphdr_size >> 2);
229 ip_pkt.pkt_len = sizeof (packet); /* huh? */
230 ip_pkt.id = 1; /* kernel will change anyway!? */
231 ip_pkt.flags_frag_offset = 0;
232 ip_pkt.ttl = IPDEFTTL;
233 ip_pkt.proto = IPPROTO_ICMP;
234 ip_pkt.checksum = 0; /* maybe the kernel helps us out..? */
235 ip_pkt.src_ip = my_ip->s_addr;
236 ip_pkt.dst_ip = other->s_addr;
237 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
238 memcpy (packet, &ip_pkt, sizeof (ip_pkt));
239 off += sizeof (ip_pkt);
241 /* icmp reply: time exceeded */
242 memset(&icmp_pkt, 0, sizeof(icmp_pkt));
243 icmp_pkt.type = ICMP_TIME_EXCEEDED;
244 icmp_pkt.code = ICMP_NET_UNREACH;
245 icmp_pkt.reserved = 0;
246 icmp_pkt.checksum = 0;
247 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&icmp_pkt, sizeof (icmp_pkt)));
248 memcpy (&packet[off], &icmp_pkt, sizeof (icmp_pkt));
249 off += sizeof (icmp_pkt);
251 /* ip header of the presumably 'lost' udp packet */
252 memset(&ip_pkt, 0, sizeof (ip_pkt));
253 ip_pkt.vers_ihl = 0x45;
255 /* no idea why i need to shift the bits here, but not on ip_pkt->pkt_len... */
256 ip_pkt.pkt_len = (sizeof (ip_pkt) + sizeof (icmp_pkt)) << 8;
257 ip_pkt.id = 1; /* kernel sets proper value htons(ip_id_counter); */
258 ip_pkt.flags_frag_offset = 0;
259 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
260 ip_pkt.proto = IPPROTO_UDP;
261 ip_pkt.src_ip = other->s_addr;
262 ip_pkt.dst_ip = dummy.s_addr;
264 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
265 memcpy (&packet[off], &ip_pkt, sizeof (ip_pkt));
266 off += sizeof (ip_pkt);
268 memset(&udp_pkt, 0, sizeof (udp_pkt));
269 udp_pkt.source_port = htons (target_port_number);
270 udp_pkt.dst_port = htons (NAT_TRAV_PORT);
272 "** Generating ICMP with rpm %u\n",
274 udp_pkt.mlen_aka_reply_port_magic = htons (source_port_number);
275 udp_pkt.checksum_aka_my_magic = htons (target_port_number);
276 memcpy (&packet[off], &udp_pkt, sizeof (udp_pkt));
277 off += sizeof (udp_pkt);
279 memset (&dst, 0, sizeof (dst));
280 dst.sin_family = AF_INET;
281 dst.sin_addr = *other;
282 err = sendto(rawsock,
285 (struct sockaddr*)&dst,
286 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
289 "sendto failed: %s\n", strerror(errno));
290 } else if (err != off)
292 "Error: partial send of ICMP message\n");
297 * We discovered the IP address of the other peer.
298 * Try to connect back to it.
301 try_connect (const struct in_addr *my_ip,
302 const struct in_addr *other,
306 char sbuf [INET_ADDRSTRLEN];
309 "Sending %u ICMPs to `%s' with reply magic %u\n",
316 for (i=0;i<NUM_UDP_PORTS;i++)
317 send_icmp (my_ip, other, make_port(), port_magic);
322 process_icmp_response (const struct in_addr *my_ip,
329 uint16_t reply_magic;
330 struct ip_packet ip_pkt;
331 struct icmp_packet icmp_pkt;
332 struct udp_packet udp_pkt;
335 have = read (s, buf, sizeof (buf));
339 "Error reading raw socket: %s\n",
344 if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) +
345 sizeof (struct udp_packet))
348 "Received ICMP message of unexpected size: %u bytes\n",
349 (unsigned int) have);
353 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
354 off += sizeof (ip_pkt);
355 memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
356 off += sizeof (icmp_pkt);
357 off += sizeof (ip_pkt);
358 memcpy (&udp_pkt, &buf[off], sizeof (udp_pkt));
359 off += sizeof (struct udp_packet);
361 if ( (ip_pkt.proto == IPPROTO_ICMP) &&
362 (icmp_pkt.type == ICMP_DEST_UNREACH) &&
363 (icmp_pkt.code == ICMP_HOST_UNREACH) )
365 /* this is what is normal due to our UDP traffic */
368 if ( (ip_pkt.proto != IPPROTO_ICMP) ||
369 (icmp_pkt.type != ICMP_TIME_EXCEEDED) ||
370 (icmp_pkt.code != ICMP_NET_UNREACH) )
372 /* Note the expected client response and not the normal network response */
374 "Received unexpected ICMP message contents (%u, %u, %u), ignoring\n",
380 memcpy(&sip, &ip_pkt.src_ip, sizeof (sip));
381 my_magic = ntohs (udp_pkt.checksum_aka_my_magic);
382 reply_magic = ntohs (udp_pkt.mlen_aka_reply_port_magic);
386 /* we get these a lot during loopback testing... */
388 "Received ICMP without hint as to which port worked, dropping\n");
393 "Received ICMP from `%s' with hints %u and %u\n",
402 try_connect (my_ip, &sip, reply_magic);
421 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
425 "Error opening RAW socket: %s\n",
429 if (ret >= FD_SETSIZE)
432 "Socket number too large (%d > %u)\n",
434 (unsigned int) FD_SETSIZE);
448 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
452 "Error opening RAW socket: %s\n",
456 if (ret >= FD_SETSIZE)
459 "Socket number too large (%d > %u)\n",
461 (unsigned int) FD_SETSIZE);
465 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
466 (char *)&one, sizeof(one)) == -1)
468 "setsockopt failed: %s\n",
470 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
471 (char *)&one, sizeof(one)) == -1)
473 "setsockopt failed: %s\n",
480 main (int argc, char *const *argv)
482 struct in_addr target;
483 struct in_addr external;
488 struct sockaddr_in dst;
494 "This program must be started with our IP, the targets external IP and the dummy IP address as arguments.\n");
497 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
498 (1 != inet_pton (AF_INET, argv[2], &target)) ||
499 (1 != inet_pton (AF_INET, argv[3], &dummy)) )
502 "Error parsing IPv4 address: %s\n",
506 memset (&dst, 0, sizeof (dst));
507 dst.sin_family = AF_INET;
508 dst.sin_port = htons (NAT_TRAV_PORT);
509 dst.sin_addr = dummy;
511 if (-1 == (icmpsock = make_icmp_socket()))
513 if (-1 == (rawsock = make_raw_socket()))
518 for (i=0;i<NUM_UDP_PORTS;i++)
519 udpsocks[i] = make_udp_socket (&udpports[i]);
524 FD_SET (icmpsock, &rs);
526 tv.tv_usec = UDP_SEND_FREQUENCY_MS * 1000;
527 select (icmpsock + 1, &rs, NULL, NULL, &tv);
528 /* FIXME: do I need my external IP here? */
529 if (FD_ISSET (icmpsock, &rs))
531 process_icmp_response (&external, icmpsock);
535 "Sending UDP message to %s:%u\n",
538 if (-1 == sendto (udpsocks[pos],
540 (struct sockaddr*) &dst, sizeof (dst)))
543 "sendto failed: %s\n",
545 close (udpsocks[pos]);
546 udpsocks[pos] = make_udp_socket (&udpports[pos]);
550 "Sending fake ICMP message to %s with port %u\n",
553 send_icmp (&external,
557 pos = (pos+1) % NUM_UDP_PORTS;
563 /* end of gnunet-nat-client.c */