2 This file is part of GNUnet.
3 Copyright (C) 2010-2013, 2017 Christian Grothoff
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file exit/gnunet-daemon-exit.c
23 * @brief tool to allow IP traffic exit from the GNUnet cadet 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_dht_service.h"
42 #include "gnunet_cadet_service.h"
43 #include "gnunet_dnsparser_lib.h"
44 #include "gnunet_dnsstub_lib.h"
45 #include "gnunet_statistics_service.h"
46 #include "gnunet_constants.h"
47 #include "gnunet_signatures.h"
48 #include "gnunet_tun_lib.h"
49 #include "gnunet_regex_service.h"
51 #include "block_dns.h"
55 * Maximum path compression length for cadet regex announcing for IPv4 address
58 #define REGEX_MAX_PATH_LEN_IPV4 4
61 * Maximum path compression length for cadet regex announcing for IPv6 address
64 #define REGEX_MAX_PATH_LEN_IPV6 8
67 * How frequently do we re-announce the regex for the exit?
69 #define REGEX_REFRESH_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
72 * How frequently do we re-announce the DNS exit in the DHT?
74 #define DHT_PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
77 * How long do we typically sign the DNS exit advertisement for?
79 #define DNS_ADVERTISEMENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 3)
83 * Generic logging shorthand
85 #define LOG(kind, ...) \
86 GNUNET_log_from (kind, "exit", __VA_ARGS__);
90 * Information about an address.
95 * AF_INET or AF_INET6.
100 * Remote address information.
105 * Address, if af is AF_INET.
110 * Address, if af is AF_INET6.
112 struct in6_addr ipv6;
116 * IPPROTO_TCP or IPPROTO_UDP;
121 * Remote port, in host byte order!
129 * This struct is saved into the services-hashmap to represent
130 * a service this peer is specifically offering an exit for
131 * (for a specific domain name).
137 * Remote address to use for the service.
139 struct SocketAddress address;
142 * Descriptor for the service (CADET port).
144 struct GNUNET_HashCode descriptor;
147 * DNS name of the service.
152 * Open port with CADET.
154 struct GNUNET_CADET_Port *port;
157 * #GNUNET_YES if this is a UDP service, otherwise TCP.
165 * Information we use to track a connection (the classical 6-tuple of
166 * IP-version, protocol, source-IP, destination-IP, source-port and
169 struct RedirectInformation
173 * Address information for the other party (equivalent of the
174 * arguments one would give to "connect").
176 struct SocketAddress remote_address;
179 * Address information we used locally (AF and proto must match
180 * "remote_address"). Equivalent of the arguments one would give to
183 struct SocketAddress local_address;
186 Note 1: additional information might be added here in the
187 future to support protocols that require special handling,
190 Note 2: we might also sometimes not match on all components
191 of the tuple, to support protocols where things do not always
198 * This struct is saved into #connections_map to allow finding the
199 * right channel given an IP packet from TUN. It is also associated
200 * with the channel's closure so we can find it again for the next
201 * message from the channel.
206 * Cadet channel that is used for this connection.
208 struct GNUNET_CADET_Channel *channel;
211 * Who is the other end of this channel.
212 * FIXME is this needed? Only used for debugging messages
214 struct GNUNET_PeerIdentity peer;
217 * #GNUNET_NO if this is a channel for TCP/UDP,
218 * #GNUNET_YES if this is a channel for DNS,
219 * #GNUNET_SYSERR if the channel is not yet initialized.
229 * Heap node for this state in the connections_heap.
231 struct GNUNET_CONTAINER_HeapNode *heap_node;
234 * Key this state has in the #connections_map.
236 struct GNUNET_HashCode state_key;
239 * Associated service record, or NULL for no service.
241 struct LocalService *serv;
244 * Primary redirection information for this connection.
246 struct RedirectInformation ri;
253 * Socket we are using to transmit this request (must match if we receive
256 struct GNUNET_DNSSTUB_RequestSocket *rs;
259 * Original DNS request ID as used by the client.
261 uint16_t original_id;
264 * DNS request ID that we used for forwarding.
276 * Return value from 'main'.
278 static int global_ret;
281 * Handle to our regex announcement for IPv4.
283 static struct GNUNET_REGEX_Announcement *regex4;
286 * Handle to our regex announcement for IPv4.
288 static struct GNUNET_REGEX_Announcement *regex6;
291 * The handle to the configuration used throughout the process
293 static const struct GNUNET_CONFIGURATION_Handle *cfg;
296 * The handle to the helper
298 static struct GNUNET_HELPER_Handle *helper_handle;
301 * Arguments to the exit helper.
303 static char *exit_argv[8];
306 * IPv6 address of our TUN interface.
308 static struct in6_addr exit_ipv6addr;
311 * IPv6 prefix (0..127) from configuration file.
313 static unsigned long long ipv6prefix;
316 * IPv4 address of our TUN interface.
318 static struct in_addr exit_ipv4addr;
321 * IPv4 netmask of our TUN interface.
323 static struct in_addr exit_ipv4mask;
328 static struct GNUNET_STATISTICS_Handle *stats;
331 * The handle to cadet
333 static struct GNUNET_CADET_Handle *cadet_handle;
336 * This hashmaps contains the mapping from peer, service-descriptor,
337 * source-port and destination-port to a struct ChannelState
339 static struct GNUNET_CONTAINER_MultiHashMap *connections_map;
342 * Heap so we can quickly find "old" connections.
344 static struct GNUNET_CONTAINER_Heap *connections_heap;
347 * If there are at least this many connections, old ones will be removed
349 static unsigned long long max_connections;
352 * This hashmaps saves interesting things about the configured services
354 static struct GNUNET_CONTAINER_MultiHashMap *services;
357 * Array of all open DNS requests from channels.
359 static struct ChannelState *channels[UINT16_MAX + 1];
362 * Handle to the DNS Stub resolver.
364 static struct GNUNET_DNSSTUB_Context *dnsstub;
367 * Handle for ongoing DHT PUT operations to advertise exit service.
369 static struct GNUNET_DHT_PutHandle *dht_put;
374 static struct GNUNET_DHT_Handle *dht;
377 * Task for doing DHT PUTs to advertise exit service.
379 static struct GNUNET_SCHEDULER_Task *dht_task;
382 * Advertisement message we put into the DHT to advertise us
385 static struct GNUNET_DNS_Advertisement dns_advertisement;
388 * Key we store the DNS advertismenet under.
390 static struct GNUNET_HashCode dht_put_key;
393 * Private key for this peer.
395 static struct GNUNET_CRYPTO_EddsaPrivateKey *peer_key;
400 static struct GNUNET_CADET_Port *dns_port;
403 * Port for IPv4 exit.
405 static struct GNUNET_CADET_Port *cadet_port4;
408 * Port for IPv6 exit.
410 static struct GNUNET_CADET_Port *cadet_port6;
413 * Are we an IPv4-exit?
415 static int ipv4_exit;
418 * Are we an IPv6-exit?
420 static int ipv6_exit;
423 * Do we support IPv4 at all on the TUN interface?
425 static int ipv4_enabled;
428 * Do we support IPv6 at all on the TUN interface?
430 static int ipv6_enabled;
433 GNUNET_NETWORK_STRUCT_BEGIN
436 * Message with a DNS response.
438 struct DnsResponseMessage
441 * GNUnet header, of type #GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET
443 struct GNUNET_MessageHeader header;
448 struct GNUNET_TUN_DnsHeader dns;
450 /* Followed by more DNS payload */
453 GNUNET_NETWORK_STRUCT_END
457 * Callback called from DNSSTUB resolver when a resolution
461 * @param dns the response itself
462 * @param r number of bytes in @a dns
465 process_dns_result (void *cls,
466 const struct GNUNET_TUN_DnsHeader *dns,
469 struct ChannelState *ts;
470 struct GNUNET_MQ_Envelope *env;
471 struct DnsResponseMessage *resp;
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474 "Processing DNS result from stub resolver\n");
475 GNUNET_assert (NULL == cls);
478 /* Handle case that this is a reply to a request from a CADET DNS channel */
479 ts = channels[dns->id];
482 LOG (GNUNET_ERROR_TYPE_DEBUG,
483 "Got a response from the stub resolver for DNS request received via CADET!\n");
484 channels[dns->id] = NULL;
485 env = GNUNET_MQ_msg_extra (resp,
486 r - sizeof (struct GNUNET_TUN_DnsHeader),
487 GNUNET_MESSAGE_TYPE_VPN_DNS_FROM_INTERNET);
488 GNUNET_memcpy (&resp->dns,
491 resp->dns.id = ts->specifics.dns.original_id;
492 GNUNET_MQ_send (GNUNET_CADET_get_mq (ts->channel),
498 * Check a request via cadet to perform a DNS query.
500 * @param cls our `struct ChannelState *`
501 * @param msg the actual message
502 * @return #GNUNET_OK to keep the connection open,
503 * #GNUNET_SYSERR to close it (signal serious error)
506 check_dns_request (void *cls,
507 const struct DnsResponseMessage *msg)
509 struct ChannelState *ts = cls;
514 return GNUNET_SYSERR;
516 if (GNUNET_NO == ts->is_dns)
519 return GNUNET_SYSERR;
526 * Process a request via cadet to perform a DNS query.
528 * @param cls our `struct ChannelState *`
529 * @param msg the actual message
532 handle_dns_request (void *cls,
533 const struct DnsResponseMessage *msg)
535 struct ChannelState *ts = cls;
536 size_t mlen = ntohs (msg->header.size);
537 size_t dlen = mlen - sizeof (struct GNUNET_MessageHeader);
538 char buf[dlen] GNUNET_ALIGN;
539 struct GNUNET_TUN_DnsHeader *dout;
541 if (GNUNET_SYSERR == ts->is_dns)
543 /* channel is DNS from now on */
544 ts->is_dns = GNUNET_YES;
546 ts->specifics.dns.original_id = msg->dns.id;
547 if (channels[ts->specifics.dns.my_id] == ts)
548 channels[ts->specifics.dns.my_id] = NULL;
549 ts->specifics.dns.my_id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
551 channels[ts->specifics.dns.my_id] = ts;
555 dout = (struct GNUNET_TUN_DnsHeader *) buf;
556 dout->id = ts->specifics.dns.my_id;
557 ts->specifics.dns.rs = GNUNET_DNSSTUB_resolve (dnsstub,
562 if (NULL == ts->specifics.dns.rs)
567 GNUNET_CADET_receive_done (ts->channel);
572 * Given IP information about a connection, calculate the respective
573 * hash we would use for the #connections_map.
575 * @param hash resulting hash
576 * @param ri information about the connection
579 hash_redirect_info (struct GNUNET_HashCode *hash,
580 const struct RedirectInformation *ri)
586 sizeof (struct GNUNET_HashCode));
587 /* the GNUnet hashmap only uses the first sizeof(unsigned int) of the hash,
588 so we put the IP address in there (and hope for few collisions) */
590 switch (ri->remote_address.af)
594 &ri->remote_address.address.ipv4,
595 sizeof (struct in_addr));
596 off += sizeof (struct in_addr);
600 &ri->remote_address.address.ipv6,
601 sizeof (struct in6_addr));
602 off += sizeof (struct in_addr);
608 &ri->remote_address.port,
610 off += sizeof (uint16_t);
611 switch (ri->local_address.af)
615 &ri->local_address.address.ipv4,
616 sizeof (struct in_addr));
617 off += sizeof (struct in_addr);
621 &ri->local_address.address.ipv6,
622 sizeof (struct in6_addr));
623 off += sizeof (struct in_addr);
629 &ri->local_address.port,
631 off += sizeof (uint16_t);
633 &ri->remote_address.proto,
635 /* off += sizeof (uint8_t); */
640 * Get our connection tracking state. Warns if it does not exists,
641 * refreshes the timestamp if it does exist.
643 * @param af address family
644 * @param protocol IPPROTO_UDP or IPPROTO_TCP
645 * @param destination_ip target IP
646 * @param destination_port target port
647 * @param local_ip local IP
648 * @param local_port local port
649 * @param state_key set to hash's state if non-NULL
650 * @return NULL if we have no tracking information for this tuple
652 static struct ChannelState *
653 get_redirect_state (int af,
655 const void *destination_ip,
656 uint16_t destination_port,
657 const void *local_ip,
659 struct GNUNET_HashCode *state_key)
661 struct RedirectInformation ri;
662 struct GNUNET_HashCode key;
663 struct ChannelState *state;
665 if ( ( (af == AF_INET) && (protocol == IPPROTO_ICMP) ) ||
666 ( (af == AF_INET6) && (protocol == IPPROTO_ICMPV6) ) )
669 destination_port = 0;
672 ri.remote_address.af = af;
674 ri.remote_address.address.ipv4 = *((struct in_addr*) destination_ip);
676 ri.remote_address.address.ipv6 = * ((struct in6_addr*) destination_ip);
677 ri.remote_address.port = destination_port;
678 ri.remote_address.proto = protocol;
679 ri.local_address.af = af;
681 ri.local_address.address.ipv4 = *((struct in_addr*) local_ip);
683 ri.local_address.address.ipv6 = * ((struct in6_addr*) local_ip);
684 ri.local_address.port = local_port;
685 ri.local_address.proto = protocol;
686 hash_redirect_info (&key,
688 if (NULL != state_key)
690 state = GNUNET_CONTAINER_multihashmap_get (connections_map,
694 /* Mark this connection as freshly used */
695 if (NULL == state_key)
696 GNUNET_CONTAINER_heap_update_cost (state->specifics.tcp_udp.heap_node,
697 GNUNET_TIME_absolute_get ().abs_value_us);
704 * Check a request via cadet to send a request to a TCP service
705 * offered by this system.
707 * @param cls our `struct ChannelState *`
708 * @param start the actual message
709 * @return #GNUNET_OK to keep the connection open,
710 * #GNUNET_SYSERR to close it (signal serious error)
713 check_tcp_service (void *cls,
714 const struct GNUNET_EXIT_TcpServiceStartMessage *start)
716 struct ChannelState *state = cls;
721 return GNUNET_SYSERR;
723 if (GNUNET_YES == state->is_dns)
726 return GNUNET_SYSERR;
728 if (NULL == state->specifics.tcp_udp.serv)
731 return GNUNET_SYSERR;
733 if (NULL != state->specifics.tcp_udp.heap_node)
736 return GNUNET_SYSERR;
738 if (start->tcp_header.off * 4 < sizeof (struct GNUNET_TUN_TcpHeader))
741 return GNUNET_SYSERR;
748 * Prepare an IPv4 packet for transmission via the TUN interface.
749 * Initializes the IP header and calculates checksums (IP+UDP/TCP).
750 * For UDP, the UDP header will be fully created, whereas for TCP
751 * only the ports and checksum will be filled in. So for TCP,
752 * a skeleton TCP header must be part of the provided payload.
754 * @param payload payload of the packet (starting with UDP payload or
755 * TCP header, depending on protocol)
756 * @param payload_length number of bytes in @a payload
757 * @param protocol IPPROTO_UDP or IPPROTO_TCP
758 * @param tcp_header skeleton of the TCP header, NULL for UDP
759 * @param src_address source address to use (IP and port)
760 * @param dst_address destination address to use (IP and port)
761 * @param pkt4 where to write the assembled packet; must
762 * contain enough space for the IP header, UDP/TCP header
766 prepare_ipv4_packet (const void *payload,
767 size_t payload_length,
769 const struct GNUNET_TUN_TcpHeader *tcp_header,
770 const struct SocketAddress *src_address,
771 const struct SocketAddress *dst_address,
772 struct GNUNET_TUN_IPv4Header *pkt4)
776 len = payload_length;
780 len += sizeof (struct GNUNET_TUN_UdpHeader);
783 len += sizeof (struct GNUNET_TUN_TcpHeader);
784 GNUNET_assert (NULL != tcp_header);
790 if (len + sizeof (struct GNUNET_TUN_IPv4Header) > UINT16_MAX)
796 GNUNET_TUN_initialize_ipv4_header (pkt4,
799 &src_address->address.ipv4,
800 &dst_address->address.ipv4);
805 struct GNUNET_TUN_UdpHeader *pkt4_udp = (struct GNUNET_TUN_UdpHeader *) &pkt4[1];
807 pkt4_udp->source_port = htons (src_address->port);
808 pkt4_udp->destination_port = htons (dst_address->port);
809 pkt4_udp->len = htons ((uint16_t) payload_length);
810 GNUNET_TUN_calculate_udp4_checksum (pkt4,
814 GNUNET_memcpy (&pkt4_udp[1],
821 struct GNUNET_TUN_TcpHeader *pkt4_tcp = (struct GNUNET_TUN_TcpHeader *) &pkt4[1];
823 *pkt4_tcp = *tcp_header;
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825 "Sending TCP packet from port %u to port %u\n",
828 pkt4_tcp->source_port = htons (src_address->port);
829 pkt4_tcp->destination_port = htons (dst_address->port);
830 GNUNET_TUN_calculate_tcp4_checksum (pkt4,
834 GNUNET_memcpy (&pkt4_tcp[1],
846 * Prepare an IPv6 packet for transmission via the TUN interface.
847 * Initializes the IP header and calculates checksums (IP+UDP/TCP).
848 * For UDP, the UDP header will be fully created, whereas for TCP
849 * only the ports and checksum will be filled in. So for TCP,
850 * a skeleton TCP header must be part of the provided payload.
852 * @param payload payload of the packet (starting with UDP payload or
853 * TCP header, depending on protocol)
854 * @param payload_length number of bytes in @a payload
855 * @param protocol IPPROTO_UDP or IPPROTO_TCP
856 * @param tcp_header skeleton TCP header data to send, NULL for UDP
857 * @param src_address source address to use (IP and port)
858 * @param dst_address destination address to use (IP and port)
859 * @param pkt6 where to write the assembled packet; must
860 * contain enough space for the IP header, UDP/TCP header
864 prepare_ipv6_packet (const void *payload,
865 size_t payload_length,
867 const struct GNUNET_TUN_TcpHeader *tcp_header,
868 const struct SocketAddress *src_address,
869 const struct SocketAddress *dst_address,
870 struct GNUNET_TUN_IPv6Header *pkt6)
874 len = payload_length;
878 len += sizeof (struct GNUNET_TUN_UdpHeader);
881 len += sizeof (struct GNUNET_TUN_TcpHeader);
887 if (len > UINT16_MAX)
893 GNUNET_TUN_initialize_ipv6_header (pkt6,
896 &src_address->address.ipv6,
897 &dst_address->address.ipv6);
903 struct GNUNET_TUN_UdpHeader *pkt6_udp = (struct GNUNET_TUN_UdpHeader *) &pkt6[1];
905 pkt6_udp->source_port = htons (src_address->port);
906 pkt6_udp->destination_port = htons (dst_address->port);
907 pkt6_udp->len = htons ((uint16_t) payload_length);
908 GNUNET_TUN_calculate_udp6_checksum (pkt6,
912 GNUNET_memcpy (&pkt6_udp[1],
919 struct GNUNET_TUN_TcpHeader *pkt6_tcp = (struct GNUNET_TUN_TcpHeader *) &pkt6[1];
921 /* GNUNET_memcpy first here as some TCP header fields are initialized this way! */
922 *pkt6_tcp = *tcp_header;
923 pkt6_tcp->source_port = htons (src_address->port);
924 pkt6_tcp->destination_port = htons (dst_address->port);
925 GNUNET_TUN_calculate_tcp6_checksum (pkt6,
929 GNUNET_memcpy (&pkt6_tcp[1],
942 * Send a TCP packet via the TUN interface.
944 * @param destination_address IP and port to use for the TCP packet's destination
945 * @param source_address IP and port to use for the TCP packet's source
946 * @param tcp_header header template to use
947 * @param payload payload of the TCP packet
948 * @param payload_length number of bytes in @a payload
951 send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
952 const struct SocketAddress *source_address,
953 const struct GNUNET_TUN_TcpHeader *tcp_header,
955 size_t payload_length)
959 GNUNET_STATISTICS_update (stats,
960 gettext_noop ("# TCP packets sent via TUN"),
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Sending packet with %u bytes TCP payload via TUN\n",
965 (unsigned int) payload_length);
966 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
967 switch (source_address->af)
970 len += sizeof (struct GNUNET_TUN_IPv4Header);
973 len += sizeof (struct GNUNET_TUN_IPv6Header);
979 len += sizeof (struct GNUNET_TUN_TcpHeader);
980 len += payload_length;
981 if (len >= GNUNET_MAX_MESSAGE_SIZE)
987 char buf[len] GNUNET_ALIGN;
988 struct GNUNET_MessageHeader *hdr;
989 struct GNUNET_TUN_Layer2PacketHeader *tun;
991 hdr = (struct GNUNET_MessageHeader *) buf;
992 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
993 hdr->size = htons (len);
994 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
995 tun->flags = htons (0);
996 switch (source_address->af)
1000 struct GNUNET_TUN_IPv4Header *ipv4
1001 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
1003 tun->proto = htons (ETH_P_IPV4);
1004 prepare_ipv4_packet (payload,
1009 destination_address,
1015 struct GNUNET_TUN_IPv6Header *ipv6
1016 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
1018 tun->proto = htons (ETH_P_IPV6);
1019 prepare_ipv6_packet (payload,
1024 destination_address,
1032 if (NULL != helper_handle)
1033 (void) GNUNET_HELPER_send (helper_handle,
1034 (const struct GNUNET_MessageHeader*) buf,
1043 * Send an ICMP packet via the TUN interface.
1045 * @param destination_address IP to use for the ICMP packet's destination
1046 * @param source_address IP to use for the ICMP packet's source
1047 * @param icmp_header ICMP header to send
1048 * @param payload payload of the ICMP packet (does NOT include ICMP header)
1049 * @param payload_length number of bytes of data in @a payload
1052 send_icmp_packet_via_tun (const struct SocketAddress *destination_address,
1053 const struct SocketAddress *source_address,
1054 const struct GNUNET_TUN_IcmpHeader *icmp_header,
1055 const void *payload, size_t payload_length)
1058 struct GNUNET_TUN_IcmpHeader *icmp;
1060 GNUNET_STATISTICS_update (stats,
1061 gettext_noop ("# ICMP packets sent via TUN"),
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 "Sending packet with %u bytes ICMP payload via TUN\n",
1065 (unsigned int) payload_length);
1066 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1067 switch (destination_address->af)
1070 len += sizeof (struct GNUNET_TUN_IPv4Header);
1073 len += sizeof (struct GNUNET_TUN_IPv6Header);
1079 len += sizeof (struct GNUNET_TUN_IcmpHeader);
1080 len += payload_length;
1081 if (len >= GNUNET_MAX_MESSAGE_SIZE)
1087 char buf[len] GNUNET_ALIGN;
1088 struct GNUNET_MessageHeader *hdr;
1089 struct GNUNET_TUN_Layer2PacketHeader *tun;
1091 hdr= (struct GNUNET_MessageHeader *) buf;
1092 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1093 hdr->size = htons (len);
1094 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
1095 tun->flags = htons (0);
1096 switch (source_address->af)
1100 struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
1102 tun->proto = htons (ETH_P_IPV4);
1103 GNUNET_TUN_initialize_ipv4_header (ipv4,
1105 sizeof (struct GNUNET_TUN_IcmpHeader) + payload_length,
1106 &source_address->address.ipv4,
1107 &destination_address->address.ipv4);
1108 icmp = (struct GNUNET_TUN_IcmpHeader*) &ipv4[1];
1113 struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
1115 tun->proto = htons (ETH_P_IPV6);
1116 GNUNET_TUN_initialize_ipv6_header (ipv6,
1118 sizeof (struct GNUNET_TUN_IcmpHeader) + payload_length,
1119 &source_address->address.ipv6,
1120 &destination_address->address.ipv6);
1121 icmp = (struct GNUNET_TUN_IcmpHeader*) &ipv6[1];
1128 *icmp = *icmp_header;
1129 GNUNET_memcpy (&icmp[1],
1132 GNUNET_TUN_calculate_icmp_checksum (icmp,
1135 if (NULL != helper_handle)
1136 (void) GNUNET_HELPER_send (helper_handle,
1137 (const struct GNUNET_MessageHeader*) buf,
1145 * We need to create a (unique) fresh local address (IP+port).
1148 * @param af desired address family
1149 * @param proto desired protocol (IPPROTO_UDP or IPPROTO_TCP)
1150 * @param local_address address to initialize
1153 setup_fresh_address (int af,
1155 struct SocketAddress *local_address)
1157 local_address->af = af;
1158 local_address->proto = (uint8_t) proto;
1159 /* default "local" port range is often 32768--61000,
1160 so we pick a random value in that range */
1161 if ( ( (af == AF_INET) && (proto == IPPROTO_ICMP) ) ||
1162 ( (af == AF_INET6) && (proto == IPPROTO_ICMPV6) ) )
1163 local_address->port = 0;
1166 = (uint16_t) 32768 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1172 struct in_addr addr;
1173 struct in_addr mask;
1176 addr = exit_ipv4addr;
1177 mask = exit_ipv4mask;
1178 if (0 == ~mask.s_addr)
1180 /* only one valid IP anyway */
1181 local_address->address.ipv4 = addr;
1184 /* Given 192.168.0.1/255.255.0.0, we want a mask
1185 of '192.168.255.255', thus: */
1186 mask.s_addr = addr.s_addr | ~mask.s_addr;
1187 /* Pick random IPv4 address within the subnet, except 'addr' or 'mask' itself */
1190 rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1192 local_address->address.ipv4.s_addr = (addr.s_addr | rnd.s_addr) & mask.s_addr;
1194 while ( (local_address->address.ipv4.s_addr == addr.s_addr) ||
1195 (local_address->address.ipv4.s_addr == mask.s_addr) );
1200 struct in6_addr addr;
1201 struct in6_addr mask;
1202 struct in6_addr rnd;
1205 addr = exit_ipv6addr;
1206 GNUNET_assert (ipv6prefix < 128);
1207 if (ipv6prefix == 127)
1209 /* only one valid IP anyway */
1210 local_address->address.ipv6 = addr;
1213 /* Given ABCD::/96, we want a mask of 'ABCD::FFFF:FFFF,
1216 for (i=127;i>=ipv6prefix;i--)
1217 mask.s6_addr[i / 8] |= (1 << (i % 8));
1219 /* Pick random IPv6 address within the subnet, except 'addr' or 'mask' itself */
1224 rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1226 local_address->address.ipv6.s6_addr[i]
1227 = (addr.s6_addr[i] | rnd.s6_addr[i]) & mask.s6_addr[i];
1230 while ( (0 == memcmp (&local_address->address.ipv6,
1232 sizeof (struct in6_addr))) ||
1233 (0 == memcmp (&local_address->address.ipv6,
1235 sizeof (struct in6_addr))) );
1245 * We are starting a fresh connection (TCP or UDP) and need
1246 * to pick a source port and IP address (within the correct
1247 * range and address family) to associate replies with the
1248 * connection / correct cadet channel. This function generates
1249 * a "fresh" source IP and source port number for a connection
1250 * After picking a good source address, this function sets up
1251 * the state in the 'connections_map' and 'connections_heap'
1252 * to allow finding the state when needed later. The function
1253 * also makes sure that we remain within memory limits by
1254 * cleaning up 'old' states.
1256 * @param state skeleton state to setup a record for; should
1257 * 'state->specifics.tcp_udp.ri.remote_address' filled in so that
1258 * this code can determine which AF/protocol is
1259 * going to be used (the 'channel' should also
1260 * already be set); after calling this function,
1261 * heap_node and the local_address will be
1262 * also initialized (heap_node != NULL can be
1263 * used to test if a state has been fully setup).
1266 setup_state_record (struct ChannelState *state)
1268 struct GNUNET_HashCode key;
1269 struct ChannelState *s;
1271 /* generate fresh, unique address */
1274 if (NULL == state->specifics.tcp_udp.serv)
1275 setup_fresh_address (state->specifics.tcp_udp.ri.remote_address.af,
1276 state->specifics.tcp_udp.ri.remote_address.proto,
1277 &state->specifics.tcp_udp.ri.local_address);
1279 setup_fresh_address (state->specifics.tcp_udp.serv->address.af,
1280 state->specifics.tcp_udp.serv->address.proto,
1281 &state->specifics.tcp_udp.ri.local_address);
1283 get_redirect_state (state->specifics.tcp_udp.ri.remote_address.af,
1284 state->specifics.tcp_udp.ri.remote_address.proto,
1285 &state->specifics.tcp_udp.ri.remote_address.address,
1286 state->specifics.tcp_udp.ri.remote_address.port,
1287 &state->specifics.tcp_udp.ri.local_address.address,
1288 state->specifics.tcp_udp.ri.local_address.port,
1291 char buf[INET6_ADDRSTRLEN];
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293 "Picked local address %s:%u for new connection\n",
1294 inet_ntop (state->specifics.tcp_udp.ri.local_address.af,
1295 &state->specifics.tcp_udp.ri.local_address.address,
1298 (unsigned int) state->specifics.tcp_udp.ri.local_address.port);
1300 state->specifics.tcp_udp.state_key = key;
1301 GNUNET_assert (GNUNET_OK ==
1302 GNUNET_CONTAINER_multihashmap_put (connections_map,
1304 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1305 state->specifics.tcp_udp.heap_node
1306 = GNUNET_CONTAINER_heap_insert (connections_heap,
1308 GNUNET_TIME_absolute_get ().abs_value_us);
1309 while (GNUNET_CONTAINER_heap_get_size (connections_heap) > max_connections)
1311 s = GNUNET_CONTAINER_heap_remove_root (connections_heap);
1312 GNUNET_assert (state != s);
1313 s->specifics.tcp_udp.heap_node = NULL;
1314 GNUNET_CADET_channel_destroy (s->channel);
1315 GNUNET_assert (GNUNET_OK ==
1316 GNUNET_CONTAINER_multihashmap_remove (connections_map,
1317 &s->specifics.tcp_udp.state_key,
1325 * Send a UDP packet via the TUN interface.
1327 * @param destination_address IP and port to use for the UDP packet's destination
1328 * @param source_address IP and port to use for the UDP packet's source
1329 * @param payload payload of the UDP packet (does NOT include UDP header)
1330 * @param payload_length number of bytes of data in @a payload
1333 send_udp_packet_via_tun (const struct SocketAddress *destination_address,
1334 const struct SocketAddress *source_address,
1335 const void *payload, size_t payload_length)
1339 GNUNET_STATISTICS_update (stats,
1340 gettext_noop ("# UDP packets sent via TUN"),
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1343 "Sending packet with %u bytes UDP payload via TUN\n",
1344 (unsigned int) payload_length);
1345 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1346 switch (source_address->af)
1349 len += sizeof (struct GNUNET_TUN_IPv4Header);
1352 len += sizeof (struct GNUNET_TUN_IPv6Header);
1358 len += sizeof (struct GNUNET_TUN_UdpHeader);
1359 len += payload_length;
1360 if (len >= GNUNET_MAX_MESSAGE_SIZE)
1366 char buf[len] GNUNET_ALIGN;
1367 struct GNUNET_MessageHeader *hdr;
1368 struct GNUNET_TUN_Layer2PacketHeader *tun;
1370 hdr= (struct GNUNET_MessageHeader *) buf;
1371 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1372 hdr->size = htons (len);
1373 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
1374 tun->flags = htons (0);
1375 switch (source_address->af)
1379 struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
1381 tun->proto = htons (ETH_P_IPV4);
1382 prepare_ipv4_packet (payload,
1387 destination_address,
1393 struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
1395 tun->proto = htons (ETH_P_IPV6);
1396 prepare_ipv6_packet (payload,
1401 destination_address,
1409 if (NULL != helper_handle)
1410 (void) GNUNET_HELPER_send (helper_handle,
1411 (const struct GNUNET_MessageHeader*) buf,
1419 * Check a request to forward UDP data to the Internet via this peer.
1421 * @param cls our `struct ChannelState *`
1422 * @param msg the actual message
1423 * @return #GNUNET_OK to keep the connection open,
1424 * #GNUNET_SYSERR to close it (signal serious error)
1427 check_udp_remote (void *cls,
1428 const struct GNUNET_EXIT_UdpInternetMessage *msg)
1430 struct ChannelState *state = cls;
1432 if (GNUNET_YES == state->is_dns)
1434 GNUNET_break_op (0);
1435 return GNUNET_SYSERR;
1442 * Process a request to forward UDP data to the Internet via this peer.
1444 * @param cls our `struct ChannelState *`
1445 * @param msg the actual message
1448 handle_udp_remote (void *cls,
1449 const struct GNUNET_EXIT_UdpInternetMessage *msg)
1451 struct ChannelState *state = cls;
1452 uint16_t pkt_len = ntohs (msg->header.size) - sizeof (struct GNUNET_EXIT_UdpInternetMessage);
1453 const struct in_addr *v4;
1454 const struct in6_addr *v6;
1455 const void *payload;
1458 if (GNUNET_SYSERR == state->is_dns)
1460 /* channel is UDP/TCP from now on */
1461 state->is_dns = GNUNET_NO;
1463 GNUNET_STATISTICS_update (stats,
1464 gettext_noop ("# Bytes received from CADET"),
1465 pkt_len, GNUNET_NO);
1466 GNUNET_STATISTICS_update (stats,
1467 gettext_noop ("# UDP IP-exit requests received via cadet"),
1469 af = (int) ntohl (msg->af);
1470 state->specifics.tcp_udp.ri.remote_address.af = af;
1474 if (pkt_len < sizeof (struct in_addr))
1476 GNUNET_break_op (0);
1481 GNUNET_break_op (0);
1484 v4 = (const struct in_addr*) &msg[1];
1486 pkt_len -= sizeof (struct in_addr);
1487 state->specifics.tcp_udp.ri.remote_address.address.ipv4 = *v4;
1490 if (pkt_len < sizeof (struct in6_addr))
1492 GNUNET_break_op (0);
1497 GNUNET_break_op (0);
1500 v6 = (const struct in6_addr*) &msg[1];
1502 pkt_len -= sizeof (struct in6_addr);
1503 state->specifics.tcp_udp.ri.remote_address.address.ipv6 = *v6;
1506 GNUNET_break_op (0);
1510 char buf[INET6_ADDRSTRLEN];
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "Received data from %s for forwarding to UDP %s:%u\n",
1513 GNUNET_i2s (&state->peer),
1515 &state->specifics.tcp_udp.ri.remote_address.address,
1517 (unsigned int) ntohs (msg->destination_port));
1519 state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_UDP;
1520 state->specifics.tcp_udp.ri.remote_address.port = msg->destination_port;
1521 if (NULL == state->specifics.tcp_udp.heap_node)
1522 setup_state_record (state);
1523 if (0 != ntohs (msg->source_port))
1524 state->specifics.tcp_udp.ri.local_address.port = msg->source_port;
1525 send_udp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
1526 &state->specifics.tcp_udp.ri.local_address,
1529 GNUNET_CADET_receive_done (state->channel);
1534 * Check a request via cadet to send a request to a UDP service
1535 * offered by this system.
1537 * @param cls our `struct ChannelState *`
1538 * @param msg the actual message
1539 * @return #GNUNET_OK to keep the connection open,
1540 * #GNUNET_SYSERR to close it (signal serious error)
1543 check_udp_service (void *cls,
1544 const struct GNUNET_EXIT_UdpServiceMessage *msg)
1546 struct ChannelState *state = cls;
1548 if (NULL == state->specifics.tcp_udp.serv)
1550 GNUNET_break_op (0);
1551 return GNUNET_SYSERR;
1558 * Process a request via cadet to send a request to a UDP service
1559 * offered by this system.
1561 * @param cls our `struct ChannelState *`
1562 * @param msg the actual message
1565 handle_udp_service (void *cls,
1566 const struct GNUNET_EXIT_UdpServiceMessage *msg)
1568 struct ChannelState *state = cls;
1569 uint16_t pkt_len = ntohs (msg->header.size) - sizeof (struct GNUNET_EXIT_UdpServiceMessage);
1571 GNUNET_STATISTICS_update (stats,
1572 gettext_noop ("# Bytes received from CADET"),
1573 pkt_len, GNUNET_NO);
1574 GNUNET_STATISTICS_update (stats,
1575 gettext_noop ("# UDP service requests received via cadet"),
1577 LOG (GNUNET_ERROR_TYPE_DEBUG,
1578 "Received data from %s for forwarding to UDP service %s on port %u\n",
1579 GNUNET_i2s (&state->peer),
1580 GNUNET_h2s (&state->specifics.tcp_udp.serv->descriptor),
1581 (unsigned int) ntohs (msg->destination_port));
1582 setup_state_record (state);
1583 if (0 != ntohs (msg->source_port))
1584 state->specifics.tcp_udp.ri.local_address.port = msg->source_port;
1585 send_udp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
1586 &state->specifics.tcp_udp.ri.local_address,
1589 GNUNET_CADET_receive_done (state->channel);
1594 * Process a request via cadet to send a request to a TCP service
1595 * offered by this system.
1597 * @param cls our `struct ChannelState *`
1598 * @param start the actual message
1599 * @return #GNUNET_OK to keep the connection open,
1600 * #GNUNET_SYSERR to close it (signal serious error)
1603 handle_tcp_service (void *cls,
1604 const struct GNUNET_EXIT_TcpServiceStartMessage *start)
1606 struct ChannelState *state = cls;
1607 uint16_t pkt_len = ntohs (start->header.size) - sizeof (struct GNUNET_EXIT_TcpServiceStartMessage);
1609 if (GNUNET_SYSERR == state->is_dns)
1611 /* channel is UDP/TCP from now on */
1612 state->is_dns = GNUNET_NO;
1614 GNUNET_STATISTICS_update (stats,
1615 gettext_noop ("# TCP service creation requests received via cadet"),
1618 GNUNET_STATISTICS_update (stats,
1619 gettext_noop ("# Bytes received from CADET"),
1622 GNUNET_break_op (ntohl (start->reserved) == 0);
1623 /* setup fresh connection */
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 "Received data from %s for forwarding to TCP service %s on port %u\n",
1626 GNUNET_i2s (&state->peer),
1627 GNUNET_h2s (&state->specifics.tcp_udp.serv->descriptor),
1628 (unsigned int) ntohs (start->tcp_header.destination_port));
1629 setup_state_record (state);
1630 send_tcp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
1631 &state->specifics.tcp_udp.ri.local_address,
1635 GNUNET_CADET_receive_done (state->channel);
1640 * Check a request to forward TCP data to the Internet via this peer.
1642 * @param cls our `struct ChannelState *`
1643 * @param start the actual message
1644 * @return #GNUNET_OK to keep the connection open,
1645 * #GNUNET_SYSERR to close it (signal serious error)
1648 check_tcp_remote (void *cls,
1649 const struct GNUNET_EXIT_TcpInternetStartMessage *start)
1651 struct ChannelState *state = cls;
1655 GNUNET_break_op (0);
1656 return GNUNET_SYSERR;
1658 if (GNUNET_YES == state->is_dns)
1660 GNUNET_break_op (0);
1661 return GNUNET_SYSERR;
1663 if ( (NULL != state->specifics.tcp_udp.serv) ||
1664 (NULL != state->specifics.tcp_udp.heap_node) )
1666 GNUNET_break_op (0);
1667 return GNUNET_SYSERR;
1669 if (start->tcp_header.off * 4 < sizeof (struct GNUNET_TUN_TcpHeader))
1671 GNUNET_break_op (0);
1672 return GNUNET_SYSERR;
1679 * Process a request to forward TCP data to the Internet via this peer.
1681 * @param cls our `struct ChannelState *`
1682 * @param start the actual message
1685 handle_tcp_remote (void *cls,
1686 const struct GNUNET_EXIT_TcpInternetStartMessage *start)
1688 struct ChannelState *state = cls;
1689 uint16_t pkt_len = ntohs (start->header.size) - sizeof (struct GNUNET_EXIT_TcpInternetStartMessage);
1690 const struct in_addr *v4;
1691 const struct in6_addr *v6;
1692 const void *payload;
1695 if (GNUNET_SYSERR == state->is_dns)
1697 /* channel is UDP/TCP from now on */
1698 state->is_dns = GNUNET_NO;
1700 GNUNET_STATISTICS_update (stats,
1701 gettext_noop ("# Bytes received from CADET"),
1702 pkt_len, GNUNET_NO);
1703 GNUNET_STATISTICS_update (stats,
1704 gettext_noop ("# TCP IP-exit creation requests received via cadet"),
1706 af = (int) ntohl (start->af);
1707 state->specifics.tcp_udp.ri.remote_address.af = af;
1711 if (pkt_len < sizeof (struct in_addr))
1713 GNUNET_break_op (0);
1718 GNUNET_break_op (0);
1721 v4 = (const struct in_addr*) &start[1];
1723 pkt_len -= sizeof (struct in_addr);
1724 state->specifics.tcp_udp.ri.remote_address.address.ipv4 = *v4;
1727 if (pkt_len < sizeof (struct in6_addr))
1729 GNUNET_break_op (0);
1734 GNUNET_break_op (0);
1737 v6 = (const struct in6_addr*) &start[1];
1739 pkt_len -= sizeof (struct in6_addr);
1740 state->specifics.tcp_udp.ri.remote_address.address.ipv6 = *v6;
1743 GNUNET_break_op (0);
1747 char buf[INET6_ADDRSTRLEN];
1748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1749 "Received payload from %s for existing TCP stream to %s:%u\n",
1750 GNUNET_i2s (&state->peer),
1752 &state->specifics.tcp_udp.ri.remote_address.address,
1754 (unsigned int) ntohs (start->tcp_header.destination_port));
1756 state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_TCP;
1757 state->specifics.tcp_udp.ri.remote_address.port = ntohs (start->tcp_header.destination_port);
1758 setup_state_record (state);
1759 send_tcp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
1760 &state->specifics.tcp_udp.ri.local_address,
1764 GNUNET_CADET_receive_done (state->channel);
1769 * Check a request to forward TCP data on an established
1770 * connection via this peer.
1772 * @param cls our `struct ChannelState *`
1773 * @param message the actual message
1774 * @return #GNUNET_OK to keep the connection open,
1775 * #GNUNET_SYSERR to close it (signal serious error)
1778 check_tcp_data (void *cls,
1779 const struct GNUNET_EXIT_TcpDataMessage *data)
1781 struct ChannelState *state = cls;
1783 if ( (NULL == state) ||
1784 (NULL == state->specifics.tcp_udp.heap_node) )
1786 /* connection should have been up! */
1787 GNUNET_STATISTICS_update (stats,
1788 gettext_noop ("# TCP DATA requests dropped (no session)"),
1790 GNUNET_break_op (0);
1791 return GNUNET_SYSERR;
1793 if (data->tcp_header.off * 4 < sizeof (struct GNUNET_TUN_TcpHeader))
1795 GNUNET_break_op (0);
1796 return GNUNET_SYSERR;
1798 if (GNUNET_YES == state->is_dns)
1800 GNUNET_break_op (0);
1801 return GNUNET_SYSERR;
1808 * Process a request to forward TCP data on an established
1809 * connection via this peer.
1811 * @param cls our `struct ChannelState *`
1812 * @param message the actual message
1815 handle_tcp_data (void *cls,
1816 const struct GNUNET_EXIT_TcpDataMessage *data)
1818 struct ChannelState *state = cls;
1819 uint16_t pkt_len = ntohs (data->header.size) - sizeof (struct GNUNET_EXIT_TcpDataMessage);
1821 GNUNET_STATISTICS_update (stats,
1822 gettext_noop ("# Bytes received from CADET"),
1823 pkt_len, GNUNET_NO);
1824 GNUNET_STATISTICS_update (stats,
1825 gettext_noop ("# TCP data requests received via cadet"),
1827 if (GNUNET_SYSERR == state->is_dns)
1829 /* channel is UDP/TCP from now on */
1830 state->is_dns = GNUNET_NO;
1833 GNUNET_break_op (ntohl (data->reserved) == 0);
1835 char buf[INET6_ADDRSTRLEN];
1836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1837 "Received additional %u bytes of data from %s for TCP stream to %s:%u\n",
1839 GNUNET_i2s (&state->peer),
1840 inet_ntop (state->specifics.tcp_udp.ri.remote_address.af,
1841 &state->specifics.tcp_udp.ri.remote_address.address,
1843 (unsigned int) state->specifics.tcp_udp.ri.remote_address.port);
1846 send_tcp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
1847 &state->specifics.tcp_udp.ri.local_address,
1850 GNUNET_CADET_receive_done (state->channel);
1855 * Synthesize a plausible ICMP payload for an ICMPv4 error
1856 * response on the given channel.
1858 * @param state channel information
1859 * @param ipp IPv6 header to fill in (ICMP payload)
1860 * @param udp "UDP" header to fill in (ICMP payload); might actually
1861 * also be the first 8 bytes of the TCP header
1864 make_up_icmpv4_payload (struct ChannelState *state,
1865 struct GNUNET_TUN_IPv4Header *ipp,
1866 struct GNUNET_TUN_UdpHeader *udp)
1868 GNUNET_TUN_initialize_ipv4_header (ipp,
1869 state->specifics.tcp_udp.ri.remote_address.proto,
1870 sizeof (struct GNUNET_TUN_TcpHeader),
1871 &state->specifics.tcp_udp.ri.remote_address.address.ipv4,
1872 &state->specifics.tcp_udp.ri.local_address.address.ipv4);
1873 udp->source_port = htons (state->specifics.tcp_udp.ri.remote_address.port);
1874 udp->destination_port = htons (state->specifics.tcp_udp.ri.local_address.port);
1875 udp->len = htons (0);
1876 udp->crc = htons (0);
1881 * Synthesize a plausible ICMP payload for an ICMPv6 error
1882 * response on the given channel.
1884 * @param state channel information
1885 * @param ipp IPv6 header to fill in (ICMP payload)
1886 * @param udp "UDP" header to fill in (ICMP payload); might actually
1887 * also be the first 8 bytes of the TCP header
1890 make_up_icmpv6_payload (struct ChannelState *state,
1891 struct GNUNET_TUN_IPv6Header *ipp,
1892 struct GNUNET_TUN_UdpHeader *udp)
1894 GNUNET_TUN_initialize_ipv6_header (ipp,
1895 state->specifics.tcp_udp.ri.remote_address.proto,
1896 sizeof (struct GNUNET_TUN_TcpHeader),
1897 &state->specifics.tcp_udp.ri.remote_address.address.ipv6,
1898 &state->specifics.tcp_udp.ri.local_address.address.ipv6);
1899 udp->source_port = htons (state->specifics.tcp_udp.ri.remote_address.port);
1900 udp->destination_port = htons (state->specifics.tcp_udp.ri.local_address.port);
1901 udp->len = htons (0);
1902 udp->crc = htons (0);
1907 * Check a request to forward ICMP data to the Internet via this peer.
1909 * @param cls our `struct ChannelState *`
1910 * @param msg the actual message
1911 * @return #GNUNET_OK to keep the connection open,
1912 * #GNUNET_SYSERR to close it (signal serious error)
1915 check_icmp_remote (void *cls,
1916 const struct GNUNET_EXIT_IcmpInternetMessage *msg)
1918 struct ChannelState *state = cls;
1920 if (GNUNET_YES == state->is_dns)
1922 GNUNET_break_op (0);
1923 return GNUNET_SYSERR;
1930 * Process a request to forward ICMP data to the Internet via this peer.
1932 * @param cls our `struct ChannelState *`
1933 * @param msg the actual message
1936 handle_icmp_remote (void *cls,
1937 const struct GNUNET_EXIT_IcmpInternetMessage *msg)
1939 struct ChannelState *state = cls;
1940 uint16_t pkt_len = ntohs (msg->header.size) - sizeof (struct GNUNET_EXIT_IcmpInternetMessage);
1941 const struct in_addr *v4;
1942 const struct in6_addr *v6;
1943 const void *payload;
1944 char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8] GNUNET_ALIGN;
1947 if (GNUNET_SYSERR == state->is_dns)
1949 /* channel is UDP/TCP from now on */
1950 state->is_dns = GNUNET_NO;
1952 GNUNET_STATISTICS_update (stats,
1953 gettext_noop ("# Bytes received from CADET"),
1954 pkt_len, GNUNET_NO);
1955 GNUNET_STATISTICS_update (stats,
1956 gettext_noop ("# ICMP IP-exit requests received via cadet"),
1959 af = (int) ntohl (msg->af);
1960 if ( (NULL != state->specifics.tcp_udp.heap_node) &&
1961 (af != state->specifics.tcp_udp.ri.remote_address.af) )
1963 /* other peer switched AF on this channel; not allowed */
1964 GNUNET_break_op (0);
1971 if (pkt_len < sizeof (struct in_addr))
1973 GNUNET_break_op (0);
1978 GNUNET_break_op (0);
1981 v4 = (const struct in_addr*) &msg[1];
1983 pkt_len -= sizeof (struct in_addr);
1984 state->specifics.tcp_udp.ri.remote_address.address.ipv4 = *v4;
1985 if (NULL == state->specifics.tcp_udp.heap_node)
1987 state->specifics.tcp_udp.ri.remote_address.af = af;
1988 state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_ICMP;
1989 setup_state_record (state);
1991 /* check that ICMP type is something we want to support
1992 and possibly make up payload! */
1993 switch (msg->icmp_header.type)
1995 case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
1996 case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
1998 case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
1999 case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
2000 case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
2003 GNUNET_break_op (0);
2006 /* make up payload */
2008 struct GNUNET_TUN_IPv4Header *ipp = (struct GNUNET_TUN_IPv4Header *) buf;
2009 struct GNUNET_TUN_UdpHeader *udp = (struct GNUNET_TUN_UdpHeader *) &ipp[1];
2011 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2012 pkt_len = sizeof (struct GNUNET_TUN_IPv4Header) + 8;
2013 make_up_icmpv4_payload (state,
2020 GNUNET_break_op (0);
2021 GNUNET_STATISTICS_update (stats,
2022 gettext_noop ("# ICMPv4 packets dropped (type not allowed)"),
2029 if (pkt_len < sizeof (struct in6_addr))
2031 GNUNET_break_op (0);
2036 GNUNET_break_op (0);
2039 v6 = (const struct in6_addr*) &msg[1];
2041 pkt_len -= sizeof (struct in6_addr);
2042 state->specifics.tcp_udp.ri.remote_address.address.ipv6 = *v6;
2043 if (NULL == state->specifics.tcp_udp.heap_node)
2045 state->specifics.tcp_udp.ri.remote_address.af = af;
2046 state->specifics.tcp_udp.ri.remote_address.proto = IPPROTO_ICMPV6;
2047 setup_state_record (state);
2049 /* check that ICMP type is something we want to support
2050 and possibly make up payload! */
2051 switch (msg->icmp_header.type)
2053 case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
2054 case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
2056 case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
2057 case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
2058 case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
2059 case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
2062 GNUNET_break_op (0);
2065 /* make up payload */
2067 struct GNUNET_TUN_IPv6Header *ipp = (struct GNUNET_TUN_IPv6Header *) buf;
2068 struct GNUNET_TUN_UdpHeader *udp = (struct GNUNET_TUN_UdpHeader *) &ipp[1];
2070 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2071 pkt_len = sizeof (struct GNUNET_TUN_IPv6Header) + 8;
2072 make_up_icmpv6_payload (state,
2079 GNUNET_break_op (0);
2080 GNUNET_STATISTICS_update (stats,
2081 gettext_noop ("# ICMPv6 packets dropped (type not allowed)"),
2089 GNUNET_break_op (0);
2094 char buf[INET6_ADDRSTRLEN];
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2096 "Received ICMP data from %s for forwarding to %s\n",
2097 GNUNET_i2s (&state->peer),
2099 &state->specifics.tcp_udp.ri.remote_address.address,
2100 buf, sizeof (buf)));
2102 send_icmp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
2103 &state->specifics.tcp_udp.ri.local_address,
2106 GNUNET_CADET_receive_done (state->channel);
2111 * Setup ICMP payload for ICMP error messages. Called
2112 * for both IPv4 and IPv6 addresses.
2114 * @param state context for creating the IP Packet
2115 * @param buf where to create the payload, has at least
2116 * sizeof (struct GNUNET_TUN_IPv6Header) + 8 bytes
2117 * @return number of bytes of payload we created in buf
2120 make_up_icmp_service_payload (struct ChannelState *state,
2123 switch (state->specifics.tcp_udp.serv->address.af)
2127 struct GNUNET_TUN_IPv4Header *ipv4;
2128 struct GNUNET_TUN_UdpHeader *udp;
2130 ipv4 = (struct GNUNET_TUN_IPv4Header *)buf;
2131 udp = (struct GNUNET_TUN_UdpHeader *) &ipv4[1];
2132 make_up_icmpv4_payload (state,
2135 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2136 return sizeof (struct GNUNET_TUN_IPv4Header) + 8;
2141 struct GNUNET_TUN_IPv6Header *ipv6;
2142 struct GNUNET_TUN_UdpHeader *udp;
2144 ipv6 = (struct GNUNET_TUN_IPv6Header *)buf;
2145 udp = (struct GNUNET_TUN_UdpHeader *) &ipv6[1];
2146 make_up_icmpv6_payload (state,
2149 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2150 return sizeof (struct GNUNET_TUN_IPv6Header) + 8;
2161 * Check a request via cadet to send ICMP data to a service
2162 * offered by this system.
2164 * @param cls our `struct ChannelState *`
2165 * @param msg the actual message
2166 * @return #GNUNET_OK to keep the connection open,
2167 * #GNUNET_SYSERR to close it (signal serious error)
2170 check_icmp_service (void *cls,
2171 const struct GNUNET_EXIT_IcmpServiceMessage *msg)
2173 struct ChannelState *state = cls;
2175 if (GNUNET_YES == state->is_dns)
2177 GNUNET_break_op (0);
2178 return GNUNET_SYSERR;
2180 if (NULL == state->specifics.tcp_udp.serv)
2182 GNUNET_break_op (0);
2183 return GNUNET_SYSERR;
2190 * Process a request via cadet to send ICMP data to a service
2191 * offered by this system.
2193 * @param cls our `struct ChannelState *`
2194 * @param msg the actual message
2197 handle_icmp_service (void *cls,
2198 const struct GNUNET_EXIT_IcmpServiceMessage *msg)
2200 struct ChannelState *state = cls;
2201 uint16_t pkt_len = ntohs (msg->header.size) - sizeof (struct GNUNET_EXIT_IcmpServiceMessage);
2202 struct GNUNET_TUN_IcmpHeader icmp;
2203 char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8] GNUNET_ALIGN;
2204 const void *payload;
2206 GNUNET_STATISTICS_update (stats,
2207 gettext_noop ("# Bytes received from CADET"),
2208 pkt_len, GNUNET_NO);
2209 GNUNET_STATISTICS_update (stats,
2210 gettext_noop ("# ICMP service requests received via cadet"),
2212 /* check that we got at least a valid header */
2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214 "Received data from %s for forwarding to ICMP service %s\n",
2215 GNUNET_i2s (&state->peer),
2216 GNUNET_h2s (&state->specifics.tcp_udp.serv->descriptor));
2217 icmp = msg->icmp_header;
2219 state->specifics.tcp_udp.ri.remote_address
2220 = state->specifics.tcp_udp.serv->address;
2221 setup_state_record (state);
2223 /* check that ICMP type is something we want to support,
2224 perform ICMP PT if needed ans possibly make up payload */
2228 switch (msg->icmp_header.type)
2230 case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
2231 if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
2232 icmp.type = GNUNET_TUN_ICMPTYPE6_ECHO_REPLY;
2234 case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
2235 if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
2236 icmp.type = GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST;
2238 case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
2239 if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
2240 icmp.type = GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE;
2243 GNUNET_break_op (0);
2247 pkt_len = make_up_icmp_service_payload (state, buf);
2249 case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
2250 if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
2251 icmp.type = GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED;
2254 GNUNET_break_op (0);
2258 pkt_len = make_up_icmp_service_payload (state, buf);
2260 case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
2261 if (state->specifics.tcp_udp.serv->address.af == AF_INET6)
2263 GNUNET_STATISTICS_update (stats,
2264 gettext_noop ("# ICMPv4 packets dropped (impossible PT to v6)"),
2270 GNUNET_break_op (0);
2274 pkt_len = make_up_icmp_service_payload (state, buf);
2277 GNUNET_break_op (0);
2278 GNUNET_STATISTICS_update (stats,
2279 gettext_noop ("# ICMPv4 packets dropped (type not allowed)"),
2283 /* end of AF_INET */
2286 switch (msg->icmp_header.type)
2288 case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
2289 if (state->specifics.tcp_udp.serv->address.af == AF_INET)
2290 icmp.type = GNUNET_TUN_ICMPTYPE_ECHO_REPLY;
2292 case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
2293 if (state->specifics.tcp_udp.serv->address.af == AF_INET)
2294 icmp.type = GNUNET_TUN_ICMPTYPE_ECHO_REQUEST;
2296 case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
2297 if (state->specifics.tcp_udp.serv->address.af == AF_INET)
2298 icmp.type = GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE;
2301 GNUNET_break_op (0);
2305 pkt_len = make_up_icmp_service_payload (state, buf);
2307 case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
2308 if (state->specifics.tcp_udp.serv->address.af == AF_INET)
2309 icmp.type = GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED;
2312 GNUNET_break_op (0);
2316 pkt_len = make_up_icmp_service_payload (state, buf);
2318 case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
2319 case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
2320 if (state->specifics.tcp_udp.serv->address.af == AF_INET)
2322 GNUNET_STATISTICS_update (stats,
2323 gettext_noop ("# ICMPv6 packets dropped (impossible PT to v4)"),
2329 GNUNET_break_op (0);
2333 pkt_len = make_up_icmp_service_payload (state, buf);
2336 GNUNET_break_op (0);
2337 GNUNET_STATISTICS_update (stats,
2338 gettext_noop ("# ICMPv6 packets dropped (type not allowed)"),
2342 /* end of AF_INET6 */
2345 GNUNET_break_op (0);
2349 send_icmp_packet_via_tun (&state->specifics.tcp_udp.ri.remote_address,
2350 &state->specifics.tcp_udp.ri.local_address,
2354 GNUNET_CADET_receive_done (state->channel);
2359 * Free memory associated with a service record.
2362 * @param key service descriptor
2363 * @param value service record to free
2364 * @return #GNUNET_OK
2367 free_service_record (void *cls,
2368 const struct GNUNET_HashCode *key,
2371 struct LocalService *service = value;
2373 GNUNET_assert (GNUNET_YES ==
2374 GNUNET_CONTAINER_multihashmap_remove (services,
2377 GNUNET_CADET_close_port (service->port);
2378 GNUNET_free_non_null (service->name);
2379 GNUNET_free (service);
2385 * Callback from CADET for new channels.
2387 * @param cls closure
2388 * @param channel new handle to the channel
2389 * @param initiator peer that started the channel
2390 * @return initial channel context for the channel
2393 new_service_channel (void *cls,
2394 struct GNUNET_CADET_Channel *channel,
2395 const struct GNUNET_PeerIdentity *initiator)
2397 struct LocalService *ls = cls;
2398 struct ChannelState *s = GNUNET_new (struct ChannelState);
2400 s->peer = *initiator;
2401 GNUNET_STATISTICS_update (stats,
2402 gettext_noop ("# Inbound CADET channels created"),
2405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2406 "Received inbound channel from `%s'\n",
2407 GNUNET_i2s (initiator));
2408 s->channel = channel;
2409 s->specifics.tcp_udp.serv = ls;
2410 s->specifics.tcp_udp.ri.remote_address = ls->address;
2416 * Function called by cadet whenever an inbound channel is destroyed.
2417 * Should clean up any associated state.
2419 * @param cls our `struct ChannelState *`
2420 * @param channel connection to the other end (henceforth invalid)
2423 clean_channel (void *cls,
2424 const struct GNUNET_CADET_Channel *channel)
2426 struct ChannelState *s = cls;
2428 LOG (GNUNET_ERROR_TYPE_DEBUG,
2429 "Channel destroyed\n");
2430 if (GNUNET_SYSERR == s->is_dns)
2435 if (GNUNET_YES == s->is_dns)
2437 if (channels[s->specifics.dns.my_id] == s)
2438 channels[s->specifics.dns.my_id] = NULL;
2442 if (NULL != s->specifics.tcp_udp.heap_node)
2444 GNUNET_assert (GNUNET_YES ==
2445 GNUNET_CONTAINER_multihashmap_remove (connections_map,
2446 &s->specifics.tcp_udp.state_key,
2448 GNUNET_CONTAINER_heap_remove_node (s->specifics.tcp_udp.heap_node);
2449 s->specifics.tcp_udp.heap_node = NULL;
2457 * Given a service descriptor and a destination port, find the
2458 * respective service entry.
2460 * @param proto IPPROTO_TCP or IPPROTO_UDP
2461 * @param name name of the service
2462 * @param destination_port destination port
2463 * @param service service information record to store (service->name will be set).
2466 store_service (int proto,
2468 uint16_t destination_port,
2469 struct LocalService *service)
2471 struct GNUNET_MQ_MessageHandler handlers[] = {
2472 GNUNET_MQ_hd_var_size (icmp_service,
2473 GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_SERVICE,
2474 struct GNUNET_EXIT_IcmpServiceMessage,
2476 GNUNET_MQ_hd_var_size (udp_service,
2477 GNUNET_MESSAGE_TYPE_VPN_UDP_TO_SERVICE,
2478 struct GNUNET_EXIT_UdpServiceMessage,
2480 GNUNET_MQ_hd_var_size (tcp_service,
2481 GNUNET_MESSAGE_TYPE_VPN_TCP_TO_SERVICE_START,
2482 struct GNUNET_EXIT_TcpServiceStartMessage,
2484 GNUNET_MQ_hd_var_size (tcp_data,
2485 GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_EXIT,
2486 struct GNUNET_EXIT_TcpDataMessage,
2488 GNUNET_MQ_handler_end ()
2491 struct GNUNET_HashCode cadet_port;
2493 service->name = GNUNET_strdup (name);
2494 GNUNET_TUN_service_name_to_hash (name,
2495 &service->descriptor);
2496 GNUNET_TUN_compute_service_cadet_port (&service->descriptor,
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 "Opening CADET port %s for SERVICE exit %s on port %u\n",
2501 GNUNET_h2s (&cadet_port),
2503 (unsigned int) destination_port);
2504 service->port = GNUNET_CADET_open_port (cadet_handle,
2506 &new_service_channel,
2511 service->is_udp = (IPPROTO_UDP == proto);
2513 GNUNET_CONTAINER_multihashmap_put (services,
2516 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
2518 GNUNET_CADET_close_port (service->port);
2519 GNUNET_free_non_null (service->name);
2520 GNUNET_free (service);
2521 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2522 _("Got duplicate service records for `%s:%u'\n"),
2524 (unsigned int) destination_port);
2530 * Send the given packet via the cadet channel.
2532 * @param s channel destination
2533 * @param env message to queue
2536 send_packet_to_cadet_channel (struct ChannelState *s,
2537 struct GNUNET_MQ_Envelope *env)
2539 GNUNET_assert (NULL != s);
2540 GNUNET_STATISTICS_update (stats,
2541 gettext_noop ("# Messages transmitted via cadet channels"),
2544 GNUNET_MQ_send (GNUNET_CADET_get_mq (s->channel),
2550 * @brief Handles an ICMP packet received from the helper.
2552 * @param icmp A pointer to the Packet
2553 * @param pktlen number of bytes in @a icmp
2554 * @param af address family (AFINET or AF_INET6)
2555 * @param destination_ip destination IP-address of the IP packet (should
2556 * be our local address)
2557 * @param source_ip original source IP-address of the IP packet (should
2558 * be the original destination address)
2561 icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
2564 const void *destination_ip,
2565 const void *source_ip)
2567 struct ChannelState *state;
2568 struct GNUNET_MQ_Envelope *env;
2569 struct GNUNET_EXIT_IcmpToVPNMessage *i2v;
2570 const struct GNUNET_TUN_IPv4Header *ipv4;
2571 const struct GNUNET_TUN_IPv6Header *ipv6;
2572 const struct GNUNET_TUN_UdpHeader *udp;
2573 uint16_t source_port;
2574 uint16_t destination_port;
2578 char sbuf[INET6_ADDRSTRLEN];
2579 char dbuf[INET6_ADDRSTRLEN];
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2581 "Received ICMP packet going from %s to %s\n",
2584 sbuf, sizeof (sbuf)),
2587 dbuf, sizeof (dbuf)));
2589 if (pktlen < sizeof (struct GNUNET_TUN_IcmpHeader))
2596 /* Find out if this is an ICMP packet in response to an existing
2597 TCP/UDP packet and if so, figure out ports / protocol of the
2598 existing session from the IP data in the ICMP payload */
2600 destination_port = 0;
2604 protocol = IPPROTO_ICMP;
2607 case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
2608 case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
2610 case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
2611 case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
2612 case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
2614 sizeof (struct GNUNET_TUN_IcmpHeader) +
2615 sizeof (struct GNUNET_TUN_IPv4Header) + 8)
2621 ipv4 = (const struct GNUNET_TUN_IPv4Header *) &icmp[1];
2622 protocol = ipv4->protocol;
2623 /* could be TCP or UDP, but both have the ports in the right
2624 place, so that doesn't matter here */
2625 udp = (const struct GNUNET_TUN_UdpHeader *) &ipv4[1];
2626 /* swap ports, as they are from the original message */
2627 destination_port = ntohs (udp->source_port);
2628 source_port = ntohs (udp->destination_port);
2629 /* throw away ICMP payload, won't be useful for the other side anyway */
2630 pktlen = sizeof (struct GNUNET_TUN_IcmpHeader);
2633 GNUNET_STATISTICS_update (stats,
2634 gettext_noop ("# ICMPv4 packets dropped (type not allowed)"),
2640 protocol = IPPROTO_ICMPV6;
2643 case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
2644 case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
2645 case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
2646 case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
2648 sizeof (struct GNUNET_TUN_IcmpHeader) +
2649 sizeof (struct GNUNET_TUN_IPv6Header) + 8)
2655 ipv6 = (const struct GNUNET_TUN_IPv6Header *) &icmp[1];
2656 protocol = ipv6->next_header;
2657 /* could be TCP or UDP, but both have the ports in the right
2658 place, so that doesn't matter here */
2659 udp = (const struct GNUNET_TUN_UdpHeader *) &ipv6[1];
2660 /* swap ports, as they are from the original message */
2661 destination_port = ntohs (udp->source_port);
2662 source_port = ntohs (udp->destination_port);
2663 /* throw away ICMP payload, won't be useful for the other side anyway */
2664 pktlen = sizeof (struct GNUNET_TUN_IcmpHeader);
2666 case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
2667 case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
2670 GNUNET_STATISTICS_update (stats,
2671 gettext_noop ("# ICMPv6 packets dropped (type not allowed)"),
2682 state = get_redirect_state (af,
2690 case IPPROTO_ICMPV6:
2691 state = get_redirect_state (af,
2700 state = get_redirect_state (af,
2709 state = get_redirect_state (af,
2718 GNUNET_STATISTICS_update (stats,
2719 gettext_noop ("# ICMP packets dropped (not allowed)"),
2726 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2727 _("ICMP Packet dropped, have no matching connection information\n"));
2730 env = GNUNET_MQ_msg_extra (i2v,
2731 pktlen - sizeof (struct GNUNET_TUN_IcmpHeader),
2732 GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_VPN);
2733 i2v->af = htonl (af);
2734 GNUNET_memcpy (&i2v->icmp_header,
2737 send_packet_to_cadet_channel (state,
2743 * @brief Handles an UDP packet received from the helper.
2745 * @param udp A pointer to the Packet
2746 * @param pktlen number of bytes in 'udp'
2747 * @param af address family (AFINET or AF_INET6)
2748 * @param destination_ip destination IP-address of the IP packet (should
2749 * be our local address)
2750 * @param source_ip original source IP-address of the IP packet (should
2751 * be the original destination address)
2754 udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
2757 const void *destination_ip,
2758 const void *source_ip)
2760 struct ChannelState *state;
2761 struct GNUNET_MQ_Envelope *env;
2762 struct GNUNET_EXIT_UdpReplyMessage *urm;
2765 char sbuf[INET6_ADDRSTRLEN];
2766 char dbuf[INET6_ADDRSTRLEN];
2768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2769 "Received UDP packet going from %s:%u to %s:%u\n",
2772 sbuf, sizeof (sbuf)),
2773 (unsigned int) ntohs (udp->source_port),
2776 dbuf, sizeof (dbuf)),
2777 (unsigned int) ntohs (udp->destination_port));
2779 if (pktlen < sizeof (struct GNUNET_TUN_UdpHeader))
2785 if (pktlen != ntohs (udp->len))
2791 state = get_redirect_state (af,
2794 ntohs (udp->source_port),
2796 ntohs (udp->destination_port),
2800 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2801 _("UDP Packet dropped, have no matching connection information\n"));
2804 env = GNUNET_MQ_msg_extra (urm,
2805 pktlen - sizeof (struct GNUNET_TUN_UdpHeader),
2806 GNUNET_MESSAGE_TYPE_VPN_UDP_REPLY);
2807 urm->source_port = htons (0);
2808 urm->destination_port = htons (0);
2809 GNUNET_memcpy (&urm[1],
2811 pktlen - sizeof (struct GNUNET_TUN_UdpHeader));
2812 send_packet_to_cadet_channel (state,
2818 * @brief Handles a TCP packet received from the helper.
2820 * @param tcp A pointer to the Packet
2821 * @param pktlen the length of the packet, including its TCP header
2822 * @param af address family (AFINET or AF_INET6)
2823 * @param destination_ip destination IP-address of the IP packet (should
2824 * be our local address)
2825 * @param source_ip original source IP-address of the IP packet (should
2826 * be the original destination address)
2829 tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
2832 const void *destination_ip,
2833 const void *source_ip)
2835 struct ChannelState *state;
2836 char buf[pktlen] GNUNET_ALIGN;
2837 struct GNUNET_TUN_TcpHeader *mtcp;
2838 struct GNUNET_EXIT_TcpDataMessage *tdm;
2839 struct GNUNET_MQ_Envelope *env;
2843 char sbuf[INET6_ADDRSTRLEN];
2844 char dbuf[INET6_ADDRSTRLEN];
2845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2846 "Received TCP packet with %u bytes going from %s:%u to %s:%u\n",
2847 (unsigned int) (pktlen - sizeof (struct GNUNET_TUN_TcpHeader)),
2850 sbuf, sizeof (sbuf)),
2851 (unsigned int) ntohs (tcp->source_port),
2854 dbuf, sizeof (dbuf)),
2855 (unsigned int) ntohs (tcp->destination_port));
2857 if (pktlen < sizeof (struct GNUNET_TUN_TcpHeader))
2863 state = get_redirect_state (af,
2866 ntohs (tcp->source_port),
2868 ntohs (tcp->destination_port),
2872 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2873 _("TCP Packet dropped, have no matching connection information\n"));
2877 /* mug port numbers and crc to avoid information leakage;
2878 sender will need to lookup the correct values anyway */
2879 GNUNET_memcpy (buf, tcp, pktlen);
2880 mtcp = (struct GNUNET_TUN_TcpHeader *) buf;
2881 mtcp->source_port = 0;
2882 mtcp->destination_port = 0;
2885 mlen = sizeof (struct GNUNET_EXIT_TcpDataMessage) + (pktlen - sizeof (struct GNUNET_TUN_TcpHeader));
2886 if (mlen >= GNUNET_MAX_MESSAGE_SIZE)
2891 env = GNUNET_MQ_msg_extra (tdm,
2892 pktlen - sizeof (struct GNUNET_TUN_TcpHeader),
2893 GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_VPN);
2894 tdm->reserved = htonl (0);
2895 GNUNET_memcpy (&tdm->tcp_header,
2898 send_packet_to_cadet_channel (state,
2904 * Receive packets from the helper-process
2907 * @param message message received from helper
2910 message_token (void *cls GNUNET_UNUSED,
2911 const struct GNUNET_MessageHeader *message)
2913 const struct GNUNET_TUN_Layer2PacketHeader *pkt_tun;
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2917 "Got %u-byte message of type %u from gnunet-helper-exit\n",
2918 ntohs (message->size),
2919 ntohs (message->type));
2920 GNUNET_STATISTICS_update (stats,
2921 gettext_noop ("# Packets received from TUN"),
2923 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
2928 size = ntohs (message->size);
2929 if (size < sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader))
2934 GNUNET_STATISTICS_update (stats,
2935 gettext_noop ("# Bytes received from TUN"),
2937 pkt_tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
2938 size -= sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader);
2939 switch (ntohs (pkt_tun->proto))
2943 const struct GNUNET_TUN_IPv4Header *pkt4;
2945 if (size < sizeof (struct GNUNET_TUN_IPv4Header))
2947 /* Kernel to blame? */
2951 pkt4 = (const struct GNUNET_TUN_IPv4Header *) &pkt_tun[1];
2952 if (size != ntohs (pkt4->total_length))
2954 /* Kernel to blame? */
2958 if (pkt4->header_length * 4 != sizeof (struct GNUNET_TUN_IPv4Header))
2960 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2961 _("IPv4 packet options received. Ignored.\n"));
2965 size -= sizeof (struct GNUNET_TUN_IPv4Header);
2966 switch (pkt4->protocol)
2969 udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt4[1], size,
2971 &pkt4->destination_address,
2972 &pkt4->source_address);
2975 tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt4[1], size,
2977 &pkt4->destination_address,
2978 &pkt4->source_address);
2981 icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt4[1], size,
2983 &pkt4->destination_address,
2984 &pkt4->source_address);
2987 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2988 _("IPv4 packet with unsupported next header %u received. Ignored.\n"),
2989 (int) pkt4->protocol);
2996 const struct GNUNET_TUN_IPv6Header *pkt6;
2998 if (size < sizeof (struct GNUNET_TUN_IPv6Header))
3000 /* Kernel to blame? */
3004 pkt6 = (struct GNUNET_TUN_IPv6Header *) &pkt_tun[1];
3005 if (size != ntohs (pkt6->payload_length) + sizeof (struct GNUNET_TUN_IPv6Header))
3007 /* Kernel to blame? */
3011 size -= sizeof (struct GNUNET_TUN_IPv6Header);
3012 switch (pkt6->next_header)
3015 udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt6[1], size,
3017 &pkt6->destination_address,
3018 &pkt6->source_address);
3021 tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt6[1], size,
3023 &pkt6->destination_address,
3024 &pkt6->source_address);
3026 case IPPROTO_ICMPV6:
3027 icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt6[1], size,
3029 &pkt6->destination_address,
3030 &pkt6->source_address);
3033 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3034 _("IPv6 packet with unsupported next header %d received. Ignored.\n"),
3041 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3042 _("Packet from unknown protocol %u received. Ignored.\n"),
3043 ntohs (pkt_tun->proto));
3051 * Callback from CADET for new channels.
3053 * @param cls closure
3054 * @param channel new handle to the channel
3055 * @param initiator peer that started the channel
3056 * @return initial channel context for the channel
3059 new_channel (void *cls,
3060 struct GNUNET_CADET_Channel *channel,
3061 const struct GNUNET_PeerIdentity *initiator)
3063 struct ChannelState *s = GNUNET_new (struct ChannelState);
3065 s->is_dns = GNUNET_SYSERR;
3066 s->peer = *initiator;
3067 GNUNET_STATISTICS_update (stats,
3068 gettext_noop ("# Inbound CADET channels created"),
3071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3072 "Received inbound channel from `%s'\n",
3073 GNUNET_i2s (initiator));
3074 s->channel = channel;
3080 * Function that frees everything from a hashmap
3084 * @param value value to free
3087 free_iterate (void *cls,
3088 const struct GNUNET_HashCode * hash,
3091 GNUNET_free (value);
3097 * Function scheduled as very last function if the service
3098 * disabled itself because the helper is not installed
3099 * properly. Does nothing, except for keeping the
3100 * service process alive by virtue of being scheduled.
3103 * @param tc scheduler context
3106 dummy_task (void *cls)
3108 /* just terminate */
3113 * Function scheduled as very last function, cleans up after us
3122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3123 "Exit service is shutting down now\n");
3125 if (NULL != helper_handle)
3127 GNUNET_HELPER_stop (helper_handle, GNUNET_NO);
3128 helper_handle = NULL;
3132 GNUNET_REGEX_announce_cancel (regex4);
3137 GNUNET_REGEX_announce_cancel (regex6);
3140 if (NULL != services)
3142 GNUNET_CONTAINER_multihashmap_iterate (services,
3143 &free_service_record,
3145 GNUNET_CONTAINER_multihashmap_destroy (services);
3147 if (NULL != dns_port)
3149 GNUNET_CADET_close_port (dns_port);
3152 if (NULL != cadet_port4)
3154 GNUNET_CADET_close_port (cadet_port4);
3157 if (NULL != cadet_port6)
3159 GNUNET_CADET_close_port (cadet_port6);
3162 if (NULL != cadet_handle)
3164 GNUNET_CADET_disconnect (cadet_handle);
3165 cadet_handle = NULL;
3167 if (NULL != connections_map)
3169 GNUNET_CONTAINER_multihashmap_iterate (connections_map,
3172 GNUNET_CONTAINER_multihashmap_destroy (connections_map);
3173 connections_map = NULL;
3175 if (NULL != connections_heap)
3177 GNUNET_CONTAINER_heap_destroy (connections_heap);
3178 connections_heap = NULL;
3180 if (NULL != dnsstub)
3182 GNUNET_DNSSTUB_stop (dnsstub);
3185 if (NULL != peer_key)
3187 GNUNET_free (peer_key);
3190 if (NULL != dht_task)
3192 GNUNET_SCHEDULER_cancel (dht_task);
3195 if (NULL != dht_put)
3197 GNUNET_DHT_put_cancel (dht_put);
3202 GNUNET_DHT_disconnect (dht);
3207 GNUNET_STATISTICS_destroy (stats,
3212 GNUNET_free_non_null (exit_argv[i]);
3217 * Add services to the service map.
3219 * @param proto IPPROTO_TCP or IPPROTO_UDP
3220 * @param cpy copy of the service descriptor (can be mutilated)
3221 * @param name DNS name of the service
3224 add_services (int proto,
3231 struct LocalService *serv;
3235 slen = strlen (name);
3236 GNUNET_assert (slen >= 8);
3237 n = GNUNET_strndup (name, slen - 8 /* remove .gnunet. */);
3239 for (redirect = strtok (cpy, " ;"); redirect != NULL;
3240 redirect = strtok (NULL, " ;"))
3242 if (NULL == (hostname = strstr (redirect, ":")))
3244 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3245 _("Option `%s' for domain `%s' is not formatted correctly!\n"),
3252 if (NULL == (hostport = strstr (hostname, ":")))
3254 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3255 _("Option `%s' for domain `%s' is not formatted correctly!\n"),
3263 int local_port = atoi (redirect);
3264 int remote_port = atoi (hostport);
3266 if (! ((local_port > 0) && (local_port < 65536)))
3268 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3269 _("`%s' is not a valid port number (for domain `%s')!"),
3274 if (! ((remote_port > 0) && (remote_port < 65536)))
3276 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3277 _("`%s' is not a valid port number (for domain `%s')!"),
3283 serv = GNUNET_new (struct LocalService);
3284 serv->address.proto = proto;
3285 serv->address.port = remote_port;
3286 if (0 == strcmp ("localhost4",
3289 const char *ip4addr = exit_argv[5];
3291 serv->address.af = AF_INET;
3292 GNUNET_assert (1 == inet_pton (AF_INET,
3294 &serv->address.address.ipv4));
3296 else if (0 == strcmp ("localhost6",
3299 const char *ip6addr = exit_argv[3];
3301 serv->address.af = AF_INET6;
3302 GNUNET_assert (1 == inet_pton (AF_INET6,
3304 &serv->address.address.ipv6));
3308 struct addrinfo *res;
3311 ret = getaddrinfo (hostname,
3315 if ( (0 != ret) || (NULL == res) )
3317 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3318 _("No addresses found for hostname `%s' of service `%s'!\n"),
3325 serv->address.af = res->ai_family;
3326 switch (res->ai_family)
3331 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3332 _("Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
3338 serv->address.address.ipv4
3339 = ((struct sockaddr_in *) res->ai_addr)->sin_addr;
3344 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3345 _("Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
3351 serv->address.address.ipv6
3352 = ((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
3356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3357 _("No IP addresses found for hostname `%s' of service `%s'!\n"),
3365 store_service (proto,
3375 * Reads the configuration and populates #udp_services and #tcp_services
3378 * @param section name of section in config
3381 read_service_conf (void *cls,
3382 const char *section)
3386 if ((strlen (section) < 8) ||
3387 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
3390 GNUNET_CONFIGURATION_get_value_string (cfg,
3395 add_services (IPPROTO_UDP,
3401 GNUNET_CONFIGURATION_get_value_string (cfg,
3406 add_services (IPPROTO_TCP,
3415 * We are running a DNS exit service, advertise it in the
3416 * DHT. This task is run periodically to do the DHT PUT.
3418 * @param cls closure
3421 do_dht_put (void *cls);
3425 * Function called when the DHT PUT operation is complete.
3426 * Schedules the next PUT.
3428 * @param cls closure, NULL
3431 dht_put_cont (void *cls)
3438 * We are running a DNS exit service, advertise it in the
3439 * DHT. This task is run periodically to do the DHT PUT.
3441 * @param cls closure
3444 do_dht_put (void *cls)
3446 struct GNUNET_TIME_Absolute expiration;
3448 dht_task = GNUNET_SCHEDULER_add_delayed (DHT_PUT_FREQUENCY,
3451 expiration = GNUNET_TIME_absolute_ntoh (dns_advertisement.expiration_time);
3452 if (GNUNET_TIME_absolute_get_remaining (expiration).rel_value_us <
3453 GNUNET_TIME_UNIT_HOURS.rel_value_us)
3455 /* refresh advertisement */
3456 expiration = GNUNET_TIME_relative_to_absolute (DNS_ADVERTISEMENT_TIMEOUT);
3457 dns_advertisement.expiration_time = GNUNET_TIME_absolute_hton (expiration);
3458 GNUNET_assert (GNUNET_OK ==
3459 GNUNET_CRYPTO_eddsa_sign (peer_key,
3460 &dns_advertisement.purpose,
3461 &dns_advertisement.signature));
3463 if (NULL != dht_put)
3464 GNUNET_DHT_put_cancel (dht_put);
3465 dht_put = GNUNET_DHT_put (dht,
3467 1 /* replication */,
3469 GNUNET_BLOCK_TYPE_DNS,
3470 sizeof (struct GNUNET_DNS_Advertisement),
3479 * Figure out which IP versions we should support (and which
3480 * are supported by the OS) according to our configuration.
3485 ipv4_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg,
3488 ipv6_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg,
3491 ipv4_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg,
3494 ipv6_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg,
3497 if ( (ipv4_exit || ipv4_enabled) &&
3498 GNUNET_OK != GNUNET_NETWORK_test_pf (PF_INET))
3500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3501 _("This system does not support IPv4, will disable IPv4 functions despite them being enabled in the configuration\n"));
3502 ipv4_exit = GNUNET_NO;
3503 ipv4_enabled = GNUNET_NO;
3505 if ( (ipv6_exit || ipv6_enabled) &&
3506 GNUNET_OK != GNUNET_NETWORK_test_pf (PF_INET6))
3508 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3509 _("This system does not support IPv6, will disable IPv6 functions despite them being enabled in the configuration\n"));
3510 ipv6_exit = GNUNET_NO;
3511 ipv6_enabled = GNUNET_NO;
3513 if (ipv4_exit && (! ipv4_enabled))
3515 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3516 _("Cannot enable IPv4 exit but disable IPv4 on TUN interface, will use ENABLE_IPv4=YES\n"));
3517 ipv4_enabled = GNUNET_YES;
3519 if (ipv6_exit && (! ipv6_enabled))
3521 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3522 _("Cannot enable IPv6 exit but disable IPv6 on TUN interface, will use ENABLE_IPv6=YES\n"));
3523 ipv6_enabled = GNUNET_YES;
3529 * Helper function to open the CADET port for DNS exits and to
3530 * advertise the DNS exit (if applicable).
3533 advertise_dns_exit ()
3535 struct GNUNET_MQ_MessageHandler handlers[] = {
3536 GNUNET_MQ_hd_var_size (dns_request,
3537 GNUNET_MESSAGE_TYPE_VPN_DNS_TO_INTERNET,
3538 struct DnsResponseMessage,
3540 GNUNET_MQ_handler_end ()
3543 struct GNUNET_HashCode port;
3546 GNUNET_CONFIGURATION_get_value_yesno (cfg,
3550 GNUNET_assert (NULL != (dnsstub = GNUNET_DNSSTUB_start (128)));
3552 /* TODO: support using multiple DNS resolvers */
3554 GNUNET_CONFIGURATION_get_value_string (cfg,
3559 GNUNET_DNSSTUB_add_dns_ip (dnsstub,
3562 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3565 _("need a valid IPv4 or IPv6 address\n"));
3566 GNUNET_free_non_null (dns_exit);
3570 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER,
3571 strlen (GNUNET_APPLICATION_PORT_INTERNET_RESOLVER),
3573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3574 "Opening CADET port %s for DNS exit service\n",
3575 GNUNET_h2s (&port));
3576 dns_port = GNUNET_CADET_open_port (cadet_handle,
3583 /* advertise exit */
3584 dht = GNUNET_DHT_connect (cfg,
3586 peer_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
3587 GNUNET_CRYPTO_eddsa_key_get_public (peer_key,
3588 &dns_advertisement.peer.public_key);
3589 dns_advertisement.purpose.size = htonl (sizeof (struct GNUNET_DNS_Advertisement) -
3590 sizeof (struct GNUNET_CRYPTO_EddsaSignature));
3591 dns_advertisement.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_DNS_RECORD);
3592 GNUNET_CRYPTO_hash ("dns",
3595 dht_task = GNUNET_SCHEDULER_add_now (&do_dht_put,
3597 GNUNET_free (dns_exit);
3602 * Initialize #exit_argv.
3604 * @return #GNUNET_OK on success, #GNUNET_SYSERR if we should shutdown
3607 setup_exit_helper_args ()
3616 exit_argv[0] = GNUNET_strdup ("exit-gnunet");
3617 if (GNUNET_SYSERR ==
3618 GNUNET_CONFIGURATION_get_value_string (cfg,
3623 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3626 return GNUNET_SYSERR;
3628 exit_argv[1] = tun_ifname;
3631 if (GNUNET_SYSERR ==
3632 GNUNET_CONFIGURATION_get_value_string (cfg,
3637 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3640 return GNUNET_SYSERR;
3642 exit_argv[2] = exit_ifname;
3646 exit_argv[2] = GNUNET_strdup ("-");
3649 if (GNUNET_YES == ipv6_enabled)
3652 if ( (GNUNET_SYSERR ==
3653 GNUNET_CONFIGURATION_get_value_string (cfg,
3657 (1 != inet_pton (AF_INET6,
3661 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3664 GNUNET_free_non_null (ipv6addr);
3665 return GNUNET_SYSERR;
3667 exit_argv[3] = ipv6addr;
3668 if (GNUNET_SYSERR ==
3669 GNUNET_CONFIGURATION_get_value_string (cfg,
3674 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3677 return GNUNET_SYSERR;
3679 exit_argv[4] = ipv6prefix_s;
3681 GNUNET_CONFIGURATION_get_value_number (cfg,
3685 (ipv6prefix >= 127) )
3687 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3690 _("Must be a number"));
3691 return GNUNET_SYSERR;
3696 /* IPv6 explicitly disabled */
3697 exit_argv[3] = GNUNET_strdup ("-");
3698 exit_argv[4] = GNUNET_strdup ("-");
3700 if (GNUNET_YES == ipv4_enabled)
3703 if ( (GNUNET_SYSERR ==
3704 GNUNET_CONFIGURATION_get_value_string (cfg,
3708 (1 != inet_pton (AF_INET,
3712 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3715 GNUNET_free_non_null (ipv4addr);
3716 return GNUNET_SYSERR;
3718 exit_argv[5] = ipv4addr;
3720 if ( (GNUNET_SYSERR ==
3721 GNUNET_CONFIGURATION_get_value_string (cfg,
3725 (1 != inet_pton (AF_INET,
3729 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3732 GNUNET_free_non_null (ipv4mask);
3733 return GNUNET_SYSERR;
3735 exit_argv[6] = ipv4mask;
3739 /* IPv4 explicitly disabled */
3740 exit_argv[5] = GNUNET_strdup ("-");
3741 exit_argv[6] = GNUNET_strdup ("-");
3743 exit_argv[7] = NULL;
3749 * @brief Main function that will be run by the scheduler.
3751 * @param cls closure
3752 * @param args remaining command-line arguments
3753 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
3754 * @param cfg_ configuration
3759 const char *cfgfile,
3760 const struct GNUNET_CONFIGURATION_Handle *cfg_)
3762 struct GNUNET_MQ_MessageHandler handlers[] = {
3763 GNUNET_MQ_hd_var_size (icmp_remote,
3764 GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_INTERNET,
3765 struct GNUNET_EXIT_IcmpInternetMessage,
3767 GNUNET_MQ_hd_var_size (udp_remote,
3768 GNUNET_MESSAGE_TYPE_VPN_UDP_TO_INTERNET,
3769 struct GNUNET_EXIT_UdpInternetMessage,
3771 GNUNET_MQ_hd_var_size (tcp_remote,
3772 GNUNET_MESSAGE_TYPE_VPN_TCP_TO_INTERNET_START,
3773 struct GNUNET_EXIT_TcpInternetStartMessage,
3775 GNUNET_MQ_hd_var_size (tcp_data,
3776 GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_EXIT,
3777 struct GNUNET_EXIT_TcpDataMessage,
3779 GNUNET_MQ_handler_end ()
3781 struct GNUNET_HashCode port;
3785 char *prefixed_regex;
3789 GNUNET_CONFIGURATION_get_value_number (cfg,
3793 max_connections = 1024;
3794 parse_ip_options ();
3795 if ( (ipv4_exit) || (ipv6_exit) )
3797 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
3799 GNUNET_OS_check_helper_binary (binary,
3801 "gnunet-vpn - - - 169.1.3.7 255.255.255.0")) //no nat, ipv4 only
3803 GNUNET_free (binary);
3804 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3805 _("`%s' must be installed SUID, EXIT will not work\n"),
3806 "gnunet-helper-exit");
3807 GNUNET_SCHEDULER_add_shutdown (&dummy_task,
3812 GNUNET_free (binary);
3814 if (! (ipv4_enabled || ipv6_enabled))
3816 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3817 _("No useful service enabled. Exiting.\n"));
3818 GNUNET_SCHEDULER_shutdown ();
3822 GNUNET_SCHEDULER_add_shutdown (&cleanup,
3824 stats = GNUNET_STATISTICS_create ("exit",
3826 cadet_handle = GNUNET_CADET_connect (cfg);
3827 if (NULL == cadet_handle)
3829 GNUNET_SCHEDULER_shutdown ();
3832 advertise_dns_exit ();
3834 setup_exit_helper_args ())
3836 GNUNET_SCHEDULER_shutdown ();
3840 services = GNUNET_CONTAINER_multihashmap_create (65536,
3842 connections_map = GNUNET_CONTAINER_multihashmap_create (65536,
3844 connections_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3845 GNUNET_CONFIGURATION_iterate_sections (cfg,
3849 /* Cadet handle acquired, now open ports and announce regular
3850 expressions matching our exit */
3851 if ( (GNUNET_YES == ipv4_enabled) &&
3852 (GNUNET_YES == ipv4_exit) )
3854 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_IPV4_GATEWAY,
3855 strlen (GNUNET_APPLICATION_PORT_IPV4_GATEWAY),
3857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3858 "Opening CADET port %s for IPv4 gateway service\n",
3859 GNUNET_h2s (&port));
3860 cadet_port4 = GNUNET_CADET_open_port (cadet_handle,
3869 GNUNET_CONFIGURATION_get_value_string (cfg,
3871 "EXIT_RANGE_IPV4_POLICY",
3875 regex = GNUNET_TUN_ipv4policy2regex (policy);
3876 GNUNET_free_non_null (policy);
3879 (void) GNUNET_asprintf (&prefixed_regex,
3881 GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
3883 regex4 = GNUNET_REGEX_announce (cfg,
3885 REGEX_REFRESH_FREQUENCY,
3886 REGEX_MAX_PATH_LEN_IPV4);
3887 GNUNET_free (regex);
3888 GNUNET_free (prefixed_regex);
3892 if ( (GNUNET_YES == ipv6_enabled) && (GNUNET_YES == ipv6_exit) )
3894 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_IPV6_GATEWAY,
3895 strlen (GNUNET_APPLICATION_PORT_IPV6_GATEWAY),
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3898 "Opening CADET port %s for IPv6 gateway service\n",
3899 GNUNET_h2s (&port));
3900 cadet_port6 = GNUNET_CADET_open_port (cadet_handle,
3909 GNUNET_CONFIGURATION_get_value_string (cfg,
3911 "EXIT_RANGE_IPV6_POLICY",
3915 regex = GNUNET_TUN_ipv6policy2regex (policy);
3916 GNUNET_free_non_null (policy);
3919 (void) GNUNET_asprintf (&prefixed_regex,
3921 GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
3923 regex6 = GNUNET_REGEX_announce (cfg,
3925 REGEX_REFRESH_FREQUENCY,
3926 REGEX_MAX_PATH_LEN_IPV6);
3927 GNUNET_free (regex);
3928 GNUNET_free (prefixed_regex);
3931 helper_handle = GNUNET_HELPER_start (GNUNET_NO,
3932 "gnunet-helper-exit",
3943 * @param argc number of arguments from the command line
3944 * @param argv command line arguments
3945 * @return 0 ok, 1 on error
3951 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3952 GNUNET_GETOPT_OPTION_END
3956 GNUNET_STRINGS_get_utf8_args (argc,
3962 return (GNUNET_OK ==
3963 GNUNET_PROGRAM_run (argc,
3965 "gnunet-daemon-exit",
3966 gettext_noop ("Daemon to run to provide an IP exit node for the VPN"),
3969 NULL)) ? global_ret : 1;
3973 /* end of gnunet-daemon-exit.c */