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
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <arpa/inet.h>
51 #include <sys/select.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip_icmp.h>
54 #include <netinet/in.h>
57 #include <sys/types.h>
68 typedef unsigned int uid_t;
69 typedef SOCKET Socket;
70 typedef unsigned short ushort;
72 #define IPDEFTTL 64 /* default ttl, from RFC 1340 */
73 #define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
74 #define IP_HDRINCL 3 /* int; Header is included with data. */
80 * Must match IP given in the server.
82 #define DUMMY_IP "192.0.2.86"
84 #define NAT_TRAV_PORT 22225
92 uint16_t flags_frag_offset;
109 struct icmp_echo_packet
127 static Socket rawsock;
129 static struct in_addr dummy;
131 static uint32_t port;
135 * @param af address family
136 * @param cp the address to print
137 * @param buf where to write the address result
139 static int inet_pton (int af, char *cp, struct in_addr *buf)
141 //ret = WSAStringToAddress (cp, af, NULL, (LPSOCKADDR)buf, &ssize);
142 buf->s_addr = inet_addr(cp);
143 if (buf->s_addr == INADDR_NONE)
145 fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp);
154 calc_checksum(const uint16_t *data,
161 for (i=0;i<bytes/2;i++)
163 sum = (sum & 0xffff) + (sum >> 16);
164 sum = htons(0xffff - sum);
170 make_echo (const struct in_addr *src_ip,
171 struct icmp_echo_packet *echo, uint32_t num)
173 memset(echo, 0, sizeof(struct icmp_echo_packet));
174 echo->type = ICMP_ECHO;
178 echo->data = htons(num);
179 echo->checksum = htons(calc_checksum((uint16_t*)echo,
180 sizeof (struct icmp_echo_packet)));
184 make_echo2 (const struct in_addr *src_ip,
185 struct icmp_packet *echo)
187 memset(echo, 0, sizeof(struct icmp_packet));
188 echo->type = ICMP_ECHO;
192 echo->checksum = htons(calc_checksum((uint16_t*)echo, sizeof (struct icmp_packet)));
196 * Send an ICMP message to the dummy IP.
198 * @param my_ip source address (our ip address)
201 send_icmp_echo (const struct in_addr *my_ip)
203 struct icmp_packet icmp_echo;
204 struct sockaddr_in dst;
207 struct ip_packet ip_pkt;
208 struct icmp_packet icmp_pkt;
209 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
212 memset(&ip_pkt, 0, sizeof(ip_pkt));
213 ip_pkt.vers_ihl = 0x45;
215 ip_pkt.pkt_len = sizeof (packet);
217 ip_pkt.flags_frag_offset = 0;
218 ip_pkt.ttl = IPDEFTTL;
219 ip_pkt.proto = IPPROTO_ICMP;
221 ip_pkt.src_ip = my_ip->s_addr;
222 ip_pkt.dst_ip = dummy.s_addr;
223 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
224 memcpy (packet, &ip_pkt, sizeof (ip_pkt));
225 off += sizeof (ip_pkt);
226 make_echo2 (my_ip, &icmp_echo);
227 memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
228 off += sizeof (icmp_echo);
230 memset (&dst, 0, sizeof (dst));
231 dst.sin_family = AF_INET;
232 dst.sin_addr = dummy;
233 err = sendto(rawsock,
235 (struct sockaddr*)&dst,
241 "sendto failed: %s\n", strerror(errno));
247 "Error: partial send of ICMP message\n");
252 * Send an ICMP message to the target.
254 * @param my_ip source address
255 * @param other target address
258 send_icmp_udp (const struct in_addr *my_ip,
259 const struct in_addr *other)
261 struct ip_packet ip_pkt;
262 struct icmp_packet icmp_pkt;
263 struct udp_packet udp_pkt;
265 struct sockaddr_in dst;
266 char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
271 /* ip header: send to (known) ip address */
273 memset(&ip_pkt, 0, sizeof(ip_pkt));
274 ip_pkt.vers_ihl = 0x45;
276 ip_pkt.pkt_len = htons(sizeof (packet));
277 ip_pkt.id = htons(256);
278 ip_pkt.flags_frag_offset = 0;
280 ip_pkt.proto = IPPROTO_ICMP;
282 ip_pkt.src_ip = my_ip->s_addr;
283 ip_pkt.dst_ip = other->s_addr;
284 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
285 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
286 off += sizeof(ip_pkt);
288 /* ip header of the presumably 'lost' udp packet */
289 ip_pkt.vers_ihl = 0x45;
291 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
293 icmp_pkt.type = 11; /* TTL exceeded */
295 icmp_pkt.checksum = 0;
296 icmp_pkt.reserved = 0;
297 memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
298 off += sizeof(icmp_pkt);
300 /* build inner IP header */
301 memset(&ip_pkt, 0, sizeof(ip_pkt));
302 ip_pkt.vers_ihl = 0x45;
304 ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
305 ip_pkt.id = htons(0);
306 ip_pkt.flags_frag_offset = 0;
308 ip_pkt.proto = IPPROTO_UDP;
310 ip_pkt.src_ip = other->s_addr;
311 ip_pkt.dst_ip = dummy.s_addr;
312 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
313 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
314 off += sizeof(ip_pkt);
316 /* build UDP header */
317 udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
318 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
320 memset(&udp_pkt.length, 0, sizeof(uint32_t));
321 udp_pkt.length = htonl(port);
322 memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
323 off += sizeof(udp_pkt);
325 /* set ICMP checksum */
326 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
327 sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
328 memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
331 memset (&dst, 0, sizeof (dst));
332 dst.sin_family = AF_INET;
333 dst.sin_addr = *other;
334 err = sendto(rawsock,
337 (struct sockaddr*)&dst,
343 "sendto failed: %s\n", strerror(errno));
348 "Error: partial send of ICMP message\n");
354 * Send an ICMP message to the target.
356 * @param my_ip source address
357 * @param other target address
360 send_icmp (const struct in_addr *my_ip,
361 const struct in_addr *other)
363 struct ip_packet ip_pkt;
364 struct icmp_packet *icmp_pkt;
365 struct icmp_echo_packet icmp_echo;
366 struct sockaddr_in dst;
367 char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
372 /* ip header: send to (known) ip address */
374 memset(&ip_pkt, 0, sizeof(ip_pkt));
375 ip_pkt.vers_ihl = 0x45;
377 ip_pkt.pkt_len = sizeof (packet); /* huh? */
379 ip_pkt.flags_frag_offset = 0;
380 ip_pkt.ttl = IPDEFTTL;
381 ip_pkt.proto = IPPROTO_ICMP;
383 ip_pkt.src_ip = my_ip->s_addr;
384 ip_pkt.dst_ip = other->s_addr;
385 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
386 memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
387 off += sizeof (ip_pkt);
388 /* icmp reply: time exceeded */
389 icmp_pkt = (struct icmp_packet*) &packet[off];
390 memset(icmp_pkt, 0, sizeof(struct icmp_packet));
391 icmp_pkt->type = ICMP_TIME_EXCEEDED;
393 icmp_pkt->reserved = 0;
394 icmp_pkt->checksum = 0;
396 off += sizeof (struct icmp_packet);
398 /* ip header of the presumably 'lost' udp packet */
399 ip_pkt.vers_ihl = 0x45;
401 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
404 ip_pkt.flags_frag_offset = 0;
405 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
406 ip_pkt.proto = IPPROTO_ICMP;
407 ip_pkt.src_ip = other->s_addr;
408 ip_pkt.dst_ip = dummy.s_addr;
410 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
411 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
412 off += sizeof (struct ip_packet);
414 make_echo (other, &icmp_echo, port);
415 memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
416 off += sizeof (struct icmp_echo_packet);
418 icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
419 sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
421 memset (&dst, 0, sizeof (dst));
422 dst.sin_family = AF_INET;
423 dst.sin_addr = *other;
424 err = sendto(rawsock,
427 (struct sockaddr*)&dst,
428 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
430 fprintf(stderr, "Sent %d bytes\n", err);
434 "sendto failed: %s\n", strerror(errno));
439 "Error: partial send of ICMP message\n");
450 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
454 "Error opening RAW socket: %s\n",
458 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
459 (char *)&one, sizeof(one)) == -1)
461 "setsockopt failed: %s\n",
463 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
464 (char *)&one, sizeof(one)) == -1)
466 "setsockopt failed: %s\n",
473 main (int argc, char *const *argv)
475 struct in_addr external;
476 struct in_addr target;
484 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
486 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
491 if (-1 == (rawsock = make_raw_socket()))
496 if (0 != setresuid (uid, uid, uid))
498 "Failed to setresuid: %s\n",
504 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
507 port = atoi(argv[3]);
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 != inet_pton (AF_INET, DUMMY_IP, &dummy))
520 "Error parsing IPv4 address: %s\n",
524 send_icmp_echo(&target);
525 fprintf(stderr, "Sending icmp message.\n");
526 send_icmp (&external,
528 fprintf(stderr, "Sending icmp udp message.\n");
529 send_icmp_udp (&external,
539 /* end of gnunet-nat-client.c */