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 * - consider moving IP-header building / checksumming code into shared library
38 * with dns/exit/vpn (libgnunettun_tcpip?)
41 #include "gnunet_util_lib.h"
42 #include "gnunet_common.h"
43 #include "gnunet_protocols.h"
44 #include "gnunet_applications.h"
45 #include "gnunet_mesh_service.h"
46 #include "gnunet_statistics_service.h"
47 #include "gnunet_constants.h"
48 #include "tcpip_tun.h"
54 * Maximum number of messages we allow in the queue for mesh.
56 #define MAX_MESSAGE_QUEUE_SIZE 4
60 * State we keep for each of our tunnels.
66 * Information we track for each IP address to determine which tunnel
67 * to send the traffic over to the destination.
69 struct DestinationEntry
73 * Key under which this entry is in the 'destination_map' (only valid
74 * if 'heap_node != NULL').
79 * Pre-allocated tunnel for this destination, or NULL for none.
81 struct TunnelState *ts;
84 * Entry for this entry in the destination_heap.
86 struct GNUNET_CONTAINER_HeapNode *heap_node;
89 * GNUNET_NO if this is a tunnel to an Internet-exit,
90 * GNUNET_YES if this tunnel is to a service.
95 * Details about the connection (depending on is_service).
103 * The description of the service (only used for service tunnels).
105 GNUNET_HashCode service_descriptor;
108 * Peer offering the service.
110 struct GNUNET_PeerIdentity target;
112 } service_destination;
118 * Address family used (AF_INET or AF_INET6).
123 * IP address of the ultimate destination (only used for exit tunnels).
128 * Address if af is AF_INET.
133 * Address if af is AF_INET6.
146 * A messages we have in queue for a particular tunnel.
148 struct TunnelMessageQueueEntry
151 * This is a doubly-linked list.
153 struct TunnelMessageQueueEntry *next;
156 * This is a doubly-linked list.
158 struct TunnelMessageQueueEntry *prev;
161 * Number of bytes in 'msg'.
166 * Message to transmit, allocated at the end of this struct.
173 * State we keep for each of our tunnels.
179 * Information about the tunnel to use, NULL if no tunnel
180 * is available right now.
182 struct GNUNET_MESH_Tunnel *tunnel;
185 * Active transmission handle, NULL for none.
187 struct GNUNET_MESH_TransmitHandle *th;
190 * Entry for this entry in the tunnel_heap, NULL as long as this
191 * tunnel state is not fully bound.
193 struct GNUNET_CONTAINER_HeapNode *heap_node;
196 * Head of list of messages scheduled for transmission.
198 struct TunnelMessageQueueEntry *tmq_head;
201 * Tail of list of messages scheduled for transmission.
203 struct TunnelMessageQueueEntry *tmq_tail;
206 * Client that needs to be notified about the tunnel being
207 * up as soon as a peer is connected; NULL for none.
209 struct GNUNET_SERVER_Client *client;
212 * Destination entry that has a pointer to this tunnel state;
213 * NULL if this tunnel state is in the tunnel map.
215 struct DestinationEntry *destination_container;
218 * ID of the client request that caused us to setup this entry.
223 * Destination to which this tunnel leads. Note that
224 * this struct is NOT in the destination_map (but a
225 * local copy) and that the 'heap_node' should always
228 struct DestinationEntry destination;
231 * Task scheduled to destroy the tunnel (or NO_TASK).
233 GNUNET_SCHEDULER_TaskIdentifier destroy_task;
236 * Addess family used for this tunnel on the local TUN interface.
241 * Length of the doubly linked 'tmq_head/tmq_tail' list.
243 unsigned int tmq_length;
246 * IPPROTO_TCP or IPPROTO_UDP once bound.
251 * IP address of the source on our end, initially uninitialized.
256 * Address if af is AF_INET.
261 * Address if af is AF_INET6.
268 * Destination IP address used by the source on our end (this is the IP
269 * that we pick freely within the VPN's tunnel IP range).
274 * Address if af is AF_INET.
279 * Address if af is AF_INET6.
286 * Source port used by the sender on our end; 0 for uninitialized.
288 uint16_t source_port;
291 * Destination port used by the sender on our end; 0 for uninitialized.
293 uint16_t destination_port;
299 * Configuration we use.
301 static const struct GNUNET_CONFIGURATION_Handle *cfg;
304 * Handle to the mesh service.
306 static struct GNUNET_MESH_Handle *mesh_handle;
309 * Map from IP address to destination information (possibly with a
310 * MESH tunnel handle for fast setup).
312 static struct GNUNET_CONTAINER_MultiHashMap *destination_map;
315 * Min-Heap sorted by activity time to expire old mappings.
317 static struct GNUNET_CONTAINER_Heap *destination_heap;
320 * Map from source and destination address (IP+port) to connection
321 * information (mostly with the respective MESH tunnel handle).
323 static struct GNUNET_CONTAINER_MultiHashMap *tunnel_map;
326 * Min-Heap sorted by activity time to expire old mappings; values are
327 * of type 'struct TunnelState'.
329 static struct GNUNET_CONTAINER_Heap *tunnel_heap;
334 static struct GNUNET_STATISTICS_Handle *stats;
337 * The handle to the VPN helper process "gnunet-helper-vpn".
339 static struct GNUNET_HELPER_Handle *helper_handle;
342 * Arguments to the vpn helper.
344 static char *vpn_argv[7];
347 * Length of the prefix of the VPN's IPv6 network.
349 static unsigned long long ipv6prefix;
352 * Notification context for sending replies to clients.
354 static struct GNUNET_SERVER_NotificationContext *nc;
357 * If there are more than this number of address-mappings, old ones
360 static unsigned long long max_destination_mappings;
363 * If there are more than this number of open tunnels, old ones
366 static unsigned long long max_tunnel_mappings;
370 * Compute the key under which we would store an entry in the
371 * destination_map for the given IP address.
373 * @param af address family (AF_INET or AF_INET6)
374 * @param address IP address, struct in_addr or struct in6_addr
375 * @param key where to store the key
378 get_destination_key_from_ip (int af,
380 GNUNET_HashCode *key)
385 GNUNET_CRYPTO_hash (address,
386 sizeof (struct in_addr),
390 GNUNET_CRYPTO_hash (address,
391 sizeof (struct in6_addr),
402 * Compute the key under which we would store an entry in the
403 * tunnel_map for the given socket address pair.
405 * @param af address family (AF_INET or AF_INET6)
406 * @param protocol IPPROTO_TCP or IPPROTO_UDP
407 * @param source_ip sender's source IP, struct in_addr or struct in6_addr
408 * @param source_port sender's source port
409 * @param destination_ip sender's destination IP, struct in_addr or struct in6_addr
410 * @param destination_port sender's destination port
411 * @param key where to store the key
414 get_tunnel_key_from_ips (int af,
416 const void *source_ip,
417 uint16_t source_port,
418 const void *destination_ip,
419 uint16_t destination_port,
420 GNUNET_HashCode *key)
424 memset (key, 0, sizeof (GNUNET_HashCode));
425 /* the GNUnet hashmap only uses the first sizeof(unsigned int) of the hash,
426 so we put the ports in there (and hope for few collisions) */
428 memcpy (off, &source_port, sizeof (uint16_t));
429 off += sizeof (uint16_t);
430 memcpy (off, &destination_port, sizeof (uint16_t));
431 off += sizeof (uint16_t);
435 memcpy (off, source_ip, sizeof (struct in_addr));
436 off += sizeof (struct in_addr);
437 memcpy (off, destination_ip, sizeof (struct in_addr));
438 off += sizeof (struct in_addr);
441 memcpy (off, source_ip, sizeof (struct in6_addr));
442 off += sizeof (struct in6_addr);
443 memcpy (off, destination_ip, sizeof (struct in6_addr));
444 off += sizeof (struct in6_addr);
450 memcpy (off, &protocol, sizeof (uint8_t));
451 off += sizeof (uint8_t);
456 * Notify the client about the result of its request.
458 * @param client client to notify
459 * @param request_id original request ID to include in response
460 * @param result_af resulting address family
461 * @param addr resulting IP address
464 send_client_reply (struct GNUNET_SERVER_Client *client,
469 char buf[sizeof (struct RedirectToIpResponseMessage) + sizeof (struct in6_addr)];
470 struct RedirectToIpResponseMessage *res;
476 rlen = sizeof (struct in_addr);
479 rlen = sizeof (struct in6_addr);
488 res = (struct RedirectToIpResponseMessage *) buf;
489 res->header.size = htons (sizeof (struct RedirectToIpResponseMessage) + rlen);
490 res->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP);
491 res->result_af = htonl (result_af);
492 res->request_id = request_id;
493 memcpy (&res[1], addr, rlen);
494 GNUNET_SERVER_notification_context_add (nc, client);
495 GNUNET_SERVER_notification_context_unicast (nc,
503 * Destroy the mesh tunnel.
505 * @param cls the 'struct TunnelState' with the tunnel to destroy
506 * @param ts schedule context
509 destroy_tunnel_task (void *cls,
510 const struct GNUNET_SCHEDULER_TaskContext *tc)
512 struct TunnelState *ts = cls;
513 struct GNUNET_MESH_Tunnel *tunnel;
515 ts->destroy_task = GNUNET_SCHEDULER_NO_TASK;
516 if (NULL == (tunnel = ts->tunnel))
519 GNUNET_MESH_tunnel_destroy (tunnel);
524 * Method called whenever a peer has disconnected from the tunnel.
527 * @param peer peer identity the tunnel stopped working with
530 tunnel_peer_disconnect_handler (void *cls,
532 GNUNET_PeerIdentity * peer)
534 struct TunnelState *ts = cls;
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "Peer %s disconnected from tunnel.\n",
539 GNUNET_STATISTICS_update (stats,
540 gettext_noop ("# Peers connected to mesh tunnels"),
544 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
547 if (ts->destination.is_service)
548 return; /* hope for reconnect eventually */
549 /* as we are most likely going to change the exit node now,
550 we should just destroy the tunnel entirely... */
551 if (GNUNET_SCHEDULER_NO_TASK == ts->destroy_task)
552 ts->destroy_task = GNUNET_SCHEDULER_add_now (&destroy_tunnel_task, ts);
557 * Method called whenever a peer has connected to the tunnel. Notifies
558 * the waiting client that the tunnel is now up.
561 * @param peer peer identity the tunnel was created to, NULL on timeout
562 * @param atsi performance data for the connection
565 tunnel_peer_connect_handler (void *cls,
566 const struct GNUNET_PeerIdentity
569 GNUNET_ATS_Information * atsi)
571 struct TunnelState *ts = cls;
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
574 "Peer %s connected to tunnel.\n",
576 GNUNET_STATISTICS_update (stats,
577 gettext_noop ("# Peers connected to mesh tunnels"),
579 if (NULL == ts->client)
580 return; /* nothing to do */
581 send_client_reply (ts->client,
584 &ts->destination_ip);
585 GNUNET_SERVER_client_drop (ts->client);
591 * Send a message from the message queue via mesh.
593 * @param cls the 'struct TunnelState' with the message queue
594 * @param size number of bytes available in buf
595 * @param buf where to copy the message
596 * @return number of bytes copied to buf
599 send_to_peer_notify_callback (void *cls, size_t size, void *buf)
601 struct TunnelState *ts = cls;
602 struct TunnelMessageQueueEntry *tnq;
609 GNUNET_assert (NULL != tnq);
610 GNUNET_assert (size >= tnq->len);
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 "Sending %u bytes via mesh tunnel\n",
614 GNUNET_CONTAINER_DLL_remove (ts->tmq_head,
618 memcpy (buf, tnq->msg, tnq->len);
621 if (NULL != (tnq = ts->tmq_head))
622 ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
623 GNUNET_NO /* cork */,
625 GNUNET_TIME_UNIT_FOREVER_REL,
628 &send_to_peer_notify_callback,
630 GNUNET_STATISTICS_update (stats,
631 gettext_noop ("# Bytes given to mesh for transmission"),
638 * Add the given message to the given tunnel and trigger the
639 * transmission process.
641 * @param tnq message to queue
642 * @param ts tunnel to queue the message for
645 send_to_tunnel (struct TunnelMessageQueueEntry *tnq,
646 struct TunnelState *ts)
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
649 "Queueing %u bytes for transmission via mesh tunnel\n",
651 GNUNET_assert (NULL != ts->tunnel);
652 GNUNET_CONTAINER_DLL_insert_tail (ts->tmq_head,
656 if (ts->tmq_length > MAX_MESSAGE_QUEUE_SIZE)
658 struct TunnelMessageQueueEntry *dq;
661 GNUNET_assert (dq != tnq);
662 GNUNET_CONTAINER_DLL_remove (ts->tmq_head,
665 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
667 GNUNET_STATISTICS_update (stats,
668 gettext_noop ("# Bytes dropped in mesh queue (overflow)"),
674 ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel,
675 GNUNET_NO /* cork */,
677 GNUNET_TIME_UNIT_FOREVER_REL,
680 &send_to_peer_notify_callback,
686 * Initialize the given destination entry's mesh tunnel.
688 * @param de destination entry for which we need to setup a tunnel
689 * @param client client to notify on successful tunnel setup, or NULL for none
690 * @param request_id request ID to send in client notification (unused if client is NULL)
691 * @return tunnel state of the tunnel that was created
693 static struct TunnelState *
694 create_tunnel_to_destination (struct DestinationEntry *de,
695 struct GNUNET_SERVER_Client *client,
698 struct TunnelState *ts;
700 GNUNET_STATISTICS_update (stats,
701 gettext_noop ("# Mesh tunnels created"),
703 GNUNET_assert (NULL == de->ts);
704 ts = GNUNET_malloc (sizeof (struct TunnelState));
707 ts->request_id = request_id;
709 GNUNET_SERVER_client_keep (client);
711 ts->destination = *de;
712 ts->destination.heap_node = NULL; /* copy is NOT in destination heap */
714 ts->destination_container = de; /* we are referenced from de */
715 ts->af = AF_UNSPEC; /* so far, unknown */
716 ts->tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
718 &tunnel_peer_connect_handler,
719 &tunnel_peer_disconnect_handler,
721 if (NULL == ts->tunnel)
723 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
724 _("Failed to setup mesh tunnel!\n"));
726 GNUNET_SERVER_client_drop (client);
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Creating tunnel to peer %s offering service %s\n",
734 GNUNET_i2s (&de->details.service_destination.target),
735 GNUNET_h2s (&de->details.service_destination.service_descriptor));
736 GNUNET_MESH_peer_request_connect_add (ts->tunnel,
737 &de->details.service_destination.target);
741 switch (de->details.exit_destination.af)
744 GNUNET_MESH_peer_request_connect_by_type (ts->tunnel,
745 GNUNET_APPLICATION_TYPE_IPV4_GATEWAY);
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747 "Creating tunnel to exit peer for %s\n",
751 GNUNET_MESH_peer_request_connect_by_type (ts->tunnel,
752 GNUNET_APPLICATION_TYPE_IPV6_GATEWAY);
753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754 "Creating tunnel to exit peer for %s\n",
767 * Free resources associated with a tunnel state.
769 * @param ts state to free
772 free_tunnel_state (struct TunnelState *ts)
775 struct TunnelMessageQueueEntry *tnq;
776 struct GNUNET_MESH_Tunnel *tunnel;
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Cleaning up tunnel state\n");
780 GNUNET_STATISTICS_update (stats,
781 gettext_noop ("# Active tunnels"),
783 if (GNUNET_SCHEDULER_NO_TASK != ts->destroy_task)
785 GNUNET_SCHEDULER_cancel (ts->destroy_task);
786 ts->destroy_task = GNUNET_SCHEDULER_NO_TASK;
788 while (NULL != (tnq = ts->tmq_head))
790 GNUNET_CONTAINER_DLL_remove (ts->tmq_head,
796 GNUNET_assert (0 == ts->tmq_length);
797 if (NULL != ts->client)
799 GNUNET_SERVER_client_drop (ts->client);
804 GNUNET_MESH_notify_transmit_ready_cancel (ts->th);
807 GNUNET_assert (NULL == ts->destination.heap_node);
808 if (NULL != (tunnel = ts->tunnel))
811 GNUNET_MESH_tunnel_destroy (tunnel);
813 if (NULL != ts->heap_node)
815 GNUNET_CONTAINER_heap_remove_node (ts->heap_node);
816 ts->heap_node = NULL;
817 get_tunnel_key_from_ips (ts->af,
822 ts->destination_port,
824 GNUNET_assert (GNUNET_YES ==
825 GNUNET_CONTAINER_multihashmap_remove (tunnel_map,
829 if (NULL != ts->destination_container)
831 GNUNET_assert (ts == ts->destination_container->ts);
832 ts->destination_container->ts = NULL;
833 ts->destination_container = NULL;
840 * We have too many active tunnels. Clean up the oldest tunnel.
842 * @param except tunnel that must NOT be cleaned up, even if it is the oldest
845 expire_tunnel (struct TunnelState *except)
847 struct TunnelState *ts;
849 ts = GNUNET_CONTAINER_heap_peek (tunnel_heap);
851 return; /* can't do this */
852 free_tunnel_state (ts);
857 * Route a packet via mesh to the given destination.
859 * @param destination description of the destination
860 * @param af address family on this end (AF_INET or AF_INET6)
861 * @param protocol IPPROTO_TCP or IPPROTO_UDP
862 * @param source_ip source IP used by the sender (struct in_addr or struct in6_addr)
863 * @param destination_ip destination IP used by the sender (struct in_addr or struct in6_addr)
864 * @param payload payload of the packet after the IP header
865 * @param payload_length number of bytes in payload
868 route_packet (struct DestinationEntry *destination,
871 const void *source_ip,
872 const void *destination_ip,
874 size_t payload_length)
877 struct TunnelState *ts;
878 struct TunnelMessageQueueEntry *tnq;
882 const struct udp_packet *udp;
883 const struct tcp_packet *tcp;
891 if (payload_length < sizeof (struct udp_packet))
898 spt = ntohs (udp->spt);
899 dpt = ntohs (udp->dpt);
900 get_tunnel_key_from_ips (af,
911 if (payload_length < sizeof (struct tcp_packet))
918 spt = ntohs (tcp->spt);
919 dpt = ntohs (tcp->dpt);
920 get_tunnel_key_from_ips (af,
930 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
931 _("Protocol %u not supported, dropping\n"),
932 (unsigned int) protocol);
935 if (! destination->is_service)
937 switch (destination->details.exit_destination.af)
940 alen = sizeof (struct in_addr);
943 alen = sizeof (struct in6_addr);
951 char sbuf[INET6_ADDRSTRLEN];
952 char dbuf[INET6_ADDRSTRLEN];
953 char xbuf[INET6_ADDRSTRLEN];
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "Routing %s packet from %s:%u -> %s:%u to destination %s:%u\n",
957 (protocol == IPPROTO_TCP) ? "TCP" : "UDP",
958 inet_ntop (af, source_ip, sbuf, sizeof (sbuf)),
960 inet_ntop (af, destination_ip, dbuf, sizeof (dbuf)),
962 inet_ntop (destination->details.exit_destination.af,
963 &destination->details.exit_destination.ip,
964 xbuf, sizeof (xbuf)),
970 /* make compiler happy */
973 char sbuf[INET6_ADDRSTRLEN];
974 char dbuf[INET6_ADDRSTRLEN];
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
977 "Routing %s packet from %s:%u -> %s:%u to service %s at peer %s\n",
978 (protocol == IPPROTO_TCP) ? "TCP" : "UDP",
979 inet_ntop (af, source_ip, sbuf, sizeof (sbuf)),
981 inet_ntop (af, destination_ip, dbuf, sizeof (dbuf)),
983 GNUNET_h2s (&destination->details.service_destination.service_descriptor),
984 GNUNET_i2s (&destination->details.service_destination.target));
989 /* see if we have an existing tunnel for this destination */
990 ts = GNUNET_CONTAINER_multihashmap_get (tunnel_map,
994 /* need to either use the existing tunnel from the destination (if still
995 available) or create a fresh one */
997 if (NULL == destination->ts)
998 ts = create_tunnel_to_destination (destination, NULL, 0);
1000 ts = destination->ts;
1003 destination->ts = NULL;
1004 ts->destination_container = NULL; /* no longer 'contained' */
1005 /* now bind existing "unbound" tunnel to our IP/port tuple */
1006 ts->protocol = protocol;
1010 ts->source_ip.v4 = * (const struct in_addr *) source_ip;
1011 ts->destination_ip.v4 = * (const struct in_addr *) destination_ip;
1015 ts->source_ip.v6 = * (const struct in6_addr *) source_ip;
1016 ts->destination_ip.v6 = * (const struct in6_addr *) destination_ip;
1018 ts->source_port = spt;
1019 ts->destination_port = dpt;
1020 ts->heap_node = GNUNET_CONTAINER_heap_insert (tunnel_heap,
1022 GNUNET_TIME_absolute_get ().abs_value);
1023 GNUNET_assert (GNUNET_YES ==
1024 GNUNET_CONTAINER_multihashmap_put (tunnel_map,
1027 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1028 GNUNET_STATISTICS_update (stats,
1029 gettext_noop ("# Active tunnels"),
1031 while (GNUNET_CONTAINER_multihashmap_size (tunnel_map) > max_tunnel_mappings)
1037 GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
1039 GNUNET_TIME_absolute_get ().abs_value);
1041 GNUNET_assert (NULL != ts->tunnel);
1043 /* send via tunnel */
1047 if (destination->is_service)
1049 struct GNUNET_EXIT_UdpServiceMessage *usm;
1051 mlen = sizeof (struct GNUNET_EXIT_UdpServiceMessage) +
1052 payload_length - sizeof (struct udp_packet);
1053 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1058 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
1061 usm = (struct GNUNET_EXIT_UdpServiceMessage *) &tnq[1];
1062 usm->header.size = htons ((uint16_t) mlen);
1063 usm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_UDP_TO_SERVICE);
1064 /* if the source port is below 32000, we assume it has a special
1065 meaning; if not, we pick a random port (this is a heuristic) */
1066 usm->source_port = (ntohs (udp->spt) < 32000) ? udp->spt : 0;
1067 usm->destination_port = udp->dpt;
1068 usm->service_descriptor = destination->details.service_destination.service_descriptor;
1071 payload_length - sizeof (struct udp_packet));
1075 struct GNUNET_EXIT_UdpInternetMessage *uim;
1076 struct in_addr *ip4dst;
1077 struct in6_addr *ip6dst;
1080 mlen = sizeof (struct GNUNET_EXIT_UdpInternetMessage) +
1081 alen + payload_length - sizeof (struct udp_packet);
1082 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1087 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) +
1091 uim = (struct GNUNET_EXIT_UdpInternetMessage *) &tnq[1];
1092 uim->header.size = htons ((uint16_t) mlen);
1093 uim->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_UDP_TO_INTERNET);
1094 uim->af = htonl (destination->details.exit_destination.af);
1095 uim->source_port = (ntohs (udp->spt) < 32000) ? udp->spt : 0;
1096 uim->destination_port = udp->dpt;
1097 switch (destination->details.exit_destination.af)
1100 ip4dst = (struct in_addr *) &uim[1];
1101 *ip4dst = destination->details.exit_destination.ip.v4;
1102 payload = &ip4dst[1];
1105 ip6dst = (struct in6_addr *) &uim[1];
1106 *ip6dst = destination->details.exit_destination.ip.v6;
1107 payload = &ip6dst[1];
1114 payload_length - sizeof (struct udp_packet));
1120 if (destination->is_service)
1122 struct GNUNET_EXIT_TcpServiceStartMessage *tsm;
1124 mlen = sizeof (struct GNUNET_EXIT_TcpServiceStartMessage) +
1125 payload_length - sizeof (struct tcp_packet);
1126 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1131 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
1134 tsm = (struct GNUNET_EXIT_TcpServiceStartMessage *) &tnq[1];
1135 tsm->header.size = htons ((uint16_t) mlen);
1136 tsm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_TO_SERVICE_START);
1137 tsm->reserved = htonl (0);
1138 tsm->service_descriptor = destination->details.service_destination.service_descriptor;
1139 tsm->tcp_header = *tcp;
1142 payload_length - sizeof (struct tcp_packet));
1146 struct GNUNET_EXIT_TcpInternetStartMessage *tim;
1147 struct in_addr *ip4dst;
1148 struct in6_addr *ip6dst;
1151 mlen = sizeof (struct GNUNET_EXIT_TcpInternetStartMessage) +
1152 alen + payload_length - sizeof (struct tcp_packet);
1153 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1158 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
1161 tim = (struct GNUNET_EXIT_TcpInternetStartMessage *) &tnq[1];
1162 tim->header.size = htons ((uint16_t) mlen);
1163 tim->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_TO_INTERNET_START);
1164 tim->af = htonl (destination->details.exit_destination.af);
1165 tim->tcp_header = *tcp;
1166 switch (destination->details.exit_destination.af)
1169 ip4dst = (struct in_addr *) &tim[1];
1170 *ip4dst = destination->details.exit_destination.ip.v4;
1171 payload = &ip4dst[1];
1174 ip6dst = (struct in6_addr *) &tim[1];
1175 *ip6dst = destination->details.exit_destination.ip.v6;
1176 payload = &ip6dst[1];
1183 payload_length - sizeof (struct tcp_packet));
1188 struct GNUNET_EXIT_TcpDataMessage *tdm;
1190 mlen = sizeof (struct GNUNET_EXIT_TcpDataMessage) +
1191 alen + payload_length - sizeof (struct tcp_packet);
1192 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1197 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueueEntry) + mlen);
1200 tdm = (struct GNUNET_EXIT_TcpDataMessage *) &tnq[1];
1201 tdm->header.size = htons ((uint16_t) mlen);
1202 tdm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_DATA);
1203 tdm->reserved = htonl (0);
1204 tdm->tcp_header = *tcp;
1207 payload_length - sizeof (struct tcp_packet));
1211 /* not supported above, how can we get here !? */
1215 send_to_tunnel (tnq, ts);
1220 * Receive packets from the helper-process (someone send to the local
1221 * virtual tunnel interface). Find the destination mapping, and if it
1222 * exists, identify the correct MESH tunnel (or possibly create it)
1223 * and forward the packet.
1225 * @param cls closure, NULL
1226 * @param client NULL
1227 * @param message message we got from the client (VPN tunnel interface)
1230 message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
1231 const struct GNUNET_MessageHeader *message)
1233 const struct tun_header *tun;
1235 GNUNET_HashCode key;
1236 struct DestinationEntry *de;
1238 GNUNET_STATISTICS_update (stats,
1239 gettext_noop ("# Packets received from TUN interface"),
1241 mlen = ntohs (message->size);
1242 if ( (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER) ||
1243 (mlen < sizeof (struct GNUNET_MessageHeader) + sizeof (struct tun_header)) )
1248 tun = (const struct tun_header *) &message[1];
1249 mlen -= (sizeof (struct GNUNET_MessageHeader) + sizeof (struct tun_header));
1250 switch (ntohs (tun->proto))
1254 const struct ip6_header *pkt6;
1256 if (mlen < sizeof (struct ip6_header))
1262 pkt6 = (const struct ip6_header *) &tun[1];
1263 get_destination_key_from_ip (AF_INET6,
1264 &pkt6->destination_address,
1266 de = GNUNET_CONTAINER_multihashmap_get (destination_map, &key);
1267 /* FIXME: do we need to guard against hash collision?
1268 (if so, we need to also store the local destination IP in the
1269 destination entry and then compare here; however, the risk
1270 of collision seems minimal AND the impact is unlikely to be
1271 super-problematic as well... */
1274 char buf[INET6_ADDRSTRLEN];
1276 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1277 _("Packet received for unmapped destination `%s' (dropping it)\n"),
1278 inet_ntop (AF_INET6,
1279 &pkt6->destination_address,
1287 &pkt6->source_address,
1288 &pkt6->destination_address,
1290 mlen - sizeof (struct ip6_header));
1295 struct ip4_header *pkt4;
1297 if (mlen < sizeof (struct ip4_header))
1303 pkt4 = (struct ip4_header *) &tun[1];
1304 get_destination_key_from_ip (AF_INET,
1305 &pkt4->destination_address,
1307 de = GNUNET_CONTAINER_multihashmap_get (destination_map, &key);
1308 /* FIXME: do we need to guard against hash collision?
1309 (if so, we need to also store the local destination IP in the
1310 destination entry and then compare here; however, the risk
1311 of collision seems minimal AND the impact is unlikely to be
1312 super-problematic as well... */
1315 char buf[INET_ADDRSTRLEN];
1317 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1318 _("Packet received for unmapped destination `%s' (dropping it)\n"),
1320 &pkt4->destination_address,
1325 if (pkt4->header_length * 4 != sizeof (struct ip4_header))
1327 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1328 _("Received IPv4 packet with options (dropping it)\n"));
1334 &pkt4->source_address,
1335 &pkt4->destination_address,
1337 mlen - sizeof (struct ip4_header));
1341 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1342 _("Received packet of unknown protocol %d from TUN (dropping it)\n"),
1343 (unsigned int) ntohs (tun->proto));
1350 * We got a UDP packet back from the MESH tunnel. Pass it on to the
1351 * local virtual interface via the helper.
1353 * @param cls closure, NULL
1354 * @param tunnel connection to the other end
1355 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1356 * @param sender who sent the message
1357 * @param message the actual message
1358 * @param atsi performance data for the connection
1359 * @return GNUNET_OK to keep the connection open,
1360 * GNUNET_SYSERR to close it (signal serious error)
1363 receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1364 void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
1365 const struct GNUNET_MessageHeader *message,
1366 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1368 struct TunnelState *ts = *tunnel_ctx;
1369 const struct GNUNET_EXIT_UdpReplyMessage *reply;
1372 GNUNET_STATISTICS_update (stats,
1373 gettext_noop ("# UDP packets received from mesh"),
1375 mlen = ntohs (message->size);
1376 if (mlen < sizeof (struct GNUNET_EXIT_UdpReplyMessage))
1378 GNUNET_break_op (0);
1379 return GNUNET_SYSERR;
1381 if (NULL == ts->heap_node)
1383 GNUNET_break_op (0);
1384 return GNUNET_SYSERR;
1386 if (AF_UNSPEC == ts->af)
1388 GNUNET_break_op (0);
1389 return GNUNET_SYSERR;
1391 reply = (const struct GNUNET_EXIT_UdpReplyMessage *) message;
1392 mlen -= sizeof (struct GNUNET_EXIT_UdpReplyMessage);
1394 char sbuf[INET6_ADDRSTRLEN];
1395 char dbuf[INET6_ADDRSTRLEN];
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 "Received UDP reply from mesh, sending %u bytes from %s:%u -> %s:%u via TUN\n",
1399 (unsigned int) mlen,
1400 inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
1401 ts->destination_port,
1402 inet_ntop (ts->af, &ts->source_ip, dbuf, sizeof (dbuf)),
1409 size_t size = sizeof (struct ip4_header)
1410 + sizeof (struct udp_packet)
1411 + sizeof (struct GNUNET_MessageHeader) +
1412 sizeof (struct tun_header) +
1416 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1417 struct tun_header *tun = (struct tun_header*) &msg[1];
1418 struct ip4_header *ipv4 = (struct ip4_header *) &tun[1];
1419 struct udp_packet *udp = (struct udp_packet *) &ipv4[1];
1420 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1421 msg->size = htons (size);
1422 tun->flags = htons (0);
1423 tun->proto = htons (ETH_P_IPV4);
1425 ipv4->header_length = sizeof (struct ip4_header) / 4;
1426 ipv4->diff_serv = 0;
1427 ipv4->total_length = htons (sizeof (struct ip4_header) +
1428 sizeof (struct udp_packet) +
1430 ipv4->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1433 ipv4->fragmentation_offset = 0;
1435 ipv4->protocol = IPPROTO_UDP;
1437 ipv4->source_address = ts->destination_ip.v4;
1438 ipv4->destination_address = ts->source_ip.v4;
1440 GNUNET_CRYPTO_crc16_n (ipv4, sizeof (struct ip4_header));
1441 if (0 == ntohs (reply->source_port))
1442 udp->spt = htons (ts->destination_port);
1444 udp->spt = reply->source_port;
1445 if (0 == ntohs (reply->destination_port))
1446 udp->dpt = htons (ts->source_port);
1448 udp->dpt = reply->destination_port;
1449 udp->len = htons (mlen + sizeof (struct udp_packet));
1450 udp->crc = 0; // FIXME: optional, but we might want to calculate this one anyway
1454 (void) GNUNET_HELPER_send (helper_handle,
1463 size_t size = sizeof (struct ip6_header)
1464 + sizeof (struct udp_packet)
1465 + sizeof (struct GNUNET_MessageHeader) +
1466 sizeof (struct tun_header) +
1470 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1471 struct tun_header *tun = (struct tun_header*) &msg[1];
1472 struct ip6_header *ipv6 = (struct ip6_header *) &tun[1];
1473 struct udp_packet *udp = (struct udp_packet *) &ipv6[1];
1474 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1475 msg->size = htons (size);
1476 tun->flags = htons (0);
1477 tun->proto = htons (ETH_P_IPV6);
1478 ipv6->traffic_class_h = 0;
1480 ipv6->traffic_class_l = 0;
1481 ipv6->flow_label = 0;
1482 ipv6->payload_length = htons (sizeof (struct udp_packet) + sizeof (struct ip6_header) + mlen);
1483 ipv6->next_header = IPPROTO_UDP;
1484 ipv6->hop_limit = 255;
1485 ipv6->source_address = ts->destination_ip.v6;
1486 ipv6->destination_address = ts->source_ip.v6;
1487 if (0 == ntohs (reply->source_port))
1488 udp->spt = htons (ts->destination_port);
1490 udp->spt = reply->source_port;
1491 if (0 == ntohs (reply->destination_port))
1492 udp->dpt = htons (ts->source_port);
1494 udp->dpt = reply->destination_port;
1495 udp->len = htons (mlen + sizeof (struct udp_packet));
1503 GNUNET_CRYPTO_crc16_step (sum, &ipv6->source_address,
1504 sizeof (struct in6_addr) * 2);
1505 uint32_t tmp = udp->len;
1506 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1507 tmp = htons (IPPROTO_UDP);
1508 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1509 sum = GNUNET_CRYPTO_crc16_step (sum,
1512 udp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1514 (void) GNUNET_HELPER_send (helper_handle,
1524 GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
1526 GNUNET_TIME_absolute_get ().abs_value);
1532 * We got a TCP packet back from the MESH tunnel. Pass it on to the
1533 * local virtual interface via the helper.
1535 * @param cls closure, NULL
1536 * @param tunnel connection to the other end
1537 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1538 * @param sender who sent the message
1539 * @param message the actual message
1540 * @param atsi performance data for the connection
1541 * @return GNUNET_OK to keep the connection open,
1542 * GNUNET_SYSERR to close it (signal serious error)
1545 receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1547 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1548 const struct GNUNET_MessageHeader *message,
1549 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1551 struct TunnelState *ts = *tunnel_ctx;
1552 const struct GNUNET_EXIT_TcpDataMessage *data;
1555 GNUNET_STATISTICS_update (stats,
1556 gettext_noop ("# TCP packets received from mesh"),
1558 mlen = ntohs (message->size);
1559 if (mlen < sizeof (struct GNUNET_EXIT_TcpDataMessage))
1561 GNUNET_break_op (0);
1562 return GNUNET_SYSERR;
1564 if (NULL == ts->heap_node)
1566 GNUNET_break_op (0);
1567 return GNUNET_SYSERR;
1569 data = (const struct GNUNET_EXIT_TcpDataMessage *) message;
1570 mlen -= sizeof (struct GNUNET_EXIT_TcpDataMessage);
1572 char sbuf[INET6_ADDRSTRLEN];
1573 char dbuf[INET6_ADDRSTRLEN];
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1576 "Received TCP reply from mesh, sending %u bytes from %s:%u -> %s:%u via TUN\n",
1577 (unsigned int) mlen,
1578 inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
1579 ts->destination_port,
1580 inet_ntop (ts->af, &ts->source_ip, dbuf, sizeof (dbuf)),
1587 size_t size = sizeof (struct ip4_header)
1588 + sizeof (struct tcp_packet)
1589 + sizeof (struct GNUNET_MessageHeader) +
1590 sizeof (struct tun_header) +
1594 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1595 struct tun_header *tun = (struct tun_header*) &msg[1];
1596 struct ip4_header *ipv4 = (struct ip4_header *) &tun[1];
1597 struct tcp_packet *tcp = (struct tcp_packet *) &ipv4[1];
1598 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1599 msg->size = htons (size);
1600 tun->flags = htons (0);
1601 tun->proto = htons (ETH_P_IPV4);
1603 ipv4->header_length = sizeof (struct ip4_header) / 4;
1604 ipv4->diff_serv = 0;
1605 ipv4->total_length = htons (sizeof (struct ip4_header) +
1606 sizeof (struct tcp_packet) +
1608 ipv4->identification = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1611 ipv4->fragmentation_offset = 0;
1613 ipv4->protocol = IPPROTO_TCP;
1615 ipv4->source_address = ts->destination_ip.v4;
1616 ipv4->destination_address = ts->source_ip.v4;
1618 GNUNET_CRYPTO_crc16_n (ipv4, sizeof (struct ip4_header));
1619 *tcp = data->tcp_header;
1620 tcp->spt = htons (ts->destination_port);
1621 tcp->dpt = htons (ts->source_port);
1630 sum = GNUNET_CRYPTO_crc16_step (sum,
1631 &ipv4->source_address,
1632 2 * sizeof (struct in_addr));
1633 tmp = htonl ((IPPROTO_TCP << 16) | (mlen + sizeof (struct tcp_packet)));
1634 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1635 sum = GNUNET_CRYPTO_crc16_step (sum, tcp, mlen + sizeof (struct tcp_packet));
1636 tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1638 (void) GNUNET_HELPER_send (helper_handle,
1647 size_t size = sizeof (struct ip6_header)
1648 + sizeof (struct tcp_packet)
1649 + sizeof (struct GNUNET_MessageHeader) +
1650 sizeof (struct tun_header) +
1654 struct GNUNET_MessageHeader *msg = (struct GNUNET_MessageHeader *) buf;
1655 struct tun_header *tun = (struct tun_header*) &msg[1];
1656 struct ip6_header *ipv6 = (struct ip6_header *) &tun[1];
1657 struct tcp_packet *tcp = (struct tcp_packet *) &ipv6[1];
1658 msg->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1659 msg->size = htons (size);
1660 tun->flags = htons (0);
1661 tun->proto = htons (ETH_P_IPV6);
1662 ipv6->traffic_class_h = 0;
1664 ipv6->traffic_class_l = 0;
1665 ipv6->flow_label = 0;
1666 ipv6->payload_length = htons (sizeof (struct tcp_packet) + sizeof (struct ip6_header) + mlen);
1667 ipv6->next_header = IPPROTO_TCP;
1668 ipv6->hop_limit = 255;
1669 ipv6->source_address = ts->destination_ip.v6;
1670 ipv6->destination_address = ts->source_ip.v6;
1671 tcp->spt = htons (ts->destination_port);
1672 tcp->dpt = htons (ts->source_port);
1678 sum = GNUNET_CRYPTO_crc16_step (sum, &ipv6->source_address, 2 * sizeof (struct in6_addr));
1679 tmp = htonl (sizeof (struct tcp_packet) + mlen);
1680 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1681 tmp = htonl (IPPROTO_TCP);
1682 sum = GNUNET_CRYPTO_crc16_step (sum, &tmp, sizeof (uint32_t));
1683 sum = GNUNET_CRYPTO_crc16_step (sum, tcp,
1684 sizeof (struct tcp_packet) + mlen);
1685 tcp->crc = GNUNET_CRYPTO_crc16_finish (sum);
1687 (void) GNUNET_HELPER_send (helper_handle,
1695 GNUNET_CONTAINER_heap_update_cost (tunnel_heap,
1697 GNUNET_TIME_absolute_get ().abs_value);
1703 * Allocate an IPv4 address from the range of the tunnel
1704 * for a new redirection.
1706 * @param v4 where to store the address
1707 * @return GNUNET_OK on success,
1708 * GNUNET_SYSERR on error
1711 allocate_v4_address (struct in_addr *v4)
1713 const char *ipv4addr = vpn_argv[4];
1714 const char *ipv4mask = vpn_argv[5];
1715 struct in_addr addr;
1716 struct in_addr mask;
1718 GNUNET_HashCode key;
1721 GNUNET_assert (1 == inet_pton (AF_INET, ipv4addr, &addr));
1722 GNUNET_assert (1 == inet_pton (AF_INET, ipv4mask, &mask));
1723 /* Given 192.168.0.1/255.255.0.0, we want a mask
1724 of '192.168.255.255', thus: */
1725 mask.s_addr = addr.s_addr | ~mask.s_addr;
1732 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1733 _("Failed to find unallocated IPv4 address in VPN's range\n"));
1734 return GNUNET_SYSERR;
1736 /* Pick random IPv4 address within the subnet, except 'addr' or 'mask' itself */
1737 rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1739 v4->s_addr = (addr.s_addr | rnd.s_addr) & mask.s_addr;
1740 get_destination_key_from_ip (AF_INET,
1744 while ( (GNUNET_YES ==
1745 GNUNET_CONTAINER_multihashmap_contains (destination_map,
1747 (v4->s_addr == addr.s_addr) ||
1748 (v4->s_addr == mask.s_addr) );
1754 * Allocate an IPv6 address from the range of the tunnel
1755 * for a new redirection.
1757 * @param v6 where to store the address
1758 * @return GNUNET_OK on success,
1759 * GNUNET_SYSERR on error
1762 allocate_v6_address (struct in6_addr *v6)
1764 const char *ipv6addr = vpn_argv[2];
1765 struct in6_addr addr;
1766 struct in6_addr mask;
1767 struct in6_addr rnd;
1769 GNUNET_HashCode key;
1772 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6addr, &addr));
1773 GNUNET_assert (ipv6prefix < 128);
1774 /* Given ABCD::/96, we want a mask of 'ABCD::FFFF:FFFF,
1777 for (i=127;i>=128-ipv6prefix;i--)
1778 mask.s6_addr[i / 8] |= (1 << (i % 8));
1780 /* Pick random IPv6 address within the subnet, except 'addr' or 'mask' itself */
1787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1788 _("Failed to find unallocated IPv6 address in VPN's range\n"));
1789 return GNUNET_SYSERR;
1794 rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1797 = (addr.s6_addr[i] | rnd.s6_addr[i]) & mask.s6_addr[i];
1799 get_destination_key_from_ip (AF_INET6,
1803 while ( (GNUNET_YES ==
1804 GNUNET_CONTAINER_multihashmap_contains (destination_map,
1808 sizeof (struct in6_addr))) ||
1811 sizeof (struct in6_addr))) );
1817 * Free resources occupied by a destination entry.
1819 * @param de entry to free
1822 free_destination_entry (struct DestinationEntry *de)
1824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1825 "Cleaning up destination entry\n");
1826 GNUNET_STATISTICS_update (stats,
1827 gettext_noop ("# Active destinations"),
1831 free_tunnel_state (de->ts);
1832 GNUNET_assert (NULL == de->ts);
1834 if (NULL != de->heap_node)
1836 GNUNET_CONTAINER_heap_remove_node (de->heap_node);
1837 de->heap_node = NULL;
1838 GNUNET_assert (GNUNET_YES ==
1839 GNUNET_CONTAINER_multihashmap_remove (destination_map,
1848 * We have too many active destinations. Clean up the oldest destination.
1850 * @param except destination that must NOT be cleaned up, even if it is the oldest
1853 expire_destination (struct DestinationEntry *except)
1855 struct DestinationEntry *de;
1857 de = GNUNET_CONTAINER_heap_peek (destination_heap);
1859 return; /* can't do this */
1860 free_destination_entry (de);
1865 * A client asks us to setup a redirection via some exit
1866 * node to a particular IP. Setup the redirection and
1867 * give the client the allocated IP.
1870 * @param client requesting client
1871 * @param message redirection request (a 'struct RedirectToIpRequestMessage')
1874 service_redirect_to_ip (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
1875 const struct GNUNET_MessageHeader *message)
1879 const struct RedirectToIpRequestMessage *msg;
1885 struct DestinationEntry *de;
1886 GNUNET_HashCode key;
1888 /* validate and parse request */
1889 mlen = ntohs (message->size);
1890 if (mlen < sizeof (struct RedirectToIpRequestMessage))
1893 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1896 alen = mlen - sizeof (struct RedirectToIpRequestMessage);
1897 msg = (const struct RedirectToIpRequestMessage *) message;
1898 addr_af = (int) htonl (msg->addr_af);
1902 if (alen != sizeof (struct in_addr))
1905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1910 if (alen != sizeof (struct in6_addr))
1913 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1923 /* allocate response IP */
1925 result_af = (int) htonl (msg->result_af);
1930 allocate_v4_address (&v4))
1931 result_af = AF_UNSPEC;
1937 allocate_v6_address (&v6))
1938 result_af = AF_UNSPEC;
1944 allocate_v4_address (&v4))
1947 result_af = AF_INET;
1949 else if (GNUNET_OK ==
1950 allocate_v6_address (&v6))
1953 result_af = AF_INET6;
1958 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1961 if ( (result_af == AF_UNSPEC) ||
1962 (GNUNET_NO == ntohl (msg->nac)) )
1964 /* send reply "instantly" */
1965 send_client_reply (client,
1970 if (result_af == AF_UNSPEC)
1972 /* failure, we're done */
1973 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1978 char sbuf[INET6_ADDRSTRLEN];
1979 char dbuf[INET6_ADDRSTRLEN];
1981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1982 "Allocated address %s for redirection via exit to %s\n",
1983 inet_ntop (result_af, addr, sbuf, sizeof (sbuf)),
1985 &msg[1], dbuf, sizeof (dbuf)));
1988 /* setup destination record */
1989 de = GNUNET_malloc (sizeof (struct DestinationEntry));
1990 de->is_service = GNUNET_NO;
1991 de->details.exit_destination.af = addr_af;
1992 memcpy (&de->details.exit_destination.ip,
1995 get_destination_key_from_ip (result_af,
1999 GNUNET_assert (GNUNET_OK ==
2000 GNUNET_CONTAINER_multihashmap_put (destination_map,
2003 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2004 de->heap_node = GNUNET_CONTAINER_heap_insert (destination_heap,
2006 GNUNET_TIME_absolute_ntoh (msg->expiration_time).abs_value);
2007 GNUNET_STATISTICS_update (stats,
2008 gettext_noop ("# Active destinations"),
2010 while (GNUNET_CONTAINER_multihashmap_size (destination_map) > max_destination_mappings)
2011 expire_destination (de);
2013 /* setup tunnel to destination */
2014 (void) create_tunnel_to_destination (de,
2015 (GNUNET_NO == ntohl (msg->nac)) ? NULL : client,
2018 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2023 * A client asks us to setup a redirection to a particular peer
2024 * offering a service. Setup the redirection and give the client the
2028 * @param client requesting client
2029 * @param message redirection request (a 'struct RedirectToPeerRequestMessage')
2032 service_redirect_to_service (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *client,
2033 const struct GNUNET_MessageHeader *message)
2035 const struct RedirectToServiceRequestMessage *msg;
2040 struct DestinationEntry *de;
2041 GNUNET_HashCode key;
2044 msg = (const struct RedirectToServiceRequestMessage *) message;
2046 /* allocate response IP */
2048 result_af = (int) htonl (msg->result_af);
2053 allocate_v4_address (&v4))
2054 result_af = AF_UNSPEC;
2060 allocate_v6_address (&v6))
2061 result_af = AF_UNSPEC;
2067 allocate_v4_address (&v4))
2070 result_af = AF_INET;
2072 else if (GNUNET_OK ==
2073 allocate_v6_address (&v6))
2076 result_af = AF_INET6;
2081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2084 if ( (result_af == AF_UNSPEC) ||
2085 (GNUNET_NO == ntohl (msg->nac)) )
2087 /* send reply "instantly" */
2088 send_client_reply (client,
2093 if (result_af == AF_UNSPEC)
2095 /* failure, we're done */
2096 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2097 _("Failed to allocate IP address for new destination\n"));
2098 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2103 char sbuf[INET6_ADDRSTRLEN];
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2106 "Allocated address %s for redirection to service %s on peer %s\n",
2107 inet_ntop (result_af, addr, sbuf, sizeof (sbuf)),
2108 GNUNET_h2s (&msg->service_descriptor),
2109 GNUNET_i2s (&msg->target));
2112 /* setup destination record */
2113 de = GNUNET_malloc (sizeof (struct DestinationEntry));
2114 de->is_service = GNUNET_YES;
2115 de->details.service_destination.service_descriptor = msg->service_descriptor;
2116 de->details.service_destination.target = msg->target;
2117 get_destination_key_from_ip (result_af,
2121 GNUNET_assert (GNUNET_OK ==
2122 GNUNET_CONTAINER_multihashmap_put (destination_map,
2125 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2126 de->heap_node = GNUNET_CONTAINER_heap_insert (destination_heap,
2128 GNUNET_TIME_absolute_ntoh (msg->expiration_time).abs_value);
2129 while (GNUNET_CONTAINER_multihashmap_size (destination_map) > max_destination_mappings)
2130 expire_destination (de);
2131 (void) create_tunnel_to_destination (de,
2132 (GNUNET_NO == ntohl (msg->nac)) ? NULL : client,
2135 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2141 * Function called for inbound tunnels. As we don't offer
2142 * any mesh services, this function should never be called.
2144 * @param cls closure
2145 * @param tunnel new handle to the tunnel
2146 * @param initiator peer that started the tunnel
2147 * @param atsi performance information for the tunnel
2148 * @return initial tunnel context for the tunnel
2149 * (can be NULL -- that's not an error)
2152 inbound_tunnel_cb (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
2153 const struct GNUNET_PeerIdentity *initiator,
2154 const struct GNUNET_ATS_Information *atsi)
2156 /* How can and why should anyone open an inbound tunnel to vpn? */
2163 * Function called whenever an inbound tunnel is destroyed. Should clean up
2164 * any associated state.
2166 * @param cls closure (set from GNUNET_MESH_connect)
2167 * @param tunnel connection to the other end (henceforth invalid)
2168 * @param tunnel_ctx place where local state associated
2169 * with the tunnel is stored (our 'struct TunnelState')
2172 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel, void *tunnel_ctx)
2174 struct TunnelState *ts = tunnel_ctx;
2181 GNUNET_assert (ts->tunnel == tunnel);
2183 free_tunnel_state (ts);
2188 * Free memory occupied by an entry in the destination map.
2192 * @param value a 'struct DestinationEntry *'
2193 * @return GNUNET_OK (continue to iterate)
2196 cleanup_destination (void *cls,
2197 const GNUNET_HashCode *key,
2200 struct DestinationEntry *de = value;
2202 free_destination_entry (de);
2208 * Free memory occupied by an entry in the tunnel map.
2212 * @param value a 'struct TunnelState *'
2213 * @return GNUNET_OK (continue to iterate)
2216 cleanup_tunnel (void *cls,
2217 const GNUNET_HashCode *key,
2220 struct TunnelState *ts = value;
2222 free_tunnel_state (ts);
2228 * Function scheduled as very last function, cleans up after us
2234 cleanup (void *cls GNUNET_UNUSED,
2235 const struct GNUNET_SCHEDULER_TaskContext *tc)
2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2240 "VPN is shutting down\n");
2241 if (NULL != destination_map)
2243 GNUNET_CONTAINER_multihashmap_iterate (destination_map,
2244 &cleanup_destination,
2246 GNUNET_CONTAINER_multihashmap_destroy (destination_map);
2247 destination_map = NULL;
2249 if (NULL != destination_heap)
2251 GNUNET_CONTAINER_heap_destroy (destination_heap);
2252 destination_heap = NULL;
2254 if (NULL != tunnel_map)
2256 GNUNET_CONTAINER_multihashmap_iterate (tunnel_map,
2259 GNUNET_CONTAINER_multihashmap_destroy (tunnel_map);
2262 if (NULL != tunnel_heap)
2264 GNUNET_CONTAINER_heap_destroy (tunnel_heap);
2267 if (NULL != mesh_handle)
2269 GNUNET_MESH_disconnect (mesh_handle);
2272 if (NULL != helper_handle)
2274 GNUNET_HELPER_stop (helper_handle);
2275 helper_handle = NULL;
2279 GNUNET_SERVER_notification_context_destroy (nc);
2284 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
2288 GNUNET_free_non_null (vpn_argv[i]);
2293 * A client disconnected, clean up all references to it.
2295 * @param cls the client that disconnected
2297 * @param value a 'struct TunnelState *'
2298 * @return GNUNET_OK (continue to iterate)
2301 cleanup_tunnel_client (void *cls,
2302 const GNUNET_HashCode *key,
2305 struct GNUNET_SERVER_Client *client = cls;
2306 struct TunnelState *ts = value;
2308 if (client == ts->client)
2310 GNUNET_SERVER_client_drop (ts->client);
2318 * A client disconnected, clean up all references to it.
2320 * @param cls the client that disconnected
2322 * @param value a 'struct DestinationEntry *'
2323 * @return GNUNET_OK (continue to iterate)
2326 cleanup_destination_client (void *cls,
2327 const GNUNET_HashCode *key,
2330 struct GNUNET_SERVER_Client *client = cls;
2331 struct DestinationEntry *de = value;
2332 struct TunnelState *ts;
2334 if (NULL == (ts = de->ts))
2336 if (client == ts->client)
2338 GNUNET_SERVER_client_drop (ts->client);
2346 * A client has disconnected from us. If we are currently building
2347 * a tunnel for it, cancel the operation.
2350 * @param client handle to the client that disconnected
2353 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
2355 if (NULL != tunnel_map)
2356 GNUNET_CONTAINER_multihashmap_iterate (tunnel_map,
2357 &cleanup_tunnel_client,
2359 if (NULL != destination_map)
2360 GNUNET_CONTAINER_multihashmap_iterate (destination_map,
2361 &cleanup_destination_client,
2367 * Main function that will be run by the scheduler.
2369 * @param cls closure
2370 * @param server the initialized server
2371 * @param cfg_ configuration
2375 struct GNUNET_SERVER_Handle *server,
2376 const struct GNUNET_CONFIGURATION_Handle *cfg_)
2378 static const struct GNUNET_SERVER_MessageHandler service_handlers[] = {
2379 /* callback, cls, type, size */
2380 {&service_redirect_to_ip, NULL, GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_IP, 0},
2381 {&service_redirect_to_service, NULL,
2382 GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_SERVICE,
2383 sizeof (struct RedirectToServiceRequestMessage) },
2386 static const struct GNUNET_MESH_MessageHandler mesh_handlers[] = {
2387 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP_BACK, 0},
2388 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP_BACK, 0},
2389 {receive_udp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP_BACK, 0},
2390 {receive_tcp_back, GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP_BACK, 0},
2393 static const GNUNET_MESH_ApplicationType types[] = {
2394 GNUNET_APPLICATION_TYPE_END
2405 stats = GNUNET_STATISTICS_create ("vpn", cfg);
2407 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_MAPPING",
2408 &max_destination_mappings))
2409 max_destination_mappings = 200;
2411 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn", "MAX_TUNNELS",
2412 &max_tunnel_mappings))
2413 max_tunnel_mappings = 200;
2415 destination_map = GNUNET_CONTAINER_multihashmap_create (max_destination_mappings * 2);
2416 destination_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2417 tunnel_map = GNUNET_CONTAINER_multihashmap_create (max_tunnel_mappings * 2);
2418 tunnel_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2421 vpn_argv[0] = GNUNET_strdup ("vpn-gnunet");
2422 if (GNUNET_SYSERR ==
2423 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IFNAME", &ifname))
2425 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2426 "No entry 'IFNAME' in configuration!\n");
2427 GNUNET_SCHEDULER_shutdown ();
2430 vpn_argv[1] = ifname;
2431 if ( (GNUNET_SYSERR ==
2432 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
2434 (1 != inet_pton (AF_INET6, ipv6addr, &v6))) )
2436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2437 "No valid entry 'IPV6ADDR' in configuration!\n");
2438 GNUNET_SCHEDULER_shutdown ();
2441 vpn_argv[2] = ipv6addr;
2442 if (GNUNET_SYSERR ==
2443 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6PREFIX",
2446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2447 "No entry 'IPV6PREFIX' in configuration!\n");
2448 GNUNET_SCHEDULER_shutdown ();
2451 vpn_argv[3] = ipv6prefix_s;
2453 GNUNET_CONFIGURATION_get_value_number (cfg, "vpn",
2456 (ipv6prefix >= 127) )
2458 GNUNET_SCHEDULER_shutdown ();
2462 if ( (GNUNET_SYSERR ==
2463 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
2465 (1 != inet_pton (AF_INET, ipv4addr, &v4))) )
2467 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2468 "No valid entry for 'IPV4ADDR' in configuration!\n");
2469 GNUNET_SCHEDULER_shutdown ();
2472 vpn_argv[4] = ipv4addr;
2473 if ( (GNUNET_SYSERR ==
2474 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK",
2476 (1 != inet_pton (AF_INET, ipv4mask, &v4))) )
2478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2479 "No valid entry 'IPV4MASK' in configuration!\n");
2480 GNUNET_SCHEDULER_shutdown ();
2483 vpn_argv[5] = ipv4mask;
2487 GNUNET_MESH_connect (cfg_, 42 /* queue length */, NULL,
2492 helper_handle = GNUNET_HELPER_start ("gnunet-helper-vpn", vpn_argv,
2493 &message_token, NULL);
2494 nc = GNUNET_SERVER_notification_context_create (server, 1);
2495 GNUNET_SERVER_add_handlers (server, service_handlers);
2496 GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
2497 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
2502 * The main function of the VPN service.
2504 * @param argc number of arguments from the command line
2505 * @param argv command line arguments
2506 * @return 0 ok, 1 on error
2509 main (int argc, char *const *argv)
2511 return (GNUNET_OK ==
2512 GNUNET_SERVICE_run (argc, argv, "vpn",
2513 GNUNET_SERVICE_OPTION_NONE,
2514 &run, NULL)) ? 0 : 1;
2517 /* end of gnunet-service-vpn.c */