2 This file is part of GNUnet.
3 (C) 2010, 2012 Christian Grothoff
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 exit/gnunet-daemon-exit.c
23 * @brief tool to allow IP traffic exit from the GNUnet mesh to the Internet
24 * @author Philipp Toelke
25 * @author Christian Grothoff
31 * - factor out crc computations from DNS/EXIT/VPN into shared library?
34 * - which code should advertise services? the service model is right
35 * now a bit odd, especially as this code DOES the exit and knows
36 * the DNS "name", but OTOH this is clearly NOT the place to advertise
37 * the service's existence; maybe the daemon should turn into a
38 * service with an API to add local-exit services dynamically?
41 #include "gnunet_util_lib.h"
42 #include "gnunet_protocols.h"
43 #include "gnunet_applications.h"
44 #include "gnunet_mesh_service.h"
45 #include "gnunet_statistics_service.h"
46 #include "gnunet_constants.h"
47 #include "gnunet_tun_lib.h"
51 * Information about an address.
56 * AF_INET or AF_INET6.
61 * Remote address information.
66 * Address, if af is AF_INET.
71 * Address, if af is AF_INET6.
77 * IPPROTO_TCP or IPPROTO_UDP;
82 * Remote port, in host byte order!
89 * This struct is saved into the services-hashmap to represent
90 * a service this peer is specifically offering an exit for
91 * (for a specific domain name).
97 * Remote address to use for the service.
99 struct SocketAddress address;
102 * DNS name of the service.
107 * Port I am listening on within GNUnet for this service, in host
108 * byte order. (as we may redirect ports).
115 * Information we use to track a connection (the classical 6-tuple of
116 * IP-version, protocol, source-IP, destination-IP, source-port and
119 struct RedirectInformation
123 * Address information for the other party (equivalent of the
124 * arguments one would give to "connect").
126 struct SocketAddress remote_address;
129 * Address information we used locally (AF and proto must match
130 * "remote_address"). Equivalent of the arguments one would give to
133 struct SocketAddress local_address;
136 Note 1: additional information might be added here in the
137 future to support protocols that require special handling,
140 Note 2: we might also sometimes not match on all components
141 of the tuple, to support protocols where things do not always
148 * Queue of messages to a tunnel.
150 struct TunnelMessageQueue
153 * This is a doubly-linked list.
155 struct TunnelMessageQueue *next;
158 * This is a doubly-linked list.
160 struct TunnelMessageQueue *prev;
163 * Payload to send via the tunnel.
168 * Number of bytes in 'payload'.
175 * This struct is saved into connections_map to allow finding the
176 * right tunnel given an IP packet from TUN. It is also associated
177 * with the tunnel's closure so we can find it again for the next
178 * message from the tunnel.
183 * Mesh tunnel that is used for this connection.
185 struct GNUNET_MESH_Tunnel *tunnel;
188 * Heap node for this state in the connections_heap.
190 struct GNUNET_CONTAINER_HeapNode *heap_node;
193 * Key this state has in the connections_map.
195 GNUNET_HashCode state_key;
198 * Associated service record, or NULL for no service.
200 struct LocalService *serv;
203 * Head of DLL of messages for this tunnel.
205 struct TunnelMessageQueue *head;
208 * Tail of DLL of messages for this tunnel.
210 struct TunnelMessageQueue *tail;
213 * Active tunnel transmission request (or NULL).
215 struct GNUNET_MESH_TransmitHandle *th;
218 * Primary redirection information for this connection.
220 struct RedirectInformation ri;
226 * The handle to the configuration used throughout the process
228 static const struct GNUNET_CONFIGURATION_Handle *cfg;
231 * The handle to the helper
233 static struct GNUNET_HELPER_Handle *helper_handle;
236 * Arguments to the exit helper.
238 static char *exit_argv[7];
241 * IPv6 prefix (0..127) from configuration file.
243 static unsigned long long ipv6prefix;
248 static struct GNUNET_STATISTICS_Handle *stats;
253 static struct GNUNET_MESH_Handle *mesh_handle;
256 * This hashmaps contains the mapping from peer, service-descriptor,
257 * source-port and destination-port to a struct TunnelState
259 static struct GNUNET_CONTAINER_MultiHashMap *connections_map;
262 * Heap so we can quickly find "old" connections.
264 static struct GNUNET_CONTAINER_Heap *connections_heap;
267 * If there are at least this many connections, old ones will be removed
269 static long long unsigned int max_connections;
272 * This hashmaps saves interesting things about the configured UDP services
274 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
277 * This hashmaps saves interesting things about the configured TCP services
279 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
282 * Are we an IPv4-exit?
284 static int ipv4_exit;
287 * Are we an IPv6-exit?
289 static int ipv6_exit;
292 * Do we support IPv4 at all on the TUN interface?
294 static int ipv4_enabled;
297 * Do we support IPv6 at all on the TUN interface?
299 static int ipv6_enabled;
303 * Given IP information about a connection, calculate the respective
304 * hash we would use for the 'connections_map'.
306 * @param hash resulting hash
307 * @param ri information about the connection
310 hash_redirect_info (GNUNET_HashCode *hash,
311 const struct RedirectInformation *ri)
315 memset (hash, 0, sizeof (GNUNET_HashCode));
316 /* the GNUnet hashmap only uses the first sizeof(unsigned int) of the hash,
317 so we put the IP address in there (and hope for few collisions) */
319 switch (ri->remote_address.af)
322 memcpy (off, &ri->remote_address.address.ipv4, sizeof (struct in_addr));
323 off += sizeof (struct in_addr);
326 memcpy (off, &ri->remote_address.address.ipv6, sizeof (struct in6_addr));
327 off += sizeof (struct in_addr);
332 memcpy (off, &ri->remote_address.port, sizeof (uint16_t));
333 off += sizeof (uint16_t);
334 switch (ri->local_address.af)
337 memcpy (off, &ri->local_address.address.ipv4, sizeof (struct in_addr));
338 off += sizeof (struct in_addr);
341 memcpy (off, &ri->local_address.address.ipv6, sizeof (struct in6_addr));
342 off += sizeof (struct in_addr);
347 memcpy (off, &ri->local_address.port, sizeof (uint16_t));
348 off += sizeof (uint16_t);
349 memcpy (off, &ri->remote_address.proto, sizeof (uint8_t));
350 off += sizeof (uint8_t);
355 * Get our connection tracking state. Warns if it does not exists,
356 * refreshes the timestamp if it does exist.
358 * @param af address family
359 * @param protocol IPPROTO_UDP or IPPROTO_TCP
360 * @param destination_ip target IP
361 * @param destination_port target port
362 * @param local_ip local IP
363 * @param local_port local port
364 * @param state_key set to hash's state if non-NULL
365 * @return NULL if we have no tracking information for this tuple
367 static struct TunnelState *
368 get_redirect_state (int af,
370 const void *destination_ip,
371 uint16_t destination_port,
372 const void *local_ip,
374 GNUNET_HashCode *state_key)
376 struct RedirectInformation ri;
378 struct TunnelState *state;
380 ri.remote_address.af = af;
382 ri.remote_address.address.ipv4 = *((struct in_addr*) destination_ip);
384 ri.remote_address.address.ipv6 = * ((struct in6_addr*) destination_ip);
385 ri.remote_address.port = destination_port;
386 ri.remote_address.proto = protocol;
387 ri.local_address.af = af;
389 ri.local_address.address.ipv4 = *((struct in_addr*) local_ip);
391 ri.local_address.address.ipv6 = * ((struct in6_addr*) local_ip);
392 ri.local_address.port = local_port;
393 ri.local_address.proto = protocol;
394 hash_redirect_info (&key, &ri);
395 if (NULL != state_key)
397 state = GNUNET_CONTAINER_multihashmap_get (connections_map, &key);
400 /* Mark this connection as freshly used */
401 if (NULL == state_key)
402 GNUNET_CONTAINER_heap_update_cost (connections_heap,
404 GNUNET_TIME_absolute_get ().abs_value);
410 * Given a service descriptor and a destination port, find the
411 * respective service entry.
413 * @param service_map map of services (TCP or UDP)
414 * @param desc service descriptor
415 * @param dpt destination port
416 * @return NULL if we are not aware of such a service
418 static struct LocalService *
419 find_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
420 const GNUNET_HashCode *desc,
423 char key[sizeof (GNUNET_HashCode) + sizeof (uint16_t)];
425 memcpy (&key[0], &dpt, sizeof (uint16_t));
426 memcpy (&key[sizeof(uint16_t)], desc, sizeof (GNUNET_HashCode));
427 return GNUNET_CONTAINER_multihashmap_get (service_map,
428 (GNUNET_HashCode *) key);
433 * Free memory associated with a service record.
436 * @param key service descriptor
437 * @param value service record to free
441 free_service_record (void *cls,
442 const GNUNET_HashCode *key,
445 struct LocalService *service = value;
447 GNUNET_free_non_null (service->name);
448 GNUNET_free (service);
454 * Given a service descriptor and a destination port, find the
455 * respective service entry.
457 * @param service_map map of services (TCP or UDP)
458 * @param name name of the service
459 * @param dpt destination port
460 * @param service service information record to store (service->name will be set).
463 store_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
466 struct LocalService *service)
468 char key[sizeof (GNUNET_HashCode) + sizeof (uint16_t)];
469 GNUNET_HashCode desc;
471 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &desc);
472 service->name = GNUNET_strdup (name);
473 memcpy (&key[0], &dpt, sizeof (uint16_t));
474 memcpy (&key[sizeof(uint16_t)], &desc, sizeof (GNUNET_HashCode));
476 GNUNET_CONTAINER_multihashmap_put (service_map,
477 (GNUNET_HashCode *) key,
479 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
481 free_service_record (NULL, (GNUNET_HashCode *) key, service);
482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
483 _("Got duplicate service records for `%s:%u'\n"),
491 * MESH is ready to receive a message for the tunnel. Transmit it.
493 * @param cls the 'struct TunnelState'.
494 * @param size number of bytes available in buf
495 * @param buf where to copy the message
496 * @return number of bytes copied to buf
499 send_to_peer_notify_callback (void *cls, size_t size, void *buf)
501 struct TunnelState *s = cls;
502 struct GNUNET_MESH_Tunnel *tunnel = s->tunnel;
503 struct TunnelMessageQueue *tnq;
507 GNUNET_assert (size >= tnq->len);
508 memcpy (buf, tnq->payload, tnq->len);
510 GNUNET_CONTAINER_DLL_remove (s->head,
514 if (NULL != (tnq = s->head))
515 s->th = GNUNET_MESH_notify_transmit_ready (tunnel,
516 GNUNET_NO /* corking */,
518 GNUNET_TIME_UNIT_FOREVER_REL,
521 &send_to_peer_notify_callback,
523 GNUNET_STATISTICS_update (stats,
524 gettext_noop ("# Bytes transmitted via mesh tunnels"),
531 * Send the given packet via the mesh tunnel.
533 * @param mesh_tunnel destination
534 * @param payload message to transmit
535 * @param payload_length number of bytes in payload
536 * @param desc descriptor to add before payload (optional)
537 * @param mtype message type to use
540 send_packet_to_mesh_tunnel (struct GNUNET_MESH_Tunnel *mesh_tunnel,
542 size_t payload_length,
543 const GNUNET_HashCode *desc,
546 struct TunnelState *s;
547 struct TunnelMessageQueue *tnq;
548 struct GNUNET_MessageHeader *msg;
552 len = sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) + payload_length;
553 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
558 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + len);
559 tnq->payload = &tnq[1];
561 msg = (struct GNUNET_MessageHeader *) &tnq[1];
562 msg->size = htons ((uint16_t) len);
563 msg->type = htons (mtype);
566 dp = (GNUNET_HashCode *) &msg[1];
568 memcpy (&dp[1], payload, payload_length);
572 memcpy (&msg[1], payload, payload_length);
574 s = GNUNET_MESH_tunnel_get_data (mesh_tunnel);
575 GNUNET_assert (NULL != s);
576 GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, tnq);
578 s->th = GNUNET_MESH_notify_transmit_ready (mesh_tunnel, GNUNET_NO /* cork */, 0 /* priority */,
579 GNUNET_TIME_UNIT_FOREVER_REL,
581 &send_to_peer_notify_callback,
587 * @brief Handles an UDP packet received from the helper.
589 * @param udp A pointer to the Packet
590 * @param pktlen number of bytes in 'udp'
591 * @param af address family (AFINET or AF_INET6)
592 * @param destination_ip destination IP-address of the IP packet (should
593 * be our local address)
594 * @param source_ip original source IP-address of the IP packet (should
595 * be the original destination address)
598 udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
601 const void *destination_ip,
602 const void *source_ip)
604 struct TunnelState *state;
607 char sbuf[INET6_ADDRSTRLEN];
608 char dbuf[INET6_ADDRSTRLEN];
609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
610 "Received UDP packet going from %s:%u to %s:%u\n",
613 sbuf, sizeof (sbuf)),
614 (unsigned int) ntohs (udp->spt),
617 dbuf, sizeof (dbuf)),
618 (unsigned int) ntohs (udp->dpt));
620 if (pktlen < sizeof (struct GNUNET_TUN_UdpHeader))
626 if (pktlen != ntohs (udp->len))
632 state = get_redirect_state (af, IPPROTO_UDP,
640 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
641 _("Packet dropped, have no matching connection information\n"));
644 send_packet_to_mesh_tunnel (state->tunnel,
645 &udp[1], pktlen - sizeof (struct GNUNET_TUN_UdpHeader),
648 ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK
649 : GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK);
654 * @brief Handles a TCP packet received from the helper.
656 * @param tcp A pointer to the Packet
657 * @param pktlen the length of the packet, including its header
658 * @param af address family (AFINET or AF_INET6)
659 * @param destination_ip destination IP-address of the IP packet (should
660 * be our local address)
661 * @param source_ip original source IP-address of the IP packet (should
662 * be the original destination address)
665 tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
668 const void *destination_ip,
669 const void *source_ip)
671 struct TunnelState *state;
673 struct GNUNET_TUN_TcpHeader *mtcp;
676 char sbuf[INET6_ADDRSTRLEN];
677 char dbuf[INET6_ADDRSTRLEN];
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679 "Received TCP packet going from %s:%u to %s:%u\n",
682 sbuf, sizeof (sbuf)),
683 (unsigned int) ntohs (tcp->spt),
686 dbuf, sizeof (dbuf)),
687 (unsigned int) ntohs (tcp->dpt));
689 if (pktlen < sizeof (struct GNUNET_TUN_TcpHeader))
695 state = get_redirect_state (af, IPPROTO_TCP,
703 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
704 _("Packet dropped, have no matching connection information\n"));
708 /* mug port numbers and crc to avoid information leakage;
709 sender will need to lookup the correct values anyway */
710 memcpy (buf, tcp, pktlen);
711 mtcp = (struct GNUNET_TUN_TcpHeader *) buf;
715 send_packet_to_mesh_tunnel (state->tunnel,
719 ? GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK
720 : GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK);
725 * Receive packets from the helper-process
728 * @param client unsued
729 * @param message message received from helper
732 message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
733 const struct GNUNET_MessageHeader *message)
735 const struct GNUNET_TUN_Layer2PacketHeader *pkt_tun;
738 GNUNET_STATISTICS_update (stats,
739 gettext_noop ("# Packets received from TUN"),
741 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
746 size = ntohs (message->size);
747 if (size < sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader))
752 GNUNET_STATISTICS_update (stats,
753 gettext_noop ("# Bytes received from TUN"),
755 pkt_tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
756 size -= sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader);
757 switch (ntohs (pkt_tun->proto))
761 const struct GNUNET_TUN_IPv6Header *pkt6;
763 if (size < sizeof (struct GNUNET_TUN_IPv6Header))
765 /* Kernel to blame? */
769 pkt6 = (struct GNUNET_TUN_IPv6Header *) &pkt_tun[1];
770 if (size != ntohs (pkt6->payload_length))
772 /* Kernel to blame? */
776 size -= sizeof (struct GNUNET_TUN_IPv6Header);
777 switch (pkt6->next_header)
780 udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt6[1], size,
782 &pkt6->destination_address,
783 &pkt6->source_address);
786 tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt6[1], size,
788 &pkt6->destination_address,
789 &pkt6->source_address);
792 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
793 _("IPv6 packet with unsupported next header received. Ignored.\n"));
800 const struct GNUNET_TUN_IPv4Header *pkt4;
802 if (size < sizeof (struct GNUNET_TUN_IPv4Header))
804 /* Kernel to blame? */
808 pkt4 = (const struct GNUNET_TUN_IPv4Header *) &pkt_tun[1];
809 if (size != ntohs (pkt4->total_length))
811 /* Kernel to blame? */
815 if (pkt4->header_length * 4 != sizeof (struct GNUNET_TUN_IPv4Header))
817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
818 _("IPv4 packet options received. Ignored.\n"));
821 size -= sizeof (struct GNUNET_TUN_IPv4Header);
822 switch (pkt4->protocol)
825 udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt4[1], size,
827 &pkt4->destination_address,
828 &pkt4->source_address);
830 tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt4[1], size,
832 &pkt4->destination_address,
833 &pkt4->source_address);
836 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
837 _("IPv4 packet with unsupported next header received. Ignored.\n"));
843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
844 _("Packet from unknown protocol %u received. Ignored.\n"),
845 ntohs (pkt_tun->proto));
852 * We need to create a (unique) fresh local address (IP+port).
855 * @param af desired address family
856 * @param proto desired protocol (IPPROTO_UDP or IPPROTO_TCP)
857 * @param local_address address to initialize
860 setup_fresh_address (int af,
862 struct SocketAddress *local_address)
864 local_address->af = af;
865 local_address->proto = (uint8_t) proto;
866 /* default "local" port range is often 32768--61000,
867 so we pick a random value in that range */
869 = (uint16_t) 32768 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
875 const char *ipv4addr = exit_argv[4];
876 const char *ipv4mask = exit_argv[5];
881 GNUNET_assert (1 == inet_pton (AF_INET, ipv4addr, &addr));
882 GNUNET_assert (1 == inet_pton (AF_INET, ipv4mask, &mask));
883 if (0 == ~mask.s_addr)
885 /* only one valid IP anyway */
886 local_address->address.ipv4 = addr;
889 /* Given 192.168.0.1/255.255.0.0, we want a mask
890 of '192.168.255.255', thus: */
891 mask.s_addr = addr.s_addr | ~mask.s_addr;
892 /* Pick random IPv4 address within the subnet, except 'addr' or 'mask' itself */
895 rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
897 local_address->address.ipv4.s_addr = (addr.s_addr | rnd.s_addr) & mask.s_addr;
899 while ( (local_address->address.ipv4.s_addr == addr.s_addr) ||
900 (local_address->address.ipv4.s_addr == mask.s_addr) );
905 const char *ipv6addr = exit_argv[2];
906 struct in6_addr addr;
907 struct in6_addr mask;
911 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6addr, &addr));
912 GNUNET_assert (ipv6prefix < 128);
913 if (ipv6prefix == 127)
915 /* only one valid IP anyway */
916 local_address->address.ipv6 = addr;
919 /* Given ABCD::/96, we want a mask of 'ABCD::FFFF:FFFF,
922 for (i=127;i>=128-ipv6prefix;i--)
923 mask.s6_addr[i / 8] |= (1 << (i % 8));
925 /* Pick random IPv6 address within the subnet, except 'addr' or 'mask' itself */
930 rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
932 local_address->address.ipv6.s6_addr[i]
933 = (addr.s6_addr[i] | rnd.s6_addr[i]) & mask.s6_addr[i];
936 while ( (0 == memcmp (&local_address->address.ipv6,
938 sizeof (struct in6_addr))) ||
939 (0 == memcmp (&local_address->address.ipv6,
941 sizeof (struct in6_addr))) );
951 * We are starting a fresh connection (TCP or UDP) and need
952 * to pick a source port and IP address (within the correct
953 * range and address family) to associate replies with the
954 * connection / correct mesh tunnel. This function generates
955 * a "fresh" source IP and source port number for a connection
956 * After picking a good source address, this function sets up
957 * the state in the 'connections_map' and 'connections_heap'
958 * to allow finding the state when needed later. The function
959 * also makes sure that we remain within memory limits by
960 * cleaning up 'old' states.
962 * @param state skeleton state to setup a record for; should
963 * 'state->ri.remote_address' filled in so that
964 * this code can determine which AF/protocol is
965 * going to be used (the 'tunnel' should also
966 * already be set); after calling this function,
967 * heap_node and the local_address will be
968 * also initialized (heap_node != NULL can be
969 * used to test if a state has been fully setup).
972 setup_state_record (struct TunnelState *state)
975 struct TunnelState *s;
977 /* generate fresh, unique address */
980 setup_fresh_address (state->serv->address.af,
981 state->serv->address.proto,
982 &state->ri.local_address);
983 } while (NULL != get_redirect_state (state->ri.remote_address.af,
984 state->ri.remote_address.proto,
985 &state->ri.remote_address.address,
986 state->ri.remote_address.port,
987 &state->ri.local_address.address,
988 state->ri.local_address.port,
991 char buf[INET6_ADDRSTRLEN];
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Picked local address %s:%u for new connection\n",
994 inet_ntop (state->ri.local_address.af,
995 &state->ri.local_address.address,
997 (unsigned int) state->ri.local_address.port);
999 GNUNET_assert (GNUNET_OK ==
1000 GNUNET_CONTAINER_multihashmap_put (connections_map,
1002 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1003 state->heap_node = GNUNET_CONTAINER_heap_insert (connections_heap,
1005 GNUNET_TIME_absolute_get ().abs_value);
1006 while (GNUNET_CONTAINER_heap_get_size (connections_heap) > max_connections)
1008 s = GNUNET_CONTAINER_heap_remove_root (connections_heap);
1009 GNUNET_assert (state != s);
1010 s->heap_node = NULL;
1011 GNUNET_MESH_tunnel_destroy (s->tunnel);
1012 GNUNET_assert (GNUNET_OK ==
1013 GNUNET_CONTAINER_multihashmap_remove (connections_map,
1022 * Prepare an IPv4 packet for transmission via the TUN interface.
1023 * Initializes the IP header and calculates checksums (IP+UDP/TCP).
1024 * For UDP, the UDP header will be fully created, whereas for TCP
1025 * only the ports and checksum will be filled in. So for TCP,
1026 * a skeleton TCP header must be part of the provided payload.
1028 * @param payload payload of the packet (starting with UDP payload or
1029 * TCP header, depending on protocol)
1030 * @param payload_length number of bytes in 'payload'
1031 * @param protocol IPPROTO_UDP or IPPROTO_TCP
1032 * @param src_address source address to use (IP and port)
1033 * @param dst_address destination address to use (IP and port)
1034 * @param pkt6 where to write the assembled packet; must
1035 * contain enough space for the IP header, UDP/TCP header
1039 prepare_ipv4_packet (const void *payload, size_t payload_length,
1041 const struct GNUNET_TUN_TcpHeader *tcp_header,
1042 const struct SocketAddress *src_address,
1043 const struct SocketAddress *dst_address,
1044 struct GNUNET_TUN_IPv4Header *pkt4)
1048 len = payload_length;
1052 len += sizeof (struct GNUNET_TUN_UdpHeader);
1055 len += sizeof (struct GNUNET_TUN_TcpHeader);
1056 GNUNET_assert (NULL != tcp_header);
1062 if (len + sizeof (struct GNUNET_TUN_IPv4Header) > UINT16_MAX)
1068 GNUNET_TUN_initialize_ipv4_header (pkt4,
1071 &src_address->address.ipv4,
1072 &dst_address->address.ipv4);
1077 struct GNUNET_TUN_UdpHeader *pkt4_udp = (struct GNUNET_TUN_UdpHeader *) &pkt4[1];
1079 pkt4_udp->spt = htons (src_address->port);
1080 pkt4_udp->dpt = htons (dst_address->port);
1081 pkt4_udp->crc = 0; /* Optional for IPv4 */
1082 pkt4_udp->len = htons ((uint16_t) payload_length);
1083 memcpy (&pkt4_udp[1], payload, payload_length);
1088 struct GNUNET_TUN_TcpHeader *pkt4_tcp = (struct GNUNET_TUN_TcpHeader *) &pkt4[1];
1090 memcpy (pkt4_tcp, tcp_header, sizeof (struct GNUNET_TUN_TcpHeader));
1091 memcpy (&pkt4_tcp[1], payload, payload_length);
1092 pkt4_tcp->spt = htons (src_address->port);
1093 pkt4_tcp->dpt = htons (dst_address->port);
1096 sum = GNUNET_CRYPTO_crc16_step (sum,
1097 &pkt4->source_address,
1098 sizeof (struct in_addr) * 2);
1099 uint32_t tmp = htonl ((protocol << 16) | (0xffff & len));
1100 sum = GNUNET_CRYPTO_crc16_step (sum, & tmp, sizeof (uint32_t));
1101 sum = GNUNET_CRYPTO_crc16_step (sum, & pkt4_tcp, len);
1102 pkt4_tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1112 * Prepare an IPv6 packet for transmission via the TUN interface.
1113 * Initializes the IP header and calculates checksums (IP+UDP/TCP).
1114 * For UDP, the UDP header will be fully created, whereas for TCP
1115 * only the ports and checksum will be filled in. So for TCP,
1116 * a skeleton TCP header must be part of the provided payload.
1118 * @param payload payload of the packet (starting with UDP payload or
1119 * TCP header, depending on protocol)
1120 * @param payload_length number of bytes in 'payload'
1121 * @param protocol IPPROTO_UDP or IPPROTO_TCP
1122 * @param src_address source address to use (IP and port)
1123 * @param dst_address destination address to use (IP and port)
1124 * @param pkt6 where to write the assembled packet; must
1125 * contain enough space for the IP header, UDP/TCP header
1129 prepare_ipv6_packet (const void *payload, size_t payload_length,
1131 const struct GNUNET_TUN_TcpHeader *tcp_header,
1132 const struct SocketAddress *src_address,
1133 const struct SocketAddress *dst_address,
1134 struct GNUNET_TUN_IPv6Header *pkt6)
1138 len = payload_length;
1142 len += sizeof (struct GNUNET_TUN_UdpHeader);
1145 /* tcp_header (with port/crc not set) must be part of payload! */
1146 if (len < sizeof (struct GNUNET_TUN_TcpHeader))
1156 if (len > UINT16_MAX)
1163 pkt6->next_header = protocol;
1164 pkt6->payload_length = htons ((uint16_t) (len + sizeof (struct GNUNET_TUN_IPv6Header)));
1165 pkt6->hop_limit = 255;
1166 pkt6->destination_address = dst_address->address.ipv6;
1167 pkt6->source_address = src_address->address.ipv6;
1173 struct GNUNET_TUN_UdpHeader *pkt6_udp = (struct GNUNET_TUN_UdpHeader *) &pkt6[1];
1175 memcpy (&pkt6[1], payload, payload_length);
1177 pkt6_udp->spt = htons (src_address->port);
1178 pkt6_udp->dpt = htons (dst_address->port);
1179 pkt6_udp->len = htons ((uint16_t) payload_length);
1182 sum = GNUNET_CRYPTO_crc16_step (sum,
1183 &pkt6->source_address,
1184 sizeof (struct in6_addr) * 2);
1185 uint32_t tmp = htons (len);
1186 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1187 tmp = htonl (pkt6->next_header);
1188 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1189 sum = GNUNET_CRYPTO_crc16_step (sum, pkt6_udp, len);
1190 pkt6_udp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1195 struct GNUNET_TUN_TcpHeader *pkt6_tcp = (struct GNUNET_TUN_TcpHeader *) pkt6;
1197 memcpy (pkt6_tcp, payload, payload_length);
1199 pkt6_tcp->spt = htons (src_address->port);
1200 pkt6_tcp->dpt = htons (dst_address->port);
1203 sum = GNUNET_CRYPTO_crc16_step (sum, &pkt6->source_address,
1204 sizeof (struct in6_addr) * 2);
1205 uint32_t tmp = htonl (len);
1206 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1207 tmp = htonl (pkt6->next_header);
1208 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1209 sum = GNUNET_CRYPTO_crc16_step (sum, pkt6_tcp, len);
1210 pkt6_tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1221 * Send a TCP packet via the TUN interface.
1223 * @param destination_address IP and port to use for the TCP packet's destination
1224 * @param source_address IP and port to use for the TCP packet's source
1225 * @param tcp header template to use
1226 * @param payload payload of the TCP packet
1227 * @param payload_length number of bytes in 'payload'
1230 send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
1231 const struct SocketAddress *source_address,
1232 const struct GNUNET_TUN_TcpHeader *tcp_header,
1233 const void *payload, size_t payload_length)
1237 GNUNET_STATISTICS_update (stats,
1238 gettext_noop ("# TCP packets sent via TUN"),
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1241 "Sending packet with %u bytes TCP payload via TUN\n",
1242 (unsigned int) payload_length);
1243 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1244 switch (source_address->af)
1247 len += sizeof (struct GNUNET_TUN_IPv4Header);
1250 len += sizeof (struct GNUNET_TUN_IPv6Header);
1256 len += payload_length;
1257 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1264 struct GNUNET_MessageHeader *hdr;
1265 struct GNUNET_TUN_Layer2PacketHeader *tun;
1267 hdr= (struct GNUNET_MessageHeader *) buf;
1268 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1269 hdr->size = htons (len);
1270 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
1271 tun->flags = htons (0);
1272 switch (source_address->af)
1276 struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
1278 tun->proto = htons (ETH_P_IPV4);
1279 prepare_ipv4_packet (payload, payload_length,
1283 destination_address,
1289 struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
1291 tun->proto = htons (ETH_P_IPV6);
1292 prepare_ipv6_packet (payload, payload_length,
1296 destination_address,
1304 (void) GNUNET_HELPER_send (helper_handle,
1305 (const struct GNUNET_MessageHeader*) buf,
1313 * Process a request via mesh to send a request to a TCP service
1314 * offered by this system.
1316 * @param cls closure, NULL
1317 * @param tunnel connection to the other end
1318 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1319 * @param sender who sent the message
1320 * @param message the actual message
1321 * @param atsi performance data for the connection
1322 * @return GNUNET_OK to keep the connection open,
1323 * GNUNET_SYSERR to close it (signal serious error)
1326 receive_tcp_service (void *unused GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1327 void **tunnel_ctx GNUNET_UNUSED,
1328 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1329 const struct GNUNET_MessageHeader *message,
1330 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1332 struct TunnelState *state = *tunnel_ctx;
1333 const struct GNUNET_EXIT_TcpServiceStartMessage *start;
1334 uint16_t pkt_len = ntohs (message->size);
1336 GNUNET_STATISTICS_update (stats,
1337 gettext_noop ("# TCP service creation requests received via mesh"),
1339 GNUNET_STATISTICS_update (stats,
1340 gettext_noop ("# Bytes received from MESH"),
1341 pkt_len, GNUNET_NO);
1342 /* check that we got at least a valid header */
1343 if (pkt_len < sizeof (struct GNUNET_EXIT_TcpServiceStartMessage))
1345 GNUNET_break_op (0);
1346 return GNUNET_SYSERR;
1348 start = (const struct GNUNET_EXIT_TcpServiceStartMessage*) message;
1349 pkt_len -= sizeof (struct GNUNET_EXIT_TcpServiceStartMessage);
1350 if ( (NULL == state) ||
1351 (NULL != state->serv) ||
1352 (NULL != state->heap_node) )
1354 GNUNET_break_op (0);
1355 return GNUNET_SYSERR;
1357 GNUNET_break_op (ntohl (start->reserved) == 0);
1358 /* setup fresh connection */
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 "Received data from %s for forwarding to TCP service %s on port %u\n",
1361 GNUNET_i2s (sender),
1362 GNUNET_h2s (&start->service_descriptor),
1363 (unsigned int) ntohs (start->tcp_header.dpt));
1364 if (NULL == (state->serv = find_service (tcp_services, &start->service_descriptor,
1365 ntohs (start->tcp_header.dpt))))
1367 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1368 _("No service found for %s on port %d!\n"),
1370 ntohs (start->tcp_header.dpt));
1371 GNUNET_STATISTICS_update (stats,
1372 gettext_noop ("# TCP requests dropped (no such service)"),
1374 return GNUNET_SYSERR;
1376 state->ri.remote_address = state->serv->address;
1377 setup_state_record (state);
1378 send_tcp_packet_via_tun (&state->ri.remote_address,
1379 &state->ri.local_address,
1381 &start[1], pkt_len);
1387 * Process a request to forward TCP data to the Internet via this peer.
1389 * @param cls closure, NULL
1390 * @param tunnel connection to the other end
1391 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1392 * @param sender who sent the message
1393 * @param message the actual message
1394 * @param atsi performance data for the connection
1395 * @return GNUNET_OK to keep the connection open,
1396 * GNUNET_SYSERR to close it (signal serious error)
1399 receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1400 void **tunnel_ctx GNUNET_UNUSED,
1401 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1402 const struct GNUNET_MessageHeader *message,
1403 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1405 struct TunnelState *state = *tunnel_ctx;
1406 const struct GNUNET_EXIT_TcpInternetStartMessage *start;
1407 uint16_t pkt_len = ntohs (message->size);
1408 const struct in_addr *v4;
1409 const struct in6_addr *v6;
1410 const void *payload;
1413 GNUNET_STATISTICS_update (stats,
1414 gettext_noop ("# Bytes received from MESH"),
1415 pkt_len, GNUNET_NO);
1416 GNUNET_STATISTICS_update (stats,
1417 gettext_noop ("# TCP IP-exit creation requests received via mesh"),
1419 if (pkt_len < sizeof (struct GNUNET_EXIT_TcpInternetStartMessage))
1421 GNUNET_break_op (0);
1422 return GNUNET_SYSERR;
1424 start = (const struct GNUNET_EXIT_TcpInternetStartMessage*) message;
1425 pkt_len -= sizeof (struct GNUNET_EXIT_TcpInternetStartMessage);
1426 if ( (NULL == state) ||
1427 (NULL != state->serv) ||
1428 (NULL != state->heap_node) )
1430 GNUNET_break_op (0);
1431 return GNUNET_SYSERR;
1433 af = (int) ntohl (start->af);
1434 state->ri.remote_address.af = af;
1438 if (pkt_len < sizeof (struct in_addr))
1440 GNUNET_break_op (0);
1441 return GNUNET_SYSERR;
1445 GNUNET_break_op (0);
1446 return GNUNET_SYSERR;
1448 v4 = (const struct in_addr*) &start[1];
1450 pkt_len -= sizeof (struct in_addr);
1451 state->ri.remote_address.address.ipv4 = *v4;
1454 if (pkt_len < sizeof (struct in6_addr))
1456 GNUNET_break_op (0);
1457 return GNUNET_SYSERR;
1461 GNUNET_break_op (0);
1462 return GNUNET_SYSERR;
1464 v6 = (const struct in6_addr*) &start[1];
1466 pkt_len -= sizeof (struct in_addr);
1467 state->ri.remote_address.address.ipv6 = *v6;
1470 GNUNET_break_op (0);
1471 return GNUNET_SYSERR;
1474 char buf[INET6_ADDRSTRLEN];
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 "Received data from %s for starting TCP stream to %s:%u\n",
1477 GNUNET_i2s (sender),
1479 &state->ri.remote_address.address,
1481 (unsigned int) ntohs (start->tcp_header.dpt));
1484 state->ri.remote_address.proto = IPPROTO_TCP;
1485 state->ri.remote_address.port = ntohs (start->tcp_header.dpt);
1486 setup_state_record (state);
1487 send_tcp_packet_via_tun (&state->ri.remote_address,
1488 &state->ri.local_address,
1496 * Process a request to forward TCP data on an established
1497 * connection via this peer.
1499 * @param cls closure, NULL
1500 * @param tunnel connection to the other end
1501 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1502 * @param sender who sent the message
1503 * @param message the actual message
1504 * @param atsi performance data for the connection
1505 * @return GNUNET_OK to keep the connection open,
1506 * GNUNET_SYSERR to close it (signal serious error)
1509 receive_tcp_data (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1510 void **tunnel_ctx GNUNET_UNUSED,
1511 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1512 const struct GNUNET_MessageHeader *message,
1513 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1515 struct TunnelState *state = *tunnel_ctx;
1516 const struct GNUNET_EXIT_TcpDataMessage *data;
1517 uint16_t pkt_len = ntohs (message->size);
1519 GNUNET_STATISTICS_update (stats,
1520 gettext_noop ("# Bytes received from MESH"),
1521 pkt_len, GNUNET_NO);
1522 GNUNET_STATISTICS_update (stats,
1523 gettext_noop ("# TCP data requests received via mesh"),
1525 if (pkt_len < sizeof (struct GNUNET_EXIT_TcpDataMessage))
1527 GNUNET_break_op (0);
1528 return GNUNET_SYSERR;
1530 data = (const struct GNUNET_EXIT_TcpDataMessage*) message;
1531 pkt_len -= sizeof (struct GNUNET_EXIT_TcpDataMessage);
1532 if ( (NULL == state) ||
1533 (NULL == state->heap_node) )
1535 /* connection should have been up! */
1536 GNUNET_STATISTICS_update (stats,
1537 gettext_noop ("# TCP DATA requests dropped (no session)"),
1539 return GNUNET_SYSERR;
1541 GNUNET_break_op (ntohl (data->reserved) == 0);
1543 char buf[INET6_ADDRSTRLEN];
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 "Received additional data from %s for TCP stream to %s:%u\n",
1546 GNUNET_i2s (sender),
1547 inet_ntop (state->ri.remote_address.af,
1548 &state->ri.remote_address.address,
1550 (unsigned int) state->ri.remote_address.port);
1553 send_tcp_packet_via_tun (&state->ri.remote_address,
1554 &state->ri.local_address,
1562 * Send a UDP packet via the TUN interface.
1564 * @param destination_address IP and port to use for the UDP packet's destination
1565 * @param source_address IP and port to use for the UDP packet's source
1566 * @param payload payload of the UDP packet (does NOT include UDP header)
1567 * @param payload_length number of bytes of data in payload
1570 send_udp_packet_via_tun (const struct SocketAddress *destination_address,
1571 const struct SocketAddress *source_address,
1572 const void *payload, size_t payload_length)
1576 GNUNET_STATISTICS_update (stats,
1577 gettext_noop ("# UDP packets sent via TUN"),
1579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1580 "Sending packet with %u bytes UDP payload via TUN\n",
1581 (unsigned int) payload_length);
1582 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1583 switch (source_address->af)
1586 len += sizeof (struct GNUNET_TUN_IPv4Header);
1589 len += sizeof (struct GNUNET_TUN_IPv6Header);
1595 len += sizeof (struct GNUNET_TUN_UdpHeader);
1596 len += payload_length;
1597 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1604 struct GNUNET_MessageHeader *hdr;
1605 struct GNUNET_TUN_Layer2PacketHeader *tun;
1607 hdr= (struct GNUNET_MessageHeader *) buf;
1608 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1609 hdr->size = htons (len);
1610 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
1611 tun->flags = htons (0);
1612 switch (source_address->af)
1616 struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
1618 tun->proto = htons (ETH_P_IPV4);
1619 prepare_ipv4_packet (payload, payload_length,
1623 destination_address,
1629 struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
1631 tun->proto = htons (ETH_P_IPV6);
1632 prepare_ipv6_packet (payload, payload_length,
1636 destination_address,
1644 (void) GNUNET_HELPER_send (helper_handle,
1645 (const struct GNUNET_MessageHeader*) buf,
1653 * Process a request to forward UDP data to the Internet via this peer.
1655 * @param cls closure, NULL
1656 * @param tunnel connection to the other end
1657 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1658 * @param sender who sent the message
1659 * @param message the actual message
1660 * @param atsi performance data for the connection
1661 * @return GNUNET_OK to keep the connection open,
1662 * GNUNET_SYSERR to close it (signal serious error)
1665 receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1666 void **tunnel_ctx GNUNET_UNUSED,
1667 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1668 const struct GNUNET_MessageHeader *message,
1669 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1671 struct TunnelState *state = *tunnel_ctx;
1672 const struct GNUNET_EXIT_UdpInternetMessage *msg;
1673 uint16_t pkt_len = ntohs (message->size);
1674 const struct in_addr *v4;
1675 const struct in6_addr *v6;
1676 const void *payload;
1679 GNUNET_STATISTICS_update (stats,
1680 gettext_noop ("# Bytes received from MESH"),
1681 pkt_len, GNUNET_NO);
1682 GNUNET_STATISTICS_update (stats,
1683 gettext_noop ("# UDP IP-exit requests received via mesh"),
1685 if (pkt_len < sizeof (struct GNUNET_EXIT_UdpInternetMessage))
1687 GNUNET_break_op (0);
1688 return GNUNET_SYSERR;
1690 msg = (const struct GNUNET_EXIT_UdpInternetMessage*) message;
1691 pkt_len -= sizeof (struct GNUNET_EXIT_UdpInternetMessage);
1692 af = (int) ntohl (msg->af);
1693 state->ri.remote_address.af = af;
1697 if (pkt_len < sizeof (struct in_addr))
1699 GNUNET_break_op (0);
1700 return GNUNET_SYSERR;
1704 GNUNET_break_op (0);
1705 return GNUNET_SYSERR;
1707 v4 = (const struct in_addr*) &msg[1];
1709 pkt_len -= sizeof (struct in_addr);
1710 state->ri.remote_address.address.ipv4 = *v4;
1713 if (pkt_len < sizeof (struct in6_addr))
1715 GNUNET_break_op (0);
1716 return GNUNET_SYSERR;
1720 GNUNET_break_op (0);
1721 return GNUNET_SYSERR;
1723 v6 = (const struct in6_addr*) &msg[1];
1725 pkt_len -= sizeof (struct in_addr);
1726 state->ri.remote_address.address.ipv6 = *v6;
1729 GNUNET_break_op (0);
1730 return GNUNET_SYSERR;
1733 char buf[INET6_ADDRSTRLEN];
1734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1735 "Received data from %s for forwarding to UDP %s:%u\n",
1736 GNUNET_i2s (sender),
1738 &state->ri.remote_address.address,
1740 (unsigned int) ntohs (msg->destination_port));
1742 state->ri.remote_address.proto = IPPROTO_UDP;
1743 state->ri.remote_address.port = msg->destination_port;
1744 if (NULL == state->heap_node)
1745 setup_state_record (state);
1746 if (0 != ntohs (msg->source_port))
1747 state->ri.local_address.port = msg->source_port;
1748 send_udp_packet_via_tun (&state->ri.remote_address,
1749 &state->ri.local_address,
1756 * Process a request via mesh to send a request to a UDP service
1757 * offered by this system.
1759 * @param cls closure, NULL
1760 * @param tunnel connection to the other end
1761 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1762 * @param sender who sent the message
1763 * @param message the actual message
1764 * @param atsi performance data for the connection
1765 * @return GNUNET_OK to keep the connection open,
1766 * GNUNET_SYSERR to close it (signal serious error)
1769 receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1771 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1772 const struct GNUNET_MessageHeader *message,
1773 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1775 struct TunnelState *state = *tunnel_ctx;
1776 const struct GNUNET_EXIT_UdpServiceMessage *msg;
1777 uint16_t pkt_len = ntohs (message->size);
1779 GNUNET_STATISTICS_update (stats,
1780 gettext_noop ("# Bytes received from MESH"),
1781 pkt_len, GNUNET_NO);
1782 GNUNET_STATISTICS_update (stats,
1783 gettext_noop ("# UDP service requests received via mesh"),
1785 /* check that we got at least a valid header */
1786 if (pkt_len < sizeof (struct GNUNET_EXIT_UdpServiceMessage))
1788 GNUNET_break_op (0);
1789 return GNUNET_SYSERR;
1791 msg = (const struct GNUNET_EXIT_UdpServiceMessage*) message;
1792 pkt_len -= sizeof (struct GNUNET_EXIT_UdpServiceMessage);
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 "Received data from %s for forwarding to UDP service %s on port %u\n",
1795 GNUNET_i2s (sender),
1796 GNUNET_h2s (&msg->service_descriptor),
1797 (unsigned int) ntohs (msg->destination_port));
1798 if (NULL == (state->serv = find_service (udp_services, &msg->service_descriptor,
1799 ntohs (msg->destination_port))))
1801 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1802 _("No service found for %s on port %d!\n"),
1804 ntohs (msg->destination_port));
1805 GNUNET_STATISTICS_update (stats,
1806 gettext_noop ("# UDP requests dropped (no such service)"),
1808 return GNUNET_SYSERR;
1810 state->ri.remote_address = state->serv->address;
1811 setup_state_record (state);
1812 if (0 != ntohs (msg->source_port))
1813 state->ri.local_address.port = msg->source_port;
1814 send_udp_packet_via_tun (&state->ri.remote_address,
1815 &state->ri.local_address,
1822 * Callback from GNUNET_MESH for new tunnels.
1824 * @param cls closure
1825 * @param tunnel new handle to the tunnel
1826 * @param initiator peer that started the tunnel
1827 * @param atsi performance information for the tunnel
1828 * @return initial tunnel context for the tunnel
1831 new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1832 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
1833 const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
1835 struct TunnelState *s = GNUNET_malloc (sizeof (struct TunnelState));
1837 GNUNET_STATISTICS_update (stats,
1838 gettext_noop ("# Inbound MESH tunnels created"),
1840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1841 "Received inbound tunnel from `%s'\n",
1842 GNUNET_i2s (initiator));
1849 * Function called by mesh whenever an inbound tunnel is destroyed.
1850 * Should clean up any associated state.
1852 * @param cls closure (set from GNUNET_MESH_connect)
1853 * @param tunnel connection to the other end (henceforth invalid)
1854 * @param tunnel_ctx place where local state associated
1855 * with the tunnel is stored
1858 clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel,
1861 struct TunnelState *s = tunnel_ctx;
1862 struct TunnelMessageQueue *tnq;
1864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1865 "Tunnel destroyed\n");
1866 while (NULL != (tnq = s->head))
1868 GNUNET_CONTAINER_DLL_remove (s->head,
1873 if (s->heap_node != NULL)
1875 GNUNET_assert (GNUNET_YES ==
1876 GNUNET_CONTAINER_multihashmap_remove (connections_map,
1879 GNUNET_CONTAINER_heap_remove_node (s->heap_node);
1880 s->heap_node = NULL;
1884 GNUNET_MESH_notify_transmit_ready_cancel (s->th);
1892 * Function that frees everything from a hashmap
1896 * @param value value to free
1899 free_iterate (void *cls GNUNET_UNUSED,
1900 const GNUNET_HashCode * hash GNUNET_UNUSED, void *value)
1902 GNUNET_free (value);
1908 * Function scheduled as very last function, cleans up after us
1911 cleanup (void *cls GNUNET_UNUSED,
1912 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "Exit service is shutting down now\n");
1918 if (helper_handle != NULL)
1920 GNUNET_HELPER_stop (helper_handle);
1921 helper_handle = NULL;
1923 if (mesh_handle != NULL)
1925 GNUNET_MESH_disconnect (mesh_handle);
1928 if (NULL != connections_map)
1930 GNUNET_CONTAINER_multihashmap_iterate (connections_map, &free_iterate, NULL);
1931 GNUNET_CONTAINER_multihashmap_destroy (connections_map);
1932 connections_map = NULL;
1934 if (NULL != connections_heap)
1936 GNUNET_CONTAINER_heap_destroy (connections_heap);
1937 connections_heap = NULL;
1939 if (NULL != tcp_services)
1941 GNUNET_CONTAINER_multihashmap_iterate (tcp_services, &free_service_record, NULL);
1942 GNUNET_CONTAINER_multihashmap_destroy (tcp_services);
1943 tcp_services = NULL;
1945 if (NULL != udp_services)
1947 GNUNET_CONTAINER_multihashmap_iterate (udp_services, &free_service_record, NULL);
1948 GNUNET_CONTAINER_multihashmap_destroy (udp_services);
1949 udp_services = NULL;
1953 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
1957 GNUNET_free_non_null (exit_argv[i]);
1962 * Add services to the service map.
1964 * @param proto IPPROTO_TCP or IPPROTO_UDP
1965 * @param cpy copy of the service descriptor (can be mutilated)
1966 * @param name DNS name of the service
1969 add_services (int proto,
1976 struct LocalService *serv;
1978 for (redirect = strtok (cpy, " "); redirect != NULL;
1979 redirect = strtok (NULL, " "))
1981 if (NULL == (hostname = strstr (redirect, ":")))
1983 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1984 "option `%s' for domain `%s' is not formatted correctly!\n",
1991 if (NULL == (hostport = strstr (hostname, ":")))
1993 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1994 "option `%s' for domain `%s' is not formatted correctly!\n",
2002 int local_port = atoi (redirect);
2003 int remote_port = atoi (hostport);
2005 if (!((local_port > 0) && (local_port < 65536)))
2007 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2008 "`%s' is not a valid port number (for domain `%s')!", redirect,
2012 if (!((remote_port > 0) && (remote_port < 65536)))
2014 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2015 "`%s' is not a valid port number (for domain `%s')!", hostport,
2020 serv = GNUNET_malloc (sizeof (struct LocalService));
2021 serv->my_port = (uint16_t) local_port;
2022 serv->address.port = remote_port;
2023 if (0 == strcmp ("localhost4", hostname))
2025 const char *ip4addr = exit_argv[4];
2027 serv->address.af = AF_INET;
2028 GNUNET_assert (1 != inet_pton (AF_INET, ip4addr, &serv->address.address.ipv4));
2030 else if (0 == strcmp ("localhost6", hostname))
2032 const char *ip6addr = exit_argv[2];
2034 serv->address.af = AF_INET6;
2035 GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, &serv->address.address.ipv6));
2039 struct addrinfo *res;
2042 ret = getaddrinfo (hostname, NULL, NULL, &res);
2043 if ( (ret != 0) || (res == NULL) )
2045 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2046 _("No addresses found for hostname `%s' of service `%s'!\n"),
2053 serv->address.af = res->ai_family;
2054 switch (res->ai_family)
2059 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2060 _("Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
2066 serv->address.address.ipv4 = ((struct sockaddr_in *) res->ai_addr)->sin_addr;
2071 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2072 _("Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
2078 serv->address.address.ipv6 = ((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
2082 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2083 _("No IP addresses found for hostname `%s' of service `%s'!\n"),
2091 store_service ((IPPROTO_UDP == proto) ? udp_services : tcp_services,
2100 * Reads the configuration servicecfg and populates udp_services
2103 * @param section name of section in config, equal to hostname
2106 read_service_conf (void *cls GNUNET_UNUSED, const char *section)
2110 if ((strlen (section) < 8) ||
2111 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
2114 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
2117 add_services (IPPROTO_UDP, cpy, section);
2121 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
2124 add_services (IPPROTO_TCP, cpy, section);
2131 * @brief Main function that will be run by the scheduler.
2133 * @param cls closure
2134 * @param args remaining command-line arguments
2135 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2136 * @param cfg_ configuration
2139 run (void *cls, char *const *args GNUNET_UNUSED,
2140 const char *cfgfile GNUNET_UNUSED,
2141 const struct GNUNET_CONFIGURATION_Handle *cfg_)
2143 static struct GNUNET_MESH_MessageHandler handlers[] = {
2144 {&receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_UDP_TO_SERVICE, 0},
2145 {&receive_udp_remote, GNUNET_MESSAGE_TYPE_VPN_UDP_TO_INTERNET, 0},
2146 {&receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_TCP_TO_SERVICE_START, 0},
2147 {&receive_tcp_remote, GNUNET_MESSAGE_TYPE_VPN_TCP_TO_INTERNET_START, 0},
2148 {&receive_tcp_data, GNUNET_MESSAGE_TYPE_VPN_TCP_DATA, 0},
2152 static GNUNET_MESH_ApplicationType apptypes[] = {
2153 GNUNET_APPLICATION_TYPE_END,
2154 GNUNET_APPLICATION_TYPE_END,
2155 GNUNET_APPLICATION_TYPE_END
2157 unsigned int app_idx;
2168 stats = GNUNET_STATISTICS_create ("exit", cfg);
2169 ipv4_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "EXIT_IPV4");
2170 ipv6_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "EXIT_IPV6");
2171 ipv4_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_IPV4");
2172 ipv6_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_IPV6");
2173 if (ipv4_exit && (! ipv4_enabled))
2175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2176 _("Cannot enable IPv4 exit but disable IPv4 on TUN interface, will use ENABLE_IPv4=YES\n"));
2177 ipv4_enabled = GNUNET_YES;
2179 if (ipv6_exit && (! ipv6_enabled))
2181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2182 _("Cannot enable IPv6 exit but disable IPv6 on TUN interface, will use ENABLE_IPv6=YES\n"));
2183 ipv6_enabled = GNUNET_YES;
2185 if (! (ipv4_enabled || ipv6_enabled))
2187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2188 _("No useful service enabled. Exiting.\n"));
2189 GNUNET_SCHEDULER_shutdown ();
2193 if (GNUNET_YES == ipv4_exit)
2195 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_IPV4_GATEWAY;
2198 if (GNUNET_YES == ipv6_exit)
2200 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_IPV6_GATEWAY;
2204 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
2207 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_CONNECTIONS",
2209 max_connections = 1024;
2210 exit_argv[0] = GNUNET_strdup ("exit-gnunet");
2211 if (GNUNET_SYSERR ==
2212 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "TUN_IFNAME", &tun_ifname))
2214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2215 "No entry 'TUN_IFNAME' in configuration!\n");
2216 GNUNET_SCHEDULER_shutdown ();
2219 exit_argv[1] = tun_ifname;
2222 if (GNUNET_SYSERR ==
2223 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "EXIT_IFNAME", &exit_ifname))
2225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2226 "No entry 'EXIT_IFNAME' in configuration!\n");
2227 GNUNET_SCHEDULER_shutdown ();
2230 exit_argv[2] = exit_ifname;
2234 exit_argv[2] = GNUNET_strdup ("%");
2236 if (GNUNET_YES == ipv6_enabled)
2238 if ( (GNUNET_SYSERR ==
2239 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
2241 (1 != inet_pton (AF_INET6, ipv6addr, &v6))) )
2243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2244 "No valid entry 'IPV6ADDR' in configuration!\n");
2245 GNUNET_SCHEDULER_shutdown ();
2248 exit_argv[3] = ipv6addr;
2249 if (GNUNET_SYSERR ==
2250 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
2253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2254 "No entry 'IPV6PREFIX' in configuration!\n");
2255 GNUNET_SCHEDULER_shutdown ();
2258 exit_argv[4] = ipv6prefix_s;
2260 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
2263 (ipv6prefix >= 127) )
2265 GNUNET_SCHEDULER_shutdown ();
2271 /* IPv6 explicitly disabled */
2272 exit_argv[3] = GNUNET_strdup ("-");
2273 exit_argv[4] = GNUNET_strdup ("-");
2275 if (GNUNET_YES == ipv4_enabled)
2277 if ( (GNUNET_SYSERR ==
2278 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
2280 (1 != inet_pton (AF_INET, ipv4addr, &v4))) )
2282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2283 "No valid entry for 'IPV4ADDR' in configuration!\n");
2284 GNUNET_SCHEDULER_shutdown ();
2287 exit_argv[5] = ipv4addr;
2288 if ( (GNUNET_SYSERR ==
2289 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
2291 (1 != inet_pton (AF_INET, ipv4mask, &v4))) )
2293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2294 "No valid entry 'IPV4MASK' in configuration!\n");
2295 GNUNET_SCHEDULER_shutdown ();
2298 exit_argv[6] = ipv4mask;
2302 /* IPv4 explicitly disabled */
2303 exit_argv[5] = GNUNET_strdup ("-");
2304 exit_argv[6] = GNUNET_strdup ("-");
2306 exit_argv[7] = NULL;
2308 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
2309 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
2310 GNUNET_CONFIGURATION_iterate_sections (cfg, &read_service_conf, NULL);
2312 connections_map = GNUNET_CONTAINER_multihashmap_create (65536);
2313 connections_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2315 = GNUNET_MESH_connect (cfg, 42 /* queue size */, NULL,
2317 &clean_tunnel, handlers,
2319 if (NULL == mesh_handle)
2321 GNUNET_SCHEDULER_shutdown ();
2324 helper_handle = GNUNET_HELPER_start ("gnunet-helper-exit",
2326 &message_token, NULL);
2333 * @param argc number of arguments from the command line
2334 * @param argv command line arguments
2335 * @return 0 ok, 1 on error
2338 main (int argc, char *const *argv)
2340 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2341 GNUNET_GETOPT_OPTION_END
2344 return (GNUNET_OK ==
2345 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit",
2347 ("Daemon to run to provide an IP exit node for the VPN"),
2348 options, &run, NULL)) ? 0 : 1;
2352 /* end of gnunet-daemon-exit.c */