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
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <arpa/inet.h>
46 #include <sys/types.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_icmp.h>
55 #include <netinet/in.h>
58 * Must match IP given in the server.
60 #define DUMMY_IP "1.2.3.4"
62 #define NAT_TRAV_PORT 22225
70 uint16_t flags_frag_offset;
87 struct icmp_echo_packet
107 static struct in_addr dummy;
109 static uint32_t port;
112 calc_checksum(const uint16_t *data,
119 for (i=0;i<bytes/2;i++)
121 sum = (sum & 0xffff) + (sum >> 16);
122 sum = htons(0xffff - sum);
128 make_echo (const struct in_addr *src_ip,
129 struct icmp_echo_packet *echo, uint32_t num)
131 memset(echo, 0, sizeof(struct icmp_echo_packet));
132 echo->type = ICMP_ECHO;
136 echo->data = htons(num);
137 echo->checksum = htons(calc_checksum((uint16_t*)echo,
138 sizeof (struct icmp_echo_packet)));
143 * Send an ICMP message to the target.
145 * @param my_ip source address
146 * @param other target address
149 send_icmp_udp (const struct in_addr *my_ip,
150 const struct in_addr *other)
152 struct ip_packet ip_pkt;
153 struct icmp_packet icmp_pkt;
154 struct udp_packet udp_pkt;
156 struct sockaddr_in dst;
157 char packet[sizeof(ip_pkt) * 2 + sizeof(icmp_pkt) * 2 + sizeof(uint32_t)];
162 /* ip header: send to (known) ip address */
164 memset(&ip_pkt, 0, sizeof(ip_pkt));
165 ip_pkt.vers_ihl = 0x45;
167 ip_pkt.pkt_len = htons(sizeof (packet));
168 ip_pkt.id = htons(256);
169 ip_pkt.flags_frag_offset = 0;
171 ip_pkt.proto = IPPROTO_ICMP;
173 ip_pkt.src_ip = my_ip->s_addr;
174 ip_pkt.dst_ip = other->s_addr;
175 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
176 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
177 off += sizeof(ip_pkt);
179 /* ip header of the presumably 'lost' udp packet */
180 ip_pkt.vers_ihl = 0x45;
182 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
184 icmp_pkt.type = 11; /* TTL exceeded */
186 icmp_pkt.checksum = 0;
187 icmp_pkt.reserved = 0;
188 memcpy(&packet[off], &icmp_pkt, sizeof(icmp_pkt));
189 off += sizeof(icmp_pkt);
191 /* build inner IP header */
192 memset(&ip_pkt, 0, sizeof(ip_pkt));
193 ip_pkt.vers_ihl = 0x45;
195 ip_pkt.pkt_len = htons(sizeof (ip_pkt) + sizeof(udp_pkt));
196 ip_pkt.id = htons(0);
197 ip_pkt.flags_frag_offset = 0;
199 ip_pkt.proto = IPPROTO_UDP;
201 ip_pkt.src_ip = other->s_addr;
202 ip_pkt.dst_ip = dummy.s_addr;
203 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
204 memcpy(&packet[off], &ip_pkt, sizeof(ip_pkt));
205 off += sizeof(ip_pkt);
207 /* build UDP header */
208 udp_pkt.src_port = htons(NAT_TRAV_PORT); /* FIXME: does this port matter? */
209 udp_pkt.dst_port = htons(NAT_TRAV_PORT);
211 memset(&udp_pkt.length, 0, sizeof(uint32_t));
212 udp_pkt.length = htonl(port);
213 memcpy(&packet[off], &udp_pkt, sizeof(udp_pkt));
214 off += sizeof(udp_pkt);
216 /* set ICMP checksum */
217 icmp_pkt.checksum = htons(calc_checksum((uint16_t*)&packet[sizeof(ip_pkt)],
218 sizeof (icmp_pkt) + sizeof(ip_pkt) + sizeof(udp_pkt)));
219 memcpy (&packet[sizeof(ip_pkt)], &icmp_pkt, sizeof (icmp_pkt));
222 memset (&dst, 0, sizeof (dst));
223 dst.sin_family = AF_INET;
224 dst.sin_addr = *other;
225 err = sendto(rawsock,
228 (struct sockaddr*)&dst,
234 "sendto failed: %s\n", strerror(errno));
239 "Error: partial send of ICMP message\n");
245 * Send an ICMP message to the target.
247 * @param my_ip source address
248 * @param other target address
251 send_icmp (const struct in_addr *my_ip,
252 const struct in_addr *other)
254 struct ip_packet ip_pkt;
255 struct icmp_packet *icmp_pkt;
256 struct icmp_echo_packet icmp_echo;
257 struct sockaddr_in dst;
258 char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet) + sizeof(struct icmp_echo_packet)];
263 /* ip header: send to (known) ip address */
265 memset(&ip_pkt, 0, sizeof(ip_pkt));
266 ip_pkt.vers_ihl = 0x45;
268 ip_pkt.pkt_len = sizeof (packet); /* huh? */
270 ip_pkt.flags_frag_offset = 0;
271 ip_pkt.ttl = IPDEFTTL;
272 ip_pkt.proto = IPPROTO_ICMP;
274 ip_pkt.src_ip = my_ip->s_addr;
275 ip_pkt.dst_ip = other->s_addr;
276 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
277 memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
278 off += sizeof (ip_pkt);
279 /* icmp reply: time exceeded */
280 icmp_pkt = (struct icmp_packet*) &packet[off];
281 memset(icmp_pkt, 0, sizeof(struct icmp_packet));
282 icmp_pkt->type = ICMP_TIME_EXCEEDED;
284 icmp_pkt->reserved = 0;
285 icmp_pkt->checksum = 0;
287 off += sizeof (struct icmp_packet);
289 /* ip header of the presumably 'lost' udp packet */
290 ip_pkt.vers_ihl = 0x45;
292 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_echo_packet));
295 ip_pkt.flags_frag_offset = 0;
296 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
297 ip_pkt.proto = IPPROTO_ICMP;
298 ip_pkt.src_ip = other->s_addr;
299 ip_pkt.dst_ip = dummy.s_addr;
301 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
302 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
303 off += sizeof (struct ip_packet);
305 make_echo (other, &icmp_echo, port);
306 memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_echo_packet));
307 off += sizeof (struct icmp_echo_packet);
309 icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
310 sizeof (struct icmp_packet) + sizeof(struct ip_packet) + sizeof(struct icmp_echo_packet)));
312 memset (&dst, 0, sizeof (dst));
313 dst.sin_family = AF_INET;
314 dst.sin_addr = *other;
315 err = sendto(rawsock,
318 (struct sockaddr*)&dst,
319 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
323 "sendto failed: %s\n", strerror(errno));
328 "Error: partial send of ICMP message\n");
339 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
343 "Error opening RAW socket: %s\n",
347 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
348 (char *)&one, sizeof(one)) == -1)
350 "setsockopt failed: %s\n",
352 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
353 (char *)&one, sizeof(one)) == -1)
355 "setsockopt failed: %s\n",
362 main (int argc, char *const *argv)
364 struct in_addr external;
365 struct in_addr target;
368 if (-1 == (rawsock = make_raw_socket()))
371 if (0 != setresuid (uid, uid, uid))
373 "Failed to setresuid: %s\n",
379 "This program must be started with our IP, the targets external IP, and our port as arguments.\n");
382 port = atoi(argv[3]);
384 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
385 (1 != inet_pton (AF_INET, argv[2], &target)) )
388 "Error parsing IPv4 address: %s\n",
392 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) abort ();
393 send_icmp (&external,
395 send_icmp_udp (&external,
401 /* end of gnunet-nat-client.c */