-fixing generation of responses from exit
[oweals/gnunet.git] / src / exit / gnunet-daemon-exit.c
index bfa600e487a07f9627bb0c38fc54882d63d8b29f..d6f0dadadd25be4684f046dc7ec800fb01dc3705 100644 (file)
  * @author Christian Grothoff
  *
  * TODO:
- * - need some statistics
  * - test
  *
- * Code cleanup:
- * - factor out crc computations from DNS/EXIT/VPN into shared library?
- *
  * Design:
  * - which code should advertise services? the service model is right
  *   now a bit odd, especially as this code DOES the exit and knows
@@ -45,7 +41,7 @@
 #include "gnunet_mesh_service.h"
 #include "gnunet_statistics_service.h"
 #include "gnunet_constants.h"
-#include "tcpip_tun.h"
+#include "gnunet_tun_lib.h"
 #include "exit.h"
 
 /**
@@ -238,11 +234,27 @@ static struct GNUNET_HELPER_Handle *helper_handle;
  */
 static char *exit_argv[7];
 
+/**
+ * IPv6 address of our TUN interface.
+ */
+static struct in6_addr exit_ipv6addr;
+
 /**
  * IPv6 prefix (0..127) from configuration file.
  */
 static unsigned long long ipv6prefix;
 
+/**
+ * IPv4 address of our TUN interface.
+ */
+static struct in_addr exit_ipv4addr;
+
+/**
+ * IPv4 netmask of our TUN interface.
+ */
+static struct in_addr exit_ipv4mask;
+
+
 /**
  * Statistics.
  */
@@ -267,7 +279,7 @@ static struct GNUNET_CONTAINER_Heap *connections_heap;
 /**
  * If there are at least this many connections, old ones will be removed
  */
-static long long unsigned int max_connections = 200;
+static long long unsigned int max_connections;
 
 /**
  * This hashmaps saves interesting things about the configured UDP services
@@ -521,6 +533,9 @@ send_to_peer_notify_callback (void *cls, size_t size, void *buf)
                                               tnq->len,
                                               &send_to_peer_notify_callback,
                                               s);
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# Bytes transmitted via mesh tunnels"),
+                           size, GNUNET_NO);
   return size;
 }
 
@@ -529,53 +544,21 @@ send_to_peer_notify_callback (void *cls, size_t size, void *buf)
  * Send the given packet via the mesh tunnel.
  *
  * @param mesh_tunnel destination
- * @param payload message to transmit
- * @param payload_length number of bytes in payload
- * @param desc descriptor to add before payload (optional)
- * @param mtype message type to use
+ * @param tnq message to queue
  */
 static void
 send_packet_to_mesh_tunnel (struct GNUNET_MESH_Tunnel *mesh_tunnel,
-                           const void *payload,
-                           size_t payload_length,
-                           const GNUNET_HashCode *desc,
-                           uint16_t mtype)
+                           struct TunnelMessageQueue *tnq)
 {
   struct TunnelState *s;
-  struct TunnelMessageQueue *tnq;
-  struct GNUNET_MessageHeader *msg;
-  size_t len;
-  GNUNET_HashCode *dp;
 
-  len = sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + payload_length;
-  if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
-  {
-    GNUNET_break (0);
-    return;
-  }
-  tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + len);
-  tnq->payload = &tnq[1];
-  tnq->len = len;
-  msg = (struct GNUNET_MessageHeader *) &tnq[1];
-  msg->size = htons ((uint16_t) len);
-  msg->type = htons (mtype);
-  if (NULL != desc)
-  {
-    dp = (GNUNET_HashCode *) &msg[1];
-    *dp = *desc;  
-    memcpy (&dp[1], payload, payload_length);
-  }
-  else
-  {
-    memcpy (&msg[1], payload, payload_length);
-  }
   s = GNUNET_MESH_tunnel_get_data (mesh_tunnel);
   GNUNET_assert (NULL != s);
   GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, tnq);
   if (NULL == s->th)
     s->th = GNUNET_MESH_notify_transmit_ready (mesh_tunnel, GNUNET_NO /* cork */, 0 /* priority */,
                                               GNUNET_TIME_UNIT_FOREVER_REL,
-                                              NULL, len,
+                                              NULL, tnq->len,
                                               &send_to_peer_notify_callback,
                                               s);
 }
@@ -593,13 +576,16 @@ send_packet_to_mesh_tunnel (struct GNUNET_MESH_Tunnel *mesh_tunnel,
  *                       be the original destination address)
  */
 static void
-udp_from_helper (const struct udp_packet *udp, 
+udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp, 
                 size_t pktlen,
                 int af,
                 const void *destination_ip, 
                 const void *source_ip)
 {
   struct TunnelState *state;
+  struct TunnelMessageQueue *tnq;
+  struct GNUNET_EXIT_UdpReplyMessage *urm;
+  size_t mlen;
 
   {
     char sbuf[INET6_ADDRSTRLEN];
@@ -615,7 +601,7 @@ udp_from_helper (const struct udp_packet *udp,
                           dbuf, sizeof (dbuf)),
                (unsigned int) ntohs (udp->dpt));
   }
-  if (pktlen < sizeof (struct udp_packet))
+  if (pktlen < sizeof (struct GNUNET_TUN_UdpHeader))
   {
     /* blame kernel */
     GNUNET_break (0);
@@ -639,12 +625,20 @@ udp_from_helper (const struct udp_packet *udp,
                _("Packet dropped, have no matching connection information\n"));
     return;
   }
+  mlen = sizeof (struct GNUNET_EXIT_UdpReplyMessage) + pktlen - sizeof (struct GNUNET_TUN_UdpHeader);
+  tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);  
+  tnq->payload = &tnq[1];
+  tnq->len = mlen;
+  urm = (struct GNUNET_EXIT_UdpReplyMessage *) &tnq[1];
+  urm->header.size = htons ((uint16_t) mlen);
+  urm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_UDP_REPLY);
+  urm->source_port = htons (0);
+  urm->destination_port = htons (0);
+  memcpy (&urm[1],
+         &udp[1],
+         pktlen - sizeof (struct GNUNET_TUN_UdpHeader));
   send_packet_to_mesh_tunnel (state->tunnel,
-                             &udp[1], pktlen - sizeof (struct udp_packet),
-                             NULL,
-                             state->serv != NULL
-                             ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK 
-                             : GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
+                             tnq);
 }
 
 
@@ -652,7 +646,7 @@ udp_from_helper (const struct udp_packet *udp,
  * @brief Handles a TCP packet received from the helper.
  *
  * @param tcp A pointer to the Packet
- * @param pktlen the length of the packet, including its header
+ * @param pktlen the length of the packet, including its TCP header
  * @param af address family (AFINET or AF_INET6)
  * @param destination_ip destination IP-address of the IP packet (should 
  *                       be our local address)
@@ -660,7 +654,7 @@ udp_from_helper (const struct udp_packet *udp,
  *                       be the original destination address)
  */
 static void
-tcp_from_helper (const struct tcp_packet *tcp, 
+tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp, 
                 size_t pktlen,
                 int af,
                 const void *destination_ip,
@@ -668,7 +662,10 @@ tcp_from_helper (const struct tcp_packet *tcp,
 {
   struct TunnelState *state;
   char buf[pktlen];
-  struct tcp_packet *mtcp;
+  struct GNUNET_TUN_TcpHeader *mtcp;
+  struct GNUNET_EXIT_TcpDataMessage *tdm;
+  struct TunnelMessageQueue *tnq;
+  size_t mlen;
 
   {
     char sbuf[INET6_ADDRSTRLEN];
@@ -684,7 +681,7 @@ tcp_from_helper (const struct tcp_packet *tcp,
                           dbuf, sizeof (dbuf)),
                (unsigned int) ntohs (tcp->dpt));
   }
-  if (pktlen < sizeof (struct tcp_packet))
+  if (pktlen < sizeof (struct GNUNET_TUN_TcpHeader))
   {
     /* blame kernel */
     GNUNET_break (0);
@@ -706,16 +703,30 @@ tcp_from_helper (const struct tcp_packet *tcp,
   /* mug port numbers and crc to avoid information leakage;
      sender will need to lookup the correct values anyway */
   memcpy (buf, tcp, pktlen);  
-  mtcp = (struct tcp_packet *) buf;
+  mtcp = (struct GNUNET_TUN_TcpHeader *) buf;
   mtcp->spt = 0;
   mtcp->dpt = 0;
   mtcp->crc = 0;
+
+  mlen = sizeof (struct GNUNET_EXIT_TcpDataMessage) + (pktlen - sizeof (struct GNUNET_TUN_TcpHeader));
+  if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+  {
+    GNUNET_break (0);
+    return;
+  }
+
+  tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);
+  tnq->payload = &tnq[1];
+  tnq->len = mlen;
+  tdm = (struct GNUNET_EXIT_TcpDataMessage *) &tnq[1];
+  tdm->header.size = htons ((uint16_t) mlen);
+  tdm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_DATA);
+  tdm->reserved = htonl (0);
+  memcpy (&tdm->tcp_header,
+         buf, 
+         pktlen);
   send_packet_to_mesh_tunnel (state->tunnel,
-                             mtcp, pktlen,
-                             NULL,
-                             state->serv != NULL
-                             ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK 
-                             : GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
+                             tnq);
 }
 
 
@@ -730,52 +741,58 @@ static void
 message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
                const struct GNUNET_MessageHeader *message)
 {
-  const struct tun_header *pkt_tun;
+  const struct GNUNET_TUN_Layer2PacketHeader *pkt_tun;
   size_t size;
 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# Packets received from TUN"),
+                           1, GNUNET_NO);
   if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
   {
     GNUNET_break (0);
     return;
   }
   size = ntohs (message->size);
-  if (size < sizeof (struct tun_header) + sizeof (struct GNUNET_MessageHeader))
+  if (size < sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader))
   {
     GNUNET_break (0);
     return;
   }
-  pkt_tun = (const struct tun_header *) &message[1];
-  size -= sizeof (struct tun_header) + sizeof (struct GNUNET_MessageHeader);
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# Bytes received from TUN"),
+                           size, GNUNET_NO);
+  pkt_tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
+  size -= sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader);
   switch (ntohs (pkt_tun->proto))
   {
   case ETH_P_IPV6:
     {
-      const struct ip6_header *pkt6;
+      const struct GNUNET_TUN_IPv6Header *pkt6;
 
-      if (size < sizeof (struct ip6_header))
+      if (size < sizeof (struct GNUNET_TUN_IPv6Header))
       {
        /* Kernel to blame? */
        GNUNET_break (0);
        return;
       }
-      pkt6 = (struct ip6_header *) &pkt_tun[1];
+      pkt6 = (struct GNUNET_TUN_IPv6Header *) &pkt_tun[1];
       if (size != ntohs (pkt6->payload_length))
       {
        /* Kernel to blame? */
        GNUNET_break (0);
        return;
       }
-      size -= sizeof (struct ip6_header);
+      size -= sizeof (struct GNUNET_TUN_IPv6Header);
       switch (pkt6->next_header)
       {
       case IPPROTO_UDP:
-       udp_from_helper ((const struct udp_packet *) &pkt6[1], size,
+       udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt6[1], size,
                         AF_INET6,
                         &pkt6->destination_address, 
                         &pkt6->source_address);
        break;
       case IPPROTO_TCP:
-       tcp_from_helper ((const struct tcp_packet *) &pkt6[1], size,
+       tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt6[1], size,
                         AF_INET6,
                         &pkt6->destination_address, 
                         &pkt6->source_address);
@@ -789,37 +806,37 @@ message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
     break;
   case ETH_P_IPV4:
     {
-      const struct ip4_header *pkt4;
+      const struct GNUNET_TUN_IPv4Header *pkt4;
 
-      if (size < sizeof (struct ip4_header))
+      if (size < sizeof (struct GNUNET_TUN_IPv4Header))
       {
        /* Kernel to blame? */
        GNUNET_break (0);
        return;
       }
-      pkt4 = (const struct ip4_header *) &pkt_tun[1];
+      pkt4 = (const struct GNUNET_TUN_IPv4Header *) &pkt_tun[1];
       if (size != ntohs (pkt4->total_length))
       {
        /* Kernel to blame? */
        GNUNET_break (0);
        return;
       }
-      if (pkt4->header_length * 4 != sizeof (struct ip4_header))
+      if (pkt4->header_length * 4 != sizeof (struct GNUNET_TUN_IPv4Header))
       {
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    _("IPv4 packet options received.  Ignored.\n"));
        return;
       }
-      size -= sizeof (struct ip4_header);
+      size -= sizeof (struct GNUNET_TUN_IPv4Header);
       switch (pkt4->protocol)
       {
       case IPPROTO_UDP:
-       udp_from_helper ((const struct udp_packet *) &pkt4[1], size,
+       udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt4[1], size,
                         AF_INET,
                         &pkt4->destination_address, 
                         &pkt4->source_address);
       case IPPROTO_TCP:
-       tcp_from_helper ((const struct tcp_packet *) &pkt4[1], size,
+       tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt4[1], size,
                         AF_INET,
                         &pkt4->destination_address, 
                         &pkt4->source_address);
@@ -864,14 +881,12 @@ setup_fresh_address (int af,
   {
   case AF_INET:
     {
-      const char *ipv4addr = exit_argv[4];
-      const char *ipv4mask = exit_argv[5];
       struct in_addr addr;
       struct in_addr mask;
       struct in_addr rnd;
 
-      GNUNET_assert (1 == inet_pton (AF_INET, ipv4addr, &addr));
-      GNUNET_assert (1 == inet_pton (AF_INET, ipv4mask, &mask));           
+      addr = exit_ipv4addr;
+      mask = exit_ipv4mask;
       if (0 == ~mask.s_addr)
       {
        /* only one valid IP anyway */
@@ -894,13 +909,12 @@ setup_fresh_address (int af,
     break;
   case AF_INET6:
     {
-      const char *ipv6addr = exit_argv[2];
       struct in6_addr addr;
       struct in6_addr mask;
       struct in6_addr rnd;
       int i;
-
-      GNUNET_assert (1 == inet_pton (AF_INET6, ipv6addr, &addr));
+      
+      addr = exit_ipv6addr;
       GNUNET_assert (ipv6prefix < 128);
       if (ipv6prefix == 127)
       {
@@ -969,9 +983,14 @@ setup_state_record (struct TunnelState *state)
   /* generate fresh, unique address */
   do
   {
-    setup_fresh_address (state->serv->address.af,
-                        state->serv->address.proto,
-                        &state->ri.local_address);
+    if (NULL == state->serv)
+      setup_fresh_address (state->ri.remote_address.af,
+                          state->ri.remote_address.proto,
+                          &state->ri.local_address);
+    else
+      setup_fresh_address (state->serv->address.af,
+                          state->serv->address.proto,
+                          &state->ri.local_address);
   } while (NULL != get_redirect_state (state->ri.remote_address.af,
                                       state->ri.remote_address.proto,
                                       &state->ri.remote_address.address,
@@ -988,6 +1007,7 @@ setup_state_record (struct TunnelState *state)
                           buf, sizeof (buf)),
                (unsigned int) state->ri.local_address.port);  
   }
+  state->state_key = key;
   GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONTAINER_multihashmap_put (connections_map, 
                                                    &key, state,
@@ -1030,10 +1050,10 @@ setup_state_record (struct TunnelState *state)
 static void
 prepare_ipv4_packet (const void *payload, size_t payload_length,
                     int protocol,
-                    const struct tcp_packet *tcp_header,
+                    const struct GNUNET_TUN_TcpHeader *tcp_header,
                     const struct SocketAddress *src_address,
                     const struct SocketAddress *dst_address,
-                    struct ip4_header *pkt4)
+                    struct GNUNET_TUN_IPv4Header *pkt4)
 {
   size_t len;
 
@@ -1041,67 +1061,54 @@ prepare_ipv4_packet (const void *payload, size_t payload_length,
   switch (protocol)
   {
   case IPPROTO_UDP:
-    len += sizeof (struct udp_packet);
+    len += sizeof (struct GNUNET_TUN_UdpHeader);
     break;
   case IPPROTO_TCP:
-    len += sizeof (struct tcp_packet);
+    len += sizeof (struct GNUNET_TUN_TcpHeader);
     GNUNET_assert (NULL != tcp_header);
     break;
   default:
     GNUNET_break (0);
     return;
   }
-  if (len + sizeof (struct ip4_header) > UINT16_MAX)
+  if (len + sizeof (struct GNUNET_TUN_IPv4Header) > UINT16_MAX)
   {
     GNUNET_break (0);
     return;
   }
 
-  pkt4->version = 4;
-  pkt4->header_length = sizeof (struct ip4_header) / 4;
-  pkt4->diff_serv = 0;
-  pkt4->total_length = htons ((uint16_t) (sizeof (struct ip4_header) + len));
-  pkt4->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 
-                                                             UINT16_MAX + 1);
-  pkt4->flags = 0;
-  pkt4->fragmentation_offset = 0;
-  pkt4->ttl = 255;
-  pkt4->protocol = protocol;
-  pkt4->checksum = 0;
-  pkt4->destination_address = dst_address->address.ipv4;
-  pkt4->source_address = src_address->address.ipv4;
-  pkt4->checksum = GNUNET_CRYPTO_crc16_n (pkt4, sizeof (struct ip4_header));
-
+  GNUNET_TUN_initialize_ipv4_header (pkt4,
+                                    protocol,
+                                    len,
+                                    &src_address->address.ipv4,
+                                    &dst_address->address.ipv4);
   switch (protocol)
   {
   case IPPROTO_UDP:
     {
-      struct udp_packet *pkt4_udp = (struct udp_packet *) &pkt4[1];
+      struct GNUNET_TUN_UdpHeader *pkt4_udp = (struct GNUNET_TUN_UdpHeader *) &pkt4[1];
 
       pkt4_udp->spt = htons (src_address->port);
       pkt4_udp->dpt = htons (dst_address->port);
-      pkt4_udp->crc = 0;  /* Optional for IPv4 */
       pkt4_udp->len = htons ((uint16_t) payload_length);
+      GNUNET_TUN_calculate_udp4_checksum (pkt4,
+                                         pkt4_udp,
+                                         payload, payload_length);
       memcpy (&pkt4_udp[1], payload, payload_length);
     }
     break;
   case IPPROTO_TCP:
     {
-      struct tcp_packet *pkt4_tcp = (struct tcp_packet *) &pkt4[1];
+      struct GNUNET_TUN_TcpHeader *pkt4_tcp = (struct GNUNET_TUN_TcpHeader *) &pkt4[1];
       
-      memcpy (pkt4_tcp, tcp_header, sizeof (struct tcp_packet));
-      memcpy (&pkt4_tcp[1], payload, payload_length);
+      memcpy (pkt4_tcp, tcp_header, sizeof (struct GNUNET_TUN_TcpHeader));
       pkt4_tcp->spt = htons (src_address->port);
       pkt4_tcp->dpt = htons (dst_address->port);
-      pkt4_tcp->crc = 0;
-      uint32_t sum = 0;
-      sum = GNUNET_CRYPTO_crc16_step (sum, 
-                                     &pkt4->source_address,
-                                     sizeof (struct in_addr) * 2);
-      uint32_t tmp = htonl ((protocol << 16) | (0xffff & len));
-      sum = GNUNET_CRYPTO_crc16_step (sum, & tmp, sizeof (uint32_t));
-      sum = GNUNET_CRYPTO_crc16_step (sum, & pkt4_tcp, len);
-      pkt4_tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
+      GNUNET_TUN_calculate_tcp4_checksum (pkt4,
+                                         pkt4_tcp,
+                                         payload,
+                                         payload_length);
+      memcpy (&pkt4_tcp[1], payload, payload_length);
     }
     break;
   default:
@@ -1130,10 +1137,10 @@ prepare_ipv4_packet (const void *payload, size_t payload_length,
 static void
 prepare_ipv6_packet (const void *payload, size_t payload_length,
                     int protocol,
-                    const struct tcp_packet *tcp_header,
+                    const struct GNUNET_TUN_TcpHeader *tcp_header,
                     const struct SocketAddress *src_address,
                     const struct SocketAddress *dst_address,
-                    struct ip6_header *pkt6)
+                    struct GNUNET_TUN_IPv6Header *pkt6)
 {
   size_t len;
 
@@ -1141,11 +1148,11 @@ prepare_ipv6_packet (const void *payload, size_t payload_length,
   switch (protocol)
   {
   case IPPROTO_UDP:
-    len += sizeof (struct udp_packet);
+    len += sizeof (struct GNUNET_TUN_UdpHeader);
     break;
   case IPPROTO_TCP:
     /* tcp_header (with port/crc not set) must be part of payload! */
-    if (len < sizeof (struct tcp_packet))
+    if (len < sizeof (struct GNUNET_TUN_TcpHeader))
     {
       GNUNET_break (0);
       return;
@@ -1161,55 +1168,41 @@ prepare_ipv6_packet (const void *payload, size_t payload_length,
     return;
   }
 
-  pkt6->version = 6;
-  pkt6->next_header = protocol;
-  pkt6->payload_length = htons ((uint16_t) (len + sizeof (struct ip6_header)));
-  pkt6->hop_limit = 255;
-  pkt6->destination_address = dst_address->address.ipv6;
-  pkt6->source_address = src_address->address.ipv6;
+  GNUNET_TUN_initialize_ipv6_header (pkt6,
+                                    protocol,
+                                    len,
+                                    &dst_address->address.ipv6,
+                                    &src_address->address.ipv6);
 
   switch (protocol)
   {
   case IPPROTO_UDP:
     {
-      struct udp_packet *pkt6_udp = (struct udp_packet *) &pkt6[1];
+      struct GNUNET_TUN_UdpHeader *pkt6_udp = (struct GNUNET_TUN_UdpHeader *) &pkt6[1];
 
-      memcpy (&pkt6[1], payload, payload_length);
-      pkt6_udp->crc = 0;
       pkt6_udp->spt = htons (src_address->port);
       pkt6_udp->dpt = htons (dst_address->port);
       pkt6_udp->len = htons ((uint16_t) payload_length);
-
-      uint32_t sum = 0;
-      sum = GNUNET_CRYPTO_crc16_step (sum,
-                                     &pkt6->source_address,
-                                     sizeof (struct in6_addr) * 2);
-      uint32_t tmp = htons (len);
-      sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
-      tmp = htonl (pkt6->next_header);
-      sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
-      sum = GNUNET_CRYPTO_crc16_step (sum, pkt6_udp, len);
-      pkt6_udp->crc = GNUNET_CRYPTO_crc16_finish (sum);
+      pkt6_udp->crc = 0;
+      GNUNET_TUN_calculate_udp6_checksum (pkt6,
+                                         pkt6_udp,
+                                         payload,
+                                         payload_length);
+      memcpy (&pkt6[1], payload, payload_length);
     }
     break;
   case IPPROTO_TCP:
     {
-      struct tcp_packet *pkt6_tcp = (struct tcp_packet *) pkt6;
-      
+      struct GNUNET_TUN_TcpHeader *pkt6_tcp = (struct GNUNET_TUN_TcpHeader *) pkt6;
+
+      /* memcpy first here as some TCP header fields are initialized this way! */
       memcpy (pkt6_tcp, payload, payload_length);
-      pkt6_tcp->crc = 0;
       pkt6_tcp->spt = htons (src_address->port);
       pkt6_tcp->dpt = htons (dst_address->port);
-
-      uint32_t sum = 0;
-      sum = GNUNET_CRYPTO_crc16_step (sum, &pkt6->source_address, 
-                                     sizeof (struct in6_addr) * 2);
-      uint32_t tmp = htonl (len);
-      sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
-      tmp = htonl (pkt6->next_header);
-      sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));      
-      sum = GNUNET_CRYPTO_crc16_step (sum,  pkt6_tcp, len);
-      pkt6_tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
+      GNUNET_TUN_calculate_tcp6_checksum (pkt6,
+                                         pkt6_tcp,
+                                         payload,
+                                         payload_length);
     }
     break;
   default:
@@ -1231,22 +1224,25 @@ prepare_ipv6_packet (const void *payload, size_t payload_length,
 static void
 send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
                         const struct SocketAddress *source_address,
-                        const struct tcp_packet *tcp_header,
+                        const struct GNUNET_TUN_TcpHeader *tcp_header,
                         const void *payload, size_t payload_length)
 {
   size_t len;
 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# TCP packets sent via TUN"),
+                           1, GNUNET_NO);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Sending packet with %u bytes TCP payload via TUN\n",
              (unsigned int) payload_length);
-  len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct tun_header);
+  len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
   switch (source_address->af)
   {
   case AF_INET:
-    len += sizeof (struct ip4_header);
+    len += sizeof (struct GNUNET_TUN_IPv4Header);
     break;
   case AF_INET6:
-    len += sizeof (struct ip6_header);
+    len += sizeof (struct GNUNET_TUN_IPv6Header);
     break;
   default:
     GNUNET_break (0);
@@ -1261,18 +1257,18 @@ send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
   {
     char buf[len];
     struct GNUNET_MessageHeader *hdr;
-    struct tun_header *tun;
+    struct GNUNET_TUN_Layer2PacketHeader *tun;
     
     hdr= (struct GNUNET_MessageHeader *) buf;
     hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
     hdr->size = htons (len);
-    tun = (struct tun_header*) &hdr[1];
+    tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
     tun->flags = htons (0);
     switch (source_address->af)
     {
     case AF_INET:
       {
-       struct ip4_header * ipv4 = (struct ip4_header*) &tun[1];
+       struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
        
        tun->proto = htons (ETH_P_IPV4);
        prepare_ipv4_packet (payload, payload_length,
@@ -1285,7 +1281,7 @@ send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
       break;
     case AF_INET6:
       {
-       struct ip6_header * ipv6 = (struct ip6_header*) &tun[1];
+       struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
        
        tun->proto = htons (ETH_P_IPV6);
        prepare_ipv6_packet (payload, payload_length, 
@@ -1332,6 +1328,12 @@ receive_tcp_service (void *unused GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunn
   const struct GNUNET_EXIT_TcpServiceStartMessage *start;
   uint16_t pkt_len = ntohs (message->size);
 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# TCP service creation requests received via mesh"),
+                           1, GNUNET_NO);
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# Bytes received from MESH"),
+                           pkt_len, GNUNET_NO);
   /* check that we got at least a valid header */
   if (pkt_len < sizeof (struct GNUNET_EXIT_TcpServiceStartMessage))
   {
@@ -1361,6 +1363,9 @@ receive_tcp_service (void *unused GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunn
                _("No service found for %s on port %d!\n"),
                "TCP",
                ntohs (start->tcp_header.dpt));
+    GNUNET_STATISTICS_update (stats,
+                             gettext_noop ("# TCP requests dropped (no such service)"),
+                             1, GNUNET_NO);
     return GNUNET_SYSERR;
   }
   state->ri.remote_address = state->serv->address;    
@@ -1400,6 +1405,12 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   const void *payload;
   int af;
 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# Bytes received from MESH"),
+                           pkt_len, GNUNET_NO);
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# TCP IP-exit creation requests received via mesh"),
+                           1, GNUNET_NO);
   if (pkt_len < sizeof (struct GNUNET_EXIT_TcpInternetStartMessage))
   {
     GNUNET_break_op (0);
@@ -1424,6 +1435,11 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
       GNUNET_break_op (0);
       return GNUNET_SYSERR;
     }
+    if (! ipv4_exit)
+    {
+      GNUNET_break_op (0);
+      return GNUNET_SYSERR;
+    }
     v4 = (const struct in_addr*) &start[1];
     payload = &v4[1];
     pkt_len -= sizeof (struct in_addr);
@@ -1435,6 +1451,11 @@ receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
       GNUNET_break_op (0);
       return GNUNET_SYSERR;
     }
+    if (! ipv6_exit)
+    {
+      GNUNET_break_op (0);
+      return GNUNET_SYSERR;
+    }
     v6 = (const struct in6_addr*) &start[1];
     payload = &v6[1];
     pkt_len -= sizeof (struct in_addr);
@@ -1490,6 +1511,12 @@ receive_tcp_data (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   const struct GNUNET_EXIT_TcpDataMessage *data;
   uint16_t pkt_len = ntohs (message->size);
 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# Bytes received from MESH"),
+                           pkt_len, GNUNET_NO);
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# TCP data requests received via mesh"),
+                           1, GNUNET_NO);
   if (pkt_len < sizeof (struct GNUNET_EXIT_TcpDataMessage))
   {
     GNUNET_break_op (0);
@@ -1501,8 +1528,9 @@ receive_tcp_data (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
        (NULL == state->heap_node) )
   {
     /* connection should have been up! */
-    GNUNET_break_op (0);
-    /* FIXME: call statistics */
+    GNUNET_STATISTICS_update (stats,
+                             gettext_noop ("# TCP DATA requests dropped (no session)"),
+                             1, GNUNET_NO);
     return GNUNET_SYSERR;
   }
   GNUNET_break_op (ntohl (data->reserved) == 0);
@@ -1540,23 +1568,26 @@ send_udp_packet_via_tun (const struct SocketAddress *destination_address,
 {
   size_t len;
 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# UDP packets sent via TUN"),
+                           1, GNUNET_NO);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Sending packet with %u bytes UDP payload via TUN\n",
              (unsigned int) payload_length);
-  len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct tun_header);
+  len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
   switch (source_address->af)
   {
   case AF_INET:
-    len += sizeof (struct ip4_header);
+    len += sizeof (struct GNUNET_TUN_IPv4Header);
     break;
   case AF_INET6:
-    len += sizeof (struct ip6_header);
+    len += sizeof (struct GNUNET_TUN_IPv6Header);
     break;
   default:
     GNUNET_break (0);
     return;
   }
-  len += sizeof (struct udp_packet);
+  len += sizeof (struct GNUNET_TUN_UdpHeader);
   len += payload_length;
   if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
   {
@@ -1566,18 +1597,18 @@ send_udp_packet_via_tun (const struct SocketAddress *destination_address,
   {
     char buf[len];
     struct GNUNET_MessageHeader *hdr;
-    struct tun_header *tun;
+    struct GNUNET_TUN_Layer2PacketHeader *tun;
     
     hdr= (struct GNUNET_MessageHeader *) buf;
     hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
     hdr->size = htons (len);
-    tun = (struct tun_header*) &hdr[1];
+    tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
     tun->flags = htons (0);
     switch (source_address->af)
     {
     case AF_INET:
       {
-       struct ip4_header * ipv4 = (struct ip4_header*) &tun[1];
+       struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
        
        tun->proto = htons (ETH_P_IPV4);
        prepare_ipv4_packet (payload, payload_length,
@@ -1590,7 +1621,7 @@ send_udp_packet_via_tun (const struct SocketAddress *destination_address,
       break;
     case AF_INET6:
       {
-       struct ip6_header * ipv6 = (struct ip6_header*) &tun[1];
+       struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
        
        tun->proto = htons (ETH_P_IPV6);
        prepare_ipv6_packet (payload, payload_length, 
@@ -1640,6 +1671,12 @@ receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   const void *payload;
   int af;
 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# Bytes received from MESH"),
+                           pkt_len, GNUNET_NO);
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# UDP IP-exit requests received via mesh"),
+                           1, GNUNET_NO);
   if (pkt_len < sizeof (struct GNUNET_EXIT_UdpInternetMessage))
   {
     GNUNET_break_op (0);
@@ -1657,6 +1694,11 @@ receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
       GNUNET_break_op (0);
       return GNUNET_SYSERR;
     }
+    if (! ipv4_exit)
+    {
+      GNUNET_break_op (0);
+      return GNUNET_SYSERR;
+    }
     v4 = (const struct in_addr*) &msg[1];
     payload = &v4[1];
     pkt_len -= sizeof (struct in_addr);
@@ -1668,6 +1710,11 @@ receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
       GNUNET_break_op (0);
       return GNUNET_SYSERR;
     }
+    if (! ipv6_exit)
+    {
+      GNUNET_break_op (0);
+      return GNUNET_SYSERR;
+    }
     v6 = (const struct in6_addr*) &msg[1];
     payload = &v6[1];
     pkt_len -= sizeof (struct in_addr);
@@ -1724,6 +1771,12 @@ receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   const struct GNUNET_EXIT_UdpServiceMessage *msg;
   uint16_t pkt_len = ntohs (message->size);
 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# Bytes received from MESH"),
+                           pkt_len, GNUNET_NO);
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# UDP service requests received via mesh"),
+                           1, GNUNET_NO);
   /* check that we got at least a valid header */
   if (pkt_len < sizeof (struct GNUNET_EXIT_UdpServiceMessage))
   {
@@ -1744,6 +1797,9 @@ receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                _("No service found for %s on port %d!\n"),
                "UDP",
                ntohs (msg->destination_port));
+    GNUNET_STATISTICS_update (stats,
+                             gettext_noop ("# UDP requests dropped (no such service)"),
+                             1, GNUNET_NO);
     return GNUNET_SYSERR;
   }
   state->ri.remote_address = state->serv->address;    
@@ -1773,6 +1829,9 @@ new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
 {
   struct TunnelState *s = GNUNET_malloc (sizeof (struct TunnelState));
 
+  GNUNET_STATISTICS_update (stats,
+                           gettext_noop ("# Inbound MESH tunnels created"),
+                           1, GNUNET_NO);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received inbound tunnel from `%s'\n",
              GNUNET_i2s (initiator));
@@ -1989,12 +2048,30 @@ add_services (int proto,
       serv->address.af = res->ai_family;
       switch (res->ai_family)
       {
-       case AF_INET:
-         serv->address.address.ipv4 = ((struct sockaddr_in *) res->ai_addr)->sin_addr;
-         break;
-       case AF_INET6:
-         serv->address.address.ipv6 = ((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
-         break;
+      case AF_INET:
+       if (! ipv4_enabled)
+       {
+         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                     _("Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
+                     name);
+         freeaddrinfo (res);
+         GNUNET_free (serv);
+         continue;
+       }
+       serv->address.address.ipv4 = ((struct sockaddr_in *) res->ai_addr)->sin_addr;
+       break;
+      case AF_INET6:
+       if (! ipv6_enabled)
+       {
+         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                     _("Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
+                     name);
+         freeaddrinfo (res);
+         GNUNET_free (serv);
+         continue;
+       }       
+       serv->address.address.ipv6 = ((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
+       break;
       default:
        freeaddrinfo (res);
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -2079,8 +2156,6 @@ run (void *cls, char *const *args GNUNET_UNUSED,
   char *ipv6prefix_s;
   char *ipv4addr;
   char *ipv4mask;
-  struct in_addr v4;
-  struct in6_addr v6;
 
   cfg = cfg_;
   stats = GNUNET_STATISTICS_create ("exit", cfg);
@@ -2156,7 +2231,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
     if ( (GNUNET_SYSERR ==
          GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
                                                 &ipv6addr) ||
-         (1 != inet_pton (AF_INET6, ipv6addr, &v6))) )
+         (1 != inet_pton (AF_INET6, ipv6addr, &exit_ipv6addr))) )
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "No valid entry 'IPV6ADDR' in configuration!\n");
@@ -2195,7 +2270,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
     if ( (GNUNET_SYSERR ==
          GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
                                                 &ipv4addr) ||
-         (1 != inet_pton (AF_INET, ipv4addr, &v4))) )
+         (1 != inet_pton (AF_INET, ipv4addr, &exit_ipv4addr))) )
       {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "No valid entry for 'IPV4ADDR' in configuration!\n");
@@ -2206,7 +2281,7 @@ run (void *cls, char *const *args GNUNET_UNUSED,
     if ( (GNUNET_SYSERR ==
          GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
                                                 &ipv4mask) ||
-         (1 != inet_pton (AF_INET, ipv4mask, &v4))) )
+         (1 != inet_pton (AF_INET, ipv4mask, &exit_ipv4mask))) )
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "No valid entry 'IPV4MASK' in configuration!\n");