c5c6b563ee89c0ebd7dea8646e2669cd6dd40a27
[oweals/gnunet.git] / src / nat / gnunet-helper-nat-server.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @file src/nat/gnunet-helper-nat-server.c
21  * @brief Tool to help bypass NATs using ICMP method; must run as root (SUID will do)
22  *        This code will work under GNU/Linux only (or maybe BSDs, but never W32)
23  * @author Christian Grothoff
24  *
25  * This program will send ONE ICMP message every 500 ms RAW sockets
26  * to a DUMMY IP address and also listens for ICMP replies.  Since
27  * it uses RAW sockets, it must be installed SUID or run as 'root'.
28  * In order to keep the security risk of the resulting SUID binary
29  * minimal, the program ONLY opens the two RAW sockets with root
30  * privileges, then drops them and only then starts to process
31  * command line arguments.  The code also does not link against
32  * any shared libraries (except libc) and is strictly minimal
33  * (except for checking for errors).  The following list of people
34  * have reviewed this code and considered it safe since the last
35  * modification (if you reviewed it, please have your name added
36  * to the list):
37  *
38  * - Christian Grothoff
39  * - Nathan Evans
40  * - Benjamin Kuperman (22 Aug 2010)
41  * - Jacob Appelbaum (19 Dec 2011)
42  */
43 #if HAVE_CONFIG_H
44 /* Just needed for HAVE_SOCKADDR_IN_SIN_LEN test macro! */
45 #include "gnunet_config.h"
46 #else
47 #define _GNU_SOURCE
48 #endif
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <arpa/inet.h>
52 #include <sys/select.h>
53 #include <sys/time.h>
54 #include <sys/types.h>
55 #include <unistd.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <errno.h>
59 #include <stdlib.h>
60 #include <stdint.h>
61 #include <time.h>
62 #include <netinet/ip.h>
63 #include <netinet/ip_icmp.h>
64 #include <netinet/in.h>
65
66 /* The following constant is missing from FreeBSD 9.2 */
67 #ifndef ICMP_TIME_EXCEEDED
68 #define ICMP_TIME_EXCEEDED 11
69 #endif
70
71 /**
72  * Call memcpy() but check for @a n being 0 first. In the latter
73  * case, it is now safe to pass NULL for @a src or @a dst.
74  * Unlike traditional memcpy(), returns nothing.
75  *
76  * @param dst destination of the copy, may be NULL if @a n is zero
77  * @param src source of the copy, may be NULL if @a n is zero
78  * @param n number of bytes to copy
79  */
80 #define GNUNET_memcpy(dst,src,n) do { if (0 != n) { (void) memcpy (dst,src,n); } } while (0)
81
82 /**
83  * Should we print some debug output?
84  */
85 #define VERBOSE 0
86
87 /**
88  * Must match packet ID used by gnunet-helper-nat-client.c
89  */
90 #define PACKET_ID 256
91
92 /**
93  * Must match IP given in the client.
94  */
95 #define DUMMY_IP "192.0.2.86"
96
97 /**
98  * Port for UDP
99  */
100 #define NAT_TRAV_PORT 22225
101
102 /**
103  * How often do we send our ICMP messages to receive replies?
104  */
105 #define ICMP_SEND_FREQUENCY_MS 500
106
107 /**
108  * IPv4 header.
109  */
110 struct ip_header
111 {
112
113   /**
114    * Version (4 bits) + Internet header length (4 bits)
115    */
116   uint8_t vers_ihl;
117
118   /**
119    * Type of service
120    */
121   uint8_t tos;
122
123   /**
124    * Total length
125    */
126   uint16_t pkt_len;
127
128   /**
129    * Identification
130    */
131   uint16_t id;
132
133   /**
134    * Flags (3 bits) + Fragment offset (13 bits)
135    */
136   uint16_t flags_frag_offset;
137
138   /**
139    * Time to live
140    */
141   uint8_t ttl;
142
143   /**
144    * Protocol
145    */
146   uint8_t proto;
147
148   /**
149    * Header checksum
150    */
151   uint16_t checksum;
152
153   /**
154    * Source address
155    */
156   uint32_t src_ip;
157
158   /**
159    * Destination address
160    */
161   uint32_t dst_ip;
162 };
163
164 /**
165  * Format of ICMP packet.
166  */
167 struct icmp_ttl_exceeded_header
168 {
169   uint8_t type;
170
171   uint8_t code;
172
173   uint16_t checksum;
174
175   uint32_t unused;
176
177   /* followed by original payload */
178 };
179
180 struct icmp_echo_header
181 {
182   uint8_t type;
183
184   uint8_t code;
185
186   uint16_t checksum;
187
188   uint32_t reserved;
189 };
190
191
192 /**
193  * Beginning of UDP packet.
194  */
195 struct udp_header
196 {
197   uint16_t src_port;
198
199   uint16_t dst_port;
200
201   uint16_t length;
202
203   uint16_t crc;
204 };
205
206 /**
207  * Socket we use to receive "fake" ICMP replies.
208  */
209 static int icmpsock;
210
211 /**
212  * Socket we use to send our ICMP requests.
213  */
214 static int rawsock;
215
216 /**
217  * Socket we use to send our UDP requests.
218  */
219 static int udpsock;
220
221 /**
222  * Target "dummy" address.
223  */
224 static struct in_addr dummy;
225
226
227 /**
228  * CRC-16 for IP/ICMP headers.
229  *
230  * @param data what to calculate the CRC over
231  * @param bytes number of bytes in data (must be multiple of 2)
232  * @return the CRC 16.
233  */
234 static uint16_t
235 calc_checksum (const uint16_t * data, unsigned int bytes)
236 {
237   uint32_t sum;
238   unsigned int i;
239
240   sum = 0;
241   for (i = 0; i < bytes / 2; i++)
242     sum += data[i];
243   sum = (sum & 0xffff) + (sum >> 16);
244   sum = htons (0xffff - sum);
245   return sum;
246 }
247
248
249 /**
250  * Send an ICMP message to the dummy IP.
251  *
252  * @param my_ip source address (our ip address)
253  */
254 static void
255 send_icmp_echo (const struct in_addr *my_ip)
256 {
257   char packet[sizeof (struct ip_header) + sizeof (struct icmp_echo_header)];
258   struct icmp_echo_header icmp_echo;
259   struct ip_header ip_pkt;
260   struct sockaddr_in dst;
261   size_t off;
262   int err;
263
264   off = 0;
265   ip_pkt.vers_ihl = 0x45;
266   ip_pkt.tos = 0;
267   ip_pkt.pkt_len = htons (sizeof (packet));
268   ip_pkt.id = htons (PACKET_ID);
269   ip_pkt.flags_frag_offset = 0;
270   ip_pkt.ttl = IPDEFTTL;
271   ip_pkt.proto = IPPROTO_ICMP;
272   ip_pkt.checksum = 0;
273   ip_pkt.src_ip = my_ip->s_addr;
274   ip_pkt.dst_ip = dummy.s_addr;
275   ip_pkt.checksum =
276       htons (calc_checksum ((uint16_t *) & ip_pkt,
277                             sizeof (struct ip_header)));
278   GNUNET_memcpy (&packet[off],
279                  &ip_pkt,
280                  sizeof (struct ip_header));
281   off += sizeof (struct ip_header);
282
283   icmp_echo.type = ICMP_ECHO;
284   icmp_echo.code = 0;
285   icmp_echo.checksum = 0;
286   icmp_echo.reserved = 0;
287   icmp_echo.checksum =
288     htons (calc_checksum
289            ((uint16_t *) & icmp_echo,
290             sizeof (struct icmp_echo_header)));
291   GNUNET_memcpy (&packet[off],
292                  &icmp_echo,
293                  sizeof (struct icmp_echo_header));
294   off += sizeof (struct icmp_echo_header);
295
296   memset (&dst, 0, sizeof (dst));
297   dst.sin_family = AF_INET;
298 #if HAVE_SOCKADDR_IN_SIN_LEN
299   dst.sin_len = sizeof (struct sockaddr_in);
300 #endif
301   dst.sin_addr = dummy;
302   err = sendto (rawsock,
303                 packet,
304                 off,
305                 0,
306                 (struct sockaddr *) &dst,
307                 sizeof (dst));
308   if (err < 0)
309   {
310 #if VERBOSE
311     fprintf (stderr,
312              "sendto failed: %s\n",
313              strerror (errno));
314 #endif
315   }
316   else if (sizeof (packet) != err)
317   {
318     fprintf (stderr,
319              "Error: partial send of ICMP message\n");
320   }
321 }
322
323
324 /**
325  * Send a UDP message to the dummy IP.
326  */
327 static void
328 send_udp ()
329 {
330   struct sockaddr_in dst;
331   ssize_t err;
332
333   memset (&dst, 0, sizeof (dst));
334   dst.sin_family = AF_INET;
335 #if HAVE_SOCKADDR_IN_SIN_LEN
336   dst.sin_len = sizeof (struct sockaddr_in);
337 #endif
338   dst.sin_addr = dummy;
339   dst.sin_port = htons (NAT_TRAV_PORT);
340   err = sendto (udpsock,
341                 NULL,
342                 0,
343                 0,
344                 (struct sockaddr *) &dst,
345                 sizeof (dst));
346   if (err < 0)
347   {
348 #if VERBOSE
349     fprintf (stderr,
350              "sendto failed: %s\n",
351              strerror (errno));
352 #endif
353   }
354   else if (0 != err)
355   {
356     fprintf (stderr,
357              "Error: partial send of ICMP message\n");
358   }
359 }
360
361
362 /**
363  * We've received an ICMP response.  Process it.
364  */
365 static void
366 process_icmp_response ()
367 {
368   char buf[65536];
369   ssize_t have;
370   struct in_addr source_ip;
371   struct ip_header ip_pkt;
372   struct icmp_ttl_exceeded_header icmp_ttl;
373   struct icmp_echo_header icmp_echo;
374   struct udp_header udp_pkt;
375   size_t off;
376   uint16_t port;
377
378   have = read (icmpsock, buf, sizeof (buf));
379   if (-1 == have)
380   {
381     fprintf (stderr,
382              "Error reading raw socket: %s\n",
383              strerror (errno));
384     return;
385   }
386 #if VERBOSE
387   fprintf (stderr,
388            "Received message of %u bytes\n",
389            (unsigned int) have);
390 #endif
391   if (have <
392       (ssize_t) (sizeof (struct ip_header) +
393                  sizeof (struct icmp_ttl_exceeded_header) +
394                  sizeof (struct ip_header)))
395   {
396     /* malformed */
397     return;
398   }
399   off = 0;
400   GNUNET_memcpy (&ip_pkt,
401                  &buf[off],
402                  sizeof (struct ip_header));
403   off += sizeof (struct ip_header);
404   GNUNET_memcpy (&icmp_ttl,
405                  &buf[off],
406                  sizeof (struct icmp_ttl_exceeded_header));
407   off += sizeof (struct icmp_ttl_exceeded_header);
408   if ((ICMP_TIME_EXCEEDED != icmp_ttl.type) || (0 != icmp_ttl.code))
409   {
410     /* different type than what we want */
411     return;
412   }
413   /* grab source IP of 1st IP header */
414   source_ip.s_addr = ip_pkt.src_ip;
415
416   /* skip 2nd IP header */
417   GNUNET_memcpy (&ip_pkt,
418                  &buf[off],
419                  sizeof (struct ip_header));
420   off += sizeof (struct ip_header);
421
422   switch (ip_pkt.proto)
423   {
424   case IPPROTO_ICMP:
425     if (have !=
426         (sizeof (struct ip_header) * 2 +
427          sizeof (struct icmp_ttl_exceeded_header) +
428          sizeof (struct icmp_echo_header)))
429     {
430       /* malformed */
431       return;
432     }
433     /* grab ICMP ECHO content */
434     GNUNET_memcpy (&icmp_echo,
435                    &buf[off],
436                    sizeof (struct icmp_echo_header));
437     port = (uint16_t) ntohl (icmp_echo.reserved);
438     break;
439   case IPPROTO_UDP:
440     if (have !=
441         (sizeof (struct ip_header) * 2 +
442          sizeof (struct icmp_ttl_exceeded_header) + sizeof (struct udp_header)))
443     {
444       /* malformed */
445       return;
446     }
447     /* grab UDP content */
448     GNUNET_memcpy (&udp_pkt,
449                    &buf[off],
450                    sizeof (struct udp_header));
451     port = ntohs (udp_pkt.length);
452     break;
453   default:
454     /* different type than what we want */
455     return;
456   }
457
458   if (port == 0)
459     fprintf (stdout, "%s\n",
460              inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)));
461   else
462     fprintf (stdout, "%s:%u\n",
463              inet_ntop (AF_INET, &source_ip, buf, sizeof (buf)),
464              (unsigned int) port);
465   fflush (stdout);
466 }
467
468
469 /**
470  * Fully initialize the raw socket.
471  *
472  * @return -1 on error, 0 on success
473  */
474 static int
475 setup_raw_socket ()
476 {
477   const int one = 1;
478
479   if (-1 ==
480       setsockopt (rawsock,
481                   SOL_SOCKET,
482                   SO_BROADCAST,
483                   (char *) &one,
484                   sizeof (one)))
485   {
486     fprintf (stderr,
487              "setsockopt failed: %s\n",
488              strerror (errno));
489     return -1;
490   }
491   if (-1 ==
492       setsockopt (rawsock,
493                   IPPROTO_IP,
494                   IP_HDRINCL,
495                   (char *) &one,
496                   sizeof (one)))
497   {
498     fprintf (stderr,
499              "setsockopt failed: %s\n",
500              strerror (errno));
501     return -1;
502   }
503   return 0;
504 }
505
506
507 /**
508  * Create a UDP socket for writing.
509  *
510  * @param my_ip source address (our ip address)
511  * @return -1 on error
512  */
513 static int
514 make_udp_socket (const struct in_addr *my_ip)
515 {
516   int ret;
517   struct sockaddr_in addr;
518
519   ret = socket (AF_INET, SOCK_DGRAM, 0);
520   if (-1 == ret)
521   {
522     fprintf (stderr,
523              "Error opening UDP socket: %s\n",
524              strerror (errno));
525     return -1;
526   }
527   memset (&addr, 0, sizeof (addr));
528   addr.sin_family = AF_INET;
529 #if HAVE_SOCKADDR_IN_SIN_LEN
530   addr.sin_len = sizeof (struct sockaddr_in);
531 #endif
532   addr.sin_addr = *my_ip;
533   addr.sin_port = htons (NAT_TRAV_PORT);
534
535   if (0 != bind (ret,
536                  (struct sockaddr *) &addr,
537                  sizeof (addr)))
538   {
539     fprintf (stderr,
540              "Error binding UDP socket to port %u: %s\n",
541              NAT_TRAV_PORT,
542              strerror (errno));
543     (void) close (ret);
544     return -1;
545   }
546   return ret;
547 }
548
549
550 int
551 main (int argc,
552       char *const *argv)
553 {
554   struct in_addr external;
555   fd_set rs;
556   struct timeval tv;
557   uid_t uid;
558   unsigned int alt;
559   int icmp_eno;
560   int raw_eno;
561   int global_ret;
562
563   /* Create an ICMP raw socket for reading (we'll check errors later) */
564   icmpsock = socket (AF_INET,
565                      SOCK_RAW,
566                      IPPROTO_ICMP);
567   icmp_eno = errno;
568
569   /* Create an (ICMP) raw socket for writing (we'll check errors later) */
570   rawsock = socket (AF_INET,
571                     SOCK_RAW,
572                     IPPROTO_RAW);
573   raw_eno = errno;
574   udpsock = -1;
575
576   /* drop root rights */
577   uid = getuid ();
578 #ifdef HAVE_SETRESUID
579   if (0 != setresuid (uid, uid, uid))
580   {
581     fprintf (stderr,
582              "Failed to setresuid: %s\n",
583              strerror (errno));
584     global_ret = 1;
585     goto error_exit;
586   }
587 #else
588   if (0 != (setuid (uid) | seteuid (uid)))
589   {
590     fprintf (stderr,
591              "Failed to setuid: %s\n",
592              strerror (errno));
593     global_ret = 2;
594     goto error_exit;
595   }
596 #endif
597
598   /* Now that we run without root rights, we can do error checking... */
599   if (2 != argc)
600   {
601     fprintf (stderr,
602              "This program must be started with our (internal NAT) IP as the only argument.\n");
603     global_ret = 3;
604     goto error_exit;
605   }
606   if (1 != inet_pton (AF_INET, argv[1], &external))
607   {
608     fprintf (stderr,
609              "Error parsing IPv4 address: %s\n",
610              strerror (errno));
611     global_ret = 4;
612     goto error_exit;
613   }
614   if (1 != inet_pton (AF_INET, DUMMY_IP, &dummy))
615   {
616     fprintf (stderr,
617              "Internal error converting dummy IP to binary.\n");
618     global_ret = 5;
619     goto error_exit;
620   }
621
622   /* error checking icmpsock */
623   if (-1 == icmpsock)
624   {
625     fprintf (stderr,
626              "Error opening RAW socket: %s\n",
627              strerror (icmp_eno));
628     global_ret = 6;
629     goto error_exit;
630   }
631   if (icmpsock >= FD_SETSIZE)
632   {
633     /* this could happen if we were started with a large number of already-open
634        file descriptors... */
635     fprintf (stderr,
636              "Socket number too large (%d > %u)\n",
637              icmpsock,
638              (unsigned int) FD_SETSIZE);
639     global_ret = 7;
640     goto error_exit;
641   }
642
643   /* error checking rawsock */
644   if (-1 == rawsock)
645   {
646     fprintf (stderr,
647              "Error opening RAW socket: %s\n",
648              strerror (raw_eno));
649     global_ret = 8;
650     goto error_exit;
651   }
652   /* no need to check 'rawsock' against FD_SETSIZE as it is never used
653      with 'select' */
654
655   if (0 != setup_raw_socket ())
656   {
657     global_ret = 9;
658     goto error_exit;
659   }
660
661   if (-1 == (udpsock = make_udp_socket (&external)))
662   {
663     global_ret = 10;
664     goto error_exit;
665   }
666
667   alt = 0;
668   while (1)
669   {
670     FD_ZERO (&rs);
671     FD_SET (icmpsock, &rs);
672     tv.tv_sec = 0;
673     tv.tv_usec = ICMP_SEND_FREQUENCY_MS * 1000;
674     if (-1 == select (icmpsock + 1, &rs, NULL, NULL, &tv))
675     {
676       if (errno == EINTR)
677         continue;
678       fprintf (stderr,
679                "select failed: %s\n",
680                strerror (errno));
681       break;
682     }
683     if (1 == getppid ())        /* Check the parent process id, if 1 the parent has died, so we should die too */
684       break;
685     if (FD_ISSET (icmpsock, &rs))
686     {
687       process_icmp_response ();
688       continue;
689     }
690     if (0 == (++alt % 2))
691       send_icmp_echo (&external);
692     else
693       send_udp ();
694   }
695
696   /* select failed (internal error or OS out of resources) */
697   global_ret = 11;
698 error_exit:
699   if (-1 != icmpsock)
700     (void) close (icmpsock);
701   if (-1 != rawsock)
702     (void) close (rawsock);
703   if (-1 != udpsock)
704     (void) close (udpsock);
705   return global_ret;
706 }
707
708
709 /* end of gnunet-helper-nat-server.c */