2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file vpn/gnunet-daemon-vpn-helper.c
24 * @author Philipp Toelke
27 #include <gnunet_common.h>
28 #include <gnunet_client_lib.h>
29 #include <gnunet_os_lib.h>
30 #include <gnunet_mesh_service.h>
31 #include <gnunet_protocols.h>
32 #include <gnunet_server_lib.h>
33 #include <gnunet_container_lib.h>
34 #include <block_dns.h>
35 #include <gnunet_configuration_lib.h>
36 #include <gnunet_applications.h>
38 #include "gnunet-daemon-vpn-dns.h"
39 #include "gnunet-daemon-vpn.h"
40 #include "gnunet-daemon-vpn-helper.h"
41 #include "gnunet-service-dns-p.h"
42 #include "gnunet-vpn-packet.h"
43 #include "gnunet-vpn-checksum.h"
44 #include "gnunet-helper-vpn-api.h"
46 struct GNUNET_VPN_HELPER_Handle *helper_handle;
48 extern struct GNUNET_CLIENT_TransmitHandle* dns_transmit_handle;
51 * The tunnels that will be used to send tcp- and udp-packets
53 static struct GNUNET_MESH_Tunnel *tcp_tunnel;
54 static struct GNUNET_MESH_Tunnel *udp_tunnel;
57 * Start the helper-process
59 * If cls != NULL it is assumed that this function is called as a result of a dying
60 * helper. cls is then taken as handle to the old helper and is cleaned up.
64 start_helper_and_schedule (void *cls,
65 const struct GNUNET_SCHEDULER_TaskContext *tc)
67 shs_task = GNUNET_SCHEDULER_NO_TASK;
68 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
82 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IFNAME", &ifname))
84 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
85 "No entry 'IFNAME' in configuration!\n");
90 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR", &ipv6addr))
92 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
93 "No entry 'IPV6ADDR' in configuration!\n");
98 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6PREFIX",
101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
102 "No entry 'IPV6PREFIX' in configuration!\n");
107 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR", &ipv4addr))
109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
110 "No entry 'IPV4ADDR' in configuration!\n");
115 GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4MASK", &ipv4mask))
117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118 "No entry 'IPV4MASK' in configuration!\n");
123 * Messages get passed to the function message_token
124 * When the helper dies, this function will be called again with the
125 * helper_handle as cls.
128 start_helper (ifname, ipv6addr, ipv6prefix, ipv4addr, ipv4mask,
129 "vpn-gnunet", start_helper_and_schedule, message_token,
132 GNUNET_free (ipv6addr);
133 GNUNET_free (ipv6prefix);
134 GNUNET_free (ipv4addr);
135 GNUNET_free (ipv4mask);
136 GNUNET_free (ifname);
138 /* Tell the dns-service to rehijack the dns-port
139 * The routing-table gets flushed if an interface disappears.
142 if (NULL != dns_connection && dns_transmit_handle == NULL)
143 dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection,
144 sizeof (struct GNUNET_MessageHeader),
145 GNUNET_TIME_UNIT_FOREVER_REL,
146 GNUNET_YES, &send_query, NULL);
148 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
149 helper_handle->fh_to_helper, &helper_write,
156 * Send an dns-answer-packet to the helper
159 helper_write (void *cls
160 __attribute__ ((unused)),
161 const struct GNUNET_SCHEDULER_TaskContext *tsdkctx)
163 if (tsdkctx->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN)
166 struct answer_packet_list *ans = answer_proc_head;
171 size_t len = ntohs (ans->pkt.hdr.size);
173 GNUNET_assert (ans->pkt.subtype == GNUNET_DNS_ANSWER_TYPE_IP);
175 GNUNET_assert (20 == sizeof (struct ip_hdr));
176 GNUNET_assert (8 == sizeof (struct udp_pkt));
177 size_t data_len = len - sizeof (struct answer_packet) + 1;
178 size_t net_len = sizeof (struct ip_hdr) + sizeof (struct udp_dns) + data_len;
180 sizeof (struct GNUNET_MessageHeader) + sizeof (struct pkt_tun) + net_len;
182 struct ip_udp_dns *pkt = alloca (pkt_len);
184 GNUNET_assert (pkt != NULL);
185 memset (pkt, 0, pkt_len);
187 /* set the gnunet-header */
188 pkt->shdr.size = htons (pkt_len);
189 pkt->shdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_HELPER);
191 /* set the tun-header (no flags and ethertype of IPv4) */
193 pkt->tun.type = htons (0x0800);
195 /* set the ip-header */
196 pkt->ip_hdr.version = 4;
197 pkt->ip_hdr.hdr_lngth = 5;
198 pkt->ip_hdr.diff_serv = 0;
199 pkt->ip_hdr.tot_lngth = htons (net_len);
200 pkt->ip_hdr.ident = 0;
201 pkt->ip_hdr.flags = 0;
202 pkt->ip_hdr.frag_off = 0;
203 pkt->ip_hdr.ttl = 255;
204 pkt->ip_hdr.proto = IPPROTO_UDP;
205 pkt->ip_hdr.chks = 0; /* Will be calculated later */
206 pkt->ip_hdr.sadr = ans->pkt.from;
207 pkt->ip_hdr.dadr = ans->pkt.to;
209 pkt->ip_hdr.chks = calculate_ip_checksum ((uint16_t *) & pkt->ip_hdr, 5 * 4);
211 /* set the udp-header */
212 pkt->udp_dns.udp_hdr.spt = htons (53);
213 pkt->udp_dns.udp_hdr.dpt = ans->pkt.dst_port;
214 pkt->udp_dns.udp_hdr.len = htons (net_len - sizeof (struct ip_hdr));
215 pkt->udp_dns.udp_hdr.crc = 0; /* Optional for IPv4 */
217 memcpy (&pkt->udp_dns.data, ans->pkt.data, data_len);
219 GNUNET_CONTAINER_DLL_remove (answer_proc_head, answer_proc_tail, ans);
222 if (GNUNET_DISK_file_write (helper_handle->fh_to_helper, pkt, pkt_len) < 0)
224 cleanup_helper (helper_handle);
225 GNUNET_SCHEDULER_add_now (start_helper_and_schedule, NULL);
229 /* if more packets are available, reschedule */
230 if (answer_proc_head != NULL)
231 GNUNET_SCHEDULER_add_write_file (GNUNET_TIME_UNIT_FOREVER_REL,
232 helper_handle->fh_to_helper, &helper_write,
237 * Receive packets from the helper-process
240 message_token (void *cls __attribute__ ((unused)), void *client
241 __attribute__ ((unused)),
242 const struct GNUNET_MessageHeader *message)
244 GNUNET_assert (ntohs (message->type) == GNUNET_MESSAGE_TYPE_VPN_HELPER);
246 struct tun_pkt *pkt_tun = (struct tun_pkt *) message;
247 GNUNET_HashCode *key;
249 /* ethertype is ipv6 */
250 if (ntohs (pkt_tun->tun.type) == 0x86dd)
252 struct ip6_pkt *pkt6 = (struct ip6_pkt *) message;
254 GNUNET_assert (pkt6->ip6_hdr.version == 6);
255 struct ip6_tcp *pkt6_tcp;
256 struct ip6_udp *pkt6_udp;
257 struct ip6_icmp *pkt6_icmp;
259 switch (pkt6->ip6_hdr.nxthdr)
263 pkt6_tcp = (struct ip6_tcp *) pkt6;
264 pkt6_udp = (struct ip6_udp *) pkt6;
266 if ((key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
268 struct map_entry *me = GNUNET_CONTAINER_multihashmap_get (hashmap, key);
270 GNUNET_assert (me != NULL);
274 sizeof (struct GNUNET_MESH_Tunnel *) +
275 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
276 ntohs (pkt6->ip6_hdr.paylgth);
278 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
279 struct GNUNET_MessageHeader *hdr =
280 (struct GNUNET_MessageHeader *) (cls + 1);
281 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
284 htons (sizeof (struct GNUNET_MessageHeader) +
285 sizeof (GNUNET_HashCode) + ntohs (pkt6->ip6_hdr.paylgth));
287 GNUNET_MESH_ApplicationType app_type;
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n",
291 if (me->addrlen == 0)
293 /* This is a mapping to a gnunet-service */
294 memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode));
296 if (IPPROTO_UDP == pkt6->ip6_hdr.nxthdr &&
297 (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)) &&
298 (port_in_ports (me->desc.ports, pkt6_udp->udp_hdr.dpt) ||
299 testBit (me->additional_ports, ntohs (pkt6_udp->udp_hdr.dpt))))
301 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
303 memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
306 else if (IPPROTO_TCP == pkt6->ip6_hdr.nxthdr &&
307 (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
308 && (port_in_ports (me->desc.ports, pkt6_tcp->tcp_hdr.dpt)))
310 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
312 memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth));
317 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "pip: %d\n", port_in_ports(me->desc.ports, pkt6_tcp->tcp_hdr.dpt));
320 if (me->tunnel == NULL && NULL != cls)
323 GNUNET_MESH_peer_request_connect_all (mesh_handle,
324 GNUNET_TIME_UNIT_FOREVER_REL,
327 GNUNET_PeerIdentity *)
329 send_pkt_to_peer, NULL,
333 else if (NULL != cls)
336 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Queued to send IPv6 to peer %x, type %d\n",
339 *((unsigned int *) &me->desc.peer), ntohs (hdr->type));
344 /* This is a mapping to a "real" address */
345 struct remote_addr *s = (struct remote_addr *) hc;
347 s->addrlen = me->addrlen;
348 memcpy (s->addr, me->addr, me->addrlen);
349 s->proto = pkt6->ip6_hdr.nxthdr;
350 if (s->proto == IPPROTO_UDP)
352 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
353 memcpy (hc + 1, &pkt6_udp->udp_hdr, ntohs (pkt6_udp->udp_hdr.len));
354 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
355 if (NULL != udp_tunnel)
356 me->tunnel = udp_tunnel;
358 else if (s->proto == IPPROTO_TCP)
360 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
361 memcpy (hc + 1, &pkt6_tcp->tcp_hdr, ntohs (pkt6->ip6_hdr.paylgth));
362 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
363 if (NULL != tcp_tunnel)
364 me->tunnel = tcp_tunnel;
370 if (me->tunnel == NULL && NULL != cls)
373 GNUNET_MESH_peer_request_connect_by_type (mesh_handle,
374 GNUNET_TIME_UNIT_FOREVER_REL,
379 if (GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY == app_type)
381 else if (GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY == app_type)
384 else if (NULL != cls)
387 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Packet to %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x, which has no mapping\n",
395 pkt6->ip6_hdr.dadr[0], pkt6->ip6_hdr.dadr[1],
396 pkt6->ip6_hdr.dadr[2], pkt6->ip6_hdr.dadr[3],
397 pkt6->ip6_hdr.dadr[4], pkt6->ip6_hdr.dadr[5],
398 pkt6->ip6_hdr.dadr[6], pkt6->ip6_hdr.dadr[7],
399 pkt6->ip6_hdr.dadr[8], pkt6->ip6_hdr.dadr[9],
400 pkt6->ip6_hdr.dadr[10], pkt6->ip6_hdr.dadr[11],
401 pkt6->ip6_hdr.dadr[12], pkt6->ip6_hdr.dadr[13],
402 pkt6->ip6_hdr.dadr[14], pkt6->ip6_hdr.dadr[15]);
407 pkt6_icmp = (struct ip6_icmp *) pkt6;
408 /* If this packet is an icmp-echo-request and a mapping exists, answer */
409 if (pkt6_icmp->icmp_hdr.type == 0x80 &&
410 (key = address6_mapping_exists (pkt6->ip6_hdr.dadr)) != NULL)
413 pkt6_icmp = GNUNET_malloc (ntohs (pkt6->shdr.size));
414 memcpy (pkt6_icmp, pkt6, ntohs (pkt6->shdr.size));
415 GNUNET_SCHEDULER_add_now (&send_icmp6_response, pkt6_icmp);
420 /* ethertype is ipv4 */
421 else if (ntohs (pkt_tun->tun.type) == 0x0800)
423 struct ip_pkt *pkt = (struct ip_pkt *) message;
424 struct ip_udp *udp = (struct ip_udp *) message;
425 struct ip_tcp *pkt_tcp;
426 struct ip_udp *pkt_udp;
427 struct ip_icmp *pkt_icmp;
429 GNUNET_assert (pkt->ip_hdr.version == 4);
431 /* Send dns-packets to the service-dns */
432 if (pkt->ip_hdr.proto == IPPROTO_UDP && ntohs (udp->udp_hdr.dpt) == 53)
434 /* 9 = 8 for the udp-header + 1 for the unsigned char data[1]; */
435 size_t len = sizeof (struct query_packet) + ntohs (udp->udp_hdr.len) - 9;
437 struct query_packet_list *query =
438 GNUNET_malloc (len + 2 * sizeof (struct query_packet_list *));
439 query->pkt.hdr.type = htons (GNUNET_MESSAGE_TYPE_VPN_DNS_LOCAL_QUERY_DNS);
440 query->pkt.hdr.size = htons (len);
441 query->pkt.orig_to = pkt->ip_hdr.dadr;
442 query->pkt.orig_from = pkt->ip_hdr.sadr;
443 query->pkt.src_port = udp->udp_hdr.spt;
444 memcpy (query->pkt.data, udp->data, ntohs (udp->udp_hdr.len) - 8);
446 GNUNET_CONTAINER_DLL_insert_after (head, tail, tail, query);
448 GNUNET_assert (head != NULL);
450 if (dns_connection != NULL && dns_transmit_handle == NULL)
451 dns_transmit_handle = GNUNET_CLIENT_notify_transmit_ready (dns_connection, len,
452 GNUNET_TIME_UNIT_FOREVER_REL,
453 GNUNET_YES, &send_query, NULL);
457 uint32_t dadr = pkt->ip_hdr.dadr;
458 unsigned char *c = (unsigned char *) &dadr;
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Packet to %d.%d.%d.%d, proto %x\n",
461 c[0], c[1], c[2], c[3], pkt->ip_hdr.proto);
462 switch (pkt->ip_hdr.proto)
466 pkt_tcp = (struct ip_tcp *) pkt;
467 pkt_udp = (struct ip_udp *) pkt;
469 if ((key = address4_mapping_exists (dadr)) != NULL)
471 struct map_entry *me =
472 GNUNET_CONTAINER_multihashmap_get (hashmap, key);
473 GNUNET_assert (me != NULL);
477 sizeof (struct GNUNET_MESH_Tunnel *) +
478 sizeof (struct GNUNET_MessageHeader) + sizeof (GNUNET_HashCode) +
479 ntohs (pkt->ip_hdr.tot_lngth) - 4 * pkt->ip_hdr.hdr_lngth;
481 struct GNUNET_MESH_Tunnel **cls = GNUNET_malloc (size);
482 struct GNUNET_MessageHeader *hdr =
483 (struct GNUNET_MessageHeader *) (cls + 1);
484 GNUNET_HashCode *hc = (GNUNET_HashCode *) (hdr + 1);
487 htons (sizeof (struct GNUNET_MessageHeader) +
488 sizeof (GNUNET_HashCode) + ntohs (pkt->ip_hdr.tot_lngth) -
489 4 * pkt->ip_hdr.hdr_lngth);
491 GNUNET_MESH_ApplicationType app_type;
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "me->addrlen is %d\n",
495 if (me->addrlen == 0)
497 /* This is a mapping to a gnunet-service */
498 memcpy (hc, &me->desc.service_descriptor, sizeof (GNUNET_HashCode));
500 if ((IPPROTO_UDP == pkt->ip_hdr.proto) &&
501 (me->desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_UDP)) &&
502 (port_in_ports (me->desc.ports, pkt_udp->udp_hdr.dpt) ||
503 testBit (me->additional_ports, ntohs (pkt_udp->udp_hdr.dpt))))
505 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_UDP);
507 memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len));
510 else if ((IPPROTO_TCP == pkt->ip_hdr.proto) &&
512 desc.service_type & htonl (GNUNET_DNS_SERVICE_TYPE_TCP))
513 && (port_in_ports (me->desc.ports, pkt_tcp->tcp_hdr.dpt)))
515 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_SERVICE_TCP);
517 memcpy (hc + 1, &pkt_tcp->tcp_hdr,
518 ntohs (pkt->ip_hdr.tot_lngth) -
519 4 * pkt->ip_hdr.hdr_lngth);
522 if (me->tunnel == NULL && NULL != cls)
525 GNUNET_MESH_peer_request_connect_all (mesh_handle,
526 GNUNET_TIME_UNIT_FOREVER_REL,
529 GNUNET_PeerIdentity *)
531 send_pkt_to_peer, NULL,
535 else if (NULL != cls)
538 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540 "Queued to send IPv4 to peer %x, type %d\n",
541 *((unsigned int *) &me->desc.peer),
547 /* This is a mapping to a "real" address */
548 struct remote_addr *s = (struct remote_addr *) hc;
550 s->addrlen = me->addrlen;
551 memcpy (s->addr, me->addr, me->addrlen);
552 s->proto = pkt->ip_hdr.proto;
553 if (s->proto == IPPROTO_UDP)
555 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_UDP);
556 memcpy (hc + 1, &pkt_udp->udp_hdr, ntohs (pkt_udp->udp_hdr.len));
557 app_type = GNUNET_APPLICATION_TYPE_INTERNET_UDP_GATEWAY;
559 else if (s->proto == IPPROTO_TCP)
561 hdr->type = htons (GNUNET_MESSAGE_TYPE_VPN_REMOTE_TCP);
562 memcpy (hc + 1, &pkt_tcp->tcp_hdr,
563 ntohs (pkt->ip_hdr.tot_lngth) -
564 4 * pkt->ip_hdr.hdr_lngth);
565 app_type = GNUNET_APPLICATION_TYPE_INTERNET_TCP_GATEWAY;
567 if (me->tunnel == NULL && NULL != cls)
570 GNUNET_MESH_peer_request_connect_by_type (mesh_handle,
571 GNUNET_TIME_UNIT_FOREVER_REL,
577 else if (NULL != cls)
580 send_pkt_to_peer (cls, (struct GNUNET_PeerIdentity *) 1, NULL);
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Packet to %x which has no mapping\n", dadr);
592 pkt_icmp = (struct ip_icmp *) pkt;
593 if (pkt_icmp->icmp_hdr.type == 0x8 &&
594 (key = address4_mapping_exists (dadr)) != NULL)
597 pkt_icmp = GNUNET_malloc (ntohs (pkt->shdr.size));
598 memcpy (pkt_icmp, pkt, ntohs (pkt->shdr.size));
599 GNUNET_SCHEDULER_add_now (&send_icmp4_response, pkt_icmp);
608 write_to_helper (void *buf, size_t len)
610 (void) GNUNET_DISK_file_write (helper_handle->fh_to_helper, buf, len);
614 schedule_helper_write (struct GNUNET_TIME_Relative time, void *cls)
616 if (GNUNET_SCHEDULER_NO_TASK != shs_task)
618 GNUNET_SCHEDULER_add_write_file (time, helper_handle->fh_to_helper,