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;
124 static struct in_addr target;
128 * create a random port number that is not totally
129 * unlikely to be chosen by the nat box.
131 static uint16_t make_port ()
133 return 1024 + ( (unsigned int)rand ()) % (63 * 1024 - 2);
138 * create a fresh udp socket bound to a random local port.
141 make_udp_socket (uint16_t *port)
145 struct sockaddr_in src;
147 for (tries=0;tries<MAX_TRIES;tries++)
149 ret = socket (AF_INET, SOCK_DGRAM, 0);
153 "Error opening udp socket: %s\n",
157 if (ret >= FD_SETSIZE)
160 "Socket number too large (%d > %u)\n",
162 (unsigned int) FD_SETSIZE);
166 memset (&src, 0, sizeof (src));
167 src.sin_family = AF_INET;
168 src.sin_port = htons (make_port ());
169 if (0 != bind (ret, (struct sockaddr*) &src, sizeof (src)))
174 *port = ntohs (src.sin_port);
178 "Error binding udp socket: %s\n",
185 calc_checksum(const uint16_t *data,
192 for (i=0;i<bytes/2;i++)
194 sum = (sum & 0xffff) + (sum >> 16);
195 sum = htons(0xffff - sum);
201 * send an icmp message to the target.
203 * @param my_ip source address (our ip address)
204 * @param other target address
205 * @param target_port_number fake port number to put into icmp response
206 * as well as the icmpextradata as 'my_magic'
207 * @param source_port_number magic_number that enables the other peer to
208 * identify our port number ('reply in response to') to
209 * put in the data portion; 0 if we are initiating;
210 * goes into 'reply_port_magic' of the icmpextradata
213 send_icmp (const struct in_addr *my_ip,
214 const struct in_addr *other,
215 uint16_t target_port_number,
216 uint16_t source_port_number)
218 struct ip_packet ip_pkt;
219 struct icmp_packet icmp_pkt;
220 struct udp_packet udp_pkt;
221 struct sockaddr_in dst;
222 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt) + sizeof (udp_pkt)];
226 /* ip header: send to (known) ip address */
228 memset(&ip_pkt, 0, sizeof(ip_pkt));
229 ip_pkt.vers_ihl = 0x45;//|(pkt_len>>2);//5;//(ipversion << 4) | (iphdr_size >> 2);
231 ip_pkt.pkt_len = sizeof (packet); /* huh? */
232 ip_pkt.id = 1; /* kernel will change anyway!? */
233 ip_pkt.flags_frag_offset = 0;
234 ip_pkt.ttl = IPDEFTTL;
235 ip_pkt.proto = IPPROTO_ICMP;
236 ip_pkt.checksum = 0; /* maybe the kernel helps us out..? */
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, sizeof (ip_pkt)));
240 memcpy (packet, &ip_pkt, sizeof (ip_pkt));
241 off += sizeof (ip_pkt);
243 /* icmp reply: time exceeded */
244 memset(&icmp_pkt, 0, sizeof(icmp_pkt));
245 icmp_pkt.type = ICMP_TIME_EXCEEDED;
246 icmp_pkt.code = ICMP_NET_UNREACH;
247 icmp_pkt.reserved = 0;
248 icmp_pkt.checksum = 0;
249 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&icmp_pkt, sizeof (icmp_pkt)));
250 memcpy (&packet[off], &icmp_pkt, sizeof (icmp_pkt));
251 off += sizeof (icmp_pkt);
253 /* ip header of the presumably 'lost' udp packet */
254 memset(&ip_pkt, 0, sizeof (ip_pkt));
255 ip_pkt.vers_ihl = 0x45;
257 /* no idea why i need to shift the bits here, but not on ip_pkt->pkt_len... */
258 ip_pkt.pkt_len = (sizeof (ip_pkt) + sizeof (icmp_pkt)) << 8;
259 ip_pkt.id = 1; /* kernel sets proper value htons(ip_id_counter); */
260 ip_pkt.flags_frag_offset = 0;
261 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
262 ip_pkt.proto = IPPROTO_UDP;
263 ip_pkt.src_ip = other->s_addr;
264 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 memset(&udp_pkt, 0, sizeof (udp_pkt));
271 udp_pkt.source_port = htons (target_port_number);
272 udp_pkt.dst_port = htons (NAT_TRAV_PORT);
274 "** Generating ICMP with rpm %u\n",
276 udp_pkt.mlen_aka_reply_port_magic = htons (source_port_number);
277 udp_pkt.checksum_aka_my_magic = htons (target_port_number);
278 memcpy (&packet[off], &udp_pkt, sizeof (udp_pkt));
279 off += sizeof (udp_pkt);
281 memset (&dst, 0, sizeof (dst));
282 dst.sin_family = AF_INET;
283 dst.sin_addr = *other;
284 err = sendto(rawsock,
287 (struct sockaddr*)&dst,
288 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
291 "sendto failed: %s\n", strerror(errno));
292 } else if (err != off)
294 "Error: partial send of ICMP message\n");
299 * We discovered the IP address of the other peer.
300 * Try to connect back to it.
303 try_connect (const struct in_addr *my_ip,
304 const struct in_addr *other,
308 char sbuf [INET_ADDRSTRLEN];
311 "Sending %u ICMPs to `%s' with reply magic %u\n",
318 for (i=0;i<NUM_UDP_PORTS;i++)
319 send_icmp (my_ip, other, make_port(), port_magic);
324 process_icmp_response (const struct in_addr *my_ip,
331 uint16_t reply_magic;
332 struct ip_packet ip_pkt;
333 struct icmp_packet icmp_pkt;
334 struct udp_packet udp_pkt;
337 have = read (s, buf, sizeof (buf));
341 "Error reading raw socket: %s\n",
346 if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) +
347 sizeof (struct udp_packet))
350 "Received ICMP message of unexpected size: %u bytes\n",
351 (unsigned int) have);
355 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
356 off += sizeof (ip_pkt);
357 memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
358 off += sizeof (icmp_pkt);
359 off += sizeof (ip_pkt);
360 memcpy (&udp_pkt, &buf[off], sizeof (udp_pkt));
361 off += sizeof (struct udp_packet);
363 if ( (ip_pkt.proto == IPPROTO_ICMP) &&
364 (icmp_pkt.type == ICMP_DEST_UNREACH) &&
365 (icmp_pkt.code == ICMP_HOST_UNREACH) )
367 /* this is what is normal due to our UDP traffic */
370 if ( (ip_pkt.proto != IPPROTO_ICMP) ||
371 (icmp_pkt.type != ICMP_TIME_EXCEEDED) ||
372 (icmp_pkt.code != ICMP_NET_UNREACH) )
374 /* Note the expected client response and not the normal network response */
376 "Received unexpected ICMP message contents (%u, %u, %u), ignoring\n",
382 memcpy(&sip, &ip_pkt.src_ip, sizeof (sip));
383 reply_magic = ntohs (udp_pkt.checksum_aka_my_magic);
384 my_magic = ntohs (udp_pkt.mlen_aka_reply_port_magic);
388 /* we get these a lot during loopback testing... */
390 "Received ICMP without hint as to which port worked, dropping\n");
395 "Received ICMP from `%s' with hints %u and %u\n",
404 try_connect (my_ip, &sip, reply_magic);
408 send_icmp (my_ip, &target, my_magic, reply_magic);
424 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
428 "Error opening RAW socket: %s\n",
432 if (ret >= FD_SETSIZE)
435 "Socket number too large (%d > %u)\n",
437 (unsigned int) FD_SETSIZE);
451 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
455 "Error opening RAW socket: %s\n",
459 if (ret >= FD_SETSIZE)
462 "Socket number too large (%d > %u)\n",
464 (unsigned int) FD_SETSIZE);
468 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
469 (char *)&one, sizeof(one)) == -1)
471 "setsockopt failed: %s\n",
473 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
474 (char *)&one, sizeof(one)) == -1)
476 "setsockopt failed: %s\n",
483 main (int argc, char *const *argv)
485 struct in_addr external;
490 struct sockaddr_in dst;
496 "This program must be started with our IP, the targets external IP and the dummy IP address as arguments.\n");
499 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
500 (1 != inet_pton (AF_INET, argv[2], &target)) ||
501 (1 != inet_pton (AF_INET, argv[3], &dummy)) )
504 "Error parsing IPv4 address: %s\n",
508 memset (&dst, 0, sizeof (dst));
509 dst.sin_family = AF_INET;
510 dst.sin_port = htons (NAT_TRAV_PORT);
511 dst.sin_addr = dummy;
513 if (-1 == (icmpsock = make_icmp_socket()))
515 if (-1 == (rawsock = make_raw_socket()))
520 for (i=0;i<NUM_UDP_PORTS;i++)
521 udpsocks[i] = make_udp_socket (&udpports[i]);
526 FD_SET (icmpsock, &rs);
528 tv.tv_usec = UDP_SEND_FREQUENCY_MS * 1000;
529 select (icmpsock + 1, &rs, NULL, NULL, &tv);
530 /* FIXME: do I need my external IP here? */
531 if (FD_ISSET (icmpsock, &rs))
533 process_icmp_response (&external, icmpsock);
537 "Sending UDP message to %s:%u\n",
540 if (-1 == sendto (udpsocks[pos],
542 (struct sockaddr*) &dst, sizeof (dst)))
545 "sendto failed: %s\n",
547 close (udpsocks[pos]);
548 udpsocks[pos] = make_udp_socket (&udpports[pos]);
552 "Sending fake ICMP message to %s with port %u\n",
555 send_icmp (&external,
559 pos = (pos+1) % NUM_UDP_PORTS;
565 /* end of gnunet-nat-client.c */