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.
131 struct icmp_ttl_exceeded_header
141 /* followed by original payload */
144 struct icmp_echo_header
156 * Beginning of UDP packet.
171 * Socket we use to send our ICMP packets.
173 static SOCKET rawsock;
176 * Target "dummy" address.
178 static struct in_addr dummy;
181 * Port we are listening on (communicated to the server).
183 static uint16_t port;
188 * Convert IPv4 address from text to binary form.
190 * @param af address family
191 * @param cp the address to print
192 * @param buf where to write the address result
193 * @return 1 on success
200 buf->s_addr = inet_addr(cp);
201 if (buf->s_addr == INADDR_NONE)
204 "Error %d handling address %s",
214 * CRC-16 for IP/ICMP headers.
216 * @param data what to calculate the CRC over
217 * @param bytes number of bytes in data (must be multiple of 2)
218 * @return the CRC 16.
221 calc_checksum(const uint16_t *data,
228 for (i=0;i<bytes/2;i++)
230 sum = (sum & 0xffff) + (sum >> 16);
231 sum = htons(0xffff - sum);
237 * Send an ICMP message to the target.
239 * @param my_ip source address
240 * @param other target address
243 send_icmp_udp (const struct in_addr *my_ip,
244 const struct in_addr *other)
246 char packet[sizeof(struct ip_header) * 2 +
247 sizeof(struct icmp_ttl_exceeded_header) +
248 sizeof(struct udp_header)];
249 struct ip_header ip_pkt;
250 struct icmp_ttl_exceeded_header icmp_pkt;
251 struct udp_header udp_pkt;
252 struct sockaddr_in dst;
256 /* ip header: send to (known) ip address */
258 ip_pkt.vers_ihl = 0x45;
260 ip_pkt.pkt_len = htons(sizeof (packet));
261 ip_pkt.id = htons(256);
262 ip_pkt.flags_frag_offset = 0;
264 ip_pkt.proto = IPPROTO_ICMP;
266 ip_pkt.src_ip = my_ip->s_addr;
267 ip_pkt.dst_ip = other->s_addr;
268 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
269 sizeof (struct ip_header)));
272 sizeof(struct ip_header));
273 off += sizeof(struct ip_header);
275 icmp_pkt.type = ICMP_TIME_EXCEEDED;
277 icmp_pkt.checksum = 0;
281 sizeof(struct icmp_ttl_exceeded_header));
282 off += sizeof(struct icmp_ttl_exceeded_header);
284 /* ip header of the presumably 'lost' udp packet */
285 ip_pkt.vers_ihl = 0x45;
287 ip_pkt.pkt_len = htons(sizeof (struct ip_header) +
288 sizeof (struct udp_header));
289 ip_pkt.id = htons(0);
290 ip_pkt.flags_frag_offset = 0;
292 ip_pkt.proto = IPPROTO_UDP;
294 ip_pkt.src_ip = other->s_addr;
295 ip_pkt.dst_ip = dummy.s_addr;
296 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
297 sizeof (struct ip_header)));
300 sizeof(struct ip_header));
301 off += sizeof(struct ip_header);
303 /* build UDP header */
304 udp_pkt.src_port = htons(NAT_TRAV_PORT);
305 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
306 udp_pkt.length = htons (port);
310 sizeof(struct udp_header));
311 off += sizeof(struct udp_header);
313 /* no go back to calculate ICMP packet checksum */
314 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[off],
315 sizeof (struct icmp_ttl_exceeded_header) +
316 sizeof (struct ip_header) +
317 sizeof (struct udp_header)));
318 memcpy (&packet[sizeof (struct ip_header)],
320 sizeof (struct icmp_ttl_exceeded_header));
322 memset (&dst, 0, sizeof (dst));
323 dst.sin_family = AF_INET;
324 dst.sin_addr = *other;
325 err = sendto(rawsock,
328 (struct sockaddr*)&dst,
333 "sendto failed: %s\n", strerror(errno));
335 else if (sizeof (packet) != (size_t) err)
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_header ip_pkt;
354 struct icmp_ttl_exceeded_header icmp_ttl;
355 struct icmp_echo_header icmp_echo;
356 struct sockaddr_in dst;
357 char packet[sizeof (struct ip_header) * 2 +
358 sizeof (struct icmp_ttl_exceeded_header) +
359 sizeof(struct icmp_echo_header)];
363 /* ip header: send to (known) ip address */
365 ip_pkt.vers_ihl = 0x45;
367 ip_pkt.pkt_len = htons (sizeof (packet));
368 ip_pkt.id = htons(256);
369 ip_pkt.flags_frag_offset = 0;
370 ip_pkt.ttl = IPDEFTTL;
371 ip_pkt.proto = IPPROTO_ICMP;
373 ip_pkt.src_ip = my_ip->s_addr;
374 ip_pkt.dst_ip = other->s_addr;
375 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
376 sizeof (struct ip_header)));
377 memcpy (&packet[off],
379 sizeof (struct ip_header));
380 off += sizeof (ip_pkt);
382 /* icmp reply: time exceeded */
383 icmp_ttl.type = ICMP_TIME_EXCEEDED;
385 icmp_ttl.checksum = 0;
387 memcpy (&packet[off],
389 sizeof (struct icmp_ttl_exceeded_header));
390 off += sizeof (struct icmp_ttl_exceeded_header);
392 /* ip header of the presumably 'lost' udp packet */
393 ip_pkt.vers_ihl = 0x45;
395 ip_pkt.pkt_len = htons(sizeof (struct ip_header) + sizeof (struct icmp_echo_header));
396 ip_pkt.id = htons (256);
397 ip_pkt.flags_frag_offset = 0;
398 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
399 ip_pkt.proto = IPPROTO_ICMP;
400 ip_pkt.src_ip = other->s_addr;
401 ip_pkt.dst_ip = dummy.s_addr;
403 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt,
404 sizeof (struct ip_header)));
405 memcpy (&packet[off],
407 sizeof (struct ip_header));
408 off += sizeof (struct ip_header);
410 icmp_echo.type = ICMP_ECHO;
412 icmp_echo.reserved = htonl(port);
413 icmp_echo.checksum = 0;
414 icmp_echo.checksum = htons(calc_checksum((uint16_t*) &icmp_echo,
415 sizeof (struct icmp_echo_header)));
416 memcpy (&packet[off],
418 sizeof(struct icmp_echo_header));
420 /* no go back to calculate ICMP packet checksum */
421 off = sizeof (struct ip_header);
422 icmp_ttl.checksum = htons(calc_checksum((uint16_t*) &packet[off],
423 sizeof (struct icmp_ttl_exceeded_header) +
424 sizeof (struct ip_header) +
425 sizeof (struct icmp_echo_header)));
426 memcpy (&packet[off],
428 sizeof (struct icmp_ttl_exceeded_header));
430 memset (&dst, 0, sizeof (dst));
431 dst.sin_family = AF_INET;
432 dst.sin_addr = *other;
434 err = sendto(rawsock,
437 (struct sockaddr*)&dst,
443 "sendto failed: %s\n", strerror(errno));
445 else if (sizeof (packet) != (size_t) err)
448 "Error: partial send of ICMP message\n");
454 * Create an ICMP raw socket.
456 * @return INVALID_SOCKET on error
461 DWORD bOptVal = TRUE;
462 int bOptLen = sizeof(bOptVal);
465 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
466 if (INVALID_SOCKET == ret)
469 "Error opening RAW socket: %s\n",
471 return INVALID_SOCKET;
473 if (0 != setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen))
476 "Error setting SO_BROADCAST to ON: %s\n",
478 closesocket(rawsock);
479 return INVALID_SOCKET;
482 if (0 != setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen))
485 "Error setting IP_HDRINCL to ON: %s\n",
487 closesocket(rawsock);
488 return INVALID_SOCKET;
495 main (int argc, char *const *argv)
497 struct in_addr external;
498 struct in_addr target;
506 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
509 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
510 (1 != inet_pton (AF_INET, argv[2], &target)) )
513 "Error parsing IPv4 address: %s\n",
517 if ( (1 != sscanf (argv[3], "%u", &p) ) ||
522 "Error parsing port value `%s'\n",
528 if (0 != WSAStartup (MAKEWORD (2, 1), &wsaData))
530 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
533 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
536 "Internal error converting dummy IP to binary.\n");
539 if (-1 == (rawsock = make_raw_socket()))
541 send_icmp (&external,
543 send_icmp_udp (&external,
545 closesocket (rawsock);
550 /* end of gnunet-nat-client-windows.c */