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 struct 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 unsigned long long 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 (struct GNUNET_HashCode *hash,
329 const struct RedirectInformation *ri)
333 memset (hash, 0, sizeof (struct 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 struct GNUNET_HashCode *state_key)
394 struct RedirectInformation ri;
395 struct GNUNET_HashCode key;
396 struct TunnelState *state;
398 if ( ( (af == AF_INET) && (protocol == IPPROTO_ICMP) ) ||
399 ( (af == AF_INET6) && (protocol == IPPROTO_ICMPV6) ) )
402 destination_port = 0;
405 ri.remote_address.af = af;
407 ri.remote_address.address.ipv4 = *((struct in_addr*) destination_ip);
409 ri.remote_address.address.ipv6 = * ((struct in6_addr*) destination_ip);
410 ri.remote_address.port = destination_port;
411 ri.remote_address.proto = protocol;
412 ri.local_address.af = af;
414 ri.local_address.address.ipv4 = *((struct in_addr*) local_ip);
416 ri.local_address.address.ipv6 = * ((struct in6_addr*) local_ip);
417 ri.local_address.port = local_port;
418 ri.local_address.proto = protocol;
419 hash_redirect_info (&key, &ri);
420 if (NULL != state_key)
422 state = GNUNET_CONTAINER_multihashmap_get (connections_map, &key);
425 /* Mark this connection as freshly used */
426 if (NULL == state_key)
427 GNUNET_CONTAINER_heap_update_cost (connections_heap,
429 GNUNET_TIME_absolute_get ().abs_value);
435 * Given a service descriptor and a destination port, find the
436 * respective service entry.
438 * @param service_map map of services (TCP or UDP)
439 * @param desc service descriptor
440 * @param destination_port destination port
441 * @return NULL if we are not aware of such a service
443 static struct LocalService *
444 find_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
445 const struct GNUNET_HashCode *desc,
446 uint16_t destination_port)
448 char key[sizeof (struct GNUNET_HashCode) + sizeof (uint16_t)];
450 memcpy (&key[0], &destination_port, sizeof (uint16_t));
451 memcpy (&key[sizeof(uint16_t)], desc, sizeof (struct GNUNET_HashCode));
452 return GNUNET_CONTAINER_multihashmap_get (service_map,
453 (struct GNUNET_HashCode *) key);
458 * Free memory associated with a service record.
461 * @param key service descriptor
462 * @param value service record to free
466 free_service_record (void *cls,
467 const struct GNUNET_HashCode *key,
470 struct LocalService *service = value;
472 GNUNET_free_non_null (service->name);
473 GNUNET_free (service);
479 * Given a service descriptor and a destination port, find the
480 * respective service entry.
482 * @param service_map map of services (TCP or UDP)
483 * @param name name of the service
484 * @param destination_port destination port
485 * @param service service information record to store (service->name will be set).
488 store_service (struct GNUNET_CONTAINER_MultiHashMap *service_map,
490 uint16_t destination_port,
491 struct LocalService *service)
493 char key[sizeof (struct GNUNET_HashCode) + sizeof (uint16_t)];
494 struct GNUNET_HashCode desc;
496 GNUNET_CRYPTO_hash (name, strlen (name) + 1, &desc);
497 service->name = GNUNET_strdup (name);
498 memcpy (&key[0], &destination_port, sizeof (uint16_t));
499 memcpy (&key[sizeof(uint16_t)], &desc, sizeof (struct GNUNET_HashCode));
501 GNUNET_CONTAINER_multihashmap_put (service_map,
502 (struct GNUNET_HashCode *) key,
504 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
506 free_service_record (NULL, (struct GNUNET_HashCode *) key, service);
507 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
508 _("Got duplicate service records for `%s:%u'\n"),
510 (unsigned int) destination_port);
516 * MESH is ready to receive a message for the tunnel. Transmit it.
518 * @param cls the 'struct TunnelState'.
519 * @param size number of bytes available in buf
520 * @param buf where to copy the message
521 * @return number of bytes copied to buf
524 send_to_peer_notify_callback (void *cls, size_t size, void *buf)
526 struct TunnelState *s = cls;
527 struct GNUNET_MESH_Tunnel *tunnel = s->tunnel;
528 struct TunnelMessageQueue *tnq;
536 s->th = GNUNET_MESH_notify_transmit_ready (tunnel,
537 GNUNET_NO /* corking */,
539 GNUNET_TIME_UNIT_FOREVER_REL,
542 &send_to_peer_notify_callback,
546 GNUNET_assert (size >= tnq->len);
547 memcpy (buf, tnq->payload, tnq->len);
549 GNUNET_CONTAINER_DLL_remove (s->head,
553 if (NULL != (tnq = s->head))
554 s->th = GNUNET_MESH_notify_transmit_ready (tunnel,
555 GNUNET_NO /* corking */,
557 GNUNET_TIME_UNIT_FOREVER_REL,
560 &send_to_peer_notify_callback,
562 GNUNET_STATISTICS_update (stats,
563 gettext_noop ("# Bytes transmitted via mesh tunnels"),
570 * Send the given packet via the mesh tunnel.
572 * @param mesh_tunnel destination
573 * @param tnq message to queue
576 send_packet_to_mesh_tunnel (struct GNUNET_MESH_Tunnel *mesh_tunnel,
577 struct TunnelMessageQueue *tnq)
579 struct TunnelState *s;
581 s = GNUNET_MESH_tunnel_get_data (mesh_tunnel);
582 GNUNET_assert (NULL != s);
583 GNUNET_CONTAINER_DLL_insert_tail (s->head, s->tail, tnq);
585 s->th = GNUNET_MESH_notify_transmit_ready (mesh_tunnel, GNUNET_NO /* cork */, 0 /* priority */,
586 GNUNET_TIME_UNIT_FOREVER_REL,
588 &send_to_peer_notify_callback,
594 * @brief Handles an ICMP packet received from the helper.
596 * @param icmp A pointer to the Packet
597 * @param pktlen number of bytes in 'icmp'
598 * @param af address family (AFINET or AF_INET6)
599 * @param destination_ip destination IP-address of the IP packet (should
600 * be our local address)
601 * @param source_ip original source IP-address of the IP packet (should
602 * be the original destination address)
605 icmp_from_helper (const struct GNUNET_TUN_IcmpHeader *icmp,
608 const void *destination_ip,
609 const void *source_ip)
611 struct TunnelState *state;
612 struct TunnelMessageQueue *tnq;
613 struct GNUNET_EXIT_IcmpToVPNMessage *i2v;
614 const struct GNUNET_TUN_IPv4Header *ipv4;
615 const struct GNUNET_TUN_IPv6Header *ipv6;
616 const struct GNUNET_TUN_UdpHeader *udp;
618 uint16_t source_port;
619 uint16_t destination_port;
623 char sbuf[INET6_ADDRSTRLEN];
624 char dbuf[INET6_ADDRSTRLEN];
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "Received ICMP packet going from %s to %s\n",
629 sbuf, sizeof (sbuf)),
632 dbuf, sizeof (dbuf)));
634 if (pktlen < sizeof (struct GNUNET_TUN_IcmpHeader))
641 /* Find out if this is an ICMP packet in response to an existing
642 TCP/UDP packet and if so, figure out ports / protocol of the
643 existing session from the IP data in the ICMP payload */
645 destination_port = 0;
649 protocol = IPPROTO_ICMP;
652 case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
653 case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
655 case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
656 case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
657 case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
659 sizeof (struct GNUNET_TUN_IcmpHeader) +
660 sizeof (struct GNUNET_TUN_IPv4Header) + 8)
666 ipv4 = (const struct GNUNET_TUN_IPv4Header *) &icmp[1];
667 protocol = ipv4->protocol;
668 /* could be TCP or UDP, but both have the ports in the right
669 place, so that doesn't matter here */
670 udp = (const struct GNUNET_TUN_UdpHeader *) &ipv4[1];
671 /* swap ports, as they are from the original message */
672 destination_port = ntohs (udp->source_port);
673 source_port = ntohs (udp->destination_port);
674 /* throw away ICMP payload, won't be useful for the other side anyway */
675 pktlen = sizeof (struct GNUNET_TUN_IcmpHeader);
678 GNUNET_STATISTICS_update (stats,
679 gettext_noop ("# ICMPv4 packets dropped (type not allowed)"),
685 protocol = IPPROTO_ICMPV6;
688 case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
689 case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
690 case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
691 case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
693 sizeof (struct GNUNET_TUN_IcmpHeader) +
694 sizeof (struct GNUNET_TUN_IPv6Header) + 8)
700 ipv6 = (const struct GNUNET_TUN_IPv6Header *) &icmp[1];
701 protocol = ipv6->next_header;
702 /* could be TCP or UDP, but both have the ports in the right
703 place, so that doesn't matter here */
704 udp = (const struct GNUNET_TUN_UdpHeader *) &ipv6[1];
705 /* swap ports, as they are from the original message */
706 destination_port = ntohs (udp->source_port);
707 source_port = ntohs (udp->destination_port);
708 /* throw away ICMP payload, won't be useful for the other side anyway */
709 pktlen = sizeof (struct GNUNET_TUN_IcmpHeader);
711 case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
712 case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
715 GNUNET_STATISTICS_update (stats,
716 gettext_noop ("# ICMPv6 packets dropped (type not allowed)"),
727 state = get_redirect_state (af, IPPROTO_ICMP,
733 state = get_redirect_state (af, IPPROTO_ICMPV6,
739 state = get_redirect_state (af, IPPROTO_UDP,
747 state = get_redirect_state (af, IPPROTO_TCP,
755 GNUNET_STATISTICS_update (stats,
756 gettext_noop ("# ICMP packets dropped (not allowed)"),
762 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
763 _("ICMP Packet dropped, have no matching connection information\n"));
766 mlen = sizeof (struct GNUNET_EXIT_IcmpToVPNMessage) + pktlen - sizeof (struct GNUNET_TUN_IcmpHeader);
767 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);
768 tnq->payload = &tnq[1];
770 i2v = (struct GNUNET_EXIT_IcmpToVPNMessage *) &tnq[1];
771 i2v->header.size = htons ((uint16_t) mlen);
772 i2v->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_VPN);
773 i2v->af = htonl (af);
774 memcpy (&i2v->icmp_header,
777 send_packet_to_mesh_tunnel (state->tunnel,
783 * @brief Handles an UDP packet received from the helper.
785 * @param udp A pointer to the Packet
786 * @param pktlen number of bytes in 'udp'
787 * @param af address family (AFINET or AF_INET6)
788 * @param destination_ip destination IP-address of the IP packet (should
789 * be our local address)
790 * @param source_ip original source IP-address of the IP packet (should
791 * be the original destination address)
794 udp_from_helper (const struct GNUNET_TUN_UdpHeader *udp,
797 const void *destination_ip,
798 const void *source_ip)
800 struct TunnelState *state;
801 struct TunnelMessageQueue *tnq;
802 struct GNUNET_EXIT_UdpReplyMessage *urm;
806 char sbuf[INET6_ADDRSTRLEN];
807 char dbuf[INET6_ADDRSTRLEN];
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Received UDP packet going from %s:%u to %s:%u\n",
812 sbuf, sizeof (sbuf)),
813 (unsigned int) ntohs (udp->source_port),
816 dbuf, sizeof (dbuf)),
817 (unsigned int) ntohs (udp->destination_port));
819 if (pktlen < sizeof (struct GNUNET_TUN_UdpHeader))
825 if (pktlen != ntohs (udp->len))
831 state = get_redirect_state (af, IPPROTO_UDP,
833 ntohs (udp->source_port),
835 ntohs (udp->destination_port),
839 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
840 _("UDP Packet dropped, have no matching connection information\n"));
843 mlen = sizeof (struct GNUNET_EXIT_UdpReplyMessage) + pktlen - sizeof (struct GNUNET_TUN_UdpHeader);
844 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);
845 tnq->payload = &tnq[1];
847 urm = (struct GNUNET_EXIT_UdpReplyMessage *) &tnq[1];
848 urm->header.size = htons ((uint16_t) mlen);
849 urm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_UDP_REPLY);
850 urm->source_port = htons (0);
851 urm->destination_port = htons (0);
854 pktlen - sizeof (struct GNUNET_TUN_UdpHeader));
855 send_packet_to_mesh_tunnel (state->tunnel,
861 * @brief Handles a TCP packet received from the helper.
863 * @param tcp A pointer to the Packet
864 * @param pktlen the length of the packet, including its TCP header
865 * @param af address family (AFINET or AF_INET6)
866 * @param destination_ip destination IP-address of the IP packet (should
867 * be our local address)
868 * @param source_ip original source IP-address of the IP packet (should
869 * be the original destination address)
872 tcp_from_helper (const struct GNUNET_TUN_TcpHeader *tcp,
875 const void *destination_ip,
876 const void *source_ip)
878 struct TunnelState *state;
879 char buf[pktlen] GNUNET_ALIGN;
880 struct GNUNET_TUN_TcpHeader *mtcp;
881 struct GNUNET_EXIT_TcpDataMessage *tdm;
882 struct TunnelMessageQueue *tnq;
886 char sbuf[INET6_ADDRSTRLEN];
887 char dbuf[INET6_ADDRSTRLEN];
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "Received TCP packet with %u bytes going from %s:%u to %s:%u\n",
890 pktlen - sizeof (struct GNUNET_TUN_TcpHeader),
893 sbuf, sizeof (sbuf)),
894 (unsigned int) ntohs (tcp->source_port),
897 dbuf, sizeof (dbuf)),
898 (unsigned int) ntohs (tcp->destination_port));
900 if (pktlen < sizeof (struct GNUNET_TUN_TcpHeader))
906 state = get_redirect_state (af, IPPROTO_TCP,
908 ntohs (tcp->source_port),
910 ntohs (tcp->destination_port),
914 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
915 _("TCP Packet dropped, have no matching connection information\n"));
919 /* mug port numbers and crc to avoid information leakage;
920 sender will need to lookup the correct values anyway */
921 memcpy (buf, tcp, pktlen);
922 mtcp = (struct GNUNET_TUN_TcpHeader *) buf;
923 mtcp->source_port = 0;
924 mtcp->destination_port = 0;
927 mlen = sizeof (struct GNUNET_EXIT_TcpDataMessage) + (pktlen - sizeof (struct GNUNET_TUN_TcpHeader));
928 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
934 tnq = GNUNET_malloc (sizeof (struct TunnelMessageQueue) + mlen);
935 tnq->payload = &tnq[1];
937 tdm = (struct GNUNET_EXIT_TcpDataMessage *) &tnq[1];
938 tdm->header.size = htons ((uint16_t) mlen);
939 tdm->header.type = htons (GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_VPN);
940 tdm->reserved = htonl (0);
941 memcpy (&tdm->tcp_header,
944 send_packet_to_mesh_tunnel (state->tunnel,
950 * Receive packets from the helper-process
953 * @param client unsued
954 * @param message message received from helper
957 message_token (void *cls GNUNET_UNUSED, void *client GNUNET_UNUSED,
958 const struct GNUNET_MessageHeader *message)
960 const struct GNUNET_TUN_Layer2PacketHeader *pkt_tun;
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Got %u-byte message of type %u from gnunet-helper-exit\n",
965 ntohs (message->size),
966 ntohs (message->type));
967 GNUNET_STATISTICS_update (stats,
968 gettext_noop ("# Packets received from TUN"),
970 if (ntohs (message->type) != GNUNET_MESSAGE_TYPE_VPN_HELPER)
975 size = ntohs (message->size);
976 if (size < sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader))
981 GNUNET_STATISTICS_update (stats,
982 gettext_noop ("# Bytes received from TUN"),
984 pkt_tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
985 size -= sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_MessageHeader);
986 switch (ntohs (pkt_tun->proto))
990 const struct GNUNET_TUN_IPv4Header *pkt4;
992 if (size < sizeof (struct GNUNET_TUN_IPv4Header))
994 /* Kernel to blame? */
998 pkt4 = (const struct GNUNET_TUN_IPv4Header *) &pkt_tun[1];
999 if (size != ntohs (pkt4->total_length))
1001 /* Kernel to blame? */
1005 if (pkt4->header_length * 4 != sizeof (struct GNUNET_TUN_IPv4Header))
1007 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1008 _("IPv4 packet options received. Ignored.\n"));
1012 size -= sizeof (struct GNUNET_TUN_IPv4Header);
1013 switch (pkt4->protocol)
1016 udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt4[1], size,
1018 &pkt4->destination_address,
1019 &pkt4->source_address);
1022 tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt4[1], size,
1024 &pkt4->destination_address,
1025 &pkt4->source_address);
1028 icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt4[1], size,
1030 &pkt4->destination_address,
1031 &pkt4->source_address);
1034 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1035 _("IPv4 packet with unsupported next header %u received. Ignored.\n"),
1036 (int) pkt4->protocol);
1043 const struct GNUNET_TUN_IPv6Header *pkt6;
1045 if (size < sizeof (struct GNUNET_TUN_IPv6Header))
1047 /* Kernel to blame? */
1051 pkt6 = (struct GNUNET_TUN_IPv6Header *) &pkt_tun[1];
1052 if (size != ntohs (pkt6->payload_length) + sizeof (struct GNUNET_TUN_IPv6Header))
1054 /* Kernel to blame? */
1058 size -= sizeof (struct GNUNET_TUN_IPv6Header);
1059 switch (pkt6->next_header)
1062 udp_from_helper ((const struct GNUNET_TUN_UdpHeader *) &pkt6[1], size,
1064 &pkt6->destination_address,
1065 &pkt6->source_address);
1068 tcp_from_helper ((const struct GNUNET_TUN_TcpHeader *) &pkt6[1], size,
1070 &pkt6->destination_address,
1071 &pkt6->source_address);
1073 case IPPROTO_ICMPV6:
1074 icmp_from_helper ((const struct GNUNET_TUN_IcmpHeader *) &pkt6[1], size,
1076 &pkt6->destination_address,
1077 &pkt6->source_address);
1080 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1081 _("IPv6 packet with unsupported next header %d received. Ignored.\n"),
1088 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1089 _("Packet from unknown protocol %u received. Ignored.\n"),
1090 ntohs (pkt_tun->proto));
1098 * We need to create a (unique) fresh local address (IP+port).
1101 * @param af desired address family
1102 * @param proto desired protocol (IPPROTO_UDP or IPPROTO_TCP)
1103 * @param local_address address to initialize
1106 setup_fresh_address (int af,
1108 struct SocketAddress *local_address)
1110 local_address->af = af;
1111 local_address->proto = (uint8_t) proto;
1112 /* default "local" port range is often 32768--61000,
1113 so we pick a random value in that range */
1114 if ( ( (af == AF_INET) && (proto == IPPROTO_ICMP) ) ||
1115 ( (af == AF_INET6) && (proto == IPPROTO_ICMPV6) ) )
1116 local_address->port = 0;
1119 = (uint16_t) 32768 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1125 struct in_addr addr;
1126 struct in_addr mask;
1129 addr = exit_ipv4addr;
1130 mask = exit_ipv4mask;
1131 if (0 == ~mask.s_addr)
1133 /* only one valid IP anyway */
1134 local_address->address.ipv4 = addr;
1137 /* Given 192.168.0.1/255.255.0.0, we want a mask
1138 of '192.168.255.255', thus: */
1139 mask.s_addr = addr.s_addr | ~mask.s_addr;
1140 /* Pick random IPv4 address within the subnet, except 'addr' or 'mask' itself */
1143 rnd.s_addr = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1145 local_address->address.ipv4.s_addr = (addr.s_addr | rnd.s_addr) & mask.s_addr;
1147 while ( (local_address->address.ipv4.s_addr == addr.s_addr) ||
1148 (local_address->address.ipv4.s_addr == mask.s_addr) );
1153 struct in6_addr addr;
1154 struct in6_addr mask;
1155 struct in6_addr rnd;
1158 addr = exit_ipv6addr;
1159 GNUNET_assert (ipv6prefix < 128);
1160 if (ipv6prefix == 127)
1162 /* only one valid IP anyway */
1163 local_address->address.ipv6 = addr;
1166 /* Given ABCD::/96, we want a mask of 'ABCD::FFFF:FFFF,
1169 for (i=127;i>=ipv6prefix;i--)
1170 mask.s6_addr[i / 8] |= (1 << (i % 8));
1172 /* Pick random IPv6 address within the subnet, except 'addr' or 'mask' itself */
1177 rnd.s6_addr[i] = (unsigned char) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1179 local_address->address.ipv6.s6_addr[i]
1180 = (addr.s6_addr[i] | rnd.s6_addr[i]) & mask.s6_addr[i];
1183 while ( (0 == memcmp (&local_address->address.ipv6,
1185 sizeof (struct in6_addr))) ||
1186 (0 == memcmp (&local_address->address.ipv6,
1188 sizeof (struct in6_addr))) );
1198 * We are starting a fresh connection (TCP or UDP) and need
1199 * to pick a source port and IP address (within the correct
1200 * range and address family) to associate replies with the
1201 * connection / correct mesh tunnel. This function generates
1202 * a "fresh" source IP and source port number for a connection
1203 * After picking a good source address, this function sets up
1204 * the state in the 'connections_map' and 'connections_heap'
1205 * to allow finding the state when needed later. The function
1206 * also makes sure that we remain within memory limits by
1207 * cleaning up 'old' states.
1209 * @param state skeleton state to setup a record for; should
1210 * 'state->ri.remote_address' filled in so that
1211 * this code can determine which AF/protocol is
1212 * going to be used (the 'tunnel' should also
1213 * already be set); after calling this function,
1214 * heap_node and the local_address will be
1215 * also initialized (heap_node != NULL can be
1216 * used to test if a state has been fully setup).
1219 setup_state_record (struct TunnelState *state)
1221 struct GNUNET_HashCode key;
1222 struct TunnelState *s;
1224 /* generate fresh, unique address */
1227 if (NULL == state->serv)
1228 setup_fresh_address (state->ri.remote_address.af,
1229 state->ri.remote_address.proto,
1230 &state->ri.local_address);
1232 setup_fresh_address (state->serv->address.af,
1233 state->serv->address.proto,
1234 &state->ri.local_address);
1235 } while (NULL != get_redirect_state (state->ri.remote_address.af,
1236 state->ri.remote_address.proto,
1237 &state->ri.remote_address.address,
1238 state->ri.remote_address.port,
1239 &state->ri.local_address.address,
1240 state->ri.local_address.port,
1243 char buf[INET6_ADDRSTRLEN];
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1245 "Picked local address %s:%u for new connection\n",
1246 inet_ntop (state->ri.local_address.af,
1247 &state->ri.local_address.address,
1249 (unsigned int) state->ri.local_address.port);
1251 state->state_key = key;
1252 GNUNET_assert (GNUNET_OK ==
1253 GNUNET_CONTAINER_multihashmap_put (connections_map,
1255 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1256 state->heap_node = GNUNET_CONTAINER_heap_insert (connections_heap,
1258 GNUNET_TIME_absolute_get ().abs_value);
1259 while (GNUNET_CONTAINER_heap_get_size (connections_heap) > max_connections)
1261 s = GNUNET_CONTAINER_heap_remove_root (connections_heap);
1262 GNUNET_assert (state != s);
1263 s->heap_node = NULL;
1264 GNUNET_MESH_tunnel_destroy (s->tunnel);
1265 GNUNET_assert (GNUNET_OK ==
1266 GNUNET_CONTAINER_multihashmap_remove (connections_map,
1275 * Prepare an IPv4 packet for transmission via the TUN interface.
1276 * Initializes the IP header and calculates checksums (IP+UDP/TCP).
1277 * For UDP, the UDP header will be fully created, whereas for TCP
1278 * only the ports and checksum will be filled in. So for TCP,
1279 * a skeleton TCP header must be part of the provided payload.
1281 * @param payload payload of the packet (starting with UDP payload or
1282 * TCP header, depending on protocol)
1283 * @param payload_length number of bytes in 'payload'
1284 * @param protocol IPPROTO_UDP or IPPROTO_TCP
1285 * @param tcp_header skeleton of the TCP header, NULL for UDP
1286 * @param src_address source address to use (IP and port)
1287 * @param dst_address destination address to use (IP and port)
1288 * @param pkt4 where to write the assembled packet; must
1289 * contain enough space for the IP header, UDP/TCP header
1293 prepare_ipv4_packet (const void *payload, size_t payload_length,
1295 const struct GNUNET_TUN_TcpHeader *tcp_header,
1296 const struct SocketAddress *src_address,
1297 const struct SocketAddress *dst_address,
1298 struct GNUNET_TUN_IPv4Header *pkt4)
1302 len = payload_length;
1306 len += sizeof (struct GNUNET_TUN_UdpHeader);
1309 len += sizeof (struct GNUNET_TUN_TcpHeader);
1310 GNUNET_assert (NULL != tcp_header);
1316 if (len + sizeof (struct GNUNET_TUN_IPv4Header) > UINT16_MAX)
1322 GNUNET_TUN_initialize_ipv4_header (pkt4,
1325 &src_address->address.ipv4,
1326 &dst_address->address.ipv4);
1331 struct GNUNET_TUN_UdpHeader *pkt4_udp = (struct GNUNET_TUN_UdpHeader *) &pkt4[1];
1333 pkt4_udp->source_port = htons (src_address->port);
1334 pkt4_udp->destination_port = htons (dst_address->port);
1335 pkt4_udp->len = htons ((uint16_t) payload_length);
1336 GNUNET_TUN_calculate_udp4_checksum (pkt4,
1338 payload, payload_length);
1339 memcpy (&pkt4_udp[1], payload, payload_length);
1344 struct GNUNET_TUN_TcpHeader *pkt4_tcp = (struct GNUNET_TUN_TcpHeader *) &pkt4[1];
1346 *pkt4_tcp = *tcp_header;
1347 pkt4_tcp->source_port = htons (src_address->port);
1348 pkt4_tcp->destination_port = htons (dst_address->port);
1349 GNUNET_TUN_calculate_tcp4_checksum (pkt4,
1353 memcpy (&pkt4_tcp[1], payload, payload_length);
1363 * Prepare an IPv6 packet for transmission via the TUN interface.
1364 * Initializes the IP header and calculates checksums (IP+UDP/TCP).
1365 * For UDP, the UDP header will be fully created, whereas for TCP
1366 * only the ports and checksum will be filled in. So for TCP,
1367 * a skeleton TCP header must be part of the provided payload.
1369 * @param payload payload of the packet (starting with UDP payload or
1370 * TCP header, depending on protocol)
1371 * @param payload_length number of bytes in 'payload'
1372 * @param protocol IPPROTO_UDP or IPPROTO_TCP
1373 * @param tcp_header skeleton TCP header data to send, NULL for UDP
1374 * @param src_address source address to use (IP and port)
1375 * @param dst_address destination address to use (IP and port)
1376 * @param pkt6 where to write the assembled packet; must
1377 * contain enough space for the IP header, UDP/TCP header
1381 prepare_ipv6_packet (const void *payload, size_t payload_length,
1383 const struct GNUNET_TUN_TcpHeader *tcp_header,
1384 const struct SocketAddress *src_address,
1385 const struct SocketAddress *dst_address,
1386 struct GNUNET_TUN_IPv6Header *pkt6)
1390 len = payload_length;
1394 len += sizeof (struct GNUNET_TUN_UdpHeader);
1397 len += sizeof (struct GNUNET_TUN_TcpHeader);
1403 if (len > UINT16_MAX)
1409 GNUNET_TUN_initialize_ipv6_header (pkt6,
1412 &src_address->address.ipv6,
1413 &dst_address->address.ipv6);
1419 struct GNUNET_TUN_UdpHeader *pkt6_udp = (struct GNUNET_TUN_UdpHeader *) &pkt6[1];
1421 pkt6_udp->source_port = htons (src_address->port);
1422 pkt6_udp->destination_port = htons (dst_address->port);
1423 pkt6_udp->len = htons ((uint16_t) payload_length);
1424 GNUNET_TUN_calculate_udp6_checksum (pkt6,
1428 memcpy (&pkt6_udp[1], payload, payload_length);
1433 struct GNUNET_TUN_TcpHeader *pkt6_tcp = (struct GNUNET_TUN_TcpHeader *) &pkt6[1];
1435 /* memcpy first here as some TCP header fields are initialized this way! */
1436 *pkt6_tcp = *tcp_header;
1437 pkt6_tcp->source_port = htons (src_address->port);
1438 pkt6_tcp->destination_port = htons (dst_address->port);
1439 GNUNET_TUN_calculate_tcp6_checksum (pkt6,
1443 memcpy (&pkt6_tcp[1], payload, payload_length);
1454 * Send a TCP packet via the TUN interface.
1456 * @param destination_address IP and port to use for the TCP packet's destination
1457 * @param source_address IP and port to use for the TCP packet's source
1458 * @param tcp_header header template to use
1459 * @param payload payload of the TCP packet
1460 * @param payload_length number of bytes in 'payload'
1463 send_tcp_packet_via_tun (const struct SocketAddress *destination_address,
1464 const struct SocketAddress *source_address,
1465 const struct GNUNET_TUN_TcpHeader *tcp_header,
1466 const void *payload, size_t payload_length)
1470 GNUNET_STATISTICS_update (stats,
1471 gettext_noop ("# TCP packets sent via TUN"),
1473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1474 "Sending packet with %u bytes TCP payload via TUN\n",
1475 (unsigned int) payload_length);
1476 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1477 switch (source_address->af)
1480 len += sizeof (struct GNUNET_TUN_IPv4Header);
1483 len += sizeof (struct GNUNET_TUN_IPv6Header);
1489 len += sizeof (struct GNUNET_TUN_TcpHeader);
1490 len += payload_length;
1491 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1497 char buf[len] GNUNET_ALIGN;
1498 struct GNUNET_MessageHeader *hdr;
1499 struct GNUNET_TUN_Layer2PacketHeader *tun;
1501 hdr = (struct GNUNET_MessageHeader *) buf;
1502 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1503 hdr->size = htons (len);
1504 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
1505 tun->flags = htons (0);
1506 switch (source_address->af)
1510 struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
1512 tun->proto = htons (ETH_P_IPV4);
1513 prepare_ipv4_packet (payload, payload_length,
1517 destination_address,
1523 struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
1525 tun->proto = htons (ETH_P_IPV6);
1526 prepare_ipv6_packet (payload, payload_length,
1530 destination_address,
1538 (void) GNUNET_HELPER_send (helper_handle,
1539 (const struct GNUNET_MessageHeader*) buf,
1547 * Process a request via mesh to send a request to a TCP service
1548 * offered by this system.
1550 * @param cls closure, NULL
1551 * @param tunnel connection to the other end
1552 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1553 * @param sender who sent the message
1554 * @param message the actual message
1555 * @param atsi performance data for the connection
1556 * @return GNUNET_OK to keep the connection open,
1557 * GNUNET_SYSERR to close it (signal serious error)
1560 receive_tcp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1561 void **tunnel_ctx GNUNET_UNUSED,
1562 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1563 const struct GNUNET_MessageHeader *message,
1564 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1566 struct TunnelState *state = *tunnel_ctx;
1567 const struct GNUNET_EXIT_TcpServiceStartMessage *start;
1568 uint16_t pkt_len = ntohs (message->size);
1570 GNUNET_STATISTICS_update (stats,
1571 gettext_noop ("# TCP service creation requests received via mesh"),
1573 GNUNET_STATISTICS_update (stats,
1574 gettext_noop ("# Bytes received from MESH"),
1575 pkt_len, GNUNET_NO);
1576 /* check that we got at least a valid header */
1577 if (pkt_len < sizeof (struct GNUNET_EXIT_TcpServiceStartMessage))
1579 GNUNET_break_op (0);
1580 return GNUNET_SYSERR;
1582 start = (const struct GNUNET_EXIT_TcpServiceStartMessage*) message;
1583 pkt_len -= sizeof (struct GNUNET_EXIT_TcpServiceStartMessage);
1584 if ( (NULL == state) ||
1585 (NULL != state->serv) ||
1586 (NULL != state->heap_node) )
1588 GNUNET_break_op (0);
1589 return GNUNET_SYSERR;
1591 if (start->tcp_header.off * 4 < sizeof (struct GNUNET_TUN_TcpHeader))
1593 GNUNET_break_op (0);
1594 return GNUNET_SYSERR;
1596 GNUNET_break_op (ntohl (start->reserved) == 0);
1597 /* setup fresh connection */
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1599 "Received data from %s for forwarding to TCP service %s on port %u\n",
1600 GNUNET_i2s (sender),
1601 GNUNET_h2s (&start->service_descriptor),
1602 (unsigned int) ntohs (start->tcp_header.destination_port));
1603 if (NULL == (state->serv = find_service (tcp_services, &start->service_descriptor,
1604 ntohs (start->tcp_header.destination_port))))
1606 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1607 _("No service found for %s on port %d!\n"),
1609 ntohs (start->tcp_header.destination_port));
1610 GNUNET_STATISTICS_update (stats,
1611 gettext_noop ("# TCP requests dropped (no such service)"),
1613 return GNUNET_SYSERR;
1615 state->ri.remote_address = state->serv->address;
1616 setup_state_record (state);
1617 send_tcp_packet_via_tun (&state->ri.remote_address,
1618 &state->ri.local_address,
1620 &start[1], pkt_len);
1626 * Process a request to forward TCP data to the Internet via this peer.
1628 * @param cls closure, NULL
1629 * @param tunnel connection to the other end
1630 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1631 * @param sender who sent the message
1632 * @param message the actual message
1633 * @param atsi performance data for the connection
1634 * @return GNUNET_OK to keep the connection open,
1635 * GNUNET_SYSERR to close it (signal serious error)
1638 receive_tcp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1639 void **tunnel_ctx GNUNET_UNUSED,
1640 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1641 const struct GNUNET_MessageHeader *message,
1642 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1644 struct TunnelState *state = *tunnel_ctx;
1645 const struct GNUNET_EXIT_TcpInternetStartMessage *start;
1646 uint16_t pkt_len = ntohs (message->size);
1647 const struct in_addr *v4;
1648 const struct in6_addr *v6;
1649 const void *payload;
1652 GNUNET_STATISTICS_update (stats,
1653 gettext_noop ("# Bytes received from MESH"),
1654 pkt_len, GNUNET_NO);
1655 GNUNET_STATISTICS_update (stats,
1656 gettext_noop ("# TCP IP-exit creation requests received via mesh"),
1658 if (pkt_len < sizeof (struct GNUNET_EXIT_TcpInternetStartMessage))
1660 GNUNET_break_op (0);
1661 return GNUNET_SYSERR;
1663 start = (const struct GNUNET_EXIT_TcpInternetStartMessage*) message;
1664 pkt_len -= sizeof (struct GNUNET_EXIT_TcpInternetStartMessage);
1665 if ( (NULL == state) ||
1666 (NULL != state->serv) ||
1667 (NULL != state->heap_node) )
1669 GNUNET_break_op (0);
1670 return GNUNET_SYSERR;
1672 if (start->tcp_header.off * 4 < sizeof (struct GNUNET_TUN_TcpHeader))
1674 GNUNET_break_op (0);
1675 return GNUNET_SYSERR;
1677 af = (int) ntohl (start->af);
1678 state->ri.remote_address.af = af;
1682 if (pkt_len < sizeof (struct in_addr))
1684 GNUNET_break_op (0);
1685 return GNUNET_SYSERR;
1689 GNUNET_break_op (0);
1690 return GNUNET_SYSERR;
1692 v4 = (const struct in_addr*) &start[1];
1694 pkt_len -= sizeof (struct in_addr);
1695 state->ri.remote_address.address.ipv4 = *v4;
1698 if (pkt_len < sizeof (struct in6_addr))
1700 GNUNET_break_op (0);
1701 return GNUNET_SYSERR;
1705 GNUNET_break_op (0);
1706 return GNUNET_SYSERR;
1708 v6 = (const struct in6_addr*) &start[1];
1710 pkt_len -= sizeof (struct in6_addr);
1711 state->ri.remote_address.address.ipv6 = *v6;
1714 GNUNET_break_op (0);
1715 return GNUNET_SYSERR;
1718 char buf[INET6_ADDRSTRLEN];
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1720 "Received data from %s for starting TCP stream to %s:%u\n",
1721 GNUNET_i2s (sender),
1723 &state->ri.remote_address.address,
1725 (unsigned int) ntohs (start->tcp_header.destination_port));
1727 state->ri.remote_address.proto = IPPROTO_TCP;
1728 state->ri.remote_address.port = ntohs (start->tcp_header.destination_port);
1729 setup_state_record (state);
1730 send_tcp_packet_via_tun (&state->ri.remote_address,
1731 &state->ri.local_address,
1739 * Process a request to forward TCP data on an established
1740 * connection via this peer.
1742 * @param cls closure, NULL
1743 * @param tunnel connection to the other end
1744 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1745 * @param sender who sent the message
1746 * @param message the actual message
1747 * @param atsi performance data for the connection
1748 * @return GNUNET_OK to keep the connection open,
1749 * GNUNET_SYSERR to close it (signal serious error)
1752 receive_tcp_data (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1753 void **tunnel_ctx GNUNET_UNUSED,
1754 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1755 const struct GNUNET_MessageHeader *message,
1756 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1758 struct TunnelState *state = *tunnel_ctx;
1759 const struct GNUNET_EXIT_TcpDataMessage *data;
1760 uint16_t pkt_len = ntohs (message->size);
1762 GNUNET_STATISTICS_update (stats,
1763 gettext_noop ("# Bytes received from MESH"),
1764 pkt_len, GNUNET_NO);
1765 GNUNET_STATISTICS_update (stats,
1766 gettext_noop ("# TCP data requests received via mesh"),
1768 if (pkt_len < sizeof (struct GNUNET_EXIT_TcpDataMessage))
1770 GNUNET_break_op (0);
1771 return GNUNET_SYSERR;
1773 data = (const struct GNUNET_EXIT_TcpDataMessage*) message;
1774 pkt_len -= sizeof (struct GNUNET_EXIT_TcpDataMessage);
1775 if ( (NULL == state) ||
1776 (NULL == state->heap_node) )
1778 /* connection should have been up! */
1779 GNUNET_STATISTICS_update (stats,
1780 gettext_noop ("# TCP DATA requests dropped (no session)"),
1782 return GNUNET_SYSERR;
1784 if (data->tcp_header.off * 4 < sizeof (struct GNUNET_TUN_TcpHeader))
1786 GNUNET_break_op (0);
1787 return GNUNET_SYSERR;
1790 GNUNET_break_op (ntohl (data->reserved) == 0);
1792 char buf[INET6_ADDRSTRLEN];
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 "Received additional %u bytes of data from %s for TCP stream to %s:%u\n",
1796 GNUNET_i2s (sender),
1797 inet_ntop (state->ri.remote_address.af,
1798 &state->ri.remote_address.address,
1800 (unsigned int) state->ri.remote_address.port);
1803 send_tcp_packet_via_tun (&state->ri.remote_address,
1804 &state->ri.local_address,
1812 * Send an ICMP packet via the TUN interface.
1814 * @param destination_address IP to use for the ICMP packet's destination
1815 * @param source_address IP to use for the ICMP packet's source
1816 * @param icmp_header ICMP header to send
1817 * @param payload payload of the ICMP packet (does NOT include ICMP header)
1818 * @param payload_length number of bytes of data in payload
1821 send_icmp_packet_via_tun (const struct SocketAddress *destination_address,
1822 const struct SocketAddress *source_address,
1823 const struct GNUNET_TUN_IcmpHeader *icmp_header,
1824 const void *payload, size_t payload_length)
1827 struct GNUNET_TUN_IcmpHeader *icmp;
1829 GNUNET_STATISTICS_update (stats,
1830 gettext_noop ("# ICMP packets sent via TUN"),
1832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1833 "Sending packet with %u bytes ICMP payload via TUN\n",
1834 (unsigned int) payload_length);
1835 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
1836 switch (destination_address->af)
1839 len += sizeof (struct GNUNET_TUN_IPv4Header);
1842 len += sizeof (struct GNUNET_TUN_IPv6Header);
1848 len += sizeof (struct GNUNET_TUN_IcmpHeader);
1849 len += payload_length;
1850 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1856 char buf[len] GNUNET_ALIGN;
1857 struct GNUNET_MessageHeader *hdr;
1858 struct GNUNET_TUN_Layer2PacketHeader *tun;
1860 hdr= (struct GNUNET_MessageHeader *) buf;
1861 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
1862 hdr->size = htons (len);
1863 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
1864 tun->flags = htons (0);
1865 switch (source_address->af)
1869 struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
1871 tun->proto = htons (ETH_P_IPV4);
1872 GNUNET_TUN_initialize_ipv4_header (ipv4,
1874 sizeof (struct GNUNET_TUN_IcmpHeader) + payload_length,
1875 &source_address->address.ipv4,
1876 &destination_address->address.ipv4);
1877 icmp = (struct GNUNET_TUN_IcmpHeader*) &ipv4[1];
1882 struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
1884 tun->proto = htons (ETH_P_IPV6);
1885 GNUNET_TUN_initialize_ipv6_header (ipv6,
1887 sizeof (struct GNUNET_TUN_IcmpHeader) + payload_length,
1888 &source_address->address.ipv6,
1889 &destination_address->address.ipv6);
1890 icmp = (struct GNUNET_TUN_IcmpHeader*) &ipv6[1];
1897 *icmp = *icmp_header;
1901 GNUNET_TUN_calculate_icmp_checksum (icmp,
1904 (void) GNUNET_HELPER_send (helper_handle,
1905 (const struct GNUNET_MessageHeader*) buf,
1913 * Synthesize a plausible ICMP payload for an ICMPv4 error
1914 * response on the given tunnel.
1916 * @param state tunnel information
1917 * @param ipp IPv6 header to fill in (ICMP payload)
1918 * @param udp "UDP" header to fill in (ICMP payload); might actually
1919 * also be the first 8 bytes of the TCP header
1922 make_up_icmpv4_payload (struct TunnelState *state,
1923 struct GNUNET_TUN_IPv4Header *ipp,
1924 struct GNUNET_TUN_UdpHeader *udp)
1926 GNUNET_TUN_initialize_ipv4_header (ipp,
1927 state->ri.remote_address.proto,
1928 sizeof (struct GNUNET_TUN_TcpHeader),
1929 &state->ri.remote_address.address.ipv4,
1930 &state->ri.local_address.address.ipv4);
1931 udp->source_port = htons (state->ri.remote_address.port);
1932 udp->destination_port = htons (state->ri.local_address.port);
1933 udp->len = htons (0);
1934 udp->crc = htons (0);
1939 * Synthesize a plausible ICMP payload for an ICMPv6 error
1940 * response on the given tunnel.
1942 * @param state tunnel information
1943 * @param ipp IPv6 header to fill in (ICMP payload)
1944 * @param udp "UDP" header to fill in (ICMP payload); might actually
1945 * also be the first 8 bytes of the TCP header
1948 make_up_icmpv6_payload (struct TunnelState *state,
1949 struct GNUNET_TUN_IPv6Header *ipp,
1950 struct GNUNET_TUN_UdpHeader *udp)
1952 GNUNET_TUN_initialize_ipv6_header (ipp,
1953 state->ri.remote_address.proto,
1954 sizeof (struct GNUNET_TUN_TcpHeader),
1955 &state->ri.remote_address.address.ipv6,
1956 &state->ri.local_address.address.ipv6);
1957 udp->source_port = htons (state->ri.remote_address.port);
1958 udp->destination_port = htons (state->ri.local_address.port);
1959 udp->len = htons (0);
1960 udp->crc = htons (0);
1965 * Process a request to forward ICMP data to the Internet via this peer.
1967 * @param cls closure, NULL
1968 * @param tunnel connection to the other end
1969 * @param tunnel_ctx pointer to our 'struct TunnelState *'
1970 * @param sender who sent the message
1971 * @param message the actual message
1972 * @param atsi performance data for the connection
1973 * @return GNUNET_OK to keep the connection open,
1974 * GNUNET_SYSERR to close it (signal serious error)
1977 receive_icmp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
1978 void **tunnel_ctx GNUNET_UNUSED,
1979 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
1980 const struct GNUNET_MessageHeader *message,
1981 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
1983 struct TunnelState *state = *tunnel_ctx;
1984 const struct GNUNET_EXIT_IcmpInternetMessage *msg;
1985 uint16_t pkt_len = ntohs (message->size);
1986 const struct in_addr *v4;
1987 const struct in6_addr *v6;
1988 const void *payload;
1989 char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8] GNUNET_ALIGN;
1992 GNUNET_STATISTICS_update (stats,
1993 gettext_noop ("# Bytes received from MESH"),
1994 pkt_len, GNUNET_NO);
1995 GNUNET_STATISTICS_update (stats,
1996 gettext_noop ("# ICMP IP-exit requests received via mesh"),
1998 if (pkt_len < sizeof (struct GNUNET_EXIT_IcmpInternetMessage))
2000 GNUNET_break_op (0);
2001 return GNUNET_SYSERR;
2003 msg = (const struct GNUNET_EXIT_IcmpInternetMessage*) message;
2004 pkt_len -= sizeof (struct GNUNET_EXIT_IcmpInternetMessage);
2006 af = (int) ntohl (msg->af);
2007 if ( (NULL != state->heap_node) &&
2008 (af != state->ri.remote_address.af) )
2010 /* other peer switched AF on this tunnel; not allowed */
2011 GNUNET_break_op (0);
2012 return GNUNET_SYSERR;
2018 if (pkt_len < sizeof (struct in_addr))
2020 GNUNET_break_op (0);
2021 return GNUNET_SYSERR;
2025 GNUNET_break_op (0);
2026 return GNUNET_SYSERR;
2028 v4 = (const struct in_addr*) &msg[1];
2030 pkt_len -= sizeof (struct in_addr);
2031 state->ri.remote_address.address.ipv4 = *v4;
2032 if (NULL == state->heap_node)
2034 state->ri.remote_address.af = af;
2035 state->ri.remote_address.proto = IPPROTO_ICMP;
2036 setup_state_record (state);
2038 /* check that ICMP type is something we want to support
2039 and possibly make up payload! */
2040 switch (msg->icmp_header.type)
2042 case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
2043 case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
2045 case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
2046 case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
2047 case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
2050 GNUNET_break_op (0);
2051 return GNUNET_SYSERR;
2053 /* make up payload */
2055 struct GNUNET_TUN_IPv4Header *ipp = (struct GNUNET_TUN_IPv4Header *) buf;
2056 struct GNUNET_TUN_UdpHeader *udp = (struct GNUNET_TUN_UdpHeader *) &ipp[1];
2058 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2059 pkt_len = sizeof (struct GNUNET_TUN_IPv4Header) + 8;
2060 make_up_icmpv4_payload (state,
2067 GNUNET_break_op (0);
2068 GNUNET_STATISTICS_update (stats,
2069 gettext_noop ("# ICMPv4 packets dropped (type not allowed)"),
2071 return GNUNET_SYSERR;
2076 if (pkt_len < sizeof (struct in6_addr))
2078 GNUNET_break_op (0);
2079 return GNUNET_SYSERR;
2083 GNUNET_break_op (0);
2084 return GNUNET_SYSERR;
2086 v6 = (const struct in6_addr*) &msg[1];
2088 pkt_len -= sizeof (struct in6_addr);
2089 state->ri.remote_address.address.ipv6 = *v6;
2090 if (NULL == state->heap_node)
2092 state->ri.remote_address.af = af;
2093 state->ri.remote_address.proto = IPPROTO_ICMPV6;
2094 setup_state_record (state);
2096 /* check that ICMP type is something we want to support
2097 and possibly make up payload! */
2098 switch (msg->icmp_header.type)
2100 case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
2101 case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
2103 case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
2104 case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
2105 case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
2106 case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
2109 GNUNET_break_op (0);
2110 return GNUNET_SYSERR;
2112 /* make up payload */
2114 struct GNUNET_TUN_IPv6Header *ipp = (struct GNUNET_TUN_IPv6Header *) buf;
2115 struct GNUNET_TUN_UdpHeader *udp = (struct GNUNET_TUN_UdpHeader *) &ipp[1];
2117 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2118 pkt_len = sizeof (struct GNUNET_TUN_IPv6Header) + 8;
2119 make_up_icmpv6_payload (state,
2126 GNUNET_break_op (0);
2127 GNUNET_STATISTICS_update (stats,
2128 gettext_noop ("# ICMPv6 packets dropped (type not allowed)"),
2130 return GNUNET_SYSERR;
2136 GNUNET_break_op (0);
2137 return GNUNET_SYSERR;
2141 char buf[INET6_ADDRSTRLEN];
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2143 "Received ICMP data from %s for forwarding to %s\n",
2144 GNUNET_i2s (sender),
2146 &state->ri.remote_address.address,
2147 buf, sizeof (buf)));
2149 send_icmp_packet_via_tun (&state->ri.remote_address,
2150 &state->ri.local_address,
2158 * Setup ICMP payload for ICMP error messages. Called
2159 * for both IPv4 and IPv6 addresses.
2161 * @param state context for creating the IP Packet
2162 * @param buf where to create the payload, has at least
2163 * sizeof (struct GNUNET_TUN_IPv6Header) + 8 bytes
2164 * @return number of bytes of payload we created in buf
2167 make_up_icmp_service_payload (struct TunnelState *state,
2170 switch (state->serv->address.af)
2174 struct GNUNET_TUN_IPv4Header *ipv4;
2175 struct GNUNET_TUN_UdpHeader *udp;
2177 ipv4 = (struct GNUNET_TUN_IPv4Header *)buf;
2178 udp = (struct GNUNET_TUN_UdpHeader *) &ipv4[1];
2179 make_up_icmpv4_payload (state,
2182 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2183 return sizeof (struct GNUNET_TUN_IPv4Header) + 8;
2188 struct GNUNET_TUN_IPv6Header *ipv6;
2189 struct GNUNET_TUN_UdpHeader *udp;
2191 ipv6 = (struct GNUNET_TUN_IPv6Header *)buf;
2192 udp = (struct GNUNET_TUN_UdpHeader *) &ipv6[1];
2193 make_up_icmpv6_payload (state,
2196 GNUNET_assert (8 == sizeof (struct GNUNET_TUN_UdpHeader));
2197 return sizeof (struct GNUNET_TUN_IPv6Header) + 8;
2208 * Process a request via mesh to send ICMP data to a service
2209 * offered by this system.
2211 * @param cls closure, NULL
2212 * @param tunnel connection to the other end
2213 * @param tunnel_ctx pointer to our 'struct TunnelState *'
2214 * @param sender who sent the message
2215 * @param message the actual message
2216 * @param atsi performance data for the connection
2217 * @return GNUNET_OK to keep the connection open,
2218 * GNUNET_SYSERR to close it (signal serious error)
2221 receive_icmp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
2223 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
2224 const struct GNUNET_MessageHeader *message,
2225 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
2227 struct TunnelState *state = *tunnel_ctx;
2228 const struct GNUNET_EXIT_IcmpServiceMessage *msg;
2229 uint16_t pkt_len = ntohs (message->size);
2230 struct GNUNET_TUN_IcmpHeader icmp;
2231 char buf[sizeof (struct GNUNET_TUN_IPv6Header) + 8] GNUNET_ALIGN;
2232 const void *payload;
2234 GNUNET_STATISTICS_update (stats,
2235 gettext_noop ("# Bytes received from MESH"),
2236 pkt_len, GNUNET_NO);
2237 GNUNET_STATISTICS_update (stats,
2238 gettext_noop ("# ICMP service requests received via mesh"),
2240 /* check that we got at least a valid header */
2241 if (pkt_len < sizeof (struct GNUNET_EXIT_IcmpServiceMessage))
2243 GNUNET_break_op (0);
2244 return GNUNET_SYSERR;
2246 msg = (const struct GNUNET_EXIT_IcmpServiceMessage*) message;
2247 pkt_len -= sizeof (struct GNUNET_EXIT_IcmpServiceMessage);
2248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2249 "Received data from %s for forwarding to ICMP service %s\n",
2250 GNUNET_i2s (sender),
2251 GNUNET_h2s (&msg->service_descriptor));
2252 if (NULL == state->serv)
2254 /* first packet to service must not be ICMP (cannot determine service!) */
2255 GNUNET_break_op (0);
2256 return GNUNET_SYSERR;
2258 icmp = msg->icmp_header;
2260 state->ri.remote_address = state->serv->address;
2261 setup_state_record (state);
2263 /* check that ICMP type is something we want to support,
2264 perform ICMP PT if needed ans possibly make up payload */
2268 switch (msg->icmp_header.type)
2270 case GNUNET_TUN_ICMPTYPE_ECHO_REPLY:
2271 if (state->serv->address.af == AF_INET6)
2272 icmp.type = GNUNET_TUN_ICMPTYPE6_ECHO_REPLY;
2274 case GNUNET_TUN_ICMPTYPE_ECHO_REQUEST:
2275 if (state->serv->address.af == AF_INET6)
2276 icmp.type = GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST;
2278 case GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE:
2279 if (state->serv->address.af == AF_INET6)
2280 icmp.type = GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE;
2283 GNUNET_break_op (0);
2284 return GNUNET_SYSERR;
2287 pkt_len = make_up_icmp_service_payload (state, buf);
2289 case GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED:
2290 if (state->serv->address.af == AF_INET6)
2291 icmp.type = GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED;
2294 GNUNET_break_op (0);
2295 return GNUNET_SYSERR;
2298 pkt_len = make_up_icmp_service_payload (state, buf);
2300 case GNUNET_TUN_ICMPTYPE_SOURCE_QUENCH:
2301 if (state->serv->address.af == AF_INET6)
2303 GNUNET_STATISTICS_update (stats,
2304 gettext_noop ("# ICMPv4 packets dropped (impossible PT to v6)"),
2310 GNUNET_break_op (0);
2311 return GNUNET_SYSERR;
2314 pkt_len = make_up_icmp_service_payload (state, buf);
2317 GNUNET_break_op (0);
2318 GNUNET_STATISTICS_update (stats,
2319 gettext_noop ("# ICMPv4 packets dropped (type not allowed)"),
2321 return GNUNET_SYSERR;
2323 /* end of AF_INET */
2326 switch (msg->icmp_header.type)
2328 case GNUNET_TUN_ICMPTYPE6_ECHO_REPLY:
2329 if (state->serv->address.af == AF_INET)
2330 icmp.type = GNUNET_TUN_ICMPTYPE_ECHO_REPLY;
2332 case GNUNET_TUN_ICMPTYPE6_ECHO_REQUEST:
2333 if (state->serv->address.af == AF_INET)
2334 icmp.type = GNUNET_TUN_ICMPTYPE_ECHO_REQUEST;
2336 case GNUNET_TUN_ICMPTYPE6_DESTINATION_UNREACHABLE:
2337 if (state->serv->address.af == AF_INET)
2338 icmp.type = GNUNET_TUN_ICMPTYPE_DESTINATION_UNREACHABLE;
2341 GNUNET_break_op (0);
2342 return GNUNET_SYSERR;
2345 pkt_len = make_up_icmp_service_payload (state, buf);
2347 case GNUNET_TUN_ICMPTYPE6_TIME_EXCEEDED:
2348 if (state->serv->address.af == AF_INET)
2349 icmp.type = GNUNET_TUN_ICMPTYPE_TIME_EXCEEDED;
2352 GNUNET_break_op (0);
2353 return GNUNET_SYSERR;
2356 pkt_len = make_up_icmp_service_payload (state, buf);
2358 case GNUNET_TUN_ICMPTYPE6_PACKET_TOO_BIG:
2359 case GNUNET_TUN_ICMPTYPE6_PARAMETER_PROBLEM:
2360 if (state->serv->address.af == AF_INET)
2362 GNUNET_STATISTICS_update (stats,
2363 gettext_noop ("# ICMPv6 packets dropped (impossible PT to v4)"),
2369 GNUNET_break_op (0);
2370 return GNUNET_SYSERR;
2373 pkt_len = make_up_icmp_service_payload (state, buf);
2376 GNUNET_break_op (0);
2377 GNUNET_STATISTICS_update (stats,
2378 gettext_noop ("# ICMPv6 packets dropped (type not allowed)"),
2380 return GNUNET_SYSERR;
2382 /* end of AF_INET6 */
2385 GNUNET_break_op (0);
2386 return GNUNET_SYSERR;
2389 send_icmp_packet_via_tun (&state->ri.remote_address,
2390 &state->ri.local_address,
2398 * Send a UDP packet via the TUN interface.
2400 * @param destination_address IP and port to use for the UDP packet's destination
2401 * @param source_address IP and port to use for the UDP packet's source
2402 * @param payload payload of the UDP packet (does NOT include UDP header)
2403 * @param payload_length number of bytes of data in payload
2406 send_udp_packet_via_tun (const struct SocketAddress *destination_address,
2407 const struct SocketAddress *source_address,
2408 const void *payload, size_t payload_length)
2412 GNUNET_STATISTICS_update (stats,
2413 gettext_noop ("# UDP packets sent via TUN"),
2415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2416 "Sending packet with %u bytes UDP payload via TUN\n",
2417 (unsigned int) payload_length);
2418 len = sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader);
2419 switch (source_address->af)
2422 len += sizeof (struct GNUNET_TUN_IPv4Header);
2425 len += sizeof (struct GNUNET_TUN_IPv6Header);
2431 len += sizeof (struct GNUNET_TUN_UdpHeader);
2432 len += payload_length;
2433 if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2439 char buf[len] GNUNET_ALIGN;
2440 struct GNUNET_MessageHeader *hdr;
2441 struct GNUNET_TUN_Layer2PacketHeader *tun;
2443 hdr= (struct GNUNET_MessageHeader *) buf;
2444 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
2445 hdr->size = htons (len);
2446 tun = (struct GNUNET_TUN_Layer2PacketHeader*) &hdr[1];
2447 tun->flags = htons (0);
2448 switch (source_address->af)
2452 struct GNUNET_TUN_IPv4Header * ipv4 = (struct GNUNET_TUN_IPv4Header*) &tun[1];
2454 tun->proto = htons (ETH_P_IPV4);
2455 prepare_ipv4_packet (payload, payload_length,
2459 destination_address,
2465 struct GNUNET_TUN_IPv6Header * ipv6 = (struct GNUNET_TUN_IPv6Header*) &tun[1];
2467 tun->proto = htons (ETH_P_IPV6);
2468 prepare_ipv6_packet (payload, payload_length,
2472 destination_address,
2480 (void) GNUNET_HELPER_send (helper_handle,
2481 (const struct GNUNET_MessageHeader*) buf,
2489 * Process a request to forward UDP data to the Internet via this peer.
2491 * @param cls closure, NULL
2492 * @param tunnel connection to the other end
2493 * @param tunnel_ctx pointer to our 'struct TunnelState *'
2494 * @param sender who sent the message
2495 * @param message the actual message
2496 * @param atsi performance data for the connection
2497 * @return GNUNET_OK to keep the connection open,
2498 * GNUNET_SYSERR to close it (signal serious error)
2501 receive_udp_remote (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
2502 void **tunnel_ctx GNUNET_UNUSED,
2503 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
2504 const struct GNUNET_MessageHeader *message,
2505 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
2507 struct TunnelState *state = *tunnel_ctx;
2508 const struct GNUNET_EXIT_UdpInternetMessage *msg;
2509 uint16_t pkt_len = ntohs (message->size);
2510 const struct in_addr *v4;
2511 const struct in6_addr *v6;
2512 const void *payload;
2515 GNUNET_STATISTICS_update (stats,
2516 gettext_noop ("# Bytes received from MESH"),
2517 pkt_len, GNUNET_NO);
2518 GNUNET_STATISTICS_update (stats,
2519 gettext_noop ("# UDP IP-exit requests received via mesh"),
2521 if (pkt_len < sizeof (struct GNUNET_EXIT_UdpInternetMessage))
2523 GNUNET_break_op (0);
2524 return GNUNET_SYSERR;
2526 msg = (const struct GNUNET_EXIT_UdpInternetMessage*) message;
2527 pkt_len -= sizeof (struct GNUNET_EXIT_UdpInternetMessage);
2528 af = (int) ntohl (msg->af);
2529 state->ri.remote_address.af = af;
2533 if (pkt_len < sizeof (struct in_addr))
2535 GNUNET_break_op (0);
2536 return GNUNET_SYSERR;
2540 GNUNET_break_op (0);
2541 return GNUNET_SYSERR;
2543 v4 = (const struct in_addr*) &msg[1];
2545 pkt_len -= sizeof (struct in_addr);
2546 state->ri.remote_address.address.ipv4 = *v4;
2549 if (pkt_len < sizeof (struct in6_addr))
2551 GNUNET_break_op (0);
2552 return GNUNET_SYSERR;
2556 GNUNET_break_op (0);
2557 return GNUNET_SYSERR;
2559 v6 = (const struct in6_addr*) &msg[1];
2561 pkt_len -= sizeof (struct in6_addr);
2562 state->ri.remote_address.address.ipv6 = *v6;
2565 GNUNET_break_op (0);
2566 return GNUNET_SYSERR;
2569 char buf[INET6_ADDRSTRLEN];
2570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2571 "Received data from %s for forwarding to UDP %s:%u\n",
2572 GNUNET_i2s (sender),
2574 &state->ri.remote_address.address,
2576 (unsigned int) ntohs (msg->destination_port));
2578 state->ri.remote_address.proto = IPPROTO_UDP;
2579 state->ri.remote_address.port = msg->destination_port;
2580 if (NULL == state->heap_node)
2581 setup_state_record (state);
2582 if (0 != ntohs (msg->source_port))
2583 state->ri.local_address.port = msg->source_port;
2584 send_udp_packet_via_tun (&state->ri.remote_address,
2585 &state->ri.local_address,
2592 * Process a request via mesh to send a request to a UDP service
2593 * offered by this system.
2595 * @param cls closure, NULL
2596 * @param tunnel connection to the other end
2597 * @param tunnel_ctx pointer to our 'struct TunnelState *'
2598 * @param sender who sent the message
2599 * @param message the actual message
2600 * @param atsi performance data for the connection
2601 * @return GNUNET_OK to keep the connection open,
2602 * GNUNET_SYSERR to close it (signal serious error)
2605 receive_udp_service (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
2607 const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
2608 const struct GNUNET_MessageHeader *message,
2609 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
2611 struct TunnelState *state = *tunnel_ctx;
2612 const struct GNUNET_EXIT_UdpServiceMessage *msg;
2613 uint16_t pkt_len = ntohs (message->size);
2615 GNUNET_STATISTICS_update (stats,
2616 gettext_noop ("# Bytes received from MESH"),
2617 pkt_len, GNUNET_NO);
2618 GNUNET_STATISTICS_update (stats,
2619 gettext_noop ("# UDP service requests received via mesh"),
2621 /* check that we got at least a valid header */
2622 if (pkt_len < sizeof (struct GNUNET_EXIT_UdpServiceMessage))
2624 GNUNET_break_op (0);
2625 return GNUNET_SYSERR;
2627 msg = (const struct GNUNET_EXIT_UdpServiceMessage*) message;
2628 pkt_len -= sizeof (struct GNUNET_EXIT_UdpServiceMessage);
2629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2630 "Received data from %s for forwarding to UDP service %s on port %u\n",
2631 GNUNET_i2s (sender),
2632 GNUNET_h2s (&msg->service_descriptor),
2633 (unsigned int) ntohs (msg->destination_port));
2634 if (NULL == (state->serv = find_service (udp_services, &msg->service_descriptor,
2635 ntohs (msg->destination_port))))
2637 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2638 _("No service found for %s on port %d!\n"),
2640 ntohs (msg->destination_port));
2641 GNUNET_STATISTICS_update (stats,
2642 gettext_noop ("# UDP requests dropped (no such service)"),
2644 return GNUNET_SYSERR;
2646 state->ri.remote_address = state->serv->address;
2647 setup_state_record (state);
2648 if (0 != ntohs (msg->source_port))
2649 state->ri.local_address.port = msg->source_port;
2650 send_udp_packet_via_tun (&state->ri.remote_address,
2651 &state->ri.local_address,
2658 * Callback from GNUNET_MESH for new tunnels.
2660 * @param cls closure
2661 * @param tunnel new handle to the tunnel
2662 * @param initiator peer that started the tunnel
2663 * @param atsi performance information for the tunnel
2664 * @return initial tunnel context for the tunnel
2667 new_tunnel (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
2668 const struct GNUNET_PeerIdentity *initiator GNUNET_UNUSED,
2669 const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
2671 struct TunnelState *s = GNUNET_malloc (sizeof (struct TunnelState));
2673 GNUNET_STATISTICS_update (stats,
2674 gettext_noop ("# Inbound MESH tunnels created"),
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2677 "Received inbound tunnel from `%s'\n",
2678 GNUNET_i2s (initiator));
2685 * Function called by mesh whenever an inbound tunnel is destroyed.
2686 * Should clean up any associated state.
2688 * @param cls closure (set from GNUNET_MESH_connect)
2689 * @param tunnel connection to the other end (henceforth invalid)
2690 * @param tunnel_ctx place where local state associated
2691 * with the tunnel is stored
2694 clean_tunnel (void *cls GNUNET_UNUSED, const struct GNUNET_MESH_Tunnel *tunnel,
2697 struct TunnelState *s = tunnel_ctx;
2698 struct TunnelMessageQueue *tnq;
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2701 "Tunnel destroyed\n");
2702 while (NULL != (tnq = s->head))
2704 GNUNET_CONTAINER_DLL_remove (s->head,
2709 if (s->heap_node != NULL)
2711 GNUNET_assert (GNUNET_YES ==
2712 GNUNET_CONTAINER_multihashmap_remove (connections_map,
2715 GNUNET_CONTAINER_heap_remove_node (s->heap_node);
2716 s->heap_node = NULL;
2720 GNUNET_MESH_notify_transmit_ready_cancel (s->th);
2728 * Function that frees everything from a hashmap
2732 * @param value value to free
2735 free_iterate (void *cls GNUNET_UNUSED,
2736 const struct GNUNET_HashCode * hash GNUNET_UNUSED, void *value)
2738 GNUNET_free (value);
2744 * Function scheduled as very last function, cleans up after us
2747 cleanup (void *cls GNUNET_UNUSED,
2748 const struct GNUNET_SCHEDULER_TaskContext *tskctx)
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2753 "Exit service is shutting down now\n");
2754 if (helper_handle != NULL)
2756 GNUNET_HELPER_stop (helper_handle);
2757 helper_handle = NULL;
2759 if (mesh_handle != NULL)
2761 GNUNET_MESH_disconnect (mesh_handle);
2764 if (NULL != connections_map)
2766 GNUNET_CONTAINER_multihashmap_iterate (connections_map, &free_iterate, NULL);
2767 GNUNET_CONTAINER_multihashmap_destroy (connections_map);
2768 connections_map = NULL;
2770 if (NULL != connections_heap)
2772 GNUNET_CONTAINER_heap_destroy (connections_heap);
2773 connections_heap = NULL;
2775 if (NULL != tcp_services)
2777 GNUNET_CONTAINER_multihashmap_iterate (tcp_services, &free_service_record, NULL);
2778 GNUNET_CONTAINER_multihashmap_destroy (tcp_services);
2779 tcp_services = NULL;
2781 if (NULL != udp_services)
2783 GNUNET_CONTAINER_multihashmap_iterate (udp_services, &free_service_record, NULL);
2784 GNUNET_CONTAINER_multihashmap_destroy (udp_services);
2785 udp_services = NULL;
2789 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
2793 GNUNET_free_non_null (exit_argv[i]);
2798 * Add services to the service map.
2800 * @param proto IPPROTO_TCP or IPPROTO_UDP
2801 * @param cpy copy of the service descriptor (can be mutilated)
2802 * @param name DNS name of the service
2805 add_services (int proto,
2812 struct LocalService *serv;
2814 for (redirect = strtok (cpy, " "); redirect != NULL;
2815 redirect = strtok (NULL, " "))
2817 if (NULL == (hostname = strstr (redirect, ":")))
2819 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2820 "option `%s' for domain `%s' is not formatted correctly!\n",
2827 if (NULL == (hostport = strstr (hostname, ":")))
2829 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2830 "option `%s' for domain `%s' is not formatted correctly!\n",
2838 int local_port = atoi (redirect);
2839 int remote_port = atoi (hostport);
2841 if (!((local_port > 0) && (local_port < 65536)))
2843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2844 "`%s' is not a valid port number (for domain `%s')!", redirect,
2848 if (!((remote_port > 0) && (remote_port < 65536)))
2850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2851 "`%s' is not a valid port number (for domain `%s')!", hostport,
2856 serv = GNUNET_malloc (sizeof (struct LocalService));
2857 serv->my_port = (uint16_t) local_port;
2858 serv->address.port = remote_port;
2859 if (0 == strcmp ("localhost4", hostname))
2861 const char *ip4addr = exit_argv[4];
2863 serv->address.af = AF_INET;
2864 GNUNET_assert (1 != inet_pton (AF_INET, ip4addr, &serv->address.address.ipv4));
2866 else if (0 == strcmp ("localhost6", hostname))
2868 const char *ip6addr = exit_argv[2];
2870 serv->address.af = AF_INET6;
2871 GNUNET_assert (1 == inet_pton (AF_INET6, ip6addr, &serv->address.address.ipv6));
2875 struct addrinfo *res;
2878 ret = getaddrinfo (hostname, NULL, NULL, &res);
2879 if ( (ret != 0) || (res == NULL) )
2881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2882 _("No addresses found for hostname `%s' of service `%s'!\n"),
2889 serv->address.af = res->ai_family;
2890 switch (res->ai_family)
2895 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2896 _("Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
2902 serv->address.address.ipv4 = ((struct sockaddr_in *) res->ai_addr)->sin_addr;
2907 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2908 _("Service `%s' configured for IPv4, but IPv4 is disabled!\n"),
2914 serv->address.address.ipv6 = ((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
2918 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2919 _("No IP addresses found for hostname `%s' of service `%s'!\n"),
2927 store_service ((IPPROTO_UDP == proto) ? udp_services : tcp_services,
2936 * Reads the configuration servicecfg and populates udp_services
2939 * @param section name of section in config, equal to hostname
2942 read_service_conf (void *cls GNUNET_UNUSED, const char *section)
2946 if ((strlen (section) < 8) ||
2947 (0 != strcmp (".gnunet.", section + (strlen (section) - 8))))
2950 GNUNET_CONFIGURATION_get_value_string (cfg, section, "UDP_REDIRECTS",
2953 add_services (IPPROTO_UDP, cpy, section);
2957 GNUNET_CONFIGURATION_get_value_string (cfg, section, "TCP_REDIRECTS",
2960 add_services (IPPROTO_TCP, cpy, section);
2967 * Test if the given AF is supported by this system.
2970 * @return GNUNET_OK if the AF is supported
2977 s = socket (af, SOCK_STREAM, 0);
2980 if (EAFNOSUPPORT == errno)
2982 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
2984 return GNUNET_SYSERR;
2992 * @brief Main function that will be run by the scheduler.
2994 * @param cls closure
2995 * @param args remaining command-line arguments
2996 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
2997 * @param cfg_ configuration
3000 run (void *cls, char *const *args GNUNET_UNUSED,
3001 const char *cfgfile GNUNET_UNUSED,
3002 const struct GNUNET_CONFIGURATION_Handle *cfg_)
3004 static struct GNUNET_MESH_MessageHandler handlers[] = {
3005 {&receive_icmp_service, GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_SERVICE, 0},
3006 {&receive_icmp_remote, GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_INTERNET, 0},
3007 {&receive_udp_service, GNUNET_MESSAGE_TYPE_VPN_UDP_TO_SERVICE, 0},
3008 {&receive_udp_remote, GNUNET_MESSAGE_TYPE_VPN_UDP_TO_INTERNET, 0},
3009 {&receive_tcp_service, GNUNET_MESSAGE_TYPE_VPN_TCP_TO_SERVICE_START, 0},
3010 {&receive_tcp_remote, GNUNET_MESSAGE_TYPE_VPN_TCP_TO_INTERNET_START, 0},
3011 {&receive_tcp_data, GNUNET_MESSAGE_TYPE_VPN_TCP_DATA_TO_EXIT, 0},
3015 static GNUNET_MESH_ApplicationType apptypes[] = {
3016 GNUNET_APPLICATION_TYPE_END,
3017 GNUNET_APPLICATION_TYPE_END,
3018 GNUNET_APPLICATION_TYPE_END
3020 unsigned int app_idx;
3029 GNUNET_OS_check_helper_binary ("gnunet-helper-exit"))
3031 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3032 _("`%s' must be installed SUID, refusing to run\n"),
3033 "gnunet-helper-exit");
3038 stats = GNUNET_STATISTICS_create ("exit", cfg);
3039 ipv4_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "EXIT_IPV4");
3040 ipv6_exit = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "EXIT_IPV6");
3041 ipv4_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_IPV4");
3042 ipv6_enabled = GNUNET_CONFIGURATION_get_value_yesno (cfg, "exit", "ENABLE_IPV6");
3044 if ( (ipv4_exit || ipv4_enabled) &&
3045 GNUNET_OK != test_af (AF_INET))
3047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3048 _("This system does not support IPv4, will disable IPv4 functions despite them being enabled in the configuration\n"));
3049 ipv4_exit = GNUNET_NO;
3050 ipv4_enabled = GNUNET_NO;
3052 if ( (ipv6_exit || ipv6_enabled) &&
3053 GNUNET_OK != test_af (AF_INET6))
3055 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3056 _("This system does not support IPv6, will disable IPv6 functions despite them being enabled in the configuration\n"));
3057 ipv6_exit = GNUNET_NO;
3058 ipv6_enabled = GNUNET_NO;
3060 if (ipv4_exit && (! ipv4_enabled))
3062 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3063 _("Cannot enable IPv4 exit but disable IPv4 on TUN interface, will use ENABLE_IPv4=YES\n"));
3064 ipv4_enabled = GNUNET_YES;
3066 if (ipv6_exit && (! ipv6_enabled))
3068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3069 _("Cannot enable IPv6 exit but disable IPv6 on TUN interface, will use ENABLE_IPv6=YES\n"));
3070 ipv6_enabled = GNUNET_YES;
3072 if (! (ipv4_enabled || ipv6_enabled))
3074 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3075 _("No useful service enabled. Exiting.\n"));
3076 GNUNET_SCHEDULER_shutdown ();
3080 if (GNUNET_YES == ipv4_exit)
3082 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_IPV4_GATEWAY;
3085 if (GNUNET_YES == ipv6_exit)
3087 apptypes[app_idx] = GNUNET_APPLICATION_TYPE_IPV6_GATEWAY;
3091 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
3094 GNUNET_CONFIGURATION_get_value_number (cfg, "exit", "MAX_CONNECTIONS",
3096 max_connections = 1024;
3097 exit_argv[0] = GNUNET_strdup ("exit-gnunet");
3098 if (GNUNET_SYSERR ==
3099 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "TUN_IFNAME", &tun_ifname))
3101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3102 "No entry 'TUN_IFNAME' in configuration!\n");
3103 GNUNET_SCHEDULER_shutdown ();
3106 exit_argv[1] = tun_ifname;
3109 if (GNUNET_SYSERR ==
3110 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "EXIT_IFNAME", &exit_ifname))
3112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3113 "No entry 'EXIT_IFNAME' in configuration!\n");
3114 GNUNET_SCHEDULER_shutdown ();
3117 exit_argv[2] = exit_ifname;
3121 exit_argv[2] = GNUNET_strdup ("%");
3125 if (GNUNET_YES == ipv6_enabled)
3127 if ( (GNUNET_SYSERR ==
3128 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6ADDR",
3130 (1 != inet_pton (AF_INET6, ipv6addr, &exit_ipv6addr))) )
3132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3133 "No valid entry 'IPV6ADDR' in configuration!\n");
3134 GNUNET_SCHEDULER_shutdown ();
3137 exit_argv[3] = ipv6addr;
3138 if (GNUNET_SYSERR ==
3139 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV6PREFIX",
3142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3143 "No entry 'IPV6PREFIX' in configuration!\n");
3144 GNUNET_SCHEDULER_shutdown ();
3147 exit_argv[4] = ipv6prefix_s;
3149 GNUNET_CONFIGURATION_get_value_number (cfg, "exit",
3152 (ipv6prefix >= 127) )
3154 GNUNET_SCHEDULER_shutdown ();
3160 /* IPv6 explicitly disabled */
3161 exit_argv[3] = GNUNET_strdup ("-");
3162 exit_argv[4] = GNUNET_strdup ("-");
3164 if (GNUNET_YES == ipv4_enabled)
3166 if ( (GNUNET_SYSERR ==
3167 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4ADDR",
3169 (1 != inet_pton (AF_INET, ipv4addr, &exit_ipv4addr))) )
3171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3172 "No valid entry for 'IPV4ADDR' in configuration!\n");
3173 GNUNET_SCHEDULER_shutdown ();
3176 exit_argv[5] = ipv4addr;
3177 if ( (GNUNET_SYSERR ==
3178 GNUNET_CONFIGURATION_get_value_string (cfg, "exit", "IPV4MASK",
3180 (1 != inet_pton (AF_INET, ipv4mask, &exit_ipv4mask))) )
3182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3183 "No valid entry 'IPV4MASK' in configuration!\n");
3184 GNUNET_SCHEDULER_shutdown ();
3187 exit_argv[6] = ipv4mask;
3191 /* IPv4 explicitly disabled */
3192 exit_argv[5] = GNUNET_strdup ("-");
3193 exit_argv[6] = GNUNET_strdup ("-");
3195 exit_argv[7] = NULL;
3197 udp_services = GNUNET_CONTAINER_multihashmap_create (65536);
3198 tcp_services = GNUNET_CONTAINER_multihashmap_create (65536);
3199 GNUNET_CONFIGURATION_iterate_sections (cfg, &read_service_conf, NULL);
3201 connections_map = GNUNET_CONTAINER_multihashmap_create (65536);
3202 connections_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3204 = GNUNET_MESH_connect (cfg, 42 /* queue size */, NULL,
3206 &clean_tunnel, handlers,
3208 if (NULL == mesh_handle)
3210 GNUNET_SCHEDULER_shutdown ();
3213 helper_handle = GNUNET_HELPER_start ("gnunet-helper-exit",
3215 &message_token, NULL);
3222 * @param argc number of arguments from the command line
3223 * @param argv command line arguments
3224 * @return 0 ok, 1 on error
3227 main (int argc, char *const *argv)
3229 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
3230 GNUNET_GETOPT_OPTION_END
3233 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
3236 return (GNUNET_OK ==
3237 GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-exit",
3239 ("Daemon to run to provide an IP exit node for the VPN"),
3240 options, &run, NULL)) ? global_ret : 1;
3244 /* end of gnunet-daemon-exit.c */