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>
59 typedef unsigned int uid_t;
60 typedef SOCKET Socket;
61 typedef unsigned short ushort;
63 #define IPDEFTTL 64 /* default ttl, from RFC 1340 */
64 #define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */
67 * Must match IP given in the server.
69 #define DUMMY_IP "192.0.2.86"
71 #define NAT_TRAV_PORT 22225
79 uint16_t flags_frag_offset;
96 struct icmp_echo_packet
114 static Socket rawsock;
116 static struct in_addr dummy;
118 static uint32_t port;
121 * @param af address family
122 * @param cp the address to print
123 * @param buf where to write the address result
125 static int inet_pton (int af, char *cp, struct in_addr *buf)
127 //ret = WSAStringToAddress (cp, af, NULL, (LPSOCKADDR)buf, &ssize);
128 buf->s_addr = inet_addr(cp);
129 if (buf->s_addr == INADDR_NONE)
131 fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp);
139 calc_checksum(const uint16_t *data,
146 for (i=0;i<bytes/2;i++)
148 sum = (sum & 0xffff) + (sum >> 16);
149 sum = htons(0xffff - sum);
155 make_echo (const struct in_addr *src_ip,
156 struct icmp_echo_packet *echo, uint32_t num)
158 memset(echo, 0, sizeof(struct icmp_echo_packet));
159 echo->type = ICMP_ECHO;
163 echo->data = htons(num);
164 echo->checksum = htons(calc_checksum((uint16_t*)echo,
165 sizeof (struct icmp_echo_packet)));
169 * Send an ICMP message to the target.
171 * @param my_ip source address
172 * @param other target address
175 send_icmp_udp (const struct in_addr *my_ip,
176 const struct in_addr *other)
178 struct ip_packet ip_pkt;
179 struct icmp_packet icmp_pkt;
180 struct udp_packet udp_pkt;
182 struct sockaddr_in dst;
183 char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
188 /* ip header: send to (known) ip address */
190 memset(&ip_pkt, 0, sizeof(ip_pkt));
191 ip_pkt.vers_ihl = 0x45;
193 ip_pkt.pkt_len = htons(sizeof (packet));
194 ip_pkt.id = htons(256);
195 ip_pkt.flags_frag_offset = 0;
197 ip_pkt.proto = IPPROTO_ICMP;
199 ip_pkt.src_ip = my_ip->s_addr;
200 ip_pkt.dst_ip = other->s_addr;
201 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
202 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
203 off += sizeof(ip_pkt);
205 /* ip header of the presumably 'lost' udp packet */
206 ip_pkt.vers_ihl = 0x45;
208 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
210 icmp_pkt.type = 11; /* TTL exceeded */
212 icmp_pkt.checksum = 0;
213 icmp_pkt.reserved = 0;
214 memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
215 off += sizeof(icmp_pkt);
217 /* build inner IP header */
218 memset(&ip_pkt, 0, sizeof(ip_pkt));
219 ip_pkt.vers_ihl = 0x45;
221 ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
222 ip_pkt.id = htons(0);
223 ip_pkt.flags_frag_offset = 0;
225 ip_pkt.proto = IPPROTO_UDP;
227 ip_pkt.src_ip = other->s_addr;
228 ip_pkt.dst_ip = dummy.s_addr;
229 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
230 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
231 off += sizeof(ip_pkt);
233 /* build UDP header */
234 udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
235 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
237 memset(&udp_pkt.length, 0, sizeof(uint32_t));
238 udp_pkt.length = htonl(port);
239 memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
240 off += sizeof(udp_pkt);
242 /* set ICMP checksum */
243 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
244 sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
245 memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
248 memset (&dst, 0, sizeof (dst));
249 dst.sin_family = AF_INET;
250 dst.sin_addr = *other;
251 err = sendto(rawsock,
254 (struct sockaddr*)&dst,
260 "sendto failed: %s\n", strerror(errno));
265 "Error: partial send of ICMP message\n");
271 * Send an ICMP message to the target.
273 * @param my_ip source address
274 * @param other target address
277 send_icmp (const struct in_addr *my_ip,
278 const struct in_addr *other)
280 struct ip_packet ip_pkt;
281 struct icmp_packet *icmp_pkt;
282 struct icmp_echo_packet icmp_echo;
283 struct sockaddr_in dst;
284 char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
289 memset(packet, 0, sizeof(packet));
290 /* ip header: send to (known) ip address */
292 memset(&ip_pkt, 0, sizeof(ip_pkt));
293 ip_pkt.vers_ihl = 0x45;
295 ip_pkt.pkt_len = sizeof (packet); /* huh? */
297 ip_pkt.flags_frag_offset = 0;
298 ip_pkt.ttl = IPDEFTTL;
299 ip_pkt.proto = IPPROTO_ICMP;
301 ip_pkt.src_ip = my_ip->s_addr;
302 ip_pkt.dst_ip = other->s_addr;
303 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
305 memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
306 off += sizeof (ip_pkt);
307 memset (&dst, 0, sizeof (dst));
308 dst.sin_family = AF_INET;
309 //dst.sin_addr = *other;
310 dst.sin_addr = *other;
312 /* icmp reply: time exceeded */
313 icmp_pkt = (struct icmp_packet*) &packet[off];
314 memset(icmp_pkt, 0, sizeof(struct icmp_packet));
315 icmp_pkt->type = ICMP_TIME_EXCEEDED;
317 icmp_pkt->reserved = 0;
318 icmp_pkt->checksum = 0;
320 off += sizeof (struct icmp_packet);
322 /* ip header of the presumably 'lost' udp packet */
323 ip_pkt.vers_ihl = 0x45;
325 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
327 ip_pkt.flags_frag_offset = 0;
328 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
329 ip_pkt.proto = IPPROTO_ICMP;
330 ip_pkt.src_ip = other->s_addr;
331 ip_pkt.dst_ip = dummy.s_addr;
333 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
334 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
335 off += sizeof (struct ip_packet);
337 make_echo (other, &icmp_echo, port);
338 memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
339 off += sizeof (struct icmp_echo_packet);
341 icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
342 sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
344 err = sendto(rawsock,
347 (struct sockaddr*)&dst,
348 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
353 "sendto failed: %s\n", strerror(errno));
358 "Error: partial send of ICMP message\n");
366 DWORD bOptVal = TRUE;
367 int bOptLen = sizeof(bOptVal);
370 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
374 "Error opening RAW socket: %s\n",
378 if (setsockopt(rawsock, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != 0)
379 fprintf(stderr, "Error setting SO_BROADCAST: ON\n");
381 if (setsockopt(rawsock, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen) != 0)
382 fprintf(stderr, "Error setting IP_HDRINCL: ON\n");
389 main (int argc, char *const *argv)
391 struct in_addr external;
392 struct in_addr target;
396 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
398 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
402 if (-1 == (rawsock = make_raw_socket()))
408 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
411 port = atoi(argv[3]);
413 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
414 (1 != inet_pton (AF_INET, argv[2], &target)) )
417 "Error parsing IPv4 address: %s\n",
421 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
424 "Error parsing IPv4 address: %s\n",
429 send_icmp (&external,
431 send_icmp_udp (&external,
435 closesocket (rawsock);
439 /* end of gnunet-nat-client-windows.c */