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 * priviledges, 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"
68 uint16_t flags_frag_offset;
86 static struct in_addr dummy;
88 static struct in_addr target;
92 calc_checksum(const uint16_t *data,
99 for (i=0;i<bytes/2;i++)
101 sum = (sum & 0xffff) + (sum >> 16);
102 sum = htons(0xffff - sum);
108 make_echo (const struct in_addr *src_ip,
109 struct icmp_packet *echo)
111 memset(echo, 0, sizeof(struct icmp_packet));
112 echo->type = ICMP_ECHO;
116 echo->checksum = htons(calc_checksum((uint16_t*)echo,
117 sizeof (struct icmp_packet)));
122 * Send an ICMP message to the target.
124 * @param my_ip source address
125 * @param other target address
128 send_icmp (const struct in_addr *my_ip,
129 const struct in_addr *other)
131 struct ip_packet ip_pkt;
132 struct icmp_packet *icmp_pkt;
133 struct icmp_packet icmp_echo;
134 struct sockaddr_in dst;
135 char packet[sizeof (struct ip_packet)*2 + sizeof (struct icmp_packet)*2];
139 /* ip header: send to (known) ip address */
141 memset(&ip_pkt, 0, sizeof(ip_pkt));
142 ip_pkt.vers_ihl = 0x45;
144 ip_pkt.pkt_len = sizeof (packet); /* huh? */
146 ip_pkt.flags_frag_offset = 0;
147 ip_pkt.ttl = IPDEFTTL;
148 ip_pkt.proto = IPPROTO_ICMP;
150 ip_pkt.src_ip = my_ip->s_addr;
151 ip_pkt.dst_ip = other->s_addr;
152 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
153 memcpy (packet, &ip_pkt, sizeof (struct ip_packet));
154 off += sizeof (ip_pkt);
155 /* icmp reply: time exceeded */
156 icmp_pkt = (struct icmp_packet*) &packet[off];
157 memset(icmp_pkt, 0, sizeof(struct icmp_packet));
158 icmp_pkt->type = ICMP_TIME_EXCEEDED;
160 icmp_pkt->reserved = 0;
161 icmp_pkt->checksum = 0;
162 off += sizeof (struct icmp_packet);
164 /* ip header of the presumably 'lost' udp packet */
165 ip_pkt.vers_ihl = 0x45;
167 ip_pkt.pkt_len = (sizeof (struct ip_packet) + sizeof (struct icmp_packet));
169 ip_pkt.flags_frag_offset = 0;
170 ip_pkt.ttl = 1; /* real TTL would be 1 on a time exceeded packet */
171 ip_pkt.proto = IPPROTO_ICMP;
172 ip_pkt.src_ip = other->s_addr;
173 ip_pkt.dst_ip = dummy.s_addr;
175 ip_pkt.checksum = htons(calc_checksum((uint16_t*)&ip_pkt, sizeof (struct ip_packet)));
176 memcpy (&packet[off], &ip_pkt, sizeof (struct ip_packet));
177 off += sizeof (struct ip_packet);
178 make_echo (other, &icmp_echo);
179 memcpy (&packet[off], &icmp_echo, sizeof(struct icmp_packet));
180 off += sizeof (struct icmp_packet);
181 icmp_pkt->checksum = htons(calc_checksum((uint16_t*)icmp_pkt,
182 sizeof (struct icmp_packet)*2 + sizeof(struct ip_packet)));
184 memset (&dst, 0, sizeof (dst));
185 dst.sin_family = AF_INET;
186 dst.sin_addr = *other;
187 err = sendto(rawsock,
190 (struct sockaddr*)&dst,
191 sizeof(dst)); /* or sizeof 'struct sockaddr'? */
195 "sendto failed: %s\n", strerror(errno));
200 "Error: partial send of ICMP message\n");
211 ret = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
215 "Error opening RAW socket: %s\n",
219 if (setsockopt(ret, SOL_SOCKET, SO_BROADCAST,
220 (char *)&one, sizeof(one)) == -1)
222 "setsockopt failed: %s\n",
224 if (setsockopt(ret, IPPROTO_IP, IP_HDRINCL,
225 (char *)&one, sizeof(one)) == -1)
227 "setsockopt failed: %s\n",
234 main (int argc, char *const *argv)
236 struct in_addr external;
239 if (-1 == (rawsock = make_raw_socket()))
242 if (0 != setresuid (uid, uid, uid))
244 "Failed to setresuid: %s\n",
249 "This program must be started with our IP and the targets external IP as arguments.\n");
252 if ( (1 != inet_pton (AF_INET, argv[1], &external)) ||
253 (1 != inet_pton (AF_INET, argv[2], &target)) )
256 "Error parsing IPv4 address: %s\n",
260 inet_pton (AF_INET, DUMMY_IP, &dummy);
261 send_icmp (&external,
267 /* end of gnunet-nat-client.c */