2 This file is part of GNUnet.
3 (C) 2010, 2011, 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 vpn/gnunet-service-vpn.c
23 * @brief service that opens a virtual interface and allows its clients
24 * to allocate IPs on the virtual interface and to then redirect
25 * IP traffic received on those IPs via the GNUnet mesh
26 * @author Philipp Toelke
27 * @author Christian Grothoff
32 * - better message queue management (bounded state, drop oldest/RED?)
33 * - actually destroy "stale" tunnels once we have too many!
36 * - add back ICMP support (especially needed for IPv6)
39 * - consider moving IP-header building / checksumming code into shared library
40 * with dns/exit/vpn (libgnunettun_tcpip?)
43 #include "gnunet_util_lib.h"
44 #include "gnunet_common.h"
45 #include "gnunet_protocols.h"
46 #include "gnunet_applications.h"
47 #include "gnunet_mesh_service.h"
48 #include "gnunet_statistics_service.h"
49 #include "gnunet_constants.h"
50 #include "tcpip_tun.h"
56 * State we keep for each of our tunnels.
62 * Information we track for each IP address to determine which tunnel
63 * to send the traffic over to the destination.
65 struct DestinationEntry
69 * Key under which this entry is in the 'destination_map' (only valid
70 * if 'heap_node != NULL'.
75 * Pre-allocated tunnel for this destination, or NULL for none.
77 struct TunnelState *ts;
80 * Entry for this entry in the destination_heap.
82 struct GNUNET_CONTAINER_HeapNode *heap_node;
85 * GNUNET_NO if this is a tunnel to an Internet-exit,
86 * GNUNET_YES if this tunnel is to a service.
91 * Details about the connection (depending on is_service).
99 * The description of the service (only used for service tunnels).
101 GNUNET_HashCode service_descriptor;
104 * Peer offering the service.
106 struct GNUNET_PeerIdentity target;
108 } service_destination;
114 * Address family used (AF_INET or AF_INET6).
119 * IP address of the ultimate destination (only used for exit tunnels).
124 * Address if af is AF_INET.
129 * Address if af is AF_INET6.
142 * A messages we have in queue for a particular tunnel.
144 struct TunnelMessageQueueEntry
147 * This is a doubly-linked list.
149 struct TunnelMessageQueueEntry *next;
152 * This is a doubly-linked list.
154 struct TunnelMessageQueueEntry *prev;
157 * Number of bytes in 'msg'.
162 * Message to transmit, allocated at the end of this struct.
169 * State we keep for each of our tunnels.
174 * Information about the tunnel to use, NULL if no tunnel
175 * is available right now.
177 struct GNUNET_MESH_Tunnel *tunnel;
180 * Active transmission handle, NULL for none.
182 struct GNUNET_MESH_TransmitHandle *th;
185 * Entry for this entry in the tunnel_heap, NULL as long as this
186 * tunnel state is not fully bound.
188 struct GNUNET_CONTAINER_HeapNode *heap_node;
191 * Head of list of messages scheduled for transmission.
193 struct TunnelMessageQueueEntry *head;
196 * Tail of list of messages scheduled for transmission.
198 struct TunnelMessageQueueEntry *tail;
201 * Client that needs to be notified about the tunnel being
202 * up as soon as a peer is connected; NULL for none.
204 struct GNUNET_SERVER_Client *client;
207 * ID of the client request that caused us to setup this entry.
212 * Destination to which this tunnel leads. Note that
213 * this struct is NOT in the destination_map (but a
214 * local copy) and that the 'heap_node' should always
217 struct DestinationEntry destination;
220 * Destination entry that has a pointer to this tunnel state;
221 * NULL if this tunnel state is in the tunnel map.
223 struct DestinationEntry *destination_container;
226 * Addess family used for this tunnel on the local TUN interface.
231 * IPPROTO_TCP or IPPROTO_UDP once bound.
236 * IP address of the source on our end, initially uninitialized.
241 * Address if af is AF_INET.
246 * Address if af is AF_INET6.
253 * Destination IP address used by the source on our end (this is the IP
254 * that we pick freely within the VPN's tunnel IP range).
259 * Address if af is AF_INET.
264 * Address if af is AF_INET6.
271 * Source port used by the sender on our end; 0 for uninitialized.
273 uint16_t source_port;
276 * Destination port used by the sender on our end; 0 for uninitialized.
278 uint16_t destination_port;
284 * Configuration we use.
286 static const struct GNUNET_CONFIGURATION_Handle *cfg;
289 * Handle to the mesh service.
291 static struct GNUNET_MESH_Handle *mesh_handle;
294 * Map from IP address to destination information (possibly with a
295 * MESH tunnel handle for fast setup).
297 static struct GNUNET_CONTAINER_MultiHashMap *destination_map;
300 * Min-Heap sorted by activity time to expire old mappings.
302 static struct GNUNET_CONTAINER_Heap *destination_heap;
305 * Map from source and destination address (IP+port) to connection
306 * information (mostly with the respective MESH tunnel handle).
308 static struct GNUNET_CONTAINER_MultiHashMap *tunnel_map;
311 * Min-Heap sorted by activity time to expire old mappings; values are
312 * of type 'struct TunnelState'.
314 static struct GNUNET_CONTAINER_Heap *tunnel_heap;
319 static struct GNUNET_STATISTICS_Handle *stats;
322 * The handle to the VPN helper process "gnunet-helper-vpn".
324 static struct GNUNET_HELPER_Handle *helper_handle;
327 * Arguments to the vpn helper.
329 static char *vpn_argv[7];
332 * Length of the prefix of the VPN's IPv6 network.
334 static unsigned long long ipv6prefix;
337 * Notification context for sending replies to clients.
339 static struct GNUNET_SERVER_NotificationContext *nc;
342 * If there are more than this number of address-mappings, old ones
345 static unsigned long long max_destination_mappings;
348 * If there are more than this number of open tunnels, old ones
351 static unsigned long long max_tunnel_mappings;
355 * Compute the key under which we would store an entry in the
356 * destination_map for the given IP address.
358 * @param af address family (AF_INET or AF_INET6)
359 * @param address IP address, struct in_addr or struct in6_addr
360 * @param key where to store the key
363 get_destination_key_from_ip (int af,
365 GNUNET_HashCode *key)
370 GNUNET_CRYPTO_hash (address,
371 sizeof (struct in_addr),
375 GNUNET_CRYPTO_hash (address,
376 sizeof (struct in6_addr),
387 * Compute the key under which we would store an entry in the
388 * tunnel_map for the given socket address pair.
390 * @param af address family (AF_INET or AF_INET6)
391 * @param protocol IPPROTO_TCP or IPPROTO_UDP
392 * @param source_ip sender's source IP, struct in_addr or struct in6_addr
393 * @param source_port sender's source port
394 * @param destination_ip sender's destination IP, struct in_addr or struct in6_addr
395 * @param destination_port sender's destination port
396 * @param key where to store the key
399 get_tunnel_key_from_ips (int af,
401 const void *source_ip,
402 uint16_t source_port,
403 const void *destination_ip,
404 uint16_t destination_port,
405 GNUNET_HashCode *key)
409 memset (key, 0, sizeof (GNUNET_HashCode));
410 /* the GNUnet hashmap only uses the first sizeof(unsigned int) of the hash,
411 so we put the ports in there (and hope for few collisions) */
413 memcpy (off, &source_port, sizeof (uint16_t));
414 off += sizeof (uint16_t);
415 memcpy (off, &destination_port, sizeof (uint16_t));
416 off += sizeof (uint16_t);
420 memcpy (off, source_ip, sizeof (struct in_addr));
421 off += sizeof (struct in_addr);
422 memcpy (off, destination_ip, sizeof (struct in_addr));
423 off += sizeof (struct in_addr);
426 memcpy (off, source_ip, sizeof (struct in6_addr));
427 off += sizeof (struct in6_addr);
428 memcpy (off, destination_ip, sizeof (struct in6_addr));
429 off += sizeof (struct in6_addr);
435 memcpy (off, &protocol, sizeof (uint8_t));
436 off += sizeof (uint8_t);
441 * Notify the client about the result of its request.
443 * @param client client to notify
444 * @param request_id original request ID to include in response
445 * @param result_af resulting address family
446 * @param addr resulting IP address
449 send_client_reply (struct GNUNET_SERVER_Client *client,
454 char buf[sizeof (struct RedirectToIpResponseMessage) + sizeof (struct in6_addr)];
455 struct RedirectToIpResponseMessage *res;
461 rlen = sizeof (struct in_addr);
464 rlen = sizeof (struct in6_addr);
473 res = (struct RedirectToIpResponseMessage *) buf;
474 res->header.size = htons (sizeof (struct RedirectToIpResponseMessage) + rlen);
475 res->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP);
476 res->result_af = htonl (result_af);
477 res->request_id = request_id;
478 memcpy (&res[1], addr, rlen);
479 GNUNET_SERVER_notification_context_add (nc, client);
480 GNUNET_SERVER_notification_context_unicast (nc,
488 * Method called whenever a peer has disconnected from the tunnel.
491 * @param peer peer identity the tunnel stopped working with
494 tunnel_peer_disconnect_handler (void *cls,
496 GNUNET_PeerIdentity * peer)
498 struct TunnelState *ts = cls;
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Peer %s disconnected from tunnel.\n",
503 GNUNET_STATISTICS_update (stats,
504 gettext_noop ("# Peers connected to mesh tunnels"),
508 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
511 if (ts->destination.is_service)
512 return; /* hope for reconnect eventually */
513 /* as we are most likely going to change the exit node now,
514 we should just destroy the tunnel entirely... */
515 GNUNET_MESH_tunnel_destroy (ts->tunnel);
520 * Method called whenever a peer has connected to the tunnel. Notifies
521 * the waiting client that the tunnel is now up.
524 * @param peer peer identity the tunnel was created to, NULL on timeout
525 * @param atsi performance data for the connection
528 tunnel_peer_connect_handler (void *cls,
529 const struct GNUNET_PeerIdentity
532 GNUNET_ATS_Information * atsi)
534 struct TunnelState *ts = cls;
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "Peer %s connected to tunnel.\n",
539 GNUNET_STATISTICS_update (stats,
540 gettext_noop ("# Peers connected to mesh tunnels"),
542 if (NULL == ts->client)
543 return; /* nothing to do */
544 send_client_reply (ts->client,
547 &ts->destination_ip);
548 GNUNET_SERVER_client_drop (ts->client);
554 * Send a message from the message queue via mesh.
556 * @param cls the 'struct TunnelState' with the message queue
557 * @param size number of bytes available in buf
558 * @param buf where to copy the message
559 * @return number of bytes copied to buf
562 send_to_peer_notify_callback (void *cls, size_t size, void *buf)
564 struct TunnelState *ts = cls;
565 struct TunnelMessageQueueEntry *tnq;
572 GNUNET_assert (NULL != tnq);
573 GNUNET_assert (size >= tnq->len);
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "Sending %u bytes via mesh tunnel\n",
577 GNUNET_CONTAINER_DLL_remove (ts->head,
580 memcpy (buf, tnq->msg, tnq->len);
583 if (NULL != (tnq = ts->head))
584 ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
585 GNUNET_NO /* cork */,
587 GNUNET_TIME_UNIT_FOREVER_REL,
590 &send_to_peer_notify_callback,
592 GNUNET_STATISTICS_update (stats,
593 gettext_noop ("# Bytes given to mesh for transmission"),
600 * Add the given message to the given tunnel and trigger the
601 * transmission process.
603 * FIXME: bound queue length!
605 * @param tnq message to queue
606 * @param ts tunnel to queue the message for
609 send_to_tunnel (struct TunnelMessageQueueEntry *tnq,
610 struct TunnelState *ts)
612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
613 "Queueing %u bytes for transmission via mesh tunnel\n",
615 GNUNET_CONTAINER_DLL_insert_tail (ts->head,
619 ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
620 GNUNET_NO /* cork */,
622 GNUNET_TIME_UNIT_FOREVER_REL,
625 &send_to_peer_notify_callback,
631 * Initialize the given destination entry's mesh tunnel.
633 * @param de destination entry for which we need to setup a tunnel
634 * @param client client to notify on successful tunnel setup, or NULL for none
635 * @param request_id request ID to send in client notification (unused if client is NULL)
636 * @return tunnel state of the tunnel that was created
638 static struct TunnelState *
639 create_tunnel_to_destination (struct DestinationEntry *de,
640 struct GNUNET_SERVER_Client *client,
643 struct TunnelState *ts;
645 GNUNET_STATISTICS_update (stats,
646 gettext_noop ("# Mesh tunnels created"),
648 GNUNET_assert (NULL == de->ts);
649 ts = GNUNET_malloc (sizeof (struct TunnelState));
652 ts->request_id = request_id;
654 GNUNET_SERVER_client_keep (client);
656 ts->destination = *de;
657 ts->destination.heap_node = NULL; /* copy is NOT in destination heap */
659 ts->destination_container = de; /* we are referenced from de */
660 ts->af = AF_UNSPEC; /* so far, unknown */
661 ts->tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
663 &tunnel_peer_connect_handler,
664 &tunnel_peer_disconnect_handler,
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "Creating tunnel to peer %s offering service %s\n",
670 GNUNET_i2s (&de->details.service_destination.target),
671 GNUNET_h2s (&de->details.service_destination.service_descriptor));
672 GNUNET_MESH_peer_request_connect_add (ts->tunnel,
673 &de->details.service_destination.target);
677 switch (de->details.exit_destination.af)
680 GNUNET_MESH_peer_request_connect_by_type (ts->tunnel,
681 GNUNET_APPLICATION_TYPE_IPV4_GATEWAY);
682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
683 "Creating tunnel to exit peer for %s\n",
687 GNUNET_MESH_peer_request_connect_by_type (ts->tunnel,
688 GNUNET_APPLICATION_TYPE_IPV6_GATEWAY);
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Creating tunnel to exit peer for %s\n",
703 * Route a packet via mesh to the given destination.
705 * @param destination description of the destination
706 * @param af address family on this end (AF_INET or AF_INET6)
707 * @param protocol IPPROTO_TCP or IPPROTO_UDP
708 * @param source_ip source IP used by the sender (struct in_addr or struct in6_addr)
709 * @param destination_ip destination IP used by the sender (struct in_addr or struct in6_addr)
710 * @param payload payload of the packet after the IP header
711 * @param payload_length number of bytes in payload
714 route_packet (struct DestinationEntry *destination,
717 const void *source_ip,
718 const void *destination_ip,
720 size_t payload_length)
723 struct TunnelState *ts;
724 struct TunnelMessageQueueEntry *tnq;
728 const struct udp_packet *udp;
729 const struct tcp_packet *tcp;
737 if (payload_length < sizeof (struct udp_packet))
744 spt = ntohs (udp->spt);
745 dpt = ntohs (udp->dpt);
746 get_tunnel_key_from_ips (af,
757 if (payload_length < sizeof (struct tcp_packet))
764 spt = ntohs (tcp->spt);
765 dpt = ntohs (tcp->dpt);
766 get_tunnel_key_from_ips (af,
776 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
777 _("Protocol %u not supported, dropping\n"),
778 (unsigned int) protocol);
781 if (! destination->is_service)
783 switch (destination->details.exit_destination.af)
786 alen = sizeof (struct in_addr);
789 alen = sizeof (struct in6_addr);
797 char sbuf[INET6_ADDRSTRLEN];
798 char dbuf[INET6_ADDRSTRLEN];
799 char xbuf[INET6_ADDRSTRLEN];
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Routing %s packet from %s:%u -> %s:%u to destination %s:%u\n",
803 (protocol == IPPROTO_TCP) ? "TCP" : "UDP",
804 inet_ntop (af, source_ip, sbuf, sizeof (sbuf)),
806 inet_ntop (af, destination_ip, dbuf, sizeof (dbuf)),
808 inet_ntop (destination->details.exit_destination.af,
809 &destination->details.exit_destination.ip,
810 xbuf, sizeof (xbuf)));
815 /* make compiler happy */
818 char sbuf[INET6_ADDRSTRLEN];
819 char dbuf[INET6_ADDRSTRLEN];
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "Routing %s packet from %s:%u -> %s:%u to service %s at peer %s\n",
823 (protocol == IPPROTO_TCP) ? "TCP" : "UDP",
824 inet_ntop (af, source_ip, sbuf, sizeof (sbuf)),
826 inet_ntop (af, destination_ip, dbuf, sizeof (dbuf)),
828 GNUNET_h2s (&destination->details.service_destination.service_descriptor),
829 GNUNET_i2s (&destination->details.service_destination.target));
834 /* see if we have an existing tunnel for this destination */
835 ts = GNUNET_CONTAINER_multihashmap_get (tunnel_map,
839 /* need to either use the existing tunnel from the destination (if still
840 available) or create a fresh one */
842 if (NULL == destination->ts)
843 ts = create_tunnel_to_destination (destination, NULL, 0);
845 ts = destination->ts;
846 destination->ts = NULL;
847 ts->destination_container = NULL; /* no longer 'contained' */
848 /* now bind existing "unbound" tunnel to our IP/port tuple */
849 ts->protocol = protocol;
853 ts->source_ip.v4 = * (const struct in_addr *) source_ip;
854 ts->destination_ip.v4 = * (const struct in_addr *) destination_ip;
858 ts->source_ip.v6 = * (const struct in6_addr *) source_ip;
859 ts->destination_ip.v6 = * (const struct in6_addr *) destination_ip;
861 ts->source_port = spt;
862 ts->destination_port = dpt;
863 ts->heap_node = GNUNET_CONTAINER_heap_insert (tunnel_heap,
865 GNUNET_TIME_absolute_get ().abs_value);
866 GNUNET_assert (GNUNET_YES ==
867 GNUNET_CONTAINER_multihashmap_put (tunnel_map,
870 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
871 GNUNET_STATISTICS_update (stats,
872 gettext_noop ("# Active tunnels"),
874 /* FIXME: expire OLD tunnels if we have too many! */
879 GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
881 GNUNET_TIME_absolute_get ().abs_value);
884 /* send via tunnel */
888 if (destination->is_service)
890 struct GNUNET_EXIT_UdpServiceMessage *usm;
892 mlen = sizeof (struct GNUNET_EXIT_UdpServiceMessage) +
893 payload_length - sizeof (struct udp_packet);
894 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
899 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
901 usm = (struct GNUNET_EXIT_UdpServiceMessage *) &tnq[1];
902 usm->header.size = htons ((uint16_t) mlen);
903 usm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_UDP_TO_SERVICE);
904 /* if the source port is below 32000, we assume it has a special
905 meaning; if not, we pick a random port (this is a heuristic) */
906 usm->source_port = (ntohs (udp->spt) < 32000) ? udp->spt : 0;
907 usm->destination_port = udp->dpt;
908 usm->service_descriptor = destination->details.service_destination.service_descriptor;
911 payload_length - sizeof (struct udp_packet));
915 struct GNUNET_EXIT_UdpInternetMessage *uim;
916 struct in_addr *ip4dst;
917 struct in6_addr *ip6dst;
920 mlen = sizeof (struct GNUNET_EXIT_UdpInternetMessage) +
921 alen + payload_length - sizeof (struct udp_packet);
922 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
927 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) +
930 uim = (struct GNUNET_EXIT_UdpInternetMessage *) &tnq[1];
931 uim->header.size = htons ((uint16_t) mlen);
932 uim->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_UDP_TO_INTERNET);
933 uim->af = htonl (destination->details.exit_destination.af);
934 uim->source_port = (ntohs (udp->spt) < 32000) ? udp->spt : 0;
935 uim->destination_port = udp->dpt;
936 switch (destination->details.exit_destination.af)
939 ip4dst = (struct in_addr *) &uim[1];
940 *ip4dst = destination->details.exit_destination.ip.v4;
941 payload = &ip4dst[1];
944 ip6dst = (struct in6_addr *) &uim[1];
945 *ip6dst = destination->details.exit_destination.ip.v6;
946 payload = &ip6dst[1];
953 payload_length - sizeof (struct udp_packet));
959 if (destination->is_service)
961 struct GNUNET_EXIT_TcpServiceStartMessage *tsm;
963 mlen = sizeof (struct GNUNET_EXIT_TcpServiceStartMessage) +
964 payload_length - sizeof (struct tcp_packet);
965 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
970 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
972 tsm = (struct GNUNET_EXIT_TcpServiceStartMessage *) &tnq[1];
973 tsm->header.size = htons ((uint16_t) mlen);
974 tsm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_TO_SERVICE_START);
975 tsm->reserved = htonl (0);
976 tsm->service_descriptor = destination->details.service_destination.service_descriptor;
977 tsm->tcp_header = *tcp;
980 payload_length - sizeof (struct tcp_packet));
984 struct GNUNET_EXIT_TcpInternetStartMessage *tim;
985 struct in_addr *ip4dst;
986 struct in6_addr *ip6dst;
989 mlen = sizeof (struct GNUNET_EXIT_TcpInternetStartMessage) +
990 alen + payload_length - sizeof (struct tcp_packet);
991 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
996 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
998 tim = (struct GNUNET_EXIT_TcpInternetStartMessage *) &tnq[1];
999 tim->header.size = htons ((uint16_t) mlen);
1000 tim->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_TO_INTERNET_START);
1001 tim->af = htonl (destination->details.exit_destination.af);
1002 tim->tcp_header = *tcp;
1003 switch (destination->details.exit_destination.af)
1006 ip4dst = (struct in_addr *) &tim[1];
1007 *ip4dst = destination->details.exit_destination.ip.v4;
1008 payload = &ip4dst[1];
1011 ip6dst = (struct in6_addr *) &tim[1];
1012 *ip6dst = destination->details.exit_destination.ip.v6;
1013 payload = &ip6dst[1];
1020 payload_length - sizeof (struct tcp_packet));
1025 struct GNUNET_EXIT_TcpDataMessage *tdm;
1027 mlen = sizeof (struct GNUNET_EXIT_TcpDataMessage) +
1028 alen + payload_length - sizeof (struct tcp_packet);
1029 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1034 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
1036 tdm = (struct GNUNET_EXIT_TcpDataMessage *) &tnq[1];
1037 tdm->header.size = htons ((uint16_t) mlen);
1038 tdm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_DATA);
1039 tdm->reserved = htonl (0);
1040 tdm->tcp_header = *tcp;
1043 payload_length - sizeof (struct tcp_packet));
1047 /* not supported above, how can we get here !? */
1051 send_to_tunnel (tnq, ts);
1056 * Receive packets from the helper-process (someone send to the local
1057 * virtual tunnel interface). Find the destination mapping, and if it
1058 * exists, identify the correct MESH tunnel (or possibly create it)
1059 * and forward the packet.
1061 * @param cls closure, NULL
1062 * @param client NULL
1063 * @param message message we got from the client (VPN tunnel interface)
1066 message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
1067 const struct GNUNET_MessageHeader *message)
1069 const struct tun_header *tun;
1071 GNUNET_HashCode key;
1072 struct DestinationEntry *de;
1074 GNUNET_STATISTICS_update (stats,
1075 gettext_noop ("# Packets received from TUN interface"),
1077 mlen = ntohs (message->size);
1078 if ( (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) ||
1079 (mlen < sizeof (struct GNUNET_MessageHeader) + sizeof (struct tun_header)) )
1084 tun = (const struct tun_header *) &message[1];
1085 mlen -= (sizeof (struct GNUNET_MessageHeader) + sizeof (struct tun_header));
1086 switch (ntohs (tun->proto))
1090 const struct ip6_header *pkt6;
1092 if (mlen < sizeof (struct ip6_header))
1098 pkt6 = (const struct ip6_header *) &tun[1];
1099 get_destination_key_from_ip (AF_INET6,
1100 &pkt6->destination_address,
1102 de = GNUNET_CONTAINER_multihashmap_get (destination_map, &key);
1103 /* FIXME: do we need to guard against hash collision?
1104 (if so, we need to also store the local destination IP in the
1105 destination entry and then compare here; however, the risk
1106 of collision seems minimal AND the impact is unlikely to be
1107 super-problematic as well... */
1110 char buf[INET6_ADDRSTRLEN];
1112 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1113 _("Packet received for unmapped destination `%s' (dropping it)\n"),
1114 inet_ntop (AF_INET6,
1115 &pkt6->destination_address,
1123 &pkt6->source_address,
1124 &pkt6->destination_address,
1126 mlen - sizeof (struct ip6_header));
1131 struct ip4_header *pkt4;
1133 if (mlen < sizeof (struct ip4_header))
1139 pkt4 = (struct ip4_header *) &tun[1];
1140 get_destination_key_from_ip (AF_INET,
1141 &pkt4->destination_address,
1143 de = GNUNET_CONTAINER_multihashmap_get (destination_map, &key);
1144 /* FIXME: do we need to guard against hash collision?
1145 (if so, we need to also store the local destination IP in the
1146 destination entry and then compare here; however, the risk
1147 of collision seems minimal AND the impact is unlikely to be
1148 super-problematic as well... */
1151 char buf[INET_ADDRSTRLEN];
1153 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1154 _("Packet received for unmapped destination `%s' (dropping it)\n"),
1156 &pkt4->destination_address,
1161 if (pkt4->header_length * 4 != sizeof (struct ip4_header))
1163 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1164 _("Received IPv4 packet with options (dropping it)\n"));
1170 &pkt4->source_address,
1171 &pkt4->destination_address,
1173 mlen - sizeof (struct ip4_header));
1177 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1178 _("Received packet of unknown protocol %d from TUN (dropping it)\n"),
1179 (unsigned int) ntohs (tun->proto));
1186 * We got a UDP packet back from the MESH tunnel. Pass it on to the
1187 * local virtual interface via the helper.
1189 * @param cls closure, NULL
1190 * @param tunnel connection to the other end
1191 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1192 * @param sender who sent the message
1193 * @param message the actual message
1194 * @param atsi performance data for the connection
1195 * @return GNUNET_OK to keep the connection open,
1196 * GNUNET_SYSERR to close it (signal serious error)
1199 receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1200 void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
1201 const struct GNUNET_MessageHeader *message,
1202 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1204 struct TunnelState *ts = *tunnel_ctx;
1205 const struct GNUNET_EXIT_UdpReplyMessage *reply;
1208 GNUNET_STATISTICS_update (stats,
1209 gettext_noop ("# UDP packets received from mesh"),
1211 mlen = ntohs (message->size);
1212 if (mlen < sizeof (struct GNUNET_EXIT_UdpReplyMessage))
1214 GNUNET_break_op (0);
1215 return GNUNET_SYSERR;
1217 if (NULL == ts->heap_node)
1219 GNUNET_break_op (0);
1220 return GNUNET_SYSERR;
1222 if (AF_UNSPEC == ts->af)
1224 GNUNET_break_op (0);
1225 return GNUNET_SYSERR;
1227 reply = (const struct GNUNET_EXIT_UdpReplyMessage *) message;
1228 mlen -= sizeof (struct GNUNET_EXIT_UdpReplyMessage);
1230 char sbuf[INET6_ADDRSTRLEN];
1231 char dbuf[INET6_ADDRSTRLEN];
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 "Received UDP reply from mesh, sending %u bytes from %s:%u -> %s:%u via TUN\n",
1235 (unsigned int) mlen,
1236 inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
1237 ts->destination_port,
1238 inet_ntop (ts->af, &ts->source_ip, dbuf, sizeof (dbuf)),
1245 size_t size = sizeof (struct ip4_header)
1246 + sizeof (struct udp_packet)
1247 + sizeof (struct GNUNET_MessageHeader) +
1248 sizeof (struct tun_header) +
1252 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1253 struct tun_header *tun = (struct tun_header*) &msg[1];
1254 struct ip4_header *ipv4 = (struct ip4_header *) &tun[1];
1255 struct udp_packet *udp = (struct udp_packet *) &ipv4[1];
1256 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1257 msg->size = htons (size);
1258 tun->flags = htons (0);
1259 tun->proto = htons (ETH_P_IPV4);
1261 ipv4->header_length = sizeof (struct ip4_header) / 4;
1262 ipv4->diff_serv = 0;
1263 ipv4->total_length = htons (sizeof (struct ip4_header) +
1264 sizeof (struct udp_packet) +
1266 ipv4->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1269 ipv4->fragmentation_offset = 0;
1271 ipv4->protocol = IPPROTO_UDP;
1273 ipv4->source_address = ts->destination_ip.v4;
1274 ipv4->destination_address = ts->source_ip.v4;
1276 GNUNET_CRYPTO_crc16_n (ipv4, sizeof (struct ip4_header));
1277 if (0 == ntohs (reply->source_port))
1278 udp->spt = htons (ts->destination_port);
1280 udp->spt = reply->source_port;
1281 if (0 == ntohs (reply->destination_port))
1282 udp->dpt = htons (ts->source_port);
1284 udp->dpt = reply->destination_port;
1285 udp->len = htons (mlen + sizeof (struct udp_packet));
1286 udp->crc = 0; // FIXME: optional, but we might want to calculate this one anyway
1290 (void) GNUNET_HELPER_send (helper_handle,
1299 size_t size = sizeof (struct ip6_header)
1300 + sizeof (struct udp_packet)
1301 + sizeof (struct GNUNET_MessageHeader) +
1302 sizeof (struct tun_header) +
1306 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1307 struct tun_header *tun = (struct tun_header*) &msg[1];
1308 struct ip6_header *ipv6 = (struct ip6_header *) &tun[1];
1309 struct udp_packet *udp = (struct udp_packet *) &ipv6[1];
1310 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1311 msg->size = htons (size);
1312 tun->flags = htons (0);
1313 tun->proto = htons (ETH_P_IPV6);
1314 ipv6->traffic_class_h = 0;
1316 ipv6->traffic_class_l = 0;
1317 ipv6->flow_label = 0;
1318 ipv6->payload_length = htons (sizeof (struct udp_packet) + sizeof (struct ip6_header) + mlen);
1319 ipv6->next_header = IPPROTO_UDP;
1320 ipv6->hop_limit = 255;
1321 ipv6->source_address = ts->destination_ip.v6;
1322 ipv6->destination_address = ts->source_ip.v6;
1323 if (0 == ntohs (reply->source_port))
1324 udp->spt = htons (ts->destination_port);
1326 udp->spt = reply->source_port;
1327 if (0 == ntohs (reply->destination_port))
1328 udp->dpt = htons (ts->source_port);
1330 udp->dpt = reply->destination_port;
1331 udp->len = htons (mlen + sizeof (struct udp_packet));
1339 GNUNET_CRYPTO_crc16_step (sum, &ipv6->source_address,
1340 sizeof (struct in6_addr) * 2);
1341 uint32_t tmp = udp->len;
1342 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1343 tmp = htons (IPPROTO_UDP);
1344 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1345 sum = GNUNET_CRYPTO_crc16_step (sum,
1348 udp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1350 (void) GNUNET_HELPER_send (helper_handle,
1360 GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
1362 GNUNET_TIME_absolute_get ().abs_value);
1368 * We got a TCP packet back from the MESH tunnel. Pass it on to the
1369 * local virtual interface via the helper.
1371 * @param cls closure, NULL
1372 * @param tunnel connection to the other end
1373 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1374 * @param sender who sent the message
1375 * @param message the actual message
1376 * @param atsi performance data for the connection
1377 * @return GNUNET_OK to keep the connection open,
1378 * GNUNET_SYSERR to close it (signal serious error)
1381 receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1383 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1384 const struct GNUNET_MessageHeader *message,
1385 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1387 struct TunnelState *ts = *tunnel_ctx;
1388 const struct GNUNET_EXIT_TcpDataMessage *data;
1391 GNUNET_STATISTICS_update (stats,
1392 gettext_noop ("# TCP packets received from mesh"),
1394 mlen = ntohs (message->size);
1395 if (mlen < sizeof (struct GNUNET_EXIT_TcpDataMessage))
1397 GNUNET_break_op (0);
1398 return GNUNET_SYSERR;
1400 if (NULL == ts->heap_node)
1402 GNUNET_break_op (0);
1403 return GNUNET_SYSERR;
1405 data = (const struct GNUNET_EXIT_TcpDataMessage *) message;
1406 mlen -= sizeof (struct GNUNET_EXIT_TcpDataMessage);
1408 char sbuf[INET6_ADDRSTRLEN];
1409 char dbuf[INET6_ADDRSTRLEN];
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "Received TCP reply from mesh, sending %u bytes from %s:%u -> %s:%u via TUN\n",
1413 (unsigned int) mlen,
1414 inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
1415 ts->destination_port,
1416 inet_ntop (ts->af, &ts->source_ip, dbuf, sizeof (dbuf)),
1423 size_t size = sizeof (struct ip4_header)
1424 + sizeof (struct tcp_packet)
1425 + sizeof (struct GNUNET_MessageHeader) +
1426 sizeof (struct tun_header) +
1430 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1431 struct tun_header *tun = (struct tun_header*) &msg[1];
1432 struct ip4_header *ipv4 = (struct ip4_header *) &tun[1];
1433 struct tcp_packet *tcp = (struct tcp_packet *) &ipv4[1];
1434 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1435 msg->size = htons (size);
1436 tun->flags = htons (0);
1437 tun->proto = htons (ETH_P_IPV4);
1439 ipv4->header_length = sizeof (struct ip4_header) / 4;
1440 ipv4->diff_serv = 0;
1441 ipv4->total_length = htons (sizeof (struct ip4_header) +
1442 sizeof (struct tcp_packet) +
1444 ipv4->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1447 ipv4->fragmentation_offset = 0;
1449 ipv4->protocol = IPPROTO_TCP;
1451 ipv4->source_address = ts->destination_ip.v4;
1452 ipv4->destination_address = ts->source_ip.v4;
1454 GNUNET_CRYPTO_crc16_n (ipv4, sizeof (struct ip4_header));
1455 *tcp = data->tcp_header;
1456 tcp->spt = htons (ts->destination_port);
1457 tcp->dpt = htons (ts->source_port);
1466 sum = GNUNET_CRYPTO_crc16_step (sum,
1467 &ipv4->source_address,
1468 2 * sizeof (struct in_addr));
1469 tmp = htonl ((IPPROTO_TCP << 16) | (mlen + sizeof (struct tcp_packet)));
1470 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1471 sum = GNUNET_CRYPTO_crc16_step (sum, tcp, mlen + sizeof (struct tcp_packet));
1472 tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1474 (void) GNUNET_HELPER_send (helper_handle,
1483 size_t size = sizeof (struct ip6_header)
1484 + sizeof (struct tcp_packet)
1485 + sizeof (struct GNUNET_MessageHeader) +
1486 sizeof (struct tun_header) +
1490 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1491 struct tun_header *tun = (struct tun_header*) &msg[1];
1492 struct ip6_header *ipv6 = (struct ip6_header *) &tun[1];
1493 struct tcp_packet *tcp = (struct tcp_packet *) &ipv6[1];
1494 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1495 msg->size = htons (size);
1496 tun->flags = htons (0);
1497 tun->proto = htons (ETH_P_IPV6);
1498 ipv6->traffic_class_h = 0;
1500 ipv6->traffic_class_l = 0;
1501 ipv6->flow_label = 0;
1502 ipv6->payload_length = htons (sizeof (struct tcp_packet) + sizeof (struct ip6_header) + mlen);
1503 ipv6->next_header = IPPROTO_TCP;
1504 ipv6->hop_limit = 255;
1505 ipv6->source_address = ts->destination_ip.v6;
1506 ipv6->destination_address = ts->source_ip.v6;
1507 tcp->spt = htons (ts->destination_port);
1508 tcp->dpt = htons (ts->source_port);
1514 sum = GNUNET_CRYPTO_crc16_step (sum, &ipv6->source_address, 2 * sizeof (struct in6_addr));
1515 tmp = htonl (sizeof (struct tcp_packet) + mlen);
1516 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1517 tmp = htonl (IPPROTO_TCP);
1518 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1519 sum = GNUNET_CRYPTO_crc16_step (sum, tcp,
1520 sizeof (struct tcp_packet) + mlen);
1521 tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1523 (void) GNUNET_HELPER_send (helper_handle,
1531 GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
1533 GNUNET_TIME_absolute_get ().abs_value);
1539 * Allocate an IPv4 address from the range of the tunnel
1540 * for a new redirection.
1542 * @param v4 where to store the address
1543 * @return GNUNET_OK on success,
1544 * GNUNET_SYSERR on error
1547 allocate_v4_address (struct in_addr *v4)
1549 const char *ipv4addr = vpn_argv[4];
1550 const char *ipv4mask = vpn_argv[5];
1551 struct in_addr addr;
1552 struct in_addr mask;
1554 GNUNET_HashCode key;
1557 GNUNET_assert (1 == inet_pton (AF_INET, ipv4addr, &addr));
1558 GNUNET_assert (1 == inet_pton (AF_INET, ipv4mask, &mask));
1559 /* Given 192.168.0.1/255.255.0.0, we want a mask
1560 of '192.168.255.255', thus: */
1561 mask.s_addr = addr.s_addr | ~mask.s_addr;
1568 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1569 _("Failed to find unallocated IPv4 address in VPN's range\n"));
1570 return GNUNET_SYSERR;
1572 /* Pick random IPv4 address within the subnet, except 'addr' or 'mask' itself */
1573 rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1575 v4->s_addr = (addr.s_addr | rnd.s_addr) & mask.s_addr;
1576 get_destination_key_from_ip (AF_INET,
1580 while ( (GNUNET_YES ==
1581 GNUNET_CONTAINER_multihashmap_contains (destination_map,
1583 (v4->s_addr == addr.s_addr) ||
1584 (v4->s_addr == mask.s_addr) );
1590 * Allocate an IPv6 address from the range of the tunnel
1591 * for a new redirection.
1593 * @param v6 where to store the address
1594 * @return GNUNET_OK on success,
1595 * GNUNET_SYSERR on error
1598 allocate_v6_address (struct in6_addr *v6)
1600 const char *ipv6addr = vpn_argv[2];
1601 struct in6_addr addr;
1602 struct in6_addr mask;
1603 struct in6_addr rnd;
1605 GNUNET_HashCode key;
1608 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6addr, &addr));
1609 GNUNET_assert (ipv6prefix < 128);
1610 /* Given ABCD::/96, we want a mask of 'ABCD::FFFF:FFFF,
1613 for (i=127;i>=128-ipv6prefix;i--)
1614 mask.s6_addr[i / 8] |= (1 << (i % 8));
1616 /* Pick random IPv6 address within the subnet, except 'addr' or 'mask' itself */
1623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1624 _("Failed to find unallocated IPv6 address in VPN's range\n"));
1625 return GNUNET_SYSERR;
1630 rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1633 = (addr.s6_addr[i] | rnd.s6_addr[i]) & mask.s6_addr[i];
1635 get_destination_key_from_ip (AF_INET6,
1639 while ( (GNUNET_YES ==
1640 GNUNET_CONTAINER_multihashmap_contains (destination_map,
1644 sizeof (struct in6_addr))) ||
1647 sizeof (struct in6_addr))) );
1653 * A client asks us to setup a redirection via some exit
1654 * node to a particular IP. Setup the redirection and
1655 * give the client the allocated IP.
1658 * @param client requesting client
1659 * @param message redirection request (a 'struct RedirectToIpRequestMessage')
1662 service_redirect_to_ip (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
1663 const struct GNUNET_MessageHeader *message)
1667 const struct RedirectToIpRequestMessage *msg;
1673 struct DestinationEntry *de;
1674 GNUNET_HashCode key;
1676 /* validate and parse request */
1677 mlen = ntohs (message->size);
1678 if (mlen < sizeof (struct RedirectToIpRequestMessage))
1681 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1684 alen = mlen - sizeof (struct RedirectToIpRequestMessage);
1685 msg = (const struct RedirectToIpRequestMessage *) message;
1686 addr_af = (int) htonl (msg->addr_af);
1690 if (alen != sizeof (struct in_addr))
1693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1698 if (alen != sizeof (struct in6_addr))
1701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1711 /* allocate response IP */
1713 result_af = (int) htonl (msg->result_af);
1718 allocate_v4_address (&v4))
1719 result_af = AF_UNSPEC;
1725 allocate_v6_address (&v6))
1726 result_af = AF_UNSPEC;
1732 allocate_v4_address (&v4))
1735 result_af = AF_INET;
1737 else if (GNUNET_OK ==
1738 allocate_v6_address (&v6))
1741 result_af = AF_INET6;
1746 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1749 if ( (result_af == AF_UNSPEC) ||
1750 (GNUNET_NO == ntohl (msg->nac)) )
1752 /* send reply "instantly" */
1753 send_client_reply (client,
1758 if (result_af == AF_UNSPEC)
1760 /* failure, we're done */
1761 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1766 char sbuf[INET6_ADDRSTRLEN];
1767 char dbuf[INET6_ADDRSTRLEN];
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 "Allocated address %s for redirection via exit to %s\n",
1771 inet_ntop (result_af, addr, sbuf, sizeof (sbuf)),
1773 &msg[1], dbuf, sizeof (dbuf)));
1776 /* setup destination record */
1777 de = GNUNET_malloc (sizeof (struct DestinationEntry));
1778 de->is_service = GNUNET_NO;
1779 de->details.exit_destination.af = addr_af;
1780 memcpy (&de->details.exit_destination.ip,
1783 get_destination_key_from_ip (result_af,
1787 GNUNET_assert (GNUNET_OK ==
1788 GNUNET_CONTAINER_multihashmap_put (destination_map,
1791 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1792 de->heap_node = GNUNET_CONTAINER_heap_insert (destination_heap,
1794 GNUNET_TIME_absolute_ntoh (msg->expiration_time).abs_value);
1795 GNUNET_STATISTICS_update (stats,
1796 gettext_noop ("# Active destinations"),
1799 /* FIXME: expire OLD destinations if we have too many! */
1800 /* setup tunnel to destination */
1801 (void) create_tunnel_to_destination (de,
1802 (GNUNET_NO == ntohl (msg->nac)) ? NULL : client,
1804 GNUNET_assert (NULL != de->ts);
1806 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1811 * A client asks us to setup a redirection to a particular peer
1812 * offering a service. Setup the redirection and give the client the
1816 * @param client requesting client
1817 * @param message redirection request (a 'struct RedirectToPeerRequestMessage')
1820 service_redirect_to_service (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
1821 const struct GNUNET_MessageHeader *message)
1823 const struct RedirectToServiceRequestMessage *msg;
1828 struct DestinationEntry *de;
1829 GNUNET_HashCode key;
1832 msg = (const struct RedirectToServiceRequestMessage *) message;
1834 /* allocate response IP */
1836 result_af = (int) htonl (msg->result_af);
1841 allocate_v4_address (&v4))
1842 result_af = AF_UNSPEC;
1848 allocate_v6_address (&v6))
1849 result_af = AF_UNSPEC;
1855 allocate_v4_address (&v4))
1858 result_af = AF_INET;
1860 else if (GNUNET_OK ==
1861 allocate_v6_address (&v6))
1864 result_af = AF_INET6;
1869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1872 if ( (result_af == AF_UNSPEC) ||
1873 (GNUNET_NO == ntohl (msg->nac)) )
1875 /* send reply "instantly" */
1876 send_client_reply (client,
1881 if (result_af == AF_UNSPEC)
1883 /* failure, we're done */
1884 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1885 _("Failed to allocate IP address for new destination\n"));
1886 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1891 char sbuf[INET6_ADDRSTRLEN];
1893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1894 "Allocated address %s for redirection to service %s on peer %s\n",
1895 inet_ntop (result_af, addr, sbuf, sizeof (sbuf)),
1896 GNUNET_h2s (&msg->service_descriptor),
1897 GNUNET_i2s (&msg->target));
1900 /* setup destination record */
1901 de = GNUNET_malloc (sizeof (struct DestinationEntry));
1902 de->is_service = GNUNET_YES;
1903 de->details.service_destination.service_descriptor = msg->service_descriptor;
1904 de->details.service_destination.target = msg->target;
1905 get_destination_key_from_ip (result_af,
1909 GNUNET_assert (GNUNET_OK ==
1910 GNUNET_CONTAINER_multihashmap_put (destination_map,
1913 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1914 de->heap_node = GNUNET_CONTAINER_heap_insert (destination_heap,
1916 GNUNET_TIME_absolute_ntoh (msg->expiration_time).abs_value);
1917 /* FIXME: expire OLD destinations if we have too many! */
1918 (void) create_tunnel_to_destination (de,
1919 (GNUNET_NO == ntohl (msg->nac)) ? NULL : client,
1922 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1928 * Function called for inbound tunnels. As we don't offer
1929 * any mesh services, this function should never be called.
1931 * @param cls closure
1932 * @param tunnel new handle to the tunnel
1933 * @param initiator peer that started the tunnel
1934 * @param atsi performance information for the tunnel
1935 * @return initial tunnel context for the tunnel
1936 * (can be NULL -- that's not an error)
1939 inbound_tunnel_cb (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
1940 const struct GNUNET_PeerIdentity *initiator,
1941 const struct GNUNET_ATS_Information *atsi)
1943 /* How can and why should anyone open an inbound tunnel to vpn? */
1950 * Free resources associated with a tunnel state.
1952 * @param ts state to free
1955 free_tunnel_state (struct TunnelState *ts)
1957 GNUNET_HashCode key;
1958 struct TunnelMessageQueueEntry *tnq;
1960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1961 "Cleaning up tunnel state\n");
1962 GNUNET_STATISTICS_update (stats,
1963 gettext_noop ("# Active tunnels"),
1965 while (NULL != (tnq = ts->head))
1967 GNUNET_CONTAINER_DLL_remove (ts->head,
1972 if (NULL != ts->client)
1974 GNUNET_SERVER_client_drop (ts->client);
1979 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
1982 GNUNET_assert (NULL == ts->destination.heap_node);
1983 if (NULL != ts->tunnel)
1985 GNUNET_MESH_tunnel_destroy (ts->tunnel);
1988 if (NULL != ts->heap_node)
1990 GNUNET_CONTAINER_heap_remove_node (ts->heap_node);
1991 ts->heap_node = NULL;
1992 get_tunnel_key_from_ips (ts->af,
1996 &ts->destination_ip,
1997 ts->destination_port,
1999 GNUNET_assert (GNUNET_YES ==
2000 GNUNET_CONTAINER_multihashmap_remove (tunnel_map,
2004 if (NULL != ts->destination_container)
2006 GNUNET_assert (ts == ts->destination_container->ts);
2007 ts->destination_container->ts = NULL;
2008 ts->destination_container = NULL;
2015 * Free resources occupied by a destination entry.
2017 * @param de entry to free
2020 free_destination_entry (struct DestinationEntry *de)
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2023 "Cleaning up destination entry\n");
2024 GNUNET_STATISTICS_update (stats,
2025 gettext_noop ("# Active destinations"),
2029 free_tunnel_state (de->ts);
2030 GNUNET_assert (NULL == de->ts);
2032 if (NULL != de->heap_node)
2034 GNUNET_CONTAINER_heap_remove_node (de->heap_node);
2035 de->heap_node = NULL;
2036 GNUNET_assert (GNUNET_YES ==
2037 GNUNET_CONTAINER_multihashmap_remove (destination_map,
2046 * Function called whenever an inbound tunnel is destroyed. Should clean up
2047 * any associated state.
2049 * @param cls closure (set from GNUNET_MESH_connect)
2050 * @param tunnel connection to the other end (henceforth invalid)
2051 * @param tunnel_ctx place where local state associated
2052 * with the tunnel is stored (our 'struct TunnelState')
2055 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
2057 struct TunnelState *ts = tunnel_ctx;
2064 GNUNET_assert (ts->tunnel == tunnel);
2066 free_tunnel_state (ts);
2071 * Free memory occupied by an entry in the destination map.
2075 * @param value a 'struct DestinationEntry *'
2076 * @return GNUNET_OK (continue to iterate)
2079 cleanup_destination (void *cls,
2080 const GNUNET_HashCode *key,
2083 struct DestinationEntry *de = value;
2085 free_destination_entry (de);
2091 * Free memory occupied by an entry in the tunnel map.
2095 * @param value a 'struct TunnelState *'
2096 * @return GNUNET_OK (continue to iterate)
2099 cleanup_tunnel (void *cls,
2100 const GNUNET_HashCode *key,
2103 struct TunnelState *ts = value;
2105 free_tunnel_state (ts);
2111 * Function scheduled as very last function, cleans up after us
2117 cleanup (void *cls GNUNET_UNUSED,
2118 const struct GNUNET_SCHEDULER_TaskContext *tc)
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 "VPN is shutting down\n");
2124 if (NULL != destination_map)
2126 GNUNET_CONTAINER_multihashmap_iterate (destination_map,
2127 &cleanup_destination,
2129 GNUNET_CONTAINER_multihashmap_destroy (destination_map);
2130 destination_map = NULL;
2132 if (NULL != destination_heap)
2134 GNUNET_CONTAINER_heap_destroy (destination_heap);
2135 destination_heap = NULL;
2137 if (NULL != tunnel_map)
2139 GNUNET_CONTAINER_multihashmap_iterate (tunnel_map,
2142 GNUNET_CONTAINER_multihashmap_destroy (tunnel_map);
2145 if (NULL != tunnel_heap)
2147 GNUNET_CONTAINER_heap_destroy (tunnel_heap);
2150 if (NULL != mesh_handle)
2152 GNUNET_MESH_disconnect (mesh_handle);
2155 if (NULL != helper_handle)
2157 GNUNET_HELPER_stop (helper_handle);
2158 helper_handle = NULL;
2162 GNUNET_SERVER_notification_context_destroy (nc);
2167 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
2171 GNUNET_free_non_null (vpn_argv[i]);
2176 * A client disconnected, clean up all references to it.
2178 * @param cls the client that disconnected
2180 * @param value a 'struct TunnelState *'
2181 * @return GNUNET_OK (continue to iterate)
2184 cleanup_tunnel_client (void *cls,
2185 const GNUNET_HashCode *key,
2188 struct GNUNET_SERVER_Client *client = cls;
2189 struct TunnelState *ts = value;
2191 if (client == ts->client)
2193 GNUNET_SERVER_client_drop (ts->client);
2201 * A client disconnected, clean up all references to it.
2203 * @param cls the client that disconnected
2205 * @param value a 'struct DestinationEntry *'
2206 * @return GNUNET_OK (continue to iterate)
2209 cleanup_destination_client (void *cls,
2210 const GNUNET_HashCode *key,
2213 struct GNUNET_SERVER_Client *client = cls;
2214 struct DestinationEntry *de = value;
2215 struct TunnelState *ts;
2217 if (NULL == (ts = de->ts))
2219 if (client == ts->client)
2221 GNUNET_SERVER_client_drop (ts->client);
2229 * A client has disconnected from us. If we are currently building
2230 * a tunnel for it, cancel the operation.
2233 * @param client handle to the client that disconnected
2236 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2238 if (NULL != tunnel_map)
2239 GNUNET_CONTAINER_multihashmap_iterate (tunnel_map,
2240 &cleanup_tunnel_client,
2242 if (NULL != destination_map)
2243 GNUNET_CONTAINER_multihashmap_iterate (destination_map,
2244 &cleanup_destination_client,
2250 * Main function that will be run by the scheduler.
2252 * @param cls closure
2253 * @param server the initialized server
2254 * @param cfg_ configuration
2258 struct GNUNET_SERVER_Handle *server,
2259 const struct GNUNET_CONFIGURATION_Handle *cfg_)
2261 static const struct GNUNET_SERVER_MessageHandler service_handlers[] = {
2262 /* callback, cls, type, size */
2263 {&service_redirect_to_ip, NULL, GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_IP, 0},
2264 {&service_redirect_to_service, NULL,
2265 GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_SERVICE,
2266 sizeof (struct RedirectToServiceRequestMessage) },
2269 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
2270 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
2271 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
2272 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
2273 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
2276 static const GNUNET_MESH_ApplicationType types[] = {
2277 GNUNET_APPLICATION_TYPE_END
2288 stats = GNUNET_STATISTICS_create ("vpn", cfg);
2290 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPING",
2291 &max_destination_mappings))
2292 max_destination_mappings = 200;
2294 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_TUNNELS",
2295 &max_tunnel_mappings))
2296 max_tunnel_mappings = 200;
2298 destination_map = GNUNET_CONTAINER_multihashmap_create (max_destination_mappings * 2);
2299 destination_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2300 tunnel_map = GNUNET_CONTAINER_multihashmap_create (max_tunnel_mappings * 2);
2301 tunnel_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2304 vpn_argv[0] = GNUNET_strdup ("vpn-gnunet");
2305 if (GNUNET_SYSERR ==
2306 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IFNAME", &ifname))
2308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2309 "No entry 'IFNAME' in configuration!\n");
2310 GNUNET_SCHEDULER_shutdown ();
2313 vpn_argv[1] = ifname;
2314 if ( (GNUNET_SYSERR ==
2315 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
2317 (1 != inet_pton (AF_INET6, ipv6addr, &v6))) )
2319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2320 "No valid entry 'IPV6ADDR' in configuration!\n");
2321 GNUNET_SCHEDULER_shutdown ();
2324 vpn_argv[2] = ipv6addr;
2325 if (GNUNET_SYSERR ==
2326 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6PREFIX",
2329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2330 "No entry 'IPV6PREFIX' in configuration!\n");
2331 GNUNET_SCHEDULER_shutdown ();
2334 vpn_argv[3] = ipv6prefix_s;
2336 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
2339 (ipv6prefix >= 127) )
2341 GNUNET_SCHEDULER_shutdown ();
2345 if ( (GNUNET_SYSERR ==
2346 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
2348 (1 != inet_pton (AF_INET, ipv4addr, &v4))) )
2350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2351 "No valid entry for 'IPV4ADDR' in configuration!\n");
2352 GNUNET_SCHEDULER_shutdown ();
2355 vpn_argv[4] = ipv4addr;
2356 if ( (GNUNET_SYSERR ==
2357 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK",
2359 (1 != inet_pton (AF_INET, ipv4mask, &v4))) )
2361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2362 "No valid entry 'IPV4MASK' in configuration!\n");
2363 GNUNET_SCHEDULER_shutdown ();
2366 vpn_argv[5] = ipv4mask;
2370 GNUNET_MESH_connect (cfg_, 42 /* queue length */, NULL,
2375 helper_handle = GNUNET_HELPER_start ("gnunet-helper-vpn", vpn_argv,
2376 &message_token, NULL);
2377 nc = GNUNET_SERVER_notification_context_create (server, 1);
2378 GNUNET_SERVER_add_handlers (server, service_handlers);
2379 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
2380 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
2385 * The main function of the VPN service.
2387 * @param argc number of arguments from the command line
2388 * @param argv command line arguments
2389 * @return 0 ok, 1 on error
2392 main (int argc, char *const *argv)
2394 return (GNUNET_OK ==
2395 GNUNET_SERVICE_run (argc, argv, "vpn",
2396 GNUNET_SERVICE_OPTION_NONE,
2397 &run, NULL)) ? 0 : 1;
2400 /* end of gnunet-service-vpn.c */