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;
64 #define ICMP_TIME_EXCEEDED 11
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 buf->s_addr = inet_addr(cp);
128 if (buf->s_addr == INADDR_NONE)
130 fprintf(stderr, "Error %d handling address %s", WSAGetLastError(), cp);
138 calc_checksum(const uint16_t *data,
145 for (i=0;i<bytes/2;i++)
147 sum = (sum & 0xffff) + (sum >> 16);
148 sum = htons(0xffff - sum);
154 make_echo (const struct in_addr *src_ip,
155 struct icmp_echo_packet *echo, uint32_t num)
157 memset(echo, 0, sizeof(struct icmp_echo_packet));
158 echo->type = ICMP_ECHO;
162 echo->data = htons(num);
163 echo->checksum = htons(calc_checksum((uint16_t*)echo,
164 sizeof (struct icmp_echo_packet)));
168 * Send an ICMP message to the target.
170 * @param my_ip source address
171 * @param other target address
174 send_icmp_udp (const struct in_addr *my_ip,
175 const struct in_addr *other)
177 struct ip_packet ip_pkt;
178 struct icmp_packet icmp_pkt;
179 struct udp_packet udp_pkt;
181 struct sockaddr_in dst;
182 char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
187 /* ip header: send to (known) ip address */
189 memset(&ip_pkt, 0, sizeof(ip_pkt));
190 ip_pkt.vers_ihl = 0x45;
192 ip_pkt.pkt_len = htons(sizeof (packet));
193 ip_pkt.id = htons(256);
194 ip_pkt.flags_frag_offset = 0;
196 ip_pkt.proto = IPPROTO_ICMP;
198 ip_pkt.src_ip = my_ip->s_addr;
199 ip_pkt.dst_ip = other->s_addr;
200 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
201 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
202 off += sizeof(ip_pkt);
204 /* ip header of the presumably 'lost' udp packet */
205 ip_pkt.vers_ihl = 0x45;
207 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
209 icmp_pkt.type = 11; /* TTL exceeded */
211 icmp_pkt.checksum = 0;
212 icmp_pkt.reserved = 0;
213 memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
214 off += sizeof(icmp_pkt);
216 /* build inner IP header */
217 memset(&ip_pkt, 0, sizeof(ip_pkt));
218 ip_pkt.vers_ihl = 0x45;
220 ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
221 ip_pkt.id = htons(0);
222 ip_pkt.flags_frag_offset = 0;
224 ip_pkt.proto = IPPROTO_UDP;
226 ip_pkt.src_ip = other->s_addr;
227 ip_pkt.dst_ip = dummy.s_addr;
228 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
229 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
230 off += sizeof(ip_pkt);
232 /* build UDP header */
233 udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
234 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
236 memset(&udp_pkt.length, 0, sizeof(uint32_t));
237 udp_pkt.length = htonl(port);
238 memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
239 off += sizeof(udp_pkt);
241 /* set ICMP checksum */
242 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
243 sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
244 memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
247 memset (&dst, 0, sizeof (dst));
248 dst.sin_family = AF_INET;
249 dst.sin_addr = *other;
250 err = sendto(rawsock,
253 (struct sockaddr*)&dst,
259 "sendto failed: %s\n", strerror(errno));
264 "Error: partial send of ICMP message\n");
270 * Send an ICMP message to the target.
272 * @param my_ip source address
273 * @param other target address
276 send_icmp (const struct in_addr *my_ip,
277 const struct in_addr *other)
279 struct ip_packet ip_pkt;
280 struct icmp_packet *icmp_pkt;
281 struct icmp_echo_packet icmp_echo;
282 struct sockaddr_in dst;
283 char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
288 memset(packet, 0, sizeof(packet));
289 /* ip header: send to (known) ip address */
291 memset(&ip_pkt, 0, sizeof(ip_pkt));
292 ip_pkt.vers_ihl = 0x45;
294 ip_pkt.pkt_len = sizeof (packet); /* huh? */
296 ip_pkt.flags_frag_offset = 0;
297 ip_pkt.ttl = IPDEFTTL;
298 ip_pkt.proto = IPPROTO_ICMP;
300 ip_pkt.src_ip = my_ip->s_addr;
301 ip_pkt.dst_ip = other->s_addr;
302 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
304 memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
305 off += sizeof (ip_pkt);
306 memset (&dst, 0, sizeof (dst));
307 dst.sin_family = AF_INET;
308 dst.sin_addr = *other;
310 /* icmp reply: time exceeded */
311 icmp_pkt = (struct icmp_packet*) &packet[off];
312 memset(icmp_pkt, 0, sizeof(struct icmp_packet));
313 icmp_pkt->type = ICMP_TIME_EXCEEDED;
315 icmp_pkt->reserved = 0;
316 icmp_pkt->checksum = 0;
318 off += sizeof (struct icmp_packet);
320 /* ip header of the presumably 'lost' udp packet */
321 ip_pkt.vers_ihl = 0x45;
323 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
325 ip_pkt.flags_frag_offset = 0;
326 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
327 ip_pkt.proto = IPPROTO_ICMP;
328 ip_pkt.src_ip = other->s_addr;
329 ip_pkt.dst_ip = dummy.s_addr;
331 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
332 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
333 off += sizeof (struct ip_packet);
335 make_echo (other, &icmp_echo, port);
336 memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
337 off += sizeof (struct icmp_echo_packet);
339 icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
340 sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
342 err = sendto(rawsock,
345 (struct sockaddr*)&dst,
346 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
351 "sendto failed: %s\n", strerror(errno));
356 "Error: partial send of ICMP message\n");
364 DWORD bOptVal = TRUE;
365 int bOptLen = sizeof(bOptVal);
368 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
372 "Error opening RAW socket: %s\n",
376 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST, (char*)&bOptVal, bOptLen) != 0)
377 fprintf(stderr, "Error setting SO_BROADCAST: ON\n");
379 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL, (char*)&bOptVal, bOptLen) != 0)
380 fprintf(stderr, "Error setting IP_HDRINCL: ON\n");
387 main (int argc, char *const *argv)
389 struct in_addr external;
390 struct in_addr target;
393 if (WSAStartup (MAKEWORD (2, 1), &wsaData) != 0)
395 fprintf (stderr, "Failed to find Winsock 2.1 or better.\n");
399 if (-1 == (rawsock = make_raw_socket()))
405 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
408 port = atoi(argv[3]);
410 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
411 (1 != inet_pton (AF_INET, argv[2], &target)) )
414 "Error parsing IPv4 address: %s\n",
418 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
421 "Error parsing IPv4 address: %s\n",
426 send_icmp (&external,
428 send_icmp_udp (&external,
432 closesocket (rawsock);
436 /* end of gnunet-nat-client-windows.c */