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
34 * - add back ICMP support (especially needed for IPv6)
37 #include "gnunet_util_lib.h"
38 #include "gnunet_common.h"
39 #include "gnunet_protocols.h"
40 #include "gnunet_applications.h"
41 #include "gnunet_mesh_service.h"
42 #include "gnunet_statistics_service.h"
43 #include "gnunet_constants.h"
44 #include "gnunet_tun_lib.h"
50 * Maximum number of messages we allow in the queue for mesh.
52 #define MAX_MESSAGE_QUEUE_SIZE 4
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.
175 * Information about the tunnel to use, NULL if no tunnel
176 * is available right now.
178 struct GNUNET_MESH_Tunnel *tunnel;
181 * Active transmission handle, NULL for none.
183 struct GNUNET_MESH_TransmitHandle *th;
186 * Entry for this entry in the tunnel_heap, NULL as long as this
187 * tunnel state is not fully bound.
189 struct GNUNET_CONTAINER_HeapNode *heap_node;
192 * Head of list of messages scheduled for transmission.
194 struct TunnelMessageQueueEntry *tmq_head;
197 * Tail of list of messages scheduled for transmission.
199 struct TunnelMessageQueueEntry *tmq_tail;
202 * Client that needs to be notified about the tunnel being
203 * up as soon as a peer is connected; NULL for none.
205 struct GNUNET_SERVER_Client *client;
208 * Destination entry that has a pointer to this tunnel state;
209 * NULL if this tunnel state is in the tunnel map.
211 struct DestinationEntry *destination_container;
214 * ID of the client request that caused us to setup this entry.
219 * Destination to which this tunnel leads. Note that
220 * this struct is NOT in the destination_map (but a
221 * local copy) and that the 'heap_node' should always
224 struct DestinationEntry destination;
227 * Task scheduled to destroy the tunnel (or NO_TASK).
229 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
232 * Addess family used for this tunnel on the local TUN interface.
237 * Length of the doubly linked 'tmq_head/tmq_tail' list.
239 unsigned int tmq_length;
242 * IPPROTO_TCP or IPPROTO_UDP once bound.
247 * IP address of the source on our end, initially uninitialized.
252 * Address if af is AF_INET.
257 * Address if af is AF_INET6.
264 * Destination IP address used by the source on our end (this is the IP
265 * that we pick freely within the VPN's tunnel IP range).
270 * Address if af is AF_INET.
275 * Address if af is AF_INET6.
282 * Source port used by the sender on our end; 0 for uninitialized.
284 uint16_t source_port;
287 * Destination port used by the sender on our end; 0 for uninitialized.
289 uint16_t destination_port;
295 * Configuration we use.
297 static const struct GNUNET_CONFIGURATION_Handle *cfg;
300 * Handle to the mesh service.
302 static struct GNUNET_MESH_Handle *mesh_handle;
305 * Map from IP address to destination information (possibly with a
306 * MESH tunnel handle for fast setup).
308 static struct GNUNET_CONTAINER_MultiHashMap *destination_map;
311 * Min-Heap sorted by activity time to expire old mappings.
313 static struct GNUNET_CONTAINER_Heap *destination_heap;
316 * Map from source and destination address (IP+port) to connection
317 * information (mostly with the respective MESH tunnel handle).
319 static struct GNUNET_CONTAINER_MultiHashMap *tunnel_map;
322 * Min-Heap sorted by activity time to expire old mappings; values are
323 * of type 'struct TunnelState'.
325 static struct GNUNET_CONTAINER_Heap *tunnel_heap;
330 static struct GNUNET_STATISTICS_Handle *stats;
333 * The handle to the VPN helper process "gnunet-helper-vpn".
335 static struct GNUNET_HELPER_Handle *helper_handle;
338 * Arguments to the vpn helper.
340 static char *vpn_argv[7];
343 * Length of the prefix of the VPN's IPv6 network.
345 static unsigned long long ipv6prefix;
348 * Notification context for sending replies to clients.
350 static struct GNUNET_SERVER_NotificationContext *nc;
353 * If there are more than this number of address-mappings, old ones
356 static unsigned long long max_destination_mappings;
359 * If there are more than this number of open tunnels, old ones
362 static unsigned long long max_tunnel_mappings;
366 * Compute the key under which we would store an entry in the
367 * destination_map for the given IP address.
369 * @param af address family (AF_INET or AF_INET6)
370 * @param address IP address, struct in_addr or struct in6_addr
371 * @param key where to store the key
374 get_destination_key_from_ip (int af,
376 GNUNET_HashCode *key)
381 GNUNET_CRYPTO_hash (address,
382 sizeof (struct in_addr),
386 GNUNET_CRYPTO_hash (address,
387 sizeof (struct in6_addr),
398 * Compute the key under which we would store an entry in the
399 * tunnel_map for the given socket address pair.
401 * @param af address family (AF_INET or AF_INET6)
402 * @param protocol IPPROTO_TCP or IPPROTO_UDP
403 * @param source_ip sender's source IP, struct in_addr or struct in6_addr
404 * @param source_port sender's source port
405 * @param destination_ip sender's destination IP, struct in_addr or struct in6_addr
406 * @param destination_port sender's destination port
407 * @param key where to store the key
410 get_tunnel_key_from_ips (int af,
412 const void *source_ip,
413 uint16_t source_port,
414 const void *destination_ip,
415 uint16_t destination_port,
416 GNUNET_HashCode *key)
420 memset (key, 0, sizeof (GNUNET_HashCode));
421 /* the GNUnet hashmap only uses the first sizeof(unsigned int) of the hash,
422 so we put the ports in there (and hope for few collisions) */
424 memcpy (off, &source_port, sizeof (uint16_t));
425 off += sizeof (uint16_t);
426 memcpy (off, &destination_port, sizeof (uint16_t));
427 off += sizeof (uint16_t);
431 memcpy (off, source_ip, sizeof (struct in_addr));
432 off += sizeof (struct in_addr);
433 memcpy (off, destination_ip, sizeof (struct in_addr));
434 off += sizeof (struct in_addr);
437 memcpy (off, source_ip, sizeof (struct in6_addr));
438 off += sizeof (struct in6_addr);
439 memcpy (off, destination_ip, sizeof (struct in6_addr));
440 off += sizeof (struct in6_addr);
446 memcpy (off, &protocol, sizeof (uint8_t));
447 off += sizeof (uint8_t);
452 * Notify the client about the result of its request.
454 * @param client client to notify
455 * @param request_id original request ID to include in response
456 * @param result_af resulting address family
457 * @param addr resulting IP address
460 send_client_reply (struct GNUNET_SERVER_Client *client,
465 char buf[sizeof (struct RedirectToIpResponseMessage) + sizeof (struct in6_addr)];
466 struct RedirectToIpResponseMessage *res;
472 rlen = sizeof (struct in_addr);
475 rlen = sizeof (struct in6_addr);
484 res = (struct RedirectToIpResponseMessage *) buf;
485 res->header.size = htons (sizeof (struct RedirectToIpResponseMessage) + rlen);
486 res->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP);
487 res->result_af = htonl (result_af);
488 res->request_id = request_id;
489 memcpy (&res[1], addr, rlen);
490 GNUNET_SERVER_notification_context_add (nc, client);
491 GNUNET_SERVER_notification_context_unicast (nc,
499 * Free resources associated with a tunnel state.
501 * @param ts state to free
504 free_tunnel_state (struct TunnelState *ts)
507 struct TunnelMessageQueueEntry *tnq;
508 struct GNUNET_MESH_Tunnel *tunnel;
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Cleaning up tunnel state\n");
512 GNUNET_STATISTICS_update (stats,
513 gettext_noop ("# Active tunnels"),
515 if (GNUNET_SCHEDULER_NO_TASK != ts->destroy_task)
517 GNUNET_SCHEDULER_cancel (ts->destroy_task);
518 ts->destroy_task = GNUNET_SCHEDULER_NO_TASK;
520 while (NULL != (tnq = ts->tmq_head))
522 GNUNET_CONTAINER_DLL_remove (ts->tmq_head,
528 GNUNET_assert (0 == ts->tmq_length);
529 if (NULL != ts->client)
531 GNUNET_SERVER_client_drop (ts->client);
536 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
539 GNUNET_assert (NULL == ts->destination.heap_node);
540 if (NULL != (tunnel = ts->tunnel))
543 GNUNET_MESH_tunnel_destroy (tunnel);
545 if (NULL != ts->heap_node)
547 GNUNET_CONTAINER_heap_remove_node (ts->heap_node);
548 ts->heap_node = NULL;
549 get_tunnel_key_from_ips (ts->af,
554 ts->destination_port,
556 GNUNET_assert (GNUNET_YES ==
557 GNUNET_CONTAINER_multihashmap_remove (tunnel_map,
561 if (NULL != ts->destination_container)
563 GNUNET_assert (ts == ts->destination_container->ts);
564 ts->destination_container->ts = NULL;
565 ts->destination_container = NULL;
572 * Destroy the mesh tunnel.
574 * @param cls the 'struct TunnelState' with the tunnel to destroy
575 * @param ts schedule context
578 destroy_tunnel_task (void *cls,
579 const struct GNUNET_SCHEDULER_TaskContext *tc)
581 struct TunnelState *ts = cls;
582 struct GNUNET_MESH_Tunnel *tunnel;
584 ts->destroy_task = GNUNET_SCHEDULER_NO_TASK;
585 GNUNET_assert (NULL != ts->tunnel);
588 GNUNET_MESH_tunnel_destroy (tunnel);
589 free_tunnel_state (ts);
594 * Method called whenever a peer has disconnected from the tunnel.
597 * @param peer peer identity the tunnel stopped working with
600 tunnel_peer_disconnect_handler (void *cls,
602 GNUNET_PeerIdentity * peer)
604 struct TunnelState *ts = cls;
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Peer %s disconnected from tunnel.\n",
609 GNUNET_STATISTICS_update (stats,
610 gettext_noop ("# Peers connected to mesh tunnels"),
614 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
617 if (ts->destination.is_service)
618 return; /* hope for reconnect eventually */
619 /* as we are most likely going to change the exit node now,
620 we should just destroy the tunnel entirely... */
621 if (GNUNET_SCHEDULER_NO_TASK == ts->destroy_task)
622 ts->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_tunnel_task, ts);
627 * Method called whenever a peer has connected to the tunnel. Notifies
628 * the waiting client that the tunnel is now up.
631 * @param peer peer identity the tunnel was created to, NULL on timeout
632 * @param atsi performance data for the connection
635 tunnel_peer_connect_handler (void *cls,
636 const struct GNUNET_PeerIdentity
639 GNUNET_ATS_Information * atsi)
641 struct TunnelState *ts = cls;
643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
644 "Peer %s connected to tunnel.\n",
646 GNUNET_STATISTICS_update (stats,
647 gettext_noop ("# Peers connected to mesh tunnels"),
649 if (NULL == ts->client)
650 return; /* nothing to do */
651 send_client_reply (ts->client,
654 &ts->destination_ip);
655 GNUNET_SERVER_client_drop (ts->client);
661 * Send a message from the message queue via mesh.
663 * @param cls the 'struct TunnelState' with the message queue
664 * @param size number of bytes available in buf
665 * @param buf where to copy the message
666 * @return number of bytes copied to buf
669 send_to_peer_notify_callback (void *cls, size_t size, void *buf)
671 struct TunnelState *ts = cls;
672 struct TunnelMessageQueueEntry *tnq;
679 GNUNET_assert (NULL != tnq);
680 GNUNET_assert (size >= tnq->len);
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 "Sending %u bytes via mesh tunnel\n",
684 GNUNET_CONTAINER_DLL_remove (ts->tmq_head,
688 memcpy (buf, tnq->msg, tnq->len);
691 if (NULL != (tnq = ts->tmq_head))
692 ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
693 GNUNET_NO /* cork */,
695 GNUNET_TIME_UNIT_FOREVER_REL,
698 &send_to_peer_notify_callback,
700 GNUNET_STATISTICS_update (stats,
701 gettext_noop ("# Bytes given to mesh for transmission"),
708 * Add the given message to the given tunnel and trigger the
709 * transmission process.
711 * @param tnq message to queue
712 * @param ts tunnel to queue the message for
715 send_to_tunnel (struct TunnelMessageQueueEntry *tnq,
716 struct TunnelState *ts)
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Queueing %u bytes for transmission via mesh tunnel\n",
721 GNUNET_assert (NULL != ts->tunnel);
722 GNUNET_CONTAINER_DLL_insert_tail (ts->tmq_head,
726 if (ts->tmq_length > MAX_MESSAGE_QUEUE_SIZE)
728 struct TunnelMessageQueueEntry *dq;
731 GNUNET_assert (dq != tnq);
732 GNUNET_CONTAINER_DLL_remove (ts->tmq_head,
735 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
737 GNUNET_STATISTICS_update (stats,
738 gettext_noop ("# Bytes dropped in mesh queue (overflow)"),
744 ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
745 GNUNET_NO /* cork */,
747 GNUNET_TIME_UNIT_FOREVER_REL,
750 &send_to_peer_notify_callback,
756 * Initialize the given destination entry's mesh tunnel.
758 * @param de destination entry for which we need to setup a tunnel
759 * @param client client to notify on successful tunnel setup, or NULL for none
760 * @param request_id request ID to send in client notification (unused if client is NULL)
761 * @return tunnel state of the tunnel that was created
763 static struct TunnelState *
764 create_tunnel_to_destination (struct DestinationEntry *de,
765 struct GNUNET_SERVER_Client *client,
768 struct TunnelState *ts;
770 GNUNET_STATISTICS_update (stats,
771 gettext_noop ("# Mesh tunnels created"),
773 GNUNET_assert (NULL == de->ts);
774 ts = GNUNET_malloc (sizeof (struct TunnelState));
777 ts->request_id = request_id;
779 GNUNET_SERVER_client_keep (client);
781 ts->destination = *de;
782 ts->destination.heap_node = NULL; /* copy is NOT in destination heap */
784 ts->destination_container = de; /* we are referenced from de */
785 ts->af = AF_UNSPEC; /* so far, unknown */
786 ts->tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
788 &tunnel_peer_connect_handler,
789 &tunnel_peer_disconnect_handler,
791 if (NULL == ts->tunnel)
793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
794 _("Failed to setup mesh tunnel!\n"));
796 GNUNET_SERVER_client_drop (client);
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "Creating tunnel to peer %s offering service %s\n",
804 GNUNET_i2s (&de->details.service_destination.target),
805 GNUNET_h2s (&de->details.service_destination.service_descriptor));
806 GNUNET_MESH_peer_request_connect_add (ts->tunnel,
807 &de->details.service_destination.target);
811 switch (de->details.exit_destination.af)
814 GNUNET_MESH_peer_request_connect_by_type (ts->tunnel,
815 GNUNET_APPLICATION_TYPE_IPV4_GATEWAY);
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817 "Creating tunnel to exit peer for %s\n",
821 GNUNET_MESH_peer_request_connect_by_type (ts->tunnel,
822 GNUNET_APPLICATION_TYPE_IPV6_GATEWAY);
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
824 "Creating tunnel to exit peer for %s\n",
837 * We have too many active tunnels. Clean up the oldest tunnel.
839 * @param except tunnel that must NOT be cleaned up, even if it is the oldest
842 expire_tunnel (struct TunnelState *except)
844 struct TunnelState *ts;
846 ts = GNUNET_CONTAINER_heap_peek (tunnel_heap);
848 return; /* can't do this */
849 free_tunnel_state (ts);
854 * Route a packet via mesh to the given destination.
856 * @param destination description of the destination
857 * @param af address family on this end (AF_INET or AF_INET6)
858 * @param protocol IPPROTO_TCP or IPPROTO_UDP
859 * @param source_ip source IP used by the sender (struct in_addr or struct in6_addr)
860 * @param destination_ip destination IP used by the sender (struct in_addr or struct in6_addr)
861 * @param payload payload of the packet after the IP header
862 * @param payload_length number of bytes in payload
865 route_packet (struct DestinationEntry *destination,
868 const void *source_ip,
869 const void *destination_ip,
871 size_t payload_length)
874 struct TunnelState *ts;
875 struct TunnelMessageQueueEntry *tnq;
879 const struct GNUNET_TUN_UdpHeader *udp;
880 const struct GNUNET_TUN_TcpHeader *tcp;
888 if (payload_length < sizeof (struct GNUNET_TUN_UdpHeader))
895 spt = ntohs (udp->spt);
896 dpt = ntohs (udp->dpt);
897 get_tunnel_key_from_ips (af,
908 if (payload_length < sizeof (struct GNUNET_TUN_TcpHeader))
915 spt = ntohs (tcp->spt);
916 dpt = ntohs (tcp->dpt);
917 get_tunnel_key_from_ips (af,
927 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
928 _("Protocol %u not supported, dropping\n"),
929 (unsigned int) protocol);
932 if (! destination->is_service)
934 switch (destination->details.exit_destination.af)
937 alen = sizeof (struct in_addr);
940 alen = sizeof (struct in6_addr);
948 char sbuf[INET6_ADDRSTRLEN];
949 char dbuf[INET6_ADDRSTRLEN];
950 char xbuf[INET6_ADDRSTRLEN];
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "Routing %s packet from %s:%u -> %s:%u to destination %s:%u\n",
954 (protocol == IPPROTO_TCP) ? "TCP" : "UDP",
955 inet_ntop (af, source_ip, sbuf, sizeof (sbuf)),
957 inet_ntop (af, destination_ip, dbuf, sizeof (dbuf)),
959 inet_ntop (destination->details.exit_destination.af,
960 &destination->details.exit_destination.ip,
961 xbuf, sizeof (xbuf)),
967 /* make compiler happy */
970 char sbuf[INET6_ADDRSTRLEN];
971 char dbuf[INET6_ADDRSTRLEN];
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "Routing %s packet from %s:%u -> %s:%u to service %s at peer %s\n",
975 (protocol == IPPROTO_TCP) ? "TCP" : "UDP",
976 inet_ntop (af, source_ip, sbuf, sizeof (sbuf)),
978 inet_ntop (af, destination_ip, dbuf, sizeof (dbuf)),
980 GNUNET_h2s (&destination->details.service_destination.service_descriptor),
981 GNUNET_i2s (&destination->details.service_destination.target));
986 /* see if we have an existing tunnel for this destination */
987 ts = GNUNET_CONTAINER_multihashmap_get (tunnel_map,
991 /* need to either use the existing tunnel from the destination (if still
992 available) or create a fresh one */
994 if (NULL == destination->ts)
995 ts = create_tunnel_to_destination (destination, NULL, 0);
997 ts = destination->ts;
1000 destination->ts = NULL;
1001 ts->destination_container = NULL; /* no longer 'contained' */
1002 /* now bind existing "unbound" tunnel to our IP/port tuple */
1003 ts->protocol = protocol;
1007 ts->source_ip.v4 = * (const struct in_addr *) source_ip;
1008 ts->destination_ip.v4 = * (const struct in_addr *) destination_ip;
1012 ts->source_ip.v6 = * (const struct in6_addr *) source_ip;
1013 ts->destination_ip.v6 = * (const struct in6_addr *) destination_ip;
1015 ts->source_port = spt;
1016 ts->destination_port = dpt;
1017 ts->heap_node = GNUNET_CONTAINER_heap_insert (tunnel_heap,
1019 GNUNET_TIME_absolute_get ().abs_value);
1020 GNUNET_assert (GNUNET_YES ==
1021 GNUNET_CONTAINER_multihashmap_put (tunnel_map,
1024 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1025 GNUNET_STATISTICS_update (stats,
1026 gettext_noop ("# Active tunnels"),
1028 while (GNUNET_CONTAINER_multihashmap_size (tunnel_map) > max_tunnel_mappings)
1034 GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
1036 GNUNET_TIME_absolute_get ().abs_value);
1038 GNUNET_assert (NULL != ts->tunnel);
1040 /* send via tunnel */
1044 if (destination->is_service)
1046 struct GNUNET_EXIT_UdpServiceMessage *usm;
1048 mlen = sizeof (struct GNUNET_EXIT_UdpServiceMessage) +
1049 payload_length - sizeof (struct GNUNET_TUN_UdpHeader);
1050 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1055 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
1058 usm = (struct GNUNET_EXIT_UdpServiceMessage *) &tnq[1];
1059 usm->header.size = htons ((uint16_t) mlen);
1060 usm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_UDP_TO_SERVICE);
1061 /* if the source port is below 32000, we assume it has a special
1062 meaning; if not, we pick a random port (this is a heuristic) */
1063 usm->source_port = (ntohs (udp->spt) < 32000) ? udp->spt : 0;
1064 usm->destination_port = udp->dpt;
1065 usm->service_descriptor = destination->details.service_destination.service_descriptor;
1068 payload_length - sizeof (struct GNUNET_TUN_UdpHeader));
1072 struct GNUNET_EXIT_UdpInternetMessage *uim;
1073 struct in_addr *ip4dst;
1074 struct in6_addr *ip6dst;
1077 mlen = sizeof (struct GNUNET_EXIT_UdpInternetMessage) +
1078 alen + payload_length - sizeof (struct GNUNET_TUN_UdpHeader);
1079 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1084 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) +
1088 uim = (struct GNUNET_EXIT_UdpInternetMessage *) &tnq[1];
1089 uim->header.size = htons ((uint16_t) mlen);
1090 uim->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_UDP_TO_INTERNET);
1091 uim->af = htonl (destination->details.exit_destination.af);
1092 uim->source_port = (ntohs (udp->spt) < 32000) ? udp->spt : 0;
1093 uim->destination_port = udp->dpt;
1094 switch (destination->details.exit_destination.af)
1097 ip4dst = (struct in_addr *) &uim[1];
1098 *ip4dst = destination->details.exit_destination.ip.v4;
1099 payload = &ip4dst[1];
1102 ip6dst = (struct in6_addr *) &uim[1];
1103 *ip6dst = destination->details.exit_destination.ip.v6;
1104 payload = &ip6dst[1];
1111 payload_length - sizeof (struct GNUNET_TUN_UdpHeader));
1117 if (destination->is_service)
1119 struct GNUNET_EXIT_TcpServiceStartMessage *tsm;
1121 mlen = sizeof (struct GNUNET_EXIT_TcpServiceStartMessage) +
1122 payload_length - sizeof (struct GNUNET_TUN_TcpHeader);
1123 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1128 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
1131 tsm = (struct GNUNET_EXIT_TcpServiceStartMessage *) &tnq[1];
1132 tsm->header.size = htons ((uint16_t) mlen);
1133 tsm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_TO_SERVICE_START);
1134 tsm->reserved = htonl (0);
1135 tsm->service_descriptor = destination->details.service_destination.service_descriptor;
1136 tsm->tcp_header = *tcp;
1139 payload_length - sizeof (struct GNUNET_TUN_TcpHeader));
1143 struct GNUNET_EXIT_TcpInternetStartMessage *tim;
1144 struct in_addr *ip4dst;
1145 struct in6_addr *ip6dst;
1148 mlen = sizeof (struct GNUNET_EXIT_TcpInternetStartMessage) +
1149 alen + payload_length - sizeof (struct GNUNET_TUN_TcpHeader);
1150 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1155 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
1158 tim = (struct GNUNET_EXIT_TcpInternetStartMessage *) &tnq[1];
1159 tim->header.size = htons ((uint16_t) mlen);
1160 tim->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_TO_INTERNET_START);
1161 tim->af = htonl (destination->details.exit_destination.af);
1162 tim->tcp_header = *tcp;
1163 switch (destination->details.exit_destination.af)
1166 ip4dst = (struct in_addr *) &tim[1];
1167 *ip4dst = destination->details.exit_destination.ip.v4;
1168 payload = &ip4dst[1];
1171 ip6dst = (struct in6_addr *) &tim[1];
1172 *ip6dst = destination->details.exit_destination.ip.v6;
1173 payload = &ip6dst[1];
1180 payload_length - sizeof (struct GNUNET_TUN_TcpHeader));
1185 struct GNUNET_EXIT_TcpDataMessage *tdm;
1187 mlen = sizeof (struct GNUNET_EXIT_TcpDataMessage) +
1188 alen + payload_length - sizeof (struct GNUNET_TUN_TcpHeader);
1189 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1194 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
1197 tdm = (struct GNUNET_EXIT_TcpDataMessage *) &tnq[1];
1198 tdm->header.size = htons ((uint16_t) mlen);
1199 tdm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_DATA);
1200 tdm->reserved = htonl (0);
1201 tdm->tcp_header = *tcp;
1204 payload_length - sizeof (struct GNUNET_TUN_TcpHeader));
1208 /* not supported above, how can we get here !? */
1212 send_to_tunnel (tnq, ts);
1217 * Receive packets from the helper-process (someone send to the local
1218 * virtual tunnel interface). Find the destination mapping, and if it
1219 * exists, identify the correct MESH tunnel (or possibly create it)
1220 * and forward the packet.
1222 * @param cls closure, NULL
1223 * @param client NULL
1224 * @param message message we got from the client (VPN tunnel interface)
1227 message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
1228 const struct GNUNET_MessageHeader *message)
1230 const struct GNUNET_TUN_Layer2PacketHeader *tun;
1232 GNUNET_HashCode key;
1233 struct DestinationEntry *de;
1235 GNUNET_STATISTICS_update (stats,
1236 gettext_noop ("# Packets received from TUN interface"),
1238 mlen = ntohs (message->size);
1239 if ( (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) ||
1240 (mlen < sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader)) )
1245 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
1246 mlen -= (sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader));
1247 switch (ntohs (tun->proto))
1251 const struct GNUNET_TUN_IPv6Header *pkt6;
1253 if (mlen < sizeof (struct GNUNET_TUN_IPv6Header))
1259 pkt6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
1260 get_destination_key_from_ip (AF_INET6,
1261 &pkt6->destination_address,
1263 de = GNUNET_CONTAINER_multihashmap_get (destination_map, &key);
1264 /* FIXME: do we need to guard against hash collision?
1265 (if so, we need to also store the local destination IP in the
1266 destination entry and then compare here; however, the risk
1267 of collision seems minimal AND the impact is unlikely to be
1268 super-problematic as well... */
1271 char buf[INET6_ADDRSTRLEN];
1273 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1274 _("Packet received for unmapped destination `%s' (dropping it)\n"),
1275 inet_ntop (AF_INET6,
1276 &pkt6->destination_address,
1284 &pkt6->source_address,
1285 &pkt6->destination_address,
1287 mlen - sizeof (struct GNUNET_TUN_IPv6Header));
1292 struct GNUNET_TUN_IPv4Header *pkt4;
1294 if (mlen < sizeof (struct GNUNET_TUN_IPv4Header))
1300 pkt4 = (struct GNUNET_TUN_IPv4Header *) &tun[1];
1301 get_destination_key_from_ip (AF_INET,
1302 &pkt4->destination_address,
1304 de = GNUNET_CONTAINER_multihashmap_get (destination_map, &key);
1305 /* FIXME: do we need to guard against hash collision?
1306 (if so, we need to also store the local destination IP in the
1307 destination entry and then compare here; however, the risk
1308 of collision seems minimal AND the impact is unlikely to be
1309 super-problematic as well... */
1312 char buf[INET_ADDRSTRLEN];
1314 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1315 _("Packet received for unmapped destination `%s' (dropping it)\n"),
1317 &pkt4->destination_address,
1322 if (pkt4->header_length * 4 != sizeof (struct GNUNET_TUN_IPv4Header))
1324 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1325 _("Received IPv4 packet with options (dropping it)\n"));
1331 &pkt4->source_address,
1332 &pkt4->destination_address,
1334 mlen - sizeof (struct GNUNET_TUN_IPv4Header));
1338 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1339 _("Received packet of unknown protocol %d from TUN (dropping it)\n"),
1340 (unsigned int) ntohs (tun->proto));
1347 * We got a UDP packet back from the MESH tunnel. Pass it on to the
1348 * local virtual interface via the helper.
1350 * @param cls closure, NULL
1351 * @param tunnel connection to the other end
1352 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1353 * @param sender who sent the message
1354 * @param message the actual message
1355 * @param atsi performance data for the connection
1356 * @return GNUNET_OK to keep the connection open,
1357 * GNUNET_SYSERR to close it (signal serious error)
1360 receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1361 void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
1362 const struct GNUNET_MessageHeader *message,
1363 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1365 struct TunnelState *ts = *tunnel_ctx;
1366 const struct GNUNET_EXIT_UdpReplyMessage *reply;
1369 GNUNET_STATISTICS_update (stats,
1370 gettext_noop ("# UDP packets received from mesh"),
1372 mlen = ntohs (message->size);
1373 if (mlen < sizeof (struct GNUNET_EXIT_UdpReplyMessage))
1375 GNUNET_break_op (0);
1376 return GNUNET_SYSERR;
1378 if (NULL == ts->heap_node)
1380 GNUNET_break_op (0);
1381 return GNUNET_SYSERR;
1383 if (AF_UNSPEC == ts->af)
1385 GNUNET_break_op (0);
1386 return GNUNET_SYSERR;
1388 reply = (const struct GNUNET_EXIT_UdpReplyMessage *) message;
1389 mlen -= sizeof (struct GNUNET_EXIT_UdpReplyMessage);
1391 char sbuf[INET6_ADDRSTRLEN];
1392 char dbuf[INET6_ADDRSTRLEN];
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Received UDP reply from mesh, sending %u bytes from %s:%u -> %s:%u via TUN\n",
1396 (unsigned int) mlen,
1397 inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
1398 ts->destination_port,
1399 inet_ntop (ts->af, &ts->source_ip, dbuf, sizeof (dbuf)),
1406 size_t size = sizeof (struct GNUNET_TUN_IPv4Header)
1407 + sizeof (struct GNUNET_TUN_UdpHeader)
1408 + sizeof (struct GNUNET_MessageHeader) +
1409 sizeof (struct GNUNET_TUN_Layer2PacketHeader) +
1413 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1414 struct GNUNET_TUN_Layer2PacketHeader *tun = (struct GNUNET_TUN_Layer2PacketHeader*) &msg[1];
1415 struct GNUNET_TUN_IPv4Header *ipv4 = (struct GNUNET_TUN_IPv4Header *) &tun[1];
1416 struct GNUNET_TUN_UdpHeader *udp = (struct GNUNET_TUN_UdpHeader *) &ipv4[1];
1417 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1418 msg->size = htons (size);
1419 tun->flags = htons (0);
1420 tun->proto = htons (ETH_P_IPV4);
1421 GNUNET_TUN_initialize_ipv4_header (ipv4,
1423 sizeof (struct GNUNET_TUN_UdpHeader) + mlen,
1424 &ts->destination_ip.v4,
1426 if (0 == ntohs (reply->source_port))
1427 udp->spt = htons (ts->destination_port);
1429 udp->spt = reply->source_port;
1430 if (0 == ntohs (reply->destination_port))
1431 udp->dpt = htons (ts->source_port);
1433 udp->dpt = reply->destination_port;
1434 udp->len = htons (mlen + sizeof (struct GNUNET_TUN_UdpHeader));
1435 GNUNET_TUN_calculate_udp4_checksum (ipv4,
1442 (void) GNUNET_HELPER_send (helper_handle,
1451 size_t size = sizeof (struct GNUNET_TUN_IPv6Header)
1452 + sizeof (struct GNUNET_TUN_UdpHeader)
1453 + sizeof (struct GNUNET_MessageHeader) +
1454 sizeof (struct GNUNET_TUN_Layer2PacketHeader) +
1458 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1459 struct GNUNET_TUN_Layer2PacketHeader *tun = (struct GNUNET_TUN_Layer2PacketHeader*) &msg[1];
1460 struct GNUNET_TUN_IPv6Header *ipv6 = (struct GNUNET_TUN_IPv6Header *) &tun[1];
1461 struct GNUNET_TUN_UdpHeader *udp = (struct GNUNET_TUN_UdpHeader *) &ipv6[1];
1462 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1463 msg->size = htons (size);
1464 tun->flags = htons (0);
1465 tun->proto = htons (ETH_P_IPV6);
1466 GNUNET_TUN_initialize_ipv6_header (ipv6,
1468 sizeof (struct GNUNET_TUN_UdpHeader) + mlen,
1469 &ts->destination_ip.v6,
1471 if (0 == ntohs (reply->source_port))
1472 udp->spt = htons (ts->destination_port);
1474 udp->spt = reply->source_port;
1475 if (0 == ntohs (reply->destination_port))
1476 udp->dpt = htons (ts->source_port);
1478 udp->dpt = reply->destination_port;
1479 udp->len = htons (mlen + sizeof (struct GNUNET_TUN_UdpHeader));
1480 GNUNET_TUN_calculate_udp6_checksum (ipv6,
1486 (void) GNUNET_HELPER_send (helper_handle,
1496 GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
1498 GNUNET_TIME_absolute_get ().abs_value);
1504 * We got a TCP packet back from the MESH tunnel. Pass it on to the
1505 * local virtual interface via the helper.
1507 * @param cls closure, NULL
1508 * @param tunnel connection to the other end
1509 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1510 * @param sender who sent the message
1511 * @param message the actual message
1512 * @param atsi performance data for the connection
1513 * @return GNUNET_OK to keep the connection open,
1514 * GNUNET_SYSERR to close it (signal serious error)
1517 receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1519 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1520 const struct GNUNET_MessageHeader *message,
1521 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1523 struct TunnelState *ts = *tunnel_ctx;
1524 const struct GNUNET_EXIT_TcpDataMessage *data;
1527 GNUNET_STATISTICS_update (stats,
1528 gettext_noop ("# TCP packets received from mesh"),
1530 mlen = ntohs (message->size);
1531 if (mlen < sizeof (struct GNUNET_EXIT_TcpDataMessage))
1533 GNUNET_break_op (0);
1534 return GNUNET_SYSERR;
1536 if (NULL == ts->heap_node)
1538 GNUNET_break_op (0);
1539 return GNUNET_SYSERR;
1541 data = (const struct GNUNET_EXIT_TcpDataMessage *) message;
1542 mlen -= sizeof (struct GNUNET_EXIT_TcpDataMessage);
1544 char sbuf[INET6_ADDRSTRLEN];
1545 char dbuf[INET6_ADDRSTRLEN];
1547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1548 "Received TCP reply from mesh, sending %u bytes from %s:%u -> %s:%u via TUN\n",
1549 (unsigned int) mlen,
1550 inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
1551 ts->destination_port,
1552 inet_ntop (ts->af, &ts->source_ip, dbuf, sizeof (dbuf)),
1559 size_t size = sizeof (struct GNUNET_TUN_IPv4Header)
1560 + sizeof (struct GNUNET_TUN_TcpHeader)
1561 + sizeof (struct GNUNET_MessageHeader) +
1562 sizeof (struct GNUNET_TUN_Layer2PacketHeader) +
1566 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1567 struct GNUNET_TUN_Layer2PacketHeader *tun = (struct GNUNET_TUN_Layer2PacketHeader*) &msg[1];
1568 struct GNUNET_TUN_IPv4Header *ipv4 = (struct GNUNET_TUN_IPv4Header *) &tun[1];
1569 struct GNUNET_TUN_TcpHeader *tcp = (struct GNUNET_TUN_TcpHeader *) &ipv4[1];
1570 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1571 msg->size = htons (size);
1572 tun->flags = htons (0);
1573 tun->proto = htons (ETH_P_IPV4);
1574 GNUNET_TUN_initialize_ipv4_header (ipv4,
1576 sizeof (struct GNUNET_TUN_TcpHeader) + mlen,
1577 &ts->destination_ip.v4,
1579 *tcp = data->tcp_header;
1580 tcp->spt = htons (ts->destination_port);
1581 tcp->dpt = htons (ts->source_port);
1582 GNUNET_TUN_calculate_tcp4_checksum (ipv4,
1589 (void) GNUNET_HELPER_send (helper_handle,
1598 size_t size = sizeof (struct GNUNET_TUN_IPv6Header)
1599 + sizeof (struct GNUNET_TUN_TcpHeader)
1600 + sizeof (struct GNUNET_MessageHeader) +
1601 sizeof (struct GNUNET_TUN_Layer2PacketHeader) +
1605 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1606 struct GNUNET_TUN_Layer2PacketHeader *tun = (struct GNUNET_TUN_Layer2PacketHeader*) &msg[1];
1607 struct GNUNET_TUN_IPv6Header *ipv6 = (struct GNUNET_TUN_IPv6Header *) &tun[1];
1608 struct GNUNET_TUN_TcpHeader *tcp = (struct GNUNET_TUN_TcpHeader *) &ipv6[1];
1609 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1610 msg->size = htons (size);
1611 tun->flags = htons (0);
1612 tun->proto = htons (ETH_P_IPV6);
1613 GNUNET_TUN_initialize_ipv6_header (ipv6,
1615 sizeof (struct GNUNET_TUN_TcpHeader) + mlen,
1616 &ts->destination_ip.v6,
1618 tcp->spt = htons (ts->destination_port);
1619 tcp->dpt = htons (ts->source_port);
1620 GNUNET_TUN_calculate_tcp6_checksum (ipv6,
1624 (void) GNUNET_HELPER_send (helper_handle,
1632 GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
1634 GNUNET_TIME_absolute_get ().abs_value);
1640 * Allocate an IPv4 address from the range of the tunnel
1641 * for a new redirection.
1643 * @param v4 where to store the address
1644 * @return GNUNET_OK on success,
1645 * GNUNET_SYSERR on error
1648 allocate_v4_address (struct in_addr *v4)
1650 const char *ipv4addr = vpn_argv[4];
1651 const char *ipv4mask = vpn_argv[5];
1652 struct in_addr addr;
1653 struct in_addr mask;
1655 GNUNET_HashCode key;
1658 GNUNET_assert (1 == inet_pton (AF_INET, ipv4addr, &addr));
1659 GNUNET_assert (1 == inet_pton (AF_INET, ipv4mask, &mask));
1660 /* Given 192.168.0.1/255.255.0.0, we want a mask
1661 of '192.168.255.255', thus: */
1662 mask.s_addr = addr.s_addr | ~mask.s_addr;
1669 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1670 _("Failed to find unallocated IPv4 address in VPN's range\n"));
1671 return GNUNET_SYSERR;
1673 /* Pick random IPv4 address within the subnet, except 'addr' or 'mask' itself */
1674 rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1676 v4->s_addr = (addr.s_addr | rnd.s_addr) & mask.s_addr;
1677 get_destination_key_from_ip (AF_INET,
1681 while ( (GNUNET_YES ==
1682 GNUNET_CONTAINER_multihashmap_contains (destination_map,
1684 (v4->s_addr == addr.s_addr) ||
1685 (v4->s_addr == mask.s_addr) );
1691 * Allocate an IPv6 address from the range of the tunnel
1692 * for a new redirection.
1694 * @param v6 where to store the address
1695 * @return GNUNET_OK on success,
1696 * GNUNET_SYSERR on error
1699 allocate_v6_address (struct in6_addr *v6)
1701 const char *ipv6addr = vpn_argv[2];
1702 struct in6_addr addr;
1703 struct in6_addr mask;
1704 struct in6_addr rnd;
1706 GNUNET_HashCode key;
1709 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6addr, &addr));
1710 GNUNET_assert (ipv6prefix < 128);
1711 /* Given ABCD::/96, we want a mask of 'ABCD::FFFF:FFFF,
1714 for (i=127;i>=128-ipv6prefix;i--)
1715 mask.s6_addr[i / 8] |= (1 << (i % 8));
1717 /* Pick random IPv6 address within the subnet, except 'addr' or 'mask' itself */
1724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1725 _("Failed to find unallocated IPv6 address in VPN's range\n"));
1726 return GNUNET_SYSERR;
1731 rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1734 = (addr.s6_addr[i] | rnd.s6_addr[i]) & mask.s6_addr[i];
1736 get_destination_key_from_ip (AF_INET6,
1740 while ( (GNUNET_YES ==
1741 GNUNET_CONTAINER_multihashmap_contains (destination_map,
1745 sizeof (struct in6_addr))) ||
1748 sizeof (struct in6_addr))) );
1754 * Free resources occupied by a destination entry.
1756 * @param de entry to free
1759 free_destination_entry (struct DestinationEntry *de)
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 "Cleaning up destination entry\n");
1763 GNUNET_STATISTICS_update (stats,
1764 gettext_noop ("# Active destinations"),
1768 free_tunnel_state (de->ts);
1769 GNUNET_assert (NULL == de->ts);
1771 if (NULL != de->heap_node)
1773 GNUNET_CONTAINER_heap_remove_node (de->heap_node);
1774 de->heap_node = NULL;
1775 GNUNET_assert (GNUNET_YES ==
1776 GNUNET_CONTAINER_multihashmap_remove (destination_map,
1785 * We have too many active destinations. Clean up the oldest destination.
1787 * @param except destination that must NOT be cleaned up, even if it is the oldest
1790 expire_destination (struct DestinationEntry *except)
1792 struct DestinationEntry *de;
1794 de = GNUNET_CONTAINER_heap_peek (destination_heap);
1796 return; /* can't do this */
1797 free_destination_entry (de);
1802 * A client asks us to setup a redirection via some exit
1803 * node to a particular IP. Setup the redirection and
1804 * give the client the allocated IP.
1807 * @param client requesting client
1808 * @param message redirection request (a 'struct RedirectToIpRequestMessage')
1811 service_redirect_to_ip (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
1812 const struct GNUNET_MessageHeader *message)
1816 const struct RedirectToIpRequestMessage *msg;
1822 struct DestinationEntry *de;
1823 GNUNET_HashCode key;
1825 /* validate and parse request */
1826 mlen = ntohs (message->size);
1827 if (mlen < sizeof (struct RedirectToIpRequestMessage))
1830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1833 alen = mlen - sizeof (struct RedirectToIpRequestMessage);
1834 msg = (const struct RedirectToIpRequestMessage *) message;
1835 addr_af = (int) htonl (msg->addr_af);
1839 if (alen != sizeof (struct in_addr))
1842 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1847 if (alen != sizeof (struct in6_addr))
1850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1856 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1860 /* allocate response IP */
1862 result_af = (int) htonl (msg->result_af);
1867 allocate_v4_address (&v4))
1868 result_af = AF_UNSPEC;
1874 allocate_v6_address (&v6))
1875 result_af = AF_UNSPEC;
1881 allocate_v4_address (&v4))
1884 result_af = AF_INET;
1886 else if (GNUNET_OK ==
1887 allocate_v6_address (&v6))
1890 result_af = AF_INET6;
1895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1898 if ( (result_af == AF_UNSPEC) ||
1899 (GNUNET_NO == ntohl (msg->nac)) )
1901 /* send reply "instantly" */
1902 send_client_reply (client,
1907 if (result_af == AF_UNSPEC)
1909 /* failure, we're done */
1910 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1915 char sbuf[INET6_ADDRSTRLEN];
1916 char dbuf[INET6_ADDRSTRLEN];
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1919 "Allocated address %s for redirection via exit to %s\n",
1920 inet_ntop (result_af, addr, sbuf, sizeof (sbuf)),
1922 &msg[1], dbuf, sizeof (dbuf)));
1925 /* setup destination record */
1926 de = GNUNET_malloc (sizeof (struct DestinationEntry));
1927 de->is_service = GNUNET_NO;
1928 de->details.exit_destination.af = addr_af;
1929 memcpy (&de->details.exit_destination.ip,
1932 get_destination_key_from_ip (result_af,
1936 GNUNET_assert (GNUNET_OK ==
1937 GNUNET_CONTAINER_multihashmap_put (destination_map,
1940 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1941 de->heap_node = GNUNET_CONTAINER_heap_insert (destination_heap,
1943 GNUNET_TIME_absolute_ntoh (msg->expiration_time).abs_value);
1944 GNUNET_STATISTICS_update (stats,
1945 gettext_noop ("# Active destinations"),
1947 while (GNUNET_CONTAINER_multihashmap_size (destination_map) > max_destination_mappings)
1948 expire_destination (de);
1950 /* setup tunnel to destination */
1951 (void) create_tunnel_to_destination (de,
1952 (GNUNET_NO == ntohl (msg->nac)) ? NULL : client,
1955 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1960 * A client asks us to setup a redirection to a particular peer
1961 * offering a service. Setup the redirection and give the client the
1965 * @param client requesting client
1966 * @param message redirection request (a 'struct RedirectToPeerRequestMessage')
1969 service_redirect_to_service (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
1970 const struct GNUNET_MessageHeader *message)
1972 const struct RedirectToServiceRequestMessage *msg;
1977 struct DestinationEntry *de;
1978 GNUNET_HashCode key;
1981 msg = (const struct RedirectToServiceRequestMessage *) message;
1983 /* allocate response IP */
1985 result_af = (int) htonl (msg->result_af);
1990 allocate_v4_address (&v4))
1991 result_af = AF_UNSPEC;
1997 allocate_v6_address (&v6))
1998 result_af = AF_UNSPEC;
2004 allocate_v4_address (&v4))
2007 result_af = AF_INET;
2009 else if (GNUNET_OK ==
2010 allocate_v6_address (&v6))
2013 result_af = AF_INET6;
2018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2021 if ( (result_af == AF_UNSPEC) ||
2022 (GNUNET_NO == ntohl (msg->nac)) )
2024 /* send reply "instantly" */
2025 send_client_reply (client,
2030 if (result_af == AF_UNSPEC)
2032 /* failure, we're done */
2033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2034 _("Failed to allocate IP address for new destination\n"));
2035 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2040 char sbuf[INET6_ADDRSTRLEN];
2042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2043 "Allocated address %s for redirection to service %s on peer %s\n",
2044 inet_ntop (result_af, addr, sbuf, sizeof (sbuf)),
2045 GNUNET_h2s (&msg->service_descriptor),
2046 GNUNET_i2s (&msg->target));
2049 /* setup destination record */
2050 de = GNUNET_malloc (sizeof (struct DestinationEntry));
2051 de->is_service = GNUNET_YES;
2052 de->details.service_destination.service_descriptor = msg->service_descriptor;
2053 de->details.service_destination.target = msg->target;
2054 get_destination_key_from_ip (result_af,
2058 GNUNET_assert (GNUNET_OK ==
2059 GNUNET_CONTAINER_multihashmap_put (destination_map,
2062 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2063 de->heap_node = GNUNET_CONTAINER_heap_insert (destination_heap,
2065 GNUNET_TIME_absolute_ntoh (msg->expiration_time).abs_value);
2066 while (GNUNET_CONTAINER_multihashmap_size (destination_map) > max_destination_mappings)
2067 expire_destination (de);
2068 (void) create_tunnel_to_destination (de,
2069 (GNUNET_NO == ntohl (msg->nac)) ? NULL : client,
2072 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2078 * Function called for inbound tunnels. As we don't offer
2079 * any mesh services, this function should never be called.
2081 * @param cls closure
2082 * @param tunnel new handle to the tunnel
2083 * @param initiator peer that started the tunnel
2084 * @param atsi performance information for the tunnel
2085 * @return initial tunnel context for the tunnel
2086 * (can be NULL -- that's not an error)
2089 inbound_tunnel_cb (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
2090 const struct GNUNET_PeerIdentity *initiator,
2091 const struct GNUNET_ATS_Information *atsi)
2093 /* How can and why should anyone open an inbound tunnel to vpn? */
2100 * Function called whenever an inbound tunnel is destroyed. Should clean up
2101 * any associated state.
2103 * @param cls closure (set from GNUNET_MESH_connect)
2104 * @param tunnel connection to the other end (henceforth invalid)
2105 * @param tunnel_ctx place where local state associated
2106 * with the tunnel is stored (our 'struct TunnelState')
2109 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
2111 struct TunnelState *ts = tunnel_ctx;
2118 GNUNET_assert (ts->tunnel == tunnel);
2120 free_tunnel_state (ts);
2125 * Free memory occupied by an entry in the destination map.
2129 * @param value a 'struct DestinationEntry *'
2130 * @return GNUNET_OK (continue to iterate)
2133 cleanup_destination (void *cls,
2134 const GNUNET_HashCode *key,
2137 struct DestinationEntry *de = value;
2139 free_destination_entry (de);
2145 * Free memory occupied by an entry in the tunnel map.
2149 * @param value a 'struct TunnelState *'
2150 * @return GNUNET_OK (continue to iterate)
2153 cleanup_tunnel (void *cls,
2154 const GNUNET_HashCode *key,
2157 struct TunnelState *ts = value;
2159 free_tunnel_state (ts);
2165 * Function scheduled as very last function, cleans up after us
2171 cleanup (void *cls GNUNET_UNUSED,
2172 const struct GNUNET_SCHEDULER_TaskContext *tc)
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 "VPN is shutting down\n");
2178 if (NULL != destination_map)
2180 GNUNET_CONTAINER_multihashmap_iterate (destination_map,
2181 &cleanup_destination,
2183 GNUNET_CONTAINER_multihashmap_destroy (destination_map);
2184 destination_map = NULL;
2186 if (NULL != destination_heap)
2188 GNUNET_CONTAINER_heap_destroy (destination_heap);
2189 destination_heap = NULL;
2191 if (NULL != tunnel_map)
2193 GNUNET_CONTAINER_multihashmap_iterate (tunnel_map,
2196 GNUNET_CONTAINER_multihashmap_destroy (tunnel_map);
2199 if (NULL != tunnel_heap)
2201 GNUNET_CONTAINER_heap_destroy (tunnel_heap);
2204 if (NULL != mesh_handle)
2206 GNUNET_MESH_disconnect (mesh_handle);
2209 if (NULL != helper_handle)
2211 GNUNET_HELPER_stop (helper_handle);
2212 helper_handle = NULL;
2216 GNUNET_SERVER_notification_context_destroy (nc);
2221 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
2225 GNUNET_free_non_null (vpn_argv[i]);
2230 * A client disconnected, clean up all references to it.
2232 * @param cls the client that disconnected
2234 * @param value a 'struct TunnelState *'
2235 * @return GNUNET_OK (continue to iterate)
2238 cleanup_tunnel_client (void *cls,
2239 const GNUNET_HashCode *key,
2242 struct GNUNET_SERVER_Client *client = cls;
2243 struct TunnelState *ts = value;
2245 if (client == ts->client)
2247 GNUNET_SERVER_client_drop (ts->client);
2255 * A client disconnected, clean up all references to it.
2257 * @param cls the client that disconnected
2259 * @param value a 'struct DestinationEntry *'
2260 * @return GNUNET_OK (continue to iterate)
2263 cleanup_destination_client (void *cls,
2264 const GNUNET_HashCode *key,
2267 struct GNUNET_SERVER_Client *client = cls;
2268 struct DestinationEntry *de = value;
2269 struct TunnelState *ts;
2271 if (NULL == (ts = de->ts))
2273 if (client == ts->client)
2275 GNUNET_SERVER_client_drop (ts->client);
2283 * A client has disconnected from us. If we are currently building
2284 * a tunnel for it, cancel the operation.
2287 * @param client handle to the client that disconnected
2290 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2292 if (NULL != tunnel_map)
2293 GNUNET_CONTAINER_multihashmap_iterate (tunnel_map,
2294 &cleanup_tunnel_client,
2296 if (NULL != destination_map)
2297 GNUNET_CONTAINER_multihashmap_iterate (destination_map,
2298 &cleanup_destination_client,
2304 * Main function that will be run by the scheduler.
2306 * @param cls closure
2307 * @param server the initialized server
2308 * @param cfg_ configuration
2312 struct GNUNET_SERVER_Handle *server,
2313 const struct GNUNET_CONFIGURATION_Handle *cfg_)
2315 static const struct GNUNET_SERVER_MessageHandler service_handlers[] = {
2316 /* callback, cls, type, size */
2317 {&service_redirect_to_ip, NULL, GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_IP, 0},
2318 {&service_redirect_to_service, NULL,
2319 GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_SERVICE,
2320 sizeof (struct RedirectToServiceRequestMessage) },
2323 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
2324 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
2325 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
2326 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
2327 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
2330 static const GNUNET_MESH_ApplicationType types[] = {
2331 GNUNET_APPLICATION_TYPE_END
2342 stats = GNUNET_STATISTICS_create ("vpn", cfg);
2344 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPING",
2345 &max_destination_mappings))
2346 max_destination_mappings = 200;
2348 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_TUNNELS",
2349 &max_tunnel_mappings))
2350 max_tunnel_mappings = 200;
2352 destination_map = GNUNET_CONTAINER_multihashmap_create (max_destination_mappings * 2);
2353 destination_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2354 tunnel_map = GNUNET_CONTAINER_multihashmap_create (max_tunnel_mappings * 2);
2355 tunnel_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2358 vpn_argv[0] = GNUNET_strdup ("vpn-gnunet");
2359 if (GNUNET_SYSERR ==
2360 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IFNAME", &ifname))
2362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2363 "No entry 'IFNAME' in configuration!\n");
2364 GNUNET_SCHEDULER_shutdown ();
2367 vpn_argv[1] = ifname;
2368 if ( (GNUNET_SYSERR ==
2369 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
2371 (1 != inet_pton (AF_INET6, ipv6addr, &v6))) )
2373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2374 "No valid entry 'IPV6ADDR' in configuration!\n");
2375 GNUNET_SCHEDULER_shutdown ();
2378 vpn_argv[2] = ipv6addr;
2379 if (GNUNET_SYSERR ==
2380 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6PREFIX",
2383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2384 "No entry 'IPV6PREFIX' in configuration!\n");
2385 GNUNET_SCHEDULER_shutdown ();
2388 vpn_argv[3] = ipv6prefix_s;
2390 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
2393 (ipv6prefix >= 127) )
2395 GNUNET_SCHEDULER_shutdown ();
2399 if ( (GNUNET_SYSERR ==
2400 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
2402 (1 != inet_pton (AF_INET, ipv4addr, &v4))) )
2404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2405 "No valid entry for 'IPV4ADDR' in configuration!\n");
2406 GNUNET_SCHEDULER_shutdown ();
2409 vpn_argv[4] = ipv4addr;
2410 if ( (GNUNET_SYSERR ==
2411 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK",
2413 (1 != inet_pton (AF_INET, ipv4mask, &v4))) )
2415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2416 "No valid entry 'IPV4MASK' in configuration!\n");
2417 GNUNET_SCHEDULER_shutdown ();
2420 vpn_argv[5] = ipv4mask;
2424 GNUNET_MESH_connect (cfg_, 42 /* queue length */, NULL,
2429 helper_handle = GNUNET_HELPER_start ("gnunet-helper-vpn", vpn_argv,
2430 &message_token, NULL);
2431 nc = GNUNET_SERVER_notification_context_create (server, 1);
2432 GNUNET_SERVER_add_handlers (server, service_handlers);
2433 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
2434 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
2439 * The main function of the VPN service.
2441 * @param argc number of arguments from the command line
2442 * @param argv command line arguments
2443 * @return 0 ok, 1 on error
2446 main (int argc, char *const *argv)
2448 return (GNUNET_OK ==
2449 GNUNET_SERVICE_run (argc, argv, "vpn",
2450 GNUNET_SERVICE_OPTION_NONE,
2451 &run, NULL)) ? 0 : 1;
2454 /* end of gnunet-service-vpn.c */