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;
175 * Port we are listening on (communicated to the server).
177 static uint16_t port;
182 * Convert IPv4 address from text to binary form.
184 * @param af address family
185 * @param cp the address to print
186 * @param buf where to write the address result
187 * @return 1 on success
194 buf->s_addr = inet_addr(cp);
195 if (buf->s_addr == INADDR_NONE)
198 "Error %d handling address %s",
208 * CRC-16 for IP/ICMP headers.
210 * @param data what to calculate the CRC over
211 * @param bytes number of bytes in data (must be multiple of 2)
212 * @return the CRC 16.
215 calc_checksum(const uint16_t *data,
222 for (i=0;i<bytes/2;i++)
224 sum = (sum & 0xffff) + (sum >> 16);
225 sum = htons(0xffff - sum);
231 * Send an ICMP message to the target.
233 * @param my_ip source address
234 * @param other target address
237 send_icmp_udp (const struct in_addr *my_ip,
238 const struct in_addr *other)
240 struct ip_packet ip_pkt;
241 struct icmp_packet icmp_pkt;
242 struct udp_packet udp_pkt;
244 struct sockaddr_in dst;
245 char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
250 /* ip header: send to (known) ip address */
252 memset(&ip_pkt, 0, sizeof(ip_pkt));
253 ip_pkt.vers_ihl = 0x45;
255 ip_pkt.pkt_len = htons(sizeof (packet));
256 ip_pkt.id = htons(256);
257 ip_pkt.flags_frag_offset = 0;
259 ip_pkt.proto = IPPROTO_ICMP;
261 ip_pkt.src_ip = my_ip->s_addr;
262 ip_pkt.dst_ip = other->s_addr;
263 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
264 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
265 off += sizeof(ip_pkt);
267 /* ip header of the presumably 'lost' udp packet */
268 ip_pkt.vers_ihl = 0x45;
270 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
272 icmp_pkt.type = 11; /* TTL exceeded */
274 icmp_pkt.checksum = 0;
275 icmp_pkt.reserved = 0;
276 memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
277 off += sizeof(icmp_pkt);
279 /* build inner IP header */
280 memset(&ip_pkt, 0, sizeof(ip_pkt));
281 ip_pkt.vers_ihl = 0x45;
283 ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
284 ip_pkt.id = htons(0);
285 ip_pkt.flags_frag_offset = 0;
287 ip_pkt.proto = IPPROTO_UDP;
289 ip_pkt.src_ip = other->s_addr;
290 ip_pkt.dst_ip = dummy.s_addr;
291 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
292 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
293 off += sizeof(ip_pkt);
295 /* build UDP header */
296 udp_pkt.src_port = htons(NAT_TRAV_PORT);
297 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
299 memset(&udp_pkt.length, 0, sizeof(uint32_t));
300 udp_pkt.length = htons (port);
301 memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
302 off += sizeof(udp_pkt);
304 /* set ICMP checksum */
305 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
306 sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
307 memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
310 memset (&dst, 0, sizeof (dst));
311 dst.sin_family = AF_INET;
312 dst.sin_addr = *other;
313 err = sendto(rawsock,
316 (struct sockaddr*)&dst,
322 "sendto failed: %s\n", strerror(errno));
327 "Error: partial send of ICMP message\n");
333 * Send an ICMP message to the target.
335 * @param my_ip source address
336 * @param other target address
339 send_icmp (const struct in_addr *my_ip,
340 const struct in_addr *other)
342 struct ip_packet ip_pkt;
343 struct icmp_packet *icmp_pkt;
344 struct icmp_echo_packet icmp_echo;
345 struct sockaddr_in dst;
346 char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
351 memset(packet, 0, sizeof(packet));
352 /* ip header: send to (known) ip address */
354 memset(&ip_pkt, 0, sizeof(ip_pkt));
355 ip_pkt.vers_ihl = 0x45;
357 ip_pkt.pkt_len = sizeof (packet); /* huh? */
359 ip_pkt.flags_frag_offset = 0;
360 ip_pkt.ttl = IPDEFTTL;
361 ip_pkt.proto = IPPROTO_ICMP;
363 ip_pkt.src_ip = my_ip->s_addr;
364 ip_pkt.dst_ip = other->s_addr;
365 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
367 memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
368 off += sizeof (ip_pkt);
369 memset (&dst, 0, sizeof (dst));
370 dst.sin_family = AF_INET;
371 dst.sin_addr = *other;
373 /* icmp reply: time exceeded */
374 icmp_pkt = (struct icmp_packet*) &packet[off];
375 memset(icmp_pkt, 0, sizeof(struct icmp_packet));
376 icmp_pkt->type = ICMP_TIME_EXCEEDED;
378 icmp_pkt->reserved = 0;
379 icmp_pkt->checksum = 0;
381 off += sizeof (struct icmp_packet);
383 /* ip header of the presumably 'lost' udp packet */
384 ip_pkt.vers_ihl = 0x45;
386 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
388 ip_pkt.flags_frag_offset = 0;
389 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
390 ip_pkt.proto = IPPROTO_ICMP;
391 ip_pkt.src_ip = other->s_addr;
392 ip_pkt.dst_ip = dummy.s_addr;
394 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
395 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
396 off += sizeof (struct ip_packet);
398 icmp_echo.type = ICMP_ECHO;
400 icmp_echo.reserved = 0;
401 icmp_echo.checksum = 0;
402 icmp_echo.data = htons(port);
403 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
404 sizeof (struct icmp_echo_packet)));
406 memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
407 off += sizeof (struct icmp_echo_packet);
409 icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
410 sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
412 err = sendto(rawsock,
415 (struct sockaddr*)&dst,
416 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
421 "sendto failed: %s\n", strerror(errno));
426 "Error: partial send of ICMP message\n");
432 * Create an ICMP raw socket.
434 * @return INVALID_SOCKET on error
439 DWORD bOptVal = TRUE;
440 int bOptLen = sizeof(bOptVal);
443 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
444 if (INVALID_SOCKET == ret)
447 "Error opening RAW socket: %s\n",
449 return INVALID_SOCKET;
451 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != 0)
454 "Error setting SO_BROADCAST to ON: %s\n",
456 closesocket(rawsock);
457 return INVALID_SOCKET;
460 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen) != 0)
463 "Error setting IP_HDRINCL to ON: %s\n",
465 closesocket(rawsock);
466 return INVALID_SOCKET;
473 main (int argc, char *const *argv)
475 struct in_addr external;
476 struct in_addr target;
484 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
487 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
488 (1 != inet_pton (AF_INET, argv[2], &target)) )
491 "Error parsing IPv4 address: %s\n",
495 if ( (1 != sscanf (argv[3], "%u", &p) ) ||
500 "Error parsing port value `%s'\n",
506 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
508 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
511 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
514 "Internal error converting dummy IP to binary.\n");
517 if (-1 == (rawsock = make_raw_socket()))
519 send_icmp (&external,
521 send_icmp_udp (&external,
523 closesocket (rawsock);
528 /* end of gnunet-nat-client-windows.c */