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-server.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 (or maybe BSDs, but never W32)
25 * @author Christian Grothoff
27 * This program will send ONE ICMP message every 500 ms RAW sockets
28 * to a DUMMY IP address and also listens for ICMP replies. 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 two RAW sockets 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/select.h>
49 #include <sys/types.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_icmp.h>
59 #include <netinet/in.h>
62 * Must match IP given in the client.
64 #define DUMMY_IP "192.0.2.86"
69 * How often do we send our ICMP messages to receive replies?
71 #define ICMP_SEND_FREQUENCY_MS 500
79 uint16_t flags_frag_offset;
108 static struct in_addr dummy;
111 calc_checksum(const uint16_t *data,
118 for (i=0;i<bytes/2;i++)
120 sum = (sum & 0xffff) + (sum >> 16);
121 sum = htons(0xffff - sum);
127 make_echo (const struct in_addr *src_ip,
128 struct icmp_packet *echo)
130 memset(echo, 0, sizeof(struct icmp_packet));
131 echo->type = ICMP_ECHO;
135 echo->checksum = htons(calc_checksum((uint16_t*)echo, sizeof (struct icmp_packet)));
140 * Send an ICMP message to the dummy IP.
142 * @param my_ip source address (our ip address)
145 send_icmp_echo (const struct in_addr *my_ip)
147 struct icmp_packet icmp_echo;
148 struct sockaddr_in dst;
151 struct ip_packet ip_pkt;
152 struct icmp_packet icmp_pkt;
153 char packet[sizeof (ip_pkt) + sizeof (icmp_pkt)];
156 memset(&ip_pkt, 0, sizeof(ip_pkt));
157 ip_pkt.vers_ihl = 0x45;
159 ip_pkt.pkt_len = sizeof (packet);
161 ip_pkt.flags_frag_offset = 0;
162 ip_pkt.ttl = IPDEFTTL;
163 ip_pkt.proto = IPPROTO_ICMP;
165 ip_pkt.src_ip = my_ip->s_addr;
166 ip_pkt.dst_ip = dummy.s_addr;
167 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (ip_pkt)));
168 memcpy (packet, &ip_pkt, sizeof (ip_pkt));
169 off += sizeof (ip_pkt);
170 make_echo (my_ip, &icmp_echo);
171 memcpy (&packet[off], &icmp_echo, sizeof (icmp_echo));
172 off += sizeof (icmp_echo);
174 memset (&dst, 0, sizeof (dst));
175 dst.sin_family = AF_INET;
176 dst.sin_addr = dummy;
177 err = sendto(rawsock,
179 (struct sockaddr*)&dst,
185 "sendto failed: %s\n", strerror(errno));
191 "Error: partial send of ICMP message\n");
197 process_icmp_response ()
202 struct ip_packet ip_pkt;
203 struct icmp_packet icmp_pkt;
204 struct udp_packet udp_pkt;
210 have = read (icmpsock, buf, sizeof (buf));
214 "Error reading raw socket: %s\n",
220 if (have == sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2 + sizeof(uint32_t))
224 else if (have != sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2)
228 "Received ICMP message of unexpected size: %u bytes\n",
229 (unsigned int) have);
234 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
235 off += sizeof (ip_pkt);
236 memcpy (&icmp_pkt, &buf[off], sizeof (icmp_pkt));
237 off += sizeof (icmp_pkt);
238 if ( ((ip_pkt.proto != IPPROTO_ICMP) && (ip_pkt.proto != IPPROTO_UDP)) ||
239 (icmp_pkt.type != ICMP_TIME_EXCEEDED) ||
240 (icmp_pkt.code != 0) )
242 /* maybe we got an actual reply back... */
249 memcpy (&ip_pkt, &buf[off], sizeof (ip_pkt));
250 off += sizeof (ip_pkt);
253 if (ip_pkt.proto == IPPROTO_UDP)
260 memcpy(&port, &buf[sizeof (struct ip_packet) *2 + sizeof (struct icmp_packet) * 2], sizeof(uint32_t));
267 sizeof (buf)), port);
271 memcpy(&udp_pkt, &buf[off], sizeof(udp_pkt));
277 sizeof (buf)), ntohl(udp_pkt.length));
297 ret = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
301 "Error opening RAW socket: %s\n",
305 if (ret >= FD_SETSIZE)
308 "Socket number too large (%d > %u)\n",
310 (unsigned int) FD_SETSIZE);
324 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
328 "Error opening RAW socket: %s\n",
332 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
333 (char *)&one, sizeof(one)) == -1)
335 "setsockopt failed: %s\n",
337 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
338 (char *)&one, sizeof(one)) == -1)
340 "setsockopt failed: %s\n",
347 main (int argc, char *const *argv)
349 struct in_addr external;
354 if (-1 == (icmpsock = make_icmp_socket()))
356 if (-1 == (rawsock = make_raw_socket()))
362 if (0 != setresuid (uid, uid, uid))
364 "Failed to setresuid: %s\n",
369 "This program must be started with our (internal NAT) IP as the only argument.\n");
372 if (1 != inet_pton (AF_INET, argv[1], &external))
375 "Error parsing IPv4 address: %s\n",
379 if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy)) abort ();
383 FD_SET (icmpsock, &rs);
385 tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
386 select (icmpsock + 1, &rs, NULL, NULL, &tv);
387 if (FD_ISSET (icmpsock, &rs))
388 process_icmp_response ();
389 send_icmp_echo (&external);
395 /* end of gnunet-nat-server.c */