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-windows.c
23 * @brief Tool to help bypass NATs using ICMP method; must run as
24 * root (SUID will do) or administrator on W32
25 * This code will work under GNU/Linux or W32.
26 * @author Nathan Evans
28 * This program will send ONE ICMP message using RAW sockets
29 * to the IP address specified as the second argument. Since
30 * it uses RAW sockets, it must be installed SUID or run as 'root'.
31 * In order to keep the security risk of the resulting SUID binary
32 * minimal, the program ONLY opens the RAW socket with root
33 * privileges, then drops them and only then starts to process
34 * command line arguments. The code also does not link against
35 * any shared libraries (except libc) and is strictly minimal
36 * (except for checking for errors). The following list of people
37 * have reviewed this code and considered it safe since the last
38 * modification (if you reviewed it, please have your name added
41 * - Christian Grothoff
49 #include <sys/types.h>
61 #define ICMP_TIME_EXCEEDED 11
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
129 * Format of ICMP packet.
142 struct icmp_echo_packet
152 * Beginning of UDP packet.
165 * Socket we use to send our ICMP packets.
167 static SOCKET rawsock;
170 * Target "dummy" address.
172 static struct in_addr dummy;
174 static uint32_t port;
179 * Convert IPv4 address from text to binary form.
181 * @param af address family
182 * @param cp the address to print
183 * @param buf where to write the address result
184 * @return 1 on success
191 buf->s_addr = inet_addr(cp);
192 if (buf->s_addr == INADDR_NONE)
195 "Error %d handling address %s",
205 * CRC-16 for IP/ICMP headers.
207 * @param data what to calculate the CRC over
208 * @param bytes number of bytes in data (must be multiple of 2)
209 * @return the CRC 16.
212 calc_checksum(const uint16_t *data,
219 for (i=0;i<bytes/2;i++)
221 sum = (sum & 0xffff) + (sum >> 16);
222 sum = htons(0xffff - sum);
228 make_echo (const struct in_addr *src_ip,
229 struct icmp_echo_packet *echo, uint32_t num)
231 memset(echo, 0, sizeof(struct icmp_echo_packet));
232 echo->type = ICMP_ECHO;
236 echo->data = htons(num);
237 echo->checksum = htons(calc_checksum((uint16_t*)echo,
238 sizeof (struct icmp_echo_packet)));
242 * Send an ICMP message to the target.
244 * @param my_ip source address
245 * @param other target address
248 send_icmp_udp (const struct in_addr *my_ip,
249 const struct in_addr *other)
251 struct ip_packet ip_pkt;
252 struct icmp_packet icmp_pkt;
253 struct udp_packet udp_pkt;
255 struct sockaddr_in dst;
256 char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
261 /* ip header: send to (known) ip address */
263 memset(&ip_pkt, 0, sizeof(ip_pkt));
264 ip_pkt.vers_ihl = 0x45;
266 ip_pkt.pkt_len = htons(sizeof (packet));
267 ip_pkt.id = htons(256);
268 ip_pkt.flags_frag_offset = 0;
270 ip_pkt.proto = IPPROTO_ICMP;
272 ip_pkt.src_ip = my_ip->s_addr;
273 ip_pkt.dst_ip = other->s_addr;
274 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
275 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
276 off += sizeof(ip_pkt);
278 /* ip header of the presumably 'lost' udp packet */
279 ip_pkt.vers_ihl = 0x45;
281 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
283 icmp_pkt.type = 11; /* TTL exceeded */
285 icmp_pkt.checksum = 0;
286 icmp_pkt.reserved = 0;
287 memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
288 off += sizeof(icmp_pkt);
290 /* build inner IP header */
291 memset(&ip_pkt, 0, sizeof(ip_pkt));
292 ip_pkt.vers_ihl = 0x45;
294 ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
295 ip_pkt.id = htons(0);
296 ip_pkt.flags_frag_offset = 0;
298 ip_pkt.proto = IPPROTO_UDP;
300 ip_pkt.src_ip = other->s_addr;
301 ip_pkt.dst_ip = dummy.s_addr;
302 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
303 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
304 off += sizeof(ip_pkt);
306 /* build UDP header */
307 udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
308 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
310 memset(&udp_pkt.length, 0, sizeof(uint32_t));
311 udp_pkt.length = htonl(port);
312 memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
313 off += sizeof(udp_pkt);
315 /* set ICMP checksum */
316 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
317 sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
318 memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
321 memset (&dst, 0, sizeof (dst));
322 dst.sin_family = AF_INET;
323 dst.sin_addr = *other;
324 err = sendto(rawsock,
327 (struct sockaddr*)&dst,
333 "sendto failed: %s\n", strerror(errno));
338 "Error: partial send of ICMP message\n");
344 * Send an ICMP message to the target.
346 * @param my_ip source address
347 * @param other target address
350 send_icmp (const struct in_addr *my_ip,
351 const struct in_addr *other)
353 struct ip_packet ip_pkt;
354 struct icmp_packet *icmp_pkt;
355 struct icmp_echo_packet icmp_echo;
356 struct sockaddr_in dst;
357 char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
362 memset(packet, 0, sizeof(packet));
363 /* ip header: send to (known) ip address */
365 memset(&ip_pkt, 0, sizeof(ip_pkt));
366 ip_pkt.vers_ihl = 0x45;
368 ip_pkt.pkt_len = sizeof (packet); /* huh? */
370 ip_pkt.flags_frag_offset = 0;
371 ip_pkt.ttl = IPDEFTTL;
372 ip_pkt.proto = IPPROTO_ICMP;
374 ip_pkt.src_ip = my_ip->s_addr;
375 ip_pkt.dst_ip = other->s_addr;
376 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
378 memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
379 off += sizeof (ip_pkt);
380 memset (&dst, 0, sizeof (dst));
381 dst.sin_family = AF_INET;
382 dst.sin_addr = *other;
384 /* icmp reply: time exceeded */
385 icmp_pkt = (struct icmp_packet*) &packet[off];
386 memset(icmp_pkt, 0, sizeof(struct icmp_packet));
387 icmp_pkt->type = ICMP_TIME_EXCEEDED;
389 icmp_pkt->reserved = 0;
390 icmp_pkt->checksum = 0;
392 off += sizeof (struct icmp_packet);
394 /* ip header of the presumably 'lost' udp packet */
395 ip_pkt.vers_ihl = 0x45;
397 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
399 ip_pkt.flags_frag_offset = 0;
400 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
401 ip_pkt.proto = IPPROTO_ICMP;
402 ip_pkt.src_ip = other->s_addr;
403 ip_pkt.dst_ip = dummy.s_addr;
405 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
406 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
407 off += sizeof (struct ip_packet);
409 make_echo (other, &icmp_echo, port);
410 memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
411 off += sizeof (struct icmp_echo_packet);
413 icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
414 sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
416 err = sendto(rawsock,
419 (struct sockaddr*)&dst,
420 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
425 "sendto failed: %s\n", strerror(errno));
430 "Error: partial send of ICMP message\n");
436 * Create an ICMP raw socket.
438 * @return INVALID_SOCKET on error
443 DWORD bOptVal = TRUE;
444 int bOptLen = sizeof(bOptVal);
447 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
448 if (INVALID_SOCKET == ret)
451 "Error opening RAW socket: %s\n",
453 return INVALID_SOCKET;
455 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != 0)
458 "Error setting SO_BROADCAST to ON: %s\n",
460 closesocket(rawsock);
461 return INVALID_SOCKET;
464 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen) != 0)
467 "Error setting IP_HDRINCL to ON: %s\n",
469 closesocket(rawsock);
470 return INVALID_SOCKET;
477 main (int argc, char *const *argv)
479 struct in_addr external;
480 struct in_addr target;
488 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
491 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
492 (1 != inet_pton (AF_INET, argv[2], &target)) )
495 "Error parsing IPv4 address: %s\n",
499 if ( (1 != sscanf (argv[3], "%u", &p) ) ||
504 "Error parsing port value `%s'\n",
510 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
512 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
515 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
518 "Internal error converting dummy IP to binary.\n");
521 if (-1 == (rawsock = make_raw_socket()))
523 send_icmp (&external,
525 send_icmp_udp (&external,
527 closesocket (rawsock);
532 /* end of gnunet-nat-client-windows.c */