2 This file is part of GNUnet.
3 (C) 2010, 2012 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file exit/gnunet-daemon-exit.c
23 * @brief tool to allow IP traffic exit from the GNUnet mesh to the Internet
24 * @author Philipp Toelke
25 * @author Christian Grothoff
31 * - which code should advertise services? the service model is right
32 * now a bit odd, especially as this code DOES the exit and knows
33 * the DNS "name", but OTOH this is clearly NOT the place to advertise
34 * the service's existence; maybe the daemon should turn into a
35 * service with an API to add local-exit services dynamically?
38 #include "gnunet_util_lib.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"
48 * Information about an address.
53 * AF_INET or AF_INET6.
58 * Remote address information.
63 * Address, if af is AF_INET.
68 * Address, if af is AF_INET6.
74 * IPPROTO_TCP or IPPROTO_UDP;
79 * Remote port, in host byte order!
86 * This struct is saved into the services-hashmap to represent
87 * a service this peer is specifically offering an exit for
88 * (for a specific domain name).
94 * Remote address to use for the service.
96 struct SocketAddress address;
99 * DNS name of the service.
104 * Port I am listening on within GNUnet for this service, in host
105 * byte order. (as we may redirect ports).
112 * Information we use to track a connection (the classical 6-tuple of
113 * IP-version, protocol, source-IP, destination-IP, source-port and
116 struct RedirectInformation
120 * Address information for the other party (equivalent of the
121 * arguments one would give to "connect").
123 struct SocketAddress remote_address;
126 * Address information we used locally (AF and proto must match
127 * "remote_address"). Equivalent of the arguments one would give to
130 struct SocketAddress local_address;
133 Note 1: additional information might be added here in the
134 future to support protocols that require special handling,
137 Note 2: we might also sometimes not match on all components
138 of the tuple, to support protocols where things do not always
145 * Queue of messages to a tunnel.
147 struct TunnelMessageQueue
150 * This is a doubly-linked list.
152 struct TunnelMessageQueue *next;
155 * This is a doubly-linked list.
157 struct TunnelMessageQueue *prev;
160 * Payload to send via the tunnel.
165 * Number of bytes in 'payload'.
172 * This struct is saved into connections_map to allow finding the
173 * right tunnel given an IP packet from TUN. It is also associated
174 * with the tunnel's closure so we can find it again for the next
175 * message from the tunnel.
180 * Mesh tunnel that is used for this connection.
182 struct GNUNET_MESH_Tunnel *tunnel;
185 * Heap node for this state in the connections_heap.
187 struct GNUNET_CONTAINER_HeapNode *heap_node;
190 * Key this state has in the connections_map.
192 GNUNET_HashCode state_key;
195 * Associated service record, or NULL for no service.
197 struct LocalService *serv;
200 * Head of DLL of messages for this tunnel.
202 struct TunnelMessageQueue *head;
205 * Tail of DLL of messages for this tunnel.
207 struct TunnelMessageQueue *tail;
210 * Active tunnel transmission request (or NULL).
212 struct GNUNET_MESH_TransmitHandle *th;
215 * Primary redirection information for this connection.
217 struct RedirectInformation ri;
223 * Return value from 'main'.
225 static int global_ret;
228 * The handle to the configuration used throughout the process
230 static const struct GNUNET_CONFIGURATION_Handle *cfg;
233 * The handle to the helper
235 static struct GNUNET_HELPER_Handle *helper_handle;
238 * Arguments to the exit helper.
240 static char *exit_argv[8];
243 * IPv6 address of our TUN interface.
245 static struct in6_addr exit_ipv6addr;
248 * IPv6 prefix (0..127) from configuration file.
250 static unsigned long long ipv6prefix;
253 * IPv4 address of our TUN interface.
255 static struct in_addr exit_ipv4addr;
258 * IPv4 netmask of our TUN interface.
260 static struct in_addr exit_ipv4mask;
266 static struct GNUNET_STATISTICS_Handle *stats;
271 static struct GNUNET_MESH_Handle *mesh_handle;
274 * This hashmaps contains the mapping from peer, service-descriptor,
275 * source-port and destination-port to a struct TunnelState
277 static struct GNUNET_CONTAINER_MultiHashMap *connections_map;
280 * Heap so we can quickly find "old" connections.
282 static struct GNUNET_CONTAINER_Heap *connections_heap;
285 * If there are at least this many connections, old ones will be removed
287 static long long unsigned int max_connections;
290 * This hashmaps saves interesting things about the configured UDP services
292 static struct GNUNET_CONTAINER_MultiHashMap *udp_services;
295 * This hashmaps saves interesting things about the configured TCP services
297 static struct GNUNET_CONTAINER_MultiHashMap *tcp_services;
300 * Are we an IPv4-exit?
302 static int ipv4_exit;
305 * Are we an IPv6-exit?
307 static int ipv6_exit;
310 * Do we support IPv4 at all on the TUN interface?
312 static int ipv4_enabled;
315 * Do we support IPv6 at all on the TUN interface?
317 static int ipv6_enabled;
321 * Given IP information about a connection, calculate the respective
322 * hash we would use for the 'connections_map'.
324 * @param hash resulting hash
325 * @param ri information about the connection
328 hash_redirect_info (GNUNET_HashCode *hash,
329 const struct RedirectInformation *ri)
333 memset (hash, 0, sizeof (GNUNET_HashCode));
334 /* the GNUnet hashmap only uses the first sizeof(unsigned int) of the hash,
335 so we put the IP address in there (and hope for few collisions) */
337 switch (ri->remote_address.af)
340 memcpy (off, &ri->remote_address.address.ipv4, sizeof (struct in_addr));
341 off += sizeof (struct in_addr);
344 memcpy (off, &ri->remote_address.address.ipv6, sizeof (struct in6_addr));
345 off += sizeof (struct in_addr);
350 memcpy (off, &ri->remote_address.port, sizeof (uint16_t));
351 off += sizeof (uint16_t);
352 switch (ri->local_address.af)
355 memcpy (off, &ri->local_address.address.ipv4, sizeof (struct in_addr));
356 off += sizeof (struct in_addr);
359 memcpy (off, &ri->local_address.address.ipv6, sizeof (struct in6_addr));
360 off += sizeof (struct in_addr);
365 memcpy (off, &ri->local_address.port, sizeof (uint16_t));
366 off += sizeof (uint16_t);
367 memcpy (off, &ri->remote_address.proto, sizeof (uint8_t));
368 off += sizeof (uint8_t);
373 * Get our connection tracking state. Warns if it does not exists,
374 * refreshes the timestamp if it does exist.
376 * @param af address family
377 * @param protocol IPPROTO_UDP or IPPROTO_TCP
378 * @param destination_ip target IP
379 * @param destination_port target port
380 * @param local_ip local IP
381 * @param local_port local port
382 * @param state_key set to hash's state if non-NULL
383 * @return NULL if we have no tracking information for this tuple
385 static struct TunnelState *
386 get_redirect_state (int af,
388 const void *destination_ip,
389 uint16_t destination_port,
390 const void *local_ip,
392 GNUNET_HashCode *state_key)
394 struct RedirectInformation ri;
396 struct TunnelState *state;
398 if (protocol == IPPROTO_ICMP)
401 destination_port = 0;
404 ri.remote_address.af = af;
406 ri.remote_address.address.ipv4 = *((struct in_addr*) destination_ip);
408 ri.remote_address.address.ipv6 = * ((struct in6_addr*) destination_ip);
409 ri.remote_address.port = destination_port;
410 ri.remote_address.proto = protocol;
411 ri.local_address.af = af;
413 ri.local_address.address.ipv4 = *((struct in_addr*) local_ip);
415 ri.local_address.address.ipv6 = * ((struct in6_addr*) local_ip);
416 ri.local_address.port = local_port;
417 ri.local_address.proto = protocol;
418 hash_redirect_info (&key, &ri);
419 if (NULL != state_key)
421 state = GNUNET_CONTAINER_multihashmap_get (connections_map, &key);
424 /* Mark this connection as freshly used */
425 if (NULL == state_key)
426 GNUNET_CONTAINER_heap_update_cost (connections_heap,
428 GNUNET_TIME_absolute_get ().abs_value);
434 * Given a service descriptor and a destination port, find the
435 * respective service entry.
437 * @param service_map map of services (TCP or UDP)
438 * @param desc service descriptor
439 * @param dpt destination port
440 * @return NULL if we are not aware of such a service
442 static struct LocalService *
443 find_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
444 const GNUNET_HashCode *desc,
447 char key[sizeof (GNUNET_HashCode) + sizeof (uint16_t)];
449 memcpy (&key[0], &dpt, sizeof (uint16_t));
450 memcpy (&key[sizeof(uint16_t)], desc, sizeof (GNUNET_HashCode));
451 return GNUNET_CONTAINER_multihashmap_get (service_map,
452 (GNUNET_HashCode *) key);
457 * Free memory associated with a service record.
460 * @param key service descriptor
461 * @param value service record to free
465 free_service_record (void *cls,
466 const GNUNET_HashCode *key,
469 struct LocalService *service = value;
471 GNUNET_free_non_null (service->name);
472 GNUNET_free (service);
478 * Given a service descriptor and a destination port, find the
479 * respective service entry.
481 * @param service_map map of services (TCP or UDP)
482 * @param name name of the service
483 * @param dpt destination port
484 * @param service service information record to store (service->name will be set).
487 store_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
490 struct LocalService *service)
492 char key[sizeof (GNUNET_HashCode) + sizeof (uint16_t)];
493 GNUNET_HashCode desc;
495 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &desc);
496 service->name = GNUNET_strdup (name);
497 memcpy (&key[0], &dpt, sizeof (uint16_t));
498 memcpy (&key[sizeof(uint16_t)], &desc, sizeof (GNUNET_HashCode));
500 GNUNET_CONTAINER_multihashmap_put (service_map,
501 (GNUNET_HashCode *) key,
503 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
505 free_service_record (NULL, (GNUNET_HashCode *) key, service);
506 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
507 _("Got duplicate service records for `%s:%u'\n"),
515 * MESH is ready to receive a message for the tunnel. Transmit it.
517 * @param cls the 'struct TunnelState'.
518 * @param size number of bytes available in buf
519 * @param buf where to copy the message
520 * @return number of bytes copied to buf
523 send_to_peer_notify_callback (void *cls, size_t size, void *buf)
525 struct TunnelState *s = cls;
526 struct GNUNET_MESH_Tunnel *tunnel = s->tunnel;
527 struct TunnelMessageQueue *tnq;
535 s->th = GNUNET_MESH_notify_transmit_ready (tunnel,
536 GNUNET_NO /* corking */,
538 GNUNET_TIME_UNIT_FOREVER_REL,
541 &send_to_peer_notify_callback,
545 GNUNET_assert (size >= tnq->len);
546 memcpy (buf, tnq->payload, tnq->len);
548 GNUNET_CONTAINER_DLL_remove (s->head,
552 if (NULL != (tnq = s->head))
553 s->th = GNUNET_MESH_notify_transmit_ready (tunnel,
554 GNUNET_NO /* corking */,
556 GNUNET_TIME_UNIT_FOREVER_REL,
559 &send_to_peer_notify_callback,
561 GNUNET_STATISTICS_update (stats,
562 gettext_noop ("# Bytes transmitted via mesh tunnels"),
569 * Send the given packet via the mesh tunnel.
571 * @param mesh_tunnel destination
572 * @param tnq message to queue
575 send_packet_to_mesh_tunnel (struct GNUNET_MESH_Tunnel *mesh_tunnel,
576 struct TunnelMessageQueue *tnq)
578 struct TunnelState *s;
580 s = GNUNET_MESH_tunnel_get_data (mesh_tunnel);
581 GNUNET_assert (NULL != s);
582 GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, tnq);
584 s->th = GNUNET_MESH_notify_transmit_ready (mesh_tunnel, GNUNET_NO /* cork */, 0 /* priority */,
585 GNUNET_TIME_UNIT_FOREVER_REL,
587 &send_to_peer_notify_callback,
593 * @brief Handles an ICMP packet received from the helper.
595 * @param icmp A pointer to the Packet
596 * @param pktlen number of bytes in 'icmp'
597 * @param af address family (AFINET or AF_INET6)
598 * @param destination_ip destination IP-address of the IP packet (should
599 * be our local address)
600 * @param source_ip original source IP-address of the IP packet (should
601 * be the original destination address)
604 icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
607 const void *destination_ip,
608 const void *source_ip)
610 struct TunnelState *state;
611 struct TunnelMessageQueue *tnq;
612 struct GNUNET_EXIT_IcmpToVPNMessage *i2v;
613 const struct GNUNET_TUN_IPv4Header *ipv4;
614 const struct GNUNET_TUN_IPv6Header *ipv6;
615 const struct GNUNET_TUN_UdpHeader *udp;
622 char sbuf[INET6_ADDRSTRLEN];
623 char dbuf[INET6_ADDRSTRLEN];
624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625 "Received ICMP packet going from %s to %s\n",
628 sbuf, sizeof (sbuf)),
631 dbuf, sizeof (dbuf)));
633 if (pktlen < sizeof (struct GNUNET_TUN_IcmpHeader))
640 /* Find out if this is an ICMP packet in response to an existing
641 TCP/UDP packet and if so, figure out ports / protocol of the
642 existing session from the IP data in the ICMP payload */
645 protocol = IPPROTO_ICMP;
651 case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
652 case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
654 case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
655 case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
656 case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
658 sizeof (struct GNUNET_TUN_IcmpHeader) +
659 sizeof (struct GNUNET_TUN_IPv4Header) + 8)
665 ipv4 = (const struct GNUNET_TUN_IPv4Header *) &icmp[1];
666 protocol = ipv4->protocol;
667 /* could be TCP or UDP, but both have the ports in the right
668 place, so that doesn't matter here */
669 udp = (const struct GNUNET_TUN_UdpHeader *) &ipv4[1];
670 spt = ntohs (udp->spt);
671 dpt = ntohs (udp->dpt);
672 /* throw away ICMP payload, won't be useful for the other side anyway */
673 pktlen = sizeof (struct GNUNET_TUN_IcmpHeader);
676 GNUNET_STATISTICS_update (stats,
677 gettext_noop ("# ICMPv4 packets dropped (type not allowed)"),
685 case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
686 case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
687 case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
688 case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
690 sizeof (struct GNUNET_TUN_IcmpHeader) +
691 sizeof (struct GNUNET_TUN_IPv6Header) + 8)
697 ipv6 = (const struct GNUNET_TUN_IPv6Header *) &icmp[1];
698 protocol = ipv6->next_header;
699 /* could be TCP or UDP, but both have the ports in the right
700 place, so that doesn't matter here */
701 udp = (const struct GNUNET_TUN_UdpHeader *) &ipv6[1];
702 spt = ntohs (udp->spt);
703 dpt = ntohs (udp->dpt);
704 /* throw away ICMP payload, won't be useful for the other side anyway */
705 pktlen = sizeof (struct GNUNET_TUN_IcmpHeader);
707 case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
708 case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
711 GNUNET_STATISTICS_update (stats,
712 gettext_noop ("# ICMPv6 packets dropped (type not allowed)"),
723 state = get_redirect_state (af, IPPROTO_ICMP,
729 state = get_redirect_state (af, IPPROTO_UDP,
737 state = get_redirect_state (af, IPPROTO_TCP,
745 GNUNET_STATISTICS_update (stats,
746 gettext_noop ("# ICMP packets dropped (not allowed)"),
752 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
753 _("ICMP Packet dropped, have no matching connection information\n"));
756 mlen = sizeof (struct GNUNET_EXIT_IcmpToVPNMessage) + pktlen - sizeof (struct GNUNET_TUN_IcmpHeader);
757 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);
758 tnq->payload = &tnq[1];
760 i2v = (struct GNUNET_EXIT_IcmpToVPNMessage *) &tnq[1];
761 i2v->header.size = htons ((uint16_t) mlen);
762 i2v->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_VPN);
763 i2v->af = htonl (af);
764 memcpy (&i2v->icmp_header,
767 send_packet_to_mesh_tunnel (state->tunnel,
773 * @brief Handles an UDP packet received from the helper.
775 * @param udp A pointer to the Packet
776 * @param pktlen number of bytes in 'udp'
777 * @param af address family (AFINET or AF_INET6)
778 * @param destination_ip destination IP-address of the IP packet (should
779 * be our local address)
780 * @param source_ip original source IP-address of the IP packet (should
781 * be the original destination address)
784 udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
787 const void *destination_ip,
788 const void *source_ip)
790 struct TunnelState *state;
791 struct TunnelMessageQueue *tnq;
792 struct GNUNET_EXIT_UdpReplyMessage *urm;
796 char sbuf[INET6_ADDRSTRLEN];
797 char dbuf[INET6_ADDRSTRLEN];
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Received UDP packet going from %s:%u to %s:%u\n",
802 sbuf, sizeof (sbuf)),
803 (unsigned int) ntohs (udp->spt),
806 dbuf, sizeof (dbuf)),
807 (unsigned int) ntohs (udp->dpt));
809 if (pktlen < sizeof (struct GNUNET_TUN_UdpHeader))
815 if (pktlen != ntohs (udp->len))
821 state = get_redirect_state (af, IPPROTO_UDP,
829 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
830 _("UDP Packet dropped, have no matching connection information\n"));
833 mlen = sizeof (struct GNUNET_EXIT_UdpReplyMessage) + pktlen - sizeof (struct GNUNET_TUN_UdpHeader);
834 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);
835 tnq->payload = &tnq[1];
837 urm = (struct GNUNET_EXIT_UdpReplyMessage *) &tnq[1];
838 urm->header.size = htons ((uint16_t) mlen);
839 urm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_UDP_REPLY);
840 urm->source_port = htons (0);
841 urm->destination_port = htons (0);
844 pktlen - sizeof (struct GNUNET_TUN_UdpHeader));
845 send_packet_to_mesh_tunnel (state->tunnel,
851 * @brief Handles a TCP packet received from the helper.
853 * @param tcp A pointer to the Packet
854 * @param pktlen the length of the packet, including its TCP header
855 * @param af address family (AFINET or AF_INET6)
856 * @param destination_ip destination IP-address of the IP packet (should
857 * be our local address)
858 * @param source_ip original source IP-address of the IP packet (should
859 * be the original destination address)
862 tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
865 const void *destination_ip,
866 const void *source_ip)
868 struct TunnelState *state;
870 struct GNUNET_TUN_TcpHeader *mtcp;
871 struct GNUNET_EXIT_TcpDataMessage *tdm;
872 struct TunnelMessageQueue *tnq;
876 char sbuf[INET6_ADDRSTRLEN];
877 char dbuf[INET6_ADDRSTRLEN];
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Received TCP packet with %u bytes going from %s:%u to %s:%u\n",
880 pktlen - sizeof (struct GNUNET_TUN_TcpHeader),
883 sbuf, sizeof (sbuf)),
884 (unsigned int) ntohs (tcp->spt),
887 dbuf, sizeof (dbuf)),
888 (unsigned int) ntohs (tcp->dpt));
890 if (pktlen < sizeof (struct GNUNET_TUN_TcpHeader))
896 state = get_redirect_state (af, IPPROTO_TCP,
904 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
905 _("TCP Packet dropped, have no matching connection information\n"));
909 /* mug port numbers and crc to avoid information leakage;
910 sender will need to lookup the correct values anyway */
911 memcpy (buf, tcp, pktlen);
912 mtcp = (struct GNUNET_TUN_TcpHeader *) buf;
917 mlen = sizeof (struct GNUNET_EXIT_TcpDataMessage) + (pktlen - sizeof (struct GNUNET_TUN_TcpHeader));
918 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
924 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);
925 tnq->payload = &tnq[1];
927 tdm = (struct GNUNET_EXIT_TcpDataMessage *) &tnq[1];
928 tdm->header.size = htons ((uint16_t) mlen);
929 tdm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_VPN);
930 tdm->reserved = htonl (0);
931 memcpy (&tdm->tcp_header,
934 send_packet_to_mesh_tunnel (state->tunnel,
940 * Receive packets from the helper-process
943 * @param client unsued
944 * @param message message received from helper
947 message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
948 const struct GNUNET_MessageHeader *message)
950 const struct GNUNET_TUN_Layer2PacketHeader *pkt_tun;
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954 "Got %u-byte message of type %u from gnunet-helper-exit\n",
955 ntohs (message->size),
956 ntohs (message->type));
957 GNUNET_STATISTICS_update (stats,
958 gettext_noop ("# Packets received from TUN"),
960 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
965 size = ntohs (message->size);
966 if (size < sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader))
971 GNUNET_STATISTICS_update (stats,
972 gettext_noop ("# Bytes received from TUN"),
974 pkt_tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
975 size -= sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader);
976 switch (ntohs (pkt_tun->proto))
980 const struct GNUNET_TUN_IPv4Header *pkt4;
982 if (size < sizeof (struct GNUNET_TUN_IPv4Header))
984 /* Kernel to blame? */
988 pkt4 = (const struct GNUNET_TUN_IPv4Header *) &pkt_tun[1];
989 if (size != ntohs (pkt4->total_length))
991 /* Kernel to blame? */
995 if (pkt4->header_length * 4 != sizeof (struct GNUNET_TUN_IPv4Header))
997 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
998 _("IPv4 packet options received. Ignored.\n"));
1002 size -= sizeof (struct GNUNET_TUN_IPv4Header);
1003 switch (pkt4->protocol)
1006 udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt4[1], size,
1008 &pkt4->destination_address,
1009 &pkt4->source_address);
1012 tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt4[1], size,
1014 &pkt4->destination_address,
1015 &pkt4->source_address);
1018 icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt4[1], size,
1020 &pkt4->destination_address,
1021 &pkt4->source_address);
1024 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1025 _("IPv4 packet with unsupported next header received. Ignored.\n"));
1032 const struct GNUNET_TUN_IPv6Header *pkt6;
1034 if (size < sizeof (struct GNUNET_TUN_IPv6Header))
1036 /* Kernel to blame? */
1040 pkt6 = (struct GNUNET_TUN_IPv6Header *) &pkt_tun[1];
1041 if (size != ntohs (pkt6->payload_length))
1043 /* Kernel to blame? */
1047 size -= sizeof (struct GNUNET_TUN_IPv6Header);
1048 switch (pkt6->next_header)
1051 udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt6[1], size,
1053 &pkt6->destination_address,
1054 &pkt6->source_address);
1057 tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt6[1], size,
1059 &pkt6->destination_address,
1060 &pkt6->source_address);
1063 icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt6[1], size,
1065 &pkt6->destination_address,
1066 &pkt6->source_address);
1069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1070 _("IPv6 packet with unsupported next header received. Ignored.\n"));
1076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1077 _("Packet from unknown protocol %u received. Ignored.\n"),
1078 ntohs (pkt_tun->proto));
1085 * We need to create a (unique) fresh local address (IP+port).
1088 * @param af desired address family
1089 * @param proto desired protocol (IPPROTO_UDP or IPPROTO_TCP)
1090 * @param local_address address to initialize
1093 setup_fresh_address (int af,
1095 struct SocketAddress *local_address)
1097 local_address->af = af;
1098 local_address->proto = (uint8_t) proto;
1099 /* default "local" port range is often 32768--61000,
1100 so we pick a random value in that range */
1101 if (proto == IPPROTO_ICMP)
1102 local_address->port = 0;
1105 = (uint16_t) 32768 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1111 struct in_addr addr;
1112 struct in_addr mask;
1115 addr = exit_ipv4addr;
1116 mask = exit_ipv4mask;
1117 if (0 == ~mask.s_addr)
1119 /* only one valid IP anyway */
1120 local_address->address.ipv4 = addr;
1123 /* Given 192.168.0.1/255.255.0.0, we want a mask
1124 of '192.168.255.255', thus: */
1125 mask.s_addr = addr.s_addr | ~mask.s_addr;
1126 /* Pick random IPv4 address within the subnet, except 'addr' or 'mask' itself */
1129 rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1131 local_address->address.ipv4.s_addr = (addr.s_addr | rnd.s_addr) & mask.s_addr;
1133 while ( (local_address->address.ipv4.s_addr == addr.s_addr) ||
1134 (local_address->address.ipv4.s_addr == mask.s_addr) );
1139 struct in6_addr addr;
1140 struct in6_addr mask;
1141 struct in6_addr rnd;
1144 addr = exit_ipv6addr;
1145 GNUNET_assert (ipv6prefix < 128);
1146 if (ipv6prefix == 127)
1148 /* only one valid IP anyway */
1149 local_address->address.ipv6 = addr;
1152 /* Given ABCD::/96, we want a mask of 'ABCD::FFFF:FFFF,
1155 for (i=127;i>=128-ipv6prefix;i--)
1156 mask.s6_addr[i / 8] |= (1 << (i % 8));
1158 /* Pick random IPv6 address within the subnet, except 'addr' or 'mask' itself */
1163 rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1165 local_address->address.ipv6.s6_addr[i]
1166 = (addr.s6_addr[i] | rnd.s6_addr[i]) & mask.s6_addr[i];
1169 while ( (0 == memcmp (&local_address->address.ipv6,
1171 sizeof (struct in6_addr))) ||
1172 (0 == memcmp (&local_address->address.ipv6,
1174 sizeof (struct in6_addr))) );
1184 * We are starting a fresh connection (TCP or UDP) and need
1185 * to pick a source port and IP address (within the correct
1186 * range and address family) to associate replies with the
1187 * connection / correct mesh tunnel. This function generates
1188 * a "fresh" source IP and source port number for a connection
1189 * After picking a good source address, this function sets up
1190 * the state in the 'connections_map' and 'connections_heap'
1191 * to allow finding the state when needed later. The function
1192 * also makes sure that we remain within memory limits by
1193 * cleaning up 'old' states.
1195 * @param state skeleton state to setup a record for; should
1196 * 'state->ri.remote_address' filled in so that
1197 * this code can determine which AF/protocol is
1198 * going to be used (the 'tunnel' should also
1199 * already be set); after calling this function,
1200 * heap_node and the local_address will be
1201 * also initialized (heap_node != NULL can be
1202 * used to test if a state has been fully setup).
1205 setup_state_record (struct TunnelState *state)
1207 GNUNET_HashCode key;
1208 struct TunnelState *s;
1210 /* generate fresh, unique address */
1213 if (NULL == state->serv)
1214 setup_fresh_address (state->ri.remote_address.af,
1215 state->ri.remote_address.proto,
1216 &state->ri.local_address);
1218 setup_fresh_address (state->serv->address.af,
1219 state->serv->address.proto,
1220 &state->ri.local_address);
1221 } while (NULL != get_redirect_state (state->ri.remote_address.af,
1222 state->ri.remote_address.proto,
1223 &state->ri.remote_address.address,
1224 state->ri.remote_address.port,
1225 &state->ri.local_address.address,
1226 state->ri.local_address.port,
1229 char buf[INET6_ADDRSTRLEN];
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "Picked local address %s:%u for new connection\n",
1232 inet_ntop (state->ri.local_address.af,
1233 &state->ri.local_address.address,
1235 (unsigned int) state->ri.local_address.port);
1237 state->state_key = key;
1238 GNUNET_assert (GNUNET_OK ==
1239 GNUNET_CONTAINER_multihashmap_put (connections_map,
1241 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1242 state->heap_node = GNUNET_CONTAINER_heap_insert (connections_heap,
1244 GNUNET_TIME_absolute_get ().abs_value);
1245 while (GNUNET_CONTAINER_heap_get_size (connections_heap) > max_connections)
1247 s = GNUNET_CONTAINER_heap_remove_root (connections_heap);
1248 GNUNET_assert (state != s);
1249 s->heap_node = NULL;
1250 GNUNET_MESH_tunnel_destroy (s->tunnel);
1251 GNUNET_assert (GNUNET_OK ==
1252 GNUNET_CONTAINER_multihashmap_remove (connections_map,
1261 * Prepare an IPv4 packet for transmission via the TUN interface.
1262 * Initializes the IP header and calculates checksums (IP+UDP/TCP).
1263 * For UDP, the UDP header will be fully created, whereas for TCP
1264 * only the ports and checksum will be filled in. So for TCP,
1265 * a skeleton TCP header must be part of the provided payload.
1267 * @param payload payload of the packet (starting with UDP payload or
1268 * TCP header, depending on protocol)
1269 * @param payload_length number of bytes in 'payload'
1270 * @param protocol IPPROTO_UDP or IPPROTO_TCP
1271 * @param tcp_header skeleton of the TCP header, NULL for UDP
1272 * @param src_address source address to use (IP and port)
1273 * @param dst_address destination address to use (IP and port)
1274 * @param pkt6 where to write the assembled packet; must
1275 * contain enough space for the IP header, UDP/TCP header
1279 prepare_ipv4_packet (const void *payload, size_t payload_length,
1281 const struct GNUNET_TUN_TcpHeader *tcp_header,
1282 const struct SocketAddress *src_address,
1283 const struct SocketAddress *dst_address,
1284 struct GNUNET_TUN_IPv4Header *pkt4)
1288 len = payload_length;
1292 len += sizeof (struct GNUNET_TUN_UdpHeader);
1295 len += sizeof (struct GNUNET_TUN_TcpHeader);
1296 GNUNET_assert (NULL != tcp_header);
1302 if (len + sizeof (struct GNUNET_TUN_IPv4Header) > UINT16_MAX)
1308 GNUNET_TUN_initialize_ipv4_header (pkt4,
1311 &src_address->address.ipv4,
1312 &dst_address->address.ipv4);
1317 struct GNUNET_TUN_UdpHeader *pkt4_udp = (struct GNUNET_TUN_UdpHeader *) &pkt4[1];
1319 pkt4_udp->spt = htons (src_address->port);
1320 pkt4_udp->dpt = htons (dst_address->port);
1321 pkt4_udp->len = htons ((uint16_t) payload_length);
1322 GNUNET_TUN_calculate_udp4_checksum (pkt4,
1324 payload, payload_length);
1325 memcpy (&pkt4_udp[1], payload, payload_length);
1330 struct GNUNET_TUN_TcpHeader *pkt4_tcp = (struct GNUNET_TUN_TcpHeader *) &pkt4[1];
1332 memcpy (pkt4_tcp, tcp_header, sizeof (struct GNUNET_TUN_TcpHeader));
1333 pkt4_tcp->spt = htons (src_address->port);
1334 pkt4_tcp->dpt = htons (dst_address->port);
1335 GNUNET_TUN_calculate_tcp4_checksum (pkt4,
1339 memcpy (&pkt4_tcp[1], payload, payload_length);
1349 * Prepare an IPv6 packet for transmission via the TUN interface.
1350 * Initializes the IP header and calculates checksums (IP+UDP/TCP).
1351 * For UDP, the UDP header will be fully created, whereas for TCP
1352 * only the ports and checksum will be filled in. So for TCP,
1353 * a skeleton TCP header must be part of the provided payload.
1355 * @param payload payload of the packet (starting with UDP payload or
1356 * TCP header, depending on protocol)
1357 * @param payload_length number of bytes in 'payload'
1358 * @param protocol IPPROTO_UDP or IPPROTO_TCP
1359 * @param src_address source address to use (IP and port)
1360 * @param dst_address destination address to use (IP and port)
1361 * @param pkt6 where to write the assembled packet; must
1362 * contain enough space for the IP header, UDP/TCP header
1366 prepare_ipv6_packet (const void *payload, size_t payload_length,
1368 const struct GNUNET_TUN_TcpHeader *tcp_header,
1369 const struct SocketAddress *src_address,
1370 const struct SocketAddress *dst_address,
1371 struct GNUNET_TUN_IPv6Header *pkt6)
1375 len = payload_length;
1379 len += sizeof (struct GNUNET_TUN_UdpHeader);
1382 /* tcp_header (with port/crc not set) must be part of payload! */
1383 if (len < sizeof (struct GNUNET_TUN_TcpHeader))
1393 if (len > UINT16_MAX)
1399 GNUNET_TUN_initialize_ipv6_header (pkt6,
1402 &dst_address->address.ipv6,
1403 &src_address->address.ipv6);
1409 struct GNUNET_TUN_UdpHeader *pkt6_udp = (struct GNUNET_TUN_UdpHeader *) &pkt6[1];
1411 pkt6_udp->spt = htons (src_address->port);
1412 pkt6_udp->dpt = htons (dst_address->port);
1413 pkt6_udp->len = htons ((uint16_t) payload_length);
1415 GNUNET_TUN_calculate_udp6_checksum (pkt6,
1419 memcpy (&pkt6[1], payload, payload_length);
1424 struct GNUNET_TUN_TcpHeader *pkt6_tcp = (struct GNUNET_TUN_TcpHeader *) pkt6;
1426 /* memcpy first here as some TCP header fields are initialized this way! */
1427 memcpy (pkt6_tcp, payload, payload_length);
1428 pkt6_tcp->spt = htons (src_address->port);
1429 pkt6_tcp->dpt = htons (dst_address->port);
1430 GNUNET_TUN_calculate_tcp6_checksum (pkt6,
1444 * Send a TCP packet via the TUN interface.
1446 * @param destination_address IP and port to use for the TCP packet's destination
1447 * @param source_address IP and port to use for the TCP packet's source
1448 * @param tcp_header header template to use
1449 * @param payload payload of the TCP packet
1450 * @param payload_length number of bytes in 'payload'
1453 send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
1454 const struct SocketAddress *source_address,
1455 const struct GNUNET_TUN_TcpHeader *tcp_header,
1456 const void *payload, size_t payload_length)
1460 GNUNET_STATISTICS_update (stats,
1461 gettext_noop ("# TCP packets sent via TUN"),
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "Sending packet with %u bytes TCP payload via TUN\n",
1465 (unsigned int) payload_length);
1466 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1467 switch (source_address->af)
1470 len += sizeof (struct GNUNET_TUN_IPv4Header);
1473 len += sizeof (struct GNUNET_TUN_IPv6Header);
1479 len += sizeof (struct GNUNET_TUN_TcpHeader);
1480 len += payload_length;
1481 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1488 struct GNUNET_MessageHeader *hdr;
1489 struct GNUNET_TUN_Layer2PacketHeader *tun;
1491 hdr = (struct GNUNET_MessageHeader *) buf;
1492 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1493 hdr->size = htons (len);
1494 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
1495 tun->flags = htons (0);
1496 switch (source_address->af)
1500 struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
1502 tun->proto = htons (ETH_P_IPV4);
1503 prepare_ipv4_packet (payload, payload_length,
1507 destination_address,
1513 struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
1515 tun->proto = htons (ETH_P_IPV6);
1516 prepare_ipv6_packet (payload, payload_length,
1520 destination_address,
1528 (void) GNUNET_HELPER_send (helper_handle,
1529 (const struct GNUNET_MessageHeader*) buf,
1537 * Process a request via mesh to send a request to a TCP service
1538 * offered by this system.
1540 * @param cls closure, NULL
1541 * @param tunnel connection to the other end
1542 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1543 * @param sender who sent the message
1544 * @param message the actual message
1545 * @param atsi performance data for the connection
1546 * @return GNUNET_OK to keep the connection open,
1547 * GNUNET_SYSERR to close it (signal serious error)
1550 receive_tcp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1551 void **tunnel_ctx GNUNET_UNUSED,
1552 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1553 const struct GNUNET_MessageHeader *message,
1554 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1556 struct TunnelState *state = *tunnel_ctx;
1557 const struct GNUNET_EXIT_TcpServiceStartMessage *start;
1558 uint16_t pkt_len = ntohs (message->size);
1560 GNUNET_STATISTICS_update (stats,
1561 gettext_noop ("# TCP service creation requests received via mesh"),
1563 GNUNET_STATISTICS_update (stats,
1564 gettext_noop ("# Bytes received from MESH"),
1565 pkt_len, GNUNET_NO);
1566 /* check that we got at least a valid header */
1567 if (pkt_len < sizeof (struct GNUNET_EXIT_TcpServiceStartMessage))
1569 GNUNET_break_op (0);
1570 return GNUNET_SYSERR;
1572 start = (const struct GNUNET_EXIT_TcpServiceStartMessage*) message;
1573 pkt_len -= sizeof (struct GNUNET_EXIT_TcpServiceStartMessage);
1574 if ( (NULL == state) ||
1575 (NULL != state->serv) ||
1576 (NULL != state->heap_node) )
1578 GNUNET_break_op (0);
1579 return GNUNET_SYSERR;
1581 GNUNET_break_op (ntohl (start->reserved) == 0);
1582 /* setup fresh connection */
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1584 "Received data from %s for forwarding to TCP service %s on port %u\n",
1585 GNUNET_i2s (sender),
1586 GNUNET_h2s (&start->service_descriptor),
1587 (unsigned int) ntohs (start->tcp_header.dpt));
1588 if (NULL == (state->serv = find_service (tcp_services, &start->service_descriptor,
1589 ntohs (start->tcp_header.dpt))))
1591 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1592 _("No service found for %s on port %d!\n"),
1594 ntohs (start->tcp_header.dpt));
1595 GNUNET_STATISTICS_update (stats,
1596 gettext_noop ("# TCP requests dropped (no such service)"),
1598 return GNUNET_SYSERR;
1600 state->ri.remote_address = state->serv->address;
1601 setup_state_record (state);
1602 send_tcp_packet_via_tun (&state->ri.remote_address,
1603 &state->ri.local_address,
1605 &start[1], pkt_len);
1611 * Process a request to forward TCP data to the Internet via this peer.
1613 * @param cls closure, NULL
1614 * @param tunnel connection to the other end
1615 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1616 * @param sender who sent the message
1617 * @param message the actual message
1618 * @param atsi performance data for the connection
1619 * @return GNUNET_OK to keep the connection open,
1620 * GNUNET_SYSERR to close it (signal serious error)
1623 receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1624 void **tunnel_ctx GNUNET_UNUSED,
1625 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1626 const struct GNUNET_MessageHeader *message,
1627 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1629 struct TunnelState *state = *tunnel_ctx;
1630 const struct GNUNET_EXIT_TcpInternetStartMessage *start;
1631 uint16_t pkt_len = ntohs (message->size);
1632 const struct in_addr *v4;
1633 const struct in6_addr *v6;
1634 const void *payload;
1637 GNUNET_STATISTICS_update (stats,
1638 gettext_noop ("# Bytes received from MESH"),
1639 pkt_len, GNUNET_NO);
1640 GNUNET_STATISTICS_update (stats,
1641 gettext_noop ("# TCP IP-exit creation requests received via mesh"),
1643 if (pkt_len < sizeof (struct GNUNET_EXIT_TcpInternetStartMessage))
1645 GNUNET_break_op (0);
1646 return GNUNET_SYSERR;
1648 start = (const struct GNUNET_EXIT_TcpInternetStartMessage*) message;
1649 pkt_len -= sizeof (struct GNUNET_EXIT_TcpInternetStartMessage);
1650 if ( (NULL == state) ||
1651 (NULL != state->serv) ||
1652 (NULL != state->heap_node) )
1654 GNUNET_break_op (0);
1655 return GNUNET_SYSERR;
1657 af = (int) ntohl (start->af);
1658 state->ri.remote_address.af = af;
1662 if (pkt_len < sizeof (struct in_addr))
1664 GNUNET_break_op (0);
1665 return GNUNET_SYSERR;
1669 GNUNET_break_op (0);
1670 return GNUNET_SYSERR;
1672 v4 = (const struct in_addr*) &start[1];
1674 pkt_len -= sizeof (struct in_addr);
1675 state->ri.remote_address.address.ipv4 = *v4;
1678 if (pkt_len < sizeof (struct in6_addr))
1680 GNUNET_break_op (0);
1681 return GNUNET_SYSERR;
1685 GNUNET_break_op (0);
1686 return GNUNET_SYSERR;
1688 v6 = (const struct in6_addr*) &start[1];
1690 pkt_len -= sizeof (struct in6_addr);
1691 state->ri.remote_address.address.ipv6 = *v6;
1694 GNUNET_break_op (0);
1695 return GNUNET_SYSERR;
1698 char buf[INET6_ADDRSTRLEN];
1699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1700 "Received data from %s for starting TCP stream to %s:%u\n",
1701 GNUNET_i2s (sender),
1703 &state->ri.remote_address.address,
1705 (unsigned int) ntohs (start->tcp_header.dpt));
1708 state->ri.remote_address.proto = IPPROTO_TCP;
1709 state->ri.remote_address.port = ntohs (start->tcp_header.dpt);
1710 setup_state_record (state);
1711 send_tcp_packet_via_tun (&state->ri.remote_address,
1712 &state->ri.local_address,
1720 * Process a request to forward TCP data on an established
1721 * connection via this peer.
1723 * @param cls closure, NULL
1724 * @param tunnel connection to the other end
1725 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1726 * @param sender who sent the message
1727 * @param message the actual message
1728 * @param atsi performance data for the connection
1729 * @return GNUNET_OK to keep the connection open,
1730 * GNUNET_SYSERR to close it (signal serious error)
1733 receive_tcp_data (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1734 void **tunnel_ctx GNUNET_UNUSED,
1735 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1736 const struct GNUNET_MessageHeader *message,
1737 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1739 struct TunnelState *state = *tunnel_ctx;
1740 const struct GNUNET_EXIT_TcpDataMessage *data;
1741 uint16_t pkt_len = ntohs (message->size);
1743 GNUNET_STATISTICS_update (stats,
1744 gettext_noop ("# Bytes received from MESH"),
1745 pkt_len, GNUNET_NO);
1746 GNUNET_STATISTICS_update (stats,
1747 gettext_noop ("# TCP data requests received via mesh"),
1749 if (pkt_len < sizeof (struct GNUNET_EXIT_TcpDataMessage))
1751 GNUNET_break_op (0);
1752 return GNUNET_SYSERR;
1754 data = (const struct GNUNET_EXIT_TcpDataMessage*) message;
1755 pkt_len -= sizeof (struct GNUNET_EXIT_TcpDataMessage);
1756 if ( (NULL == state) ||
1757 (NULL == state->heap_node) )
1759 /* connection should have been up! */
1760 GNUNET_STATISTICS_update (stats,
1761 gettext_noop ("# TCP DATA requests dropped (no session)"),
1763 return GNUNET_SYSERR;
1765 GNUNET_break_op (ntohl (data->reserved) == 0);
1767 char buf[INET6_ADDRSTRLEN];
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769 "Received additional %u bytes of data from %s for TCP stream to %s:%u\n",
1771 GNUNET_i2s (sender),
1772 inet_ntop (state->ri.remote_address.af,
1773 &state->ri.remote_address.address,
1775 (unsigned int) state->ri.remote_address.port);
1778 send_tcp_packet_via_tun (&state->ri.remote_address,
1779 &state->ri.local_address,
1787 * Send an ICMP packet via the TUN interface.
1789 * @param destination_address IP to use for the ICMP packet's destination
1790 * @param source_address IP to use for the ICMP packet's source
1791 * @param icmp_header ICMP header to send
1792 * @param payload payload of the ICMP packet (does NOT include ICMP header)
1793 * @param payload_length number of bytes of data in payload
1796 send_icmp_packet_via_tun (const struct SocketAddress *destination_address,
1797 const struct SocketAddress *source_address,
1798 const struct GNUNET_TUN_IcmpHeader *icmp_header,
1799 const void *payload, size_t payload_length)
1802 struct GNUNET_TUN_IcmpHeader *icmp;
1804 GNUNET_STATISTICS_update (stats,
1805 gettext_noop ("# ICMP packets sent via TUN"),
1807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1808 "Sending packet with %u bytes ICMP payload via TUN\n",
1809 (unsigned int) payload_length);
1810 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1811 switch (destination_address->af)
1814 len += sizeof (struct GNUNET_TUN_IPv4Header);
1817 len += sizeof (struct GNUNET_TUN_IPv6Header);
1823 len += sizeof (struct GNUNET_TUN_IcmpHeader);
1824 len += payload_length;
1825 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1832 struct GNUNET_MessageHeader *hdr;
1833 struct GNUNET_TUN_Layer2PacketHeader *tun;
1835 hdr= (struct GNUNET_MessageHeader *) buf;
1836 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1837 hdr->size = htons (len);
1838 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
1839 tun->flags = htons (0);
1840 switch (source_address->af)
1844 struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
1846 tun->proto = htons (ETH_P_IPV4);
1847 GNUNET_TUN_initialize_ipv4_header (ipv4,
1849 sizeof (struct GNUNET_TUN_IcmpHeader) + payload_length,
1850 &source_address->address.ipv4,
1851 &destination_address->address.ipv4);
1852 icmp = (struct GNUNET_TUN_IcmpHeader*) &ipv4[1];
1857 struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
1859 tun->proto = htons (ETH_P_IPV6);
1860 GNUNET_TUN_initialize_ipv6_header (ipv6,
1862 sizeof (struct GNUNET_TUN_IcmpHeader) + payload_length,
1863 &source_address->address.ipv6,
1864 &destination_address->address.ipv6);
1865 icmp = (struct GNUNET_TUN_IcmpHeader*) &ipv6[1];
1872 *icmp = *icmp_header;
1876 GNUNET_TUN_calculate_icmp_checksum (icmp,
1879 (void) GNUNET_HELPER_send (helper_handle,
1880 (const struct GNUNET_MessageHeader*) buf,
1888 * Synthesize a plausible ICMP payload for an ICMPv4 error
1889 * response on the given tunnel.
1891 * @param state tunnel information
1892 * @param ipp IPv6 header to fill in (ICMP payload)
1893 * @param udp "UDP" header to fill in (ICMP payload); might actually
1894 * also be the first 8 bytes of the TCP header
1897 make_up_icmpv4_payload (struct TunnelState *state,
1898 struct GNUNET_TUN_IPv4Header *ipp,
1899 struct GNUNET_TUN_UdpHeader *udp)
1901 GNUNET_TUN_initialize_ipv4_header (ipp,
1902 state->ri.remote_address.proto,
1903 sizeof (struct GNUNET_TUN_TcpHeader),
1904 &state->ri.remote_address.address.ipv4,
1905 &state->ri.local_address.address.ipv4);
1906 udp->spt = htons (state->ri.remote_address.port);
1907 udp->dpt = htons (state->ri.local_address.port);
1908 udp->len = htons (0);
1909 udp->crc = htons (0);
1914 * Synthesize a plausible ICMP payload for an ICMPv6 error
1915 * response on the given tunnel.
1917 * @param state tunnel information
1918 * @param ipp IPv6 header to fill in (ICMP payload)
1919 * @param udp "UDP" header to fill in (ICMP payload); might actually
1920 * also be the first 8 bytes of the TCP header
1923 make_up_icmpv6_payload (struct TunnelState *state,
1924 struct GNUNET_TUN_IPv6Header *ipp,
1925 struct GNUNET_TUN_UdpHeader *udp)
1927 GNUNET_TUN_initialize_ipv6_header (ipp,
1928 state->ri.remote_address.proto,
1929 sizeof (struct GNUNET_TUN_TcpHeader),
1930 &state->ri.remote_address.address.ipv6,
1931 &state->ri.local_address.address.ipv6);
1932 udp->spt = htons (state->ri.remote_address.port);
1933 udp->dpt = htons (state->ri.local_address.port);
1934 udp->len = htons (0);
1935 udp->crc = htons (0);
1940 * Process a request to forward ICMP data to the Internet via this peer.
1942 * @param cls closure, NULL
1943 * @param tunnel connection to the other end
1944 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1945 * @param sender who sent the message
1946 * @param message the actual message
1947 * @param atsi performance data for the connection
1948 * @return GNUNET_OK to keep the connection open,
1949 * GNUNET_SYSERR to close it (signal serious error)
1952 receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1953 void **tunnel_ctx GNUNET_UNUSED,
1954 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1955 const struct GNUNET_MessageHeader *message,
1956 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1958 struct TunnelState *state = *tunnel_ctx;
1959 const struct GNUNET_EXIT_IcmpInternetMessage *msg;
1960 uint16_t pkt_len = ntohs (message->size);
1961 const struct in_addr *v4;
1962 const struct in6_addr *v6;
1963 const void *payload;
1964 char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8];
1967 GNUNET_STATISTICS_update (stats,
1968 gettext_noop ("# Bytes received from MESH"),
1969 pkt_len, GNUNET_NO);
1970 GNUNET_STATISTICS_update (stats,
1971 gettext_noop ("# ICMP IP-exit requests received via mesh"),
1973 if (pkt_len < sizeof (struct GNUNET_EXIT_IcmpInternetMessage))
1975 GNUNET_break_op (0);
1976 return GNUNET_SYSERR;
1978 msg = (const struct GNUNET_EXIT_IcmpInternetMessage*) message;
1979 pkt_len -= sizeof (struct GNUNET_EXIT_IcmpInternetMessage);
1981 af = (int) ntohl (msg->af);
1982 if ( (NULL != state->heap_node) &&
1983 (af != state->ri.remote_address.af) )
1985 /* other peer switched AF on this tunnel; not allowed */
1986 GNUNET_break_op (0);
1987 return GNUNET_SYSERR;
1993 if (pkt_len < sizeof (struct in_addr))
1995 GNUNET_break_op (0);
1996 return GNUNET_SYSERR;
2000 GNUNET_break_op (0);
2001 return GNUNET_SYSERR;
2003 v4 = (const struct in_addr*) &msg[1];
2005 pkt_len -= sizeof (struct in_addr);
2006 state->ri.remote_address.address.ipv4 = *v4;
2007 if (NULL == state->heap_node)
2009 state->ri.remote_address.af = af;
2010 state->ri.remote_address.proto = IPPROTO_ICMP;
2011 setup_state_record (state);
2013 /* check that ICMP type is something we want to support
2014 and possibly make up payload! */
2015 switch (msg->icmp_header.type)
2017 case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
2018 case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
2020 case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
2021 case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
2022 case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
2025 GNUNET_break_op (0);
2026 return GNUNET_SYSERR;
2028 /* make up payload */
2030 struct GNUNET_TUN_IPv4Header *ipp = (struct GNUNET_TUN_IPv4Header *) buf;
2031 struct GNUNET_TUN_UdpHeader *udp = (struct GNUNET_TUN_UdpHeader *) &ipp[1];
2033 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2034 pkt_len = sizeof (struct GNUNET_TUN_IPv4Header) + 8;
2035 make_up_icmpv4_payload (state,
2042 GNUNET_break_op (0);
2043 GNUNET_STATISTICS_update (stats,
2044 gettext_noop ("# ICMPv4 packets dropped (type not allowed)"),
2046 return GNUNET_SYSERR;
2051 if (pkt_len < sizeof (struct in6_addr))
2053 GNUNET_break_op (0);
2054 return GNUNET_SYSERR;
2058 GNUNET_break_op (0);
2059 return GNUNET_SYSERR;
2061 v6 = (const struct in6_addr*) &msg[1];
2063 pkt_len -= sizeof (struct in6_addr);
2064 state->ri.remote_address.address.ipv6 = *v6;
2065 if (NULL == state->heap_node)
2067 state->ri.remote_address.af = af;
2068 state->ri.remote_address.proto = IPPROTO_ICMP;
2069 setup_state_record (state);
2071 /* check that ICMP type is something we want to support
2072 and possibly make up payload! */
2073 switch (msg->icmp_header.type)
2075 case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
2076 case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
2078 case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
2079 case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
2080 case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
2081 case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
2084 GNUNET_break_op (0);
2085 return GNUNET_SYSERR;
2087 /* make up payload */
2089 struct GNUNET_TUN_IPv6Header *ipp = (struct GNUNET_TUN_IPv6Header *) buf;
2090 struct GNUNET_TUN_UdpHeader *udp = (struct GNUNET_TUN_UdpHeader *) &ipp[1];
2092 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2093 pkt_len = sizeof (struct GNUNET_TUN_IPv6Header) + 8;
2094 make_up_icmpv6_payload (state,
2101 GNUNET_break_op (0);
2102 GNUNET_STATISTICS_update (stats,
2103 gettext_noop ("# ICMPv6 packets dropped (type not allowed)"),
2105 return GNUNET_SYSERR;
2111 GNUNET_break_op (0);
2112 return GNUNET_SYSERR;
2116 char buf[INET6_ADDRSTRLEN];
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2118 "Received ICMP data from %s for forwarding to %s\n",
2119 GNUNET_i2s (sender),
2121 &state->ri.remote_address.address,
2122 buf, sizeof (buf)));
2124 send_icmp_packet_via_tun (&state->ri.remote_address,
2125 &state->ri.local_address,
2133 * Setup ICMP payload for ICMP error messages. Called
2134 * for both IPv4 and IPv6 addresses.
2136 * @param state context for creating the IP Packet
2137 * @param buf where to create the payload, has at least
2138 * sizeof (struct GNUNET_TUN_IPv6Header) + 8 bytes
2139 * @return number of bytes of payload we created in buf
2142 make_up_icmp_service_payload (struct TunnelState *state,
2145 switch (state->serv->address.af)
2149 struct GNUNET_TUN_IPv4Header *ipv4;
2150 struct GNUNET_TUN_UdpHeader *udp;
2152 ipv4 = (struct GNUNET_TUN_IPv4Header *)buf;
2153 udp = (struct GNUNET_TUN_UdpHeader *) &ipv4[1];
2154 make_up_icmpv4_payload (state,
2157 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2158 return sizeof (struct GNUNET_TUN_IPv4Header) + 8;
2163 struct GNUNET_TUN_IPv6Header *ipv6;
2164 struct GNUNET_TUN_UdpHeader *udp;
2166 ipv6 = (struct GNUNET_TUN_IPv6Header *)buf;
2167 udp = (struct GNUNET_TUN_UdpHeader *) &ipv6[1];
2168 make_up_icmpv6_payload (state,
2171 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2172 return sizeof (struct GNUNET_TUN_IPv6Header) + 8;
2183 * Process a request via mesh to send ICMP data to a service
2184 * offered by this system.
2186 * @param cls closure, NULL
2187 * @param tunnel connection to the other end
2188 * @param tunnel_ctx pointer to our 'struct TunnelState *'
2189 * @param sender who sent the message
2190 * @param message the actual message
2191 * @param atsi performance data for the connection
2192 * @return GNUNET_OK to keep the connection open,
2193 * GNUNET_SYSERR to close it (signal serious error)
2196 receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
2198 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
2199 const struct GNUNET_MessageHeader *message,
2200 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
2202 struct TunnelState *state = *tunnel_ctx;
2203 const struct GNUNET_EXIT_IcmpServiceMessage *msg;
2204 uint16_t pkt_len = ntohs (message->size);
2205 struct GNUNET_TUN_IcmpHeader icmp;
2206 char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8];
2207 const void *payload;
2209 GNUNET_STATISTICS_update (stats,
2210 gettext_noop ("# Bytes received from MESH"),
2211 pkt_len, GNUNET_NO);
2212 GNUNET_STATISTICS_update (stats,
2213 gettext_noop ("# ICMP service requests received via mesh"),
2215 /* check that we got at least a valid header */
2216 if (pkt_len < sizeof (struct GNUNET_EXIT_IcmpServiceMessage))
2218 GNUNET_break_op (0);
2219 return GNUNET_SYSERR;
2221 msg = (const struct GNUNET_EXIT_IcmpServiceMessage*) message;
2222 pkt_len -= sizeof (struct GNUNET_EXIT_IcmpServiceMessage);
2223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2224 "Received data from %s for forwarding to ICMP service %s\n",
2225 GNUNET_i2s (sender),
2226 GNUNET_h2s (&msg->service_descriptor));
2227 if (NULL == state->serv)
2229 /* first packet to service must not be ICMP (cannot determine service!) */
2230 GNUNET_break_op (0);
2231 return GNUNET_SYSERR;
2233 icmp = msg->icmp_header;
2235 state->ri.remote_address = state->serv->address;
2236 setup_state_record (state);
2238 /* check that ICMP type is something we want to support,
2239 perform ICMP PT if needed ans possibly make up payload */
2243 switch (msg->icmp_header.type)
2245 case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
2246 if (state->serv->address.af == AF_INET6)
2247 icmp.type = GNUNET_TUN_ICMPTYPE6_ECHO_REPLY;
2249 case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
2250 if (state->serv->address.af == AF_INET6)
2251 icmp.type = GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST;
2253 case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
2254 if (state->serv->address.af == AF_INET6)
2255 icmp.type = GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE;
2258 GNUNET_break_op (0);
2259 return GNUNET_SYSERR;
2262 pkt_len = make_up_icmp_service_payload (state, buf);
2264 case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
2265 if (state->serv->address.af == AF_INET6)
2266 icmp.type = GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED;
2269 GNUNET_break_op (0);
2270 return GNUNET_SYSERR;
2273 pkt_len = make_up_icmp_service_payload (state, buf);
2275 case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
2276 if (state->serv->address.af == AF_INET6)
2278 GNUNET_STATISTICS_update (stats,
2279 gettext_noop ("# ICMPv4 packets dropped (impossible PT to v6)"),
2285 GNUNET_break_op (0);
2286 return GNUNET_SYSERR;
2289 pkt_len = make_up_icmp_service_payload (state, buf);
2292 GNUNET_break_op (0);
2293 GNUNET_STATISTICS_update (stats,
2294 gettext_noop ("# ICMPv4 packets dropped (type not allowed)"),
2296 return GNUNET_SYSERR;
2298 /* end of AF_INET */
2301 switch (msg->icmp_header.type)
2303 case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
2304 if (state->serv->address.af == AF_INET)
2305 icmp.type = GNUNET_TUN_ICMPTYPE_ECHO_REPLY;
2307 case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
2308 if (state->serv->address.af == AF_INET)
2309 icmp.type = GNUNET_TUN_ICMPTYPE_ECHO_REQUEST;
2311 case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
2312 if (state->serv->address.af == AF_INET)
2313 icmp.type = GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE;
2316 GNUNET_break_op (0);
2317 return GNUNET_SYSERR;
2320 pkt_len = make_up_icmp_service_payload (state, buf);
2322 case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
2323 if (state->serv->address.af == AF_INET)
2324 icmp.type = GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED;
2327 GNUNET_break_op (0);
2328 return GNUNET_SYSERR;
2331 pkt_len = make_up_icmp_service_payload (state, buf);
2333 case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
2334 case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
2335 if (state->serv->address.af == AF_INET)
2337 GNUNET_STATISTICS_update (stats,
2338 gettext_noop ("# ICMPv6 packets dropped (impossible PT to v4)"),
2344 GNUNET_break_op (0);
2345 return GNUNET_SYSERR;
2348 pkt_len = make_up_icmp_service_payload (state, buf);
2351 GNUNET_break_op (0);
2352 GNUNET_STATISTICS_update (stats,
2353 gettext_noop ("# ICMPv6 packets dropped (type not allowed)"),
2355 return GNUNET_SYSERR;
2357 /* end of AF_INET6 */
2360 GNUNET_break_op (0);
2361 return GNUNET_SYSERR;
2364 send_icmp_packet_via_tun (&state->ri.remote_address,
2365 &state->ri.local_address,
2373 * Send a UDP packet via the TUN interface.
2375 * @param destination_address IP and port to use for the UDP packet's destination
2376 * @param source_address IP and port to use for the UDP packet's source
2377 * @param payload payload of the UDP packet (does NOT include UDP header)
2378 * @param payload_length number of bytes of data in payload
2381 send_udp_packet_via_tun (const struct SocketAddress *destination_address,
2382 const struct SocketAddress *source_address,
2383 const void *payload, size_t payload_length)
2387 GNUNET_STATISTICS_update (stats,
2388 gettext_noop ("# UDP packets sent via TUN"),
2390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2391 "Sending packet with %u bytes UDP payload via TUN\n",
2392 (unsigned int) payload_length);
2393 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
2394 switch (source_address->af)
2397 len += sizeof (struct GNUNET_TUN_IPv4Header);
2400 len += sizeof (struct GNUNET_TUN_IPv6Header);
2406 len += sizeof (struct GNUNET_TUN_UdpHeader);
2407 len += payload_length;
2408 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2415 struct GNUNET_MessageHeader *hdr;
2416 struct GNUNET_TUN_Layer2PacketHeader *tun;
2418 hdr= (struct GNUNET_MessageHeader *) buf;
2419 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
2420 hdr->size = htons (len);
2421 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
2422 tun->flags = htons (0);
2423 switch (source_address->af)
2427 struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
2429 tun->proto = htons (ETH_P_IPV4);
2430 prepare_ipv4_packet (payload, payload_length,
2434 destination_address,
2440 struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
2442 tun->proto = htons (ETH_P_IPV6);
2443 prepare_ipv6_packet (payload, payload_length,
2447 destination_address,
2455 (void) GNUNET_HELPER_send (helper_handle,
2456 (const struct GNUNET_MessageHeader*) buf,
2464 * Process a request to forward UDP data to the Internet via this peer.
2466 * @param cls closure, NULL
2467 * @param tunnel connection to the other end
2468 * @param tunnel_ctx pointer to our 'struct TunnelState *'
2469 * @param sender who sent the message
2470 * @param message the actual message
2471 * @param atsi performance data for the connection
2472 * @return GNUNET_OK to keep the connection open,
2473 * GNUNET_SYSERR to close it (signal serious error)
2476 receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
2477 void **tunnel_ctx GNUNET_UNUSED,
2478 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
2479 const struct GNUNET_MessageHeader *message,
2480 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
2482 struct TunnelState *state = *tunnel_ctx;
2483 const struct GNUNET_EXIT_UdpInternetMessage *msg;
2484 uint16_t pkt_len = ntohs (message->size);
2485 const struct in_addr *v4;
2486 const struct in6_addr *v6;
2487 const void *payload;
2490 GNUNET_STATISTICS_update (stats,
2491 gettext_noop ("# Bytes received from MESH"),
2492 pkt_len, GNUNET_NO);
2493 GNUNET_STATISTICS_update (stats,
2494 gettext_noop ("# UDP IP-exit requests received via mesh"),
2496 if (pkt_len < sizeof (struct GNUNET_EXIT_UdpInternetMessage))
2498 GNUNET_break_op (0);
2499 return GNUNET_SYSERR;
2501 msg = (const struct GNUNET_EXIT_UdpInternetMessage*) message;
2502 pkt_len -= sizeof (struct GNUNET_EXIT_UdpInternetMessage);
2503 af = (int) ntohl (msg->af);
2504 state->ri.remote_address.af = af;
2508 if (pkt_len < sizeof (struct in_addr))
2510 GNUNET_break_op (0);
2511 return GNUNET_SYSERR;
2515 GNUNET_break_op (0);
2516 return GNUNET_SYSERR;
2518 v4 = (const struct in_addr*) &msg[1];
2520 pkt_len -= sizeof (struct in_addr);
2521 state->ri.remote_address.address.ipv4 = *v4;
2524 if (pkt_len < sizeof (struct in6_addr))
2526 GNUNET_break_op (0);
2527 return GNUNET_SYSERR;
2531 GNUNET_break_op (0);
2532 return GNUNET_SYSERR;
2534 v6 = (const struct in6_addr*) &msg[1];
2536 pkt_len -= sizeof (struct in6_addr);
2537 state->ri.remote_address.address.ipv6 = *v6;
2540 GNUNET_break_op (0);
2541 return GNUNET_SYSERR;
2544 char buf[INET6_ADDRSTRLEN];
2545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2546 "Received data from %s for forwarding to UDP %s:%u\n",
2547 GNUNET_i2s (sender),
2549 &state->ri.remote_address.address,
2551 (unsigned int) ntohs (msg->destination_port));
2553 state->ri.remote_address.proto = IPPROTO_UDP;
2554 state->ri.remote_address.port = msg->destination_port;
2555 if (NULL == state->heap_node)
2556 setup_state_record (state);
2557 if (0 != ntohs (msg->source_port))
2558 state->ri.local_address.port = msg->source_port;
2559 send_udp_packet_via_tun (&state->ri.remote_address,
2560 &state->ri.local_address,
2567 * Process a request via mesh to send a request to a UDP service
2568 * offered by this system.
2570 * @param cls closure, NULL
2571 * @param tunnel connection to the other end
2572 * @param tunnel_ctx pointer to our 'struct TunnelState *'
2573 * @param sender who sent the message
2574 * @param message the actual message
2575 * @param atsi performance data for the connection
2576 * @return GNUNET_OK to keep the connection open,
2577 * GNUNET_SYSERR to close it (signal serious error)
2580 receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
2582 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
2583 const struct GNUNET_MessageHeader *message,
2584 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
2586 struct TunnelState *state = *tunnel_ctx;
2587 const struct GNUNET_EXIT_UdpServiceMessage *msg;
2588 uint16_t pkt_len = ntohs (message->size);
2590 GNUNET_STATISTICS_update (stats,
2591 gettext_noop ("# Bytes received from MESH"),
2592 pkt_len, GNUNET_NO);
2593 GNUNET_STATISTICS_update (stats,
2594 gettext_noop ("# UDP service requests received via mesh"),
2596 /* check that we got at least a valid header */
2597 if (pkt_len < sizeof (struct GNUNET_EXIT_UdpServiceMessage))
2599 GNUNET_break_op (0);
2600 return GNUNET_SYSERR;
2602 msg = (const struct GNUNET_EXIT_UdpServiceMessage*) message;
2603 pkt_len -= sizeof (struct GNUNET_EXIT_UdpServiceMessage);
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 "Received data from %s for forwarding to UDP service %s on port %u\n",
2606 GNUNET_i2s (sender),
2607 GNUNET_h2s (&msg->service_descriptor),
2608 (unsigned int) ntohs (msg->destination_port));
2609 if (NULL == (state->serv = find_service (udp_services, &msg->service_descriptor,
2610 ntohs (msg->destination_port))))
2612 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2613 _("No service found for %s on port %d!\n"),
2615 ntohs (msg->destination_port));
2616 GNUNET_STATISTICS_update (stats,
2617 gettext_noop ("# UDP requests dropped (no such service)"),
2619 return GNUNET_SYSERR;
2621 state->ri.remote_address = state->serv->address;
2622 setup_state_record (state);
2623 if (0 != ntohs (msg->source_port))
2624 state->ri.local_address.port = msg->source_port;
2625 send_udp_packet_via_tun (&state->ri.remote_address,
2626 &state->ri.local_address,
2633 * Callback from GNUNET_MESH for new tunnels.
2635 * @param cls closure
2636 * @param tunnel new handle to the tunnel
2637 * @param initiator peer that started the tunnel
2638 * @param atsi performance information for the tunnel
2639 * @return initial tunnel context for the tunnel
2642 new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
2643 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
2644 const struct GNUNET_ATS_Information *ats GNUNET_UNUSED)
2646 struct TunnelState *s = GNUNET_malloc (sizeof (struct TunnelState));
2648 GNUNET_STATISTICS_update (stats,
2649 gettext_noop ("# Inbound MESH tunnels created"),
2651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2652 "Received inbound tunnel from `%s'\n",
2653 GNUNET_i2s (initiator));
2660 * Function called by mesh whenever an inbound tunnel is destroyed.
2661 * Should clean up any associated state.
2663 * @param cls closure (set from GNUNET_MESH_connect)
2664 * @param tunnel connection to the other end (henceforth invalid)
2665 * @param tunnel_ctx place where local state associated
2666 * with the tunnel is stored
2669 clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel,
2672 struct TunnelState *s = tunnel_ctx;
2673 struct TunnelMessageQueue *tnq;
2675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2676 "Tunnel destroyed\n");
2677 while (NULL != (tnq = s->head))
2679 GNUNET_CONTAINER_DLL_remove (s->head,
2684 if (s->heap_node != NULL)
2686 GNUNET_assert (GNUNET_YES ==
2687 GNUNET_CONTAINER_multihashmap_remove (connections_map,
2690 GNUNET_CONTAINER_heap_remove_node (s->heap_node);
2691 s->heap_node = NULL;
2695 GNUNET_MESH_notify_transmit_ready_cancel (s->th);
2703 * Function that frees everything from a hashmap
2707 * @param value value to free
2710 free_iterate (void *cls GNUNET_UNUSED,
2711 const GNUNET_HashCode * hash GNUNET_UNUSED, void *value)
2713 GNUNET_free (value);
2719 * Function scheduled as very last function, cleans up after us
2722 cleanup (void *cls GNUNET_UNUSED,
2723 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
2727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2728 "Exit service is shutting down now\n");
2729 if (helper_handle != NULL)
2731 GNUNET_HELPER_stop (helper_handle);
2732 helper_handle = NULL;
2734 if (mesh_handle != NULL)
2736 GNUNET_MESH_disconnect (mesh_handle);
2739 if (NULL != connections_map)
2741 GNUNET_CONTAINER_multihashmap_iterate (connections_map, &free_iterate, NULL);
2742 GNUNET_CONTAINER_multihashmap_destroy (connections_map);
2743 connections_map = NULL;
2745 if (NULL != connections_heap)
2747 GNUNET_CONTAINER_heap_destroy (connections_heap);
2748 connections_heap = NULL;
2750 if (NULL != tcp_services)
2752 GNUNET_CONTAINER_multihashmap_iterate (tcp_services, &free_service_record, NULL);
2753 GNUNET_CONTAINER_multihashmap_destroy (tcp_services);
2754 tcp_services = NULL;
2756 if (NULL != udp_services)
2758 GNUNET_CONTAINER_multihashmap_iterate (udp_services, &free_service_record, NULL);
2759 GNUNET_CONTAINER_multihashmap_destroy (udp_services);
2760 udp_services = NULL;
2764 GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
2768 GNUNET_free_non_null (exit_argv[i]);
2773 * Add services to the service map.
2775 * @param proto IPPROTO_TCP or IPPROTO_UDP
2776 * @param cpy copy of the service descriptor (can be mutilated)
2777 * @param name DNS name of the service
2780 add_services (int proto,
2787 struct LocalService *serv;
2789 for (redirect = strtok (cpy, " "); redirect != NULL;
2790 redirect = strtok (NULL, " "))
2792 if (NULL == (hostname = strstr (redirect, ":")))
2794 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2795 "option `%s' for domain `%s' is not formatted correctly!\n",
2802 if (NULL == (hostport = strstr (hostname, ":")))
2804 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2805 "option `%s' for domain `%s' is not formatted correctly!\n",
2813 int local_port = atoi (redirect);
2814 int remote_port = atoi (hostport);
2816 if (!((local_port > 0) && (local_port < 65536)))
2818 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2819 "`%s' is not a valid port number (for domain `%s')!", redirect,
2823 if (!((remote_port > 0) && (remote_port < 65536)))
2825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2826 "`%s' is not a valid port number (for domain `%s')!", hostport,
2831 serv = GNUNET_malloc (sizeof (struct LocalService));
2832 serv->my_port = (uint16_t) local_port;
2833 serv->address.port = remote_port;
2834 if (0 == strcmp ("localhost4", hostname))
2836 const char *ip4addr = exit_argv[4];
2838 serv->address.af = AF_INET;
2839 GNUNET_assert (1 != inet_pton (AF_INET, ip4addr, &serv->address.address.ipv4));
2841 else if (0 == strcmp ("localhost6", hostname))
2843 const char *ip6addr = exit_argv[2];
2845 serv->address.af = AF_INET6;
2846 GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, &serv->address.address.ipv6));
2850 struct addrinfo *res;
2853 ret = getaddrinfo (hostname, NULL, NULL, &res);
2854 if ( (ret != 0) || (res == NULL) )
2856 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2857 _("No addresses found for hostname `%s' of service `%s'!\n"),
2864 serv->address.af = res->ai_family;
2865 switch (res->ai_family)
2870 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2871 _("Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
2877 serv->address.address.ipv4 = ((struct sockaddr_in *) res->ai_addr)->sin_addr;
2882 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2883 _("Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
2889 serv->address.address.ipv6 = ((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
2893 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2894 _("No IP addresses found for hostname `%s' of service `%s'!\n"),
2902 store_service ((IPPROTO_UDP == proto) ? udp_services : tcp_services,
2911 * Reads the configuration servicecfg and populates udp_services
2914 * @param section name of section in config, equal to hostname
2917 read_service_conf (void *cls GNUNET_UNUSED, const char *section)
2921 if ((strlen (section) < 8) ||
2922 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
2925 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
2928 add_services (IPPROTO_UDP, cpy, section);
2932 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
2935 add_services (IPPROTO_TCP, cpy, section);
2942 * @brief Main function that will be run by the scheduler.
2944 * @param cls closure
2945 * @param args remaining command-line arguments
2946 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2947 * @param cfg_ configuration
2950 run (void *cls, char *const *args GNUNET_UNUSED,
2951 const char *cfgfile GNUNET_UNUSED,
2952 const struct GNUNET_CONFIGURATION_Handle *cfg_)
2954 static struct GNUNET_MESH_MessageHandler handlers[] = {
2955 {&receive_icmp_service, GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_SERVICE, 0},
2956 {&receive_icmp_remote, GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_INTERNET, 0},
2957 {&receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_UDP_TO_SERVICE, 0},
2958 {&receive_udp_remote, GNUNET_MESSAGE_TYPE_VPN_UDP_TO_INTERNET, 0},
2959 {&receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_TCP_TO_SERVICE_START, 0},
2960 {&receive_tcp_remote, GNUNET_MESSAGE_TYPE_VPN_TCP_TO_INTERNET_START, 0},
2961 {&receive_tcp_data, GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_EXIT, 0},
2965 static GNUNET_MESH_ApplicationType apptypes[] = {
2966 GNUNET_APPLICATION_TYPE_END,
2967 GNUNET_APPLICATION_TYPE_END,
2968 GNUNET_APPLICATION_TYPE_END
2970 unsigned int app_idx;
2979 GNUNET_OS_check_helper_binary ("gnunet-helper-exit"))
2982 "`%s' is not SUID, refusing to run.\n",
2983 "gnunet-helper-exit");
2988 stats = GNUNET_STATISTICS_create ("exit", cfg);
2989 ipv4_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "EXIT_IPV4");
2990 ipv6_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "EXIT_IPV6");
2991 ipv4_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_IPV4");
2992 ipv6_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_IPV6");
2993 if (ipv4_exit && (! ipv4_enabled))
2995 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2996 _("Cannot enable IPv4 exit but disable IPv4 on TUN interface, will use ENABLE_IPv4=YES\n"));
2997 ipv4_enabled = GNUNET_YES;
2999 if (ipv6_exit && (! ipv6_enabled))
3001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3002 _("Cannot enable IPv6 exit but disable IPv6 on TUN interface, will use ENABLE_IPv6=YES\n"));
3003 ipv6_enabled = GNUNET_YES;
3005 if (! (ipv4_enabled || ipv6_enabled))
3007 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3008 _("No useful service enabled. Exiting.\n"));
3009 GNUNET_SCHEDULER_shutdown ();
3013 if (GNUNET_YES == ipv4_exit)
3015 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_IPV4_GATEWAY;
3018 if (GNUNET_YES == ipv6_exit)
3020 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_IPV6_GATEWAY;
3024 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
3027 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_CONNECTIONS",
3029 max_connections = 1024;
3030 exit_argv[0] = GNUNET_strdup ("exit-gnunet");
3031 if (GNUNET_SYSERR ==
3032 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "TUN_IFNAME", &tun_ifname))
3034 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3035 "No entry 'TUN_IFNAME' in configuration!\n");
3036 GNUNET_SCHEDULER_shutdown ();
3039 exit_argv[1] = tun_ifname;
3042 if (GNUNET_SYSERR ==
3043 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "EXIT_IFNAME", &exit_ifname))
3045 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3046 "No entry 'EXIT_IFNAME' in configuration!\n");
3047 GNUNET_SCHEDULER_shutdown ();
3050 exit_argv[2] = exit_ifname;
3054 exit_argv[2] = GNUNET_strdup ("%");
3056 if (GNUNET_YES == ipv6_enabled)
3058 if ( (GNUNET_SYSERR ==
3059 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
3061 (1 != inet_pton (AF_INET6, ipv6addr, &exit_ipv6addr))) )
3063 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3064 "No valid entry 'IPV6ADDR' in configuration!\n");
3065 GNUNET_SCHEDULER_shutdown ();
3068 exit_argv[3] = ipv6addr;
3069 if (GNUNET_SYSERR ==
3070 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
3073 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3074 "No entry 'IPV6PREFIX' in configuration!\n");
3075 GNUNET_SCHEDULER_shutdown ();
3078 exit_argv[4] = ipv6prefix_s;
3080 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
3083 (ipv6prefix >= 127) )
3085 GNUNET_SCHEDULER_shutdown ();
3091 /* IPv6 explicitly disabled */
3092 exit_argv[3] = GNUNET_strdup ("-");
3093 exit_argv[4] = GNUNET_strdup ("-");
3095 if (GNUNET_YES == ipv4_enabled)
3097 if ( (GNUNET_SYSERR ==
3098 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
3100 (1 != inet_pton (AF_INET, ipv4addr, &exit_ipv4addr))) )
3102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3103 "No valid entry for 'IPV4ADDR' in configuration!\n");
3104 GNUNET_SCHEDULER_shutdown ();
3107 exit_argv[5] = ipv4addr;
3108 if ( (GNUNET_SYSERR ==
3109 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
3111 (1 != inet_pton (AF_INET, ipv4mask, &exit_ipv4mask))) )
3113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3114 "No valid entry 'IPV4MASK' in configuration!\n");
3115 GNUNET_SCHEDULER_shutdown ();
3118 exit_argv[6] = ipv4mask;
3122 /* IPv4 explicitly disabled */
3123 exit_argv[5] = GNUNET_strdup ("-");
3124 exit_argv[6] = GNUNET_strdup ("-");
3126 exit_argv[7] = NULL;
3128 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
3129 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
3130 GNUNET_CONFIGURATION_iterate_sections (cfg, &read_service_conf, NULL);
3132 connections_map = GNUNET_CONTAINER_multihashmap_create (65536);
3133 connections_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3135 = GNUNET_MESH_connect (cfg, 42 /* queue size */, NULL,
3137 &clean_tunnel, handlers,
3139 if (NULL == mesh_handle)
3141 GNUNET_SCHEDULER_shutdown ();
3144 helper_handle = GNUNET_HELPER_start ("gnunet-helper-exit",
3146 &message_token, NULL);
3153 * @param argc number of arguments from the command line
3154 * @param argv command line arguments
3155 * @return 0 ok, 1 on error
3158 main (int argc, char *const *argv)
3160 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3161 GNUNET_GETOPT_OPTION_END
3164 return (GNUNET_OK ==
3165 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit",
3167 ("Daemon to run to provide an IP exit node for the VPN"),
3168 options, &run, NULL)) ? global_ret : 1;
3172 /* end of gnunet-daemon-exit.c */