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.c
23 * @brief Tool to help bypass NATs using ICMP method; must run as root (SUID will do)
24 * This code will work under GNU/Linux only.
25 * @author Christian Grothoff
27 * This program will send ONE ICMP message using RAW sockets
28 * to the IP address specified as the second argument. Since
29 * it uses RAW sockets, it must be installed SUID or run as 'root'.
30 * In order to keep the security risk of the resulting SUID binary
31 * minimal, the program ONLY opens the RAW socket with root
32 * privileges, then drops them and only then starts to process
33 * command line arguments. The code also does not link against
34 * any shared libraries (except libc) and is strictly minimal
35 * (except for checking for errors). The following list of people
36 * have reviewed this code and considered it safe since the last
37 * modification (if you reviewed it, please have your name added
40 * - Christian Grothoff
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <arpa/inet.h>
47 #include <sys/types.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_icmp.h>
56 #include <netinet/in.h>
59 * Must match IP given in the server.
61 #define DUMMY_IP "192.0.2.86"
63 #define NAT_TRAV_PORT 22225
71 uint16_t flags_frag_offset;
88 struct icmp_echo_packet
108 static struct in_addr dummy;
110 static uint32_t port;
113 calc_checksum(const uint16_t *data,
120 for (i=0;i<bytes/2;i++)
122 sum = (sum & 0xffff) + (sum >> 16);
123 sum = htons(0xffff - sum);
129 make_echo (const struct in_addr *src_ip,
130 struct icmp_echo_packet *echo, uint32_t num)
132 memset(echo, 0, sizeof(struct icmp_echo_packet));
133 echo->type = ICMP_ECHO;
137 echo->data = htons(num);
138 echo->checksum = htons(calc_checksum((uint16_t*)echo,
139 sizeof (struct icmp_echo_packet)));
144 * Send an ICMP message to the target.
146 * @param my_ip source address
147 * @param other target address
150 send_icmp_udp (const struct in_addr *my_ip,
151 const struct in_addr *other)
153 struct ip_packet ip_pkt;
154 struct icmp_packet icmp_pkt;
155 struct udp_packet udp_pkt;
157 struct sockaddr_in dst;
158 char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
163 /* ip header: send to (known) ip address */
165 memset(&ip_pkt, 0, sizeof(ip_pkt));
166 ip_pkt.vers_ihl = 0x45;
168 ip_pkt.pkt_len = htons(sizeof (packet));
169 ip_pkt.id = htons(256);
170 ip_pkt.flags_frag_offset = 0;
172 ip_pkt.proto = IPPROTO_ICMP;
174 ip_pkt.src_ip = my_ip->s_addr;
175 ip_pkt.dst_ip = other->s_addr;
176 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
177 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
178 off += sizeof(ip_pkt);
180 /* ip header of the presumably 'lost' udp packet */
181 ip_pkt.vers_ihl = 0x45;
183 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
185 icmp_pkt.type = 11; /* TTL exceeded */
187 icmp_pkt.checksum = 0;
188 icmp_pkt.reserved = 0;
189 memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
190 off += sizeof(icmp_pkt);
192 /* build inner IP header */
193 memset(&ip_pkt, 0, sizeof(ip_pkt));
194 ip_pkt.vers_ihl = 0x45;
196 ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
197 ip_pkt.id = htons(0);
198 ip_pkt.flags_frag_offset = 0;
200 ip_pkt.proto = IPPROTO_UDP;
202 ip_pkt.src_ip = other->s_addr;
203 ip_pkt.dst_ip = dummy.s_addr;
204 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
205 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
206 off += sizeof(ip_pkt);
208 /* build UDP header */
209 udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
210 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
212 memset(&udp_pkt.length, 0, sizeof(uint32_t));
213 udp_pkt.length = htonl(port);
214 memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
215 off += sizeof(udp_pkt);
217 /* set ICMP checksum */
218 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
219 sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
220 memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
223 memset (&dst, 0, sizeof (dst));
224 dst.sin_family = AF_INET;
225 dst.sin_addr = *other;
226 err = sendto(rawsock,
229 (struct sockaddr*)&dst,
235 "sendto failed: %s\n", strerror(errno));
240 "Error: partial send of ICMP message\n");
246 * Send an ICMP message to the target.
248 * @param my_ip source address
249 * @param other target address
252 send_icmp (const struct in_addr *my_ip,
253 const struct in_addr *other)
255 struct ip_packet ip_pkt;
256 struct icmp_packet *icmp_pkt;
257 struct icmp_echo_packet icmp_echo;
258 struct sockaddr_in dst;
259 char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
264 /* ip header: send to (known) ip address */
266 memset(&ip_pkt, 0, sizeof(ip_pkt));
267 ip_pkt.vers_ihl = 0x45;
269 ip_pkt.pkt_len = sizeof (packet); /* huh? */
271 ip_pkt.flags_frag_offset = 0;
272 ip_pkt.ttl = IPDEFTTL;
273 ip_pkt.proto = IPPROTO_ICMP;
275 ip_pkt.src_ip = my_ip->s_addr;
276 ip_pkt.dst_ip = other->s_addr;
277 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
278 memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
279 off += sizeof (ip_pkt);
280 /* icmp reply: time exceeded */
281 icmp_pkt = (struct icmp_packet*) &packet[off];
282 memset(icmp_pkt, 0, sizeof(struct icmp_packet));
283 icmp_pkt->type = ICMP_TIME_EXCEEDED;
285 icmp_pkt->reserved = 0;
286 icmp_pkt->checksum = 0;
288 off += sizeof (struct icmp_packet);
290 /* ip header of the presumably 'lost' udp packet */
291 ip_pkt.vers_ihl = 0x45;
293 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
296 ip_pkt.flags_frag_offset = 0;
297 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
298 ip_pkt.proto = IPPROTO_ICMP;
299 ip_pkt.src_ip = other->s_addr;
300 ip_pkt.dst_ip = dummy.s_addr;
302 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
303 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
304 off += sizeof (struct ip_packet);
306 make_echo (other, &icmp_echo, port);
307 memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
308 off += sizeof (struct icmp_echo_packet);
310 icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
311 sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
313 memset (&dst, 0, sizeof (dst));
314 dst.sin_family = AF_INET;
315 dst.sin_addr = *other;
316 err = sendto(rawsock,
319 (struct sockaddr*)&dst,
320 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
324 "sendto failed: %s\n", strerror(errno));
329 "Error: partial send of ICMP message\n");
340 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
344 "Error opening RAW socket: %s\n",
348 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
349 (char *)&one, sizeof(one)) == -1)
351 "setsockopt failed: %s\n",
353 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
354 (char *)&one, sizeof(one)) == -1)
356 "setsockopt failed: %s\n",
363 main (int argc, char *const *argv)
365 struct in_addr external;
366 struct in_addr target;
369 if (-1 == (rawsock = make_raw_socket()))
372 if (0 != setresuid (uid, uid, uid))
374 "Failed to setresuid: %s\n",
380 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
383 port = atoi(argv[3]);
385 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
386 (1 != inet_pton (AF_INET, argv[2], &target)) )
389 "Error parsing IPv4 address: %s\n",
393 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) abort ();
394 send_icmp (&external,
396 send_icmp_udp (&external,
402 /* end of gnunet-nat-client.c */